Skip to content

Commit

Permalink
Rename Exp -> Expr
Browse files Browse the repository at this point in the history
  • Loading branch information
Eyal Lotem committed Jun 23, 2014
1 parent 877e811 commit 14ea341
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 43 deletions.
40 changes: 20 additions & 20 deletions AlgorithmW.hs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ mgu t@TRecExtend {}
mgu t1 t2 = throwError $ show $
PP.text "types do not unify: " <+> prType t1 <+>
PP.text "vs." <+> prType t2
typeInference :: Map.Map String Scheme -> Exp a -> Either String (Exp (Type, a))
typeInference :: Map.Map String Scheme -> Expr a -> Either String (Expr (Type, a))
typeInference rootEnv rootExpr =
runTI $
do ((_, t), s) <- runWriterT $ ti (,) (TypeEnv rootEnv) rootExpr
Expand All @@ -184,8 +184,8 @@ envLookup key (TypeEnv env) = Map.lookup key env
envInsert :: String -> Scheme -> TypeEnv -> TypeEnv
envInsert key scheme (TypeEnv env) = TypeEnv (Map.insert key scheme env)

ti :: (Type -> a -> b) -> TypeEnv -> Exp a -> TIW (Type, Exp b)
ti f env expr@(Exp pl body) = case body of
ti :: (Type -> a -> b) -> TypeEnv -> Expr a -> TIW (Type, Expr b)
ti f env expr@(Expr pl body) = case body of
ELeaf leaf ->
mkResult (ELeaf leaf) <$>
case leaf of
Expand Down Expand Up @@ -225,33 +225,33 @@ ti f env expr@(Exp pl body) = case body of
(t2, e2') <- ti f (apply s1 env) e2
return $ mkResult (ERecExtend name e1' e2') $ TRecExtend name t1 t2
where
mkResult body' typ = (typ, Exp (f typ pl) body')
mkResult body' typ = (typ, Expr (f typ pl) body')


tiLit :: Lit -> TIW Type
tiLit (LInt _) = return (TCon "Int")
tiLit (LChar _) = return (TCon "Char")

eLet :: String -> Exp () -> Exp () -> Exp ()
eLet name e1 e2 = Exp () $ ELet name e1 e2
eLet :: String -> Expr () -> Expr () -> Expr ()
eLet name e1 e2 = Expr () $ ELet name e1 e2

eAbs :: String -> Exp () -> Exp ()
eAbs name body = Exp () $ EAbs name body
eAbs :: String -> Expr () -> Expr ()
eAbs name body = Expr () $ EAbs name body

eVar :: String -> Exp ()
eVar = Exp () . ELeaf . EVar
eVar :: String -> Expr ()
eVar = Expr () . ELeaf . EVar

eLit :: Lit -> Exp ()
eLit = Exp () . ELeaf . ELit
eLit :: Lit -> Expr ()
eLit = Expr () . ELeaf . ELit

eRecEmpty :: Exp ()
eRecEmpty = Exp () $ ELeaf ERecEmpty
eRecEmpty :: Expr ()
eRecEmpty = Expr () $ ELeaf ERecEmpty

eApp :: Exp () -> Exp () -> Exp ()
eApp f x = Exp () $ EApp f x
eApp :: Expr () -> Expr () -> Expr ()
eApp f x = Expr () $ EApp f x

eRecExtend :: String -> Exp () -> Exp () -> Exp ()
eRecExtend name typ rest = Exp () $ ERecExtend name typ rest
eRecExtend :: String -> Expr () -> Expr () -> Expr ()
eRecExtend name typ rest = Expr () $ ERecExtend name typ rest

eGetField :: Exp () -> String -> Exp ()
eGetField r n = Exp () $ EGetField r n
eGetField :: Expr () -> String -> Expr ()
eGetField r n = Expr () $ EGetField r n
12 changes: 6 additions & 6 deletions Expr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module Expr
( Lit(..)
, Leaf(..)
, Body(..)
, Exp(..), expPayload
, Expr(..), expPayload
, Scheme(..)
, Type(..)
) where
Expand Down Expand Up @@ -36,14 +36,14 @@ data Body exp = EApp exp exp
deriving (Functor, Foldable, Traversable, Generic, Show)
instance NFData exp => NFData (Body exp) where rnf = genericRnf

data Exp a = Exp
data Expr a = Expr
{ _expPayload :: a
, expBody :: !(Body (Exp a))
, expBody :: !(Body (Expr a))
} deriving (Functor, Foldable, Traversable, Generic, Show)
instance NFData a => NFData (Exp a) where rnf = genericRnf
instance NFData a => NFData (Expr a) where rnf = genericRnf

expPayload :: Lens' (Exp a) a
expPayload f (Exp pl body) = (`Exp` body) <$> f pl
expPayload :: Lens' (Expr a) a
expPayload f (Expr pl body) = (`Expr` body) <$> f pl

data Type = TVar String
| TFun Type Type
Expand Down
4 changes: 2 additions & 2 deletions Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ prScheme (Scheme vars t) = PP.text "All" <+>
(PP.punctuate PP.comma (map PP.text vars))
<> PP.text "." <+> prType t

prParenExp :: Exp a -> PP.Doc
prParenExp :: Expr a -> PP.Doc
prParenExp t = case expBody t of
ELet _ _ _ -> PP.parens (prExp t)
EApp _ _ -> PP.parens (prExp t)
Expand All @@ -30,7 +30,7 @@ prLit :: Lit -> PP.Doc
prLit (LInt i) = PP.integer i
prLit (LChar c) = PP.text (show c)

prExp :: Exp a -> PP.Doc
prExp :: Expr a -> PP.Doc
prExp expr =
case expBody expr of
ELeaf (EVar name) -> PP.text name
Expand Down
6 changes: 3 additions & 3 deletions Record.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ flattenRec TRecEmpty = return $ FlatRecord Map.empty Nothing
flattenRec (TVar name) = return $ FlatRecord Map.empty (Just name)
flattenRec t = Left $ "TRecExtend on non-record: " ++ show t

flattenERec :: Exp a -> (Map.Map String (Exp a), Maybe (Exp a))
flattenERec (Exp _ (ERecExtend name val body)) =
flattenERec :: Expr a -> (Map.Map String (Expr a), Maybe (Expr a))
flattenERec (Expr _ (ERecExtend name val body)) =
flattenERec body
& _1 %~ Map.insert name val
flattenERec (Exp _ (ELeaf ERecEmpty)) = (Map.empty, Nothing)
flattenERec (Expr _ (ELeaf ERecEmpty)) = (Map.empty, Nothing)
flattenERec other = (Map.empty, Just other)
24 changes: 12 additions & 12 deletions Test.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,59 +2,59 @@ import AlgorithmW
import Pretty
import qualified Data.Map as Map

exp0 :: Exp ()
exp0 :: Expr ()
exp0 = eLet "id" (eAbs "x" (eVar "x"))
(eVar "id")

exp1 :: Exp ()
exp1 :: Expr ()
exp1 = eLet "id" (eAbs "x" (eVar "x"))
(eApp (eVar "id") (eVar "id"))

exp2 :: Exp ()
exp2 :: Expr ()
exp2 = eLet "id" (eAbs "x" (eLet "y" (eVar "x") (eVar "y")))
(eApp (eVar "id") (eVar "id"))

exp3 :: Exp ()
exp3 :: Expr ()
exp3 = eLet "id" (eAbs "x" (eLet "y" (eVar "x") (eVar "y")))
(eApp (eApp (eVar "id") (eVar "id")) (eLit (LInt 2)))

exp4 :: Exp ()
exp4 :: Expr ()
exp4 = eLet "id" (eAbs "x" (eApp (eVar "x") (eVar "x")))
(eVar "id")

exp5 :: Exp ()
exp5 :: Expr ()
exp5 = eAbs "m" (eLet "y" (eVar "m")
(eLet "x" (eApp (eVar "y") (eLit (LChar 'x')))
(eVar "x")))

exp6 :: Exp ()
exp6 :: Expr ()
exp6 = eApp (eLit (LInt 2)) (eLit (LInt 2))

exp7 :: Exp ()
exp7 :: Expr ()
exp7 = eAbs "vec" $
eRecExtend "newX" (eGetField (eVar "vec") "x") $
eRecExtend "newY" (eGetField (eVar "vec") "y") $
eRecEmpty

exp8 :: Exp ()
exp8 :: Expr ()
exp8 = eLet
"vec" ( eRecExtend "x" (eLit (LInt 5)) $
eRecExtend "y" (eLit (LInt 7)) $
eRecEmpty ) $
eGetField (eVar "vec") "x"

exp9 :: Exp ()
exp9 :: Expr ()
exp9 = eLet
"vec" ( eRecExtend "x" (eLit (LInt 5)) $
eRecExtend "y" (eLit (LInt 7)) $
eRecEmpty ) $
eGetField (eVar "vec") "z"

test :: Exp () -> IO ()
test :: Expr () -> IO ()
test e =
case typeInference Map.empty e of
Left err -> putStrLn $ show (prExp e) ++ "\n " ++ err ++ "\n"
Right (Exp (t, _) _) -> putStrLn $ show (prExp e) ++ " :: " ++ show (prType t) ++ "\n"
Right (Expr (t, _) _) -> putStrLn $ show (prExp e) ++ " :: " ++ show (prType t) ++ "\n"

main :: IO ()
main = mapM_ test [exp0, exp1, exp2, exp3, exp4, exp5, exp6, exp7, exp8, exp9]

0 comments on commit 14ea341

Please sign in to comment.