Prolog Cookbook
By Robert Laing
These are notes I’ve made for myself and anyone else interested as I explore logic programming with Prolog, specifically SWI Prolog.
I originally created this website using MoinMoin, but have now moved it to Hugo thanks to its fantastic syntax highlighting which includes support for Prolog and just about every other programing language and data format.
I’m finding a static site generator as opposed to the various content management systems I’ve dabbled with over the years a pleasure since it doesn’t get in the way of using third party JavaScript libraries. So far, I’ve used vis.js to draw graphs I originally did with graphviz.
I’ve also played with MathJax to try fool people with intimidating maths into thinking I know what I’m talking about.
I’m in the process of rewriting the various puzzles etc that were here in MoinMoin to the new format. As we say in South Africa, it will be here again “now now”.
If you have any suggestions or corrections, please post them to a thread I started on SWI Prolog’s discourse forum.
Documenting, testing, and moduling
By Robert Laing
A nice thing about Prolog, specifically SWI Prolog, is there isn’t the cacophony of competing document generating systems, unit testing frameworks etc as there are for, say, JavaScript.
Nevertheless, getting to grips with PLDoc, PLUnit, along with SWI Prolog’s modules etc is not that easy, so I’ve made these notes for myself and anyone else interested.
The very basics of PLDoc is it can be launched as a web server either from within the swipl repl with
doc_server(4000).
or from the bash shell with swipl --pldoc myfile.pl
. See
Running the documentation system for details.
Game Trees
By Robert Laing
I put some notes and examples on this at https://swish.swi-prolog.org/p/Graphs1.swinb.
Glossary
- Analysis
- Assumes what is required to be done is already done. What is sought as already found, what we have to prove as true.
- Antecedent
- A thing that existed before or logically precedes another.
My introduction to this was via a free online course, General Game Playing, which offers a nice selection of games written in a Lisp-dialect called KIF which is fairly easy to translate into Prolog. The games follow a template called Game Description Language.
Graph Traversal
By Robert Laing
Traversing trees comes up a lot in coding. For intance, I needed to write a recursive HTML template to generate the table of the contents on the left of this web page using Hugo, and I posted my solution to Hugo’s discourse group for anyone interested.
The pattern I followed is called a transitive closure. Whenever I need some computer science theory, I turn to a textbook written by Turing Award winners Al Aho and Jeff Ullman which they have kindly made freely available online. They introduce transivitive closures in chapter 7, The Set Data Model, and expand on it in chapter 9, The Graph Data Model.
Puzzle Solving
A fun application of graph traversal, specifically route finding, is using Prolog to solve puzzles.
An example of a puzzle written in kif
is at
Buttons and Lights.
For the start state in ButtonsAndLights generatemoves(Start, Children)
produces
[ move([off(p), off(q), off(r), step(1)], does(robot, a), [on(p), off(q), off(r), step(2)], goal(robot, 0)),
move([off(p), off(q), off(r), step(1)], does(robot, b), [off(p), off(q), off(r), step(2)], goal(robot, 0)),
move([off(p), off(q), off(r), step(1)], does(robot, c), [off(p), off(q), off(r), step(2)], goal(robot, 0))
]
A thing to note above is actions b
and c
cycle back to the start state, only the step counter changing. To keep the problem space as skinny
as possible, we need to rewrite our nocycle filter to strip steps out of states to make this clearer.
game-description-language
A basic framework for representing games in Prolog has been provided by Stanford University’s General Game Playing course. The notes for the course written by Michael Genesereth include A Brief Introduction to Basic Logic Programming.
GDL
There are two dialects of Game Description Language, infix GDL which is nearly identical to Prolog, and a Lisp-like prefix GDL, called Knowledge Interchange Format, which is what the many examples provided at its public game repositories are written in.