git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Merge pull request #61 from chrissmoak/guards-scope

Allow guards access to current scope

commit136cd2131d3f121d77d2eb23e6d0a908f8dc33ed authorPhil Freeman <paf31@cantab.net> dateThu 7 Nov 2013, 06:46 (13 years ago) parent2f0699610746bf
3 files changed+20−5
examples/failing/PatternScope.ps+7 examples/passing/Guards.ps+5 src/Language/PureScript/TypeChecker/Types.hs+8−5
examples/failing/PatternScope.psnew file+7
@@ -0,0 +1,7 @@
1+-- Pattern scope doesn't leak
2+foobar = \x -> do
3+ var y = case x of
4+ z | z % 2 == 0 -> 1
5+ _ -> 2
6+ return z
7+
examples/passing/Guards.ps+5
@@ -1,3 +1,8 @@
11collatz = \x -> case x of
22 y | y % 2 == 0 -> y / 2
33 y -> y * 3 + 1
4+
5+-- Guards have access to current scope
6+collatz2 = \x, y -> case x of
7+ z | y > 0 -> z / 2
8+ z -> z * 3 + 1
src/Language/PureScript/TypeChecker/Types.hs+8−5
@@ -436,10 +436,13 @@ typeConstraintsForBinder val b@(NamedBinder name binder) = do
436436 me <- fresh
437437 (cs, m) <- typeConstraintsForBinder val binder
438438 return (TypeConstraint me (TUnknown val) (BinderOrigin b) : cs, M.insert name me m)
439−typeConstraintsForBinder val b@(GuardedBinder cond binder) = do
440− (cs1, m) <- typeConstraintsForBinder val binder
441− (cs2, n) <- typeConstraints m cond
442− return (TypeConstraint n Boolean (ValueOrigin cond) : cs1 ++ cs2, m)
439+
440+typeConstraintsForGuardedBinder :: M.Map Ident Int -> Int -> Binder -> Check ([TypeConstraint], M.Map Ident Int)
441+typeConstraintsForGuardedBinder m val b@(GuardedBinder cond binder) = do
442+ (cs1, m1) <- typeConstraintsForBinder val binder
443+ (cs2, n) <- typeConstraints (m `M.union` m1) cond
444+ return (TypeConstraint n Boolean (ValueOrigin cond) : cs1 ++ cs2, m1)
445+typeConstraintsForGuardedBinder m val b = typeConstraintsForBinder val b >>= return
443446
444447constantBinder :: Binder -> Int -> Type -> Check ([TypeConstraint], M.Map Ident Int)
445448constantBinder b val ty = return ([TypeConstraint val ty (BinderOrigin b)], M.empty)
@@ -447,7 +450,7 @@ constantBinder b val ty = return ([TypeConstraint val ty (BinderOrigin b)], M.em
447450typeConstraintsForBinders :: M.Map Ident Int -> Int -> Int -> [(Binder, Value)] -> Check [TypeConstraint]
448451typeConstraintsForBinders _ _ _ [] = return []
449452typeConstraintsForBinders m nval ret ((binder, val):bs) = do
450− (cs1, m1) <- typeConstraintsForBinder nval binder
453+ (cs1, m1) <- typeConstraintsForGuardedBinder m nval binder
451454 (cs2, n2) <- typeConstraints (m `M.union` m1) val
452455 cs3 <- typeConstraintsForBinders m nval ret bs
453456 return (TypeConstraint n2 (TUnknown ret) (BinderOrigin binder) : cs1 ++ cs2 ++ cs3)