@@ -12,21 +12,12 @@
1212--
1313-----------------------------------------------------------------------------
1414
15−{-# LANGUAGE CPP #-}
16−
1715module Language.PureScript.AST.Traversals where
1816
19−#if __GLASGOW_HASKELL__ < 710
20−import Data.Monoid (Monoid(..), mconcat)
21−#endif
22−import Data.Maybe (mapMaybe)
23−#if __GLASGOW_HASKELL__ < 710
24−import Data.Traversable (traverse)
25−#endif
17+import Prelude ()
18+import Prelude.Compat
2619
27−#if __GLASGOW_HASKELL__ < 710
28−import Control.Applicative
29−#endif
20+import Data.Maybe (mapMaybe)
3021import Control.Monad
3122import Control.Arrow ((***), (+++), second)
3223
@@ -101,10 +92,10 @@ everywhereOnValuesTopDownM :: (Functor m, Applicative m, Monad m) =>
10192 (Declaration -> m Declaration, Expr -> m Expr, Binder -> m Binder)
10293everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
10394 where
104− f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f' <=< f) ds
105− f' (ValueDeclaration name nameKind bs val) = ValueDeclaration name nameKind <$> mapM (h' <=< h) bs <*> eitherM (mapM (pairM (g' <=< g) (g' <=< g))) (g' <=< g) val
106− f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
107− f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f' <=< f) ds
95+ f' (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> traverse (f' <=< f) ds
96+ f' (ValueDeclaration name nameKind bs val) = ValueDeclaration name nameKind <$> traverse (h' <=< h) bs <*> eitherM (traverse (pairM (g' <=< g) (g' <=< g))) (g' <=< g) val
97+ f' (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> traverse (\(name, nameKind, val) -> (,,) name nameKind <$> (g val >>= g')) ds
98+ f' (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> traverse (f' <=< f) ds
10899 f' (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> traverseTypeInstanceBody (traverse (f' <=< f)) ds
109100 f' (PositionedDeclaration pos com d) = PositionedDeclaration pos com <$> (f d >>= f')
110101 f' other = f other
@@ -114,37 +105,37 @@ everywhereOnValuesTopDownM f g h = (f' <=< f, g' <=< g, h' <=< h)
114105 g' (Parens v) = Parens <$> (g v >>= g')
115106 g' (OperatorSection op (Left v)) = OperatorSection <$> (g op >>= g') <*> (Left <$> (g v >>= g'))
116107 g' (OperatorSection op (Right v)) = OperatorSection <$> (g op >>= g') <*> (Right <$> (g v >>= g'))
117− g' (ArrayLiteral vs) = ArrayLiteral <$> mapM (g' <=< g) vs
118− g' (ObjectLiteral vs) = ObjectLiteral <$> mapM (sndM (g' <=< g)) vs
119− g' (ObjectConstructor vs) = ObjectConstructor <$> mapM (sndM $ maybeM (g' <=< g)) vs
108+ g' (ArrayLiteral vs) = ArrayLiteral <$> traverse (g' <=< g) vs
109+ g' (ObjectLiteral vs) = ObjectLiteral <$> traverse (sndM (g' <=< g)) vs
110+ g' (ObjectConstructor vs) = ObjectConstructor <$> traverse (sndM $ maybeM (g' <=< g)) vs
120111 g' (TypeClassDictionaryConstructorApp name v) = TypeClassDictionaryConstructorApp name <$> (g v >>= g')
121112 g' (Accessor prop v) = Accessor prop <$> (g v >>= g')
122− g' (ObjectUpdate obj vs) = ObjectUpdate <$> (g obj >>= g') <*> mapM (sndM (g' <=< g)) vs
123− g' (ObjectUpdater obj vs) = ObjectUpdater <$> (maybeM g obj >>= maybeM g') <*> mapM (sndM $ maybeM (g' <=< g)) vs
113+ g' (ObjectUpdate obj vs) = ObjectUpdate <$> (g obj >>= g') <*> traverse (sndM (g' <=< g)) vs
114+ g' (ObjectUpdater obj vs) = ObjectUpdater <$> (maybeM g obj >>= maybeM g') <*> traverse (sndM $ maybeM (g' <=< g)) vs
124115 g' (Abs name v) = Abs name <$> (g v >>= g')
125116 g' (App v1 v2) = App <$> (g v1 >>= g') <*> (g v2 >>= g')
126117 g' (IfThenElse v1 v2 v3) = IfThenElse <$> (g v1 >>= g') <*> (g v2 >>= g') <*> (g v3 >>= g')
127− g' (Case vs alts) = Case <$> mapM (g' <=< g) vs <*> mapM handleCaseAlternative alts
118+ g' (Case vs alts) = Case <$> traverse (g' <=< g) vs <*> traverse handleCaseAlternative alts
128119 g' (TypedValue check v ty) = TypedValue check <$> (g v >>= g') <*> pure ty
129− g' (Let ds v) = Let <$> mapM (f' <=< f) ds <*> (g v >>= g')
130− g' (Do es) = Do <$> mapM handleDoNotationElement es
120+ g' (Let ds v) = Let <$> traverse (f' <=< f) ds <*> (g v >>= g')
121+ g' (Do es) = Do <$> traverse handleDoNotationElement es
131122 g' (PositionedValue pos com v) = PositionedValue pos com <$> (g v >>= g')
132123 g' other = g other
133124
134− h' (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> mapM (h' <=< h) bs
135− h' (ObjectBinder bs) = ObjectBinder <$> mapM (sndM (h' <=< h)) bs
136− h' (ArrayBinder bs) = ArrayBinder <$> mapM (h' <=< h) bs
125+ h' (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> traverse (h' <=< h) bs
126+ h' (ObjectBinder bs) = ObjectBinder <$> traverse (sndM (h' <=< h)) bs
127+ h' (ArrayBinder bs) = ArrayBinder <$> traverse (h' <=< h) bs
137128 h' (NamedBinder name b) = NamedBinder name <$> (h b >>= h')
138129 h' (PositionedBinder pos com b) = PositionedBinder pos com <$> (h b >>= h')
139130 h' (TypedBinder t b) = TypedBinder t <$> (h b >>= h')
140131 h' other = h other
141132
142− handleCaseAlternative (CaseAlternative bs val) = CaseAlternative <$> mapM (h' <=< h) bs
143− <*> eitherM (mapM (pairM (g' <=< g) (g' <=< g))) (g' <=< g) val
133+ handleCaseAlternative (CaseAlternative bs val) = CaseAlternative <$> traverse (h' <=< h) bs
134+ <*> eitherM (traverse (pairM (g' <=< g) (g' <=< g))) (g' <=< g) val
144135
145136 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> (g' <=< g) v
146137 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> (h' <=< h) b <*> (g' <=< g) v
147− handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM (f' <=< f) ds
138+ handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> traverse (f' <=< f) ds
148139 handleDoNotationElement (PositionedDoNotationElement pos com e) = PositionedDoNotationElement pos com <$> handleDoNotationElement e
149140
150141everywhereOnValuesM :: (Functor m, Applicative m, Monad m) =>
@@ -154,11 +145,11 @@ everywhereOnValuesM :: (Functor m, Applicative m, Monad m) =>
154145 (Declaration -> m Declaration, Expr -> m Expr, Binder -> m Binder)
155146everywhereOnValuesM f g h = (f', g', h')
156147 where
157− f' (DataBindingGroupDeclaration ds) = (DataBindingGroupDeclaration <$> mapM f' ds) >>= f
158− f' (ValueDeclaration name nameKind bs val) = (ValueDeclaration name nameKind <$> mapM h' bs <*> eitherM (mapM (pairM g' g')) g' val) >>= f
159− f' (BindingGroupDeclaration ds) = (BindingGroupDeclaration <$> mapM (\(name, nameKind, val) -> (,,) name nameKind <$> g' val) ds) >>= f
160− f' (TypeClassDeclaration name args implies ds) = (TypeClassDeclaration name args implies <$> mapM f' ds) >>= f
161− f' (TypeInstanceDeclaration name cs className args ds) = (TypeInstanceDeclaration name cs className args <$> traverseTypeInstanceBody (mapM f') ds) >>= f
148+ f' (DataBindingGroupDeclaration ds) = (DataBindingGroupDeclaration <$> traverse f' ds) >>= f
149+ f' (ValueDeclaration name nameKind bs val) = (ValueDeclaration name nameKind <$> traverse h' bs <*> eitherM (traverse (pairM g' g')) g' val) >>= f
150+ f' (BindingGroupDeclaration ds) = (BindingGroupDeclaration <$> traverse (\(name, nameKind, val) -> (,,) name nameKind <$> g' val) ds) >>= f
151+ f' (TypeClassDeclaration name args implies ds) = (TypeClassDeclaration name args implies <$> traverse f' ds) >>= f
152+ f' (TypeInstanceDeclaration name cs className args ds) = (TypeInstanceDeclaration name cs className args <$> traverseTypeInstanceBody (traverse f') ds) >>= f
162153 f' (PositionedDeclaration pos com d) = (PositionedDeclaration pos com <$> f' d) >>= f
163154 f' other = f other
164155
@@ -167,37 +158,37 @@ everywhereOnValuesM f g h = (f', g', h')
167158 g' (Parens v) = (Parens <$> g' v) >>= g
168159 g' (OperatorSection op (Left v)) = (OperatorSection <$> g' op <*> (Left <$> g' v)) >>= g
169160 g' (OperatorSection op (Right v)) = (OperatorSection <$> g' op <*> (Right <$> g' v)) >>= g
170− g' (ArrayLiteral vs) = (ArrayLiteral <$> mapM g' vs) >>= g
171− g' (ObjectLiteral vs) = (ObjectLiteral <$> mapM (sndM g') vs) >>= g
172− g' (ObjectConstructor vs) = (ObjectConstructor <$> mapM (sndM $ maybeM g') vs) >>= g
161+ g' (ArrayLiteral vs) = (ArrayLiteral <$> traverse g' vs) >>= g
162+ g' (ObjectLiteral vs) = (ObjectLiteral <$> traverse (sndM g') vs) >>= g
163+ g' (ObjectConstructor vs) = (ObjectConstructor <$> traverse (sndM $ maybeM g') vs) >>= g
173164 g' (TypeClassDictionaryConstructorApp name v) = (TypeClassDictionaryConstructorApp name <$> g' v) >>= g
174165 g' (Accessor prop v) = (Accessor prop <$> g' v) >>= g
175− g' (ObjectUpdate obj vs) = (ObjectUpdate <$> g' obj <*> mapM (sndM g') vs) >>= g
176− g' (ObjectUpdater obj vs) = (ObjectUpdater <$> maybeM g' obj <*> mapM (sndM $ maybeM g') vs) >>= g
166+ g' (ObjectUpdate obj vs) = (ObjectUpdate <$> g' obj <*> traverse (sndM g') vs) >>= g
167+ g' (ObjectUpdater obj vs) = (ObjectUpdater <$> maybeM g' obj <*> traverse (sndM $ maybeM g') vs) >>= g
177168 g' (Abs name v) = (Abs name <$> g' v) >>= g
178169 g' (App v1 v2) = (App <$> g' v1 <*> g' v2) >>= g
179170 g' (IfThenElse v1 v2 v3) = (IfThenElse <$> g' v1 <*> g' v2 <*> g' v3) >>= g
180− g' (Case vs alts) = (Case <$> mapM g' vs <*> mapM handleCaseAlternative alts) >>= g
171+ g' (Case vs alts) = (Case <$> traverse g' vs <*> traverse handleCaseAlternative alts) >>= g
181172 g' (TypedValue check v ty) = (TypedValue check <$> g' v <*> pure ty) >>= g
182− g' (Let ds v) = (Let <$> mapM f' ds <*> g' v) >>= g
183− g' (Do es) = (Do <$> mapM handleDoNotationElement es) >>= g
173+ g' (Let ds v) = (Let <$> traverse f' ds <*> g' v) >>= g
174+ g' (Do es) = (Do <$> traverse handleDoNotationElement es) >>= g
184175 g' (PositionedValue pos com v) = (PositionedValue pos com <$> g' v) >>= g
185176 g' other = g other
186177
187− h' (ConstructorBinder ctor bs) = (ConstructorBinder ctor <$> mapM h' bs) >>= h
188− h' (ObjectBinder bs) = (ObjectBinder <$> mapM (sndM h') bs) >>= h
189− h' (ArrayBinder bs) = (ArrayBinder <$> mapM h' bs) >>= h
178+ h' (ConstructorBinder ctor bs) = (ConstructorBinder ctor <$> traverse h' bs) >>= h
179+ h' (ObjectBinder bs) = (ObjectBinder <$> traverse (sndM h') bs) >>= h
180+ h' (ArrayBinder bs) = (ArrayBinder <$> traverse h' bs) >>= h
190181 h' (NamedBinder name b) = (NamedBinder name <$> h' b) >>= h
191182 h' (PositionedBinder pos com b) = (PositionedBinder pos com <$> h' b) >>= h
192183 h' (TypedBinder t b) = (TypedBinder t <$> h' b) >>= h
193184 h' other = h other
194185
195− handleCaseAlternative (CaseAlternative bs val) = CaseAlternative <$> mapM h' bs
196− <*> eitherM (mapM (pairM g' g')) g' val
186+ handleCaseAlternative (CaseAlternative bs val) = CaseAlternative <$> traverse h' bs
187+ <*> eitherM (traverse (pairM g' g')) g' val
197188
198189 handleDoNotationElement (DoNotationValue v) = DoNotationValue <$> g' v
199190 handleDoNotationElement (DoNotationBind b v) = DoNotationBind <$> h' b <*> g' v
200− handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> mapM f' ds
191+ handleDoNotationElement (DoNotationLet ds) = DoNotationLet <$> traverse f' ds
201192 handleDoNotationElement (PositionedDoNotationElement pos com e) = PositionedDoNotationElement pos com <$> handleDoNotationElement e
202193
203194everythingOnValues :: (r -> r -> r) ->
@@ -345,11 +336,11 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
345336 where
346337 f'' s = uncurry f' <=< f s
347338
348− f' s (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> mapM (f'' s) ds
349− f' s (ValueDeclaration name nameKind bs val) = ValueDeclaration name nameKind <$> mapM (h'' s) bs <*> eitherM (mapM (pairM (g'' s) (g'' s))) (g'' s) val
350− f' s (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> mapM (thirdM (g'' s)) ds
351− f' s (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> mapM (f'' s) ds
352− f' s (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> traverseTypeInstanceBody (mapM (f'' s)) ds
339+ f' s (DataBindingGroupDeclaration ds) = DataBindingGroupDeclaration <$> traverse (f'' s) ds
340+ f' s (ValueDeclaration name nameKind bs val) = ValueDeclaration name nameKind <$> traverse (h'' s) bs <*> eitherM (traverse (pairM (g'' s) (g'' s))) (g'' s) val
341+ f' s (BindingGroupDeclaration ds) = BindingGroupDeclaration <$> traverse (thirdM (g'' s)) ds
342+ f' s (TypeClassDeclaration name args implies ds) = TypeClassDeclaration name args implies <$> traverse (f'' s) ds
343+ f' s (TypeInstanceDeclaration name cs className args ds) = TypeInstanceDeclaration name cs className args <$> traverseTypeInstanceBody (traverse (f'' s)) ds
353344 f' s (PositionedDeclaration pos com d1) = PositionedDeclaration pos com <$> f'' s d1
354345 f' _ other = return other
355346
@@ -360,28 +351,28 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
360351 g' s (Parens v) = Parens <$> g'' s v
361352 g' s (OperatorSection op (Left v)) = OperatorSection <$> g'' s op <*> (Left <$> g'' s v)
362353 g' s (OperatorSection op (Right v)) = OperatorSection <$> g'' s op <*> (Right <$> g'' s v)
363− g' s (ArrayLiteral vs) = ArrayLiteral <$> mapM (g'' s) vs
364− g' s (ObjectLiteral vs) = ObjectLiteral <$> mapM (sndM (g'' s)) vs
365− g' s (ObjectConstructor vs) = ObjectConstructor <$> mapM (sndM $ maybeM (g'' s)) vs
354+ g' s (ArrayLiteral vs) = ArrayLiteral <$> traverse (g'' s) vs
355+ g' s (ObjectLiteral vs) = ObjectLiteral <$> traverse (sndM (g'' s)) vs
356+ g' s (ObjectConstructor vs) = ObjectConstructor <$> traverse (sndM $ maybeM (g'' s)) vs
366357 g' s (TypeClassDictionaryConstructorApp name v) = TypeClassDictionaryConstructorApp name <$> g'' s v
367358 g' s (Accessor prop v) = Accessor prop <$> g'' s v
368− g' s (ObjectUpdate obj vs) = ObjectUpdate <$> g'' s obj <*> mapM (sndM (g'' s)) vs
369− g' s (ObjectUpdater obj vs) = ObjectUpdater <$> maybeM (g'' s) obj <*> mapM (sndM $ maybeM (g'' s)) vs
359+ g' s (ObjectUpdate obj vs) = ObjectUpdate <$> g'' s obj <*> traverse (sndM (g'' s)) vs
360+ g' s (ObjectUpdater obj vs) = ObjectUpdater <$> maybeM (g'' s) obj <*> traverse (sndM $ maybeM (g'' s)) vs
370361 g' s (Abs name v) = Abs name <$> g'' s v
371362 g' s (App v1 v2) = App <$> g'' s v1 <*> g'' s v2
372363 g' s (IfThenElse v1 v2 v3) = IfThenElse <$> g'' s v1 <*> g'' s v2 <*> g'' s v3
373− g' s (Case vs alts) = Case <$> mapM (g'' s) vs <*> mapM (i'' s) alts
364+ g' s (Case vs alts) = Case <$> traverse (g'' s) vs <*> traverse (i'' s) alts
374365 g' s (TypedValue check v ty) = TypedValue check <$> g'' s v <*> pure ty
375− g' s (Let ds v) = Let <$> mapM (f'' s) ds <*> g'' s v
376− g' s (Do es) = Do <$> mapM (j'' s) es
366+ g' s (Let ds v) = Let <$> traverse (f'' s) ds <*> g'' s v
367+ g' s (Do es) = Do <$> traverse (j'' s) es
377368 g' s (PositionedValue pos com v) = PositionedValue pos com <$> g'' s v
378369 g' _ other = return other
379370
380371 h'' s = uncurry h' <=< h s
381372
382− h' s (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> mapM (h'' s) bs
383− h' s (ObjectBinder bs) = ObjectBinder <$> mapM (sndM (h'' s)) bs
384− h' s (ArrayBinder bs) = ArrayBinder <$> mapM (h'' s) bs
373+ h' s (ConstructorBinder ctor bs) = ConstructorBinder ctor <$> traverse (h'' s) bs
374+ h' s (ObjectBinder bs) = ObjectBinder <$> traverse (sndM (h'' s)) bs
375+ h' s (ArrayBinder bs) = ArrayBinder <$> traverse (h'' s) bs
385376 h' s (NamedBinder name b) = NamedBinder name <$> h'' s b
386377 h' s (PositionedBinder pos com b) = PositionedBinder pos com <$> h'' s b
387378 h' s (TypedBinder t b) = TypedBinder t <$> h'' s b
@@ -389,13 +380,13 @@ everywhereWithContextOnValuesM s0 f g h i j = (f'' s0, g'' s0, h'' s0, i'' s0, j
389380
390381 i'' s = uncurry i' <=< i s
391382
392− i' s (CaseAlternative bs val) = CaseAlternative <$> mapM (h'' s) bs <*> eitherM (mapM (pairM (g'' s) (g'' s))) (g'' s) val
383+ i' s (CaseAlternative bs val) = CaseAlternative <$> traverse (h'' s) bs <*> eitherM (traverse (pairM (g'' s) (g'' s))) (g'' s) val
393384
394385 j'' s = uncurry j' <=< j s
395386
396387 j' s (DoNotationValue v) = DoNotationValue <$> g'' s v
397388 j' s (DoNotationBind b v) = DoNotationBind <$> h'' s b <*> g'' s v
398− j' s (DoNotationLet ds) = DoNotationLet <$> mapM (f'' s) ds
389+ j' s (DoNotationLet ds) = DoNotationLet <$> traverse (f'' s) ds
399390 j' s (PositionedDoNotationElement pos com e1) = PositionedDoNotationElement pos com <$> j'' s e1
400391
401392accumTypes :: (Monoid r) => (Type -> r) -> (Declaration -> r, Expr -> r, Binder -> r, CaseAlternative -> r, DoNotationElement -> r)