{-|
    Compos compatibility layer. This module serves as a drop-in
    replacement in some situations for some of the Compos operations.
    Only the single-type traversals are supported, on normal
    algebraic data types. Users should also import either "Data.Generics.Uniplate.Data"
    or "Data.Generics.Uniplate.Direct".

    Compos is described in the paper: \"A Pattern for Almost Compositional Functions\"
    by Bjorn Bringert and Aarne Ranta.

    * <http://doi.acm.org/10.1145/1159803.1159834>

    * <http://www.cs.chalmers.se/~bringert/publ/composOp/composOp.pdf>
-}

module Data.Generics.Compos where

import Control.Monad
import Data.Generics.Uniplate.Operations


-- | If you want to keep an existing type class
class Uniplate a => Compos a where


-- | @composOp == 'descend'@
composOp :: Uniplate a => (a -> a) -> a -> a
composOp :: (a -> a) -> a -> a
composOp = (a -> a) -> a -> a
forall on. Uniplate on => (on -> on) -> on -> on
descend


-- | @composOpM == 'descendM'@
composOpM :: (Uniplate a, Applicative m) => (a -> m a) -> a -> m a
composOpM :: (a -> m a) -> a -> m a
composOpM = (a -> m a) -> a -> m a
forall on (m :: * -> *).
(Uniplate on, Applicative m) =>
(on -> m on) -> on -> m on
descendM


-- | @composOpM_ == 'composOpFold' (return ()) (>>)@
composOpM_ :: (Uniplate a, Monad m) => (a -> m ()) -> a -> m ()
composOpM_ :: (a -> m ()) -> a -> m ()
composOpM_ = m () -> (m () -> m () -> m ()) -> (a -> m ()) -> a -> m ()
forall a b. Uniplate a => b -> (b -> b -> b) -> (a -> b) -> a -> b
composOpFold (() -> m ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()) m () -> m () -> m ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
(>>)


-- | @composOpMonoid = 'composOpFold' mempty mappend@
composOpMonoid :: (Uniplate a, Monoid m) => (a -> m) -> a -> m
composOpMonoid :: (a -> m) -> a -> m
composOpMonoid = m -> (m -> m -> m) -> (a -> m) -> a -> m
forall a b. Uniplate a => b -> (b -> b -> b) -> (a -> b) -> a -> b
composOpFold m
forall a. Monoid a => a
mempty m -> m -> m
forall a. Monoid a => a -> a -> a
mappend


-- | @composOpMPlus = 'composOpFold' mzero mplus@
composOpMPlus :: (Uniplate a, MonadPlus m) => (a -> m b) -> a -> m b
composOpMPlus :: (a -> m b) -> a -> m b
composOpMPlus = m b -> (m b -> m b -> m b) -> (a -> m b) -> a -> m b
forall a b. Uniplate a => b -> (b -> b -> b) -> (a -> b) -> a -> b
composOpFold m b
forall (m :: * -> *) a. MonadPlus m => m a
mzero m b -> m b -> m b
forall (m :: * -> *) a. MonadPlus m => m a -> m a -> m a
mplus


-- | Probably replace with 'universe', perhaps 'para'
composOpFold :: Uniplate a => b -> (b -> b -> b) -> (a -> b) -> a -> b
composOpFold :: b -> (b -> b -> b) -> (a -> b) -> a -> b
composOpFold b
zero b -> b -> b
combine a -> b
f = (b -> b -> b) -> b -> [b] -> b
forall (t :: * -> *) a b.
Foldable t =>
(a -> b -> b) -> b -> t a -> b
foldr b -> b -> b
combine b
zero ([b] -> b) -> (a -> [b]) -> a -> b
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (a -> b) -> [a] -> [b]
forall a b. (a -> b) -> [a] -> [b]
map a -> b
f ([a] -> [b]) -> (a -> [a]) -> a -> [b]
forall b c a. (b -> c) -> (a -> b) -> a -> c
. a -> [a]
forall on. Uniplate on => on -> [on]
children