Make Pred a type synonym of Type (issue #7021)
[packages/template-haskell.git] / 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 module Language.Haskell.TH.Lib where
6 -- All of the exports from this module should
7 -- be "public" functions. The main module TH
8 -- re-exports them all.
9
10 import Language.Haskell.TH.Syntax hiding (Role)
11 import qualified Language.Haskell.TH.Syntax as TH
12 import Control.Monad( liftM, liftM2 )
13 import Data.Word( Word8 )
14
15 ----------------------------------------------------------
16 -- * Type synonyms
17 ----------------------------------------------------------
18
19 type InfoQ = Q Info
20 type PatQ = Q Pat
21 type FieldPatQ = Q FieldPat
22 type ExpQ = Q Exp
23 type TExpQ a = Q (TExp a)
24 type DecQ = Q Dec
25 type DecsQ = Q [Dec]
26 type ConQ = Q Con
27 type TypeQ = Q Type
28 type TyLitQ = Q TyLit
29 type CxtQ = Q Cxt
30 type PredQ = Q Pred
31 type MatchQ = Q Match
32 type ClauseQ = Q Clause
33 type BodyQ = Q Body
34 type GuardQ = Q Guard
35 type StmtQ = Q Stmt
36 type RangeQ = Q Range
37 type StrictTypeQ = Q StrictType
38 type VarStrictTypeQ = Q VarStrictType
39 type FieldExpQ = Q FieldExp
40 type RuleBndrQ = Q RuleBndr
41 type TySynEqnQ = Q TySynEqn
42 type Role = TH.Role -- must be defined here for DsMeta to find it
43
44 ----------------------------------------------------------
45 -- * Lowercase pattern syntax functions
46 ----------------------------------------------------------
47
48 intPrimL :: Integer -> Lit
49 intPrimL = IntPrimL
50 wordPrimL :: Integer -> Lit
51 wordPrimL = WordPrimL
52 floatPrimL :: Rational -> Lit
53 floatPrimL = FloatPrimL
54 doublePrimL :: Rational -> Lit
55 doublePrimL = DoublePrimL
56 integerL :: Integer -> Lit
57 integerL = IntegerL
58 charL :: Char -> Lit
59 charL = CharL
60 stringL :: String -> Lit
61 stringL = StringL
62 stringPrimL :: [Word8] -> Lit
63 stringPrimL = StringPrimL
64 rationalL :: Rational -> Lit
65 rationalL = RationalL
66
67 litP :: Lit -> PatQ
68 litP l = return (LitP l)
69 varP :: Name -> PatQ
70 varP v = return (VarP v)
71 tupP :: [PatQ] -> PatQ
72 tupP ps = do { ps1 <- sequence ps; return (TupP ps1)}
73 unboxedTupP :: [PatQ] -> PatQ
74 unboxedTupP ps = do { ps1 <- sequence ps; return (UnboxedTupP ps1)}
75 conP :: Name -> [PatQ] -> PatQ
76 conP n ps = do ps' <- sequence ps
77 return (ConP n ps')
78 infixP :: PatQ -> Name -> PatQ -> PatQ
79 infixP p1 n p2 = do p1' <- p1
80 p2' <- p2
81 return (InfixP p1' n p2')
82 uInfixP :: PatQ -> Name -> PatQ -> PatQ
83 uInfixP p1 n p2 = do p1' <- p1
84 p2' <- p2
85 return (UInfixP p1' n p2')
86 parensP :: PatQ -> PatQ
87 parensP p = do p' <- p
88 return (ParensP p')
89
90 tildeP :: PatQ -> PatQ
91 tildeP p = do p' <- p
92 return (TildeP p')
93 bangP :: PatQ -> PatQ
94 bangP p = do p' <- p
95 return (BangP p')
96 asP :: Name -> PatQ -> PatQ
97 asP n p = do p' <- p
98 return (AsP n p')
99 wildP :: PatQ
100 wildP = return WildP
101 recP :: Name -> [FieldPatQ] -> PatQ
102 recP n fps = do fps' <- sequence fps
103 return (RecP n fps')
104 listP :: [PatQ] -> PatQ
105 listP ps = do ps' <- sequence ps
106 return (ListP ps')
107 sigP :: PatQ -> TypeQ -> PatQ
108 sigP p t = do p' <- p
109 t' <- t
110 return (SigP p' t')
111 viewP :: ExpQ -> PatQ -> PatQ
112 viewP e p = do e' <- e
113 p' <- p
114 return (ViewP e' p')
115
116 fieldPat :: Name -> PatQ -> FieldPatQ
117 fieldPat n p = do p' <- p
118 return (n, p')
119
120
121 -------------------------------------------------------------------------------
122 -- * Stmt
123
124 bindS :: PatQ -> ExpQ -> StmtQ
125 bindS p e = liftM2 BindS p e
126
127 letS :: [DecQ] -> StmtQ
128 letS ds = do { ds1 <- sequence ds; return (LetS ds1) }
129
130 noBindS :: ExpQ -> StmtQ
131 noBindS e = do { e1 <- e; return (NoBindS e1) }
132
133 parS :: [[StmtQ]] -> StmtQ
134 parS sss = do { sss1 <- mapM sequence sss; return (ParS sss1) }
135
136 -------------------------------------------------------------------------------
137 -- * Range
138
139 fromR :: ExpQ -> RangeQ
140 fromR x = do { a <- x; return (FromR a) }
141
142 fromThenR :: ExpQ -> ExpQ -> RangeQ
143 fromThenR x y = do { a <- x; b <- y; return (FromThenR a b) }
144
145 fromToR :: ExpQ -> ExpQ -> RangeQ
146 fromToR x y = do { a <- x; b <- y; return (FromToR a b) }
147
148 fromThenToR :: ExpQ -> ExpQ -> ExpQ -> RangeQ
149 fromThenToR x y z = do { a <- x; b <- y; c <- z;
150 return (FromThenToR a b c) }
151 -------------------------------------------------------------------------------
152 -- * Body
153
154 normalB :: ExpQ -> BodyQ
155 normalB e = do { e1 <- e; return (NormalB e1) }
156
157 guardedB :: [Q (Guard,Exp)] -> BodyQ
158 guardedB ges = do { ges' <- sequence ges; return (GuardedB ges') }
159
160 -------------------------------------------------------------------------------
161 -- * Guard
162
163 normalG :: ExpQ -> GuardQ
164 normalG e = do { e1 <- e; return (NormalG e1) }
165
166 normalGE :: ExpQ -> ExpQ -> Q (Guard, Exp)
167 normalGE g e = do { g1 <- g; e1 <- e; return (NormalG g1, e1) }
168
169 patG :: [StmtQ] -> GuardQ
170 patG ss = do { ss' <- sequence ss; return (PatG ss') }
171
172 patGE :: [StmtQ] -> ExpQ -> Q (Guard, Exp)
173 patGE ss e = do { ss' <- sequence ss;
174 e' <- e;
175 return (PatG ss', e') }
176
177 -------------------------------------------------------------------------------
178 -- * Match and Clause
179
180 -- | Use with 'caseE'
181 match :: PatQ -> BodyQ -> [DecQ] -> MatchQ
182 match p rhs ds = do { p' <- p;
183 r' <- rhs;
184 ds' <- sequence ds;
185 return (Match p' r' ds') }
186
187 -- | Use with 'funD'
188 clause :: [PatQ] -> BodyQ -> [DecQ] -> ClauseQ
189 clause ps r ds = do { ps' <- sequence ps;
190 r' <- r;
191 ds' <- sequence ds;
192 return (Clause ps' r' ds') }
193
194
195 ---------------------------------------------------------------------------
196 -- * Exp
197
198 -- | Dynamically binding a variable (unhygenic)
199 dyn :: String -> ExpQ
200 dyn s = return (VarE (mkName s))
201
202 global :: Name -> ExpQ
203 {-# DEPRECATED global "Use varE instead" #-}
204 -- Trac #8656; I have no idea why this function is duplicated
205 global s = return (VarE 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 -- ** 'arithSeqE' Shortcuts
300 fromE :: ExpQ -> ExpQ
301 fromE x = do { a <- x; return (ArithSeqE (FromR a)) }
302
303 fromThenE :: ExpQ -> ExpQ -> ExpQ
304 fromThenE x y = do { a <- x; b <- y; return (ArithSeqE (FromThenR a b)) }
305
306 fromToE :: ExpQ -> ExpQ -> ExpQ
307 fromToE x y = do { a <- x; b <- y; return (ArithSeqE (FromToR a b)) }
308
309 fromThenToE :: ExpQ -> ExpQ -> ExpQ -> ExpQ
310 fromThenToE x y z = do { a <- x; b <- y; c <- z;
311 return (ArithSeqE (FromThenToR a b c)) }
312
313
314 -------------------------------------------------------------------------------
315 -- * Dec
316
317 valD :: PatQ -> BodyQ -> [DecQ] -> DecQ
318 valD p b ds =
319 do { p' <- p
320 ; ds' <- sequence ds
321 ; b' <- b
322 ; return (ValD p' b' ds')
323 }
324
325 funD :: Name -> [ClauseQ] -> DecQ
326 funD nm cs =
327 do { cs1 <- sequence cs
328 ; return (FunD nm cs1)
329 }
330
331 tySynD :: Name -> [TyVarBndr] -> TypeQ -> DecQ
332 tySynD tc tvs rhs = do { rhs1 <- rhs; return (TySynD tc tvs rhs1) }
333
334 dataD :: CxtQ -> Name -> [TyVarBndr] -> [ConQ] -> [Name] -> DecQ
335 dataD ctxt tc tvs cons derivs =
336 do
337 ctxt1 <- ctxt
338 cons1 <- sequence cons
339 return (DataD ctxt1 tc tvs cons1 derivs)
340
341 newtypeD :: CxtQ -> Name -> [TyVarBndr] -> ConQ -> [Name] -> DecQ
342 newtypeD ctxt tc tvs con derivs =
343 do
344 ctxt1 <- ctxt
345 con1 <- con
346 return (NewtypeD ctxt1 tc tvs con1 derivs)
347
348 classD :: CxtQ -> Name -> [TyVarBndr] -> [FunDep] -> [DecQ] -> DecQ
349 classD ctxt cls tvs fds decs =
350 do
351 decs1 <- sequence decs
352 ctxt1 <- ctxt
353 return $ ClassD ctxt1 cls tvs fds decs1
354
355 instanceD :: CxtQ -> TypeQ -> [DecQ] -> DecQ
356 instanceD ctxt ty decs =
357 do
358 ctxt1 <- ctxt
359 decs1 <- sequence decs
360 ty1 <- ty
361 return $ InstanceD ctxt1 ty1 decs1
362
363 sigD :: Name -> TypeQ -> DecQ
364 sigD fun ty = liftM (SigD fun) $ ty
365
366 forImpD :: Callconv -> Safety -> String -> Name -> TypeQ -> DecQ
367 forImpD cc s str n ty
368 = do ty' <- ty
369 return $ ForeignD (ImportF cc s str n ty')
370
371 infixLD :: Int -> Name -> DecQ
372 infixLD prec nm = return (InfixD (Fixity prec InfixL) nm)
373
374 infixRD :: Int -> Name -> DecQ
375 infixRD prec nm = return (InfixD (Fixity prec InfixR) nm)
376
377 infixND :: Int -> Name -> DecQ
378 infixND prec nm = return (InfixD (Fixity prec InfixN) nm)
379
380 pragInlD :: Name -> Inline -> RuleMatch -> Phases -> DecQ
381 pragInlD name inline rm phases
382 = return $ PragmaD $ InlineP name inline rm phases
383
384 pragSpecD :: Name -> TypeQ -> Phases -> DecQ
385 pragSpecD n ty phases
386 = do
387 ty1 <- ty
388 return $ PragmaD $ SpecialiseP n ty1 Nothing phases
389
390 pragSpecInlD :: Name -> TypeQ -> Inline -> Phases -> DecQ
391 pragSpecInlD n ty inline phases
392 = do
393 ty1 <- ty
394 return $ PragmaD $ SpecialiseP n ty1 (Just inline) phases
395
396 pragSpecInstD :: TypeQ -> DecQ
397 pragSpecInstD ty
398 = do
399 ty1 <- ty
400 return $ PragmaD $ SpecialiseInstP ty1
401
402 pragRuleD :: String -> [RuleBndrQ] -> ExpQ -> ExpQ -> Phases -> DecQ
403 pragRuleD n bndrs lhs rhs phases
404 = do
405 bndrs1 <- sequence bndrs
406 lhs1 <- lhs
407 rhs1 <- rhs
408 return $ PragmaD $ RuleP n bndrs1 lhs1 rhs1 phases
409
410 pragAnnD :: AnnTarget -> ExpQ -> DecQ
411 pragAnnD target expr
412 = do
413 exp1 <- expr
414 return $ PragmaD $ AnnP target exp1
415
416 familyNoKindD :: FamFlavour -> Name -> [TyVarBndr] -> DecQ
417 familyNoKindD flav tc tvs = return $ FamilyD flav tc tvs Nothing
418
419 familyKindD :: FamFlavour -> Name -> [TyVarBndr] -> Kind -> DecQ
420 familyKindD flav tc tvs k = return $ FamilyD flav tc tvs (Just k)
421
422 dataInstD :: CxtQ -> Name -> [TypeQ] -> [ConQ] -> [Name] -> DecQ
423 dataInstD ctxt tc tys cons derivs =
424 do
425 ctxt1 <- ctxt
426 tys1 <- sequence tys
427 cons1 <- sequence cons
428 return (DataInstD ctxt1 tc tys1 cons1 derivs)
429
430 newtypeInstD :: CxtQ -> Name -> [TypeQ] -> ConQ -> [Name] -> DecQ
431 newtypeInstD ctxt tc tys con derivs =
432 do
433 ctxt1 <- ctxt
434 tys1 <- sequence tys
435 con1 <- con
436 return (NewtypeInstD ctxt1 tc tys1 con1 derivs)
437
438 tySynInstD :: Name -> TySynEqnQ -> DecQ
439 tySynInstD tc eqn =
440 do
441 eqn1 <- eqn
442 return (TySynInstD tc eqn1)
443
444 closedTypeFamilyNoKindD :: Name -> [TyVarBndr] -> [TySynEqnQ] -> DecQ
445 closedTypeFamilyNoKindD tc tvs eqns =
446 do
447 eqns1 <- sequence eqns
448 return (ClosedTypeFamilyD tc tvs Nothing eqns1)
449
450 closedTypeFamilyKindD :: Name -> [TyVarBndr] -> Kind -> [TySynEqnQ] -> DecQ
451 closedTypeFamilyKindD tc tvs kind eqns =
452 do
453 eqns1 <- sequence eqns
454 return (ClosedTypeFamilyD tc tvs (Just kind) eqns1)
455
456 roleAnnotD :: Name -> [Role] -> DecQ
457 roleAnnotD name roles = return $ RoleAnnotD name roles
458
459 tySynEqn :: [TypeQ] -> TypeQ -> TySynEqnQ
460 tySynEqn lhs rhs =
461 do
462 lhs1 <- sequence lhs
463 rhs1 <- rhs
464 return (TySynEqn lhs1 rhs1)
465
466 cxt :: [PredQ] -> CxtQ
467 cxt = sequence
468
469 normalC :: Name -> [StrictTypeQ] -> ConQ
470 normalC con strtys = liftM (NormalC con) $ sequence strtys
471
472 recC :: Name -> [VarStrictTypeQ] -> ConQ
473 recC con varstrtys = liftM (RecC con) $ sequence varstrtys
474
475 infixC :: Q (Strict, Type) -> Name -> Q (Strict, Type) -> ConQ
476 infixC st1 con st2 = do st1' <- st1
477 st2' <- st2
478 return $ InfixC st1' con st2'
479
480 forallC :: [TyVarBndr] -> CxtQ -> ConQ -> ConQ
481 forallC ns ctxt con = liftM2 (ForallC ns) ctxt con
482
483
484 -------------------------------------------------------------------------------
485 -- * Type
486
487 forallT :: [TyVarBndr] -> CxtQ -> TypeQ -> TypeQ
488 forallT tvars ctxt ty = do
489 ctxt1 <- ctxt
490 ty1 <- ty
491 return $ ForallT tvars ctxt1 ty1
492
493 varT :: Name -> TypeQ
494 varT = return . VarT
495
496 conT :: Name -> TypeQ
497 conT = return . ConT
498
499 appT :: TypeQ -> TypeQ -> TypeQ
500 appT t1 t2 = do
501 t1' <- t1
502 t2' <- t2
503 return $ AppT t1' t2'
504
505 arrowT :: TypeQ
506 arrowT = return ArrowT
507
508 listT :: TypeQ
509 listT = return ListT
510
511 litT :: TyLitQ -> TypeQ
512 litT l = fmap LitT l
513
514 tupleT :: Int -> TypeQ
515 tupleT i = return (TupleT i)
516
517 unboxedTupleT :: Int -> TypeQ
518 unboxedTupleT i = return (UnboxedTupleT i)
519
520 sigT :: TypeQ -> Kind -> TypeQ
521 sigT t k
522 = do
523 t' <- t
524 return $ SigT t' k
525
526 equalityT :: TypeQ -> TypeQ -> TypeQ
527 equalityT tleft tright
528 = do
529 tleft1 <- tleft
530 tright1 <- tright
531 let typ = AppT (AppT EqualityT tleft1) tright1
532 return typ
533
534 promotedT :: Name -> TypeQ
535 promotedT = return . PromotedT
536
537 promotedTupleT :: Int -> TypeQ
538 promotedTupleT i = return (PromotedTupleT i)
539
540 promotedNilT :: TypeQ
541 promotedNilT = return PromotedNilT
542
543 promotedConsT :: TypeQ
544 promotedConsT = return PromotedConsT
545
546 isStrict, notStrict, unpacked :: Q Strict
547 isStrict = return $ IsStrict
548 notStrict = return $ NotStrict
549 unpacked = return Unpacked
550
551 strictType :: Q Strict -> TypeQ -> StrictTypeQ
552 strictType = liftM2 (,)
553
554 varStrictType :: Name -> StrictTypeQ -> VarStrictTypeQ
555 varStrictType v st = do (s, t) <- st
556 return (v, s, t)
557
558 -- * Type Literals
559
560 numTyLit :: Integer -> TyLitQ
561 numTyLit n = if n >= 0 then return (NumTyLit n)
562 else fail ("Negative type-level number: " ++ show n)
563
564 strTyLit :: String -> TyLitQ
565 strTyLit s = return (StrTyLit s)
566
567
568
569 -------------------------------------------------------------------------------
570 -- * Kind
571
572 plainTV :: Name -> TyVarBndr
573 plainTV = PlainTV
574
575 kindedTV :: Name -> Kind -> TyVarBndr
576 kindedTV = KindedTV
577
578 varK :: Name -> Kind
579 varK = VarT
580
581 conK :: Name -> Kind
582 conK = ConT
583
584 tupleK :: Int -> Kind
585 tupleK = TupleT
586
587 arrowK :: Kind
588 arrowK = ArrowT
589
590 listK :: Kind
591 listK = ListT
592
593 appK :: Kind -> Kind -> Kind
594 appK = AppT
595
596 starK :: Kind
597 starK = StarT
598
599 constraintK :: Kind
600 constraintK = ConstraintT
601
602 -------------------------------------------------------------------------------
603 -- * Role
604
605 nominalR, representationalR, phantomR, inferR :: Role
606 nominalR = NominalR
607 representationalR = RepresentationalR
608 phantomR = PhantomR
609 inferR = InferR
610
611 -------------------------------------------------------------------------------
612 -- * Callconv
613
614 cCall, stdCall :: Callconv
615 cCall = CCall
616 stdCall = StdCall
617
618 -------------------------------------------------------------------------------
619 -- * Safety
620
621 unsafe, safe, interruptible :: Safety
622 unsafe = Unsafe
623 safe = Safe
624 interruptible = Interruptible
625
626 -------------------------------------------------------------------------------
627 -- * FunDep
628
629 funDep :: [Name] -> [Name] -> FunDep
630 funDep = FunDep
631
632 -------------------------------------------------------------------------------
633 -- * FamFlavour
634
635 typeFam, dataFam :: FamFlavour
636 typeFam = TypeFam
637 dataFam = DataFam
638
639 -------------------------------------------------------------------------------
640 -- * RuleBndr
641 ruleVar :: Name -> RuleBndrQ
642 ruleVar = return . RuleVar
643
644 typedRuleVar :: Name -> TypeQ -> RuleBndrQ
645 typedRuleVar n ty = ty >>= return . TypedRuleVar n
646
647 --------------------------------------------------------------
648 -- * Useful helper function
649
650 appsE :: [ExpQ] -> ExpQ
651 appsE [] = error "appsE []"
652 appsE [x] = x
653 appsE (x:y:zs) = appsE ( (appE x y) : zs )
654
655 -- | Return the Module at the place of splicing. Can be used as an
656 -- input for 'reifyModule'.
657 thisModule :: Q Module
658 thisModule = do
659 loc <- location
660 return $ Module (mkPkgName $ loc_package loc) (mkModName $ loc_module loc)