X-Git-Url: http://git.tomasm.cz/fp.git/blobdiff_plain/79ff57615dcea00a035f125f8d8d77d5d128de5f..33464c9ce7b0b1559f8cb23734065b8ecdd382c1:/src/HM.hs?ds=sidebyside diff --git a/src/HM.hs b/src/HM.hs index 8f6df2f..9591706 100644 --- a/src/HM.hs +++ b/src/HM.hs @@ -86,18 +86,25 @@ unify (TypeVar a) t = varBind a t unify t (TypeVar a) = varBind a t unify (TypeFunction a b) (TypeFunction a' b') = do s1 <- unify a a' - s2 <- unify b b' + s2 <- unify (substituteT s1 b) (substituteT s1 b') return $ s1 `composeSub` s2 unify (Primitive a) (Primitive b) | a == b = return idSub unify a b = fail $ "cannot unify " ++ show a ++ " with " ++ show b +tiLit :: Literal -> TI (Substitution, Type) +tiLit (LBool _) = return (idSub, Primitive "Bool") +tiLit (LInt _) = return (idSub, Primitive "Integer") +tiLit (LFunc If) = do + a <- newVar + return (idSub, Primitive "Bool" `TypeFunction` (a `TypeFunction` (a `TypeFunction` a))) + ti :: TypeEnv -> TypedTerm -> TI (Substitution, Type) ---ti _ (TTerm (Var v) (TScheme t@(Primitive _))) = return (idSub, t) ti e (TTerm tr sch) = do (s, t) <- ti e (NTTerm tr) sch' <- instantiate sch s' <- unify t sch' return (s', substituteT s' sch') +ti _ (NTTerm (Lit l)) = tiLit l ti e (NTTerm (Var v)) = case Map.lookup v e of Nothing -> fail $ "unbound variable: " ++ v Just sigma -> do @@ -120,7 +127,6 @@ ti e (NTTerm (Let x a b)) = do e' = Map.insert x t' e (s2, t2) <- ti (Map.map (substituteS s1) e') b return (s1 `composeSub` s2, t2) - algW :: TypedTerm -> Either String Type algW t = fst . runTI $ do