44module Language.PureScript.AST.Traversals where
55
66import Prelude.Compat
7+ import Protolude (swap )
78
89import Control.Monad
10+ import Control.Monad.Trans.State
911
1012import Data.Foldable (fold )
1113import Data.Functor.Identity (runIdentity )
@@ -424,15 +426,17 @@ everywhereWithContextOnValues
424426 -> (s -> Binder -> (s , Binder ))
425427 -> (s -> CaseAlternative -> (s , CaseAlternative ))
426428 -> (s -> DoNotationElement -> (s , DoNotationElement ))
429+ -> (s -> Guard -> (s , Guard ))
427430 -> ( Declaration -> Declaration
428431 , Expr -> Expr
429432 , Binder -> Binder
430433 , CaseAlternative -> CaseAlternative
431434 , DoNotationElement -> DoNotationElement
435+ , Guard -> Guard
432436 )
433- everywhereWithContextOnValues s f g h i j = (runIdentity . f', runIdentity . g', runIdentity . h', runIdentity . i', runIdentity . j')
437+ everywhereWithContextOnValues s f g h i j k = (runIdentity . f', runIdentity . g', runIdentity . h', runIdentity . i', runIdentity . j', runIdentity . k ')
434438 where
435- (f', g', h', i', j') = everywhereWithContextOnValuesM s (wrap f) (wrap g) (wrap h) (wrap i) (wrap j)
439+ (f', g', h', i', j', k' ) = everywhereWithContextOnValuesM s (wrap f) (wrap g) (wrap h) (wrap i) (wrap j) (wrap k )
436440 wrap = ((pure . ) . )
437441
438442everywhereWithContextOnValuesM
@@ -444,13 +448,15 @@ everywhereWithContextOnValuesM
444448 -> (s -> Binder -> m (s , Binder ))
445449 -> (s -> CaseAlternative -> m (s , CaseAlternative ))
446450 -> (s -> DoNotationElement -> m (s , DoNotationElement ))
451+ -> (s -> Guard -> m (s , Guard ))
447452 -> ( Declaration -> m Declaration
448453 , Expr -> m Expr
449454 , Binder -> m Binder
450455 , CaseAlternative -> m CaseAlternative
451456 , DoNotationElement -> m DoNotationElement
457+ , Guard -> m Guard
452458 )
453- everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j'' s0)
459+ everywhereWithContextOnValuesM s0 f g h i j k = (f'' s0, g'' s0, h'' s0, i'' s0, j'' s0, k '' s0)
454460 where
455461 f'' s = uncurry f' <=< f s
456462
@@ -501,7 +507,18 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
501507
502508 i'' s = uncurry i' <=< i s
503509
504- i' s (CaseAlternative bs val) = CaseAlternative <$> traverse (h'' s) bs <*> traverse (guardedExprM (k' s) (g'' s)) val
510+ i' s (CaseAlternative bs val) = CaseAlternative <$> traverse (h'' s) bs <*> traverse (guardedExprM' s) val
511+
512+ -- A specialized `guardedExprM` that keeps track of the context `s`
513+ -- after traversing `guards`, such that it's also exposed to `expr`.
514+ guardedExprM' :: s -> GuardedExpr -> m GuardedExpr
515+ guardedExprM' s (GuardedExpr guards expr) = do
516+ (guards', s') <- runStateT (traverse (StateT . goGuard) guards) s
517+ GuardedExpr guards' <$> g'' s' expr
518+
519+ -- Like k'', but `s` is tracked.
520+ goGuard :: Guard -> s -> m (Guard , s )
521+ goGuard x s = k s x >>= fmap swap . sndM' k'
505522
506523 j'' s = uncurry j' <=< j s
507524
@@ -510,6 +527,8 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
510527 j' s (DoNotationLet ds) = DoNotationLet <$> traverse (f'' s) ds
511528 j' s (PositionedDoNotationElement pos com e1) = PositionedDoNotationElement pos com <$> j'' s e1
512529
530+ k'' s = uncurry k' <=< k s
531+
513532 k' s (ConditionGuard e) = ConditionGuard <$> g'' s e
514533 k' s (PatternGuard b e) = PatternGuard <$> h'' s b <*> g'' s e
515534
0 commit comments