r/ProgrammingLanguages • u/smthamazing • Jun 14 '26
Discussion Why does Flix embed Datalog, specifically?
Or, in other words: what flavors of constraint solvers have their place in the standard library of a language, or even in the core language itself?
I've been following Flix for a few years. It's a fun language with effect tracking and other interesting ideas, but the most unusual part is embedding Datalog into the core language (example from the main page):
def reachable(g: List[(String, Int32, String)], minSpeed: Int32): List[(String, String)] =
let facts = inject g into Road/3;
let rules = #{
Path(x, y) :- Road(x, maxSpeed, y), if maxSpeed >= minSpeed.
Path(x, z) :- Path(x, y), Road(y, maxSpeed, z), if maxSpeed >= minSpeed.
};
query facts, rules select (src, dst) from Path(src, dst) |> Foldable.toListIt definitely works there and is well-integrated. The type checker statically confirms that Datalog programs that you write make sense, which is probably more than a library-level integration could achieve.
But I'm wondering: why Datalog, specifically? It's a rather specific point in the whole space of constraint solvers. I'm not an expert on this, but I think Datalog's main thing is deriving all possible facts from the inputs. It's good for certain problems on finite domains, like finding cycles in graphs or finding transitive dependencies in a tree, but completely useless for others, like proving facts about list concatenation. For that latter problem Prolog works better, since it can actually express facts about abstract lists. And there are many other tools in the similar space of constraint solving: miniKanren, Gecode, Z3, etc. I personally use Z3 a lot for game design problems, like balancing progression curves and ensuring my values can never leave specified ranges.
So my question is actually twofold:
- Is Datalog just a specific interest of the Flix team, or is there a more fundamental reason for integrating it into the core language, compared to other technologies like Prolog?
- Is such a thing actually valuable in a general-purpose language? On the one hand, I think constraint solving falls into the bucket of common practical tools like collections or regular expressions. On the other hand, I'm not aware of any clear "winner" implementation that makes sense for everything: simpler systems like miniKanren and Datalog are quite limited, while more complex ones like Z3 are amalgamations of many different theories matched to specific problems: booleans and SAT, integers, arrays, etc are all handled by different mechanisms. They may not terminate in reasonable time on many inputs. This could mean that logic programming and constraint solving is better left for the library ecosystem and not std.
Any thoughts are welcome!
5
u/Potterrrrrrrr Jun 14 '26
Would you consider “subsumption analysis” to be a form of constraint solving? C++ has these things called concepts which are compile time constraints on what a given T’s type can be. Subsumption analysis is when you have multiple overloads available for a given T, the compiler calculates “the most constrained overload” based on all the concepts in play and chooses that. Would this be considered constraint solving in the way you’re talking about here?
I’m not sure how I feel about what you’ve mentioned, I’ve never knowingly used a language that does that but C++’s concepts are my favourite feature of any language I’ve used and it seems semi adjacent to this.
2
u/SnooGoats8463 Jun 15 '26
many language's compiles include a datalog engine, see https://en.wikipedia.org/wiki/Datalog#Datalog_engines
20
u/initial-algebra Jun 14 '26 edited Jun 14 '26
I believe it's because the authors come from a program analysis background, where Datalog is a very standard tool.
Maybe not the program analysis part, but Datalog is also basically equivalent to SQL, or rather it's a far more principled realization of relational algebra + fixed points (recursive CTEs in SQL parlance). That is obviously a very useful feature for a programming language to have, at least in a hypothetical universe where an enterprise Datalog RDBMS exists...
I'd say there is room for both Datalog-like and Prolog-like features, though. Datalog embodies bottom-up computation (which actually appears all the time, but is pretty much always implemented ad hoc when needed), whereas Prolog extends pattern matching to unification (backtracking too, but it's pretty easy to do backtracking already). Both are very useful features that are not well supported by most "normal" programming languages, and they're actually fairly orthogonal (tabling in Prolog is just adding a bit of Datalog, although constructors and unification allow non-termination).
For constraint solving, once you have equality saturation and e-matching (which can be seen as an extension of Datalog, called "Egglog"), I don't think special support is needed. Specific theories can be implemented as libraries.
10
u/thehenkan Jun 14 '26
I would imagine that it not being Turing complete makes it easier to avoid accidental performance footguns, while also having a better debugging story than something like Z3. Since it's already embedded into a full-blown general purpose language, it's fine if it doesn't tackle every type of problem but instead focuses on tackling one type of problem very well.
7
u/neel-krishnaswami Jun 16 '26
Datalog hits a very good sweet spot in terms of balancing expressiveness and performance.
So 1 means programmers will find it useful, 2 means they can't easily shoot themselves in the foot with it, and 3 means that you can benefit from moving it from library to language.
7
u/jorkadeen Jun 19 '26
I think there are several answers:
Functional programming is exceptional for programming with trees. It is simply a pure joy to write small recursive functions using pattern matching over lists, trees, etc. But once we move to graphs, transitive closure, and fixpoints, recursion quickly breaks down and becomes awkward. Here Datalog really shines: it is elegant, concise, and performant (free parallelism!).
Embedding a query language inside a general-purpose programming language seems like a powerful idea (think: Linq). But what is the most powerful query language with simple semantics? Datalog. (Also important for a query language: every query terminates)
Flix is a functional-first programming language in which one writes small functions and composes them. But another important idea in Flix is that each function can be implemented in the most suitable way without the callers knowing about it. For example, it could be purely functional, it could use imperative programming with regions, or it could use Datalog.
Check out our recent OOPSLA paper for more:
Flix: A Design for Language-Integrated Datalog
https://dl.acm.org/doi/10.1145/3763126
5
u/vanilla-bungee Jun 14 '26
I think we need /u/jorkadeen
5
u/yuri-kilochek Jun 14 '26
It's nowhere near as ubiquitous. Maybe it ought to be.
3
u/ShacoinaBox Jun 14 '26 edited Jun 14 '26
idk honestly lol but I love flix to death n the inclusion of datalog is enormously cool imo. I guess I'm writing this to just say that I think ppl should give it a try.
2
u/Positive_Total_4414 Jun 26 '26
As you mentioned that you use Z3 for game design, could you maybe tell a bit more about it and how you approach it? Or would you maybe recommend some specific blogs, articles or examples? I did find a lot of material available online, but was just wondering if you would highlight or suggest something particular.