{-# LANGUAGE RankNTypes, ScopedTypeVariables, CPP #-}
module Data.Generics.Schemes (
everywhere,
everywhere',
everywhereBut,
everywhereM,
somewhere,
everything,
everythingBut,
everythingWithContext,
listify,
something,
synthesize,
gsize,
glength,
gdepth,
gcount,
gnodecount,
gtypecount,
gfindtype
) where
#ifdef __HADDOCK__
import Prelude
#endif
import Data.Data
import Data.Generics.Aliases
import Control.Monad
everywhere :: (forall a. Data a => a -> a)
-> (forall a. Data a => a -> a)
everywhere :: (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere forall a. Data a => a -> a
f = forall a. Data a => a -> a
go
where
go :: forall a. Data a => a -> a
go :: forall a. Data a => a -> a
go = forall a. Data a => a -> a
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Data a => (forall a. Data a => a -> a) -> a -> a
gmapT forall a. Data a => a -> a
go
everywhere' :: (forall a. Data a => a -> a)
-> (forall a. Data a => a -> a)
everywhere' :: (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhere' forall a. Data a => a -> a
f = forall a. Data a => a -> a
go
where
go :: forall a. Data a => a -> a
go :: forall a. Data a => a -> a
go = forall a. Data a => (forall a. Data a => a -> a) -> a -> a
gmapT forall a. Data a => a -> a
go forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Data a => a -> a
f
everywhereBut :: GenericQ Bool -> GenericT -> GenericT
everywhereBut :: GenericQ Bool
-> (forall a. Data a => a -> a) -> forall a. Data a => a -> a
everywhereBut GenericQ Bool
q forall a. Data a => a -> a
f = forall a. Data a => a -> a
go
where
go :: GenericT
go :: forall a. Data a => a -> a
go a
x
| GenericQ Bool
q a
x = a
x
| Bool
otherwise = forall a. Data a => a -> a
f (forall a. Data a => (forall a. Data a => a -> a) -> a -> a
gmapT forall a. Data a => a -> a
go a
x)
everywhereM :: forall m. Monad m => GenericM m -> GenericM m
everywhereM :: forall (m :: * -> *). Monad m => GenericM m -> GenericM m
everywhereM GenericM m
f = GenericM m
go
where
go :: GenericM m
go :: GenericM m
go a
x = do
a
x' <- forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> a -> m a
gmapM GenericM m
go a
x
GenericM m
f a
x'
somewhere :: forall m. MonadPlus m => GenericM m -> GenericM m
somewhere :: forall (m :: * -> *). MonadPlus m => GenericM m -> GenericM m
somewhere GenericM m
f = GenericM m
go
where
go :: GenericM m
go :: GenericM m
go a
x = GenericM m
f a
x forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
`mplus` forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> a -> m a
gmapMp GenericM m
go a
x
everything :: forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything :: forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything r -> r -> r
k GenericQ r
f = GenericQ r
go
where
go :: GenericQ r
go :: GenericQ r
go a
x = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl r -> r -> r
k (GenericQ r
f a
x) (forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ GenericQ r
go a
x)
everythingBut :: forall r. (r -> r -> r) -> GenericQ (r, Bool) -> GenericQ r
everythingBut :: forall r. (r -> r -> r) -> GenericQ (r, Bool) -> GenericQ r
everythingBut r -> r -> r
k GenericQ (r, Bool)
f = GenericQ r
go
where
go :: GenericQ r
go :: GenericQ r
go a
x = let (r
v, Bool
stop) = GenericQ (r, Bool)
f a
x
in if Bool
stop
then r
v
else forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl r -> r -> r
k r
v (forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ GenericQ r
go a
x)
everythingWithContext :: forall s r. s -> (r -> r -> r) -> GenericQ (s -> (r, s)) -> GenericQ r
everythingWithContext :: forall s r.
s -> (r -> r -> r) -> GenericQ (s -> (r, s)) -> GenericQ r
everythingWithContext s
s0 r -> r -> r
f GenericQ (s -> (r, s))
q = s -> GenericQ r
go s
s0
where
go :: s -> GenericQ r
go :: s -> GenericQ r
go s
s a
x = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl r -> r -> r
f r
r (forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ (s -> GenericQ r
go s
s') a
x)
where (r
r, s
s') = GenericQ (s -> (r, s))
q a
x s
s
listify :: Typeable r => (r -> Bool) -> GenericQ [r]
listify :: forall r. Typeable r => (r -> Bool) -> GenericQ [r]
listify r -> Bool
p = forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything forall a. [a] -> [a] -> [a]
(++) ([] forall a b r. (Typeable a, Typeable b) => r -> (b -> r) -> a -> r
`mkQ` (\r
x -> if r -> Bool
p r
x then [r
x] else []))
something :: GenericQ (Maybe u) -> GenericQ (Maybe u)
something :: forall u. GenericQ (Maybe u) -> GenericQ (Maybe u)
something = forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything forall a. Maybe a -> Maybe a -> Maybe a
orElse
synthesize :: forall s t. s -> (t -> s -> s) -> GenericQ (s -> t) -> GenericQ t
synthesize :: forall s t. s -> (t -> s -> s) -> GenericQ (s -> t) -> GenericQ t
synthesize s
z t -> s -> s
o GenericQ (s -> t)
f = GenericQ t
go
where
go :: GenericQ t
go :: GenericQ t
go a
x = GenericQ (s -> t)
f a
x (forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr t -> s -> s
o s
z (forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ GenericQ t
go a
x))
gsize :: Data a => a -> Int
gsize :: forall a. Data a => a -> Int
gsize a
t = Int
1 forall a. Num a => a -> a -> a
+ forall (t :: * -> *) a. (Foldable t, Num a) => t a -> a
sum (forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ forall a. Data a => a -> Int
gsize a
t)
glength :: GenericQ Int
glength :: forall a. Data a => a -> Int
glength = forall (t :: * -> *) a. Foldable t => t a -> Int
length forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ (forall a b. a -> b -> a
const ())
gdepth :: GenericQ Int
gdepth :: forall a. Data a => a -> Int
gdepth = forall a. Num a => a -> a -> a
(+) Int
1 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr forall a. Ord a => a -> a -> a
max Int
0 forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ forall a. Data a => a -> Int
gdepth
gcount :: GenericQ Bool -> GenericQ Int
gcount :: GenericQ Bool -> forall a. Data a => a -> Int
gcount GenericQ Bool
p = forall r. (r -> r -> r) -> GenericQ r -> GenericQ r
everything forall a. Num a => a -> a -> a
(+) (\a
x -> if GenericQ Bool
p a
x then Int
1 else Int
0)
gnodecount :: GenericQ Int
gnodecount :: forall a. Data a => a -> Int
gnodecount = GenericQ Bool -> forall a. Data a => a -> Int
gcount (forall a b. a -> b -> a
const Bool
True)
gtypecount :: Typeable a => a -> GenericQ Int
gtypecount :: forall a. Typeable a => a -> forall a. Data a => a -> Int
gtypecount (a
_::a) = GenericQ Bool -> forall a. Data a => a -> Int
gcount (Bool
False forall a b r. (Typeable a, Typeable b) => r -> (b -> r) -> a -> r
`mkQ` (\(a
_::a) -> Bool
True))
gfindtype :: (Data x, Typeable y) => x -> Maybe y
gfindtype :: forall x y. (Data x, Typeable y) => x -> Maybe y
gfindtype = forall {a}. [a] -> Maybe a
singleton
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl forall {a}. [a] -> Maybe a -> [a]
unJust []
forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a u. Data a => (forall d. Data d => d -> u) -> a -> [u]
gmapQ (forall a. Maybe a
Nothing forall a b r. (Typeable a, Typeable b) => r -> (b -> r) -> a -> r
`mkQ` forall a. a -> Maybe a
Just)
where
unJust :: [a] -> Maybe a -> [a]
unJust [a]
l (Just a
x) = a
xforall a. a -> [a] -> [a]
:[a]
l
unJust [a]
l Maybe a
Nothing = [a]
l
singleton :: [a] -> Maybe a
singleton [a
s] = forall a. a -> Maybe a
Just a
s
singleton [a]
_ = forall a. Maybe a
Nothing