File: SYB.hs

package info (click to toggle)
haskell-uniplate 1.6.13-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 224 kB
  • sloc: haskell: 1,233; makefile: 2
file content (84 lines) | stat: -rw-r--r-- 2,082 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
{-|
    SYB compatibility layer. This module serves as a drop-in
    replacement in some situations for some of the SYB operations.
    Users should also import "Data.Generics.Uniplate.Data".

    SYB is described in the paper: \"Scrap your boilerplate: a practical design
    pattern for generic programming\" by Ralf Lammel and Simon
    Peyton Jones.

    * <http://www.cs.vu.nl/boilerplate/>

    * <http://doi.acm.org/10.1145/604174.604179>

    * <http://www.cs.vu.nl/boilerplate/tldi03.pdf>
-}

module Data.Generics.SYB where

import Data.Generics.Uniplate.Operations


-- | @gmapT == 'descend'@
gmapT :: Uniplate a => (a -> a) -> a -> a
gmapT = descend


-- | Use 'children' and 'foldl'
gmapQl :: Uniplate a => (r -> r' -> r) -> r -> (a -> r') -> a -> r
gmapQl combine zero op = foldl combine zero . map op . children


-- | Use 'children' and 'foldr'
gmapQr :: Uniplate a => (r' -> r -> r) -> r -> (a -> r') -> a -> r
gmapQr combine zero op = foldr combine zero . map op . children


-- | Use 'children'
gmapQ :: Uniplate a => (a -> u) -> a -> [u]
gmapQ f = map f . children


-- | Use 'children' and '!!'
gmapQi :: Uniplate a => Int -> (a -> u) -> a -> u
gmapQi i f x = gmapQ f x !! i


-- | @gmapM == 'descendM'@
gmapM :: (Uniplate a, Applicative m) => (a -> m a) -> a -> m a
gmapM = descendM



-- | @mkT == 'id'@
mkT :: (a -> a) -> (a -> a)
mkT = id


-- | @everywhere == 'transformBi'@
everywhere :: Biplate b a => (a -> a) -> b -> b
everywhere = transformBi


-- | @mkM == id@
mkM :: (a -> m a) -> a -> m a
mkM = id


-- | @everywhereM == 'transformBiM'@
everywhereM :: (Biplate b a, Monad m, Applicative m) => (a -> m a) -> b -> m b
everywhereM = transformBiM



-- | Only for use with 'everything'
mkQ :: r -> (a -> r) -> (r, a -> r)
mkQ = (,)


-- | Use 'universe' or 'universeBi', perhaps followed by a fold.
--
--   Not an exact equivalent to the SYB @everything@, as the
--   operators may be applied in different orders.
everything :: Biplate b a => (r -> r -> r) -> (r, a -> r) -> b -> r
everything combine (nil, op) = foldl combine nil . map op . universeBi