on master help sign in
Move `boundVars` function into SourceSyntax.Pattern
commit3c65b5c69dd72177e42b2442f3915cf964c05ec4 authorEvan Czaplicki <info@elm-lang.org> dateThu 2 Jan 2014, 23:23 (13 years ago) parente3b8ea7
2 files changed+16−8
compiler/SourceSyntax/Pattern.hs+11 compiler/Transform/Substitute.hs+5−8
compiler/SourceSyntax/Pattern.hs+11
@@ -4,6 +4,7 @@ import Data.List (intercalate)
44import SourceSyntax.Helpers as Help
55import SourceSyntax.PrettyPrint
66import Text.PrettyPrint as PP
7+import qualified Data.Set as Set
78import SourceSyntax.Literal as Literal
89
910data Pattern = PData String [Pattern]
@@ -19,6 +20,16 @@ nil = PData "[]" []
1920list = foldr cons nil
2021tuple es = PData ("_Tuple" ++ show (length es)) es
2122
23+boundVars :: Pattern -> Set.Set String
24+boundVars pattern =
25+ case pattern of
26+ PVar x -> Set.singleton x
27+ PAlias x p -> Set.insert x (boundVars p)
28+ PData _ ps -> Set.unions (map boundVars ps)
29+ PRecord fields -> Set.fromList fields
30+ PAnything -> Set.empty
31+ PLiteral _ -> Set.empty
32+
2233
2334instance Pretty Pattern where
2435 pretty pattern =
compiler/Transform/Substitute.hs+5−8
@@ -4,10 +4,10 @@ module Transform.Substitute (subst) where
44import Control.Arrow (second, (***))
55import SourceSyntax.Expression
66import SourceSyntax.Location
7+import qualified SourceSyntax.Pattern as Pattern
78import qualified Data.Set as Set
8−import qualified Transform.SortDefinitions as SD
99
10−subst :: String -> Expr t v -> Expr t v -> Expr t v
10+subst :: String -> Expr -> Expr -> Expr
1111subst old new expr =
1212 let f (L s e) = L s (subst old new e) in
1313 case expr of
@@ -15,7 +15,7 @@ subst old new expr =
1515 ExplicitList es -> ExplicitList (map f es)
1616 Binop op e1 e2 -> Binop op (f e1) (f e2)
1717 Lambda p e
18− | Set.member old (SD.boundVars p) -> expr
18+ | Set.member old (Pattern.boundVars p) -> expr
1919 | otherwise -> Lambda p (f e)
2020 App e1 e2 -> App (f e1) (f e2)
2121 MultiIf ps -> MultiIf (map (f *** f) ps)
@@ -24,12 +24,9 @@ subst old new expr =
2424 | anyShadow -> expr
2525 | otherwise -> Let (map substDef defs) (f body)
2626 where
27+ substDef (Definition p e t) = Definition p (f e) t
2728 anyShadow =
28− any (Set.member old . SD.boundVars) [ p | Def p _ <- defs ]
29− substDef def =
30− case def of
31− TypeAnnotation _ _ -> def
32− Def p e -> Def p (f e)
29+ any (Set.member old . Pattern.boundVars) [ p | Definition p _ _ <- defs ]
3330
3431 Var x -> if x == old then new else expr
3532 Case e cases -> Case (f e) $ map (second f) cases