/
TH.hs
176 lines (132 loc) · 5.55 KB
/
TH.hs
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
{-# LANGUAGE CPP #-}
#ifdef TRUSTWORTHY
{-# LANGUAGE Trustworthy #-}
#endif
#ifdef HLINT
{-# ANN module "HLint: ignore Use camelCase" #-}
#endif
#ifndef MIN_VERSION_template_haskell
#define MIN_VERSION_template_haskell(x,y,z) (defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ >= 706)
#endif
#ifndef MIN_VERSION_containers
#define MIN_VERSION_containers(x,y,z) 1
#endif
-----------------------------------------------------------------------------
-- |
-- Module : Control.Lens.Internal.TH
-- Copyright : (C) 2013-2014 Edward Kmett, 2013 Eric Mertens
-- License : BSD-style (see the file LICENSE)
-- Maintainer : Edward Kmett <ekmett@gmail.com>
-- Stability : experimental
-- Portability : non-portable
--
----------------------------------------------------------------------------
module Control.Lens.Internal.TH where
import Language.Haskell.TH
import Language.Haskell.TH.Syntax
import qualified Data.Map as Map
import qualified Data.Set as Set
import Data.Version (showVersion)
import Paths_lens (version)
-- | Compatibility shim for recent changes to template haskell's 'tySynInstD'
tySynInstD' :: Name -> [TypeQ] -> TypeQ -> DecQ
#if MIN_VERSION_template_haskell(2,9,0)
tySynInstD' fam ts r = tySynInstD fam (tySynEqn ts r)
#else
tySynInstD' = tySynInstD
#endif
-- | Apply arguments to a type constructor
appsT :: TypeQ -> [TypeQ] -> TypeQ
appsT = foldl appT
-- | Apply arguments to a function
appsE1 :: ExpQ -> [ExpQ] -> ExpQ
appsE1 = foldl appE
-- | Construct a tuple type given a list of types.
toTupleT :: [TypeQ] -> TypeQ
toTupleT [x] = x
toTupleT xs = appsT (tupleT (length xs)) xs
-- | Construct a tuple value given a list of expressions.
toTupleE :: [ExpQ] -> ExpQ
toTupleE [x] = x
toTupleE xs = tupE xs
-- | Construct a tuple pattern given a list of patterns.
toTupleP :: [PatQ] -> PatQ
toTupleP [x] = x
toTupleP xs = tupP xs
-- | Apply arguments to a type constructor.
conAppsT :: Name -> [Type] -> Type
conAppsT conName = foldl AppT (ConT conName)
-- | Return 'Name' contained in a 'TyVarBndr'.
bndrName :: TyVarBndr -> Name
bndrName (PlainTV n ) = n
bndrName (KindedTV n _) = n
fromSet :: Ord k => (k -> v) -> Set.Set k -> Map.Map k v
#if MIN_VERSION_containers(0,5,0)
fromSet = Map.fromSet
#else
fromSet f x = Map.fromList [ (k,f k) | k <- Set.toList x ]
#endif
------------------------------------------------------------------------
-- Manually quoted names
------------------------------------------------------------------------
-- By manually generating these names we avoid needing to use the
-- TemplateHaskell language extension when compiling the lens library.
-- This allows the library to be used in stage1 cross-compilers.
mkLensName_tc :: String -> String -> Name
mkLensName_tc = mkNameG_tc ("lens-" ++ showVersion version)
mkLensName_v :: String -> String -> Name
mkLensName_v = mkNameG_v ("lens-" ++ showVersion version)
traversalTypeName :: Name
traversalTypeName = mkLensName_tc "Control.Lens.Type" "Traversal"
traversal'TypeName :: Name
traversal'TypeName = mkLensName_tc "Control.Lens.Type" "Traversal'"
lensTypeName :: Name
lensTypeName = mkLensName_tc "Control.Lens.Type" "Lens"
lens'TypeName :: Name
lens'TypeName = mkLensName_tc "Control.Lens.Type" "Lens'"
isoTypeName :: Name
isoTypeName = mkLensName_tc "Control.Lens.Type" "Iso"
iso'TypeName :: Name
iso'TypeName = mkLensName_tc "Control.Lens.Type" "Iso'"
getterTypeName :: Name
getterTypeName = mkLensName_tc "Control.Lens.Type" "Getter"
foldTypeName :: Name
foldTypeName = mkLensName_tc "Control.Lens.Type" "Fold"
prismTypeName :: Name
prismTypeName = mkLensName_tc "Control.Lens.Type" "Prism"
prism'TypeName :: Name
prism'TypeName = mkLensName_tc "Control.Lens.Type" "Prism'"
reviewTypeName :: Name
reviewTypeName = mkLensName_tc "Control.Lens.Type" "Review"
review'TypeName :: Name
review'TypeName = mkLensName_tc "Control.Lens.Type" "Review'"
wrappedTypeName :: Name
wrappedTypeName = mkLensName_tc "Control.Lens.Wrapped" "Wrapped"
unwrappedTypeName :: Name
unwrappedTypeName = mkLensName_tc "Control.Lens.Wrapped" "Unwrapped"
rewrappedTypeName :: Name
rewrappedTypeName = mkLensName_tc "Control.Lens.Wrapped" "Rewrapped"
_wrapped'ValName :: Name
_wrapped'ValName = mkLensName_v "Control.Lens.Wrapped" "_Wrapped'"
isoValName :: Name
isoValName = mkLensName_v "Control.Lens.Iso" "iso"
prismValName :: Name
prismValName = mkLensName_v "Control.Lens.Prism" "prism"
untoValName :: Name
untoValName = mkLensName_v "Control.Lens.Review" "unto"
coerceValName :: Name
coerceValName = mkLensName_v "Control.Lens.Internal.Getter" "coerce"
composeValName :: Name
composeValName = mkNameG_v "base" "GHC.Base" "."
idValName :: Name
idValName = mkNameG_v "base" "GHC.Base" "id"
fmapValName :: Name
fmapValName = mkNameG_v "base" "GHC.Base" "fmap"
pureValName :: Name
pureValName = mkNameG_v "base" "Control.Applicative" "pure"
apValName :: Name
apValName = mkNameG_v "base" "Control.Applicative" "<*>"
rightDataName :: Name
rightDataName = mkNameG_d "base" "Data.Either" "Right"
leftDataName :: Name
leftDataName = mkNameG_d "base" "Data.Either" "Left"