1% Lewis Carroll's Zebra (alias five houses) puzzle. 2 3 4/* Problem formulation -------------------------------------------- 5"Five men with different nationalities live in the first five houses 6of a street. They practise five distinct professions, and each of 7them has a favourite animal and a favourite drink, all of them 8different. The five houses are painted in different colours." 9 10The Englishman lives in a red house. 11The Spaniard owns a dog. 12The Japanese is a painter. 13The Italian drinks tea. 14The Norwegian lives in the first house on the left. 15The owner of the green house drinks coffee. 16The green house is on the right of the white one. 17The sculptor breeds snails. 18The diplomat lives in the yellow house. 19Milk is drunk in the middle house. 20The Norwegian's house is next to the blue one. 21The violinist drinks fruit juice. 22The fox is in a house next to that of the doctor. 23The horse is in a house next to that of the diplomat. 24 25 26Q. Who owns a Zebra, and who drinks water? 27 28----------------------------------------------------------- */ 29 30 31/* Solution and performance 32 33Only solution: 34 35Houses = 36[ 37house(house1, norwegian, yellow, diplomat, fox, water), 38house(house2, italian, blue, doctor, horse, tea ), 39house(house3, englishman, red, sculptor, snails, milk ), 40house(house4, spaniard, white, violinist, dog, juice), 41house(house4, japanese, green, painter, zebra, coffee)] 42 43 44Performance: 45 46Prolog, same order as the program (below): 380 secs. 47Prolog, optimal ordering 6 secs 48Prolog, with delay declarations 0.8secs 49 50------------------------------------------------------------------*/ 51 52:- module(zebra). 53 54% To run this program without Propia declare 55%:- op(100,xfx,infers). 56% Add a delay declaration here if you like. 57% delay (X infers Y) if nonground(X). 58%(X infers delay) :- call(X). 59:- use_module(library(propia)). 60 61:- export(zebra/2). 62 63% Problem representation ---------------------------------------- 64 65zebra(Houses,Language) :- 66 Houses = 67 [ house(house1,_,_,_,_,_), 68 house(house2,_,_,_,_,_), 69 house(house3,_,_,_,_,_), 70 house(house4,_,_,_,_,_), 71 house(house5,_,_,_,_,_) 72 ], 73 74 Constraints = 75 [ 76 house( _,englishman, red, _, _, _ ), 77 house( _, spaniard, _, _, dog, _ ), 78 house( _, japanese, _, painter, _, _ ), 79 house( _, italian, _, _, _, tea ), 80 house(house1, norwegian, _, _, _, _ ), 81 house( _, _, green, _, _, coffee ), 82 house( GreH, _, green, _, _, _ ), 83 house( WhiH, _, white, _, _, _ ), 84 house( _, _, _, sculptor, snails, _ ), 85 house( _, _, yellow, diplomat, _, _ ), 86 house(house3, _, _, _, _, milk ), 87 house( NorH, norwegian, _, _, _, _ ), 88 house( BluH, _, blue, _, _, _ ), 89 house( _, _, _,violinist, _, juice ), 90 house( FoxH, _, _, _, fox, _ ), 91 house( DocH, _, _, doctor, _, _ ), 92 house( HorH, _, _, _, horse, _ ), 93 house( DipH, _, _, diplomat, _, _ ), 94 house( _, _, _, _, zebra, _ ), 95 house( _, _, _, _, _, water ) 96 ], 97 98 on_the_right(GreH,WhiH) infers Language, 99 next_to(NorH,BluH) infers Language, 100 next_to(FoxH,DocH) infers Language, 101 next_to(HorH,DipH) infers Language, 102 103 all_member(Constraints,Houses). 104 105 106% Constraint Definition ---------------------------------------- 107 108on_the_right(house2,house1). 109on_the_right(house3,house2). 110on_the_right(house4,house3). 111on_the_right(house5,house4). 112 113next_to(H1,H2) :- on_the_right(H1,H2). 114next_to(H1,H2) :- on_the_right(H2,H1). 115 116all_member([],_). 117all_member([H|T],List) :- 118 member(H,List), 119 all_member(T,List). 120 121