Live data from Hacker News

Prolog Basics Explained with Pokémon

unplannedobsolescence.com

61–64 of 64 posts

Re: Prolog Basics Explained with Pokémon

#61

> " Don't be bothered with by the fact that the solutions end with "or false" here. It's a function of how the search algorithms work; the solver looked for more solutions, then failed. I'll admit, I don't totally understand why it only sometimes does this, but it's expected. " I think this is explained in The Power of Prolog[1] that the answers coming from Prolog are not printing text to a terminal, they are valid P…

Eh, no, admittedly this is a bit confusing but the ";" at the top-level is not the same as the ;/2 disjunction operator. At the top-level the ";" is just a switch to say "give me more". In most Prolog systems you can also press space for the same thing. Whether a query will end with "false" or "true" really depends entirely on the query, and the predicates it's calling. So it depends on how a predicate is called. You…

> "but the ";" at the top-level is not the same as the ;/2 disjunction operator. At the top-level the ";" is just a switch to say "give me more"."

I understand that's the experience of using it, but I mean if I open the SWI Prolog toplevel and query:

    ?- X = 1 ; X = 2.
Then press space, I get:

    X = 1 ;
    X = 2.
which is exactly the same Prolog term, two unifications, each is a solution to the query, a disjunction between them, terminated with a full stop.

Is that really not the same semicolon usage/meaning/semantics in the query as in the answers?

Re: Prolog Basics Explained with Pokémon

#62

Earlier quoted context omitted.

Eh, no, admittedly this is a bit confusing but the ";" at the top-level is not the same as the ;/2 disjunction operator. At the top-level the ";" is just a switch to say "give me more". In most Prolog systems you can also press space for the same thing. Whether a query will end with "false" or "true" really depends entirely on the query, and the predicates it's calling. So it depends on how a predicate is called. You…

> " but the ";" at the top-level is not the same as the ;/2 disjunction operator. At the top-level the ";" is just a switch to say "give me more". " I understand that's the experience of using it, but I mean if I open the SWI Prolog toplevel and query: ?- X = 1 ; X = 2. Then press space, I get: X = 1 ; X = 2. which is exactly the same Prolog term, two unifications, each is a solution to the query, a disjunction betwe…

[deleted]

Re: Prolog Basics Explained with Pokémon

#63

Earlier quoted context omitted.

Eh, no, admittedly this is a bit confusing but the ";" at the top-level is not the same as the ;/2 disjunction operator. At the top-level the ";" is just a switch to say "give me more". In most Prolog systems you can also press space for the same thing. Whether a query will end with "false" or "true" really depends entirely on the query, and the predicates it's calling. So it depends on how a predicate is called. You…

> " but the ";" at the top-level is not the same as the ;/2 disjunction operator. At the top-level the ";" is just a switch to say "give me more". " I understand that's the experience of using it, but I mean if I open the SWI Prolog toplevel and query: ?- X = 1 ; X = 2. Then press space, I get: X = 1 ; X = 2. which is exactly the same Prolog term, two unifications, each is a solution to the query, a disjunction betwe…

In that case, your query is a disjunction but suppose you had a set of clauses like this:

  fruit(apples).
  fruit(oranges).
  fruit(bananas).
  likes(sam,X):- fruit(X).
And then entered a query like this:

  ?- likes(sam,X). 
And got results like this:

  X = apples ;
  X = oranges ;
  X = bananas.
Are those the results of a disjunction? I think it's clear that not: our rule says that "sam likes X if X is a fruit, for all X"; "for all" because in Prolog every variable is implicitly universally quantified. So the interpretation of the results of the query is:

  sam likes apples AND sam likes oranges AND sam likes bananas
Not:

  sam likes apples OR sam likes oranges OR sam likes bananas
Sam likes all the fruit! The different values of X are not disjuncts, they're conjuncts.

All that'll probably get me lynched because it flies in the face of a lot of fudging perpetrated by Prolog courses and even some textbooks that should know better, and that tell you that e.g. a set of rules is like an if-then-else and that when you get more results at the top-level those are alternatives. That's just wrong and I don't know why people teach Prolog like this, all it does is confuse students and make them hate Prolog more, but the formal definition of a logic program is that it's a conjunction of definite program clauses; where a definite program clause is a ... clause, therefore a disjunction. So a logic program is a conjunction of disjunctions, i.e. basically a formula in conjunctive normal form.

So the ";" that appears automatically at the end of your query when you press space is not an "OR", otherwise it wouldn't appear when the results of the query are the members of a conjunction. What is it then?

Try this. Instead of pressing ";" after the first result of your query (after the X = 1) press "h" for "help". On SWI-Prolog you'll get this menu:

  ?- X = 1 ; X = 2.
  X = 1
    Possible actions:
    ; (n,r,space,TAB): redo              | t:           trace&redo
    *:                 show choicepoint  | . (c,a,RET): stop
    w:                 write             | p:           print
    +:                 max_depth*5       | -:           max_depth//5
    b:                 break             | h (?):       help

  Action?
Thus finally* revealing the meaning of ";" at the top-level: it instructs the listener (i.e. the Prolog REPL) to enter the redo port. The ";" at the top-level means "redo" not "OR".

And that's true even in your query which is explicitly formulated as a disjunction. With an ";". It's confusing.

_______________

* I know, sorry.

Re: Prolog Basics Explained with Pokémon

#64

Earlier quoted context omitted.

Ahem. "Prolog underneath" is doing SLD-Resolution implemented as a Depth-First Search with backtracking. Saying it's "doing backtracking" is really fudging quite a bit. Datalog, instead, is "underneath" implementing a TP-Operator, a procedure that finds the fix-point of a Datalog program which happens to be the same as its Least Herbrand Model, which is what SLD-Resolution also finds, except that SLD-Resolution allow…

By the same logic, would you say that the fact that there's only one mainstream Rust is testament to the simplicity of implementing a macro-heavy, borrow checked language without breaking safety, performance and expressiveness?

I... don't really have an opinion on Rust. I have no idea, honest.

Mostly what I mean above is that Prolog is a hard balancing act, specifically balancing efficient execution with the practicalities of programming (e.g. lists, database asserts/retracts, I/O etc) and the theoretical underpinnings of Horn logic and SLD-Resolution. There's a lot of stuff that went into Prolog and its development process is quite distinct to any other language I'm aware of, where there wasn't like a central committee or an enlightened dictator, or a small band of hard-headed academics (...Haskell...) and so on, but instead there was a decades-long academic research process of going from First-Order Logic to Herbrand semantics, to definite logic, to Resolution, to Linear Resolution, to Linear Resolution with a Selection Rule, to Linear Resolution with a Selection Rule restricted to Definite clauses, to Negation-As-Failure under a Closed World Assumption, and finally to a language with an automated theorem prover as an interpreter and a clunky mockery of FOL syntax and terminology that isn't even complete (despite Resolution being complete); a process with no centralised structure instead split across multiple acadmics in several universities in the UK, in France, and in Japan.

Another way to see Prolog is that it's the result of a very peculiar academic process that is hard to replicate and that yielded a result that is difficult to outdo, with all its compromises; because of its compromises. Because its particular compromises were made to solve hard problems and were arrived at after a long, collaborative process that won't be easily outdone. Not that attempts haven't been made. For example, in logic programming circles Prolog is now considered a little bit quaint, even outdated. Most of the activity has shifted to Answer Set Programming (ASP). And that makes sense, Prolog is old news. ASP is the new kid on the block, it's only 30 years old! And like normal programming languages it was designed by a couple of people before being adopted by a wider community.

I just really don't know how any of that maps to Rust.

Post reply on HN