A newbie to Prolog, introducing myself

I am an absolute newbie to Prolog.

I began programming in high school, though lack a formal computer science background.

I have no commercial interest in programming, but do like to tinker.

I possess an innate ability to think deeply about thinking while being able to observe that process as it happens, as well as analyse it later, at will.

I stumbled upon a book on artificial intelligence called “Common Sense, The Turing test, and the quest for real AI” by Mr Hector Levesque which piqued a curiousity in logic-based artificial intelligence research.

I found a set of books on LbAI related methods authored and co-authored by Mr Hector Levesque and found that almost all of them use Prolog for exposition.
I did try to wrap my mind around Prolog, but failed, in fact, I couldn’t understand it at all. It’s completely different from any of the programming languages I’ve used so far.

I have an x86-64 laptop running Ubuntu and have installed the “swi-prolog-x” package to be able to use the GUI toolkit as well as the graphical code-editor & debugger. I have been unable to get the XPCE-based editor running yet, because I don’t know how to.

So, here I am, looking for advice on easing myself into the relational programming style used by Prolog.

Hope this community would not mind questions from me which would seem strange rather than naive. Request you all to understand that I have no intention of trolling.

Thank you.

2 Likes

Welcome.

You might find this of interest.

Useful Prolog references


I can not help with this as I use VSCode on Windows to do the editing but do use the top level to run and debug the code.

There are many others here who do use XPCE for editing and can offer advise.

Once you have a Prolog file (e.g. myfile.pl), run this to load the file and edit it.

swipl myfile.pl
?- edit.

There is also edit/1, which takes the name of a file, predicate, module or whatever it can associate with a file (and optionally a line number) and starts editing that.

If you have no file yet, start Prolog and you can run emacs/0 below to start the editor and the use the File/New … menu item.

?- emacs.

Success!

It might be useful to think about Prolog as “SQL queries on steroids” (and with a more compact syntax). And these “queries” can be used to do general purpose programming.
(The other big thing in Prolog is data structures, but that’s lesson 2, and goes beyond what SQL can do.)

Suppose you have a table of parent/child:

create table parent_child (
    parent varchar,
    child  varchar)

The equivalent in Prolog would have the parent-child table defined by facts:

parent_child(john, sally).
parent_child(sally, fred).
...

Here are translations of simple SQL queries to Prolog:

select parent from parent_child where child = 'sally'
select child from parent_child where parent = 'sally'
select parent, child from parent_child where parent in ('john', 'fred')

becomes in Prolog

?- parent_child(Parent, sally). %or: parent_child(Parent, Child), Child=sally.
?- parent_child(sally, Child). 
?- parent_child(Parent, Child), member(Parent, [john, fred]).

Suppose you want to find grandparent / grandchildren. Then you could define this by:

create table grandparent_grandchild as
    select pc1.parent as grandparent, pc2.child as grandchild  from 
                 parent_child pc1, parent_child pc2
    where pc1.child = pc2.parent

In Prolog:

grandparent_grandchild(GrandParent, GrandChild) :-
    parent_child(GrandParent, Parent),
    parent_child(Parent, GrandChild).

But Prolog can go further than SQL because it’s Turing complete, so we can define:

ancestor_descendent(Ancestor, Descendent) :-
    parent_child(Ancestor, Inbetween),
    ancestor_descendent(Inbetween, Descendent).

HTH
(None of this has been run, and probably has typos; any my SQL is very rusty)

Hi and welcome :slight_smile:

When i started out learning i read Learn Prolog Now a lot. It’s really compact and well written.

I am also using Visual Studio Code (VSC) to edit and i am really happy with it. If you want to use VSC you install it after SWI Prolog and, from within VSC, you install the VSC Prolog extension.

Cheers/JCR

Yes indeed. Success. :slight_smile:
Thanks a million. That editor is quite well made, surpassed all expectations.
Can the debugger be attached to that same window? It would give the whole environment a standard IDE-like feel (Microsoft Visual Studio, not VS Code).

Thanks for that link to the web resource.
That’s quite a large collection, all in one place. Definitely helped me. :slight_smile:

I’m sure the SQL reference would have helped me if I had been in touch with working with SQL, haven’t done so in over 23 years.
Thanks anyways. :slight_smile:

Thanks for that tip regarding VS Code.
Am yet to try out that environment, so asking, does the VSC with it’s Prolog extension also integrate a debugger?

The graphical debugger is started using guitracer/0, gtrace/0 or gspy/1. It integrates the editor, so it is more the other way around. The normal editor does allow you to set breakpoints that also trap the graphical debugger.

I think there is a debugger in VSC but i am not sure how well it works. The graphical one that ships with SWI Prolog is hard to beat…