3 ms·
I've been reading this book to get an overview of Prolog: http://www.learnprolognow.org/ http://www.learnprolognow.org/. It's much better than most of the tutor
by girzel 7y ago
I've been reading this book to get an overview of Prolog: http://www.learnprolognow.org/ http://www.learnprolognow.org/. It's much better than most of the tutorials I found out there, and provides a very gentle, very thorough introduction to what might be some fairly alien programming ideas (at least, they were to me).
I'm still chewing over the fact that a Prolog term looks exactly the same whether it's in a knowledge base (ie it's true), or whether you're feeding it to the prompt (ie you're asking if it's true or not). Maybe I'm just spooking myself out, but the identity of facts and queries feels reminiscent of Lisp's homoiconicity between code and data.
- tom_mellior 7y ago> Maybe I'm just spooking myself out, but the identity of facts and queries feels reminiscent of Lisp's homoiconicity between code and data. Wait until you discover Prolog's homoiconicity ;-) You can treat Prolog terms as data and also add them to the database as code and call the resulting goals like all others: ?- MyFact = f(x), assert(MyFact). MyFact = f(x). ?- f(A). A = x. ?- MyRule = (f(g(X)) :- f(X)), assert(MyRule). MyRule = (f(g(X)):-f(X)). ?- f(A). A = x ; A = g(x) ; A = g(g(x)) . That is, asserting the "data" "f(x)" and "f(g(X)) :- f(X)" behaves the same as putting the identical-looking "code" in a source file. You can also query the code and get back it back as identical-looking "data": ?- Head = f(_), clause(Head, Body). Head = f(x), Body = true ; Head = f(g(_G1014)), Body = f(_G1014). (The first of these represents the fact "f(X)." as "f(X) :- true." to make things more uniform internally.) As for what this is good for, you can use it to write your own metainterpreters that execute Prolog code in the normal way, or in some nonstandard or enhanced way, recording tracing data for example. There is a good writeup on metainterpreters at https://www.metalevel.at/acomip/ https://www.metalevel.at/acomip/