lab16: impl
This commit is contained in:
@@ -51,10 +51,19 @@ typecheck e@(EIf e1 e2 e3) env =
|
|||||||
t2 = typecheck e2 env
|
t2 = typecheck e2 env
|
||||||
t3 = typecheck e3 env
|
t3 = typecheck e3 env
|
||||||
in if t1 == TBool && t2 == t3 then t2 else typecheckFail e
|
in if t1 == TBool && t2 == t3 then t2 else typecheckFail e
|
||||||
typecheck e@(EIsZero e1) env = error "TBD"
|
typecheck e@(EIsZero e1) env = case typecheck e1 env of
|
||||||
typecheck e@(EVar x) env = error "TBD"
|
TInt -> TBool
|
||||||
typecheck e@(ELambda x tin e') env = error "TBD"
|
_ -> typecheckFail e
|
||||||
typecheck e@(EApp e1 e2) env = error "TBD"
|
typecheck e@(EVar x) env = case Map.lookup x env of
|
||||||
|
Just t -> t
|
||||||
|
Nothing -> typecheckFail e
|
||||||
|
typecheck e@(ELambda x tin e') env =
|
||||||
|
let env' = Map.insert x tin env
|
||||||
|
in TFun tin (typecheck e' env')
|
||||||
|
typecheck e@(EApp e1 e2) env =
|
||||||
|
let t1@(TFun tin tout) = typecheck e1 env
|
||||||
|
t2 = typecheck e2 env
|
||||||
|
in if t2 == tin then tout else typecheckFail e
|
||||||
|
|
||||||
|
|
||||||
--Some sample cases
|
--Some sample cases
|
||||||
@@ -67,3 +76,13 @@ test5 = typecheck (EApp (ELambda "x" TInt (EIf (EIsZero (EVar "x")) (ESucc zero)
|
|||||||
bad1 = typecheck (ESucc EFalse) Map.empty
|
bad1 = typecheck (ESucc EFalse) Map.empty
|
||||||
bad2 = typecheck (EApp (ELambda "x" TInt (EIsZero (EVar "x"))) ETrue) Map.empty
|
bad2 = typecheck (EApp (ELambda "x" TInt (EIsZero (EVar "x"))) ETrue) Map.empty
|
||||||
|
|
||||||
|
-- main
|
||||||
|
|
||||||
|
main = do
|
||||||
|
print test1
|
||||||
|
print test2
|
||||||
|
print test3
|
||||||
|
print test4
|
||||||
|
print test5
|
||||||
|
print bad1
|
||||||
|
print bad2
|
||||||
|
|||||||
Reference in New Issue
Block a user