Add typed holes support in Template Haskell.
[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 unboundVarE :: Name -> ExpQ
304 unboundVarE s = return (UnboundVarE s)
305
306 -- ** 'arithSeqE' Shortcuts
307 fromE :: ExpQ -> ExpQ
308 fromE x = do { a <- x; return (ArithSeqE (FromR a)) }
309
310 fromThenE :: ExpQ -> ExpQ -> ExpQ
311 fromThenE x y = do { a <- x; b <- y; return (ArithSeqE (FromThenR a b)) }
312
313 fromToE :: ExpQ -> ExpQ -> ExpQ
314 fromToE x y = do { a <- x; b <- y; return (ArithSeqE (FromToR a b)) }
315
316 fromThenToE :: ExpQ -> ExpQ -> ExpQ -> ExpQ
317 fromThenToE x y z = do { a <- x; b <- y; c <- z;
318 return (ArithSeqE (FromThenToR a b c)) }
319
320
321 -------------------------------------------------------------------------------
322 -- * Dec
323
324 valD :: PatQ -> BodyQ -> [DecQ] -> DecQ
325 valD p b ds =
326 do { p' <- p
327 ; ds' <- sequence ds
328 ; b' <- b
329 ; return (ValD p' b' ds')
330 }
331
332 funD :: Name -> [ClauseQ] -> DecQ
333 funD nm cs =
334 do { cs1 <- sequence cs
335 ; return (FunD nm cs1)
336 }
337
338 tySynD :: Name -> [TyVarBndr] -> TypeQ -> DecQ
339 tySynD tc tvs rhs = do { rhs1 <- rhs; return (TySynD tc tvs rhs1) }
340
341 dataD :: CxtQ -> Name -> [TyVarBndr] -> [ConQ] -> [Name] -> DecQ
342 dataD ctxt tc tvs cons derivs =
343 do
344 ctxt1 <- ctxt
345 cons1 <- sequence cons
346 return (DataD ctxt1 tc tvs cons1 derivs)
347
348 newtypeD :: CxtQ -> Name -> [TyVarBndr] -> ConQ -> [Name] -> DecQ
349 newtypeD ctxt tc tvs con derivs =
350 do
351 ctxt1 <- ctxt
352 con1 <- con
353 return (NewtypeD ctxt1 tc tvs con1 derivs)
354
355 classD :: CxtQ -> Name -> [TyVarBndr] -> [FunDep] -> [DecQ] -> DecQ
356 classD ctxt cls tvs fds decs =
357 do
358 decs1 <- sequence decs
359 ctxt1 <- ctxt
360 return $ ClassD ctxt1 cls tvs fds decs1
361
362 instanceD :: CxtQ -> TypeQ -> [DecQ] -> DecQ
363 instanceD ctxt ty decs =
364 do
365 ctxt1 <- ctxt
366 decs1 <- sequence decs
367 ty1 <- ty
368 return $ InstanceD ctxt1 ty1 decs1
369
370 sigD :: Name -> TypeQ -> DecQ
371 sigD fun ty = liftM (SigD fun) $ ty
372
373 forImpD :: Callconv -> Safety -> String -> Name -> TypeQ -> DecQ
374 forImpD cc s str n ty
375 = do ty' <- ty
376 return $ ForeignD (ImportF cc s str n ty')
377
378 infixLD :: Int -> Name -> DecQ
379 infixLD prec nm = return (InfixD (Fixity prec InfixL) nm)
380
381 infixRD :: Int -> Name -> DecQ
382 infixRD prec nm = return (InfixD (Fixity prec InfixR) nm)
383
384 infixND :: Int -> Name -> DecQ
385 infixND prec nm = return (InfixD (Fixity prec InfixN) nm)
386
387 pragInlD :: Name -> Inline -> RuleMatch -> Phases -> DecQ
388 pragInlD name inline rm phases
389 = return $ PragmaD $ InlineP name inline rm phases
390
391 pragSpecD :: Name -> TypeQ -> Phases -> DecQ
392 pragSpecD n ty phases
393 = do
394 ty1 <- ty
395 return $ PragmaD $ SpecialiseP n ty1 Nothing phases
396
397 pragSpecInlD :: Name -> TypeQ -> Inline -> Phases -> DecQ
398 pragSpecInlD n ty inline phases
399 = do
400 ty1 <- ty
401 return $ PragmaD $ SpecialiseP n ty1 (Just inline) phases
402
403 pragSpecInstD :: TypeQ -> DecQ
404 pragSpecInstD ty
405 = do
406 ty1 <- ty
407 return $ PragmaD $ SpecialiseInstP ty1
408
409 pragRuleD :: String -> [RuleBndrQ] -> ExpQ -> ExpQ -> Phases -> DecQ
410 pragRuleD n bndrs lhs rhs phases
411 = do
412 bndrs1 <- sequence bndrs
413 lhs1 <- lhs
414 rhs1 <- rhs
415 return $ PragmaD $ RuleP n bndrs1 lhs1 rhs1 phases
416
417 pragAnnD :: AnnTarget -> ExpQ -> DecQ
418 pragAnnD target expr
419 = do
420 exp1 <- expr
421 return $ PragmaD $ AnnP target exp1
422
423 pragLineD :: Int -> String -> DecQ
424 pragLineD line file = return $ PragmaD $ LineP line file
425
426 dataInstD :: CxtQ -> Name -> [TypeQ] -> [ConQ] -> [Name] -> DecQ
427 dataInstD ctxt tc tys cons derivs =
428 do
429 ctxt1 <- ctxt
430 tys1 <- sequence tys
431 cons1 <- sequence cons
432 return (DataInstD ctxt1 tc tys1 cons1 derivs)
433
434 newtypeInstD :: CxtQ -> Name -> [TypeQ] -> ConQ -> [Name] -> DecQ
435 newtypeInstD ctxt tc tys con derivs =
436 do
437 ctxt1 <- ctxt
438 tys1 <- sequence tys
439 con1 <- con
440 return (NewtypeInstD ctxt1 tc tys1 con1 derivs)
441
442 tySynInstD :: Name -> TySynEqnQ -> DecQ
443 tySynInstD tc eqn =
444 do
445 eqn1 <- eqn
446 return (TySynInstD tc eqn1)
447
448 dataFamilyD :: Name -> [TyVarBndr] -> Maybe Kind -> DecQ
449 dataFamilyD tc tvs kind
450 = return $ DataFamilyD tc tvs kind
451
452 openTypeFamilyD :: Name -> [TyVarBndr] -> FamilyResultSig
453 -> Maybe InjectivityAnn -> DecQ
454 openTypeFamilyD tc tvs res inj
455 = return $ OpenTypeFamilyD tc tvs res inj
456
457 closedTypeFamilyD :: Name -> [TyVarBndr] -> FamilyResultSig
458 -> Maybe InjectivityAnn -> [TySynEqnQ] -> DecQ
459 closedTypeFamilyD tc tvs result injectivity eqns =
460 do eqns1 <- sequence eqns
461 return (ClosedTypeFamilyD tc tvs result injectivity eqns1)
462
463 -- These were deprecated in GHC 7.12 with a plan to remove them in 7.14. If you
464 -- remove this check please also:
465 -- 1. remove deprecated functions
466 -- 2. remove CPP language extension from top of this module
467 -- 3. remove the FamFlavour data type from Syntax module
468 -- 4. make sure that all references to FamFlavour are gone from DsMeta,
469 -- Convert, TcSplice (follows from 3)
470 #if __GLASGOW_HASKELL__ > 712
471 #error Remove deprecated familyNoKindD, familyKindD, closedTypeFamilyNoKindD and closedTypeFamilyKindD
472 #endif
473
474 {-# DEPRECATED familyNoKindD, familyKindD
475 "This function will be removed in the next stable release. Use openTypeFamilyD/dataFamilyD instead." #-}
476 familyNoKindD :: FamFlavour -> Name -> [TyVarBndr] -> DecQ
477 familyNoKindD flav tc tvs =
478 case flav of
479 TypeFam -> return $ OpenTypeFamilyD tc tvs NoSig Nothing
480 DataFam -> return $ DataFamilyD tc tvs Nothing
481
482 familyKindD :: FamFlavour -> Name -> [TyVarBndr] -> Kind -> DecQ
483 familyKindD flav tc tvs k =
484 case flav of
485 TypeFam -> return $ OpenTypeFamilyD tc tvs (KindSig k) Nothing
486 DataFam -> return $ DataFamilyD tc tvs (Just k)
487
488 {-# DEPRECATED closedTypeFamilyNoKindD, closedTypeFamilyKindD
489 "This function will be removed in the next stable release. Use closedTypeFamilyD instead." #-}
490 closedTypeFamilyNoKindD :: Name -> [TyVarBndr] -> [TySynEqnQ] -> DecQ
491 closedTypeFamilyNoKindD tc tvs eqns =
492 do eqns1 <- sequence eqns
493 return (ClosedTypeFamilyD tc tvs NoSig Nothing eqns1)
494
495 closedTypeFamilyKindD :: Name -> [TyVarBndr] -> Kind -> [TySynEqnQ] -> DecQ
496 closedTypeFamilyKindD tc tvs kind eqns =
497 do eqns1 <- sequence eqns
498 return (ClosedTypeFamilyD tc tvs (KindSig kind) Nothing eqns1)
499
500 roleAnnotD :: Name -> [Role] -> DecQ
501 roleAnnotD name roles = return $ RoleAnnotD name roles
502
503 standaloneDerivD :: CxtQ -> TypeQ -> DecQ
504 standaloneDerivD ctxtq tyq =
505 do
506 ctxt <- ctxtq
507 ty <- tyq
508 return $ StandaloneDerivD ctxt ty
509
510 defaultSigD :: Name -> TypeQ -> DecQ
511 defaultSigD n tyq =
512 do
513 ty <- tyq
514 return $ DefaultSigD n ty
515
516 tySynEqn :: [TypeQ] -> TypeQ -> TySynEqnQ
517 tySynEqn lhs rhs =
518 do
519 lhs1 <- sequence lhs
520 rhs1 <- rhs
521 return (TySynEqn lhs1 rhs1)
522
523 cxt :: [PredQ] -> CxtQ
524 cxt = sequence
525
526 normalC :: Name -> [StrictTypeQ] -> ConQ
527 normalC con strtys = liftM (NormalC con) $ sequence strtys
528
529 recC :: Name -> [VarStrictTypeQ] -> ConQ
530 recC con varstrtys = liftM (RecC con) $ sequence varstrtys
531
532 infixC :: Q (Strict, Type) -> Name -> Q (Strict, Type) -> ConQ
533 infixC st1 con st2 = do st1' <- st1
534 st2' <- st2
535 return $ InfixC st1' con st2'
536
537 forallC :: [TyVarBndr] -> CxtQ -> ConQ -> ConQ
538 forallC ns ctxt con = liftM2 (ForallC ns) ctxt con
539
540
541 -------------------------------------------------------------------------------
542 -- * Type
543
544 forallT :: [TyVarBndr] -> CxtQ -> TypeQ -> TypeQ
545 forallT tvars ctxt ty = do
546 ctxt1 <- ctxt
547 ty1 <- ty
548 return $ ForallT tvars ctxt1 ty1
549
550 varT :: Name -> TypeQ
551 varT = return . VarT
552
553 conT :: Name -> TypeQ
554 conT = return . ConT
555
556 infixT :: TypeQ -> Name -> TypeQ -> TypeQ
557 infixT t1 n t2 = do t1' <- t1
558 t2' <- t2
559 return (InfixT t1' n t2')
560
561 uInfixT :: TypeQ -> Name -> TypeQ -> TypeQ
562 uInfixT t1 n t2 = do t1' <- t1
563 t2' <- t2
564 return (UInfixT t1' n t2')
565
566 parensT :: TypeQ -> TypeQ
567 parensT t = do t' <- t
568 return (ParensT t')
569
570 appT :: TypeQ -> TypeQ -> TypeQ
571 appT t1 t2 = do
572 t1' <- t1
573 t2' <- t2
574 return $ AppT t1' t2'
575
576 arrowT :: TypeQ
577 arrowT = return ArrowT
578
579 listT :: TypeQ
580 listT = return ListT
581
582 litT :: TyLitQ -> TypeQ
583 litT l = fmap LitT l
584
585 tupleT :: Int -> TypeQ
586 tupleT i = return (TupleT i)
587
588 unboxedTupleT :: Int -> TypeQ
589 unboxedTupleT i = return (UnboxedTupleT i)
590
591 sigT :: TypeQ -> Kind -> TypeQ
592 sigT t k
593 = do
594 t' <- t
595 return $ SigT t' k
596
597 equalityT :: TypeQ
598 equalityT = return EqualityT
599
600 wildCardT :: TypeQ
601 wildCardT = return (WildCardT Nothing)
602
603 namedWildCardT :: Name -> TypeQ
604 namedWildCardT = return . WildCardT . Just
605
606 {-# DEPRECATED classP "As of template-haskell-2.10, constraint predicates (Pred) are just types (Type), in keeping with ConstraintKinds. Please use 'conT' and 'appT'." #-}
607 classP :: Name -> [Q Type] -> Q Pred
608 classP cla tys
609 = do
610 tysl <- sequence tys
611 return (foldl AppT (ConT cla) tysl)
612
613 {-# DEPRECATED equalP "As of template-haskell-2.10, constraint predicates (Pred) are just types (Type), in keeping with ConstraintKinds. Please see 'equalityT'." #-}
614 equalP :: TypeQ -> TypeQ -> PredQ
615 equalP tleft tright
616 = do
617 tleft1 <- tleft
618 tright1 <- tright
619 eqT <- equalityT
620 return (foldl AppT eqT [tleft1, tright1])
621
622 promotedT :: Name -> TypeQ
623 promotedT = return . PromotedT
624
625 promotedTupleT :: Int -> TypeQ
626 promotedTupleT i = return (PromotedTupleT i)
627
628 promotedNilT :: TypeQ
629 promotedNilT = return PromotedNilT
630
631 promotedConsT :: TypeQ
632 promotedConsT = return PromotedConsT
633
634 isStrict, notStrict, unpacked :: Q Strict
635 isStrict = return $ IsStrict
636 notStrict = return $ NotStrict
637 unpacked = return Unpacked
638
639 strictType :: Q Strict -> TypeQ -> StrictTypeQ
640 strictType = liftM2 (,)
641
642 varStrictType :: Name -> StrictTypeQ -> VarStrictTypeQ
643 varStrictType v st = do (s, t) <- st
644 return (v, s, t)
645
646 -- * Type Literals
647
648 numTyLit :: Integer -> TyLitQ
649 numTyLit n = if n >= 0 then return (NumTyLit n)
650 else fail ("Negative type-level number: " ++ show n)
651
652 strTyLit :: String -> TyLitQ
653 strTyLit s = return (StrTyLit s)
654
655
656
657 -------------------------------------------------------------------------------
658 -- * Kind
659
660 plainTV :: Name -> TyVarBndr
661 plainTV = PlainTV
662
663 kindedTV :: Name -> Kind -> TyVarBndr
664 kindedTV = KindedTV
665
666 varK :: Name -> Kind
667 varK = VarT
668
669 conK :: Name -> Kind
670 conK = ConT
671
672 tupleK :: Int -> Kind
673 tupleK = TupleT
674
675 arrowK :: Kind
676 arrowK = ArrowT
677
678 listK :: Kind
679 listK = ListT
680
681 appK :: Kind -> Kind -> Kind
682 appK = AppT
683
684 starK :: Kind
685 starK = StarT
686
687 constraintK :: Kind
688 constraintK = ConstraintT
689
690 -------------------------------------------------------------------------------
691 -- * Type family result
692
693 noSig :: FamilyResultSig
694 noSig = NoSig
695
696 kindSig :: Kind -> FamilyResultSig
697 kindSig = KindSig
698
699 tyVarSig :: TyVarBndr -> FamilyResultSig
700 tyVarSig = TyVarSig
701
702 -------------------------------------------------------------------------------
703 -- * Injectivity annotation
704
705 injectivityAnn :: Name -> [Name] -> InjectivityAnn
706 injectivityAnn = TH.InjectivityAnn
707
708 -------------------------------------------------------------------------------
709 -- * Role
710
711 nominalR, representationalR, phantomR, inferR :: Role
712 nominalR = NominalR
713 representationalR = RepresentationalR
714 phantomR = PhantomR
715 inferR = InferR
716
717 -------------------------------------------------------------------------------
718 -- * Callconv
719
720 cCall, stdCall, cApi, prim, javaScript :: Callconv
721 cCall = CCall
722 stdCall = StdCall
723 cApi = CApi
724 prim = Prim
725 javaScript = JavaScript
726
727 -------------------------------------------------------------------------------
728 -- * Safety
729
730 unsafe, safe, interruptible :: Safety
731 unsafe = Unsafe
732 safe = Safe
733 interruptible = Interruptible
734
735 -------------------------------------------------------------------------------
736 -- * FunDep
737
738 funDep :: [Name] -> [Name] -> FunDep
739 funDep = FunDep
740
741 -------------------------------------------------------------------------------
742 -- * FamFlavour
743
744 typeFam, dataFam :: FamFlavour
745 typeFam = TypeFam
746 dataFam = DataFam
747
748 -------------------------------------------------------------------------------
749 -- * RuleBndr
750 ruleVar :: Name -> RuleBndrQ
751 ruleVar = return . RuleVar
752
753 typedRuleVar :: Name -> TypeQ -> RuleBndrQ
754 typedRuleVar n ty = ty >>= return . TypedRuleVar n
755
756 -------------------------------------------------------------------------------
757 -- * AnnTarget
758 valueAnnotation :: Name -> AnnTarget
759 valueAnnotation = ValueAnnotation
760
761 typeAnnotation :: Name -> AnnTarget
762 typeAnnotation = TypeAnnotation
763
764 moduleAnnotation :: AnnTarget
765 moduleAnnotation = ModuleAnnotation
766
767 --------------------------------------------------------------
768 -- * Useful helper function
769
770 appsE :: [ExpQ] -> ExpQ
771 appsE [] = error "appsE []"
772 appsE [x] = x
773 appsE (x:y:zs) = appsE ( (appE x y) : zs )
774
775 -- | Return the Module at the place of splicing. Can be used as an
776 -- input for 'reifyModule'.
777 thisModule :: Q Module
778 thisModule = do
779 loc <- location
780 return $ Module (mkPkgName $ loc_package loc) (mkModName $ loc_module loc)