on master help sign in
Stop creating types for ADT's, use Constant and Flexible in the right place
4dc68acEvan Czaplicki
compiler/Type/Environment.hs+3−19
@@ -33,7 +33,7 @@ makeTypes datatypes =
33 nameAndKind (name, tvars, _) = (name, length tvars)
33 nameAndKind (name, tvars, _) = (name, length tvars)
34
34
35 makeCtor (name, kind) = do
35 makeCtor (name, kind) = do
36− ctor <- VarN <$> namedVar name
36+ ctor <- VarN <$> namedVar Constant name
37 return (name, ctor)
37 return (name, ctor)
38
38
39 tuple n = ("_Tuple" ++ show n, n)
39 tuple n = ("_Tuple" ++ show n, n)
@@ -46,11 +46,8 @@ makeTypes datatypes =
46 , kind 0 ["Int","Float","Char","Bool","Element"]
46 , kind 0 ["Int","Float","Char","Bool","Element"]
47 ]
47 ]
48
48
49−makeConstructors types datatypes =
49+makeConstructors types datatypes = Map.fromList builtins
50− Map.union (Map.fromList builtins) (Map.unions $ map (instances dummyEnv) datatypes)
51 where
50 where
52− dummyEnv = Environment { constructor=Map.empty, types=types, value=Map.empty }
53−
54 list t = (types ! "_List") <| t
51 list t = (types ! "_List") <| t
55 maybe t = (types ! "Maybe") <| t
52 maybe t = (types ! "Maybe") <| t
56
53
@@ -70,7 +67,6 @@ makeConstructors types datatypes =
70 , ("::" , inst 2 1 $ \ [t] -> t ==> list t ==> list t)
67 , ("::" , inst 2 1 $ \ [t] -> t ==> list t ==> list t)
71 ] ++ map tupleCtor [0..9]
68 ] ++ map tupleCtor [0..9]
72
69
73−
74
70
75get :: Environment -> (Environment -> Map.Map String a) -> String -> a
71get :: Environment -> (Environment -> Map.Map String a) -> String -> a
76get env subDict key = Map.findWithDefault err key (subDict env)
72get env subDict key = Map.findWithDefault err key (subDict env)
@@ -82,18 +78,6 @@ freshDataScheme :: Environment -> String -> IO (Int, [Variable], Type)
82freshDataScheme env name = get env constructor name
78freshDataScheme env name = get env constructor name
83
79
84
80
85−instances :: Environment
86− -> (String, [String], [(String, [Src.Type])])
87− -> Map.Map String (IO (Int, [Variable], Type))
88−instances env (name, tvars, ctors) = Map.fromList $ map go ctors
89− where
90− tipe = Src.Data name (map Src.Var tvars)
91−
92− go (ctor, tipes) = (,) ctor $ do
93− let funcType = foldr Src.Lambda tipe tipes
94− (vars, ttype) <- instantiateTypeWithContext env funcType Map.empty
95− return (length tipes, vars, ttype)
96−
97instantiateType :: Environment -> Src.Type -> IO Type
81instantiateType :: Environment -> Src.Type -> IO Type
98instantiateType env sourceType =
82instantiateType env sourceType =
99 snd <$> instantiateTypeWithContext env sourceType Map.empty
83 snd <$> instantiateTypeWithContext env sourceType Map.empty
@@ -116,7 +100,7 @@ instantiateTypeWithContext env sourceType dict =
116 case Map.lookup x dict of
100 case Map.lookup x dict of
117 Just var -> return (VarN var)
101 Just var -> return (VarN var)
118 Nothing -> do
102 Nothing -> do
119− var <- State.liftIO $ namedVar x -- should this be Constant or Flexible?
103+ var <- State.liftIO $ namedVar Flexible x -- should this be Constant or Flexible?
120 State.put (Map.insert x var dict)
104 State.put (Map.insert x var dict)
121 return (VarN var)
105 return (VarN var)
122
106