/**************************/ /* */ /* Answers to worksheet 2 */ /* */ /**************************/ /* This stuff was already given */ /* A Zoological Example */ isa_mammal(gertie). isa_mammal(zebedee). isa_mammal(tigger). has_feathers(tweety). has_hooves(gertie). has_hooves(zebedee). has_spots(gertie). has_long_neck(gertie). has_stripes(zebedee). has_pointed_teeth(tigger). has_claws(tigger). has_forward_eyes(tigger). colour(tweety, blue). colour(gertie, tawny). colour(zebedee, black_and_white). colour(tigger, tawny). isa_ungulate(X) :- isa_mammal(X), has_hooves(X). isa_zebra(Y) :- isa_ungulate(Y), has_stripes(Y). has_camouflage(Z1) :- has_stripes(Z1). has_camouflage(Z2) :- has_spots(Z2). isa_coloured_mammal(X) :- isa_mammal(X), colour(X, _). /* Exercise 2.1 */ /* A query to return the names of animals that have spots */ /* and a long neck. */ /* ?- has_spots(X), has_long_neck(X). */ /* Exercise 2.2 */ /* Definitions of carnivores, birds, cheetahs, tigers and */ /* giraffes. */ isa_rhino(X) :- has_horn(X), has_leathery_skin(X). isa_carnivore(X) :- isa_mammal(X), animal_eats_meat(X). isa_carnivore(X) :- has_claws(X), has_pointed_teeth(X), has_forward_eyes(X). isa_bird(X) :- has_feathers(X). isa_bird(X) :- flies(X), lays_eggs(X). isa_cheetah(X) :- isa_carnivore(X), has_spots(X). isa_tiger(X) :- isa_carnivore(X), has_stripes(X). isa_giraffe(X) :- isa_ungulate(X), has_spots(X), has_long_neck(X). /* Exercise 2.3: Chez Henri */ /* For these definitions to work you have to load in facts such */ /* as those given in the file chez_henri. */ /* a) Definition of maincourse */ maincourse(M) :- meat(M). maincourse(M) :- fish(M). /* b) Definition of meal */ meal(H, M, D) :- hors_d_oeuvre(H), maincourse(M), dessert(D). /* c) Query to find meals with fish as their maincourse */ /* ?- meal(H, M, D), fish(M). */ /* d) Query to find meals with artichauts melanie as hors d'oeuvre */ /* ?- meal(artichauts_melanie, M, D). */ /* e) Definitions for full meals */ full_meal(H, M, D, B) :- hors_d_oeuvre(H), maincourse(M), dessert(D), beverage(B). beverage(wine). beverage(beer). beverage(mineral_water). /* Exercise 2.4: Achin' Hearts */ /* For these definitions to work you have to load in facts such */ /* as those given in the file dating. */ /* a) An example of a fact with a variable is */ /* seeks(ann, small, red, Z). */ /* This means that ann seeks a small red-haired person, whose age */ /* can be anything (hence the variable Z). So, in general, the */ /* variables add universal quantification to the facts. */ /* b) Definition of suits physically */ suits_physically(X, Y) :- man(X, S1, C1, A1), woman(Y, S2, C2, A2), seeks(X, S2, C2, A2), seeks(Y, S1, C1, A1). /* c) Definition of having the same tastes */ have_same_tastes(X, Y) :- tastes(X, M, L, S), tastes(Y, M, L, S). /* d) Definition of matched */ matched(X, Y) :- suits_physically(X, Y), have_same_tastes(X, Y). /* A note about the definition of have_same_tastes. */ /* This definition will match men with men and women with women */ /* as long as they have the same tastes. Arguably, this is */ /* correct. But it also matches people with themselves: obviously */ /* you have the same tastes as yourself! */ /* There are 3 responses to this `problem': */ /* 1) Ignore it: the definition of matched will make sure that */ /* you have a physically suited man-woman pair, so this will */ /* eliminate self-dating. */ /* 2) Add to the definition of have_same_tastes a specification */ /* that X is a man and Y is a woman. */ /* 3) Use some impure Prolog (which we haven't encountered yet) */ /* to add a preventative test to the definition. */ /* I have chosen option (1). */