(This presentation is taken primarily taken from an Introduction to Prolog by Paul Brna and Tamsin Treasure-Jones at http://cbl.leeds.ac.uk/~tamsin/prologtutorial.)
Problem --> Algorithm --> Assembly Code --> Machine Code |------------->--------------| Assembler |-------------------->---------------------| C compiler |------------------------>-----------------------------| Prolog Interpreter
sunny.
?- sunny.
john_is_cold. /* john is cold */
raining. /* it is raining */
john_Forgot_His_Raincoat. /* john forgot his raincoat */
fred_lost_his_car_keys. /* fred lost is car keys */
peter_footballer. /* peter plays football */
?- john_Forgot_His_Raincoat.
yes
?- raining.
yes
?- foggy.
no
relation(<argument1>,<argument2>,....,<argumentN> ).
likes(john,mary).
eats(fred,oranges). /* "Fred eats oranges" */
eats(fred,t_bone_steaks). /* "Fred eats T-bone steaks" */
eats(tony,apples). /* "Tony eats apples" */
eats(john,apples). /* "John eats apples" */
eats(john,grapefruit). /* "John eats grapefruit" */
?- eats(fred,oranges). /* does this match anything in the database? */
yes /* yes, matches the first clause in the database */
?- eats(john,apples). /* do we have a fact that says john eats apples? */
yes /* yes we do, clause 4 of our eats database */
?- eats(mike,apples). /* how about this query, does mike eat apples */
no /* not according to the above database. */
?- eats(fred,apples). /* does fred eat apples */
no /* again no, we don't know whether fred eats apples */
age(john,32). /* John is 32 years old */
age(agnes,41). /* Agnes is 41 */
age(george,72). /* George is 72 */
age(ian,2). /* Ian is 2 */
age(thomas,25). /* Thomas is 25 */
?- age(ian,2). /* is Ian 2 years old? */
yes /* yes, matches against the fourth clause of age */
?- agnes(41). /* for some relation agnes are they 41 */
no /* No. In the database above we only know about the relation */
/* age, not about the relation agnes, so the query fails */
?- age(ian,two) /* is Ian two years old? */
no /* No. two and 2 are not the same and therefore don't match */
eats(fred,mangoes).
?- eats(fred,what).
X /* a capital letter */
VaRiAbLe /* a word - it be made up or either case of letters */
My_name /* we can link words together via '_' (underscore) */
?- eats(fred,What).
What=mangoes
yes
loves(john,mary).
loves(fred,hobbies).
?- loves(john,Who). /* Who does john love? */
Who=mary /* yes , Who gets bound to mary */
yes /* and the query succeeds*/
?- loves(arnold,Who) /* does arnold love anybody */
no /* no, arnold doesn't match john or fred */
?- loves(fred,Who). /* Who does fred love */
Who = hobbies /* Note the to Prolog Who is just the name of a variable, it */
yes /* semantic connotations are not picked up, hence Who unifies */
/* with hobbies */
tape(1,van_morrison,astral_weeks,madam_george).
tape(2,beatles,sgt_pepper,a_day_in_the_life).
tape(3,beatles,abbey_road,something).
tape(4,rolling_stones,sticky_fingers,brown_sugar).
tape(5,eagles,hotel_california,new_kid_in_town).
?- tape(5,Artist,Album,Fave_Song). /* what are the contents of tape 5 */
Artist=eagles
Album=hotel_california
Fave_Song=new_kid_in_town
yes
?- tape(4,rolling_stones,sticky_fingers,Song). /* find just song */
Song=brown_sugar /* which you like best from the album */
yes
mortal(X) :-
human(X).
?- mortal(P).
P = socrates
yes
fun(X) :- /* an item is fun if */
red(X), /* the item is red */
car(X). /* and it is a car */
fun(X) :- /* or an item is fun if */
blue(X), /* the item is blue */
bike(X). /* and it is a bike */
fun(ice_cream). /* and ice cream is also fun. */
?- fun(harley_davidson). /* to which Prolog will reply */
yes /* to show the program succeeded */
?- fun(What).
What=vw_beatle
yes
eats(fred,pears).
eats(fred,t_bone_steak).
eats(fred,apples).
?- eats(fred,FoodItem).
FoodItem = pears
FoodItem = t_bone_steak
FoodItem = apples
?- hold_party(Who).
fun(X) :- /* something is either fun because its .... */
red(X), /* red */
car(X). /* and a car */
fun(X) :- /* or its fun if its.... */
blue(X), /* blue */
bike(X). /* and a bike */
/* database of red items */
red(apple_1).
red(block_1).
red(car_27).
/* database of cars */
car(desoto_48).
car(edsel_57).
?- fun(What).
/* clause 1 */
fun(X) :-
red(X),
car(X).
/* clause 2 */
fun(X) :-
blue(X),
bike(X).
/* database of red items */
red(apple_1). first choice
red(block_1).
red(car_27).
/* database of cars */
car(desoto_48).
car(edsel_57).
red(apple_1).
red(block_1). second choice
red(car_27).
red(apple_1).
red(block_1).
red(car_27). third choice
car(desoto_48).
car(edsel_57).
/* database of blue items */
blue(flower_3).
blue(glass_9).
blue(honda_81).
bike(iris_8).
bike(my_bike).
bike(honda_81).
blue(flower_3).
blue(glass_9).
blue(honda_81).
?- fun(What).
What=honda_81
yes
[first,second,third] = [A|B]
where A = first and B=[second,third]
[a,b,c,d,e,f,g]
[apple,pear,bananas,breadfruit]
[ ] /* this is a special list, it is called the empty list because it contains nothing */
[a,b,c] unifies with [Head|Tail] resulting in Head=a and Tail=[b,c]
[a] unifies with [H|T] resulting in H=a and T=[]
[a,b,c] unifies with [a|T] resulting in T=[b,c]
[a,b,c] doesn't unify with [b|T]
[] doesn't unify with [H|T]
[] unifies with []. Two empty lists always match
p([H|T], H, T).
?- p([a,b,c], X, Y).
X=a
Y=[b,c]
yes
?- p([a], X, Y).
X=a
Y=[]
yes
?- p([], X, Y).
no