Chapter #5 Examples in Oz
%%%%%%%%%%%%%%%%%%% Chapter - 5 %%%%%%%%%%%%%%%%%%%%%%
% 5.13
class RemoveAnchovyV
meth init skip end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R ?$)
if {T instanceOf(Anchovy $)}
then {R removeAnchovy($)}
else {New Topping init(T {R removeAnchovy($)})}
end
end
end
% 5.32
class RemoveFishV
meth init skip end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R F ?$)
if {T instanceOf({F getClass($)} $)}
then {R removeFish(F $)}
else {New Topping init(T {R removeFish(F $)})}
end
end
end
% 5.44
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 RemoveIntegerV
meth init skip end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R I ?$)
if {T equals(I $)}
then {R removeInteger(I $)}
else {New Topping init(T {R removeInteger(I $)})}
end
end
end
% 5.51
class RemoveV
meth init skip end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R O ?$)
if {T equals(O $)}
then {R remove(O $)}
else {New Topping init(T {R remove(O $)})}
end
end
end
% 5.86
%% Note: N may get interspersed multiple times?
class SubstituteFishV
meth init skip end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R N O ?$)
if {T equals(O $)}
then {New Topping init(N {R substituteFish(N O $)})}
else {New Topping init(T {R substituteFish(N O $)})}
end
end
end
class SubstituteIntegerV
meth init skip end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R N O ?$)
if {T equals(O $)}
then {New Topping init(N {R substituteInteger(N O $)})}
else {New Topping init(T {R substituteInteger(N O $)})}
end
end
end
% 5.85
class SubstituteV
meth init skip end
meth forBottom(?$) {New Bottom init} end
meth forTopping(T R N O ?$)
if {T equals(O $)}
then {New Topping init(N {R substitute(N O $)})}
else {New Topping init(T {R substitute(N O $)})}
end
end
end
% 5.1
class PieD
feat
raFn : {New RemoveAnchovyV init}
rfFn : {New RemoveFishV init}
riFn : {New RemoveIntegerV init}
remFn : {New RemoveV init}
sfFn : {New SubstituteFishV init}
siFn : {New SubstituteIntegerV init}
subFn : {New SubstituteV init}
meth removeAnchovy(?$) raise abstract() end end
meth removeFish(F ?$) raise abstract() end end
meth removeInteger(I ?$) raise abstract() end end
meth remove(O ?$) raise abstract() end end
meth substituteFish(N O ?$) raise abstract() end end
meth substituteInteger(N O ?$) raise abstract() end end
meth substitute(N O ?$) raise abstract() end end
end
class Bottom from PieD
meth init skip end
meth removeAnchovy(?$) {self.raFn forBottom($)} end
meth removeFish(F ?$) {self.rfFn forBottom($)} end
meth removeInteger(I ?$) {self.riFn forBottom($)} end
meth remove(O ?$) {self.remFn forBottom($)} end
meth substituteFish(N O ?$) {self.sfFn forBottom($)} end
meth substituteInteger(N O ?$) {self.siFn forBottom($)} end
meth substitute(N O ?$) {self.subFn forBottom($)} end
end
class Topping from PieD
feat T R
meth init(Ti Ri)
self.T = Ti
self.R = Ri
end
meth removeAnchovy(?$) {self.raFn forTopping(self.T self.R $)} end
meth removeFish(F ?$) {self.rfFn forTopping(self.T self.R F $)} end
meth removeInteger(I ?$) {self.riFn forTopping(self.T self.R I $)} end
meth remove(O ?$) {self.remFn forTopping(self.T self.R O $)} end
meth substituteFish(N O ?$) {self.sfFn forTopping(self.T self.R N O $)} end
meth substituteInteger(N O ?$) {self.siFn forTopping(self.T self.R N O $)} end
meth substitute(N O ?$) {self.subFn forTopping(self.T self.R N O $)} end
end
% 5.3
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
% 5.4
{Browse 4#{New Topping init({New Anchovy init}
{New Topping init({New Tuna init}
{New Topping init({New Anchovy init}
{New Bottom init})})})}}
{Browse 4#{New Topping init({New Tuna init}
{New Topping init({New Integer init(42)}
{New Topping init({New Anchovy init}
{New Topping init({New Integer init(5)}
{New Bottom init})})})})}}
% 5.5
{Browse 5#{{New Topping init({New Salmon init}
{New Topping init({New Anchovy init}
{New Topping init({New Tuna init}
{New Topping init({New Anchovy init}
{New Bottom init})})})})} removeAnchovy($)}}
% 5.6
{Browse 6#{{New Topping init({New Salmon init}
{New Topping init({New Tuna init}
{New Bottom init})})} removeAnchovy($)}}
% 5.34
{Browse 34#{{New Topping init({New Anchovy init}
{New Bottom init})} removeFish({New Anchovy init} $)}}
% 5.41
{Browse 41#{{New Topping init({New Integer init(2)}
{New Topping init({New Integer init(3)}
{New Topping init({New Integer init(2)}
{New Bottom init})})})} removeInteger({New Integer init(3)} $)}}
% 5.56
{Browse 56#{{New Topping init({New Integer init(2)}
{New Topping init({New Integer init(3)}
{New Topping init({New Integer init(2)}
{New Bottom init})})})} remove({New Integer init(3)} $)}}
% 5.57
{Browse 57#{{New Topping init({New Anchovy init}
{New Bottom init})} remove({New Anchovy init} $)}}
% 5.63
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
class OneMoreThan from NumD
feat Predecessor
meth init(P)
self.Predecessor = P
end
meth getClass(?$) OneMoreThan end
meth instanceOf(N ?$) N == OneMoreThan orelse NumD,instanceOf(N $) end
meth equals(N ?$)
{Browse self#N}
{N instanceOf(OneMoreThan $)} andthen
{self.Predecessor equals(N.Predecessor $)}
end
end
% 5.58
{Browse 58#{{New Topping init({New Anchovy init}
{New Topping init({New Integer init(3)}
{New Topping init({New Zero init}
{New Bottom init})})})} remove({New Integer init(3)} $)}}
% 5.59 | 5.76
{Browse 59#{{New Topping init({New Anchovy init}
{New Topping init({New Integer init(3)}
{New Topping init({New Zero init}
{New Bottom init})})})} remove({New Zero init} $)}}
% 5.80
{Browse 59#{{New Topping init({New Anchovy init}
{New Topping init({New Tuna init}
{New Topping init({New Anchovy init}
{New Bottom init})})})}
substituteFish({New Salmon init} {New Anchovy init} $)}}
|