| (:.) infixr 0 WhileExpr WhileExpr
| While WhileBool Do WhileExpr
| Skip
- | E.e: WExpr e & eval e
+ | E.e: WExpr e & eval e & print e
:: WhileBool
= Bool Bool
| (==.) infix 4 WhileInt WhileInt
| (&.) infix 3 WhileBool WhileBool
| Not WhileBool
- | E.e: WBool e & evalb e
+ | E.e: WBool e & evalb e & print e
:: WhileInt
= Int Int
| Var String
| (+.) infixl 6 WhileInt WhileInt
- | E.e: WInt e & evali e
+ | E.e: WInt e & evali e & print e
class gamma g
where
instance eval WhileExpr
instance evali WhileInt
instance evalb WhileBool
+
+class print m :: m -> String
+
+instance print WhileExpr
+instance print WhileInt
+instance print WhileBool
evalb (Not a) = not o evalb a
evalb (WBool e) = evalb e
+instance print WhileExpr
+where
+ print (i =. v) = i +++ " := " +++ print v
+ print (If b _ t _ e) = "If " +++ print b +++ " then " +++ print t +++ " else " +++ print e
+ print (a :. b) = print a +++ "; " +++ print b
+ print x=:(While b _ e) = "While " +++ print b +++ " do " +++ print e
+ print Skip = "Skip"
+ print (WExpr e) = print e
+
+instance print WhileInt
+where
+ print (Int i) = toString i
+ print (Var s) = s
+ print (a +. b) = print a +++ " + " +++ print b
+ print (WInt e) = print e
+
+instance print WhileBool
+where
+ print (Bool b) = toString b
+ print (a ==. b) = print a +++ " = " +++ print b
+ print (a &. b) = print a +++ " && " +++ print b
+ print (Not a) = "!" +++ print a
+ print (WBool e) = print e
+
Start = (eval ("a" =. Int 42 :. While (Bool False) Do ("b" =. Int 4)) emptyGamma) "a"
(*.) a b :== WInt (WMult a b)
instance evali WMult
+instance print WMult
import while
instance evali WMult where evali (WMult a b) = \g->evali a g * evali b g
+instance print WMult where print (WMult a b) = print a +++ " * " +++ print b
Repeat e Until b :== WExpr (WRepeat e Until b)
instance eval WRep
+instance print WRep
implementation module whileRep
+import StdEnv
import while
:: WRep = WRepeat WhileExpr Until WhileBool
-instance eval WRep where eval (WRepeat e Until b) = eval (e :. While b Do e)
+instance eval WRep where eval (WRepeat e _ b) = eval (e :. While b Do e)
+instance print WRep where print (WRepeat e _ b) = "Repeat " +++ print e +++ " until " +++ print b