From e9d61b79faf40200d8f9806d83a05ece272cd7d3 Mon Sep 17 00:00:00 2001 From: Łukasz Hanuszczak Date: Fri, 10 Jul 2015 11:42:18 +0200 Subject: Move SYB utilities to standalone module. --- haddock-api/src/Haddock/Syb.hs | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 haddock-api/src/Haddock/Syb.hs (limited to 'haddock-api/src/Haddock/Syb.hs') diff --git a/haddock-api/src/Haddock/Syb.hs b/haddock-api/src/Haddock/Syb.hs new file mode 100644 index 00000000..dd7ffc1b --- /dev/null +++ b/haddock-api/src/Haddock/Syb.hs @@ -0,0 +1,26 @@ +{-# LANGUAGE Rank2Types #-} + + +module Haddock.Syb + ( everything + , combine + ) where + + +import Data.Data +import Control.Applicative + + +-- | Perform a query on each level of a tree. +-- +-- This is stolen directly from SYB package and copied here to not introduce +-- additional dependencies. +everything :: (r -> r -> r) -> (forall a. Data a => a -> r) + -> (forall a. Data a => a -> r) +everything k f x = foldl k (f x) (gmapQ (everything k f) x) + +-- | Combine two queries into one using alternative combinator. +combine :: Alternative f => (forall a. Data a => a -> f r) + -> (forall a. Data a => a -> f r) + -> (forall a. Data a => a -> f r) +combine f g x = f x <|> g x -- cgit v1.2.3 From f7d377ee238d3b44240a4537986a7561e822f79f Mon Sep 17 00:00:00 2001 From: Łukasz Hanuszczak Date: Fri, 10 Jul 2015 11:49:02 +0200 Subject: Implement `everywhere` transformation in SYB module. --- haddock-api/src/Haddock/Syb.hs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) (limited to 'haddock-api/src/Haddock/Syb.hs') diff --git a/haddock-api/src/Haddock/Syb.hs b/haddock-api/src/Haddock/Syb.hs index dd7ffc1b..3cec724e 100644 --- a/haddock-api/src/Haddock/Syb.hs +++ b/haddock-api/src/Haddock/Syb.hs @@ -2,7 +2,7 @@ module Haddock.Syb - ( everything + ( everything, everywhere , combine ) where @@ -19,6 +19,12 @@ everything :: (r -> r -> r) -> (forall a. Data a => a -> r) -> (forall a. Data a => a -> r) everything k f x = foldl k (f x) (gmapQ (everything k f) x) +-- | Apply transformation on each level of a tree. +-- +-- Just like 'everything', this is stolen from SYB package. +everywhere :: (forall a. Data a => a -> a) -> (forall a. Data a => a -> a) +everywhere f = f . gmapT (everywhere f) + -- | Combine two queries into one using alternative combinator. combine :: Alternative f => (forall a. Data a => a -> f r) -> (forall a. Data a => a -> f r) -- cgit v1.2.3 From dc25b7099a0d54c03bdf51dfb9a4e286942d9c31 Mon Sep 17 00:00:00 2001 From: Łukasz Hanuszczak Date: Fri, 10 Jul 2015 12:24:12 +0200 Subject: Implement generic transformation constructor. --- haddock-api/src/Haddock/Syb.hs | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'haddock-api/src/Haddock/Syb.hs') diff --git a/haddock-api/src/Haddock/Syb.hs b/haddock-api/src/Haddock/Syb.hs index 3cec724e..2016b74c 100644 --- a/haddock-api/src/Haddock/Syb.hs +++ b/haddock-api/src/Haddock/Syb.hs @@ -3,6 +3,7 @@ module Haddock.Syb ( everything, everywhere + , mkT , combine ) where @@ -25,6 +26,14 @@ everything k f x = foldl k (f x) (gmapQ (everything k f) x) everywhere :: (forall a. Data a => a -> a) -> (forall a. Data a => a -> a) everywhere f = f . gmapT (everywhere f) +-- | Create generic transformation. +-- +-- Another function stolen from SYB package. +mkT :: (Typeable a, Typeable b) => (b -> b) -> (a -> a) +mkT f = case cast f of + Just f' -> f' + Nothing -> id + -- | Combine two queries into one using alternative combinator. combine :: Alternative f => (forall a. Data a => a -> f r) -> (forall a. Data a => a -> f r) -- cgit v1.2.3 From d5700e1427f7171db0e0e9393aedb734b554459f Mon Sep 17 00:00:00 2001 From: Łukasz Hanuszczak Date: Sun, 19 Jul 2015 18:16:11 +0200 Subject: Add SYB-like utility function for performing stateful queries. --- haddock-api/src/Haddock/Syb.hs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'haddock-api/src/Haddock/Syb.hs') diff --git a/haddock-api/src/Haddock/Syb.hs b/haddock-api/src/Haddock/Syb.hs index 2016b74c..4847e486 100644 --- a/haddock-api/src/Haddock/Syb.hs +++ b/haddock-api/src/Haddock/Syb.hs @@ -2,7 +2,7 @@ module Haddock.Syb - ( everything, everywhere + ( everything, everythingWithState, everywhere , mkT , combine ) where @@ -20,6 +20,20 @@ everything :: (r -> r -> r) -> (forall a. Data a => a -> r) -> (forall a. Data a => a -> r) everything k f x = foldl k (f x) (gmapQ (everything k f) x) + +-- | Perform a query with state on each level of a tree. +-- +-- This is the same as 'everything' but allows for stateful computations. In +-- SYB it is called @everythingWithContext@ but I find this name somewhat +-- nicer. +everythingWithState :: s -> (r -> r -> r) + -> (forall a. Data a => a -> s -> (r, s)) + -> (forall a. Data a => a -> r) +everythingWithState s k f x = + let (r, s') = f x s + in foldl k r (gmapQ (everythingWithState s' k f) x) + + -- | Apply transformation on each level of a tree. -- -- Just like 'everything', this is stolen from SYB package. -- cgit v1.2.3