From c6e07f1680fc9e8c8e96e3f2e92e25e82feddc2b Mon Sep 17 00:00:00 2001 From: Mann mit Hut Date: Wed, 10 Feb 2021 13:33:14 +0100 Subject: [PATCH 1/2] Expose a traversal over immediate subexpressions with the ability to map over embedded values --- dhall/src/Dhall/Core.hs | 1 + dhall/src/Dhall/Syntax.hs | 25 +++++++++++++++++-------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/dhall/src/Dhall/Core.hs b/dhall/src/Dhall/Core.hs index 1d970994f..7c5f0efd8 100644 --- a/dhall/src/Dhall/Core.hs +++ b/dhall/src/Dhall/Core.hs @@ -56,6 +56,7 @@ module Dhall.Core ( -- * Optics , subExpressions + , subExpressionsWith , chunkExprs , bindingExprs , recordFieldExprs diff --git a/dhall/src/Dhall/Syntax.hs b/dhall/src/Dhall/Syntax.hs index 9cf8087ec..252b50bb6 100644 --- a/dhall/src/Dhall/Syntax.hs +++ b/dhall/src/Dhall/Syntax.hs @@ -44,6 +44,7 @@ module Dhall.Syntax ( -- ** Optics , subExpressions + , subExpressionsWith , unsafeSubExpressions , chunkExprs , bindingExprs @@ -757,16 +758,24 @@ data MultiLet s a = MultiLet (NonEmpty (Binding s a)) (Expr s a) -- | A traversal over the immediate sub-expressions of an expression. subExpressions :: Applicative f => (Expr s a -> f (Expr s a)) -> Expr s a -> f (Expr s a) -subExpressions _ (Embed a) = pure (Embed a) -subExpressions f (Note a b) = Note a <$> f b -subExpressions f (Let a b) = Let <$> bindingExprs f a <*> f b -subExpressions f (Record a) = Record <$> traverse (recordFieldExprs f) a -subExpressions f (RecordLit a) = RecordLit <$> traverse (recordFieldExprs f) a -subExpressions f (Lam cs fb e) = Lam cs <$> functionBindingExprs f fb <*> f e -subExpressions f (Field a b) = Field <$> f a <*> pure b -subExpressions f expression = unsafeSubExpressions f expression +subExpressions = subExpressionsWith (pure . Embed) {-# INLINABLE subExpressions #-} +{-| A traversal over the immediate sub-expressions of an expression which + allows mapping embedded values +-} +subExpressionsWith + :: Applicative f => (a -> f (Expr s b)) -> (Expr s a -> f (Expr s b)) -> Expr s a -> f (Expr s b) +subExpressionsWith h _ (Embed a) = h a +subExpressionsWith _ f (Note a b) = Note a <$> f b +subExpressionsWith _ f (Let a b) = Let <$> bindingExprs f a <*> f b +subExpressionsWith _ f (Record a) = Record <$> traverse (recordFieldExprs f) a +subExpressionsWith _ f (RecordLit a) = RecordLit <$> traverse (recordFieldExprs f) a +subExpressionsWith _ f (Lam cs fb e) = Lam cs <$> functionBindingExprs f fb <*> f e +subExpressionsWith _ f (Field a b) = Field <$> f a <*> pure b +subExpressionsWith _ f expression = unsafeSubExpressions f expression +{-# INLINABLE subExpressionsWith #-} + {-| An internal utility used to implement transformations that require changing one of the type variables of the `Expr` type From c7cdb1fd94bc54ee2251a85a08f81ac4fa0d3e5a Mon Sep 17 00:00:00 2001 From: Mann mit Hut Date: Sat, 18 Sep 2021 10:57:53 +0200 Subject: [PATCH 2/2] Re-run CI