1-----------------------------------------------------------------------------
3-- Module : Data.Generics.Extras
4-- Copyright : (c) Phil Freeman 2013
7-- Maintainer : Phil Freeman <paf31@cantab.net>
8-- Stability : experimental
12-- Additional SYB combinators
14-----------------------------------------------------------------------------
16{-# LANGUAGE Rank2Types #-}
18module Data.Generics.Extras where
23-- Apply a top-down monadic transformation everywhere
25everywhereM' :: (Monad m, Data d) => (forall d1. (Data d1) => d1 -> m d1) -> d -> m d
28 gmapM (everywhereM' f) y
31-- Apply a top-down transformation, mutating a state when descending from parents to children
33-- For example, if we want to relabel bound variables with a different data constructor, we can do so:
35-- > data Expr = Var String
38-- > | LocalVar String deriving (Show, Data, Typeable)
40-- > test = App (Lam "a" (App (Var "a") (Var "b"))) (Var "a")
42-- > varsToLocals :: Expr -> Expr
43-- > varsToLocals = everywhereWithContext' [] (mkS go)
45-- > go locals (Var v) | v `elem` locals = (locals, LocalVar v)
46-- > go locals lam@(Lam local _) = (local : locals, lam)
47-- > go locals other = (locals, other)
49everywhereWithContextM' :: (Monad m, Data d) => s -> (forall d1. (Data d1) => s -> d1 -> m (s, d1)) -> d -> m d
50everywhereWithContextM' s0 f x = do
52 gmapM (everywhereWithContextM' s f) y
55-- Make a stateful transformation function
57mkS :: (Monad m, Data a, Data b) => (s -> a -> m (s, a)) -> s -> b -> m (s, b)
58mkS = extS (curry return)
61-- Extend a stateful transformation function
63extS :: (Monad m, Data a, Data b) => (s -> a -> m (s, a)) -> (s -> b -> m (s, b)) -> (s -> a -> m (s, a))
70 Just a'' -> return (s'', a'')
71 Nothing -> return (s', a')
72 Nothing -> return (s', a')
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list