{-# OPTIONS_GHC -fno-warn-deprecations #-}
module Data.Generics.UniplateOn
(
module Data.Generics.Uniplate,
module Data.Generics.UniplateOn
) where
import Data.Generics.Uniplate
import Control.Monad(liftM)
type BiplateType from to = from -> ([to], [to] -> from)
universeOn :: Uniplate to => BiplateType from to -> from -> [to]
universeOn :: forall to from. Uniplate to => BiplateType from to -> from -> [to]
universeOn BiplateType from to
biplate from
x = (to -> [to]) -> [to] -> [to]
forall (t :: * -> *) a b. Foldable t => (a -> [b]) -> t a -> [b]
concatMap to -> [to]
forall on. Uniplate on => on -> [on]
universe ([to] -> [to]) -> [to] -> [to]
forall a b. (a -> b) -> a -> b
$ ([to], [to] -> from) -> [to]
forall a b. (a, b) -> a
fst (([to], [to] -> from) -> [to]) -> ([to], [to] -> from) -> [to]
forall a b. (a -> b) -> a -> b
$ BiplateType from to
biplate from
x
childrenOn :: Uniplate to => BiplateType from to -> from -> [to]
childrenOn :: forall to from. Uniplate to => BiplateType from to -> from -> [to]
childrenOn BiplateType from to
biplate from
x = ([to], [to] -> from) -> [to]
forall a b. (a, b) -> a
fst (([to], [to] -> from) -> [to]) -> ([to], [to] -> from) -> [to]
forall a b. (a -> b) -> a -> b
$ BiplateType from to
biplate from
x
transformOn :: Uniplate to => BiplateType from to -> (to -> to) -> from -> from
transformOn :: forall to from.
Uniplate to =>
BiplateType from to -> (to -> to) -> from -> from
transformOn BiplateType from to
biplate to -> to
f from
x = [to] -> from
generate ([to] -> from) -> [to] -> from
forall a b. (a -> b) -> a -> b
$ (to -> to) -> [to] -> [to]
forall a b. (a -> b) -> [a] -> [b]
map ((to -> to) -> to -> to
forall on. Uniplate on => (on -> on) -> on -> on
transform to -> to
f) [to]
current
where ([to]
current, [to] -> from
generate) = BiplateType from to
biplate from
x
transformOnM :: (Monad m, Uniplate to) => BiplateType from to -> (to -> m to) -> from -> m from
transformOnM :: forall (m :: * -> *) to from.
(Monad m, Uniplate to) =>
BiplateType from to -> (to -> m to) -> from -> m from
transformOnM BiplateType from to
biplate to -> m to
f from
x = ([to] -> from) -> m [to] -> m from
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [to] -> from
generate (m [to] -> m from) -> m [to] -> m from
forall a b. (a -> b) -> a -> b
$ (to -> m to) -> [to] -> m [to]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((to -> m to) -> to -> m to
forall (m :: * -> *) on.
(Monad m, Uniplate on) =>
(on -> m on) -> on -> m on
transformM to -> m to
f) [to]
current
where ([to]
current, [to] -> from
generate) = BiplateType from to
biplate from
x
rewriteOn :: Uniplate to => BiplateType from to -> (to -> Maybe to) -> from -> from
rewriteOn :: forall to from.
Uniplate to =>
BiplateType from to -> (to -> Maybe to) -> from -> from
rewriteOn BiplateType from to
biplate to -> Maybe to
f from
x = [to] -> from
generate ([to] -> from) -> [to] -> from
forall a b. (a -> b) -> a -> b
$ (to -> to) -> [to] -> [to]
forall a b. (a -> b) -> [a] -> [b]
map ((to -> Maybe to) -> to -> to
forall on. Uniplate on => (on -> Maybe on) -> on -> on
rewrite to -> Maybe to
f) [to]
current
where ([to]
current, [to] -> from
generate) = BiplateType from to
biplate from
x
rewriteOnM :: (Monad m, Uniplate to) => BiplateType from to -> (to -> m (Maybe to)) -> from -> m from
rewriteOnM :: forall (m :: * -> *) to from.
(Monad m, Uniplate to) =>
BiplateType from to -> (to -> m (Maybe to)) -> from -> m from
rewriteOnM BiplateType from to
biplate to -> m (Maybe to)
f from
x = ([to] -> from) -> m [to] -> m from
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [to] -> from
generate (m [to] -> m from) -> m [to] -> m from
forall a b. (a -> b) -> a -> b
$ (to -> m to) -> [to] -> m [to]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM ((to -> m (Maybe to)) -> to -> m to
forall (m :: * -> *) on.
(Monad m, Uniplate on) =>
(on -> m (Maybe on)) -> on -> m on
rewriteM to -> m (Maybe to)
f) [to]
current
where ([to]
current, [to] -> from
generate) = BiplateType from to
biplate from
x
descendOn :: Uniplate to => BiplateType from to -> (to -> to) -> from -> from
descendOn :: forall to from.
Uniplate to =>
BiplateType from to -> (to -> to) -> from -> from
descendOn BiplateType from to
biplate to -> to
f from
x = [to] -> from
generate ([to] -> from) -> [to] -> from
forall a b. (a -> b) -> a -> b
$ (to -> to) -> [to] -> [to]
forall a b. (a -> b) -> [a] -> [b]
map to -> to
f [to]
current
where ([to]
current, [to] -> from
generate) = BiplateType from to
biplate from
x
descendOnM :: (Monad m, Uniplate to) => BiplateType from to -> (to -> m to) -> from -> m from
descendOnM :: forall (m :: * -> *) to from.
(Monad m, Uniplate to) =>
BiplateType from to -> (to -> m to) -> from -> m from
descendOnM BiplateType from to
biplate to -> m to
f from
x = ([to] -> from) -> m [to] -> m from
forall (m :: * -> *) a1 r. Monad m => (a1 -> r) -> m a1 -> m r
liftM [to] -> from
generate (m [to] -> m from) -> m [to] -> m from
forall a b. (a -> b) -> a -> b
$ (to -> m to) -> [to] -> m [to]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
forall (m :: * -> *) a b. Monad m => (a -> m b) -> [a] -> m [b]
mapM to -> m to
f [to]
current
where ([to]
current, [to] -> from
generate) = BiplateType from to
biplate from
x
holesOn :: Uniplate to => BiplateType from to -> from -> [(to, to -> from)]
holesOn :: forall to from.
Uniplate to =>
BiplateType from to -> from -> [(to, to -> from)]
holesOn BiplateType from to
biplate from
x = ([to] -> ([to] -> from) -> [(to, to -> from)])
-> ([to], [to] -> from) -> [(to, to -> from)]
forall a b c. (a -> b -> c) -> (a, b) -> c
uncurry [to] -> ([to] -> from) -> [(to, to -> from)]
forall {a} {t}. [a] -> ([a] -> t) -> [(a, a -> t)]
f (BiplateType from to
biplate from
x)
where f :: [a] -> ([a] -> t) -> [(a, a -> t)]
f [] [a] -> t
_ = []
f (a
x:[a]
xs) [a] -> t
gen = (a
x, [a] -> t
gen ([a] -> t) -> (a -> [a]) -> a -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> [a] -> [a]
forall a. a -> [a] -> [a]
:[a]
xs)) (a, a -> t) -> [(a, a -> t)] -> [(a, a -> t)]
forall a. a -> [a] -> [a]
:
[a] -> ([a] -> t) -> [(a, a -> t)]
f [a]
xs ([a] -> t
gen ([a] -> t) -> ([a] -> [a]) -> [a] -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a
xa -> [a] -> [a]
forall a. a -> [a] -> [a]
:))
contextsOn :: Uniplate to => BiplateType from to -> from -> [(to, to -> from)]
contextsOn :: forall to from.
Uniplate to =>
BiplateType from to -> from -> [(to, to -> from)]
contextsOn BiplateType from to
biplate from
x = [(to, to -> from)] -> [(to, to -> from)]
forall {t} {t}. Uniplate t => [(t, t -> t)] -> [(t, t -> t)]
f (BiplateType from to -> from -> [(to, to -> from)]
forall to from.
Uniplate to =>
BiplateType from to -> from -> [(to, to -> from)]
holesOn BiplateType from to
biplate from
x)
where
f :: [(t, t -> t)] -> [(t, t -> t)]
f [(t, t -> t)]
xs = [ (t
y, t -> t
ctx (t -> t) -> (t -> t) -> t -> t
forall b c a. (b -> c) -> (a -> b) -> a -> c
. t -> t
context)
| (t
child, t -> t
ctx) <- [(t, t -> t)]
xs
, (t
y, t -> t
context) <- t -> [(t, t -> t)]
forall on. Uniplate on => on -> [(on, on -> on)]
contexts t
child]
uniplateOnList :: BiplateType a b -> BiplateType [a] b
uniplateOnList :: forall a b. BiplateType a b -> BiplateType [a] b
uniplateOnList BiplateType a b
f [] = ([], \[] -> [])
uniplateOnList BiplateType a b
f (a
x:[a]
xs) =
([b]
a [b] -> [b] -> [b]
forall a. [a] -> [a] -> [a]
++ [b]
as,
\[b]
ns -> let ([b]
n1,[b]
n2) = Int -> [b] -> ([b], [b])
forall a. Int -> [a] -> ([a], [a])
splitAt ([b] -> Int
forall a. [a] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [b]
a) [b]
ns in [b] -> a
b [b]
n1 a -> [a] -> [a]
forall a. a -> [a] -> [a]
: [b] -> [a]
bs [b]
n2)
where
([b]
a , [b] -> a
b ) = BiplateType a b
f a
x
([b]
as, [b] -> [a]
bs) = BiplateType a b -> BiplateType [a] b
forall a b. BiplateType a b -> BiplateType [a] b
uniplateOnList BiplateType a b
f [a]
xs