Fix problem with variable shadowing when instantiating aliased types

This commit is contained in:
Evan Czaplicki 2013-08-08 15:45:22 -07:00
parent c70d59fdf9
commit 143547e766

View file

@ -122,12 +122,10 @@ instantiator env sourceType = go sourceType
Src.Var x -> do Src.Var x -> do
(dict, aliases) <- State.get (dict, aliases) <- State.get
case Map.lookup x dict of case (Map.lookup x dict, Map.lookup x aliases) of
Just var -> return (VarN var) (_, Just t) -> return t
Nothing -> (Just v, _) -> return (VarN v)
case Map.lookup x aliases of _ ->
Just t -> return t
Nothing ->
do var <- State.liftIO $ namedVar flex x do var <- State.liftIO $ namedVar flex x
State.put (Map.insert x var dict, aliases) State.put (Map.insert x var dict, aliases)
return (VarN var) return (VarN var)
@ -142,20 +140,22 @@ instantiator env sourceType = go sourceType
Src.Data name ts -> do Src.Data name ts -> do
ts' <- mapM go ts ts' <- mapM go ts
case Map.lookup name (types env) of case (Map.lookup name (types env), Map.lookup name (aliases env)) of
Just t -> return $ foldl (<|) t ts' (Just t, _) -> return $ foldl (<|) t ts'
Nothing -> (_, Just (tvars, t)) ->
case Map.lookup name (aliases env) of
Nothing -> error $ "\nCould not find type constructor '" ++ name ++ "' while checking types."
Just (tvars, t) ->
let tvarLen = length tvars let tvarLen = length tvars
msg = "\nType alias '" ++ name ++ "' expects " ++ show tvarLen ++ msg = "\nType alias '" ++ name ++ "' expects " ++ show tvarLen ++
" type argument" ++ (if tvarLen == 1 then "" else "s") ++ " type argument" ++ (if tvarLen == 1 then "" else "s") ++
" but was given " ++ show (length ts') " but was given " ++ show (length ts')
in if length ts' /= length tvars then error msg else in if length ts' /= length tvars then error msg else
do (dict, aliases) <- State.get do (dict, aliases) <- State.get
State.put (dict, Map.union aliases . Map.fromList $ zip tvars ts') let aliases' = Map.union (Map.fromList $ zip tvars ts') aliases
go t State.put (dict, aliases')
t' <- go t
State.put (dict, aliases)
return t'
_ -> error $ "\nCould not find type constructor '" ++
name ++ "' while checking types."
Src.EmptyRecord -> return (TermN EmptyRecord1) Src.EmptyRecord -> return (TermN EmptyRecord1)