TRS Chapter #03 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 3.1 fun {IsList P} case P of nil then true [] H|T then {IsList T} else false end end {Browse 1#{IsList [[a] [a b] c]}} % 3.2 {Browse 2#{IsList nil}} % 3.3 {Browse 3#{IsList s}} % 3.4 {Browse 4#{IsList [d a t e]|s}} % 3.5 fun {Listo L} choice L = nil [] H T in L = H|T H|{Listo T} end end % 3.7 {Browse 7#{SolveAll fun {$} X in {Listo [a b X d]} end}} % 3.10 {Browse 10#{SolveOne fun {$} X in {Listo a|b|c|X} end}} % 3.13 %%{Browse 13#{SolveAll fun {$} X in {Listo a|b|c|X} end}} % 3.14 {Browse 14#{SolveN 5 fun {$} X in {Listo a|b|c|X} end}} % 3.15 fun {IsLol L} case L of nil then true [] H|T then if {IsList H} then {IsLol T} else false end else false end end % 3.16 fun {Lolo L} choice L = nil [] H T in L = H|T if {IsList H} then H|{Lolo T} else fail end end end % 3.20 {Browse 20#{SolveOne fun {$} L in {Lolo L} end}} % 3.21 local Q in _ = {SolveAll fun {$} X Y in {Lolo [[a b] [X c] [d Y]]} end} true = Q {Browse 21#Q} end % 3.22 local Q in _ = {SolveOne fun {$} X in {Lolo [a b]|X} end} true = Q {Browse 22#Q} end % 3.23 {Browse 23#{SolveOne fun {$} X in {Lolo [a b]|[c d]|X} end}} % 3.24 {Browse 24#{SolveN 5 fun {$} X in {Lolo [a b]|[c d]|X} end}} % 3.25 {Browse 25#{SolveN 5 fun {$} {Lolo [a b]|[c d]|[nil nil nil nil]} end}} % 3.26 fun {IsTwins L} case L of H|H|nil then true else false end end {Browse 26#{IsTwins [tofu tofu]}} % 3.27 {Browse 27#{IsTwins [e tofu]}} % 3.28 {Browse 28#{IsTwins [g g g]}} % 3.29 fun {IsLot L} case L of H|T then if {IsTwins H} then {IsLot T} else false end else true end end {Browse 29#{IsLot [[g g] [tofu tofu]]}} % 3.30 {Browse {IsLot [[g g] [e tofu]]}} % 3.31 fun {Twinso S} X Y in Y = X|nil S = X|Y end % 3.32 local Q in _ = {Twinso [tofu tofu]} true = Q {Browse 32#Q} end % 3.33 local Z in _ = {Twinso [Z tofu]} {Browse 33#Z} end % 3.36 fun {Twinso2 S} X in S = [X X] end % 3.37 fun {Loto L} choice L = nil [] H T X in L = H|T H = [X X] H|{Loto T} end end % 3.38 {Browse 38#{SolveOne fun {$} Z in {Loto [g g]|Z} end}} % 3.40 {Browse 40#{SolveOne fun {$} {Loto [g g]|nil} end}} % 3.42 {Browse 42#{SolveN 5 fun {$} Z in {Loto [g g]|Z} end}} % 3.45 {Browse 45#{SolveN 5 fun {$} W X Y Z in {Loto [g g]|[e W]|[X Y]|Z} end}} % 3.47 {Browse 47#{SolveOne fun {$} W X Y Z in {Loto [g g]|[e W]|[X Y]|Z} end}} % 3.48 fun {Listofo Predo L} choice L = nil [] H T in L = H|T _ = {Predo H} H|{Listofo Predo T} end end % 3.49 {Browse 49# {SolveN 3 fun {$} Out W X Y Z in [g g]|[e W]|[X Y]|Z = Out {Listofo Twinso Out} end}} % 3.50 fun {Loto2 L} {Listofo Twinso L} end % 3.51 fun {IsMember X L} case L of nil then false [] H|T then if H == X then true else {IsMember X T} end else fail end end % 3.53 {Browse 53#{IsMember olive [virgin olive oil]}} % 3.54 fun {EqCaro L X} T in L = X|T end fun {Membero X L} choice L = nil fail [] L = X|_ X [] T in L = _|T {Membero X T} end end % 3.57 {Browse 57# {SolveAll fun {$} Q in _ = {Membero olive [virgin olive oil]} true = Q end}} % 3.58 {Browse 58# {SolveOne fun {$} Y in _ = {Membero Y [hummus with pita]} Y end}} % 3.59 {Browse 59# {SolveOne fun {$} Y in _ = {Membero Y [with pita]} Y end}} % 3.60 {Browse 60# {SolveOne fun {$} Y in _ = {Membero Y [pita]} Y end}} % 3.61 {Browse 61# {SolveOne fun {$} Y in _ = {Membero Y nil} Y end}} % 3.62 {Browse 62# {SolveAll fun {$} Y in _ = {Membero Y [hummus with pita]} Y end}} % 3.65 fun {Identity L} {SolveAll fun {$} Y in _ = {Membero Y L} Y end} end % 3.66 - 3.67 {Browse 66# {SolveAll fun {$} X in _ = {Membero e [pasta X fagioli]} X end}} % 3.69 {Browse 69# {SolveOne fun {$} X in _ = {Membero e [pasta e X fagioli]} X end}} % 3.70 {Browse 70# {SolveOne fun {$} X in _ = {Membero e [pasta X e fagioli]} X end}} % 3.71 {Browse 71# {SolveAll fun {$} R X Y in _ = {Membero e [pasta X fagioli Y]} [X Y] = R end}} % 3.73 {Browse 73# {SolveOne fun {$} L in _ = {Membero tofu L} L end}} % 3.75 %%{Browse 75# % {SolveAll %% fun {$} L in % _ = {Membero tofu L} %% L % end}} % 3.76 {Browse 76# {SolveN 5 fun {$} L in _ = {Membero tofu L} L end}} % 3.80 fun {Pmembero_1 X L} choice L = nil fail [] L = X|nil [] H T in L = _|T H|{Pmembero_1 X T} end end {Browse 80# {SolveN 5 fun {$} L in {Pmembero_1 tofu L} end}} % 3.81 {Browse 81# {SolveAll fun {$} Q in _ = {Pmembero_1 tofu [a b tofu d tofu]} true = Q end}} % 3.83 fun {Pmembero_2 X L} choice L = nil fail [] L = X|nil [] L = X|_ [] H T in L = _|T H|{Pmembero_2 X T} end end % 3.84 {Browse 84# {SolveAll fun {$} Q in _ = {Pmembero_2 tofu [a b tofu d tofu]} true = Q end}} % 3.86 fun {Pmembero_3 X L} choice L = nil fail [] L = X|nil [] L = X|_|_ [] H T in L = _|T H|{Pmembero_3 X T} end end % 3.88 {Browse 88# {SolveAll fun {$} Q in _ = {Pmembero_3 tofu [a b tofu d tofu]} true = Q end}} % 3.89 {Browse 89# {SolveN 12 fun {$} L in {Pmembero_3 tofu L} end}} % 3.93 fun {Pmembero X L} choice L = X|_|_ [] L = X|nil [] H T in L = _|T H|{Pmembero X T} end end % 3.94 {Browse 94# {SolveN 12 fun {$} L in {Pmembero tofu L} end}} % 3.95 fun {FirstValue L} {SolveOne fun {$} Y in _ = {Membero Y L} Y end} end % 3.96 {Browse 96#{FirstValue [pasta e fagioli]}} % 3.98 fun {MemberRevo X L} choice L = nil fail [] T in L = _|T {MemberRevo X T} [] L = X|_ X end end % 3.100 {Browse 100# {SolveAll fun {$} X in {MemberRevo X [pasta e fagioli]} end}} % 3.101 fun {ReverseList L} {SolveAll fun {$} Y in {MemberRevo Y L} end} end {Browse 101#{ReverseList [a b c]}} |