5 ms·
I've never used it in production, but I have a deep love of Prolog, just because of how different it is from any other programming language I've used. As a prog
by openasocket 2y ago
I've never used it in production, but I have a deep love of Prolog, just because of how different it is from any other programming language I've used. As a programming paradigm, it found it as eye-opening as functional programming. What I found interesting is that you are operating on logical statements and pattern matching, which often means that the same "function" can be used for multiple different things. For example:
append([], List, List).
append([Head|Tail], List, [Head|Rest]) :-
append(Tail, List, Rest).
Is a simple list append function: append(X, Y, Z) is a predicate that match if Z is the list of all elements of X, followed by all elements of Y. You can use it to concatenate lists, of course. But you can also use it to confirm that a particular list start with a particular sequence, or ends with a particular sequence! The idea that the same predicate can be used in multiple different ways is really fascinating to me. I have no idea to what extent that would scale in a large system, but it's very interesting
- agumonkey 2y agoSame, I love the bidirectional aspect of relations
- wizzwizz4 2y agoIt doesn't scale that well because integers are their own, opaque thing in Prolog, and the is predicate is unidirectional. However, there's no inherent reason this has to be the case: you can construct your own representation of the integers from the Peano axioms, and recover bidirectionality of addition. (You'll want to be using some typed variant of Prolog with the "unary tree of units" optimisation, otherwise integers take O(n) space in memory, but it's possible in the language.) Prolog works how it does, because it's (mostly) backwards-compatible with a facts database used for symbolic AI research, where all atoms were opaque.
- lmkg 2y agoCheck out the CLP(ℤ) library by Markus Triska, who is also the author of Scryer Prolog. It defines new comparison predicates that lets you use bidirectional logical programming on standard integers with standard math operations. https://github.com/triska/clpz https://github.com/triska/clpz
- dsabanin 2y agoMarkus is not the author of Scryer Prolog, Mark Thom is.
- ahoka 2y ago“you can construct your own representation of the integers from the Peano axioms” This is how the Idris prelude defines nat, the type of natural numbers (with some magic making it actually fast). I think that’s very cool.
- rscho 2y agoThere are many ways to recover relational behaviour. Libs such as clp(fd) (superseded by clp(Z)), clp(BNR) and others.
- Avshalom 2y agoBNR notably works with real numbers and non linear constraints.
- coliveira 2y agoModern Prolog libraries have handled the issues of unidirectional integer predicates, so the old ways of handling numeric values is not relevant for most problems.
- jfmc 2y agoMany times the algorithm that you are implementing requires a precise data flow that is not reversible, so using traditional arithmetic (is/2) is better for catching errors. On the other hand CLP(FD) is not new at all (it is very popular for constraint programming).
- rscho 2y agoWhy would it be better for error handling? If you'll be using unidirectional flow only, then the point is moot. But using clp is arguably better IMO, allowing type and range checks while allowing relational execution.
- lynx23 2y agoDo you have a suggestion on where to / how to start learning Prolog beyond towers-of-hanoi? Prolog is basically the last language on my list of things I want to look at, but whenever I tried, I failed to find anything practical to do/try.
- dbcurtis 2y agoTry a rules-based synthesis problem. Prolog style (put simplisticly) is to write down what is true about a solution, and turn the search engine loose. Prolog is still procedural, unfortunately, so practical Prolog requires understanding the search algorithm. A suggestion: produce the XY locations of the nails for a building-code-compliant nailing pattern for a panel of plywood roof sheathing. Allow different stud spacing and wind speed zones. This is a small toy problem but will channel your thought process toward something Prologgy.
- tonyarkles 2y agoI love this example so much. I used a similar kind of problem a year or so ago when playing with some kind of logic/linear programming/optimization system: given a set of shelves (width and length), find the set of guillotine cuts (cuts across the entire sheet) that minimizes how many sheets of plywood are needed. If I recall the general problem is actually in NP but by formulating it as a problem of finding a list of operations (new sheet, cut sheet X horizontally, cut sheet X vertically) the solver was able to come up with high quality solutions in seconds. I then took it and added a second level: once it has found a minimal number of sheets, minimize the number of cuts. Super handy little tool! Once I had the solver part figured out and added a little tweak for handling kerf, it was quite simple to have it spit out an SVG that showed all of the pieces and cuts. And now… I want to go find that code and play with it again. Super fun stuff outside of the daily grind.
- amock 2y agoI’ve enjoyed https://www.metalevel.at/prolog https://www.metalevel.at/prolog but I’m not a Prolog Programmer.
- coliveira 2y agoI always thought that pattern matching would be an excellent feature for other programming languages, but it seems that it hasn't become popular. Many strategies available to Prolog could become possible just by the addition of this feature. One possible implementation of the idea occurs with C++ templates specialized with numerical parameters. It also seems that Mathematica provides pattern matching, but I don't use that language.
- mananaysiempre 2y agoMatching of patterns, with only a single occurrence allowed for each variable, is fairly popular in languages designed in the last two decades, isn’t it? A lot of those have or at least borrow from ML heritage, and that of course places a big emphasis on algebraic types and pattern matching. Full-blown unification remains niche, that’s true, but it’s just a fairly heavyweight feature, and I don’t really know how you’d integrate it without turning your language into (a superset of) Prolog.
- iamwil 2y agoSimon Peyton Jones (of Haskell) is working on Verse, a functional relational programming language, that merges the ideas of both functional and logical programming into one. It’s being done to facilitate Fortnite metaverse, as he’s doing this work at Epic. https://youtu.be/OJv8rFap0Nw?si=OmBT9CBJIxdJE1Jv https://youtu.be/OJv8rFap0Nw?si=OmBT9CBJIxdJE1Jv
- deleted 2y ago[deleted]