Skip to content

Commit d68d1ea

Browse files
committed
Simplify replaceAllTypeVars and handle some more edge cases
1 parent d410497 commit d68d1ea

File tree

1 file changed

+21
-21
lines changed

1 file changed

+21
-21
lines changed

src/Language/PureScript/Types.hs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -132,29 +132,29 @@ replaceTypeVars v r = replaceAllTypeVars [(v, r)]
132132
-- Replace named type variables with types
133133
--
134134
replaceAllTypeVars :: [(String, Type)] -> Type -> Type
135-
replaceAllTypeVars = replaceAllTypeVars' []
135+
replaceAllTypeVars = go []
136136
where
137-
replaceAllTypeVars' bound m = go bound
138-
where
139-
go :: [String] -> Type -> Type
140-
go _ (TypeVar v) =
141-
case v `lookup` m of
142-
Just r -> r
143-
Nothing -> TypeVar v
144-
go bs (TypeApp t1 t2) = TypeApp (go bs t1) (go bs t2)
145-
go bs (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs) ts
146-
go bs f@(ForAll v t sco) | v `elem` keys = f
137+
138+
go :: [String] -> [(String, Type)] -> Type -> Type
139+
go _ m (TypeVar v) =
140+
case v `lookup` m of
141+
Just r -> r
142+
Nothing -> TypeVar v
143+
go bs m (TypeApp t1 t2) = TypeApp (go bs m t1) (go bs m t2)
144+
go bs m (SaturatedTypeSynonym name' ts) = SaturatedTypeSynonym name' $ map (go bs m) ts
145+
go bs m f@(ForAll v t sco) | v `elem` keys = go bs (filter ((/= v) . fst) m) f
147146
| v `elem` usedVars =
148-
let v' = genName v (keys ++ bs ++ usedVars)
149-
t' = replaceAllTypeVars' bs [(v, TypeVar v')] t
150-
in ForAll v' (go (v' : bs) t') sco
151-
| otherwise = ForAll v (go (v : bs) t) sco
152-
where
153-
keys = map fst m
154-
usedVars = concatMap (usedTypeVariables . snd) m
155-
go bs (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs)) cs) (go bs t)
156-
go bs (RCons name' t r) = RCons name' (go bs t) (go bs r)
157-
go _ ty = ty
147+
let v' = genName v (keys ++ bs ++ usedVars)
148+
t' = go bs [(v, TypeVar v')] t
149+
in ForAll v' (go (v' : bs) m t') sco
150+
| otherwise = ForAll v (go (v : bs) m t) sco
151+
where
152+
keys = map fst m
153+
usedVars = concatMap (usedTypeVariables . snd) m
154+
go bs m (ConstrainedType cs t) = ConstrainedType (map (second $ map (go bs m)) cs) (go bs m t)
155+
go bs m (RCons name' t r) = RCons name' (go bs m t) (go bs m r)
156+
go _ _ ty = ty
157+
158158
genName orig inUse = try 0
159159
where
160160
try :: Integer -> String

0 commit comments

Comments
 (0)