module Data.Generics.UniplateStr
(
module Data.Generics.UniplateStr,
module Data.Generics.Str
) where
import Control.Monad hiding (mapM)
import Data.Traversable
import Prelude hiding (mapM)
import Data.Generics.Uniplate.Internal.Utils
import Data.Generics.Str
type UniplateType on = on -> (Str on, Str on -> on)
class Uniplate on where
uniplate :: UniplateType on
uniplateList :: Uniplate on => on -> ([on], [on] -> on)
uniplateList :: on -> ([on], [on] -> on)
uniplateList on
x = ([on]
c, Str on -> on
b (Str on -> on) -> ([on] -> Str on) -> [on] -> on
forall b c a. (b -> c) -> (a -> b) -> a -> c
. [on] -> Str on
d)
where
(Str on
a,Str on -> on
b) = UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x
([on]
c,[on] -> Str on
d) = Str on -> ([on], [on] -> Str on)
forall a. Str a -> ([a], [a] -> Str a)
strStructure Str on
a
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 forall b. (on -> b -> b) -> b -> b
f
where
f :: (on -> t -> t) -> t -> t
f on -> t -> t
cons t
nil = (on -> t -> t) -> t -> Str on -> t -> t
forall on t t.
Uniplate on =>
(on -> t -> t) -> t -> Str on -> t -> t
g on -> t -> t
cons t
nil (on -> Str on
forall a. a -> Str a
One on
x) t
nil
g :: (on -> t -> t) -> t -> Str on -> t -> t
g on -> t -> t
cons t
nil Str on
Zero t
res = t
res
g on -> t -> t
cons t
nil (One on
x) t
res = on
x on -> t -> t
`cons` (on -> t -> t) -> t -> Str on -> t -> t
g on -> t -> t
cons t
nil ((Str on, Str on -> on) -> Str on
forall a b. (a, b) -> a
fst ((Str on, Str on -> on) -> Str on)
-> (Str on, Str on -> on) -> Str on
forall a b. (a -> b) -> a -> b
$ UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x) t
res
g on -> t -> t
cons t
nil (Two Str on
x Str on
y) t
res = (on -> t -> t) -> t -> Str on -> t -> t
g on -> t -> t
cons t
nil Str on
x ((on -> t -> t) -> t -> Str on -> t -> t
g on -> t -> t
cons t
nil Str on
y t
res)
children :: Uniplate on => on -> [on]
children :: on -> [on]
children on
x = (forall b. (on -> b -> b) -> b -> b) -> [on]
forall a. (forall b. (a -> b -> b) -> b -> b) -> [a]
builder forall b. (on -> b -> b) -> b -> b
f
where
f :: (on -> t -> t) -> t -> t
f on -> t -> t
cons t
nil = (on -> t -> t) -> t -> Str on -> t -> t
forall t t t. (t -> t -> t) -> t -> Str t -> t -> t
g on -> t -> t
cons t
nil ((Str on, Str on -> on) -> Str on
forall a b. (a, b) -> a
fst ((Str on, Str on -> on) -> Str on)
-> (Str on, Str on -> on) -> Str on
forall a b. (a -> b) -> a -> b
$ UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x) t
nil
g :: (t -> t -> t) -> t -> Str t -> t -> t
g t -> t -> t
cons t
nil Str t
Zero t
res = t
res
g t -> t -> t
cons t
nil (One t
x) t
res = t
x t -> t -> t
`cons` t
res
g t -> t -> t
cons t
nil (Two Str t
x Str t
y) t
res = (t -> t -> t) -> t -> Str t -> t -> t
g t -> t -> t
cons t
nil Str t
x ((t -> t -> t) -> t -> Str t -> t -> t
g t -> t -> t
cons t
nil Str t
y t
res)
transform :: Uniplate on => (on -> on) -> on -> on
transform :: (on -> on) -> on -> on
transform on -> on
f = on -> on
f (on -> on) -> (on -> on) -> on -> on
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (on -> on) -> on -> on
forall on. Uniplate on => (on -> on) -> on -> on
descend ((on -> on) -> on -> on
forall on. Uniplate on => (on -> on) -> on -> on
transform on -> on
f)
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
f (on -> m on) -> m on -> m on
forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< (on -> m on) -> on -> m on
forall (m :: * -> *) on.
(Monad m, Uniplate on) =>
(on -> m on) -> on -> m on
descendM ((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
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 = Str on -> on
generate (Str on -> on) -> Str on -> on
forall a b. (a -> b) -> a -> b
$ (on -> on) -> Str on -> Str on
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap on -> on
f Str on
current
where (Str on
current, Str 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 = (Str on -> on) -> m (Str on) -> m on
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM Str on -> on
generate (m (Str on) -> m on) -> m (Str on) -> m on
forall a b. (a -> b) -> a -> b
$ (on -> m on) -> Str on -> m (Str on)
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM on -> m on
f Str on
current
where (Str on
current, Str 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 = (Str on -> (Str on -> on) -> [(on, on -> on)])
-> (Str on, Str on -> on) -> [(on, on -> on)]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry Str on -> (Str on -> on) -> [(on, on -> on)]
forall a c. Str a -> (Str a -> c) -> [(a, a -> c)]
f (UniplateType on
forall on. Uniplate on => UniplateType on
uniplate on
x)
where f :: Str a -> (Str a -> c) -> [(a, a -> c)]
f Str a
Zero Str a -> c
_ = []
f (One a
i) Str a -> c
generate = [(a
i, Str a -> c
generate (Str a -> c) -> (a -> Str a) -> a -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> Str a
forall a. a -> Str a
One)]
f (Two Str a
l Str a
r) Str a -> c
gen = Str a -> (Str a -> c) -> [(a, a -> c)]
f Str a
l (Str a -> c
gen (Str a -> c) -> (Str a -> Str a) -> Str a -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\Str a
i -> Str a -> Str a -> Str a
forall a. Str a -> Str a -> Str a
Two Str a
i Str a
r))
[(a, a -> c)] -> [(a, a -> c)] -> [(a, a -> c)]
forall a. [a] -> [a] -> [a]
++ Str a -> (Str a -> c) -> [(a, a -> c)]
f Str a
r (Str a -> c
gen (Str a -> c) -> (Str a -> Str a) -> Str a -> c
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (\Str a
i -> Str a -> Str a -> Str a
forall a. Str a -> Str a -> Str a
Two Str a
l Str a
i))
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