Merge branch 'master' of https://github.com/dopefishh/cc1516
[cc1516.git] / sem.icl
diff --git a/sem.icl b/sem.icl
index 1a2ed00..0c5b552 100644 (file)
--- a/sem.icl
+++ b/sem.icl
 implementation module sem
 
 import qualified Data.Map as Map
+
 from Data.Func import $
-import Data.Maybe
-import Data.Either
-import Data.Functor
-import Control.Applicative
+from StdFunc import o, flip, const, id
+
 import Control.Monad
+import Control.Monad.Trans
 import Control.Monad.State
-import Control.Monad.Identity
-import StdMisc
-from StdFunc import id, const
+import Data.Either
+import Data.Maybe
+import Data.Monoid
+import Data.List
+import Data.Functor
+import Data.Tuple
+
 import StdString
+import StdTuple
 import StdList
+import StdMisc
+import StdEnum
+import GenEq
 
 from Text import class Text(concat), instance Text String
 
 import AST
-from parse import :: ParserOutput, :: Error
 
-:: Gamma :== 'Map'.Map String Type
-:: Env a :== (State Gamma (Either SemError a))
+:: Scheme = Forall [TVar] Type
+:: Gamma :== 'Map'.Map String Scheme //map from Variables! to types
+:: Typing a :== StateT (Gamma, [TVar]) (Either SemError) a
+:: Substitution :== 'Map'.Map TVar Type
+:: Constraints :== [(Type, Type)]
+:: SemError
+       = ParseError Pos String 
+       | UnifyError Pos Type Type 
+    | InfiniteTypeError Pos Type
+    | FieldSelectorError Pos Type FieldSelector 
+       | OperatorError Pos Op2 Type
+    | UndeclaredVariableError Pos String
+    | ArgumentMisMatchError Pos String
+       | SanityError Pos String
+       | Error String
+
+instance zero Gamma where
+       zero = 'Map'.newMap
+
+variableStream :: [TVar]
+variableStream = map toString [1..]
+
+defaultGamma :: Gamma //includes all default functions
+defaultGamma = extend "print" (Forall ["a"] ((IdType "a") ->> VoidType))
+                $ extend "isEmpty" (Forall ["a"] ((ListType (IdType "a")) ->> BoolType))
+                $ extend "read" (Forall [] CharType)
+                zero
+
+sem :: AST -> Either [SemError] AST
+sem (AST fd) = case foldM (const $ hasNoDups fd) () fd 
+                       >>| foldM (const isNiceMain) () fd
+                       >>| hasMain fd
+                    >>| evalStateT (type fd) (defaultGamma, variableStream) of
+       Left e = Left [e]
+    Right (_,fds) = Right (AST fds)
+where
+               hasNoDups :: [FunDecl] FunDecl -> Either SemError ()
+               hasNoDups fds (FunDecl p n _ _ _ _)
+               # mbs = map (\(FunDecl p` n` _ _ _ _)->if (n == n`) (Just p`) Nothing) fds
+               = case catMaybes mbs of
+                       [] = Left $ SanityError p "HUH THIS SHOULDN'T HAPPEN"
+                       [x] = pure ()
+                       [_:x] = Left $ SanityError p (concat 
+                               [n, " multiply defined at ", toString p])
+
+               hasMain :: [FunDecl] -> Either SemError ()
+               hasMain [(FunDecl _ "main" _ _ _ _):fd] = pure ()
+               hasMain [_:fd] = hasMain fd
+               hasMain [] = Left $ SanityError zero "no main function defined"
+
+               isNiceMain :: FunDecl -> Either SemError ()
+               isNiceMain (FunDecl p "main" as mt _ _) = case (as, mt) of
+                       ([_:_], _) = Left $ SanityError p "main must have arity 0"
+                       ([], t) = (case t of
+                               Nothing = pure ()
+                               Just VoidType = pure ()
+                               _ = Left $ SanityError p "main has to return Void")
+               isNiceMain _ = pure ()
+
+class Typeable a where
+    ftv :: a -> [TVar]
+    subst :: Substitution a -> a
+
+instance Typeable Scheme where
+    ftv (Forall bound t) = difference (ftv t) bound
+    subst s (Forall bound t) = Forall bound $ subst s_ t
+        where s_ = 'Map'.filterWithKey (\k _ -> not (elem k bound)) s
+
+instance Typeable [a] | Typeable a where
+    ftv types   = foldr (\t ts-> ftv t ++ ts) [] types
+    subst s ts  = map (\t->subst s t) ts
+
+instance Typeable Type where
+    ftv (TupleType (t1, t2))    = ftv t1 ++ ftv t2
+    ftv (ListType t)            = ftv t
+    ftv (IdType tvar)           = [tvar]
+    ftv (t1 ->> t2)             = ftv t1 ++ ftv t2
+    ftv _                       = []
+    subst s (TupleType (t1, t2))= TupleType (subst s t1, subst s t2)
+    subst s (ListType t1)       = ListType (subst s t1)
+    subst s (t1 ->> t2)         = (subst s t1) ->> (subst s t2)
+    subst s t1=:(IdType tvar)   = 'Map'.findWithDefault t1 tvar s
+    subst s t                   = t
+
+instance Typeable Gamma where
+    ftv gamma       = concatMap id $ map ftv ('Map'.elems gamma)
+    subst s gamma   = Mapmap (subst s) gamma
+
+extend :: String Scheme Gamma -> Gamma
+extend k t g = 'Map'.put k t g
+
+//// ------------------------
+//// algorithm U, Unification 
+//// ------------------------
+instance zero Substitution where zero = 'Map'.newMap
+
+compose :: Substitution Substitution -> Substitution
+compose s1 s2 = 'Map'.union (Mapmap (subst s1) s2) s1
+//Note: just like function compositon compose does snd first
+
+occurs :: TVar a -> Bool | Typeable a
+occurs tvar a = elem tvar (ftv a)
+
+unify :: Type Type -> Either SemError Substitution
+unify t1 t2=:(IdType tv)    | t1 == (IdType tv) = Right zero
+                            | occurs tv t1 = Left $ InfiniteTypeError zero t1
+                            | otherwise = Right $ 'Map'.singleton tv t1
+unify t1=:(IdType tv) t2 = unify t2 t1
+unify (ta1->>ta2) (tb1->>tb2) = unify ta1 tb1 >>= \s1->
+                                unify ta2 tb2 >>= \s2->
+                                Right $ compose s1 s2
+unify (TupleType (ta1,ta2)) (TupleType (tb1,tb2)) = unify ta1 tb1 >>= \s1->
+                                                    unify ta2 tb2 >>= \s2->
+                                                    Right $ compose s1 s2
+unify (ListType t1) (ListType t2) = unify t1 t2
+unify t1 t2 | t1 == t2  = Right zero
+            | otherwise = Left $ UnifyError zero t1 t2
+
+//// ------------------------
+//// Algorithm M, Inference and Solving
+//// ------------------------
+gamma :: Typing Gamma
+gamma = gets fst
+putGamma :: Gamma -> Typing ()
+putGamma g = modify (appFst $ const g) >>| pure ()
+changeGamma :: (Gamma -> Gamma) -> Typing Gamma
+changeGamma f = modify (appFst f) >>| gamma
+withGamma :: (Gamma -> a) -> Typing a
+withGamma f = f <$> gamma
+fresh :: Typing Type
+fresh = gets snd >>= \vars-> 
+        modify (appSnd $ const $ tail vars) >>| 
+        pure (IdType (head vars))
+
+lift :: (Either SemError a) -> Typing a
+lift (Left e) = liftT $ Left e 
+lift (Right v) = pure v
+
+//instantiate maps a schemes type variables to variables with fresh names
+//and drops the quantification: i.e. forall a,b.a->[b] becomes c->[d]
+instantiate :: Scheme -> Typing Type
+instantiate (Forall bound t) = 
+    mapM (const fresh) bound >>= \newVars->
+    let s = 'Map'.fromList (zip (bound,newVars)) in
+    pure (subst s t)
+
+//generalize quentifies all free type variables in a type which are not
+//in the gamma
+generalize :: Type -> Typing Scheme
+generalize t = gamma >>= \g-> pure $ Forall (difference (ftv t) (ftv g)) t
 
-get = state $ \s -> (s,s)
+lookup :: String -> Typing Type 
+lookup k = gamma >>= \g-> case 'Map'.member k g of
+    False = liftT (Left $ UndeclaredVariableError zero k)
+    True = instantiate $ 'Map'.find k g
 
-putIdent :: String Type -> Env Void
-putIdent i t = gets ('Map'.get i) >>= \mt -> case mt of
-    Nothing = pure <$> modify ('Map'.put i t)
-    Just t2 = unify t t2 >>= \r -> case r of
-        Left e  = pure $ Left e
-        Right t3 = pure <$> modify ('Map'.put i t3)
+//The inference class
+//When tying it all together we will treat the program is a big 
+//let x=e1 in let y=e2 in .... 
+class infer a :: a -> Typing (Substitution, Type)
+
+////---- Inference for Expressions ----
+
+instance infer Expr where 
+ infer e = case e of
+    VarExpr _ (VarDef k fs) = lookup k >>= \t ->
+        foldM foldFieldSelectors t fs >>= \finalT ->
+        pure (zero, finalT)
+
+    Op2Expr _ e1 op e2 =
+        infer e1 >>= \(s1, t1) -> 
+        infer e2 >>= \(s2, t2) ->
+        fresh >>= \tv ->
+        let given = t1 ->> t2 ->> tv in 
+        op2Type op >>= \expected ->
+        lift (unify expected given) >>= \s3 ->
+        pure ((compose s3 $ compose s2 s1), subst s3 tv)
+
+    Op1Expr _ op e1 =
+        infer e1 >>= \(s1, t1) -> 
+        fresh >>= \tv ->
+        let given = t1 ->> tv in 
+        op1Type op >>= \expected ->
+        lift (unify expected given) >>= \s2 ->
+        pure (compose s2 s1, subst s2 tv)
+
+    EmptyListExpr _ = (\tv->(zero,tv)) <$> fresh
+
+    TupleExpr _ (e1, e2) = 
+        infer e1 >>= \(s1, t1) -> 
+        infer e2 >>= \(s2, t2) ->
+        pure (compose s2 s1, TupleType (t1,t2))
+
+    FunExpr _ f args fs = //todo: fieldselectors
+        lookup f >>= \expected ->
+        let accST = (\(s,ts) e->infer e >>= \(s_,et)->pure (compose s_ s,ts++[et])) in
+        foldM accST (zero,[]) args >>= \(s1, argTs)->
+        fresh >>= \tv->
+        let given = foldr (->>) tv argTs in
+        lift (unify expected given) >>= \s2->
+        let fReturnType = subst s2 tv in
+        foldM foldFieldSelectors fReturnType fs >>= \returnType ->
+        pure (compose s2 s1, returnType)
+
+    IntExpr _ _ = pure $ (zero, IntType)
+    BoolExpr _ _ = pure $ (zero, BoolType)
+    CharExpr _ _ = pure $ (zero, CharType)
+
+foldFieldSelectors :: Type FieldSelector -> Typing Type 
+foldFieldSelectors (ListType t) (FieldHd) = pure t
+foldFieldSelectors t=:(ListType _) (FieldTl) = pure t 
+foldFieldSelectors (TupleType (t1, _)) (FieldFst) = pure t1
+foldFieldSelectors (TupleType (_, t2)) (FieldSnd) = pure t2
+foldFieldSelectors t fs = liftT $ Left $ FieldSelectorError zero t fs
+
+op2Type :: Op2 -> Typing Type
+op2Type op
+| elem op [BiPlus, BiMinus, BiTimes, BiDivide, BiMod]
+  = pure (IntType ->> IntType ->> IntType)
+| elem op [BiEquals, BiUnEqual]
+  = fresh >>= \t1-> fresh >>= \t2-> pure (t1 ->> t2 ->> BoolType)
+| elem op [BiLesser, BiGreater, BiLesserEq, BiGreaterEq]
+  = pure (IntType ->> IntType ->> BoolType)
+| elem op [BiAnd, BiOr]
+  = pure (BoolType ->> BoolType ->> BoolType)
+| op == BiCons 
+  = fresh >>= \t1-> pure (t1 ->> ListType t1 ->> ListType t1)
+
+op1Type :: Op1 -> Typing Type
+op1Type UnNegation = pure $ (BoolType ->> BoolType)
+op1Type UnMinus = pure $ (IntType ->> IntType)
+
+////----- Inference for Statements -----
+applySubst :: Substitution -> Typing Gamma
+applySubst s = changeGamma (subst s)
+
+instance infer Stmt where
+ infer s = case s of
+    IfStmt e th el = 
+        infer e >>= \(s1, et)->
+        lift (unify et BoolType) >>= \s2 ->
+        applySubst (compose s2 s1) >>|        
+        infer th >>= \(s3, tht)->
+        applySubst s3 >>|
+        infer el >>= \(s4, elt)->
+        applySubst s4 >>|
+        lift (unify tht elt) >>= \s5-> 
+        pure (compose s5 $ compose s4 $ compose s3 $ compose s2 s1, subst s5 tht)
+
+    WhileStmt e wh = 
+        infer e >>= \(s1, et)->
+        lift (unify et BoolType) >>= \s2 ->
+        applySubst (compose s2 s1) >>|
+        infer wh >>= \(s3, wht)->
+        pure (compose s3 $ compose s2 s1, subst s3 wht)
+
+    AssStmt (VarDef k fs) e =
+        lookup k >>= \expected ->
+        infer e >>= \(s1, given)->
+        foldM reverseFs given (reverse fs) >>= \varType->
+        lift (unify expected varType) >>= \s2->
+        let s = compose s2 s1 in
+        applySubst s >>|
+        changeGamma (extend k (Forall [] (subst s varType))) >>| 
+        pure (s, VoidType)
+
+    FunStmt f es _ = pure (zero, VoidType) 
+
+    ReturnStmt Nothing = pure (zero, VoidType)
+    ReturnStmt (Just e) = infer e
+
+reverseFs :: Type FieldSelector -> Typing Type 
+reverseFs t FieldHd = pure $ ListType t
+reverseFs t FieldTl = pure $ ListType t 
+reverseFs t FieldFst = fresh >>= \tv -> pure $ TupleType (t, tv)
+reverseFs t FieldSnd = fresh >>= \tv -> pure $ TupleType (tv, t)
+
+//The type of a list of statements is either an encountered
+//return, or VoidType
+instance infer [a] | infer a where
+    infer []        = pure (zero, VoidType)
+    infer [stmt:ss] = 
+        infer stmt >>= \(s1, t1) ->
+        applySubst s1 >>|
+        infer ss >>= \(s2, t2) ->
+        applySubst s2 >>|
+        case t1 of
+            VoidType = pure (compose s2 s1, t2)
+            _ = case t2 of
+                VoidType = pure (compose s2 s1, t1)
+                _ = lift (unify t1 t2) >>= \s3 -> 
+                    pure (compose s3 $ compose s2 s1, t1)
+
+//the type class inferes the type of an AST element (VarDecl or FunDecl)
+//and adds it to the AST element
+class type a :: a -> Typing (Substitution, a)
+
+instance type VarDecl where
+    type (VarDecl p expected k e) = 
+        infer e >>= \(s1, given) ->
+        applySubst s1 >>| 
+        case expected of
+            Nothing = pure zero
+            Just expected_ = lift (unify expected_ given)
+        >>= \s2->
+        applySubst s2 >>|
+        let vtype = subst (compose s2 s1) given in
+        generalize vtype >>= \t ->
+        changeGamma (extend k t) >>| 
+        pure (compose s2 s1, VarDecl p (Just vtype) k e)
+
+instance type FunDecl where
+    type (FunDecl p f args expected vds stmts) = 
+        gamma >>= \outerScope-> //functions are infered in their own scopde
+        introduce f >>|
+        mapM introduce args >>= \argTs->
+        type vds >>= \(s1, tVds)->
+        applySubst s1 >>|
+        infer stmts >>= \(s2, result)->
+        applySubst s1 >>|
+        let argTs_ = map (subst $ compose s2 s1) argTs in 
+        //abort (concat $ intersperse "\n" $ map toString argTs_) >>|
+        let given = foldr (->>) result argTs_ in 
+        (case expected of
+            Nothing = pure zero
+            Just expected_ = lift (unify expected_ given))
+        >>= \s3 ->
+        let ftype = subst (compose s3 $ compose s2 s1) given in
+        generalize ftype >>= \t->
+        putGamma outerScope >>|
+        changeGamma (extend f t) >>| 
+        pure (compose s3 $ compose s2 s1, FunDecl p f args (Just ftype) tVds stmts)
+
+instance type [a] | type a where
+    type []     = pure (zero, [])
+    type [v:vs] = 
+        type v >>= \(s1, v_)->
+        applySubst s1 >>|
+        type vs >>= \(s2, vs_)->
+        applySubst (compose s2 s1) >>|
+        pure (compose s2 s1, [v_:vs_])
+
+introduce :: String -> Typing Type
+introduce k = 
+    fresh >>= \tv ->
+    changeGamma (extend k (Forall [] tv)) >>|
+    pure tv
+
+instance toString Scheme where
+    toString (Forall x t) = 
+        concat ["Forall ": intersperse "," x] +++ concat [". ", toString t];
+
+instance toString Gamma where
+    toString mp = 
+        concat [concat [k, ": ", toString v, "\n"]\\(k, v)<-'Map'.toList mp]
+
+instance toString Substitution where
+    toString subs = 
+        concat [concat [k, ": ", toString t, "\n"]\\(k, t)<-'Map'.toList subs]
 
 instance toString SemError where
-       toString (ParseError p e) = concat [
-               toString p,"SemError: ParseError: ", e]
-       toString (Error e) = "SemError: " +++ e
-       toString (UnifyErrorStub t1 t2) = toString (UnifyError {line=0,col=0} t1 t2)
-       toString (UnifyError p t1 t2) = concat [
-               toString p,
-               "SemError: Cannot unify types. Expected: ",
-               toString t1, ". Given: ", toString t2]
-
-sem :: AST -> SemOutput
-sem (AST vd fd)
-# (eithervds, gamma) = runState (mapM semVarDecl vd) 'Map'.newMap
-# (eitherfds, gamma) = runState (mapM semFunDecl fd) gamma
-= case splitEithers eithervds of
-       (Left errs) = Left $ errs ++ [x\\(Left x)<-eitherfds]
-       (Right vds) = case splitEithers eitherfds of
-               (Left errs) = Left errs
-               (Right fds) = Right $ AST vds fds
-
-splitEithers :: [Either a b] -> Either [a] [b]
-splitEithers [] = Right []
-splitEithers [Right x:xs] = splitEithers xs >>= \rest->Right [x:rest]
-splitEithers xs = Left $ [x\\(Left x)<-xs]
-
-semFunDecl :: FunDecl -> Env FunDecl
-semFunDecl f = pure $ Right f
-
-semVarDecl :: VarDecl -> Env VarDecl
-semVarDecl vd=:(VarDecl pos type ident ex) = unify type ex
-       >>= \et->case et of
-               Left err = pure $ Left err
-               Right t = putIdent ident t >>| pure (Right $ VarDecl pos t ident ex)
-//Right v
-//     //TODO ident in de environment
-//     Right e = Right $ pure vd
-
-typeExpr :: Expr -> Env Type
-typeExpr (IntExpr _ _) = pure $ Right IntType
-typeExpr (CharExpr _ _) = pure $ Right CharType
-typeExpr (BoolExpr _ _) = pure $ Right BoolType
-typeExpr (Op1Expr p UnNegation expr) = unify BoolType expr
-typeExpr (Op1Expr p UnMinus expr) = unify IntType expr
-typeExpr (TupleExpr p (e1, e2)) = typeExpr e1 
-       >>= \ete1->typeExpr e2 >>= \ete2->pure (
-               ete1 >>= \te1->ete2 >>= \te2->Right $ TupleType (te1, te2))
-//typeExpr (Op2Expr Pos Expr Op2 Expr) = undef
-//typeExpr (FunExpr Pos FunCall) = undef
-//typeExpr (EmptyListExpr Pos) = undef
-//typeExpr (VarExpr Pos VarDef) = undef
-
-class unify a :: Type a -> Env Type
-
-instance unify Expr where
-       unify (_ ->> _) e = pure $ Left $ ParseError (extrPos e)
-                       "Expression cannot be a higher order function. Yet..."
-       unify VoidType e = pure $ Left $ ParseError (extrPos e)
-                       "Expression cannot be a Void type."
-       unify (IdType _) e = pure $ Left $ ParseError (extrPos e)
-                       "Expression cannot be an polymorf type."
-       unify t e = typeExpr e
-               >>= \eithertype->case eithertype of
-                       Left e = pure $ Left e
-                       Right tex = unify t tex >>= \eitherun->case eitherun of
-                               Left err = pure $ Left $ decErr e err
-                               Right t = pure $ Right t
-
-instance unify Type where
-       unify IntType IntType = pure $ Right IntType
-       unify BoolType BoolType = pure $ Right BoolType
-       unify CharType CharType = pure $ Right CharType
-       unify t1 t2 = pure $ Left $ UnifyError zero t1 t2
-
-instance zero Pos where
-       zero = {line=0,col=0}
-
-decErr :: Expr SemError -> SemError
-decErr e (UnifyError _ t1 t2) = UnifyError (extrPos e) t1 t2
-decErr e (ParseError _ s) = ParseError (extrPos e) s
-decErr e err = err
-
-extrPos :: Expr -> Pos
-extrPos (VarExpr p _) = p
-extrPos (Op2Expr p _ _ _) = p
-extrPos (Op1Expr p _ _) = p
-extrPos (IntExpr p _) = p
-extrPos (CharExpr p _) = p
-extrPos (BoolExpr p _) = p
-extrPos (FunExpr p _) = p
-extrPos (EmptyListExpr p) = p
-extrPos (TupleExpr p _) = p
+    toString (SanityError p e) = concat [toString p, 
+        "SemError: SanityError: ", e]
+    toString (ParseError p s) = concat [toString p, 
+        "ParseError: ", s]
+    toString (UnifyError p t1 t2) = concat [toString p, 
+        "Can not unify types, expected|given:\n", toString t1,
+        "\n", toString t2]
+    toString (InfiniteTypeError p t) = concat [toString p, 
+        "Infinite type: ", toString t]
+    toString (FieldSelectorError p t fs) = concat [toString p,
+        "Can not run fieldselector '", toString fs, "' on type: ",
+        toString t]
+    toString (OperatorError p op t) = concat [toString p,
+        "Operator error, operator '", toString op, "' can not be",
+        "used on type: ", toString t]
+    toString (UndeclaredVariableError p k) = concat [toString p,
+        "Undeclared identifier: ", k]
+    toString (ArgumentMisMatchError p str) = concat [toString p,
+        "Argument mismatch: ", str]
+    toString (Error e) = concat ["Unknown error during semantical",
+        "analysis: ", e]
+
+instance toString (Maybe a) | toString a where
+    toString Nothing = "Nothing"
+    toString (Just e) = concat ["Just ", toString e]
+
+instance MonadTrans (StateT (Gamma, [TVar])) where
+    liftT m = StateT \s-> m >>= \a-> return (a, s)
+
+Mapmap :: (a->b) ('Map'.Map k a) -> ('Map'.Map k b)
+Mapmap _ 'Map'.Tip = 'Map'.Tip
+Mapmap f ('Map'.Bin sz k v ml mr) = 'Map'.Bin sz k (f v) 
+                                        (Mapmap f ml) 
+                                        (Mapmap f mr)