Skip to content

Commit ab64a9d

Browse files
authored
Traversal over immediate subexpressions and the embedded values (#2302)
1 parent 97f0cd8 commit ab64a9d

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

dhall/src/Dhall/Core.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ module Dhall.Core (
5656

5757
-- * Optics
5858
, subExpressions
59+
, subExpressionsWith
5960
, chunkExprs
6061
, bindingExprs
6162
, recordFieldExprs

dhall/src/Dhall/Syntax.hs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ module Dhall.Syntax (
4646

4747
-- ** Optics
4848
, subExpressions
49+
, subExpressionsWith
4950
, unsafeSubExpressions
5051
, chunkExprs
5152
, bindingExprs
@@ -782,16 +783,24 @@ data MultiLet s a = MultiLet (NonEmpty (Binding s a)) (Expr s a)
782783
-- | A traversal over the immediate sub-expressions of an expression.
783784
subExpressions
784785
:: Applicative f => (Expr s a -> f (Expr s a)) -> Expr s a -> f (Expr s a)
785-
subExpressions _ (Embed a) = pure (Embed a)
786-
subExpressions f (Note a b) = Note a <$> f b
787-
subExpressions f (Let a b) = Let <$> bindingExprs f a <*> f b
788-
subExpressions f (Record a) = Record <$> traverse (recordFieldExprs f) a
789-
subExpressions f (RecordLit a) = RecordLit <$> traverse (recordFieldExprs f) a
790-
subExpressions f (Lam cs fb e) = Lam cs <$> functionBindingExprs f fb <*> f e
791-
subExpressions f (Field a b) = Field <$> f a <*> pure b
792-
subExpressions f expression = unsafeSubExpressions f expression
786+
subExpressions = subExpressionsWith (pure . Embed)
793787
{-# INLINABLE subExpressions #-}
794788

789+
{-| A traversal over the immediate sub-expressions of an expression which
790+
allows mapping embedded values
791+
-}
792+
subExpressionsWith
793+
:: Applicative f => (a -> f (Expr s b)) -> (Expr s a -> f (Expr s b)) -> Expr s a -> f (Expr s b)
794+
subExpressionsWith h _ (Embed a) = h a
795+
subExpressionsWith _ f (Note a b) = Note a <$> f b
796+
subExpressionsWith _ f (Let a b) = Let <$> bindingExprs f a <*> f b
797+
subExpressionsWith _ f (Record a) = Record <$> traverse (recordFieldExprs f) a
798+
subExpressionsWith _ f (RecordLit a) = RecordLit <$> traverse (recordFieldExprs f) a
799+
subExpressionsWith _ f (Lam cs fb e) = Lam cs <$> functionBindingExprs f fb <*> f e
800+
subExpressionsWith _ f (Field a b) = Field <$> f a <*> pure b
801+
subExpressionsWith _ f expression = unsafeSubExpressions f expression
802+
{-# INLINABLE subExpressionsWith #-}
803+
795804
{-| An internal utility used to implement transformations that require changing
796805
one of the type variables of the `Expr` type
797806

0 commit comments

Comments
 (0)