Advertisement
logicmoo

Untitled

Jan 22nd, 2025
395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Prolog 5.78 KB | None | 0 0
  1. % Main predicate to find the owner of the fish
  2. zebra_owner(Owner) :-
  3.     % Initialize Houses and exists_props
  4.     init_houses(['S', ['S', ['S', ['S', ['S', 'Z']]]]]),  % Equivalent to 5
  5.     b_setval(exists_props, 'Nil'),
  6.  
  7.     % Clue 1: The Brit lives in the red house.
  8.     declare(nationality, Brit, brit),
  9.     declare(color, Brit, red),
  10.  
  11.     % Clue 2: The Swede keeps dogs as pets.
  12.     declare(nationality, Swede, swede),
  13.     declare(pet, Swede, dog),
  14.  
  15.     % Clue 3: The Dane drinks tea.
  16.     declare(nationality, Dane, dane),
  17.     declare(drink, Dane, tea),
  18.  
  19.     % Clue 4: The green house is immediately to the left of the white house.
  20.     left_of(Green, White),
  21.     declare(color, Green, green),
  22.     declare(color, White, white),
  23.  
  24.     % Clue 5: The owner of the green house drinks coffee.
  25.     declare(color, Green, green),
  26.     declare(drink, Green, coffee),
  27.  
  28.     % Clue 6: The person who smokes Pall Mall rears birds.
  29.     declare(smokes, PallMallSmoker, 'PallMall'),
  30.     declare(pet, PallMallSmoker, 'Birds'),
  31.  
  32.     % Clue 7: The owner of the yellow house smokes Dunhill.
  33.     declare(color, Yellow, yellow),
  34.     declare(smokes, Yellow, 'Dunhill'),
  35.  
  36.     % Clue 8: The man living in the center house drinks milk.
  37.     center_house(CenterHouse),
  38.     declare(drink, CenterHouse, milk),
  39.  
  40.     % Clue 9: The Norwegian lives in the first house.
  41.     first_house(FirstHouse),
  42.     declare(nationality, FirstHouse, norwegian),
  43.  
  44.     % Clue 10: The man who smokes Blends lives next to the one who keeps cats.
  45.     declare(smokes, SmokesBlends, 'Blends'),
  46.     next_to(SmokesBlends, HouseCat),
  47.     declare(pet, HouseCat, cat),
  48.  
  49.     % Clue 11: The man who keeps horses lives next to the man who smokes Dunhill.
  50.     declare(pet, HorseKeeper, horse),
  51.     next_to(HorseKeeper, SmokesDunhills),
  52.     declare(smokes, SmokesDunhills, 'Dunhill'),
  53.  
  54.     % Clue 12: The owner who smokes BlueMaster drinks beer.
  55.     declare(smokes, SmokesBlueMaster, 'BlueMaster'),
  56.     declare(drink, SmokesBlueMaster, beer),
  57.  
  58.     % Clue 13: The German smokes Prince.
  59.     declare(nationality, German, german),
  60.     declare(smokes, German, 'Prince'),
  61.  
  62.     % Clue 14: The Norwegian lives next to the blue house.
  63.     declare(nationality, Norwegian, norwegian),
  64.     next_to(Norwegian, HouseBlue),
  65.     declare(color, HouseBlue, blue),
  66.  
  67.     % Clue 15: The man who smokes Blends has a neighbor who drinks water.
  68.     declare(smokes, BlendsSmoker, 'Blends'),
  69.     next_to(BlendsSmoker, WaterDrinker),
  70.     declare(drink, WaterDrinker, water),
  71.  
  72.     % Determine who owns the fish
  73.     declare(pet, FishOwner, fish),
  74.     declare(nationality, FishOwner, Owner).
  75.  
  76. % Initialize Houses as a list of uninitialized existss
  77. init_houses(NatNumber) :-
  78.     create_empty_list(NatNumber, Houses),
  79.     b_setval(neighborhood, Houses).
  80.  
  81. % Create an empty list of length N using Nat numbers
  82. create_empty_list('Z', 'Nil').
  83. create_empty_list(['S', N], '[|]'(Exists, Tail)) :-
  84.     create_exists(Exists),
  85.     create_empty_list(N, Tail).
  86.  
  87. create_exists('[|]'(_, _)).
  88.  
  89.  
  90.  
  91. % Helper predicates
  92.  
  93. % Predicate: L is immediately to the left of R in the list
  94. left_of(L, R) :-
  95.     b_getval(neighborhood, Houses),
  96.     left_of_list(L, R, Houses).
  97.  
  98. left_of_list(L, R, '[|]'(L, '[|]'(R, _))).
  99. left_of_list(L, R, '[|]'(_, Rest)) :-
  100.     left_of_list(L, R, Rest).
  101.  
  102. % Predicate: A and B are next to each other in the list
  103. next_to(A, B) :-
  104.     left_of(A, B);
  105.     left_of(B, A).
  106.  
  107. % Predicate to get the first house in the list
  108. first_house(First) :-
  109.     % left_of(First, H2), left_of(H2, Center), left_of(Center, H4), left_of(H4, _H5).
  110.     b_getval(neighborhood, Houses),
  111.     nth0_nat('Z', Houses, First).
  112.  
  113. % Predicate to get the center house in the list
  114. center_house(Center) :-
  115.     %left_of(_H1, H2), left_of(H2, Center), left_of(Center, H4), left_of(H4, _H5).
  116.     b_getval(neighborhood, Houses),
  117.     nth0_nat(['S', ['S', 'Z']], Houses, Center).
  118.  
  119. % =============================
  120. % Property handling predicates
  121. % =============================
  122.  
  123. % General property declaration predicate
  124. declare(PropName, Exists, PropValue) :-
  125.     prop(PropName, Exists, PropValue).
  126.  
  127. prop(PropName, Object, PropValue) :-
  128.     get_prop_num(PropName, NumNat), !,
  129.     nth0_nat(NumNat, Object, PropValue), !,
  130.     something_existing(Object).
  131.  
  132. % General property access predicate
  133. prop2(PropName, Exists, PropValue) :-
  134.     get_prop_num(PropName, NumNat),
  135.     nth0_nat(NumNat, Object, PropValue), !,
  136.     something_existing(Object), Exists = Object.
  137.  
  138. % Ensure Exists is a member of Houses
  139. something_existing(Exists) :-
  140.     b_getval(neighborhood, Houses),
  141.     member_cons(Exists, Houses).
  142.  
  143. % Member predicate for '[|]'/'Nil' lists
  144. member_cons(Elem, '[|]'(Elem, _)).
  145. member_cons(Elem, '[|]'(_, Tail)) :-
  146.     member_cons(Elem, Tail).
  147.  
  148. % Map property names to their positions (numbers) using Nat
  149. get_prop_num(PropName, NumNat) :-
  150.     b_getval(exists_props, PropList),
  151.     (   nth0_nat_exists(NumNat, PropList, Prop),
  152.         Prop == PropName -> true
  153.     ;   append_cons(PropList, '[|]'(PropName, 'Nil'), NewPropList),
  154.         length_nat(PropList, NumNat),
  155.         b_setval(exists_props, NewPropList)
  156.     ).
  157.  
  158. % Zero-based nth predicate using Nat that might extend an open list
  159. nth0_nat('Z', '[|]'(Elem, _), Elem).
  160. nth0_nat(['S', N], '[|]'(_, Rest), Elem) :-
  161.     nth0_nat(N, Rest, Elem).
  162.  
  163. % Zero-based nth predicate using Nat that requires bound existss
  164. nth0_nat_exists('Z', '[|]'(Elem, _), Elem) :- nonvar(Elem).
  165. nth0_nat_exists(['S', N], '[|]'(_, Rest), Elem) :-
  166.     nonvar(Rest),
  167.     nth0_nat_exists(N, Rest, Elem).
  168.  
  169. % Length of list using Nat
  170. length_nat('Nil', 'Z').
  171. length_nat('[|]'(_, Rest), ['S', N]) :-
  172.     length_nat(Rest, N).
  173.  
  174. % Append two '[|]'/'Nil' lists
  175. append_cons('Nil', List2, List2).
  176. append_cons('[|]'(H, T), List2, '[|]'(H, T2)) :-
  177.     append_cons(T, List2, T2).
  178.  
  179.  
  180.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement