Skip to content

Commit fd49d97

Browse files
committed
1 parent 879a030 commit fd49d97

File tree

3 files changed

+34
-10
lines changed

3 files changed

+34
-10
lines changed

examples/passing/Let2.purs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
module Main where
2+
3+
test =
4+
let f :: Number -> Boolean
5+
f 0 = false
6+
f n = g (n - 1)
7+
8+
g :: Number -> Boolean
9+
g 0 = true
10+
g n = f (n - 1)
11+
12+
x = f 1
13+
in not x
14+
15+
main = Debug.Trace.print test

src/Language/PureScript/TypeChecker/Monad.hs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,19 @@ bindLocalTypeVariables moduleName bindings =
9696
-- |
9797
-- Update the visibility of all names to Defined
9898
--
99-
makeBindingGroupVisible :: (Functor m, MonadState CheckState m) => m a -> m a
100-
makeBindingGroupVisible action = do
101-
orig <- get
102-
modify $ \st -> st { checkEnv = (checkEnv st) { names = M.map (\(ty, nk, _) -> (ty, nk, Defined)) (names . checkEnv $ st) } }
99+
makeBindingGroupVisible :: (Functor m, MonadState CheckState m) => m ()
100+
makeBindingGroupVisible = modifyEnv $ \e -> e { names = M.map (\(ty, nk, _) -> (ty, nk, Defined)) (names e) }
101+
102+
-- | Update the visibility of all names to Defined in the scope of the provided action
103+
withBindingGroupVisible :: (Functor m, MonadState CheckState m) => m a -> m a
104+
withBindingGroupVisible action = preservingNames $ makeBindingGroupVisible >> action
105+
106+
-- | Perform an action while preserving the names from the @Environment@.
107+
preservingNames :: (Functor m, MonadState CheckState m) => m a -> m a
108+
preservingNames action = do
109+
orig <- gets (names . checkEnv)
103110
a <- action
104-
modify $ \st -> st { checkEnv = (checkEnv st) { names = names . checkEnv $ orig } }
111+
modifyEnv $ \e -> e { names = orig }
105112
return a
106113

107114
-- |

src/Language/PureScript/TypeChecker/Types.hs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ infer' (Accessor prop val) = do
247247
infer' (Abs (Left arg) ret) = do
248248
ty <- fresh
249249
Just moduleName <- checkCurrentModule <$> get
250-
makeBindingGroupVisible $ bindLocalVariables moduleName [(arg, ty, Defined)] $ do
250+
withBindingGroupVisible $ bindLocalVariables moduleName [(arg, ty, Defined)] $ do
251251
body@(TypedValue _ _ bodyTy) <- infer' ret
252252
return $ TypedValue True (Abs (Left arg) body) $ function ty bodyTy
253253
infer' (Abs (Right _) _) = error "Binder was not desugared"
@@ -298,7 +298,7 @@ infer' (PositionedValue pos _ val) = rethrowWithPosition pos $ infer' val
298298
infer' _ = error "Invalid argument to infer"
299299

300300
inferLetBinding :: [Declaration] -> [Declaration] -> Expr -> (Expr -> UnifyT Type Check Expr) -> UnifyT Type Check ([Declaration], Expr)
301-
inferLetBinding seen [] ret j = (,) seen <$> makeBindingGroupVisible (j ret)
301+
inferLetBinding seen [] ret j = (,) seen <$> withBindingGroupVisible (j ret)
302302
inferLetBinding seen (ValueDeclaration ident nameKind [] (Right (tv@(TypedValue checkType val ty))) : rest) ret j = do
303303
Just moduleName <- checkCurrentModule <$> get
304304
(kind, args) <- liftCheck $ kindOfWithScopedVars ty
@@ -320,7 +320,9 @@ inferLetBinding seen (BindingGroupDeclaration ds : rest) ret j = do
320320
ds1' <- parU typed $ \e -> checkTypedBindingGroupElement moduleName e dict
321321
ds2' <- forM untyped $ \e -> typeForBindingGroupElement e dict untypedDict
322322
let ds' = [(ident, LocalVariable, val') | (ident, (val', _)) <- ds1' ++ ds2']
323-
makeBindingGroupVisible $ bindNames dict $ inferLetBinding (seen ++ [BindingGroupDeclaration ds']) rest ret j
323+
bindNames dict $ do
324+
makeBindingGroupVisible
325+
inferLetBinding (seen ++ [BindingGroupDeclaration ds']) rest ret j
324326
inferLetBinding seen (PositionedDeclaration pos com d : ds) ret j = rethrowWithPosition pos $ do
325327
(d' : ds', val') <- inferLetBinding seen (d : ds) ret j
326328
return (PositionedDeclaration pos com d' : ds', val')
@@ -442,7 +444,7 @@ check' val t@(ConstrainedType constraints ty) = do
442444
dictNames <- forM constraints $ \(Qualified _ (ProperName className), _) -> do
443445
n <- liftCheck freshDictionaryName
444446
return $ Ident $ "__dict_" ++ className ++ "_" ++ show n
445-
val' <- makeBindingGroupVisible $ withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
447+
val' <- withBindingGroupVisible $ withTypeClassDictionaries (zipWith (\name (className, instanceTy) ->
446448
TypeClassDictionaryInScope name className instanceTy Nothing TCDRegular False) (map (Qualified Nothing) dictNames)
447449
constraints) $ check val ty
448450
return $ TypedValue True (foldr (Abs . Left) val' dictNames) t
@@ -468,7 +470,7 @@ check' (ArrayLiteral vals) t@(TypeApp a ty) = do
468470
check' (Abs (Left arg) ret) ty@(TypeApp (TypeApp t argTy) retTy) = do
469471
t =?= tyFunction
470472
Just moduleName <- checkCurrentModule <$> get
471-
ret' <- makeBindingGroupVisible $ bindLocalVariables moduleName [(arg, argTy, Defined)] $ check ret retTy
473+
ret' <- withBindingGroupVisible $ bindLocalVariables moduleName [(arg, argTy, Defined)] $ check ret retTy
472474
return $ TypedValue True (Abs (Left arg) ret') ty
473475
check' (Abs (Right _) _) _ = error "Binder was not desugared"
474476
check' (App f arg) ret = do

0 commit comments

Comments
 (0)