]> Git — Sourcephile - haskell/symantic-parser.git/blob - src/Language/Haskell/TH/HideName.hs
machine: remove horizonStack and experiment with alwaysRaise
[haskell/symantic-parser.git] / src / Language / Haskell / TH / HideName.hs
1 {-# LANGUAGE AllowAmbiguousTypes #-} -- For hideableShow
2 -- | This module enables to 'hideName'
3 -- to get reproductible dumps of TemplateHaskell slices.
4 module Language.Haskell.TH.HideName where
5
6 import Data.Bool (Bool(..))
7 import Data.Function (id)
8 import Data.Functor ((<$>))
9 import Language.Haskell.TH
10 import Language.Haskell.TH.Syntax
11 import Prelude (undefined)
12
13 -- ** Type 'HideableName'
14 -- | Useful on golden unit tests because 'Name's
15 -- change often when changing unrelated source code
16 -- or even when changing basic GHC or executable flags.
17 class HideableName (showNames::Bool) where
18 hideableName :: HideName a => a -> a
19 -- | Like 'id'.
20 instance HideableName 'True where
21 hideableName = id
22 -- | Like 'hideName'.
23 instance HideableName 'False where
24 hideableName = hideName
25
26 -- * Class 'HideName'
27 class HideName a where
28 -- | Map all 'Name's to a constant in order to overcome
29 -- cases where resetting 'TH.counter' is not enough
30 -- to get deterministic 'TH.Name's.
31 hideName :: a -> a
32 instance HideName Body where
33 hideName (GuardedB gs) = GuardedB ((\(g, e) -> (hideName g, hideName e)) <$> gs)
34 hideName (NormalB e) = NormalB (hideName e)
35 instance HideName Clause where
36 hideName (Clause ps b ds) = Clause (hideName <$> ps) (hideName b) (hideName <$> ds)
37 instance HideName Dec where
38 hideName (FunD f cs) = FunD (hideName f) (hideName <$> cs)
39 hideName (ValD p r ds) = ValD (hideName p) (hideName r) (hideName <$> ds)
40 -- Other alternatives are not used by Symantic.Parser, hence don't bother.
41 hideName _ = undefined
42 instance HideName Exp where
43 hideName (AppE e1 e2) = AppE (hideName e1) (hideName e2)
44 hideName (AppTypeE e t) = AppTypeE (hideName e) (hideName t)
45 hideName (ArithSeqE d) = ArithSeqE (hideName d)
46 hideName (CaseE e ms) = CaseE (hideName e) (hideName <$> ms)
47 hideName (CompE ss) = CompE (hideName <$> ss)
48 hideName (ConE c) = ConE (hideName c)
49 hideName (CondE guard true false) = CondE (hideName guard) (hideName true) (hideName false)
50 hideName (DoE m ss) = DoE (hideName <$> m) (hideName <$> ss)
51 hideName (ImplicitParamVarE n) = ImplicitParamVarE n
52 hideName (InfixE e1 op e2) = InfixE (hideName <$> e1) (hideName op) (hideName <$> e2)
53 hideName (LabelE s) = LabelE s
54 hideName (LamCaseE ms) = LamCaseE (hideName <$> ms)
55 hideName (LamE ps e) = LamE (hideName <$> ps) (hideName e)
56 hideName (LetE ds e) = LetE (hideName <$> ds) (hideName e)
57 hideName (ListE es) = ListE (hideName <$> es)
58 hideName (LitE l) = LitE l
59 hideName (MDoE m ss) = MDoE (hideName <$> m) (hideName <$> ss)
60 hideName (MultiIfE alts) = MultiIfE ((\(g, e) -> (hideName g, hideName e)) <$> alts)
61 hideName (ParensE e) = ParensE (hideName e)
62 hideName (RecConE nm fs) = RecConE (hideName nm) ((\(n, e) -> (hideName n, hideName e)) <$> fs)
63 hideName (RecUpdE e fs) = RecUpdE (hideName e) ((\(n, ee) -> (hideName n, hideName ee)) <$> fs)
64 hideName (SigE e t) = SigE (hideName e) (hideName t)
65 hideName (StaticE e) = StaticE (hideName e)
66 hideName (TupE es) = TupE ((hideName <$>) <$> es)
67 hideName (UInfixE e1 op e2) = UInfixE (hideName e1) (hideName op) (hideName e2)
68 hideName (UnboundVarE v) = UnboundVarE (hideName v)
69 hideName (UnboxedSumE e alt arity) = UnboxedSumE (hideName e) alt arity
70 hideName (UnboxedTupE es) = UnboxedTupE ((hideName <$>) <$> es)
71 hideName (VarE v) = VarE (hideName v)
72 instance HideName Guard where
73 hideName (NormalG e) = NormalG (hideName e)
74 hideName (PatG ss) = PatG (hideName <$> ss)
75 instance HideName Lit where
76 hideName x = x
77 instance HideName Match where
78 hideName (Match p b ds) = Match (hideName p) (hideName b) (hideName <$> ds)
79 instance HideName ModName where
80 hideName (ModName n) = ModName n
81 instance HideName Name where
82 -- This is the hidding
83 hideName (Name (OccName on) (NameU _u)) = Name (OccName on) NameS
84 hideName (Name (OccName on) (NameL _u)) = Name (OccName on) NameS
85 hideName (Name on n) = Name (hideName on) (hideName n)
86 instance HideName NameFlavour where
87 hideName (NameG n p m) = NameG n p m
88 hideName (NameL n) = NameL n
89 hideName (NameQ n) = NameQ n
90 hideName NameS = NameS
91 hideName (NameU n) = NameU n
92 instance HideName OccName where
93 hideName (OccName n) = OccName n
94 instance HideName Range where
95 hideName (FromR e) = FromR (hideName e)
96 hideName (FromThenR f t) = FromThenR (hideName f) (hideName t)
97 hideName (FromToR f t) = FromToR (hideName f) (hideName t)
98 hideName (FromThenToR f t to) = FromThenToR (hideName f) (hideName t) (hideName to)
99 instance HideName Stmt where
100 hideName (BindS p e) = BindS (hideName p) (hideName e)
101 hideName (LetS ds) = LetS (hideName <$> ds)
102 hideName (NoBindS e) = NoBindS (hideName e)
103 hideName (ParS ss) = ParS ((hideName <$>) <$> ss)
104 hideName (RecS ss) = RecS (hideName <$> ss)
105 instance HideName (TyVarBndr f) where
106 hideName (PlainTV n f) = PlainTV (hideName n) f
107 hideName (KindedTV n f k) = KindedTV (hideName n) f (hideName k)
108 instance HideName Type where
109 hideName (ForallT vs ctx t) = ForallT (hideName <$> vs) (hideName <$> ctx) (hideName t)
110 hideName (ForallVisT vs t) = ForallVisT (hideName <$> vs) (hideName t)
111 hideName (AppT t x) = AppT (hideName t) (hideName x)
112 hideName (AppKindT t k) = AppKindT (hideName t) (hideName k)
113 hideName (SigT t k) = SigT (hideName t) (hideName k)
114 hideName (VarT n) = VarT (hideName n)
115 hideName (ConT n) = ConT (hideName n)
116 hideName (PromotedT n) = PromotedT (hideName n)
117 hideName (InfixT x n y) = InfixT (hideName x) (hideName n) (hideName y)
118 hideName (UInfixT x n y) = UInfixT (hideName x) (hideName n) (hideName y)
119 hideName (ParensT t) = ParensT (hideName t)
120 hideName (TupleT x) = TupleT x
121 hideName (UnboxedTupleT x) = UnboxedTupleT x
122 hideName (UnboxedSumT x) = UnboxedSumT x
123 hideName (ArrowT) = ArrowT
124 hideName (MulArrowT) = MulArrowT
125 hideName (EqualityT) = EqualityT
126 hideName (ListT) = ListT
127 hideName (PromotedTupleT x) = PromotedTupleT x
128 hideName (PromotedNilT) = PromotedNilT
129 hideName (PromotedConsT) = PromotedConsT
130 hideName (StarT) = StarT
131 hideName (ConstraintT) = ConstraintT
132 hideName (LitT t) = LitT t
133 hideName (WildCardT) = WildCardT
134 hideName (ImplicitParamT n t) = ImplicitParamT n (hideName t)
135 instance HideName Pat where
136 hideName (AsP v p) = AsP (hideName v) (hideName p)
137 hideName (BangP p) = BangP (hideName p)
138 hideName (ConP s ps) = ConP (hideName s) (hideName <$> ps)
139 hideName (InfixP p1 n p2) = InfixP (hideName p1) (hideName n) (hideName p2)
140 hideName (ListP ps) = ListP (hideName <$> ps)
141 hideName (LitP l) = LitP (hideName l)
142 hideName (ParensP p) = ParensP (hideName p)
143 hideName (RecP nm fs) = RecP (nm) ((\(n,p) -> (hideName n, hideName p)) <$> fs)
144 hideName (SigP p t) = SigP (hideName p) (hideName t)
145 hideName (TildeP p) = TildeP (hideName p)
146 hideName (TupP ps) = TupP (hideName <$> ps)
147 hideName (UInfixP p1 n p2) = UInfixP (hideName p1) (hideName n) (hideName p2)
148 hideName (UnboxedSumP p alt arity) = UnboxedSumP (hideName p) alt arity
149 hideName (UnboxedTupP ps) = UnboxedTupP (hideName <$> ps)
150 hideName (VarP v) = VarP (hideName v)
151 hideName (ViewP e p) = ViewP (hideName e) (hideName p)
152 hideName WildP = WildP
153 instance HideName a => HideName [a] where
154 hideName = (hideName <$>)