You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
type Env = [(Name, Type)]<br />
extend :: (Name, Type) -> Env -> Env<br />
extend xt env = xt : env<br />
inEnv :: (Name, Type) -> Check a -> Check a<br />
inEnv (x,t) = local (extend (x,t))<br />
lookupVar :: Name -> Check Type<br />
lookupVar x = do<br />
env return e<br />
Nothing -> throwError $ NotInScope x<br />
e typechecker will be a ExceptT + Reader monad transformer stack, with the reader holding the<br />
typing environment. ere are three possible failure modes for our simply typed lambda calculus typechecker:<br />
• e case when we try to unify two unlike types.<br />
• e case when we try to apply a non-function to an argument.<br />
• e case when a variable is referred to that is not in scope.<br />
data TypeError<br />
= Mismatch Type Type<br />
| NotFunction Type<br />
| NotInScope Name<br />
type Check = ExceptT TypeError (Reader Env)<br />
ere is a direct equivalence between syntax patterns here and the equivalent typing judgement for it.<br />
is will not always be the case in general though. e implementation of the type checker is as follows:<br />
check :: Expr -> Check Type<br />
check expr = case expr of<br />
Lit (LInt{}) -> return TInt<br />
Lit (LBool{}) -> return TBool<br />
Lam x t e -> do<br />
rhs do<br />
64