{-# LANGUAGE MultiParamTypeClasses #-}

{- |
    /DEPRECATED/: Use "Data.Generics.Uniplate.Direct" instead.

    This module supplies a method for writing 'Biplate' instances more easily.
    This module requires fewest extensions, highest performance, and most instance
    definitions.

    To take an example:

    > data Expr = Var Int | Pos Expr String | Neg Expr | Add Expr Expr
    > data Stmt = Seq [Stmt] | Sel [Expr] | Let String Expr
    >
    > instance Uniplate Expr where
    >     uniplate (Var x  ) = plate Var |- x
    >     uniplate (Pos x y) = plate Pos |* x |- y
    >     uniplate (Neg x  ) = plate Neg |* x
    >     uniplate (Add x y) = plate Add |* x |* y
    >
    > instance Biplate Expr Expr where
    >     biplate = plateSelf
    >
    > instance Uniplate Stmt where
    >     uniplate (Seq x  ) = plate Seq ||* x
    >     uniplate (Sel x  ) = plate Sel ||+ x
    >     uniplate (Let x y) = plate Let |-  x |- y
    >
    > instance Biplate Stmt Stmt where
    >     biplate = plateSelf
    >
    > instance Biplate Stmt Expr where
    >     biplate (Seq x  ) = plate Seq ||+ x
    >     biplate (Sel x  ) = plate Sel ||* x
    >     biplate (Let x y) = plate Let |-  x |* y
-}


module Data.Generics.PlateDirect
    {-# DEPRECATED "Use Data.Generics.Uniplate.Direct instead" #-}
    (
    module Data.Generics.Biplate,
    -- * The Combinators
    plate, plateSelf,
    (|+), (|-), (|*), (||+), (||*)
    ) where

import Data.Generics.Biplate


type Type from to = (Str to, Str to -> from)

-- | The main combinator used to start the chain.
--
-- The following rule can be used for optimisation:
--
-- > plate Ctor |- x == plate (Ctor x)
plate :: from -> Type from to
plate :: from -> Type from to
plate from
f = (Str to
forall a. Str a
Zero, \Str to
_ -> from
f)


-- | The field to the right is the target.
(|*) :: Type (to -> from) to -> to -> Type from to
|* :: Type (to -> from) to -> to -> Type from to
(|*) (Str to
xs,Str to -> to -> from
x_) to
y = (Str to -> Str to -> Str to
forall a. Str a -> Str a -> Str a
Two Str to
xs (to -> Str to
forall a. a -> Str a
One to
y),\(Two Str to
xs (One to
y)) -> Str to -> to -> from
x_ Str to
xs to
y)



-- | The field to the right may contain the target.
(|+) :: Biplate item to => Type (item -> from) to -> item -> Type from to
|+ :: Type (item -> from) to -> item -> Type from to
(|+) (Str to
xs,Str to -> item -> from
x_) item
y = case BiplateType item to
forall from to. Biplate from to => BiplateType from to
biplate item
y of
                      (Str to
ys,Str to -> item
y_) -> (Str to -> Str to -> Str to
forall a. Str a -> Str a -> Str a
Two Str to
xs Str to
ys, \(Two Str to
xs Str to
ys) -> Str to -> item -> from
x_ Str to
xs (Str to -> item
y_ Str to
ys))


-- | The field to the right /does not/ contain the target.
(|-) :: Type (item -> from) to -> item -> Type from to
|- :: Type (item -> from) to -> item -> Type from to
(|-) (Str to
xs,Str to -> item -> from
x_) item
y = (Str to
xs,\Str to
xs -> Str to -> item -> from
x_ Str to
xs item
y)


-- | The field to the right is a list of the type of the target
(||*) :: Type ([to] -> from) to -> [to] -> Type from to
||* :: Type ([to] -> from) to -> [to] -> Type from to
(||*) (Str to
xs,Str to -> [to] -> from
x_) [to]
y = (Str to -> Str to -> Str to
forall a. Str a -> Str a -> Str a
Two Str to
xs ([to] -> Str to
forall a. [a] -> Str a
listStr [to]
y), \(Two Str to
xs Str to
ys) -> Str to -> [to] -> from
x_ Str to
xs (Str to -> [to]
forall a. Str a -> [a]
strList Str to
ys))


-- | The field to the right is a list of types which may contain the target
(||+) :: Biplate item to => Type ([item] -> from) to -> [item] -> Type from to
||+ :: Type ([item] -> from) to -> [item] -> Type from to
(||+) (Str to
xs,Str to -> [item] -> from
x_) [item]
y = case [item] -> Type [item] to
forall a to. Biplate a to => [a] -> Type [a] to
plateListDiff [item]
y of
                       (Str to
ys,Str to -> [item]
y_) -> (Str to -> Str to -> Str to
forall a. Str a -> Str a -> Str a
Two Str to
xs Str to
ys, \(Two Str to
xs Str to
ys) -> Str to -> [item] -> from
x_ Str to
xs (Str to -> [item]
y_ Str to
ys))
    where
        plateListDiff :: [a] -> Type [a] to
plateListDiff [] = [a] -> Type [a] to
forall from to. from -> Type from to
plate []
        plateListDiff (a
x:[a]
xs) = (a -> [a] -> [a]) -> Type (a -> [a] -> [a]) to
forall from to. from -> Type from to
plate (:) Type (a -> [a] -> [a]) to -> a -> Type ([a] -> [a]) to
forall item to from.
Biplate item to =>
Type (item -> from) to -> item -> Type from to
|+ a
x Type ([a] -> [a]) to -> [a] -> Type [a] to
forall item to from.
Biplate item to =>
Type ([item] -> from) to -> [item] -> Type from to
||+ [a]
xs


-- | Used for 'PlayAll' definitions where both types are the same.
plateSelf :: to -> Type to to
plateSelf :: to -> Type to to
plateSelf to
x = (to -> Str to
forall a. a -> Str a
One to
x, \(One to
x) -> to
x)