Evaluate `val' and `var' bindings in the environment in which they were defined
[hcoop/domtool2.git] / src / reduce.sml
index 5d7a1b9..7996580 100644 (file)
@@ -42,6 +42,8 @@ fun freeIn x (b, _) =
       | EWith (e1, e2) => freeIn x e1 orelse freeIn x e2
       | EALam (x', _, b') => x <> x' andalso freeIn x b'
 
+      | EIf (e1, e2, e3) => freeIn x e1 orelse freeIn x e2 orelse freeIn x e3
+
 local
     val freshCount = ref 0
 in
@@ -106,6 +108,8 @@ fun subst x e (bAll as (b, loc)) =
        else
            (EALam (x', p, subst x e b'), loc)
 
+      | EIf (b1, b2, b3) => (EIf (subst x e b1, subst x e b2, subst x e b3), loc)
+
 fun findPrim (e, _) =
     case e of
        EApp (f, x) =>
@@ -133,8 +137,13 @@ fun reduceExp G (eAll as (e, loc)) =
        end
       | EVar x =>
        (case lookupEquation G x of
-            NONE => eAll
-          | SOME e => reduceExp G e)
+            NONE =>
+            (case function x of
+                 NONE => eAll
+               | SOME f => case f [] of
+                               NONE => eAll
+                             | SOME e' => reduceExp G e')
+          | SOME (e, G') => reduceExp G' e)
       | EApp (e1, e2) =>
        let
            val e1' = reduceExp G e1
@@ -174,4 +183,16 @@ fun reduceExp G (eAll as (e, loc)) =
            (EALam (x, p, reduceExp G' e), loc)
        end
 
+      | EIf (e1, e2, e3) =>
+       let
+           val e1' = reduceExp G e1
+           fun e2' () = reduceExp G e2
+           fun e3' () = reduceExp G e3
+       in
+           case e1' of
+               (EVar "true", _) => e2' ()
+             | (EVar "false", _) => e3' ()
+             | _ => (EIf (e1', e2' (), e3' ()), loc)
+       end
+
 end