Artificial Intelligence and Prolog
Copyright © 2020 Jiri Kriz, www.nosco.ch

1    Basics

Solution of Exercise 1

1.1    Family Relations

/* 1.1 Example family: British Royals */

woman(queen_elizabeth_II).
mother(queen_elizabeth_II, prince_charles).
mother(queen_elizabeth_II, princess_anne).
mother(queen_elizabeth_II, prince_andrew).
mother(queen_elizabeth_II, prince_edward).

man(duke_philip).
father(duke_philip, prince_charles).
father(duke_philip, princess_anne).
father(duke_philip, prince_andrew).
father(duke_philip, prince_edward).

man(prince_charles).
father(prince_charles, prince_william).
father(prince_charles, prince_henry).

woman( princess_anne).

man( prince_andrew).

man( prince_edward).

woman(princess_diana).
mother(princess_diana, prince_william).
mother(princess_diana, prince_henry).

man(prince_william).

man(prince_henry).

/* 1.2 */

parent( Father, Child) :- father( Father, Child).
parent( Mother, Child) :- mother( Mother, Child).
parents( Father, Mother, Child) :- 
     father( Father, Child), mother( Mother, Child).
child( Child, Parent) :- parent( Parent, Child).
son( S, P) :- child( S, P), man( S).
daughter( D, P) :- child( D, P), woman( D).
grandfather( GP, Grandchild) :- 
     parent( P, Grandchild), father( GP, P).
grandmother( GM, Grandchild) :- 
     parent( P, Grandchild), mother( GM, P).
grandchild( Grandchild, G) :- 
     parent( P, Grandchild), parent( G, P).
brother( Brother, Sibling) :- 
     parents( Father, Mother, Brother), 
     parents( Father, Mother, Sibling), 
     man( Brother), Brother \= Sibling.
sister( Sister, Sibling) :- 
     parents( Father, Mother, Sister),
     parents( Father, Mother, Sibling), 
     woman( Sister), Sister \= Sibling.
uncle( U, N) :- parent( P, N), brother( U, P).
aunt( A, N) :- parent( P, N), sister( A, P).

/* spouse( Man, Woman) cannot be represented by a rule.
   These must be facts. 
*/

Back to example 1.1

1.2    A Simple Thought (Basic Inference)

wish_to_live_in_peace( X) :- people( X).
people( X) :- man( X).
people( X) :- woman( X).
people( X) :- child( X).
man( 'I').
/* ?-wish_to_live_in_peace( 'I') ==> true */ 

Back to example 1.2

1.3    A Small World (Knowledge Representation)

furniture( wardrobe, 1).
furniture( bookshelf, 1).
furniture( bed, 1).
furniture( desk, 1).
furniture( chair, 1).

furniture( X) :- furniture( X, N).      /* Query 1: ?- furniture( X) */

number( X, N) :- furniture( X, N).      /* Query 2: ?- number( Object, Number) */
number( door, 1).
number( window, 2).

at( wall_1, wardrobe).                  /* Query 5: ?- at( At, Object) */
at( wall_1, door).
at( wall_2, bookshelf).
at( wall_2, bed).
at( wall_3, bed).
at( wall_3, window_1).
at( wall_3, desk).
at( wall_4, desk).
at( wall_4, window_2).
at( wall_4, wardrobe).
at( window_1, bed).
at( window_2, desk).

in( corner_1, wardrobe).                /* Query 6: ?- in( Place, Object) */
in( corner_3, bed).
in( corner_4, desk).

place( X, P) :- at( P, X).              /* Query 3: ?- place( Object, Place) */
place( X, P) :- in( P, X).
place( chair, at_desk).

left( door, bookshelf).                 /* Query 4: ?- left( Object1, Object2) */
left( bookshelf, bed).
left( bed, desk).
left( desk, wardrobe).
left( wardrobe, door).

right( X, Y) :- left( Y, X).            /* Query 4: ?- right( Object1, Object2) */

Back to example 1.3