curry gotcha
[cc1516.git] / sem.icl
diff --git a/sem.icl b/sem.icl
index 2f9e7d6..448c67e 100644 (file)
--- a/sem.icl
+++ b/sem.icl
@@ -132,6 +132,13 @@ instance unfoldL_ Expr where
         pure ([fd], fe)
     unfoldL_ (FunExpr p f es fs) = flattenT <$> mapM unfoldL_ es >>= \(fds, es_)->
         pure (fds, FunExpr p f es_ fs)
+    unfoldL_ (Op2Expr p e1 op e2) = unfoldL_ e1 >>= \(fds1, e1_)->
+        unfoldL_ e2 >>= \(fds2, e2_)->
+        pure (fds1++fds2, Op2Expr p e1_ op e2_)
+    unfoldL_ (Op1Expr p op e1) = unfoldL_ e1 >>= \(fds, e1_)->pure (fds, Op1Expr p op e1_)
+    unfoldL_ (TupleExpr p (e1, e2)) = unfoldL_ e1 >>= \(fds1, e1_)->
+        unfoldL_ e2 >>= \(fds2, e2_)->
+        pure (fds1++fds2, TupleExpr p (e1_, e2_))
     unfoldL_ e = pure ([], e)
 
 //------------
@@ -192,11 +199,11 @@ unify t1 t2=:(IdType tv)    | t1 == (IdType tv) = Right zero
                             | 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 (subst s1 ta2) (subst s1 tb2) >>= \s2->
+                                Right $ compose s2 s1
 unify (TupleType (ta1,ta2)) (TupleType (tb1,tb2)) = unify ta1 tb1 >>= \s1->
-                                                    unify ta2 tb2 >>= \s2->
-                                                    Right $ compose s1 s2
+                                                    unify (subst s1 ta2) (subst s1 tb2) >>= \s2->
+                                                    Right $ compose s2 s1
 unify (ListType t1) (ListType t2) = unify t1 t2
 unify (FuncType t1) (FuncType t2) = unify t1 t2
 unify t1 t2 | t1 == t2  = Right zero
@@ -255,26 +262,33 @@ instance infer Expr where
 
     Op2Expr p e1 op e2 =
         infer e1 >>= \(s1, t1, e1_) -> 
+        applySubst s1 >>|
         infer e2 >>= \(s2, t2, e2_) ->
+        applySubst s2 >>|
         fresh >>= \tv ->
         let given = t1 ->> t2 ->> tv in 
         op2Type op >>= \expected ->
         lift (unify expected given) >>= \s3 ->
+        applySubst s3 >>|
         pure ((compose s3 $ compose s2 s1), subst s3 tv, Op2Expr p e1_ op e2_)
 
     Op1Expr p op e1 =
         infer e1 >>= \(s1, t1, e1_) -> 
+        applySubst s1 >>|
         fresh >>= \tv ->
         let given = t1 ->> tv in 
         op1Type op >>= \expected ->
         lift (unify expected given) >>= \s2 ->
+        applySubst s2 >>|
         pure (compose s2 s1, subst s2 tv, Op1Expr p op e1)
 
-    EmptyListExpr _ = (\tv->(zero,tv,e)) <$> fresh
+    EmptyListExpr _ = (\tv->(zero,ListType tv,e)) <$> fresh
 
     TupleExpr p (e1, e2) = 
         infer e1 >>= \(s1, t1, e1_) -> 
+        applySubst s1 >>|
         infer e2 >>= \(s2, t2, e2_) ->
+        applySubst s2 >>|
         pure (compose s2 s1, TupleType (t1,t2), TupleExpr p (e1_,e2_))
 
     LambdaExpr _ _ _ = liftT $ Left $ Error "PANIC: lambdas should be Unfolded"
@@ -283,6 +297,7 @@ instance infer Expr where
         lookup f >>= \expected ->
         let accST = (\(s,ts,es) e->infer e >>= \(s_,et,e_)-> pure (compose s_ s,ts++[et],es++[e_])) in
         foldM accST (zero,[],[]) args >>= \(s1, argTs, args_)->
+        applySubst s1 >>|
         (case f of
             "print" = case head argTs of
                 IntType = pure "1printint"
@@ -293,9 +308,11 @@ instance infer Expr where
             _ = pure f
         ) >>= \newF->
         fresh >>= \tv->case expected of
-            FuncType t = pure (s1, t, (FunExpr p newF args fs))
+            FuncType t = foldM foldFieldSelectors t fs >>= \returnType ->
+                pure (s1, returnType, (FunExpr p newF args fs))
             _ = (let given = foldr (->>) tv argTs in
                 lift (unify expected given) >>= \s2->
+                applySubst s2 >>|
                 let fReturnType = subst s2 tv in
                 foldM foldFieldSelectors fReturnType fs >>= \returnType ->
                 pure (compose s2 s1, returnType, FunExpr p newF args_ fs))
@@ -383,11 +400,12 @@ instance infer Stmt where
         pure (compose s2 s1, VoidType, FunStmt newF args_ fs)
 
     ReturnStmt Nothing = pure (zero, VoidType, s)
+    //hier ook sub applyen
     ReturnStmt (Just e) = infer e >>= \(sub, t, e_)-> pure (sub, t, ReturnStmt (Just e_))
 
 reverseFs :: Type FieldSelector -> Typing Type 
 reverseFs t FieldHd = pure $ ListType t
-reverseFs t FieldTl = pure $ ListType 
+reverseFs t FieldTl = pure t 
 reverseFs t FieldFst = fresh >>= \tv -> pure $ TupleType (t, tv)
 reverseFs t FieldSnd = fresh >>= \tv -> pure $ TupleType (tv, t)
 
@@ -427,22 +445,28 @@ instance type VarDecl where
 
 instance type FunDecl where
     type fd=:(FunDecl p f args expected vds stmts) = 
-        //if (f=="main") (abort (toString fd)) (pure ()) >>|
         gamma >>= \outerScope-> //functions are infered in their own scopde
         introduce f >>|
         mapM introduce args >>= \argTs->
+        fresh >>= \tempTv ->
+        let temp = foldr (->>) tempTv argTs in 
+        (case expected of
+            Just expected_ = lift (unify expected_ temp)
+            _   = pure zero
+        ) >>= \s0->
+        applySubst s0 >>|
         type vds >>= \(s1, tVds)->
         applySubst s1 >>|
         infer stmts >>= \(s2, result, stmts_)->
         applySubst s1 >>|
-        let argTs_ = map (subst $ compose s2 s1) argTs in 
+        let argTs_ = map (subst $ compose s2 $ compose s1 s0) argTs in 
         let given = foldr (->>) result argTs_ in 
         (case expected of
             Nothing = pure zero
             Just (FuncType expected_) = lift (unify expected_ given)
             Just expected_ = lift (unify expected_ given)
         ) >>= \s3 ->
-        let ftype = subst (compose s3 $ compose s2 s1) given in
+        let ftype = subst (compose s3 $ compose s2 $ compose s1 s0) given in
         (case ftype of
             _ ->> _ = pure ftype
             _       = pure $ FuncType ftype
@@ -450,7 +474,8 @@ instance type FunDecl where
         generalize ftype_ >>= \t->
         putGamma outerScope >>|
         changeGamma (extend f t) >>| 
-        pure (compose s3 $ compose s2 s1, FunDecl p f args (Just ftype_) tVds stmts_)
+        pure (compose s3 $ compose s2 $ compose s1 s0, 
+                FunDecl p f args (Just ftype_) tVds stmts_)
 
 instance type [a] | type a where
     type []     = pure (zero, [])