/* Jobs problem in Picat. From http://www.anselm.edu/internet/compsci/faculty_staff/mmalita/HOMEPAGE/logic/jobs1.txt """ File: Jobs1.pl Author: (sol MM) Title: Finding the profession Smith, Baker, Carpenter and Tailor have each a profession (smith, baker, carpenter, tailor) but not showed by their names. Each of them has a son. But the sons also do not have the profession showed by their name. If you know that: 1) no son has the same profession as his father has and 2) Baker has the same profession as Carpenter's son has and 3) Smith's son is a baker. find the professions of the parents and sons. """ Solution: [carpenter,smith,baker,tailor] [baker,carpenter,smith,tailor] This Picat model was created by Hakan Kjellerstrand, hakank@gmail.com See also my Picat page: http://www.hakank.org/picat/ */ % import util. % import cp. main => go. go => sol([S,B,C,T],[Son_S, Son_B, Son_C, Son_T]), println('Father''s professions'=[S,B,C,T]), println('Son''s professions '=[Son_S, Son_B, Son_C, Son_T]), nl. /* list of professions */ professions(Professions) => Professions = [smith,baker,carpenter,tailor]. /* S = Smith's profession B = Baker's profession C = Carpenter's profession T = Taylor's profession Son_S = Smith son's profession Son_B = Baker son's profession Son_C = Carpenter son's profession Son_T = Taylor son's profession */ sol([S,B,C,T],[Son_S, Son_B, Son_C, Son_T]) => professions(L), member(S,L),not(S=smith), member(B,L),not(B=baker), member(C,L),not(C=carpenter), member(T,L),not(T=taylor), /* The sons do not have the same profession as their name shows */ member(Son_S,L),not(Son_S=smith), member(Son_B,L),not(Son_B=baker), member(Son_C,L),not(Son_C=carpenter), member(Son_T,L),not(Son_T=taylor), /* The sons do not have the same profession as their fathers either */ not(S = Son_S), % hakank: The source stated "not S = Son_F" which is wrong not(B = Son_B), not(C = Son_T), not(T = Son_C), /* Baker has the same profession as Carpenter's son. */ B=Son_C, /* Smith's son is a baker */ Son_S=baker, distinct([S, B, C, T]), % hakank: added this distinct([Son_S, Son_B, Son_C, Son_T]). % hakank: added this set([]) => true. set([H|T]) => not(member(H,T)),set(T). distinct(M) => set(M).