git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Recursive functions need to be made lazy
commit071d687408f7a4437c22e3351b944b49d24664f7 authorPhil Freeman <paf31@cantab.net> dateSat 26 Oct 2013, 16:23 (13 years ago) parent0f7185b
3 files changed+13−5
examples/Recursion.ps+4 src/PureScript/CodeGen/JS.hs+1 src/PureScript/TypeChecker/Types.hs+8−5
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
3131import PureScript.CodeGen.Common.Gen
3232
3333declToJs :: Declaration -> Maybe String
34+declToJs (ValueDeclaration ident (Abs args ret)) = Just $ "function " ++ identToJs ident ++ "(" ++ intercalate "," (map identToJs args) ++ ") { return " ++ valueToJs ret ++ "; }"
3435declToJs (ValueDeclaration ident val) = Just $ "var " ++ identToJs ident ++ " = " ++ valueToJs val ++ ";"
3536declToJs (DataDeclaration _ _ ctors) =
3637 Just $ flip concatMap ctors $ \(ctor, maybeTy) ->
src/PureScript/TypeChecker/Types.hs+8−5
@@ -51,12 +51,15 @@ emptyTypeSolution = TypeSolution (TUnknown, RUnknown)
5151
5252typeOf :: Ident -> Value -> Check PolyType
5353typeOf name val = do
54− me <- fresh
55− (cs, n) <- typeConstraints (M.singleton name me) val
56− let allConstraints = TypeConstraint me (TUnknown n) : cs
57− solution <- solveTypeConstraints allConstraints emptyTypeSolution
54+ (cs, n) <- case val of
55+ Abs _ _ -> do
56+ me <- fresh
57+ (cs, n) <- typeConstraints (M.singleton name me) val
58+ return (TypeConstraint me (TUnknown n) : cs, n)
59+ _ -> typeConstraints M.empty val
60+ solution <- solveTypeConstraints cs emptyTypeSolution
5861 let ty = fst (runTypeSolution solution) n
59− allUnknownsBecameQuantified allConstraints solution ty
62+ allUnknownsBecameQuantified cs solution ty
6063 return $ varIfUnknown ty
6164
6265allUnknownsBecameQuantified :: [TypeConstraint] -> TypeSolution -> Type -> Check ()