Move boundVars
function into SourceSyntax.Pattern
This commit is contained in:
parent
e3b8ea7d09
commit
3c65b5c69d
2 changed files with 16 additions and 8 deletions
|
@ -4,6 +4,7 @@ import Data.List (intercalate)
|
|||
import SourceSyntax.Helpers as Help
|
||||
import SourceSyntax.PrettyPrint
|
||||
import Text.PrettyPrint as PP
|
||||
import qualified Data.Set as Set
|
||||
import SourceSyntax.Literal as Literal
|
||||
|
||||
data Pattern = PData String [Pattern]
|
||||
|
@ -19,6 +20,16 @@ nil = PData "[]" []
|
|||
list = foldr cons nil
|
||||
tuple es = PData ("_Tuple" ++ show (length es)) es
|
||||
|
||||
boundVars :: Pattern -> Set.Set String
|
||||
boundVars pattern =
|
||||
case pattern of
|
||||
PVar x -> Set.singleton x
|
||||
PAlias x p -> Set.insert x (boundVars p)
|
||||
PData _ ps -> Set.unions (map boundVars ps)
|
||||
PRecord fields -> Set.fromList fields
|
||||
PAnything -> Set.empty
|
||||
PLiteral _ -> Set.empty
|
||||
|
||||
|
||||
instance Pretty Pattern where
|
||||
pretty pattern =
|
||||
|
|
|
@ -4,10 +4,10 @@ module Transform.Substitute (subst) where
|
|||
import Control.Arrow (second, (***))
|
||||
import SourceSyntax.Expression
|
||||
import SourceSyntax.Location
|
||||
import qualified SourceSyntax.Pattern as Pattern
|
||||
import qualified Data.Set as Set
|
||||
import qualified Transform.SortDefinitions as SD
|
||||
|
||||
subst :: String -> Expr t v -> Expr t v -> Expr t v
|
||||
subst :: String -> Expr -> Expr -> Expr
|
||||
subst old new expr =
|
||||
let f (L s e) = L s (subst old new e) in
|
||||
case expr of
|
||||
|
@ -15,7 +15,7 @@ subst old new expr =
|
|||
ExplicitList es -> ExplicitList (map f es)
|
||||
Binop op e1 e2 -> Binop op (f e1) (f e2)
|
||||
Lambda p e
|
||||
| Set.member old (SD.boundVars p) -> expr
|
||||
| Set.member old (Pattern.boundVars p) -> expr
|
||||
| otherwise -> Lambda p (f e)
|
||||
App e1 e2 -> App (f e1) (f e2)
|
||||
MultiIf ps -> MultiIf (map (f *** f) ps)
|
||||
|
@ -24,12 +24,9 @@ subst old new expr =
|
|||
| anyShadow -> expr
|
||||
| otherwise -> Let (map substDef defs) (f body)
|
||||
where
|
||||
substDef (Definition p e t) = Definition p (f e) t
|
||||
anyShadow =
|
||||
any (Set.member old . SD.boundVars) [ p | Def p _ <- defs ]
|
||||
substDef def =
|
||||
case def of
|
||||
TypeAnnotation _ _ -> def
|
||||
Def p e -> Def p (f e)
|
||||
any (Set.member old . Pattern.boundVars) [ p | Definition p _ _ <- defs ]
|
||||
|
||||
Var x -> if x == old then new else expr
|
||||
Case e cases -> Case (f e) $ map (second f) cases
|
||||
|
|
Loading…
Reference in a new issue