/* Looking at unmarried people in Picat. From Adrian Groza "Modelling Puzzles in First Order Logic" """ Puzzle 73. Looking at unmarried people There are three friends staying on the couch in Central Perk: Rachel, Ross, and Mon- ica. Monica is looking at Ross. Ross is looking at Rachel. Monica is married; Rachel is not. Is a married person looking at an unmarried person? """ There are two cases: Ross is either married or unmarried. * go/0: Constraint programming * If Ross is unmarried: [rachel = 0,ross = 0,monica = 1] Monica: married Ross: unmarried Rachel: unmarried A married Monica is looking at an unmarried Ross * If Ross is married: [rachel = 0,ross = 1,monica = 1] Monica: married Ross: married Rachel: unmarried A married Ross is looking at an unmarried Rachel * go2/0: Logic programming [a,married,monica,is,looking,at,an,unmarried,ross] [a,married,ross,is,looking,at,an,unmarried,rachel] Cf married_looking_at_unmarried.pi for some other approaches. This program was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ import cp. main => go. go ?=> Married = [Rachel,Ross,Monica], Married :: 0..1, % Monica is married; Rachel is not. Monica #= 1, Rachel #= 0, % Monica is looking at Ross. % Ross is looking at Rachel. % Is a married person looking at an unmarried person? % Yes, if Monica is married (which she is) and Ross is unmarried (unknown) % OR % if Ross is married (unknown) and Rachel is unmarried (which she is) % (Monica #= 1 #/\ Ross #= 0) #\/ (Ross #= 1 #/\ Rachel #= 0), Vars = [rachel=Rachel,ross=Ross,monica=Monica], solve(Vars), println(Vars), Map = new_map([0="unmarried",1="married"]), printf("Monica: %w Ross: %w Rachel: %w\n",Map.get(Monica),Map.get(Ross),Map.get(Rachel)), if Monica == 1, Ross == 0 then println("A married Monica is looking at an unmarried Ross") elseif Ross == 1, Rachel == 0 then println("A married Ross is looking at an unmarried Rachel") end, nl, fail, nl. go => true. /* Logic programming Two solutions: [a,married,monica,is,looking,at,an,unmarried,ross] [a,married,ross,is,looking,at,an,unmarried,rachel] */ go2 ?=> status(X,married), status(Y,unmarried), looking_at(X,Y), println([a,married,X,is,looking,at,an,unmarried,Y]), fail, nl. go2 => true. looking_at(monica,ross). looking_at(ross,rachel). status(monica,married) => true. status(rachel,unmarried) => true. status(X,Status) => (Status = married ; Status = unmarried).