@@ -12,21 +12,21 @@ import qualified Data.Map as Map
12import Data.List (isPrefixOf)
12import Data.List (isPrefixOf)
13import qualified Text.PrettyPrint as PP
13import qualified Text.PrettyPrint as PP
15−import AST.Module (ADTs)
16import qualified AST.Type as T
15import qualified AST.Type as T
17import qualified AST.Variable as V
16import qualified AST.Variable as V
17+import AST.Module (CanonicalAdt, AdtInfo)
18import Type.Type
18import Type.Type
20type TypeDict = Map.Map String Type
20type TypeDict = Map.Map String Type
21type VarDict = Map.Map String Variable
21type VarDict = Map.Map String Variable
23−data Environment = Environment {
23+data Environment = Environment
24− constructor :: Map.Map String (IO (Int, [Variable], [Type], Type)),
24+ { constructor :: Map.Map String (IO (Int, [Variable], [Type], Type))
25− types :: TypeDict,
25+ , types :: TypeDict
26− value :: TypeDict
26+ , value :: TypeDict
29−initialEnvironment :: ADTs -> IO Environment
29+initialEnvironment :: [CanonicalAdt] -> IO Environment
30initialEnvironment datatypes = do
30initialEnvironment datatypes = do
31 types <- makeTypes datatypes
31 types <- makeTypes datatypes
32 let env = Environment
32 let env = Environment
@@ -36,27 +36,35 @@ initialEnvironment datatypes = do
37 return $ env { constructor = makeConstructors env datatypes }
37 return $ env { constructor = makeConstructors env datatypes }
39−makeTypes :: ADTs -> IO TypeDict
39+makeTypes :: [CanonicalAdt] -> IO TypeDict
40makeTypes datatypes =
40makeTypes datatypes =
41− do adts <- Traverse.sequence (Map.mapWithKey make datatypes)
41+ do adts <- mapM makeImported datatypes
42− bs <- Map.fromList . zip (map fst builtins) <$> mapM (uncurry make) builtins
42+ bs <- mapM makeBuiltin builtins
43− return (Map.union adts bs)
43+ return (Map.fromList (adts ++ bs))
45+ makeImported :: (V.Canonical, AdtInfo V.Canonical) -> IO (String, Type)
46+ makeImported (var, _) = do
45− make name _ = VarN <$> namedVar Constant name
47+ tvar <- namedVar Constant var
48+ return (V.toString var, VarN tvar)
47− tuple n = ("_Tuple" ++ show n, n)
50+ makeBuiltin :: (String, Int) -> IO (String, Type)
51+ makeBuiltin (name, _) = do
52+ name' <- namedVar Constant (V.builtin name)
53+ return (name, VarN name')
49− kind n names = map (\name -> (name, n)) names
51− builtins :: [(String,Int)]
55+ builtins :: [(String, Int)]
52 builtins = concat [ map tuple [0..9]
56 builtins = concat [ map tuple [0..9]
53 , kind 1 ["_List"]
57 , kind 1 ["_List"]
54 , kind 0 ["Int","Float","Char","String","Bool"]
58 , kind 0 ["Int","Float","Char","String","Bool"]
61+ tuple n = ("_Tuple" ++ show n, n)
62+ kind n names = map (\name -> (name, n)) names
58makeConstructors :: Environment
66makeConstructors :: Environment
59− -> ADTs
67+ -> [CanonicalAdt]
60 -> Map.Map String (IO (Int, [Variable], [Type], Type))
68 -> Map.Map String (IO (Int, [Variable], [Type], Type))
61makeConstructors env datatypes = Map.fromList builtins
69makeConstructors env datatypes = Map.fromList builtins
@@ -76,30 +84,32 @@ makeConstructors env datatypes = Map.fromList builtins
76 builtins = [ ("[]", inst 1 $ \ [t] -> ([], list t))
84 builtins = [ ("[]", inst 1 $ \ [t] -> ([], list t))
77 , ("::", inst 1 $ \ [t] -> ([t, list t], list t))
85 , ("::", inst 1 $ \ [t] -> ([t, list t], list t))
78 ] ++ map tupleCtor [0..9]
86 ] ++ map tupleCtor [0..9]
79− ++ concatMap (ctorToType env) (Map.toList datatypes)
87+ ++ concatMap (ctorToType env) datatypes
90+ctorToType :: Environment
91+ -> (V.Canonical, AdtInfo V.Canonical)
82−--ctorToType :: Environment -> ADT -> [ (String, IO (Int, [Variable], [Type], Type)) ]
92+ -> [(String, IO (Int, [Variable], [Type], Type))]
83ctorToType env (name, (tvars, ctors)) =
93ctorToType env (name, (tvars, ctors)) =
84− zip (map fst ctors) (map inst ctors)
94+ zip (map (V.toString . fst) ctors) (map inst ctors)
86− inst :: (String, [T.CanonicalType]) -> IO (Int, [Variable], [Type], Type)
96+ inst :: (V.Canonical, [T.CanonicalType]) -> IO (Int, [Variable], [Type], Type)
87 inst ctor = do
97 inst ctor = do
88 ((args, tipe), dict) <- State.runStateT (go ctor) Map.empty
98 ((args, tipe), dict) <- State.runStateT (go ctor) Map.empty
89 return (length args, Map.elems dict, args, tipe)
99 return (length args, Map.elems dict, args, tipe)
92− go :: (String, [T.CanonicalType]) -> State.StateT VarDict IO ([Type], Type)
102+ go :: (V.Canonical, [T.CanonicalType]) -> State.StateT VarDict IO ([Type], Type)
93 go (_, args) = do
103 go (_, args) = do
94 types <- mapM (instantiator env) args
104 types <- mapM (instantiator env) args
95− returnType <- instantiator env (T.Data (error "name") (map T.Var tvars))
105+ returnType <- instantiator env (T.App (T.Type name) (map T.Var tvars))
96 return (types, returnType)
106 return (types, returnType)
99get :: Environment -> (Environment -> Map.Map String a) -> String -> a
109get :: Environment -> (Environment -> Map.Map String a) -> String -> a
100get env subDict key = Map.findWithDefault err key (subDict env)
110get env subDict key = Map.findWithDefault err key (subDict env)
102− err = error $ "\nCould not find type constructor '" ++ key ++ "' while checking types."
112+ err = error $ "Could not find type constructor '" ++ key ++ "' while checking types."
105freshDataScheme :: Environment -> String -> IO (Int, [Variable], [Type], Type)
115freshDataScheme :: Environment -> String -> IO (Int, [Variable], [Type], Type)
@@ -128,7 +138,7 @@ instantiator env sourceType = go sourceType
128 case Map.lookup x dict of
138 case Map.lookup x dict of
129 Just v -> return (VarN v)
139 Just v -> return (VarN v)
130 Nothing ->
140 Nothing ->
131− do var <- State.liftIO $ namedVar flex x
141+ do var <- State.liftIO $ namedVar flex (V.local x)
132 State.put (Map.insert x var dict)
142 State.put (Map.insert x var dict)
133 return (VarN var)
143 return (VarN var)
@@ -137,12 +147,18 @@ instantiator env sourceType = go sourceType
137 | "appendable" `isPrefixOf` x = Is Appendable
147 | "appendable" `isPrefixOf` x = Is Appendable
138 | otherwise = Flexible
148 | otherwise = Flexible
140− T.Data name ts -> do
150+ T.Aliased _ t -> go t
153+ case Map.lookup (V.toString var) (types env) of
155+ Nothing -> error $ "Could not find type constructor '" ++
156+ V.toString var ++ "' while checking types."
141 ts' <- mapM go ts
160 ts' <- mapM go ts
142− case Map.lookup (V.toString name) (types env) of
143− Just t -> return $ foldl (<|) t ts'
161+ return $ foldl (<|) t' ts'
144− Nothing -> error $ "\nCould not find type constructor '" ++
145− V.toString name ++ "' while checking types."
147 T.Record fields ext -> do
163 T.Record fields ext -> do
148 fields' <- Traverse.traverse (mapM go) (T.fieldMap fields)
164 fields' <- Traverse.traverse (mapM go) (T.fieldMap fields)