module Data.Generics.Traversable
(
GTraversable(..)
, gmap
, gmapM
, gfoldMap
, gfoldr
, gfoldl'
, Rec
, everywhere
, everywhere'
, everywhereM
, everything
)
where
import GHC.Exts (Constraint)
import Control.Applicative
import Control.Monad
import Data.Monoid
import Data.Functor.Identity
import Data.Functor.Constant
import Data.Proxy.Fork
import Data.Generics.Traversable.Core
import Data.Generics.Traversable.Instances ()
import Data.Foldable
import Data.Traversable
class (GTraversable (Rec c) a, c a) => Rec (c :: * -> Constraint) a
instance (GTraversable (Rec c) a, c a) => Rec (c :: * -> Constraint) a
gmap
:: (GTraversable c a, ?c :: p c)
=> (forall d . (c d) => d -> d)
-> a -> a
gmap f = runIdentity . gtraverse (Identity . f)
gmapM
:: (Monad m, GTraversable c a, ?c :: p c)
=> (forall d . (c d) => d -> m d)
-> a -> m a
gmapM f = unwrapMonad . gtraverse (WrapMonad . f)
gfoldMap
:: (Monoid r, GTraversable c a, ?c :: p c)
=> (forall d . (c d) => d -> r)
-> a -> r
gfoldMap f = getConstant . gtraverse (Constant . f)
gfoldr
:: (GTraversable c a, ?c :: p c)
=> (forall d . (c d) => d -> r -> r)
-> r -> a -> r
gfoldr f z t = appEndo (gfoldMap (Endo . f) t) z
gfoldl'
:: (GTraversable c a, ?c :: p c)
=> (forall d . (c d) => r -> d -> r)
-> r -> a -> r
gfoldl' f z0 xs = gfoldr f' id xs z0
where f' x k z = k $! f z x
everywhere
:: forall a c p .
(Rec c a, ?c :: p c)
=> (forall d. (Rec c d) => d -> d)
-> a -> a
everywhere f =
let ?c = Proxy :: Proxy (Rec c) in
let
go :: forall a . Rec c a => a -> a
go = f . gmap go
in go
everywhere'
:: forall a c p .
(Rec c a, ?c :: p c)
=> (forall d. (Rec c d) => d -> d)
-> a -> a
everywhere' f =
let ?c = Proxy :: Proxy (Rec c) in
let
go :: forall a . Rec c a => a -> a
go = gmap go . f
in go
everywhereM
:: forall m a c p .
(Monad m, Rec c a, ?c :: p c)
=> (forall d. (Rec c d) => d -> m d)
-> a -> m a
everywhereM f =
let ?c = Proxy :: Proxy (Rec c) in
let
go :: forall a . Rec c a => a -> m a
go = f <=< gmapM go
in go
everything
:: forall r a c p .
(Rec c a, ?c :: p c)
=> (r -> r -> r)
-> (forall d . (Rec c d) => d -> r)
-> a -> r
everything combine f =
let ?c = Proxy :: Proxy (Rec c) in
let
go :: forall a . Rec c a => a -> r
go x = gfoldl' (\a y -> combine a (go y)) (f x) x
in go