1{-# OPTIONS_GHC -Wall #-}
5 , getType, freshDataScheme, ctorNames
11import qualified Control.Monad.State as State
13import qualified Data.Map as Map
14import qualified Data.Set as Set
16import qualified AST.Type as T
17import qualified AST.Variable as V
18import qualified AST.Module as Module
22type TypeDict = Map.Map String Type
23type VarDict = Map.Map String Variable
27 { _constructor :: Map.Map String (IO (Int, [Variable], [Type], Type))
33initialize :: [Module.CanonicalUnion] -> IO Env
35 do types <- makeTypes datatypes
38 { _constructor = Map.empty
42 return $ env { _constructor = makeConstructors env datatypes }
45makeTypes :: [Module.CanonicalUnion] -> IO TypeDict
47 do unions <- mapM makeImported datatypes
48 bs <- mapM makeBuiltin builtins
49 return (Map.fromList (unions ++ bs))
51 makeImported :: (V.Canonical, Module.UnionInfo V.Canonical) -> IO (String, Type)
52 makeImported (name, _) =
53 do tvar <- mkAtom name
54 return (V.toString name, VarN tvar)
56 makeBuiltin :: (String, Int) -> IO (String, Type)
57 makeBuiltin (name, _) =
58 do name' <- mkAtom (V.builtin name)
59 return (name, VarN name')
61 builtins :: [(String, Int)]
66 , kind 0 ["Int","Float","Char","String","Bool"]
69 tuple n = ("_Tuple" ++ show n, n)
70 kind n names = map (\name -> (name, n)) names
75 -> [Module.CanonicalUnion]
76 -> Map.Map String (IO (Int, [Variable], [Type], Type))
77makeConstructors env datatypes =
81 (_types env ! "List") <| t
83 inst :: Int -> ([Type] -> ([Type], Type)) -> IO (Int, [Variable], [Type], Type)
85 do vars <- mapM (\_ -> mkVar Nothing) [1..numTVars]
86 let (args, result) = tipe (map (VarN) vars)
87 return (length args, vars, args, result)
90 let name = "_Tuple" ++ show n
91 in (name, inst n $ \vs -> (vs, foldl (<|) (_types env ! name) vs))
93 builtins :: [ (String, IO (Int, [Variable], [Type], Type)) ]
95 [ ("[]", inst 1 $ \ [t] -> ([], list t))
96 , ("::", inst 1 $ \ [t] -> ([t, list t], list t))
97 ] ++ map tupleCtor [0..9]
98 ++ concatMap (ctorToType env) datatypes
103 -> (V.Canonical, Module.UnionInfo V.Canonical)
104 -> [(String, IO (Int, [Variable], [Type], Type))]
105ctorToType env (name, (tvars, ctors)) =
106 zip (map (V.toString . fst) ctors) (map inst ctors)
108 inst :: (V.Canonical, [T.Canonical]) -> IO (Int, [Variable], [Type], Type)
110 do ((args, tipe), dict) <- State.runStateT (go ctor) Map.empty
111 return (length args, Map.elems dict, args, tipe)
114 go :: (V.Canonical, [T.Canonical]) -> State.StateT VarDict IO ([Type], Type)
116 do types <- mapM (instantiator Flex env) args
117 returnType <- instantiator Flex env (T.App (T.Type name) (map T.Var tvars))
118 return (types, returnType)
125get :: (Env -> Map.Map String a) -> Env -> String -> a
127 Map.findWithDefault (error msg) key (subDict env)
129 msg = "Could not find type constructor `" ++ key ++ "` while checking types."
132getType :: Env -> String -> Type
137freshDataScheme :: Env -> String -> IO (Int, [Variable], [Type], Type)
142ctorNames :: Env -> [String]
144 Map.keys (_constructor env)
151addValues :: VarDict -> Env -> Env
152addValues newValues env =
153 env { _value = Map.union (_value env) (Map.map VarN newValues) }
160instantiateType :: Flex -> Env -> T.Canonical -> IO (Type, VarDict)
161instantiateType flex env sourceType =
162 State.runStateT (instantiator flex env sourceType) Map.empty
165instantiator :: Flex -> Env -> T.Canonical -> State.StateT VarDict IO Type
166instantiator flex env sourceType =
167 instantiatorHelp flex env Set.empty sourceType
170instantiatorHelp :: Flex -> Env -> Set.Set String -> T.Canonical -> State.StateT VarDict IO Type
171instantiatorHelp flex env aliasVars sourceType =
174 instantiatorHelp flex env aliasVars
178 (==>) <$> go t1 <*> go t2
181 if Set.member name aliasVars then
182 return (PlaceHolder name)
186 case Map.lookup name (_value env) of
191 case Map.lookup name dict of
193 return (VarN variable)
196 do variable <- State.liftIO (mkNamedVar flex name)
197 State.put (Map.insert name variable dict)
198 return (VarN variable)
200 T.Aliased name args aliasType ->
201 do targs <- mapM (\(arg,tipe) -> (,) arg <$> go tipe) args
205 instantiatorHelp flex env Set.empty tipe
208 instantiatorHelp flex env (Set.fromList (map fst args)) tipe
210 return (AliasN name targs realType)
213 case Map.lookup (V.toString name) (_types env) of
219 "Could not find type constructor `" ++
220 V.toString name ++ "` while checking types."
224 targs <- mapM go args
225 return $ foldl (<|) tfunc targs
227 T.Record fields ext ->
228 do tfields <- traverse go (Map.fromList fields)
232 return $ TermN EmptyRecord1
237 return $ TermN (Record1 tfields text)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list