about summary refs log tree commit diff
path: root/src/HW.hs
diff options
context:
space:
mode:
Diffstat (limited to 'src/HW.hs')
-rw-r--r--src/HW.hs55
1 files changed, 30 insertions, 25 deletions
diff --git a/src/HW.hs b/src/HW.hs
index ee26584..7e4e579 100644
--- a/src/HW.hs
+++ b/src/HW.hs
@@ -1,11 +1,11 @@
-module HW
-  ( fv
-  , subst
-  , normalstep
-  , pickFresh
-  , repeatedly 
-  , printnormal
-  ) where
+module HW (
+    fv,
+    subst,
+    normalstep,
+    pickFresh,
+    repeatedly,
+    printnormal,
+) where
 
 import AST
 import qualified Data.Set as Set
@@ -17,33 +17,38 @@ fv (Var x) = Set.singleton x
 fv (Lam x m) = x `Set.delete` (fv m)
 fv (App m1 m2) = (fv m1) `Set.union` (fv m2)
 
--- | Substitute n for x in e, avoiding name capture
---    subst n x e     e[x := n]
+{- | Substitute n for x in e, avoiding name capture
+    subst n x e     e[x := n]
+-}
 subst :: Expr -> String -> Expr -> Expr
-subst _ _ _ = Var "UNIMPLEMENTED" -- Replace with your solution to problem 2
-
-
+-- subst _ _ _ = Var "UNIMPLEMENTED" -- Replace with your solution to problem 2
+subst n x (Var e)
+    | x == e = n
+    | otherwise = Var e
 
 -- | Take a single step in normal order reduction or return Nothing
 normalstep :: Expr -> Maybe Expr
 normalstep _ = Just (Var "UNIMPLEMENTED") -- Replace with your solution to problem 3
 
-
-
-
--- | Return a "fresh" name not already in the set.
--- Tries x' then x'', etc.
+{- | Return a "fresh" name not already in the set.
+ Tries x' then x'', etc.
+-}
 pickFresh :: Set.Set String -> String -> String
 pickFresh s = pickFresh'
-  where pickFresh' n | n `Set.notMember` s = n
-        pickFresh' n                       = pickFresh' $ n ++ "'"
-               
--- | Repeatedly apply a function to transform a value, returning the list
--- of steps it took.  The result list starts with the given initial value
+  where
+    pickFresh' n | n `Set.notMember` s = n
+    pickFresh' n = pickFresh' $ n ++ "'"
+
+{- | Repeatedly apply a function to transform a value, returning the list
+ of steps it took.  The result list starts with the given initial value
+-}
 repeatedly :: (a -> Maybe a) -> a -> [a]
 repeatedly f = repeatedly'
-  where repeatedly' x = x : case f x of Nothing -> []
-                                        Just y -> repeatedly' y
+  where
+    repeatedly' x =
+        x : case f x of
+            Nothing -> []
+            Just y -> repeatedly' y
 
 -- | Print out the series of normal order reduction steps
 printnormal :: String -> IO ()