projog

1. Getting Started

Examples

"Hello world" example.

?- write('hello, world'), nl.
hello, world

yes

Dynamically populate knowledge base with facts.

?- asserta(message(hi, world)).

yes

?- assertz(message(hello, everyone)).

yes

?- asserta(message(hello, world)).

yes

Query using new facts added to the knowledge base.

?- message(hello, everyone).

yes

?- message(hi, everyone).

no

?- message(X, Y).
X = hello
Y = world

yes;
X = hi
Y = world

yes;
X = hello
Y = everyone

yes

?- message(hello, Y).
Y = world

yes;
Y = everyone

yes

Simple example of both unification and arithmetic.

?- W=X, X=1+1, Y is W, Z is -W.
W = 1 + 1
X = 1 + 1
Y = 2
Z = -2

yes

Populate the knowledge base with clauses read from a file containing Prolog syntax.

?- consult('towers-of-hanoi-example.pl').

yes

View the definition of hanoi that has just been parsed from the consulted file.

?- listing(hanoi).
hanoi(N) :- move(N, left, centre, right)

yes

Use the clauses loaded from towers-of-hanoi-example.pl in a query.

?- hanoi(2).
[move,a,disc,from,the,left,pole,to,the,right,pole]
[move,a,disc,from,the,left,pole,to,the,centre,pole]
[move,a,disc,from,the,right,pole,to,the,centre,pole]

yes