About AFP The following Oz code is derived from the examples provided in the book:
      "A Little Java, A Few Patterns" by Matthias Felleisen and Daniel P. Friedman.
      http://www.ccs.neu.edu/home/matthias/BALJ/

Chapter #8 Examples in Oz
%%%%%%%%%%%%%%%%%%% Chapter - 8 %%%%%%%%%%%%%%%%%%%%%%

% 8.1
{Browse 1#(7 + ((4 - 3) * 5))}

% 8.14
class ExprD
   meth accept(Ask ?$) raise abstract() end end
end
class Plus from ExprD
   feat L R
   meth init(Li Ri)
      self.L = Li
      self.R = Ri
   end
   meth accept(Ask ?$) {Ask forPlus(self.L self.R $)} end
end
class Diff from ExprD
   feat L R
   meth init(Li Ri)
      self.L = Li
      self.R = Ri
   end
   meth accept(Ask ?$) {Ask forDiff(self.L self.R $)} end
end
class Prod from ExprD
   feat L R
   meth init(Li Ri)
      self.L = Li
      self.R = Ri
   end
   meth accept(Ask ?$) {Ask forProd(self.L self.R $)} end
end
class Const from ExprD
   feat C
   meth init(Ci) self.C = Ci end
   meth accept(Ask ?$) {Ask forConst(self.C $)} end
end

% 8.13
class ExprVisitorI
   meth forPlus(L R ?$) raise abstract() end end
   meth forDiff(L R ?$) raise abstract() end end
   meth forProd(L R ?$) raise abstract() end end
   meth forConst(C ?$) raise abstract() end end
end

% 8.19 | 8.29
class IntEvalV from ExprVisitorI
   meth init skip end
   meth forPlus(L R ?$) {self plus({L accept(self $)} {R accept(self $)} $)} end
   meth forDiff(L R ?$) {self diff({L accept(self $)} {R accept(self $)} $)} end
   meth forProd(L R ?$) {self prod({L accept(self $)} {R accept(self $)} $)} end
   meth forConst(C ?$) C end
   meth plus(L R ?$) L + R end
   meth diff(L R ?$) L - R end
   meth prod(L R ?$) L * R end
end

% 8.3
local E in
   E = {New Plus init(
            {New Const init(7)}
            {New Prod init(
               {New Diff init(
                  {New Const init(4)}
                  {New Const init(3)})}
               {New Const init(5)})})}
   {Browse 3#{E accept({New IntEvalV init} $)}}
end

% 8.32
class SetD
   meth add(I ?$)
      if {self mem(I $)}
         then self
         else {New Add init(I self)}
      end
   end
   meth mem(I ?$) raise abstract() end end
   meth plus(S ?$) raise abstract() end end
   meth diff(S ?$) raise abstract() end end
   meth prod(S ?$) raise abstract() end end
end

% 8.36
class Empty from SetD
   meth init skip end
   meth mem(N ?$) false end
   meth plus(S ?$) S end
   meth diff(S $) {New Empty init} end
   meth prod(S $) {New Empty init} end
end
class Add from SetD
   feat I S
   meth init(Ii Si)
      self.I = Ii
      self.S = Si
   end
   meth mem(N ?$)
      if self.I == N
         then true
         else {self.S mem(N $)}
      end
   end
   meth plus(T ?$)
      {self.S plus({T add(self.I $)} $)}
   end
   meth diff(T ?$)
      if {T mem(self.I $)}
         then {self.S diff(T $)}
         else {{self.S diff(T $)} add(self.I $)}
      end
   end
   meth prod(T ?$)
      if {T mem(self.I $)}
         then {{self.S prod(T $)} add(self.I $)}
         else {self.S prod(T $)}
      end
   end
end

% 8.9
local E in
   E = {New Plus init(
            {{{New Empty init} add(7 $)} add(5 $)}
            {New Prod init(
               {New Diff init(
                  {{New Empty init} add(4 $)}
                  {{New Empty init} add(3 $)})}
               {{New Empty init} add(5 $)})})}
end

% 8.26
local E in
   E = {New Plus init(
            {New Const init({New Empty init})}
            {New Const init(5)})}
   % {Browse 26#{E accept({New IntEvalV init} $)}}
end

% 8.43
class SetEvalV from IntEvalV
   meth plus(L R ?$) {L plus(R $)} end
   meth diff(L R ?$) {L diff(R $)} end
   meth prod(L R ?$) {L prod(R $)} end
end

% 8.51
local E in
   E = {New Prod init(
            {New Const init({{New Empty init} add(7 $)})}
            {New Const init({{New Empty init} add(3 $)})})}
   {Browse 51#{E accept({New SetEvalV init} $)}}
end

% 8.76
class EvalD
   meth forPlus(L R ?$) {self plus({L accept(self $)} {R accept(self $)} $)} end
   meth forDiff(L R ?$) {self diff({L accept(self $)} {R accept(self $)} $)} end
   meth forProd(L R ?$) {self prod({L accept(self $)} {R accept(self $)} $)} end
   meth forConst(C ?$) C end
   meth plus(L R ?$) raise abstract() end end
   meth diff(L R ?$) raise abstract() end end
   meth prod(L R ?$) raise abstract() end end
end

% 8.78
class IntEval2V from EvalD
   meth init skip end
   meth plus(L R ?$) L + R end
   meth diff(L R ?$) L - R end
   meth prod(L R ?$) L * R end
end
% 8.43
class SetEval2V from EvalD
   meth plus(L R ?$) {L plus(R $)} end
   meth diff(L R ?$) {L diff(R $)} end
   meth prod(L R ?$) {L prod(R $)} end
end

% 8.81
class PieVisitorI
   meth forBottom(?$) raise abstract() end end
   meth forTopping(T R ?$) raise abstract() end end
end

class SubstituteV_ from PieVisitorI
   feat N O
   meth init(Ni Oi)
      self.N = Ni
      self.O = Oi
   end
   meth forBottom(?$) {New Bottom init} end
   meth forTopping(T R ?$)
      if {self.O equals(T $)}
         then {New Topping init(self.N {R accept(self $)})}
         else {New Topping init(T {R accept(self $)})}
      end
   end
end
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 Bottom init} end
   meth forTopping(T R ?$)
      if self.C == 0
         then {New Topping init(T R)}
         else
            if {self.O equals(T $)}
               then {New Topping init(self.N {R accept({New LimitedSubstituteV init(self.C-1 self.N self.O)} $)})}
               else {New Topping init(T {R accept(self $)})}
            end
      end
   end
end

class PieD
   meth accept(Ask ?$) raise abstract() end end
end
class Bottom from PieD
   meth init skip end
   meth accept(Ask ?$) {Ask forBottom($)} end
end
class Topping from PieD
   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

% 8.86
class SubstituteD
   feat n o
   meth init(N O)
      self.n = N
      self.o = O
   end
   meth forBottom(?$) {New Bottom init} end
   meth forTopping(T R ?$) raise abstract() end end
end

% 8.87
class SubstituteV from SubstituteD
   meth init(N O)
      SubstituteD,init(N O)
   end
   meth forTopping(T R ?$)
      if {self.o equals(T $)}
         then {New Topping init(self.n {R accept(self $)})}
         else {New Topping init(T {R accept(self $)})}
      end
   end
end
class LimitedSubstituteV from SubstituteD
   feat C
   meth init(C N O)
      self.C = C
      SubstituteD,init(N O)
   end
   meth forTopping(T R ?$)
      if self.C == 0
         then {New Topping init(T R)}
         else
            if {self.o equals(T $)}
               then {New Topping init(self.n {R accept({New LimitedSubstituteV init(self.C-1 self.n self.o)} $)})}
               else {New Topping init(T {R accept(self $)})}
            end
      end
   end
end

Chris Rathman / Chris.Rathman@tx.rr.com