b7a88d6ec6e5b5c3b8dc37165d136e92a5ea3d80
[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 classP :: Name -> [TypeQ] -> PredQ
470 classP cla tys
471 = do
472 tys1 <- sequence tys
473 return (ClassP cla tys1)
474
475 equalP :: TypeQ -> TypeQ -> PredQ
476 equalP tleft tright
477 = do
478 tleft1 <- tleft
479 tright1 <- tright
480 return (EqualP tleft1 tright1)
481
482 normalC :: Name -> [StrictTypeQ] -> ConQ
483 normalC con strtys = liftM (NormalC con) $ sequence strtys
484
485 recC :: Name -> [VarStrictTypeQ] -> ConQ
486 recC con varstrtys = liftM (RecC con) $ sequence varstrtys
487
488 infixC :: Q (Strict, Type) -> Name -> Q (Strict, Type) -> ConQ
489 infixC st1 con st2 = do st1' <- st1
490 st2' <- st2
491 return $ InfixC st1' con st2'
492
493 forallC :: [TyVarBndr] -> CxtQ -> ConQ -> ConQ
494 forallC ns ctxt con = liftM2 (ForallC ns) ctxt con
495
496
497 -------------------------------------------------------------------------------
498 -- * Type
499
500 forallT :: [TyVarBndr] -> CxtQ -> TypeQ -> TypeQ
501 forallT tvars ctxt ty = do
502 ctxt1 <- ctxt
503 ty1 <- ty
504 return $ ForallT tvars ctxt1 ty1
505
506 varT :: Name -> TypeQ
507 varT = return . VarT
508
509 conT :: Name -> TypeQ
510 conT = return . ConT
511
512 appT :: TypeQ -> TypeQ -> TypeQ
513 appT t1 t2 = do
514 t1' <- t1
515 t2' <- t2
516 return $ AppT t1' t2'
517
518 arrowT :: TypeQ
519 arrowT = return ArrowT
520
521 listT :: TypeQ
522 listT = return ListT
523
524 litT :: TyLitQ -> TypeQ
525 litT l = fmap LitT l
526
527 tupleT :: Int -> TypeQ
528 tupleT i = return (TupleT i)
529
530 unboxedTupleT :: Int -> TypeQ
531 unboxedTupleT i = return (UnboxedTupleT i)
532
533 sigT :: TypeQ -> Kind -> TypeQ
534 sigT t k
535 = do
536 t' <- t
537 return $ SigT t' k
538
539 promotedT :: Name -> TypeQ
540 promotedT = return . PromotedT
541
542 promotedTupleT :: Int -> TypeQ
543 promotedTupleT i = return (PromotedTupleT i)
544
545 promotedNilT :: TypeQ
546 promotedNilT = return PromotedNilT
547
548 promotedConsT :: TypeQ
549 promotedConsT = return PromotedConsT
550
551 isStrict, notStrict, unpacked :: Q Strict
552 isStrict = return $ IsStrict
553 notStrict = return $ NotStrict
554 unpacked = return Unpacked
555
556 strictType :: Q Strict -> TypeQ -> StrictTypeQ
557 strictType = liftM2 (,)
558
559 varStrictType :: Name -> StrictTypeQ -> VarStrictTypeQ
560 varStrictType v st = do (s, t) <- st
561 return (v, s, t)
562
563 -- * Type Literals
564
565 numTyLit :: Integer -> TyLitQ
566 numTyLit n = if n >= 0 then return (NumTyLit n)
567 else fail ("Negative type-level number: " ++ show n)
568
569 strTyLit :: String -> TyLitQ
570 strTyLit s = return (StrTyLit s)
571
572
573
574 -------------------------------------------------------------------------------
575 -- * Kind
576
577 plainTV :: Name -> TyVarBndr
578 plainTV = PlainTV
579
580 kindedTV :: Name -> Kind -> TyVarBndr
581 kindedTV = KindedTV
582
583 varK :: Name -> Kind
584 varK = VarT
585
586 conK :: Name -> Kind
587 conK = ConT
588
589 tupleK :: Int -> Kind
590 tupleK = TupleT
591
592 arrowK :: Kind
593 arrowK = ArrowT
594
595 listK :: Kind
596 listK = ListT
597
598 appK :: Kind -> Kind -> Kind
599 appK = AppT
600
601 starK :: Kind
602 starK = StarT
603
604 constraintK :: Kind
605 constraintK = ConstraintT
606
607 -------------------------------------------------------------------------------
608 -- * Role
609
610 nominalR, representationalR, phantomR, inferR :: Role
611 nominalR = NominalR
612 representationalR = RepresentationalR
613 phantomR = PhantomR
614 inferR = InferR
615
616 -------------------------------------------------------------------------------
617 -- * Callconv
618
619 cCall, stdCall :: Callconv
620 cCall = CCall
621 stdCall = StdCall
622
623 -------------------------------------------------------------------------------
624 -- * Safety
625
626 unsafe, safe, interruptible :: Safety
627 unsafe = Unsafe
628 safe = Safe
629 interruptible = Interruptible
630
631 -------------------------------------------------------------------------------
632 -- * FunDep
633
634 funDep :: [Name] -> [Name] -> FunDep
635 funDep = FunDep
636
637 -------------------------------------------------------------------------------
638 -- * FamFlavour
639
640 typeFam, dataFam :: FamFlavour
641 typeFam = TypeFam
642 dataFam = DataFam
643
644 -------------------------------------------------------------------------------
645 -- * RuleBndr
646 ruleVar :: Name -> RuleBndrQ
647 ruleVar = return . RuleVar
648
649 typedRuleVar :: Name -> TypeQ -> RuleBndrQ
650 typedRuleVar n ty = ty >>= return . TypedRuleVar n
651
652 --------------------------------------------------------------
653 -- * Useful helper function
654
655 appsE :: [ExpQ] -> ExpQ
656 appsE [] = error "appsE []"
657 appsE [x] = x
658 appsE (x:y:zs) = appsE ( (appE x y) : zs )
659
660 -- | Return the Module at the place of splicing. Can be used as an
661 -- input for 'reifyModule'.
662 thisModule :: Q Module
663 thisModule = do
664 loc <- location
665 return $ Module (mkPkgName $ loc_package loc) (mkModName $ loc_module loc)