mirror of https://github.com/lianthony/NT4.0
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
186 lines
3.3 KiB
186 lines
3.3 KiB
/* Small Prolog solution of water pouring problem. */
|
|
|
|
((length () Count)
|
|
(eq Count 0)
|
|
)
|
|
((length (H|T) Count)
|
|
(length T Tcount)
|
|
(iplus Tcount 1 Count)
|
|
)
|
|
|
|
( (minList (Min) Min)
|
|
(cut)
|
|
)
|
|
( (minList (H S|T) Min)
|
|
(length H HL)
|
|
(length S SL)
|
|
(not (iless SL HL))
|
|
(cut)
|
|
(minList (H|T) Min)
|
|
)
|
|
( (minList (H S|T) Min)
|
|
(length H HL)
|
|
(length S SL)
|
|
(iless SL HL)
|
|
(cut)
|
|
(minList (S|T) Min)
|
|
)
|
|
( (concat3 () Result Result)
|
|
(cut)
|
|
)
|
|
( (concat3 (H|T) Old New)
|
|
(integer H)
|
|
(cut)
|
|
(string_from H Sh)
|
|
(string_concat Old Sh Temp)
|
|
(concat3 T Temp New)
|
|
)
|
|
( (concat3 (H|T) Old New)
|
|
(string H)
|
|
(cut)
|
|
(string_concat Old H Temp)
|
|
(concat3 T Temp New)
|
|
)
|
|
( (concat List String)
|
|
(concat3 List "" String)
|
|
)
|
|
|
|
( (method X Y M)
|
|
(findall P (water X Y P) L)
|
|
(minList L M)
|
|
)
|
|
( (water X Y H)
|
|
(state X Y () H)
|
|
/* (nl)
|
|
(writes "Solution found:")
|
|
(showList H)
|
|
*/
|
|
)
|
|
( (trail X Y Old (New|Old))
|
|
(imult X 10 T)
|
|
(iplus T Y New)
|
|
)
|
|
|
|
/* Goal node: 7 liter has 4 liters */
|
|
|
|
( (state 4 _ _ ())
|
|
(cut)
|
|
)
|
|
|
|
/* Trail check: fail if we've seen this state before */
|
|
|
|
( (state X Y Z _)
|
|
(imult X 10 T)
|
|
(iplus T Y Q)
|
|
(member Q Z)
|
|
(cut)
|
|
(fail)
|
|
)
|
|
|
|
/* Pour from 7 into 5 until 5 full */
|
|
( (state X Y Z H)
|
|
(iless 0 X)
|
|
(iless Y 5)
|
|
(trail X Y Z N)
|
|
(iminus 5 Y D)
|
|
(not (iless X D))
|
|
(iminus X D U)
|
|
(iplus Y D V)
|
|
(state U V N G)
|
|
(concat ("pour 7 -> 5 =" U " " V) F)
|
|
(append (F) G H)
|
|
)
|
|
/* Pour from 7 into 5 until 7 empty */
|
|
( (state X Y Z H)
|
|
(iless 0 X)
|
|
(iminus 5 Y T1)
|
|
(not (iless T1 X))
|
|
(iplus Y X V)
|
|
(trail X Y Z N)
|
|
(state 0 V N G)
|
|
(concat ("empty 7 -> 5 =" 0 " " V) F)
|
|
(append (F) G H)
|
|
)
|
|
/* Pour from 5 to 7 until 7 full */
|
|
( (state X Y Z H)
|
|
(iless 0 Y)
|
|
(iless X 7)
|
|
(iminus 7 X D)
|
|
(not (iless Y D))
|
|
(iplus X D U)
|
|
(iminus Y D V)
|
|
(trail X Y Z N)
|
|
(state U V N G)
|
|
(concat ("pour 5 -> 7 =" U " " V) F)
|
|
(append (F) G H)
|
|
)
|
|
/* Pour from 7 into 5 until 5 empty */
|
|
( (state X Y Z H)
|
|
(iless 0 Y)
|
|
(iminus 7 X T1)
|
|
(not (iless T1 Y))
|
|
(iplus X Y U)
|
|
(trail X Y Z N)
|
|
(state U 0 N G)
|
|
(concat ("empty 5 -> 7 =" U " " 0) F)
|
|
(append (F) G H)
|
|
)
|
|
/* Fill 7 from tap */
|
|
( (state X Y Z H)
|
|
(not (eq X 7))
|
|
(trail X Y Z N)
|
|
(state 7 Y N G)
|
|
(concat ("fill 7 =" 7 " " Y) F)
|
|
(append (F) G H)
|
|
)
|
|
/* Empty 7 */
|
|
( (state X Y Z H)
|
|
(not (eq X 0))
|
|
(trail X Y Z N)
|
|
(state 0 Y N G)
|
|
(concat ("empty 7 =" 0 " " Y) F)
|
|
(append (F) G H)
|
|
)
|
|
/* Fill 5 from tap */
|
|
( (state X Y Z H)
|
|
(not (eq Y 5))
|
|
(trail X Y Z N)
|
|
(state X 5 N G)
|
|
(concat ("fill 5 =" X " " 5) F)
|
|
(append (F) G H)
|
|
)
|
|
/* Empty 5 */
|
|
( (state X Y Z H)
|
|
(not (eq Y 0))
|
|
(trail X Y Z N)
|
|
(state X 0 N G)
|
|
(concat ("empty 5 =" X " " 0) F)
|
|
(append (F) G H)
|
|
)
|
|
(showList ())
|
|
|
|
( (showList (H|T))
|
|
(writes H)
|
|
(nl)
|
|
(showList T)
|
|
)
|
|
((showSizes)
|
|
(space_left Heap Str Dyn Trail Subst Temp)
|
|
(string_from Heap Sheap)
|
|
(string_from Str Sstr)
|
|
(string_from Dyn Sdyn)
|
|
(string_from Trail Strail)
|
|
(string_from Subst Ssubst)
|
|
(string_from Temp Stemp)
|
|
(writes "Heap=") (writes Sheap) (nl)
|
|
(writes "Str=") (writes Sstr) (nl)
|
|
(writes "Dyn=") (writes Sdyn) (nl)
|
|
(writes "Trail=") (writes Strail) (nl)
|
|
(writes "Subst=") (writes Ssubst) (nl)
|
|
(writes "Temp=") (writes Stemp) (nl)
|
|
)
|
|
( (solve X Y)
|
|
(method X Y M)
|
|
(showList M)
|
|
(clean_temp)
|
|
)
|