Chapter #6 Examples in Oz % Defined in previous chapters class FishD meth getClass(?$) FishD end meth instanceOf(F ?$) F == FishD end meth equals(F ?$) {F instanceOf(FishD $)} end end class Anchovy from FishD meth init skip end meth getClass(?$) Anchovy end meth instanceOf(F ?$) F == Anchovy orelse FishD,instanceOf(F $) end meth equals(F ?$) {F instanceOf(Anchovy $)} end end class Salmon from FishD meth init skip end meth getClass(?$) Salmon end meth instanceOf(F ?$) F == Salmon orelse FishD,instanceOf(F $) end meth equals(F ?$) {F instanceOf(Salmon $)} end end class Tuna from FishD meth init skip end meth getClass(?$) Tuna end meth instanceOf(F ?$) F == Tuna orelse FishD,instanceOf(F $) end meth equals(F ?$) {F instanceOf(Tuna $)} end end class Integer feat X meth init(Xi) self.X = Xi end meth value(?$) self.X end meth getClass(?$) Integer end meth instanceOf(F ?$) F == Integer end meth equals(F ?$) {F instanceOf(Integer $)} andthen {F value($)} == self.X end end class NumD meth getClass(?$) NumD end meth instanceOf(F ?$) F == NumD end meth equals(F ?$) {F instanceOf(NumD $)} end end class Zero from NumD meth init skip end meth getClass(?$) Zero end meth instanceOf(N ?$) N == Zero orelse NumD,instanceOf(N $) end meth equals(N ?$) {N instanceOf(Zero $)} end end %%%%%%%%%%%%%%%%%%% Chapter - 6 %%%%%%%%%%%%%%%%%%%%%% % 6.9 class PieD meth remove(RemFn O ?$) raise abstract() end end meth substitute(SubFn N O ?$) raise abstract() end end end class Bottom from PieD meth init skip end meth remove(RemFn O ?$) {RemFn forBottom(O $)} end meth substitute(SubFn N O ?$) {SubFn forBottom(N O $)} end end class Topping from PieD feat T R meth init(Ti Ri) self.T = Ti self.R = Ri end meth remove(RemFn O ?$) {RemFn forTopping(self.T self.R O $)} end meth substitute(SubFn N O ?$) {SubFn forTopping(self.T self.R N O $)} end end % 6.13 class RemoveV meth init skip end meth forBottom(O ?$) {New Bottom init} end meth forTopping(T R O ?$) if {O equals(T $)} then {R remove(self O $)} else {New Topping init(T {R remove(self O $)})} end end end class SubstituteV meth init skip end meth forBottom(N O ?$) {New Bottom init} end meth forTopping(T R N O ?$) if {O equals(T $)} then {New Topping init(N {R substitute(self N O $)})} else {New Topping init(T {R substitute(self N O $)})} end end end % 6.21 {Browse 21#{{New Topping init({New Anchovy init} {New Topping init({New Integer init(3)} {New Topping init({New Zero init} {New Bottom init})})})} remove({New RemoveV init} {New Zero init} $)}} % 6.23 {Browse 23#{{New Topping init({New Integer init(3)} {New Topping init({New Integer init(2)} {New Topping init({New Integer init(3)} {New Bottom init})})})} substitute({New SubstituteV init} {New Integer init(5)} {New Integer init(3)} $)}} % 6.30 class Substitute2V feat N O meth init(Ni Oi) self.N = Ni self.O = Oi end meth forBottom(?$) {New Bottom2 init} end meth forTopping(T R ?$) if {self.O equals(T $)} then {New Topping2 init(self.N {R substitute(self $)})} else {New Topping2 init(T {R substitute(self $)})} end end end class Remove2V feat O meth init(Oi) self.O = Oi end meth forBottom(?$) {New Bottom2 init} end meth forTopping(T R ?$) if {self.O equals(T $)} then {R remove(self $)} else {New Topping2 init(T {R remove(self $)})} end end end % 6.37 class Pie2D meth remove(RemFn ?$) raise abstract() end end meth substitute(SubFn ?$) raise abstract() end end end class Bottom2 from Pie2D meth init skip end meth remove(RemFn ?$) {RemFn forBottom($)} end meth substitute(SubFn ?$) {SubFn forBottom($)} end end class Topping2 from Pie2D feat T R meth init(Ti Ri) self.T = Ti self.R = Ri end meth remove(RemFn ?$) {RemFn forTopping(self.T self.R $)} end meth substitute(SubFn ?$) {SubFn forTopping(self.T self.R $)} end end % 6.34 {Browse 34#{{New Topping2 init({New Integer init(3)} {New Topping2 init({New Integer init(2)} {New Topping2 init({New Integer init(3)} {New Bottom2 init})})})} substitute( {New Substitute2V init( {New Integer init(5)} {New Integer init(3)})} $)}} % 6.35 {Browse 35#{{New Topping2 init({New Integer init(3)} {New Topping2 init({New Integer init(2)} {New Topping2 init({New Integer init(3)} {New Bottom2 init})})})} substitute( {New Substitute2V init( {New Integer init(7)} {New Integer init(2)})} $)}} % 6.44 class PieVisitorI meth forBottom(?$) raise abstract() end end meth forTopping(T R ?$) raise abstract() end end end % 6.50 class Remove3V from PieVisitorI feat O meth init(Oi) self.O = Oi end meth forBottom(?$) {New Bottom3 init} end meth forTopping(T R ?$) if {self.O equals(T $)} then {R accept(self $)} else {New Topping3 init(T {R accept(self $)})} end end end class Pie3D meth accept(Ask ?$) raise abstract() end end end class Bottom3 from Pie3D meth init skip end meth accept(Ask ?$) {Ask forBottom($)} end end class Topping3 from Pie3D feat T R meth init(Ti Ri) self.T = Ti self.R = Ri end meth accept(Ask ?$) {Ask forTopping(self.T self.R $)} end end {Browse 50#{{New Topping3 init({New Integer init(3)} {New Topping3 init({New Integer init(2)} {New Topping3 init({New Integer init(3)} {New Bottom3 init})})})} accept( {New Remove3V init({New Integer init(2)})} $)}} % 6.55 class Substitute3V from PieVisitorI feat N O meth init(Ni Oi) self.N = Ni self.O = Oi end meth forBottom(?$) {New Bottom3 init} end meth forTopping(T R ?$) if {self.O equals(T $)} then {New Topping3 init(self.N {R accept(self $)})} else {New Topping3 init(T {R accept(self $)})} end end end {Browse 55#{{New Topping3 init({New Integer init(3)} {New Topping3 init({New Integer init(2)} {New Topping3 init({New Integer init(3)} {New Bottom3 init})})})} accept( {New Substitute3V init( {New Integer init(5)} {New Integer init(3)})} $)}} % 6.67 class LimitedSubstituteV from PieVisitorI feat C N O meth init(Ci Ni Oi) self.C = Ci self.N = Ni self.O = Oi end meth forBottom(?$) {New Bottom3 init} end meth forTopping(T R ?$) if self.C == 0 then {New Topping3 init(T R)} else if {self.O equals(T $)} then {New Topping3 init(self.N {R accept({New LimitedSubstituteV init(self.C-1 self.N self.O)} $)})} else {New Topping3 init(T {R accept(self $)})} end end end end % 6.59 {Browse 59#{{New Topping3 init({New Anchovy init} {New Topping3 init({New Tuna init} {New Topping3 init({New Anchovy init} {New Topping3 init({New Tuna init} {New Topping3 init({New Anchovy init} {New Bottom3 init})})})})})} accept( {New LimitedSubstituteV init( 2 {New Salmon init} {New Anchovy init})} $)}} |