Fixed printing from lambdas
[cc1516.git] / sem.icl
diff --git a/sem.icl b/sem.icl
index 4f4e874..47b6c1a 100644 (file)
--- a/sem.icl
+++ b/sem.icl
@@ -126,7 +126,7 @@ instance unfoldL_ Stmt where
 instance unfoldL_ Expr where
     unfoldL_ (LambdaExpr p args e) = 
         fresh >>= \(IdType n) ->
-        let f = ("lambda_"+++n) in
+        let f = ("2lambda_"+++n) in
         let fd = FunDecl p f args Nothing [] [ReturnStmt $ Just e] in 
         let fe = FunExpr p f [] [] in
         pure ([fd], fe)
@@ -283,20 +283,21 @@ 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_)->
+        (case f of
+            "print" = case head argTs of
+                IntType = pure "1printint"
+                CharType = pure "1printchar"
+                BoolType = pure "1printbool"
+                ListType (CharType) = pure "1printstr"
+                t = liftT $ Left $ SanityError p ("can not print " +++ toString t)
+            _ = pure f
+        ) >>= \newF->
         fresh >>= \tv->case expected of
-            FuncType t = pure (s1, t, e)
+            FuncType t = pure (s1, t, (FunExpr p newF args fs))
             _ = (let given = foldr (->>) tv argTs in
                 lift (unify expected given) >>= \s2->
                 let fReturnType = subst s2 tv in
                 foldM foldFieldSelectors fReturnType fs >>= \returnType ->
-                (case f of
-                    "print" = case head argTs of
-                        IntType = pure "1printint"
-                        CharType = pure "1printchar"
-                        BoolType = pure "1printbool"
-                        ListType (CharType) = pure "1printstr"
-                        t = liftT $ Left $ SanityError p ("can not print " +++ toString t)
-                    _ = pure f) >>= \newF->
                 pure (compose s2 s1, returnType, FunExpr p newF args_ fs))
 
     IntExpr _ _ = pure $ (zero, IntType, e)
@@ -382,7 +383,7 @@ instance infer Stmt where
         pure (compose s2 s1, VoidType, FunStmt newF args_ fs)
 
     ReturnStmt Nothing = pure (zero, VoidType, s)
-    ReturnStmt (Just e) = infer e >>= \(sub, t, _)-> pure (sub, t, s)
+    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