TRS Chapter #02 Examples in Oz %%%%%%%%%%%%%%%%%%%%%%%%%%% From CTM Chapter 9 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % Lazy problem solving (Solve) % This is the Solve operation, which returns a lazy list of solutions % to a relational program. The list is ordered according to a % depth-first traversal. Solve is written using the computation space % operations of the Space module. fun {Solve Script} {SolStep {Space.new Script} nil} end fun {SolStep S Rest} case {Space.ask S} of failed then Rest [] succeeded then {Space.merge S}|Rest [] alternatives(N) then {SolLoop S 1 N Rest} end end fun lazy {SolLoop S I N Rest} if I>N then Rest elseif I==N then {Space.commit S I} {SolStep S Rest} else Right C in Right={SolLoop S I+1 N Rest} C={Space.clone S} {Space.commit C I} {SolStep C Right} end end fun {SolveOne F} L = {Solve F} in if L==nil then nil else [L.1] end end fun {SolveAll F} L = {Solve F} proc {TouchAll L} if L==nil then skip else {TouchAll L.2} end end in {TouchAll L} L end fun {SolveN N F} L = {Solve F} in {List.take L N} end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 2.1 {Browse 1# local X Y in X = fun {$ A} A end Y = c {X Y} end} % 2.2 local R in local Y X in [X Y] = R end {Browse 2#R} end % 2.3 local R in local V W in local X=V Y=W in [X Y] = R end end {Browse 3#R} end % 2.9 fun {CAR L} case L of H|T then H else fail end end % 2.4 {Browse 4#{CAR [grape raisin pear]}} % 2.5 {Browse 5#{CAR [a c o r n]}} % 2.6 local R in {CAR [a c o r n]} = R {Browse 6#R} end % 2.7 local Q in {CAR [a c o r n]} = a true = Q {Browse 7#Q} end % 2.8 local R in local X Y in {CAR [R Y]} = X pear = X end {Browse 8#R} end % 2.10 {Browse 10# {CAR [grape raisin pear]} | {CAR [[a] [b] [c]]}} % 2.11 local R in local X Y in {CAR [grape raisin pear]} = X {CAR [[a] [b] [c]]} = Y X|Y = R end {Browse 11#R} end % 2.16 fun {CDR L} case L of H|T then T else fail end end % 2.13 {Browse 13#{CDR [grape raisin pear]}} % 2.14 {Browse 14#{CAR {CDR [a c o r n]}}} % 2.15 local R in local V in {CDR [a c o r n]} = V {CAR V} = R end {Browse 15#R} end % 2.17 {Browse 17# {CDR [grape raisin pear]} | {CAR [[a] [b] [c]]}} % 2.18 local R in local X Y in {CDR [grape raisin pear]} = X {CAR [[a] [b] [c]]} = Y X|Y = R end {Browse 18#R} end % 2.19 local Q in {CDR [a c o r n]} = [c o r n] true = Q {Browse 19#Q} end % 2.20 local X in {CDR [c o r n]} = [X r n] {Browse 20#X} end % 2.21 local L in local X in L = _|c|o|r|n|nil L = X|_ a = X end {Browse 21#L} end % 2.22 local L in [[a b c] d e] = L {Browse 22#L} end % 2.23 local X in [X a b c] = [d a b c] {Browse 23#X} end % 2.24 local R in local X Y Z in [e a d X] = R [Y a Z c] = R end {Browse 24#R} end % 2.25 local X in [X a X c] = [d a X c] {Browse 25#X} end % 2.26 local L in local X in [d a X c] = L [X a X c] = L end {Browse 26#L} end % 2.27 local L in local X in [X a X c] = L [d a X c] = L end {Browse 27#L} end % 2.29 local L in local D X Y W S in [W a n s] = S L = _|S L = X|_ b = X L = _|D D = Y|_ e = Y end {Browse 29#L} end % 2.30 fun {IsNil X} X == nil end {Browse 30#{IsNil [grape raisin pear]}} % 2.31 {Browse 31#{IsNil nil}} % 2.32 local Q in %% Note: Oz smart enuf to catch unify error at compile time %%nil = [grape raisin pear] true = Q end % 2.33 local Q in nil = nil true = Q {Browse 33#Q} end % 2.34 local X in nil = X {Browse 34#X} end % 2.36 fun {IsEqual A B} A == B end {Browse 36#{IsEqual pear plum}} % 2.37 {Browse 37#{IsEqual plum plum}} % 2.38 proc {IsEqualo A B} if A == B then skip else fail end end local Q in try {IsEqualo pear plum} true = Q catch _ then skip end end % 2.39 local Q in {IsEqualo plum plum} true = Q {Browse 39#Q} end % 2.41 {Browse 41#(split|pea)} % 2.42 local X in {Browse 42#(split|X)} end % 2.43 fun {IsPair P} case P of _|_ then true else false end end {Browse 43#{IsPair [split]|pea}} % 2.44 {Browse 44#{IsPair nil}} % 2.45 {Browse 45#{IsPair pair}} % 2.46 {Browse 46#{IsPair pear}} % 2.47 {Browse 47#{IsPair [pear]}} % 2.48 {Browse 48#{CAR [pear]}} % 2.49 {Browse 49#{CDR [pear]}} % 2.51 {Browse 51#[split]|pea} % 2.52 local R in local X Y in [X Y]|salad = R end {Browse 52#R} end % 2.53 proc {Pairo P} X Y in try P = X|Y catch _ then case P of _|_ then skip else fail end end end % 2.54 local Q in {Pairo [Q Q]} true = Q {Browse 54#Q} end % 2.55 local Q in try {Pairo nil} catch _ then skip end end % 2.56 local Q in try {Pairo pair} catch _ then skip end end % 2.57 local X in {Pairo X} {Browse 57#X} end % 2.58 local R in {Pairo R|pear} {Browse 58#R} end |