git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Recursive functions need to be made lazy
071d687Phil Freeman
examples/Recursion.psnew file+4
@@ -0,0 +1,4 @@
1+fib = \n -> case n of
2+ 0 -> 1
3+ 1 -> 1
4+ n -> fib (n - 1) + fib (n - 2)
src/PureScript/CodeGen/JS.hs+1
@@ -31,6 +31,7 @@ import PureScript.CodeGen.Common
31import PureScript.CodeGen.Common.Gen
31import PureScript.CodeGen.Common.Gen
32
32
33declToJs :: Declaration -> Maybe String
33declToJs :: Declaration -> Maybe String
34+declToJs (ValueDeclaration ident (Abs args ret)) = Just $ "function " ++ identToJs ident ++ "(" ++ intercalate "," (map identToJs args) ++ ") { return " ++ valueToJs ret ++ "; }"
34declToJs (ValueDeclaration ident val) = Just $ "var " ++ identToJs ident ++ " = " ++ valueToJs val ++ ";"
35declToJs (ValueDeclaration ident val) = Just $ "var " ++ identToJs ident ++ " = " ++ valueToJs val ++ ";"
35declToJs (DataDeclaration _ _ ctors) =
36declToJs (DataDeclaration _ _ ctors) =
36 Just $ flip concatMap ctors $ \(ctor, maybeTy) ->
37 Just $ flip concatMap ctors $ \(ctor, maybeTy) ->
src/PureScript/TypeChecker/Types.hs+8−5
@@ -51,12 +51,15 @@ emptyTypeSolution = TypeSolution (TUnknown, RUnknown)
51
51
52typeOf :: Ident -> Value -> Check PolyType
52typeOf :: Ident -> Value -> Check PolyType
53typeOf name val = do
53typeOf name val = do
54+ (cs, n) <- case val of
55+ Abs _ _ -> do
54− me <- fresh
56+ me <- fresh
55− (cs, n) <- typeConstraints (M.singleton name me) val
57+ (cs, n) <- typeConstraints (M.singleton name me) val
56− let allConstraints = TypeConstraint me (TUnknown n) : cs
58+ return (TypeConstraint me (TUnknown n) : cs, n)
59+ _ -> typeConstraints M.empty val
57− solution <- solveTypeConstraints allConstraints emptyTypeSolution
60+ solution <- solveTypeConstraints cs emptyTypeSolution
58 let ty = fst (runTypeSolution solution) n
61 let ty = fst (runTypeSolution solution) n
59− allUnknownsBecameQuantified allConstraints solution ty
62+ allUnknownsBecameQuantified cs solution ty
60 return $ varIfUnknown ty
63 return $ varIfUnknown ty
61
64
62allUnknownsBecameQuantified :: [TypeConstraint] -> TypeSolution -> Type -> Check ()
65allUnknownsBecameQuantified :: [TypeConstraint] -> TypeSolution -> Type -> Check ()