Chapter #7 Examples in Oz %%%%%%%%%%%%%%%%%%% Chapter - 7 %%%%%%%%%%%%%%%%%%%%%% % 7.7 class FruitD meth getClass(?$) FruitD end meth instanceOf(F ?$) F == FruitD end meth equals(F ?$) {F instanceOf(FruitD $)} end end class Peach from FruitD meth init skip end meth getClass(?$) Peach end meth instanceOf(F ?$) F == Peach orelse FruitD,instanceOf(F $) end meth equals(F ?$) {F instanceOf(Peach $)} end end class Apple from FruitD meth init skip end meth getClass(?$) Apple end meth instanceOf(F ?$) F == Apple orelse FruitD,instanceOf(F $) end meth equals(F ?$) {F instanceOf(Apple $)} end end class Pear from FruitD meth init skip end meth getClass(?$) Pear end meth instanceOf(F ?$) F == Pear orelse FruitD,instanceOf(F $) end meth equals(F ?$) {F instanceOf(Pear $)} end end class Lemon from FruitD meth init skip end meth getClass(?$) Lemon end meth instanceOf(F ?$) F == Lemon orelse FruitD,instanceOf(F $) end meth equals(F ?$) {F instanceOf(Lemon $)} end end class Fig from FruitD meth init skip end meth getClass(?$) Fig end meth instanceOf(F ?$) F == Fig orelse FruitD,instanceOf(F $) end meth equals(F ?$) {F instanceOf(Fig $)} end end class TreeD meth accept(Ask ?$) raise abstract() end end end class Bud from TreeD meth init skip end meth accept(Ask ?$) {Ask forBud($)} end end class Flat from TreeD feat F T meth init(Fi Ti) self.F = Fi self.T = Ti end meth accept(Ask ?$) {Ask forFlat(self.F self.T $)} end end class Split from TreeD feat L R meth init(Li Ri) self.L = Li self.R = Ri end meth accept(Ask ?$) {Ask forSplit(self.L self.R $)} end end % 7.11 class TreeVisitorI meth forBud(?$) raise abstract() end end meth forFlat(F T ?$) raise abstract() end end meth forSplit(L R ?$) raise abstract() end end end % 7.16 class BlsFlat from TreeVisitorI meth init skip end meth forBud(?$) true end meth forFlat(F T ?$) {T accept(self $)} end meth forSplit(L R ?$) false end end % 7.1 local T in T = {New Flat init( {New Apple init} {New Flat init( {New Peach init} {New Bud init})})} {Browse 1#{T accept({New BlsFlat init} $)}} end % 7.2 local T in T = {New Flat init( {New Pear init} {New Bud init})} {Browse 2#{T accept({New BlsFlat init} $)}} end % 7.3 local T in T = {New Split init( {New Bud init} {New Flat init( {New Fig init} {New Split init( {New Bud init} {New Bud init})})})} {Browse 3#{T accept({New BlsFlat init} $)}} end % 7.4 local T in T = {New Split init( {New Split init( {New Bud init} {New Flat init( {New Lemon init} {New Bud init})})} {New Flat init( {New Fig init} {New Split init( {New Bud init} {New Bud init})})})} {Browse 4#{T accept({New BlsFlat init} $)}} end % 7.17 class BlsSplit from TreeVisitorI meth init skip end meth forBud(?$) true end meth forFlat(F T ?$) false end meth forSplit(L R ?$) if {L accept(self $)} then {R accept(self $)} else false end end end % 7.24 local T in T = {New Bud init} {Browse 24#{T accept({New BlsSplit init} $)}} end % 7.25 local T in T = {New Split init( {New Split init( {New Bud init} {New Split init( {New Bud init} {New Bud init})})} {New Split init( {New Bud init} {New Split init( {New Bud init} {New Bud init})})})} {Browse 25#{T accept({New BlsSplit init} $)}} end % 7.27 class BHasFruit from TreeVisitorI meth init skip end meth forBud(?$) false end meth forFlat(F T ?$) true end meth forSplit(L R ?$) if {L accept(self $)} then true else {R accept(self $)} end end end % 7.26 local T in T = {New Split init( {New Split init( {New Bud init} {New Split init( {New Bud init} {New Bud init})})} {New Split init( {New Bud init} {New Split init( {New Bud init} {New Bud init})})})} {Browse 26#{T accept({New BHasFruit init} $)}} end % 7.47 class IHeightV from TreeVisitorI meth init skip end meth forBud(?$) 0 end meth forFlat(F T ?$) {T accept(self $)} + 1 end meth forSplit(L R ?$) {Max {L accept(self $)} {R accept(self $)}} + 1 end end % Note: Because Oz is dynamically typed, no need to build bool vs. int interface % 7.28 local T in T = {New Split init( {New Split init( {New Bud init} {New Flat init( {New Lemon init} {New Bud init})})} {New Flat init( {New Fig init} {New Split init( {New Bud init} {New Bud init})})})} {Browse 28#{T accept({New IHeightV init} $)}} end % 7.29 local T in T = {New Split init( {New Bud init} {New Flat init( {New Lemon init} {New Bud init})})} {Browse 29#{T accept({New IHeightV init} $)}} end % 7.30 local T in T = {New Flat init( {New Lemon init} {New Bud init})} {Browse 30#{T accept({New IHeightV init} $)}} end % 7.31 local T in T = {New Bud init} {Browse 31#{T accept({New IHeightV init} $)}} end % 7.35 local T in T = {New Split init( {New Split init( {New Bud init} {New Bud init})} {New Flat init( {New Fig init} {New Flat init( {New Lemon init} {New Flat init( {New Apple init} {New Bud init})})})})} {Browse 35#{T accept({New IHeightV init} $)}} end % 7.48 local T in T = {New Split init( {New Bud init} {New Bud init})} {Browse 48#{T accept({New IHeightV init} $)}} end % 7.55 class TSubstituteV from TreeVisitorI feat N O meth init(Ni Oi) self.N = Ni self.O = Oi end meth forBud(?$) {New Bud init} end meth forFlat(F T ?$) if {self.O equals(F $)} then {New Flat init(self.N {T accept(self $)})} else {New Flat init(F {T accept(self $)})} end end meth forSplit(L R ?$) {New Split init({L accept(self $)} {R accept(self $)})} end end % 7.50 local T in T = {New Split init( {New Split init( {New Flat init( {New Fig init} {New Bud init} )} {New Flat init( {New Fig init} {New Bud init} )} )} {New Flat init( {New Fig init} {New Flat init( {New Lemon init} {New Flat init( {New Apple init} {New Bud init} )} )} )} )} {Browse 50#{T accept({New TSubstituteV init({New Apple init} {New Fig init})} $)}} end % 7.56 class IOccursV from TreeVisitorI feat A meth init(Ai) self.A = Ai end meth forBud(?$) 0 end meth forFlat(F T ?$) if {F equals(self.A $)} then {T accept(self $)} + 1 else {T accept(self $)} end end meth forSplit(L R ?$) {L accept(self $)} + {R accept(self $)} end end |