Rendered at 22:14:45 GMT+0000 (Coordinated Universal Time) with Cloudflare Workers.
inkyoto 4 days ago [-]
There has been no shortage of query and SQL-like DSL's in different programming languages; however, when it comes to querying, Prolog is heavily underrated.
The interesting property of Prolog is that it needs neither a DSL nor a differentiation between input and output parameters at the level of its relational predicates. Joins, filtering, projection and grouping can all also be expressed using native Prolog constructs.
The same Prolog query can also be run in both directions, including the reverse. That is, if I known the «answer» (i.e. the result), I can ask Prolog to give me an exhaustive set of «questions» that may lead to the «answer».
The first sample query (named «exhibit1») from the blog post can be rewritten in Prolog thusly (SWI-Prolog is used in the example):
Note that it uses the bog standard Prolog syntax. We can now ask to:
1. Retrieve all matching prices and discounts:
?- exhibit1(Price, Discount).
2. Retrieve discounts associated with a price of 200:
?- exhibit1(200, Discount).
3. Retrieve prices associated with a discount of 10%:
?- exhibit1(Price, 1r10).
4. Determine whether a particular price-discount combination exists:
?- exhibit1(200, 1r10).
5. Retrieve prices and discounts subject to additional constraints:
?- { Price > 100, Discount < 1/5 },
exhibit1(Price, Discount).
6. Collect all results into a list:
?- findall(Price-Discount, exhibit1(Price, Discount), Results).
Note how the query itself («exhibit1») does not change and remains the same for all queries.
shakna 4 days ago [-]
> Sadly comparisons like < and == cannot be overloaded in Lua :(
You can overload == in 5.3. Before that, there was a slightly-less-overloadable __eq method. But as of 5.3, as long as the bound __eq method is the same underlying function, it will be called.
I assume you meant instead, that the return value of the comparison is always coerced to a boolean, so you can't re-use it to get arbitrary infix operators?
For that sort of thing, I've used Mike Pall's hack in the past:
local sm = setmetatable
local function infix(f)
local mt = { __sub = function(self, b) return f(self[1], b) end }
return sm({}, { __sub = function(a, _) return sm({ a }, mt) end })
end
local shl = infix(function(a, b) return a*(2^b) end)
print(5 -shl- 4)
remywang 4 days ago [-]
Nice! Yes I meant the type is coerced to bool. For me the a:eq(b) syntax is already good enough and less hacky
phillc73 4 days ago [-]
A little while ago I had a similar itch, but my query language at home happened to be Janet[1].
It's built over DuckDB and Rayforce[2]. Single query, either engine.
The SQL is more readable for me, but I am very used to looking at that.
PinkSheep 4 days ago [-]
Tarantool[1] has been using Lua for the queries since inception. Eventually 3rd party users complained enough that they added a SQL as a query frontend.
Very nice, Lua is great for creating small DSLs. Honestly I wish Lua was more mainstream as an embedded language.
flincode 2 days ago [-]
isn't it already THE MAINSTREAM embedded language.
iLemming 3 days ago [-]
Don't ever try Fennel. In general, don't ever try Lisp dialects. You will be disappointed seeing where everyone else has been moving. It would feel as if the industry at large chooses to stay foolish, for the sake of questionable gains.
CyberDildonics 3 days ago [-]
People try this stuff all the time, they don't want to have to read s-expressions.
iLemming 3 days ago [-]
And that's the silliest, most foolish justification ever. It's not like s-expressions make things unequivocally cryptic. If anything, things get far more readable - this can be observed by simply grabbing a small piece of JSON and converting it to idiomatic EDN - it's almost twice as compact, there's no redundant noise, commas are optional - things can be aligned and read in tabular form.
CyberDildonics 3 days ago [-]
> And that's the silliest, most foolish justification ever.
It's not a justification, it's just reality. You not accepting it doesn't change anything. People don't write like yoda and they don't want to read expressions with an order that they don't think is natural.
This is just how it is. The sooner you come to grips with it the sooner you can move on and join everyone else in focusing on the big picture.
iLemming 2 days ago [-]
> in focusing on the big picture
Oh, I have the big picture. I see it; I've been staring at it for years. You confuse my enthusiasm for sentimentality. In fact, it's driven by pure pragmatism. I almost never choose a tool for its aesthetics, rarely for ergonomics - I use things for their utility and practical angle. I have zero problem with using Lua as is in a team setting, which doesn't happen a lot. Lua runtimes are typically used for personal scripting, thus allowing some flexibility for spicing things up. Fennel comes in enormously handy here. You probably have no idea, because you can't see past the common horizon.
> it's just reality
Yes, it is. The crowd has no wisdom, and as history shows us over and over again, popular choices are not only not always ideal - most of the time they are outright the wrong pick. I'm very grateful for learning to be a well-grounded pragmatist. I'd never dismiss a tool, a philosophy or a trick that may look unnatural without first understanding the idea. Sometimes, looking past the surface reveals shortcuts worth knowing.
CyberDildonics 2 days ago [-]
Seems like a self righteous rationalization that most people don't want what you like.
Syntax is immediate and visible. It isn't a technical decision that takes months or years to materialize like the architecture of a long term program. People go with what they like and move on.
iLemming 2 days ago [-]
I feel there's a concept you have missed when studying things required for the field you are currently in. It is called "trade-offs". I suggest you grok this concept; it ain't that hard.
CyberDildonics 2 days ago [-]
Now you're into the insults after running out of real information to talk about.
iLemming 2 days ago [-]
Well, you gave me no option. What am I supposed to do when you're outright rejecting any attempt at a constructive dialog? The subject was DSLs, for which Lisp dialects are famous - low cost, one of the best pragmatic choices known for decades. That is simply factually true. Sure, like any other solution, they have their own share of trade-offs, yet, properly used, they are completely worth every penny.
Fennel specifically hosted on top of Lua is a fantastic tool. There are tons of interesting things one can do in a way that's just more difficult otherwise.
CyberDildonics 2 days ago [-]
> Well, you gave me no option.
Now you're rationalizing your bad behavior. Instead of using real information and explanations you're pretending that you resorting to insults is because someone else made you do it. Take responsibility for your words and actions.
> rejecting any attempt at a constructive dialog?
Explaining reality isn't "rejecting constructive dialog" it just isn't what you want to hear.
stevefan1999 3 days ago [-]
Any programming language can be a query language by turning into DSL, especially with a Datalog style interface. See https://github.com/ekzhang/crepe/tree/main but I have a fork for it to replace the underlying data structures.
ianm218 4 days ago [-]
Since the author mentioned Rust and Lua I've bee chipping away at OmniLua for awhile https://github.com/ianm199/omnilua a project that bundles Lua 5.1-5.5 in one Rust project. Might be useful for this kind of thing!
lenkite 4 days ago [-]
> Overall the queries came out very nicely, many of them even easier to read than the original SQL!
Really ? I found the SQL far easier to read at a single glance compared to the special Lua syntax that I had to pause, focus and think on to grok.
steve_taylor 4 days ago [-]
Not much is really gained here in terms of shifting query validation from runtime to build time as Lua is dynamically typed.
behnamoh 4 days ago [-]
You can do this with Ruby and Elixir too, and the syntax would end up much nicer!
remywang 4 days ago [-]
Lua is nice because it can be embedded anywhere with very little overhead, but in my experience it’s very easy (and fun) to port Prela to most languages
sumnole 4 days ago [-]
Well, that's a matter of opinion. I find Lua syntax to be quite nice already.
Rohansi 4 days ago [-]
This is almost what C# does with LINQ to SQL, etc. Very cool hack.
Tinqerjs adopts LINQ's expression-tree based approach. It's type-safe, and I suppose so is kysely.
harrykris 4 days ago [-]
once i used lua to rice linux, the language was fun to use
"it's not really a query langauge. It's just a bunch of Rust types and methods that give the feel of a query language" I FEEL THIS LMAO
nice project anyways love it
MomsAVoxell 3 days ago [-]
> used lua to rice linux
I often fantasize of a Linux distro that is pure Lua, like .. for everything, and literally nothing in the userland/config realm is anything but Lua, so even the interfaces to a GUI and so on, no other scripting languages, just full commit to Lua only. The ultimate rice.
The interesting property of Prolog is that it needs neither a DSL nor a differentiation between input and output parameters at the level of its relational predicates. Joins, filtering, projection and grouping can all also be expressed using native Prolog constructs.
The same Prolog query can also be run in both directions, including the reverse. That is, if I known the «answer» (i.e. the result), I can ask Prolog to give me an exhaustive set of «questions» that may lead to the «answer».
The first sample query (named «exhibit1») from the blog post can be rewritten in Prolog thusly (SWI-Prolog is used in the example):
Note that it uses the bog standard Prolog syntax. We can now ask to: Note how the query itself («exhibit1») does not change and remains the same for all queries.You can overload == in 5.3. Before that, there was a slightly-less-overloadable __eq method. But as of 5.3, as long as the bound __eq method is the same underlying function, it will be called.
I assume you meant instead, that the return value of the comparison is always coerced to a boolean, so you can't re-use it to get arbitrary infix operators?
For that sort of thing, I've used Mike Pall's hack in the past:
It's built over DuckDB and Rayforce[2]. Single query, either engine.
[1] https://github.com/jennystats/janet-query
[2] https://github.com/RayforceDB/rayforce
[1]: https://en.wikipedia.org/wiki/Tarantool
It's not a justification, it's just reality. You not accepting it doesn't change anything. People don't write like yoda and they don't want to read expressions with an order that they don't think is natural.
This is just how it is. The sooner you come to grips with it the sooner you can move on and join everyone else in focusing on the big picture.
Oh, I have the big picture. I see it; I've been staring at it for years. You confuse my enthusiasm for sentimentality. In fact, it's driven by pure pragmatism. I almost never choose a tool for its aesthetics, rarely for ergonomics - I use things for their utility and practical angle. I have zero problem with using Lua as is in a team setting, which doesn't happen a lot. Lua runtimes are typically used for personal scripting, thus allowing some flexibility for spicing things up. Fennel comes in enormously handy here. You probably have no idea, because you can't see past the common horizon.
> it's just reality
Yes, it is. The crowd has no wisdom, and as history shows us over and over again, popular choices are not only not always ideal - most of the time they are outright the wrong pick. I'm very grateful for learning to be a well-grounded pragmatist. I'd never dismiss a tool, a philosophy or a trick that may look unnatural without first understanding the idea. Sometimes, looking past the surface reveals shortcuts worth knowing.
Syntax is immediate and visible. It isn't a technical decision that takes months or years to materialize like the architecture of a long term program. People go with what they like and move on.
Fennel specifically hosted on top of Lua is a fantastic tool. There are tons of interesting things one can do in a way that's just more difficult otherwise.
Now you're rationalizing your bad behavior. Instead of using real information and explanations you're pretending that you resorting to insults is because someone else made you do it. Take responsibility for your words and actions.
> rejecting any attempt at a constructive dialog?
Explaining reality isn't "rejecting constructive dialog" it just isn't what you want to hear.
Really ? I found the SQL far easier to read at a single glance compared to the special Lua syntax that I had to pause, focus and think on to grok.
By now battle-tested in production.
"it's not really a query langauge. It's just a bunch of Rust types and methods that give the feel of a query language" I FEEL THIS LMAO
nice project anyways love it
I often fantasize of a Linux distro that is pure Lua, like .. for everything, and literally nothing in the userland/config realm is anything but Lua, so even the interfaces to a GUI and so on, no other scripting languages, just full commit to Lua only. The ultimate rice.