module Data.Generics.Uniplate
where
import Control.Monad
import Data.Generics.Uniplate.Internal.Utils
type UniplateType on = on -> ([on], [on] -> on)
class Uniplate on where
uniplate :: UniplateType on
universe :: Uniplate on => on -> [on]
universe :: on -> [on]
universe on
x = (forall b. (on -> b -> b) -> b -> b) -> [on]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
builder (on -> (on -> b -> b) -> b -> b
forall on res.
Uniplate on =>
on -> (on -> res -> res) -> res -> res
f on
x)
where
f :: Uniplate on => on -> (on -> res -> res) -> res -> res
f :: on -> (on -> res -> res) -> res -> res
f on
x on -> res -> res
cons res
nil = on
x on -> res -> res
`cons` [res -> res] -> res -> res
forall a. [a -> a] -> a -> a
concatCont ((on -> res -> res) -> [on] -> [res -> res]
forall a b. (a -> b) -> [a] -> [b]
map (\on
x -> on -> (on -> res -> res) -> res -> res
forall on res.
Uniplate on =>
on -> (on -> res -> res) -> res -> res
f on
x on -> res -> res
cons) ([on] -> [res -> res]) -> [on] -> [res -> res]
forall a b. (a -> b) -> a -> b
$ on -> [on]
forall on. Uniplate on => on -> [on]
children on
x) res
nil
children :: Uniplate on => on -> [on]
children :: on -> [on]
children = ([on], [on] -> on) -> [on]
forall a b. (a, b) -> a
fst (([on], [on] -> on) -> [on])
-> (on -> ([on], [on] -> on)) -> on -> [on]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. on -> ([on], [on] -> on)
forall on. Uniplate on => UniplateType on
uniplate
transform :: Uniplate on => (on -> on) -> on -> on
transform :: (on -> on) -> on -> on
transform on -> on
f on
x = on -> on
f (on -> on) -> on -> on
forall a b. (a -> b) -> a -> b
$ [on] -> on
generate ([on] -> on) -> [on] -> on
forall a b. (a -> b) -> a -> b
$ (on -> on) -> [on] -> [on]
forall a b. (a -> b) -> [a] -> [b]
map ((on -> on) -> on -> on
forall on. Uniplate on => (on -> on) -> on -> on
transform on -> on
f) [on]
current
where ([on]
current, [on] -> on
generate) = UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x
transformM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on
transformM :: (on -> m on) -> on -> m on
transformM on -> m on
f on
x = (on -> m on) -> [on] -> m [on]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM ((on -> m on) -> on -> m on
forall (m :: * -> *) on.
(Monad m, Uniplate on) =>
(on -> m on) -> on -> m on
transformM on -> m on
f) [on]
current m [on] -> ([on] -> m on) -> m on
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= on -> m on
f (on -> m on) -> ([on] -> on) -> [on] -> m on
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [on] -> on
generate
where ([on]
current, [on] -> on
generate) = UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x
rewrite :: Uniplate on => (on -> Maybe on) -> on -> on
rewrite :: (on -> Maybe on) -> on -> on
rewrite on -> Maybe on
f = (on -> on) -> on -> on
forall on. Uniplate on => (on -> on) -> on -> on
transform on -> on
g
where g :: on -> on
g on
x = on -> (on -> on) -> Maybe on -> on
forall b a. b -> (a -> b) -> Maybe a -> b
maybe on
x ((on -> Maybe on) -> on -> on
forall on. Uniplate on => (on -> Maybe on) -> on -> on
rewrite on -> Maybe on
f) (on -> Maybe on
f on
x)
rewriteM :: (Monad m, Uniplate on) => (on -> m (Maybe on)) -> on -> m on
rewriteM :: (on -> m (Maybe on)) -> on -> m on
rewriteM on -> m (Maybe on)
f = (on -> m on) -> on -> m on
forall (m :: * -> *) on.
(Monad m, Uniplate on) =>
(on -> m on) -> on -> m on
transformM on -> m on
g
where g :: on -> m on
g on
x = on -> m (Maybe on)
f on
x m (Maybe on) -> (Maybe on -> m on) -> m on
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= m on -> (on -> m on) -> Maybe on -> m on
forall b a. b -> (a -> b) -> Maybe a -> b
maybe (on -> m on
forall (m :: * -> *) a. Monad m => a -> m a
return on
x) ((on -> m (Maybe on)) -> on -> m on
forall (m :: * -> *) on.
(Monad m, Uniplate on) =>
(on -> m (Maybe on)) -> on -> m on
rewriteM on -> m (Maybe on)
f)
descend :: Uniplate on => (on -> on) -> on -> on
descend :: (on -> on) -> on -> on
descend on -> on
f on
x = [on] -> on
generate ([on] -> on) -> [on] -> on
forall a b. (a -> b) -> a -> b
$ (on -> on) -> [on] -> [on]
forall a b. (a -> b) -> [a] -> [b]
map on -> on
f [on]
current
where ([on]
current, [on] -> on
generate) = UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x
descendM :: (Monad m, Uniplate on) => (on -> m on) -> on -> m on
descendM :: (on -> m on) -> on -> m on
descendM on -> m on
f on
x = ([on] -> on) -> m [on] -> m on
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [on] -> on
generate (m [on] -> m on) -> m [on] -> m on
forall a b. (a -> b) -> a -> b
$ (on -> m on) -> [on] -> m [on]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM on -> m on
f [on]
current
where ([on]
current, [on] -> on
generate) = UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x
contexts :: Uniplate on => on -> [(on, on -> on)]
contexts :: on -> [(on, on -> on)]
contexts on
x = (on
x,on -> on
forall a. a -> a
id) (on, on -> on) -> [(on, on -> on)] -> [(on, on -> on)]
forall a. a -> [a] -> [a]
: [(on, on -> on)] -> [(on, on -> on)]
forall b c. Uniplate b => [(b, b -> c)] -> [(b, b -> c)]
f (on -> [(on, on -> on)]
forall on. Uniplate on => on -> [(on, on -> on)]
holes on
x)
where
f :: [(b, b -> c)] -> [(b, b -> c)]
f [(b, b -> c)]
xs = [ (b
y, b -> c
ctx (b -> c) -> (b -> b) -> b -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. b -> b
context)
| (b
child, b -> c
ctx) <- [(b, b -> c)]
xs
, (b
y, b -> b
context) <- b -> [(b, b -> b)]
forall on. Uniplate on => on -> [(on, on -> on)]
contexts b
child]
holes :: Uniplate on => on -> [(on, on -> on)]
holes :: on -> [(on, on -> on)]
holes on
x = ([on] -> ([on] -> on) -> [(on, on -> on)])
-> ([on], [on] -> on) -> [(on, on -> on)]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [on] -> ([on] -> on) -> [(on, on -> on)]
forall a c. [a] -> ([a] -> c) -> [(a, a -> c)]
f (UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x)
where f :: [a] -> ([a] -> c) -> [(a, a -> c)]
f [] [a] -> c
_ = []
f (a
x:[a]
xs) [a] -> c
gen = (a
x, [a] -> c
gen ([a] -> c) -> (a -> [a]) -> a -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)) (a, a -> c) -> [(a, a -> c)] -> [(a, a -> c)]
forall a. a -> [a] -> [a]
:
[a] -> ([a] -> c) -> [(a, a -> c)]
f [a]
xs ([a] -> c
gen ([a] -> c) -> ([a] -> [a]) -> [a] -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:))
para :: Uniplate on => (on -> [r] -> r) -> on -> r
para :: (on -> [r] -> r) -> on -> r
para on -> [r] -> r
op on
x = on -> [r] -> r
op on
x ([r] -> r) -> [r] -> r
forall a b. (a -> b) -> a -> b
$ (on -> r) -> [on] -> [r]
forall a b. (a -> b) -> [a] -> [b]
map ((on -> [r] -> r) -> on -> r
forall on r. Uniplate on => (on -> [r] -> r) -> on -> r
para on -> [r] -> r
op) ([on] -> [r]) -> [on] -> [r]
forall a b. (a -> b) -> a -> b
$ on -> [on]
forall on. Uniplate on => on -> [on]
children on
x