Copyright | (c) 2014 Jonas Scholl |
---|---|

License | BSD3 |

Maintainer | jonas.scholl@gmx.de |

Stability | experimental |

Portability | non-portable |

Safe Haskell | Trustworthy |

Language | Haskell2010 |

This module provides a simple way to transform parts of complex data structures.

- data EndoList
- data EndoItem
- data EndoListM m
- data EndoMItem m
- class Transformation d
- class Monad m => MonadicTransformation d m | d -> m
- mkItem :: Data a => (a -> a) -> EndoItem
- mkItemM :: (Monad m, Data a) => (a -> m a) -> EndoMItem m
- transform :: (Transformation d, Data a) => d -> a -> a
- transformM :: (MonadicTransformation d m, Data a) => d -> a -> m a
- unsafeTransform :: (Transformation d, Data a) => d -> a -> a
- unsafeTransformM :: (MonadicTransformation d m, Data a) => d -> a -> m a
- getSubterms :: (Data a, Data b, Monoid m) => (b -> m) -> a -> m
- getSubterms' :: (Data a, Data b) => a -> [b]
- getSubtermsBy :: (Data a, Data b) => (b -> Bool) -> a -> [b]
- getSubtermsWith :: (Data a, Data b, Monoid m) => (b -> Maybe m) -> a -> m

# Types

Heterogeneous list structure holding endomorphisms.

Wrapper object holding some endomorphism without exposing its type.

Heterogeneous list structure holding endomorphisms in a monadic context.

Wrapper object holding some endomorphism in a monadic context without exposing its type.

Monad m => MonadicTransformation (EndoMItem m) m |

# Classes

class Transformation d Source

Class of transformations, i.e. objects containing endomorphisms.

mkEndoList

Transformation EndoList | |

Transformation EndoItem | |

Transformation a => Transformation [a] | |

Data a => Transformation (Endo a) | |

Data a => Transformation (a -> a) |

class Monad m => MonadicTransformation d m | d -> m Source

Monadic version of `Transformation`

.

mkEndoListM

MonadicTransformation a m => MonadicTransformation [a] m | |

Monad m => MonadicTransformation (EndoListM m) m | |

Monad m => MonadicTransformation (EndoMItem m) m | |

(Monad m, Data a) => MonadicTransformation (a -> m a) m |

# Wrapper functions

mkItemM :: (Monad m, Data a) => (a -> m a) -> EndoMItem m Source

Wrap a monadic function as an `EndoMItem`

.

# Transformation functions

transform :: (Transformation d, Data a) => d -> a -> a Source

Transform some data structure by applying one or more endomorphisms to the data structure or any sub-term of it. Sub-terms are transformed before the terms containing them are transformed. If the given endomorphisms contain two or more endomorphisms working on the same type the latter endomorphisms will be applied to the result of the former endomorphisms

NOTE: This function attempts to check at runtime if all given endomorphisms
can be applied to at least one term in the given argument. If at least one
endomorphism can never be applied because of its type, `error`

is called.
If you don't want this behavior consider using `unsafeTransform`

instead.

Example:

`>>>`

(2, 4.0, (False, [5, 6, 7]))`transform (+1) (1, 4.0, (False, [4, 5, 6]))`

`>>>`

(4, 2.0, (False, [10, 12, 14]))`transform [mkItem (+1), mkItem (sqrt :: Double -> Double), mkItem (*2)] (1, 4.0, (False, [4, 5, 6]))`

`>>>`

*** Exception: Data.DataTraverse.transform: Could not find all needed types when mapping over a value of type Bool. Types of missing terms: [Integer]`transform (+1) False`

transformM :: (MonadicTransformation d m, Data a) => d -> a -> m a Source

unsafeTransform :: (Transformation d, Data a) => d -> a -> a Source

unsafeTransformM :: (MonadicTransformation d m, Data a) => d -> a -> m a Source

Same as `transformM`

but omits any type checking (and hence does not call `fail`

).

# Searching functions

getSubterms :: (Data a, Data b, Monoid m) => (b -> m) -> a -> m Source

Returns all sub-terms (intermediate and non intermediate) of some type of a
value transformed by the supplied function to some `Monoid`

.

NOTE: Calls `error`

if no sub-term which the needed type can exist.

Example:

`>>>`

[True, True]`getSubterms (\ x -> if x then [x] else []) (3, 4.0, True, 'c', (False, (True, 5, 6)))`

getSubterms' :: (Data a, Data b) => a -> [b] Source

Returns all sub-terms (intermediate and non intermediate) of some type of a value as a list.

NOTE: Calls `error`

if no sub-term which the needed type can exist.

Example:

`>>>`

[3, 5, 6]`getSubterms' (3, 4.0, True, 'c', (False, (True, 5, 6))) :: [Integer]`

getSubtermsBy :: (Data a, Data b) => (b -> Bool) -> a -> [b] Source

Returns all sub-terms (intermediate and non intermediate) of some type of a value which fulfill the predicate.

NOTE: Calls `error`

if no sub-term which the needed type can exist.

Example:

`>>>`

[3, 5]`getSubtermsBy (<6) (3, 4.0, True, 'c', (False, (True, 5, 6)))`

getSubtermsWith :: (Data a, Data b, Monoid m) => (b -> Maybe m) -> a -> m Source

Returns all sub-terms (intermediate and non intermediate) of some type of a
value which could be transformed to some `Monoid`

.

NOTE: Calls `error`

if no sub-term which the needed type can exist.

Example:

`>>>`

[3, 5]`getSubtermsWith (\ x -> guard (x < 6) >> return [x]) (3, 4.0, True, 'c', (False, (True, 5, 6)))`