Xah Prolog Learning Notes

By Xah Lee. Date:

This is my learning notes. When this tutorial is robust, i'll remove this paragraph.

install

# install prolog
sudo apt-get install swi-prolog

this will install the command /usr/bin/swipl

there's also a alias /usr/bin/prolog to swipl.

the following are also installed, but you don't have to worry about them now.

prolog -> /etc/alternatives/prolog
swipl -> ../lib/swi-prolog/bin/amd64/swipl
swipl-ld -> ../lib/swi-prolog/bin/amd64/swipl-ld
swipl-rc -> ../lib/swi-prolog/bin/amd64/swipl-rc
xpce -> ../lib/swi-prolog/bin/amd64/xpce
xpce-client -> ../lib/swi-prolog/xpce-6.6.66/bin/amd64/xpce-client

you can now man swipl to read the man page if you like.

he syntax for a fact is

pred(arg1, arg2, ...  argN).

where

pred
    The name of the predicate
arg1, ...
    The arguments
N
    The arity
.
    The syntactic end of all Prolog clauses

A predicate of arity 0 is simply

    pred.
The arguments can be any legal Prolog term. The basic Prolog terms are

integer
    A positive or negative number whose absolute value is less than some implementation-specific power of 2
atom
    A text constant beginning with a lowercase letter
variable
    Begins with an uppercase letter or underscore (_)
structure
    Complex terms, which will be covered in chapter 9

Various Prolog implementations enhance this basic list with other data types, such as floating point numbers, or strings.

The Prolog character set is made up of

    Uppercase letters, A-Z
    Lowercase letters, a-z
    Digits, 0-9
    Symbols, + - * / \ ^ , . ~ : . ? @ # $ &

Integers are made from digits. Other numerical types are allowed in some Prolog implementations.

Atoms are usually made from letters and digits with the first character being a lowercase letter, such as

    hello
    twoWordsTogether
    x14

For readability, the underscore (_), but not the hyphen (-), can be used as a separator in longer names. So the following are legal.

    a_long_atom_name
    z_23

The following are not legal atoms.

    no-embedded-hyphens
    123nodigitsatbeginning
    _nounderscorefirst
    Nocapsfirst

Use single quotes to make any character combination a legal atom as follows.

    'this-hyphen-is-ok'
    'UpperCase'
    'embedded blanks'

Do not use double quotes ("") to build atoms. This is a special syntax that causes the character string to be treated as a list of ASCII character codes.

Atoms can also be legally made from symbols, as follows.

    ++

Variables are similar to atoms, but are distinguished by beginning with either an uppercase letter or the underscore (_).

    X
    Input_List
    _4th_argument
    Z56

Using these building blocks, we can start to code facts. The predicate name follows the rules for atoms. The arguments can be any Prolog terms.

Facts are often used to store the data a program is using. For example, a business application might have customer/3.

    customer('John Jones', boston, good_credit).
    customer('Sally Smith', chicago, good_credit).

The single quotes are needed around the names because they begin with uppercase letters and because they have embedded blanks.

Another example is a windowing system that uses facts to store data about the various windows. In this example the arguments give the window name and coordinates of the upper left and lower right corners.

    window(main, 2, 2, 20, 72).
    window(errors, 15, 40, 20, 78).

Simple Queries

    ?- room(office).
    yes

Prolog will respond with a 'yes' if a match was found. If we wanted to know if the attic was a room, we would enter that goal.

    ?- room(attic).
    no

room(kitchen).
room(office).
room(hall).
room('dining room').
room(cellar).

door(office, hall).
door(kitchen, office).
door(hall, 'dining room').
door(kitchen, cellar).
door('dining room', kitchen).

location(desk, office).
location(apple, kitchen).
location(flashlight, desk).
location('washing machine', cellar).
location(nani, 'washing machine').
location(broccoli, kitchen).
location(crackers, kitchen).
location(computer, office).

edible(apple).
edible(crackers).

tastes_yucky(broccoli).

here(kitchen).

?- room(X).
X = kitchen ;
X = office ;
X = hall ;
X = 'dining room' ;
X = cellar ;
no

?- location(Thing, kitchen).
Thing = apple ;
Thing = broccoli ;
Thing = crackers ;
no

Compound Queries

Simple goals can be combined to form compound queries. For example, we might want to know if there is anything good to eat in the kitchen. In Prolog we might ask

?- location(X, kitchen), edible(X).

to load a prolog file in terminal, prolog -f filename.pl, it'll start repl anyway.

to load a file in repl, do consult filename.pl

Reference