4 ms·
Btw, I wanted to note that Prolog is indeed very good at any problem that requires manipulating recursive data structures, like parsing. But that's one thing th
by YeGoblynQueenne 4y ago
Btw, I wanted to note that Prolog is indeed very good at any problem that
requires manipulating recursive data structures, like parsing. But that's one
thing that you can do more easily in Prolog than you can in other languages.
It's one of the things it is good for, if you want, not the one thing it is good
for.
For me, what Prolog does best is get all the ceremony of other programming
languages out of my way and let me concentrate on the problem at hand. By
"ceremony" I mean special syntactic constructs. For example, you don't even
declare variables, as such, in Prolog, there's no assignment expressions,
classes and enums and switch statements, there are no for and while loops etc
etc. Prolog instead gives you a very small set of abstract building blocks that
you can use to write any program. It's like programming with Lego bricks, except
you have one kind of brick, a universal Lego brick that you can build anything
you want with.
Which is why it's so good at solving hard problems, because once you're used to
its abstractions and how to combine them, any problem becomes an exercise in
finding the right way to fit the "bricks" together in the right way [1] [2].
That's also why Prolog is hard to learn: imperative languages have ceremonial
constructs (if-then-else, while-do, yield, etc) for a reason. The reason being,
because that makes it easier to write programs. But not easier to _solve
problems_. Not when the problems get hard anyway [3].
So perhaps this is one reason why people say it's not "general purpose"? Because
it's hard to learn, so it's hard to use when you're just trying to write, dunno,
simple list processing programs, but makes things easier when you're trying to
do something inherently difficult, like write a parser? If I think about that, I
could see how Prolog may come across as a language only good for certain kinds
of problem (which is my attempt to interpret "general purpose", though I'd still
be grateful if someone gave me a clear definition that I can grok).
____________
[1] As an example, here's two ways to write the Ackermann–Péter function in
Prolog:
https://rosettacode.org/wiki/Ackermann_function#Prolog https://rosettacode.org/wiki/Ackermann_function#Prolog
One uses the is/2 predicate, which is an instance of what I mean by "ceremony".
The second uses Peano notation for natural numbers and is basically a direct
translation of the Function's definition on wikipedia (full disclosure: the
second version is mine).
[2] That's also the reason why it's infinitely easier to machine-learn Prolog
programs, as in Inductive Logic Programming, than to learn, say, Python
programs. If you want to loop in Prolog, all you need to do is place a literal
in the body of a clause that has the same symbol as the literal at the head of
the clause: p:- q, p is recursive. So if you want loops, just move your "bricks"
around until they're in the right place, and job done. To machine-learn Python
you have to find a way to compose every single special-purpose expression, and
all the other kinds of loops, and that gets a lot more complicated, more so as a
program grows in size.
It should be noted that you get the same simplicity with languages of the lambda
calculus, hence Inductive Functional Programming is also a thing.
[3] All the high-level abstraction also makes it much harder to read. See the
Peano notation Ackermann-Péter Function above.
- agumonkey 4y agoThis is something I appreciate too, that's why I gradually moved toward lisp, sml, haskell and prolog. I find other languages just give me the wrong tools and setups and restrict my solution space and pace a lot.
- knome 4y ago>All the high-level abstraction also makes it much harder to read All that abstraction can also make code easier to read by hiding details irrelevant to a function. Not that you can't do similar abstractions in prolog. When I wrote my bf-in-prolog implementation [1] to play with the language, I found myself writing functions to create a purposed wad of data and then manipulate or extract from it, so the rest of the program could neatly ignore how that wad of data actually worked internally, in much the same way as I would in any language. >why Prolog is hard to learn Prolog is a pretty standard functional programming language, just hidden in a backtracking query runner. All of the introductions and tutorials generally start from the query runner's point of view, of facts and rules and backtracking with examples of `parent(This,That)` and showing it can identify grandparents or cousins or whatever, so it's little wonder that most programmers aren't going to bother sifting through to see it's just a little pattern-matching haskell/c++-template-language kind of thing. >you don't even declare variables, as such, in Prolog, there's no assignment expressions I definitely viewed unification as a form of variable assignment ( understanding it would backtrack if unable to assign ). There is a `=` in there to unify a variable with another variable or a given chunk of data. I used it to initialize data structures and to terminate recursive calls by assigning to the 'out' variable. I understand it's "unifying" rather than "assigning", but the effect is the same if you know one of the variables to be unbound. Otherwise it's just an assertion of equality (unifiability?) between the two values. ----- [1] https://github.com/knome/bf-in-prolog https://github.com/knome/bf-in-prolog
- dreamcompiler 4y ago> Prolog is a pretty standard functional programming language, just hidden in a backtracking query runner. Disagree. The fundamental difference (to me) is that in a functional language you write functions that have inputs and outputs. You can write code like this in Prolog, but Prolog encourages the idea that there's no such thing as an input or an output; there are only relations. If you write a function such as c = a + b in a functional language you cannot just say "here are c and a; tell me what b is." That idea makes no sense in a functional language, but Prolog will simply tell you what b is. You can even say "here is c; give me an assortment of a's and b's that make the relation true" and Prolog will start listing them off (albeit probably not very efficiently, and one does not typically use this capability for simple arithmetic). This is a very different way of thinking about functions because they are inherently "bidirectional" and it's one of the things that makes Prolog feel more like a magical query language than a functional language.