A Plated type is one where we know how to extract its immediate self-similar children.
Example 1:
import Control.Applicative
import Control.Lens
import Control.Lens.Plated
import Data.Data
import Data.Data.Lens (uniplate)
data Expr
= Val Int
| Neg Expr
| Add Expr Expr
deriving (Eq,Ord,Show,Read,Data)
instance Plated Expr where
plate f (Neg e) = Neg <$> f e
plate f (Add a b) = Add <$> f a <*> f b
plate _ a = pure a
or
instance Plated Expr where
plate = uniplate
Example 2:
import Control.Applicative
import Control.Lens
import Control.Lens.Plated
import Data.Data
import Data.Data.Lens (uniplate)
data Tree a
= Bin (Tree a) (Tree a)
| Tip a
deriving (Eq,Ord,Show,Read,Data)
instance Plated (Tree a) where
plate f (Bin l r) = Bin <$> f l <*> f r
plate _ t = pure t
or
instance Data a => Plated (Tree a) where
plate = uniplate
Note the big distinction between these two implementations.
The former will only treat children directly in this tree as descendents, the latter will treat trees contained in the values under the tips also as descendants!
When in doubt, pick a Traversal and just use the various ...Of combinators
rather than pollute Plated with orphan instances!
If you want to find something unplated and non-recursive with biplate
use the ...OnOf variant with ignored, though those usecases are much better served
in most cases by using the existing Lens combinators! e.g.
toListOf biplate ≡ universeOnOf biplate ignored
This same ability to explicitly pass the Traversal in question is why there is no
analogue to uniplate's Biplate.
Moreover, since we can allow custom traversals, we implement reasonable defaults for
polymorphic data types, that only Control.Traversable.traverse into themselves, and not their
polymorphic arguments.
Instances13Plated, …
Plated ConDefined in lens-5.3.5 · Control.Lens.PlatedPlated DecDefined in lens-5.3.5 · Control.Lens.PlatedPlated ExpDefined in lens-5.3.5 · Control.Lens.PlatedPlated PatDefined in lens-5.3.5 · Control.Lens.PlatedPlated StmtDefined in lens-5.3.5 · Control.Lens.PlatedPlated TypeDefined in lens-5.3.5 · Control.Lens.PlatedPlated (Tree a)Defined in lens-5.3.5 · Control.Lens.PlatedPlated [a]Defined in lens-5.3.5 · Control.Lens.PlatedTraversable f => Plated (Cofree f a)Defined in lens-5.3.5 · Control.Lens.PlatedTraversable f => Plated (Free f a)Defined in lens-5.3.5 · Control.Lens.PlatedTraversable f => Plated (F f a)Defined in lens-5.3.5 · Control.Lens.Plated(Traversable f, Traversable m) => Plated (FreeT f m a)Defined in lens-5.3.5 · Control.Lens.Plated(Traversable f, Traversable w) => Plated (CofreeT f w a)Defined in lens-5.3.5 · Control.Lens.Plated