@@ -21,6 +21,16 @@ boundVars pattern =
2121 PAnything -> Set.empty
2222 PLiteral _ -> Set.empty
2323
24+ctors :: Pattern -> [String]
25+ctors pattern =
26+ case pattern of
27+ PVar x -> []
28+ PAlias x p -> ctors p
29+ PData ctor ps -> ctor : concatMap ctors ps
30+ PRecord fields -> []
31+ PAnything -> []
32+ PLiteral _ -> []
33+
2434free :: String -> State (Set.Set String) ()
2535free x = modify (Set.insert x)
2636
@@ -105,7 +115,9 @@ reorder lexpr@(L a b expr) =
105115 case def of
106116 Def pattern _ -> pattern
107117 TypeAnnotation name _ -> PVar name
108− mapM (bound . boundVars . getPatterns) defs
118+ forM (map getPatterns defs) $ \pattern -> do
119+ bound (boundVars pattern)
120+ mapM free (ctors pattern)
109121
110122 let addDefs ds bod = L a b (Let (concatMap toDefs ds) bod)
111123 where
@@ -128,6 +140,7 @@ bindingReorder :: (Pattern, LExpr t v) -> State (Set.Set String) (Pattern, LExpr
128140bindingReorder (pattern,expr) =
129141 do expr' <- reorder expr
130142 bound (boundVars pattern)
143+ mapM free (ctors pattern)
131144 return (pattern, expr')
132145
133146