TRS Chapter #04 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 4.1 fun {Mem X L} case L of nil then false [] H|T then if H == X then L else {Mem X T} end end end {Browse 1#{Mem tofu [a b tofu d peas e]}} % 4.2 {Browse 2#{Mem tofu [a b peas d peas e]}} % 4.3 local Out in {Mem tofu [a b tofu d peas e]} = Out {Browse 3#Out} end % 4.4 {Browse 4# {Mem peas {Mem tofu [a b tofu d peas e]}}} % 4.5 {Browse 5# {Mem tofu {Mem tofu [a b tofu d tofu e]}}} % 4.6 {Browse 6# {Mem tofu {Mem tofu [a b tofu d tofu e]}.2}} % 4.7 fun {Memo X L Out} choice L = nil fail [] L = X|_ L = Out [] T in L = _|T {Memo X T Out} end end % 4.10 {Browse 10# {SolveOne fun {$} Out in {Memo tofu [a b tofu d tofu e] Out} end}} % 4.11 {Browse 11# {SolveOne fun {$} Out X in {Memo tofu [a b X d tofu e] Out} end}} % 4.12 {Browse 12# {SolveAll fun {$} R in _ = {Memo R [a b tofu d tofu e] [tofu d tofu e]} R end}} % 4.13 {Browse 13# {SolveAll fun {$} Q in _ = {Memo tofu [tofu e] [tofu e]} true = Q end}} % 4.14 {Browse 14# {SolveAll fun {$} Q in _ = {Memo tofu [tofu e] [tofu]} true = Q end}} % 4.15 {Browse 15# {SolveAll fun {$} X in _ = {Memo tofu [tofu e] [X e]} X end}} % 4.16 {Browse 16# {SolveAll fun {$} X in _ = {Memo tofu [tofu e] [peas e]} X end}} % 4.17 {Browse 17# {SolveAll fun {$} Out X in {Memo tofu [a b X d tofu e] Out} end}} % 4.18 {Browse 18# {SolveN 12 fun {$} Z U in _ = {Memo tofu a|b|tofu|d|tofu|e|Z U} Z end}} % 4.21 fun {Memo2 X L Out} choice L = X|_ L = Out [] T in L = _|T {Memo2 X T Out} end end % 4.22 fun {Rember X L} case L of nil then nil [] H|T then if H == X then T else H|{Rember X T} end end end % 4.23 {Browse 23#{Rember peas [a b peas d peas e]}} % 4.24 fun {Rembero X L Out} choice L = nil nil = Out [] L = X|Out [] H T Res in L = H|T _ = {Rembero X T Res} H|Res = Out end end % 4.30 {Browse 30# {SolveOne fun {$} Out Y in _ = {Rembero peas [a b Y d peas e] Out} Out end}} % 4.31 {Browse 31# {SolveAll fun {$} Out Y Z in _ = {Rembero Y [a b Y d Z e] Out} Out end}} % 4.49 {Browse 49# {SolveAll fun {$} R Y Z in _ = {Rembero Y [Y d Z e] [Y d e]} [Y Z] = R end}} % 4.57 {Browse 57# {SolveN 13 fun {$} W Y Z Out in _ = {Rembero Y a|b|Y|d|Z|W Out} W end}} % 4.68 fun {Surpriseo S} {Rembero S [a b c] [a b c]} end % 4.69 {Browse 69# {SolveAll fun {$} R in d = R _ = {Surpriseo R} R end}} % 4.70 {Browse 70# {SolveAll fun {$} R in _ = {Surpriseo R} R end}} % 4.71 {Browse 71# {SolveAll fun {$} R in _ = {Surpriseo R} b = R end}} % 4.72 {Browse 72# {SolveAll fun {$} R in b = R _ = {Surpriseo R} R end}} |