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
21import Data.Maybe (fromMaybe)
24-- Apply a top-down monadic transformation everywhere
26everywhereM' :: (Monad m, Data d) => (forall d1. (Data d1) => d1 -> m d1) -> d -> m d
29 gmapM (everywhereM' f) y
32-- Apply a top-down transformation, mutating a state when descending from parents to children
34-- For example, if we want to relabel bound variables with a different data constructor, we can do so:
36-- > data Expr = Var String
39-- > | LocalVar String deriving (Show, Data, Typeable)
41-- > test = App (Lam "a" (App (Var "a") (Var "b"))) (Var "a")
43-- > varsToLocals :: Expr -> Expr
44-- > varsToLocals = everywhereWithContext' [] (mkS go)
46-- > go locals (Var v) | v `elem` locals = (locals, LocalVar v)
47-- > go locals lam@(Lam local _) = (local : locals, lam)
48-- > go locals other = (locals, other)
50everywhereWithContext' :: (Data d) => s -> (forall d1. (Data d1) => s -> d1 -> (s, d1)) -> d -> d
51everywhereWithContext' s0 f x =
52 let (s, y) = f s0 x in
53 gmapT (everywhereWithContext' s f) y
56-- Make a stateful transformation function
58mkS :: (Data a, Data b) => (s -> a -> (s, a)) -> s -> b -> (s, b)
59mkS f s b = fromMaybe (s, b) $ do
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