TRS Chapter #01 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 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % 1.6 skip % 1.8 try fail catch _ then skip end % 1.10 try fail catch _ then skip end % 1.11 local Q in true = Q {Browse 11#Q} end % 1.12 local Q in try fail true = Q catch _ then skip end end % 1.13 - 1.14 local Q in skip true = Q {Browse 13#Q} end % 1.15 - 1.16 local R in skip corn = R {Browse 15#R} end % 1.17 local R in try fail corn = R catch _ then skip end end % 1.18 local Q in skip false = Q {Browse 18#Q} end % 1.19 local X in false = X {Browse 19#X} end % 1.20 local X = true in try %% Note: Oz smart enuf to catch unify error at compile time %%false = X skip catch _ then skip end end % 1.21 local X=false in false = X {Browse 21#X} end % 1.22 local X in X = true %% false = X end % 1.23 - 1.25 local Q in local X in true = X true = Q end {Browse 23#Q} end % 1.26 local Q in local X in X = true true = Q end {Browse 26#Q} end % 1.27 local Q in local X in X = true Q = true end {Browse 27#Q} end % 1.28 local X in skip end % 1.29 local X in local X=false in local X in true = X end end {Browse 29#X} end % 1.30 local R in local X Y in [X Y] = R end {Browse 30#R} end % 1.31 local S in local T U in [T U] = S end {Browse 31#S} end % 1.32 local R in local X in local Y=X in local X in [Y X Y] = R end end end {Browse 32#R} end % 1.33 local R in local X in local Y=X in local X in [X Y X] = R end end end {Browse 33#R} end % 1.34 local Q in try false = Q %%true = Q catch _ then skip end end % 1.35 local Q in false = Q false = Q {Browse 35#Q} end % 1.36 local Q in local X=Q in true = X end {Browse 36#Q} end % 1.37 local R in local X in X = R end {Browse 37#R} end % 1.38 local Q in local X in true = X X = Q end {Browse 38#Q} end % 1.39 local Q in local X in X = Q true = X end {Browse 39#Q} end % 1.40 local Q in local X in true = X X = Q end {Browse 40#Q} end % 1.41 {Browse 41# if false then true else false end} % 1.43 try if false then skip else fail end catch _ then skip end % 1.44 try cond fail then skip [] skip then fail end catch _ then skip end % 1.45 cond fail then fail [] skip then skip end % 1.46 cond skip then skip else fail end % 1.47 - 1.49 local X in cond X = olive then skip [] X = oil then skip [] skip then skip end {Browse 47#X} end {Browse 47# {SolveAll fun {$} X in choice X = olive [] X = oil [] fail end end}} {Browse 47#{SolveAll fun {$} choice olive [] oil end end}} % 1.50 {Browse 50# {SolveAll fun {$} X in choice virgin = X fail [] olive = X [] skip X [] oil = X [] fail end end}} {Browse 50#{SolveAll fun {$} X in choice fail virgin [] olive [] X [] oil end end}} % 1.52 {Browse 52# {SolveN 2 fun {$} X in choice extra = X [] virgin = X fail [] olive = X [] oil = X [] fail end end}} {Browse 52#{SolveN 2 fun {$} choice extra [] fail virgin [] olive [] oil end end}} % 1.53 local R in local X Y in split = X pea = Y [X Y] = R end end % 1.54 {Browse 54# {SolveAll fun {$} R X Y in choice split = X pea = Y [X Y] = R [] navy = X bean = Y [X Y] = R [] fail end end}} {Browse 54#{SolveAll fun {$} choice [split pea] [] [navy bean] end end}} % 1.55 {Browse 55# {SolveAll fun {$} R X Y in choice split = X pea = Y [X Y soup] = R [] navy = X bean = Y [X Y soup] = R [] fail end end}} {Browse 55#{SolveAll fun {$} choice [split pea soup] [] [navy bean soup] end end}} % 1.56 local Teacup in Teacup = fun {$} choice tea [] cup end end {Browse 56#{SolveAll Teacup}} end fun {Teacup} choice tea [] cup end end {Browse 56#{SolveAll Teacup}} % 1.57 {Browse 57# {SolveAll fun {$} X Y R in choice {Teacup} = X true = Y [X Y] = R [] false = X true = Y [X Y] = R [] fail end end}} {Browse 57#{SolveAll fun {$} choice [{Teacup} true] [] [false true] end end}} % 1.58 {Browse 58# {SolveAll fun {$} X Y Z R in choice Y = X local X in Z = X end [Y Z] = R [] local X in Y = X end Z = X [Y Z] = R [] fail end end}} % 1.59 {Browse 59# {SolveAll fun {$} X Y Z R in choice Y = X local X in Z = X end false = X [Y Z] = R [] local X in Y = X end Z = X false = X [Y Z] = R [] fail end end}} % 1.60 local Q in Q = local A=Q Q=true in local B=Q Q=false in B end end {Browse 60#Q} end % 1.61 local Q in Q = local A=Q Q=true in local B=X Q=X X=false in cond Q=true then skip [] skip then Q=false end B end end {Browse 61#Q} end |