fd5dd70802c70efea6b27551513c56decb35fb9c
[ghc.git] / libraries / template-haskell / Language / Haskell / TH / Lib.hs
1 -- |
2 -- TH.Lib contains lots of useful helper functions for
3 -- generating and manipulating Template Haskell terms
4
5 {-# LANGUAGE CPP #-}
6
7 module Language.Haskell.TH.Lib where
8 -- All of the exports from this module should
9 -- be "public" functions. The main module TH
10 -- re-exports them all.
11
12 import Language.Haskell.TH.Syntax hiding (Role, InjectivityAnn)
13 import qualified Language.Haskell.TH.Syntax as TH
14 import Control.Monad( liftM, liftM2 )
15 import Data.Word( Word8 )
16
17 ----------------------------------------------------------
18 -- * Type synonyms
19 ----------------------------------------------------------
20
21 type InfoQ = Q Info
22 type PatQ = Q Pat
23 type FieldPatQ = Q FieldPat
24 type ExpQ = Q Exp
25 type TExpQ a = Q (TExp a)
26 type DecQ = Q Dec
27 type DecsQ = Q [Dec]
28 type ConQ = Q Con
29 type TypeQ = Q Type
30 type TyLitQ = Q TyLit
31 type CxtQ = Q Cxt
32 type PredQ = Q Pred
33 type MatchQ = Q Match
34 type ClauseQ = Q Clause
35 type BodyQ = Q Body
36 type GuardQ = Q Guard
37 type StmtQ = Q Stmt
38 type RangeQ = Q Range
39 type StrictTypeQ = Q StrictType
40 type VarStrictTypeQ = Q VarStrictType
41 type FieldExpQ = Q FieldExp
42 type RuleBndrQ = Q RuleBndr
43 type TySynEqnQ = Q TySynEqn
44 type Role = TH.Role -- must be defined here for DsMeta to find it
45 type InjectivityAnn = TH.InjectivityAnn
46
47 ----------------------------------------------------------
48 -- * Lowercase pattern syntax functions
49 ----------------------------------------------------------
50
51 intPrimL :: Integer -> Lit
52 intPrimL = IntPrimL
53 wordPrimL :: Integer -> Lit
54 wordPrimL = WordPrimL
55 floatPrimL :: Rational -> Lit
56 floatPrimL = FloatPrimL
57 doublePrimL :: Rational -> Lit
58 doublePrimL = DoublePrimL
59 integerL :: Integer -> Lit
60 integerL = IntegerL
61 charL :: Char -> Lit
62 charL = CharL
63 charPrimL :: Char -> Lit
64 charPrimL = CharPrimL
65 stringL :: String -> Lit
66 stringL = StringL
67 stringPrimL :: [Word8] -> Lit
68 stringPrimL = StringPrimL
69 rationalL :: Rational -> Lit
70 rationalL = RationalL
71
72 litP :: Lit -> PatQ
73 litP l = return (LitP l)
74 varP :: Name -> PatQ
75 varP v = return (VarP v)
76 tupP :: [PatQ] -> PatQ
77 tupP ps = do { ps1 <- sequence ps; return (TupP ps1)}
78 unboxedTupP :: [PatQ] -> PatQ
79 unboxedTupP ps = do { ps1 <- sequence ps; return (UnboxedTupP ps1)}
80 conP :: Name -> [PatQ] -> PatQ
81 conP n ps = do ps' <- sequence ps
82 return (ConP n ps')
83 infixP :: PatQ -> Name -> PatQ -> PatQ
84 infixP p1 n p2 = do p1' <- p1
85 p2' <- p2
86 return (InfixP p1' n p2')
87 uInfixP :: PatQ -> Name -> PatQ -> PatQ
88 uInfixP p1 n p2 = do p1' <- p1
89 p2' <- p2
90 return (UInfixP p1' n p2')
91 parensP :: PatQ -> PatQ
92 parensP p = do p' <- p
93 return (ParensP p')
94
95 tildeP :: PatQ -> PatQ
96 tildeP p = do p' <- p
97 return (TildeP p')
98 bangP :: PatQ -> PatQ
99 bangP p = do p' <- p
100 return (BangP p')
101 asP :: Name -> PatQ -> PatQ
102 asP n p = do p' <- p
103 return (AsP n p')
104 wildP :: PatQ
105 wildP = return WildP
106 recP :: Name -> [FieldPatQ] -> PatQ
107 recP n fps = do fps' <- sequence fps
108 return (RecP n fps')
109 listP :: [PatQ] -> PatQ
110 listP ps = do ps' <- sequence ps
111 return (ListP ps')
112 sigP :: PatQ -> TypeQ -> PatQ
113 sigP p t = do p' <- p
114 t' <- t
115 return (SigP p' t')
116 viewP :: ExpQ -> PatQ -> PatQ
117 viewP e p = do e' <- e
118 p' <- p
119 return (ViewP e' p')
120
121 fieldPat :: Name -> PatQ -> FieldPatQ
122 fieldPat n p = do p' <- p
123 return (n, p')
124
125
126 -------------------------------------------------------------------------------
127 -- * Stmt
128
129 bindS :: PatQ -> ExpQ -> StmtQ
130 bindS p e = liftM2 BindS p e
131
132 letS :: [DecQ] -> StmtQ
133 letS ds = do { ds1 <- sequence ds; return (LetS ds1) }
134
135 noBindS :: ExpQ -> StmtQ
136 noBindS e = do { e1 <- e; return (NoBindS e1) }
137
138 parS :: [[StmtQ]] -> StmtQ
139 parS sss = do { sss1 <- mapM sequence sss; return (ParS sss1) }
140
141 -------------------------------------------------------------------------------
142 -- * Range
143
144 fromR :: ExpQ -> RangeQ
145 fromR x = do { a <- x; return (FromR a) }
146
147 fromThenR :: ExpQ -> ExpQ -> RangeQ
148 fromThenR x y = do { a <- x; b <- y; return (FromThenR a b) }
149
150 fromToR :: ExpQ -> ExpQ -> RangeQ
151 fromToR x y = do { a <- x; b <- y; return (FromToR a b) }
152
153 fromThenToR :: ExpQ -> ExpQ -> ExpQ -> RangeQ
154 fromThenToR x y z = do { a <- x; b <- y; c <- z;
155 return (FromThenToR a b c) }
156 -------------------------------------------------------------------------------
157 -- * Body
158
159 normalB :: ExpQ -> BodyQ
160 normalB e = do { e1 <- e; return (NormalB e1) }
161
162 guardedB :: [Q (Guard,Exp)] -> BodyQ
163 guardedB ges = do { ges' <- sequence ges; return (GuardedB ges') }
164
165 -------------------------------------------------------------------------------
166 -- * Guard
167
168 normalG :: ExpQ -> GuardQ
169 normalG e = do { e1 <- e; return (NormalG e1) }
170
171 normalGE :: ExpQ -> ExpQ -> Q (Guard, Exp)
172 normalGE g e = do { g1 <- g; e1 <- e; return (NormalG g1, e1) }
173
174 patG :: [StmtQ] -> GuardQ
175 patG ss = do { ss' <- sequence ss; return (PatG ss') }
176
177 patGE :: [StmtQ] -> ExpQ -> Q (Guard, Exp)
178 patGE ss e = do { ss' <- sequence ss;
179 e' <- e;
180 return (PatG ss', e') }
181
182 -------------------------------------------------------------------------------
183 -- * Match and Clause
184
185 -- | Use with 'caseE'
186 match :: PatQ -> BodyQ -> [DecQ] -> MatchQ
187 match p rhs ds = do { p' <- p;
188 r' <- rhs;
189 ds' <- sequence ds;
190 return (Match p' r' ds') }
191
192 -- | Use with 'funD'
193 clause :: [PatQ] -> BodyQ -> [DecQ] -> ClauseQ
194 clause ps r ds = do { ps' <- sequence ps;
195 r' <- r;
196 ds' <- sequence ds;
197 return (Clause ps' r' ds') }
198
199
200 ---------------------------------------------------------------------------
201 -- * Exp
202
203 -- | Dynamically binding a variable (unhygenic)
204 dyn :: String -> ExpQ
205 dyn s = return (VarE (mkName s))
206
207 varE :: Name -> ExpQ
208 varE s = return (VarE s)
209
210 conE :: Name -> ExpQ
211 conE s = return (ConE s)
212
213 litE :: Lit -> ExpQ
214 litE c = return (LitE c)
215
216 appE :: ExpQ -> ExpQ -> ExpQ
217 appE x y = do { a <- x; b <- y; return (AppE a b)}
218
219 parensE :: ExpQ -> ExpQ
220 parensE x = do { x' <- x; return (ParensE x') }
221
222 uInfixE :: ExpQ -> ExpQ -> ExpQ -> ExpQ
223 uInfixE x s y = do { x' <- x; s' <- s; y' <- y;
224 return (UInfixE x' s' y') }
225
226 infixE :: Maybe ExpQ -> ExpQ -> Maybe ExpQ -> ExpQ
227 infixE (Just x) s (Just y) = do { a <- x; s' <- s; b <- y;
228 return (InfixE (Just a) s' (Just b))}
229 infixE Nothing s (Just y) = do { s' <- s; b <- y;
230 return (InfixE Nothing s' (Just b))}
231 infixE (Just x) s Nothing = do { a <- x; s' <- s;
232 return (InfixE (Just a) s' Nothing)}
233 infixE Nothing s Nothing = do { s' <- s; return (InfixE Nothing s' Nothing) }
234
235 infixApp :: ExpQ -> ExpQ -> ExpQ -> ExpQ
236 infixApp x y z = infixE (Just x) y (Just z)
237 sectionL :: ExpQ -> ExpQ -> ExpQ
238 sectionL x y = infixE (Just x) y Nothing
239 sectionR :: ExpQ -> ExpQ -> ExpQ
240 sectionR x y = infixE Nothing x (Just y)
241
242 lamE :: [PatQ] -> ExpQ -> ExpQ
243 lamE ps e = do ps' <- sequence ps
244 e' <- e
245 return (LamE ps' e')
246
247 -- | Single-arg lambda
248 lam1E :: PatQ -> ExpQ -> ExpQ
249 lam1E p e = lamE [p] e
250
251 lamCaseE :: [MatchQ] -> ExpQ
252 lamCaseE ms = sequence ms >>= return . LamCaseE
253
254 tupE :: [ExpQ] -> ExpQ
255 tupE es = do { es1 <- sequence es; return (TupE es1)}
256
257 unboxedTupE :: [ExpQ] -> ExpQ
258 unboxedTupE es = do { es1 <- sequence es; return (UnboxedTupE es1)}
259
260 condE :: ExpQ -> ExpQ -> ExpQ -> ExpQ
261 condE x y z = do { a <- x; b <- y; c <- z; return (CondE a b c)}
262
263 multiIfE :: [Q (Guard, Exp)] -> ExpQ
264 multiIfE alts = sequence alts >>= return . MultiIfE
265
266 letE :: [DecQ] -> ExpQ -> ExpQ
267 letE ds e = do { ds2 <- sequence ds; e2 <- e; return (LetE ds2 e2) }
268
269 caseE :: ExpQ -> [MatchQ] -> ExpQ
270 caseE e ms = do { e1 <- e; ms1 <- sequence ms; return (CaseE e1 ms1) }
271
272 doE :: [StmtQ] -> ExpQ
273 doE ss = do { ss1 <- sequence ss; return (DoE ss1) }
274
275 compE :: [StmtQ] -> ExpQ
276 compE ss = do { ss1 <- sequence ss; return (CompE ss1) }
277
278 arithSeqE :: RangeQ -> ExpQ
279 arithSeqE r = do { r' <- r; return (ArithSeqE r') }
280
281 listE :: [ExpQ] -> ExpQ
282 listE es = do { es1 <- sequence es; return (ListE es1) }
283
284 sigE :: ExpQ -> TypeQ -> ExpQ
285 sigE e t = do { e1 <- e; t1 <- t; return (SigE e1 t1) }
286
287 recConE :: Name -> [Q (Name,Exp)] -> ExpQ
288 recConE c fs = do { flds <- sequence fs; return (RecConE c flds) }
289
290 recUpdE :: ExpQ -> [Q (Name,Exp)] -> ExpQ
291 recUpdE e fs = do { e1 <- e; flds <- sequence fs; return (RecUpdE e1 flds) }
292
293 stringE :: String -> ExpQ
294 stringE = litE . stringL
295
296 fieldExp :: Name -> ExpQ -> Q (Name, Exp)
297 fieldExp s e = do { e' <- e; return (s,e') }
298
299 -- | @staticE x = [| static x |]@
300 staticE :: ExpQ -> ExpQ
301 staticE = fmap StaticE
302
303 -- ** 'arithSeqE' Shortcuts
304 fromE :: ExpQ -> ExpQ
305 fromE x = do { a <- x; return (ArithSeqE (FromR a)) }
306
307 fromThenE :: ExpQ -> ExpQ -> ExpQ
308 fromThenE x y = do { a <- x; b <- y; return (ArithSeqE (FromThenR a b)) }
309
310 fromToE :: ExpQ -> ExpQ -> ExpQ
311 fromToE x y = do { a <- x; b <- y; return (ArithSeqE (FromToR a b)) }
312
313 fromThenToE :: ExpQ -> ExpQ -> ExpQ -> ExpQ
314 fromThenToE x y z = do { a <- x; b <- y; c <- z;
315 return (ArithSeqE (FromThenToR a b c)) }
316
317
318 -------------------------------------------------------------------------------
319 -- * Dec
320
321 valD :: PatQ -> BodyQ -> [DecQ] -> DecQ
322 valD p b ds =
323 do { p' <- p
324 ; ds' <- sequence ds
325 ; b' <- b
326 ; return (ValD p' b' ds')
327 }
328
329 funD :: Name -> [ClauseQ] -> DecQ
330 funD nm cs =
331 do { cs1 <- sequence cs
332 ; return (FunD nm cs1)
333 }
334
335 tySynD :: Name -> [TyVarBndr] -> TypeQ -> DecQ
336 tySynD tc tvs rhs = do { rhs1 <- rhs; return (TySynD tc tvs rhs1) }
337
338 dataD :: CxtQ -> Name -> [TyVarBndr] -> [ConQ] -> [Name] -> DecQ
339 dataD ctxt tc tvs cons derivs =
340 do
341 ctxt1 <- ctxt
342 cons1 <- sequence cons
343 return (DataD ctxt1 tc tvs cons1 derivs)
344
345 newtypeD :: CxtQ -> Name -> [TyVarBndr] -> ConQ -> [Name] -> DecQ
346 newtypeD ctxt tc tvs con derivs =
347 do
348 ctxt1 <- ctxt
349 con1 <- con
350 return (NewtypeD ctxt1 tc tvs con1 derivs)
351
352 classD :: CxtQ -> Name -> [TyVarBndr] -> [FunDep] -> [DecQ] -> DecQ
353 classD ctxt cls tvs fds decs =
354 do
355 decs1 <- sequence decs
356 ctxt1 <- ctxt
357 return $ ClassD ctxt1 cls tvs fds decs1
358
359 instanceD :: CxtQ -> TypeQ -> [DecQ] -> DecQ
360 instanceD ctxt ty decs =
361 do
362 ctxt1 <- ctxt
363 decs1 <- sequence decs
364 ty1 <- ty
365 return $ InstanceD ctxt1 ty1 decs1
366
367 sigD :: Name -> TypeQ -> DecQ
368 sigD fun ty = liftM (SigD fun) $ ty
369
370 forImpD :: Callconv -> Safety -> String -> Name -> TypeQ -> DecQ
371 forImpD cc s str n ty
372 = do ty' <- ty
373 return $ ForeignD (ImportF cc s str n ty')
374
375 infixLD :: Int -> Name -> DecQ
376 infixLD prec nm = return (InfixD (Fixity prec InfixL) nm)
377
378 infixRD :: Int -> Name -> DecQ
379 infixRD prec nm = return (InfixD (Fixity prec InfixR) nm)
380
381 infixND :: Int -> Name -> DecQ
382 infixND prec nm = return (InfixD (Fixity prec InfixN) nm)
383
384 pragInlD :: Name -> Inline -> RuleMatch -> Phases -> DecQ
385 pragInlD name inline rm phases
386 = return $ PragmaD $ InlineP name inline rm phases
387
388 pragSpecD :: Name -> TypeQ -> Phases -> DecQ
389 pragSpecD n ty phases
390 = do
391 ty1 <- ty
392 return $ PragmaD $ SpecialiseP n ty1 Nothing phases
393
394 pragSpecInlD :: Name -> TypeQ -> Inline -> Phases -> DecQ
395 pragSpecInlD n ty inline phases
396 = do
397 ty1 <- ty
398 return $ PragmaD $ SpecialiseP n ty1 (Just inline) phases
399
400 pragSpecInstD :: TypeQ -> DecQ
401 pragSpecInstD ty
402 = do
403 ty1 <- ty
404 return $ PragmaD $ SpecialiseInstP ty1
405
406 pragRuleD :: String -> [RuleBndrQ] -> ExpQ -> ExpQ -> Phases -> DecQ
407 pragRuleD n bndrs lhs rhs phases
408 = do
409 bndrs1 <- sequence bndrs
410 lhs1 <- lhs
411 rhs1 <- rhs
412 return $ PragmaD $ RuleP n bndrs1 lhs1 rhs1 phases
413
414 pragAnnD :: AnnTarget -> ExpQ -> DecQ
415 pragAnnD target expr
416 = do
417 exp1 <- expr
418 return $ PragmaD $ AnnP target exp1
419
420 pragLineD :: Int -> String -> DecQ
421 pragLineD line file = return $ PragmaD $ LineP line file
422
423 dataInstD :: CxtQ -> Name -> [TypeQ] -> [ConQ] -> [Name] -> DecQ
424 dataInstD ctxt tc tys cons derivs =
425 do
426 ctxt1 <- ctxt
427 tys1 <- sequence tys
428 cons1 <- sequence cons
429 return (DataInstD ctxt1 tc tys1 cons1 derivs)
430
431 newtypeInstD :: CxtQ -> Name -> [TypeQ] -> ConQ -> [Name] -> DecQ
432 newtypeInstD ctxt tc tys con derivs =
433 do
434 ctxt1 <- ctxt
435 tys1 <- sequence tys
436 con1 <- con
437 return (NewtypeInstD ctxt1 tc tys1 con1 derivs)
438
439 tySynInstD :: Name -> TySynEqnQ -> DecQ
440 tySynInstD tc eqn =
441 do
442 eqn1 <- eqn
443 return (TySynInstD tc eqn1)
444
445 dataFamilyD :: Name -> [TyVarBndr] -> Maybe Kind -> DecQ
446 dataFamilyD tc tvs kind
447 = return $ DataFamilyD tc tvs kind
448
449 openTypeFamilyD :: Name -> [TyVarBndr] -> FamilyResultSig
450 -> Maybe InjectivityAnn -> DecQ
451 openTypeFamilyD tc tvs res inj
452 = return $ OpenTypeFamilyD tc tvs res inj
453
454 closedTypeFamilyD :: Name -> [TyVarBndr] -> FamilyResultSig
455 -> Maybe InjectivityAnn -> [TySynEqnQ] -> DecQ
456 closedTypeFamilyD tc tvs result injectivity eqns =
457 do eqns1 <- sequence eqns
458 return (ClosedTypeFamilyD tc tvs result injectivity eqns1)
459
460 -- These were deprecated in GHC 7.12 with a plan to remove them in 7.14. If you
461 -- remove this check please also:
462 -- 1. remove deprecated functions
463 -- 2. remove CPP language extension from top of this module
464 -- 3. remove the FamFlavour data type from Syntax module
465 -- 4. make sure that all references to FamFlavour are gone from DsMeta,
466 -- Convert, TcSplice (follows from 3)
467 #if __GLASGOW_HASKELL__ > 712
468 #error Remove deprecated familyNoKindD, familyKindD, closedTypeFamilyNoKindD and closedTypeFamilyKindD
469 #endif
470
471 {-# DEPRECATED familyNoKindD, familyKindD
472 "This function will be removed in the next stable release. Use openTypeFamilyD/dataFamilyD instead." #-}
473 familyNoKindD :: FamFlavour -> Name -> [TyVarBndr] -> DecQ
474 familyNoKindD flav tc tvs =
475 case flav of
476 TypeFam -> return $ OpenTypeFamilyD tc tvs NoSig Nothing
477 DataFam -> return $ DataFamilyD tc tvs Nothing
478
479 familyKindD :: FamFlavour -> Name -> [TyVarBndr] -> Kind -> DecQ
480 familyKindD flav tc tvs k =
481 case flav of
482 TypeFam -> return $ OpenTypeFamilyD tc tvs (KindSig k) Nothing
483 DataFam -> return $ DataFamilyD tc tvs (Just k)
484
485 {-# DEPRECATED closedTypeFamilyNoKindD, closedTypeFamilyKindD
486 "This function will be removed in the next stable release. Use closedTypeFamilyD instead." #-}
487 closedTypeFamilyNoKindD :: Name -> [TyVarBndr] -> [TySynEqnQ] -> DecQ
488 closedTypeFamilyNoKindD tc tvs eqns =
489 do eqns1 <- sequence eqns
490 return (ClosedTypeFamilyD tc tvs NoSig Nothing eqns1)
491
492 closedTypeFamilyKindD :: Name -> [TyVarBndr] -> Kind -> [TySynEqnQ] -> DecQ
493 closedTypeFamilyKindD tc tvs kind eqns =
494 do eqns1 <- sequence eqns
495 return (ClosedTypeFamilyD tc tvs (KindSig kind) Nothing eqns1)
496
497 roleAnnotD :: Name -> [Role] -> DecQ
498 roleAnnotD name roles = return $ RoleAnnotD name roles
499
500 standaloneDerivD :: CxtQ -> TypeQ -> DecQ
501 standaloneDerivD ctxtq tyq =
502 do
503 ctxt <- ctxtq
504 ty <- tyq
505 return $ StandaloneDerivD ctxt ty
506
507 defaultSigD :: Name -> TypeQ -> DecQ
508 defaultSigD n tyq =
509 do
510 ty <- tyq
511 return $ DefaultSigD n ty
512
513 tySynEqn :: [TypeQ] -> TypeQ -> TySynEqnQ
514 tySynEqn lhs rhs =
515 do
516 lhs1 <- sequence lhs
517 rhs1 <- rhs
518 return (TySynEqn lhs1 rhs1)
519
520 cxt :: [PredQ] -> CxtQ
521 cxt = sequence
522
523 normalC :: Name -> [StrictTypeQ] -> ConQ
524 normalC con strtys = liftM (NormalC con) $ sequence strtys
525
526 recC :: Name -> [VarStrictTypeQ] -> ConQ
527 recC con varstrtys = liftM (RecC con) $ sequence varstrtys
528
529 infixC :: Q (Strict, Type) -> Name -> Q (Strict, Type) -> ConQ
530 infixC st1 con st2 = do st1' <- st1
531 st2' <- st2
532 return $ InfixC st1' con st2'
533
534 forallC :: [TyVarBndr] -> CxtQ -> ConQ -> ConQ
535 forallC ns ctxt con = liftM2 (ForallC ns) ctxt con
536
537
538 -------------------------------------------------------------------------------
539 -- * Type
540
541 forallT :: [TyVarBndr] -> CxtQ -> TypeQ -> TypeQ
542 forallT tvars ctxt ty = do
543 ctxt1 <- ctxt
544 ty1 <- ty
545 return $ ForallT tvars ctxt1 ty1
546
547 varT :: Name -> TypeQ
548 varT = return . VarT
549
550 conT :: Name -> TypeQ
551 conT = return . ConT
552
553 infixT :: TypeQ -> Name -> TypeQ -> TypeQ
554 infixT t1 n t2 = do t1' <- t1
555 t2' <- t2
556 return (InfixT t1' n t2')
557
558 uInfixT :: TypeQ -> Name -> TypeQ -> TypeQ
559 uInfixT t1 n t2 = do t1' <- t1
560 t2' <- t2
561 return (UInfixT t1' n t2')
562
563 parensT :: TypeQ -> TypeQ
564 parensT t = do t' <- t
565 return (ParensT t')
566
567 appT :: TypeQ -> TypeQ -> TypeQ
568 appT t1 t2 = do
569 t1' <- t1
570 t2' <- t2
571 return $ AppT t1' t2'
572
573 arrowT :: TypeQ
574 arrowT = return ArrowT
575
576 listT :: TypeQ
577 listT = return ListT
578
579 litT :: TyLitQ -> TypeQ
580 litT l = fmap LitT l
581
582 tupleT :: Int -> TypeQ
583 tupleT i = return (TupleT i)
584
585 unboxedTupleT :: Int -> TypeQ
586 unboxedTupleT i = return (UnboxedTupleT i)
587
588 sigT :: TypeQ -> Kind -> TypeQ
589 sigT t k
590 = do
591 t' <- t
592 return $ SigT t' k
593
594 equalityT :: TypeQ
595 equalityT = return EqualityT
596
597 wildCardT :: TypeQ
598 wildCardT = return (WildCardT Nothing)
599
600 namedWildCardT :: Name -> TypeQ
601 namedWildCardT = return . WildCardT . Just
602
603 {-# DEPRECATED classP "As of template-haskell-2.10, constraint predicates (Pred) are just types (Type), in keeping with ConstraintKinds. Please use 'conT' and 'appT'." #-}
604 classP :: Name -> [Q Type] -> Q Pred
605 classP cla tys
606 = do
607 tysl <- sequence tys
608 return (foldl AppT (ConT cla) tysl)
609
610 {-# DEPRECATED equalP "As of template-haskell-2.10, constraint predicates (Pred) are just types (Type), in keeping with ConstraintKinds. Please see 'equalityT'." #-}
611 equalP :: TypeQ -> TypeQ -> PredQ
612 equalP tleft tright
613 = do
614 tleft1 <- tleft
615 tright1 <- tright
616 eqT <- equalityT
617 return (foldl AppT eqT [tleft1, tright1])
618
619 promotedT :: Name -> TypeQ
620 promotedT = return . PromotedT
621
622 promotedTupleT :: Int -> TypeQ
623 promotedTupleT i = return (PromotedTupleT i)
624
625 promotedNilT :: TypeQ
626 promotedNilT = return PromotedNilT
627
628 promotedConsT :: TypeQ
629 promotedConsT = return PromotedConsT
630
631 isStrict, notStrict, unpacked :: Q Strict
632 isStrict = return $ IsStrict
633 notStrict = return $ NotStrict
634 unpacked = return Unpacked
635
636 strictType :: Q Strict -> TypeQ -> StrictTypeQ
637 strictType = liftM2 (,)
638
639 varStrictType :: Name -> StrictTypeQ -> VarStrictTypeQ
640 varStrictType v st = do (s, t) <- st
641 return (v, s, t)
642
643 -- * Type Literals
644
645 numTyLit :: Integer -> TyLitQ
646 numTyLit n = if n >= 0 then return (NumTyLit n)
647 else fail ("Negative type-level number: " ++ show n)
648
649 strTyLit :: String -> TyLitQ
650 strTyLit s = return (StrTyLit s)
651
652
653
654 -------------------------------------------------------------------------------
655 -- * Kind
656
657 plainTV :: Name -> TyVarBndr
658 plainTV = PlainTV
659
660 kindedTV :: Name -> Kind -> TyVarBndr
661 kindedTV = KindedTV
662
663 varK :: Name -> Kind
664 varK = VarT
665
666 conK :: Name -> Kind
667 conK = ConT
668
669 tupleK :: Int -> Kind
670 tupleK = TupleT
671
672 arrowK :: Kind
673 arrowK = ArrowT
674
675 listK :: Kind
676 listK = ListT
677
678 appK :: Kind -> Kind -> Kind
679 appK = AppT
680
681 starK :: Kind
682 starK = StarT
683
684 constraintK :: Kind
685 constraintK = ConstraintT
686
687 -------------------------------------------------------------------------------
688 -- * Type family result
689
690 noSig :: FamilyResultSig
691 noSig = NoSig
692
693 kindSig :: Kind -> FamilyResultSig
694 kindSig = KindSig
695
696 tyVarSig :: TyVarBndr -> FamilyResultSig
697 tyVarSig = TyVarSig
698
699 -------------------------------------------------------------------------------
700 -- * Injectivity annotation
701
702 injectivityAnn :: Name -> [Name] -> InjectivityAnn
703 injectivityAnn = TH.InjectivityAnn
704
705 -------------------------------------------------------------------------------
706 -- * Role
707
708 nominalR, representationalR, phantomR, inferR :: Role
709 nominalR = NominalR
710 representationalR = RepresentationalR
711 phantomR = PhantomR
712 inferR = InferR
713
714 -------------------------------------------------------------------------------
715 -- * Callconv
716
717 cCall, stdCall, cApi, prim, javaScript :: Callconv
718 cCall = CCall
719 stdCall = StdCall
720 cApi = CApi
721 prim = Prim
722 javaScript = JavaScript
723
724 -------------------------------------------------------------------------------
725 -- * Safety
726
727 unsafe, safe, interruptible :: Safety
728 unsafe = Unsafe
729 safe = Safe
730 interruptible = Interruptible
731
732 -------------------------------------------------------------------------------
733 -- * FunDep
734
735 funDep :: [Name] -> [Name] -> FunDep
736 funDep = FunDep
737
738 -------------------------------------------------------------------------------
739 -- * FamFlavour
740
741 typeFam, dataFam :: FamFlavour
742 typeFam = TypeFam
743 dataFam = DataFam
744
745 -------------------------------------------------------------------------------
746 -- * RuleBndr
747 ruleVar :: Name -> RuleBndrQ
748 ruleVar = return . RuleVar
749
750 typedRuleVar :: Name -> TypeQ -> RuleBndrQ
751 typedRuleVar n ty = ty >>= return . TypedRuleVar n
752
753 -------------------------------------------------------------------------------
754 -- * AnnTarget
755 valueAnnotation :: Name -> AnnTarget
756 valueAnnotation = ValueAnnotation
757
758 typeAnnotation :: Name -> AnnTarget
759 typeAnnotation = TypeAnnotation
760
761 moduleAnnotation :: AnnTarget
762 moduleAnnotation = ModuleAnnotation
763
764 --------------------------------------------------------------
765 -- * Useful helper function
766
767 appsE :: [ExpQ] -> ExpQ
768 appsE [] = error "appsE []"
769 appsE [x] = x
770 appsE (x:y:zs) = appsE ( (appE x y) : zs )
771
772 -- | Return the Module at the place of splicing. Can be used as an
773 -- input for 'reifyModule'.
774 thisModule :: Q Module
775 thisModule = do
776 loc <- location
777 return $ Module (mkPkgName $ loc_package loc) (mkModName $ loc_module loc)