@@ -30,10 +30,14 @@ mkFirstAgda pats body = AgdaMatch pats body
30
30
-- | Transform an 'AgdaMatch' whose body is a case over a bound pattern, by
31
31
-- splitting it into multiple matches: one for each alternative of the case.
32
32
agdaSplit :: AgdaMatch -> [AgdaMatch ]
33
- agdaSplit (AgdaMatch pats (Case (HsVar _ (L _ var)) matches)) = do
34
- (pat, body) <- matches
35
- -- TODO(sandy): use an at pattern if necessary
36
- pure $ AgdaMatch (rewriteVarPat var pat pats) $ unLoc body
33
+ agdaSplit (AgdaMatch pats (Case (HsVar _ (L _ var)) matches))
34
+ -- Ensure the thing we're destructing is actually a pattern that's been
35
+ -- bound.
36
+ | containsVar var pats
37
+ = do
38
+ (pat, body) <- matches
39
+ -- TODO(sandy): use an at pattern if necessary
40
+ pure $ AgdaMatch (rewriteVarPat var pat pats) $ unLoc body
37
41
agdaSplit x = [x]
38
42
39
43
@@ -53,6 +57,19 @@ wildifyT (S.map occNameString -> used) = everywhere $ mkT $ \case
53
57
(x :: Pat GhcPs ) -> x
54
58
55
59
60
+ ------------------------------------------------------------------------------
61
+ -- | Determine whether the given 'RdrName' exists as a 'VarPat' inside of @a@.
62
+ containsVar :: Data a => RdrName -> a -> Bool
63
+ containsVar name = everything (||) $
64
+ mkQ False (\ case
65
+ VarPat _ (L _ var) -> eqRdrName name var
66
+ (_ :: Pat GhcPs ) -> False
67
+ )
68
+ `extQ` \ case
69
+ HsRecField lbl _ True -> eqRdrName name $ unLoc $ rdrNameFieldOcc $ unLoc lbl
70
+ (_ :: HsRecField' (FieldOcc GhcPs ) (PatCompat GhcPs )) -> False
71
+
72
+
56
73
------------------------------------------------------------------------------
57
74
-- | Replace a 'VarPat' with the given @'Pat' GhcPs@.
58
75
rewriteVarPat :: Data a => RdrName -> Pat GhcPs -> a -> a
@@ -68,7 +85,6 @@ rewriteVarPat name rep = everywhere $
68
85
(x :: HsRecField' (FieldOcc GhcPs ) (PatCompat GhcPs )) -> x
69
86
70
87
71
-
72
88
------------------------------------------------------------------------------
73
89
-- | Construct an 'HsDecl' from a set of 'AgdaMatch'es.
74
90
splitToDecl
0 commit comments