; CS 342 ; the code in this handout with tracing prints is found in ; /home/cs342/public/lib/prolog/tracing.pro ; Section 8.1 (infer (member X (cons X L)) from (print member base) (print x = X l = L)) (infer (member X (cons Y M)) from (print entering member recursive) (print x = X y = Y m = M) (member X M) (print exiting member recursive) (print x = X m = M)) -> (infer? (member 1 (cons 1 nil))) member base x = 1 l = nil Satisfied -> (infer? (member 3 (cons 1 (cons 2 (cons 3 nil))))) entering member recursive x = 3 y = 1 m = (cons 2 (cons 3 nil)) entering member recursive x = 3 y = 2 m = (cons 3 nil) member base x = 3 l = nil exiting member recursive x = 3 m = (cons 3 nil) exiting member recursive x = 3 m = (cons 2 (cons 3 nil)) Satisfied -> (infer? (member What (cons 3 (cons 4 nil))) (print) (print What)) member base x = 3 l = (cons 4 nil) 3 Satisfied -> (infer? (member What (cons 3 (cons 4 nil))) (print) (print what = What)) member base x = 3 l = (cons 4 nil) what = 3 Satisfied ; section 8.2.3 (infer (addtoend nil X (cons X nil)) from (print addtoend base) (print x = X)) (infer (addtoend (cons Y L) X (cons Y M)) from (print entering addtoend recursive) (print y = Y l = L x = X m = M) (addtoend L X M) (print exiting addtoend recursive) (print l = L x = X m = M)) -> (infer? (addtoend 4 (cons 1 (cons 2 nil)))) (infer? (addtoend 4 (cons 1 (cons 2 nil)))) Not satisfied ; do you see why? -> (infer? (addtoend (cons 1 (cons 2 nil)) 3 Ans) (print) (print ans = Ans)) entering addtoend recursive y = 1 l = (cons 2 nil) x = 3 m = M1 entering addtoend recursive y = 2 l = nil x = 3 m = M4 addtoend base x = 3 exiting addtoend recursive l = nil x = 3 m = (cons 3 nil) exiting addtoend recursive l = (cons 2 nil) x = 3 m = (cons 2 (cons 3 nil)) ans = (cons 1 (cons 2 (cons 3 nil))) Satisfied -> (infer? (addtoend Ans 3 (cons 1 (cons 2 (cons 3 nil)))) > (print) (print ans = Ans)) entering addtoend recursive y = 1 l = L1 x = 3 m = (cons 2 (cons 3 nil)) entering addtoend recursive y = 2 l = L4 x = 3 m = (cons 3 nil) addtoend base x = 3 exiting addtoend recursive l = nil x = 3 m = (cons 3 nil) exiting addtoend recursive l = (cons 2 nil) x = 3 m = (cons 2 (cons 3 nil)) ans = (cons 1 (cons 2 nil)) Satisfied (infer (mult 0 Y 0) from (print mult base)) (infer (mult X Y Z) from (print entering mult recursive) (print x = X y = Y z = Z v = V w = W) (minus X 1 V) (mult V Y W) (plus W Y Z) (print exiting mult recursive) (print x = X y = Y z = Z v = V w = W)) -> (infer? (mult 3 4 Ans) (print) (print ans = Ans)) entering mult recursive x = 3 y = 4 z = Ans v = V1 w = W1 entering mult recursive x = 2 y = 4 z = W1 v = V5 w = W5 entering mult recursive x = 1 y = 4 z = W5 v = V9 w = W9 mult base exiting mult recursive x = 1 y = 4 z = 4 v = 0 w = 0 exiting mult recursive x = 2 y = 4 z = 8 v = 1 w = 4 exiting mult recursive x = 3 y = 4 z = 12 v = 2 w = 8 ans = 12 Satisfied ; plus can't be run backwards -> (infer? (plus 1 X 5) (print X)) Not satisfied ; neither can mult, but we aren't so lucky as to have it stop... -> (infer? (mult 3 X 12) (print) (print x = X)) entering mult recursive x = 3 y = X z = 12 v = V1 w = W1 entering mult recursive x = 2 y = X z = W1 v = V5 w = W5 entering mult recursive x = 1 y = X z = W5 v = V9 w = W9 mult base entering mult recursive x = 0 y = X z = W9 v = V13 w = W13 entering mult recursive x = -7 y = X z = W37 v = V41 w = W41 entering mult recursive x = -8 y = X z = W41 v = V45 w = W45 entering mult recursive x = -9 y = X z = W45 v = V49 w = W49 entering mult recursive x = -10 y = X z = W49 v = V53 w = W53 ;... I had to stop it here... (infer (fac 0 1) from (print fact basse)) (infer (fac N R) from (print entering fact recursive) (print n = N r = R n1 = N1 r1 = R1) (minus N 1 N1) (fac N1 R1) (mult R1 N R) (print entering fact recursive) (print n = N r = R n1 = N1 r1 = R1)) -> (infer? (fac 3 Ans) (print) (print ans = Ans)) entering fact recursive n = 3 r = Ans n1 = N11 r1 = R11 entering fact recursive n = 2 r = R11 n1 = N15 r1 = R15 entering fact recursive n = 1 r = R15 n1 = N19 r1 = R19 fact basse entering mult recursive x = 1 y = 1 z = R15 v = V15 w = W15 mult base exiting mult recursive x = 1 y = 1 z = 1 v = 0 w = 0 entering fact recursive n = 1 r = 1 n1 = 0 r1 = 1 entering mult recursive x = 1 y = 2 z = R11 v = V26 w = W26 mult base exiting mult recursive x = 1 y = 2 z = 2 v = 0 w = 0 entering fact recursive n = 2 r = 2 n1 = 1 r1 = 1 entering mult recursive x = 2 y = 3 z = Ans v = V37 w = W37 entering mult recursive x = 1 y = 3 z = W37 v = V41 w = W41 mult base exiting mult recursive x = 1 y = 3 z = 3 v = 0 w = 0 exiting mult recursive x = 2 y = 3 z = 6 v = 1 w = 3 entering fact recursive n = 3 r = 6 n1 = 2 r1 = 2 ans = 6 Satisfied (infer (naive-sort L M) from (print entering naive-sort) (print l = L m = M) (permutation L M) (ordered M) (print exiting naive-sort) (print l = L m = M)) (infer (<= X X)) (infer (<= X Y) from (less X Y)) ; (infer (append nil L L) from (print append base) (print l = L)) (infer (append (cons X L) M (cons X N)) from (print entering append recursive) (print x = X l = L m = M n = N) (append L M N) (print exiting append recursive) (print x = X l = L m = M n = N)) ; (infer (ordered nil)) (infer (ordered (cons A nil))) (infer (ordered (cons A (cons B L))) from (<= A B) (ordered (cons B L))) ; (infer (permutation nil nil) from (print permutation base)) (infer (permutation L (cons H T)) from (print entering permutation recursive) (print l = L h = H t = T) (print) (append V (cons H U) L) (print in permutation after first append) (print l = L h = H t = T) (print v = V u = U) (append V U W) (print in permutation after second append) (print l = L h = H t = T) (print v = V u = U w = W) (permutation W T) (print exiting permutation recursive) (print l = L h = H t = T) (print v = V u = U w = W)) -> (infer? (naive-sort nil Ans) (print) (print ans = Ans)) entering naive-sort l = nil m = Ans permutation base exiting naive-sort l = nil m = nil ans = nil Satisfied -> (infer? (naive-sort (cons 2 (cons 1 nil)) Ans) (print) (print ans = Ans)) entering naive-sort l = (cons 2 (cons 1 nil)) m = Ans entering permutation recursive l = (cons 2 (cons 1 nil)) h = H4 t = T4 append base l = (cons 2 (cons 1 nil)) in permutation after first append l = (cons 2 (cons 1 nil)) h = 2 t = T4 v = nil u = (cons 1 nil) append base l = (cons 1 nil) in permutation after second append l = (cons 2 (cons 1 nil)) h = 2 t = T4 v = nil u = (cons 1 nil) w = (cons 1 nil) entering permutation recursive l = (cons 1 nil) h = H19 t = T19 append base l = (cons 1 nil) in permutation after first append l = (cons 1 nil) h = 1 t = T19 v = nil u = nil append base l = nil in permutation after second append l = (cons 1 nil) h = 1 t = T19 v = nil u = nil w = nil permutation base exiting permutation recursive l = (cons 1 nil) h = 1 t = nil v = nil u = nil w = nil exiting permutation recursive l = (cons 2 (cons 1 nil)) h = 2 t = (cons 1 nil) v = nil u = (cons 1 nil) w = (cons 1 nil) entering permutation recursive l = nil h = H34 t = T34 entering append recursive x = 1 l = L22 m = (cons H19 U19) n = nil entering append recursive x = 2 l = L7 m = (cons H4 U4) n = (cons 1 nil) append base l = (cons 1 nil) exiting append recursive x = 2 l = nil m = (cons 1 nil) n = (cons 1 nil) in permutation after first append l = (cons 2 (cons 1 nil)) h = 1 t = T4 v = (cons 2 nil) u = nil entering append recursive x = 2 l = nil m = nil n = N18 append base l = nil exiting append recursive x = 2 l = nil m = nil n = nil in permutation after second append l = (cons 2 (cons 1 nil)) h = 1 t = T4 v = (cons 2 nil) u = nil w = (cons 2 nil) entering permutation recursive l = (cons 2 nil) h = H29 t = T29 append base l = (cons 2 nil) in permutation after first append l = (cons 2 nil) h = 2 t = T29 v = nil u = nil append base l = nil in permutation after second append l = (cons 2 nil) h = 2 t = T29 v = nil u = nil w = nil permutation base exiting permutation recursive l = (cons 2 nil) h = 2 t = nil v = nil u = nil w = nil exiting permutation recursive l = (cons 2 (cons 1 nil)) h = 1 t = (cons 2 nil) v = (cons 2 nil) u = nil w = (cons 2 nil) exiting naive-sort l = (cons 2 (cons 1 nil)) m = (cons 1 (cons 2 nil)) ans = (cons 1 (cons 2 nil)) Satisfied (infer (simplify (diff X X) nil) from (print simplify base) (print x = X)) (infer (simplify (diff (cons X Y) Z) (cons X W)) from (print entering simplify recursive) (print x = X y = Y z = Z w = W) (simplify (diff Y Z) W) (print exiting simplify recursive) (print x = X y = Y z = Z w = W)) (infer (diffappend (diff L X) (diff X Y) (diff L Y)) from (print diffappend base) (print l = L x = X y = Y)) -> (infer? (simplify (diff (cons 1 (cons 2 X)) X) L) (print) (print l = L)) entering simplify recursive x = 1 y = (cons 2 X) z = X w = W1 entering simplify recursive x = 2 y = X z = X w = W4 simplify base x = X exiting simplify recursive x = 2 y = X z = X w = nil exiting simplify recursive x = 1 y = (cons 2 X) z = X w = (cons 2 nil) l = (cons 1 (cons 2 nil)) Satisfied -> (infer? (simplify M (cons 1 (cons 2 nil))) (print) (print m = M)) entering simplify recursive x = 1 y = Y1 z = Z1 w = (cons 2 nil) entering simplify recursive x = 2 y = Y4 z = Z1 w = nil simplify base x = Z1 exiting simplify recursive x = 2 y = Z1 z = Z1 w = nil exiting simplify recursive x = 1 y = (cons 2 Z1) z = Z1 w = (cons 2 nil) m = (diff (cons 1 (cons 2 Z1)) Z1) Satisfied -> (infer? (diffappend (diff (cons 1 (cons 2 X)) X) > (diff (cons 3 (cons 4 Y)) Y) > L) > (print) (print l = L)) diffappend base l = (cons 1 (cons 2 (cons 3 (cons 4 Y)))) x = (cons 3 (cons 4 Y)) y = Y l = (diff (cons 1 (cons 2 (cons 3 (cons 4 Y)))) Y) Satisfied -> (infer? (diffappend (diff (cons 1 (cons 2 X)) X) > L > (diff (cons 1 (cons 2 (cons 3 Z))) Z)) > (print) (print l = L)) diffappend base l = (cons 1 (cons 2 (cons 3 Z))) x = (cons 3 Z) y = Z l = (diff (cons 3 Z) Z) Satisfied