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