1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
7 , get, getType, freshDataScheme, ctorNames
13import Control.Monad (foldM)
14import qualified Control.Monad.State as State
15import qualified Data.List as List
16import qualified Data.Map as Map
17import qualified Data.Set as Set
18import Data.Text (Text)
20import qualified AST.Helpers as Help
21import qualified AST.Type as T
22import qualified AST.Variable as Var
23import qualified AST.Module as Module
33 { _ctor :: Dict Instantiator
40 IO (Int, [Variable], [Type], Type)
47initialize :: [Module.CanonicalUnion] -> IO Env
49 do types <- makeTypes unions
56 return $ env { _ctor = makeCtors env unions }
63makeTypes :: [Module.CanonicalUnion] -> IO (Dict Type)
65 do builtinDict <- Map.fromList <$> mapM makeBuiltin builtinTypes
66 foldM addImported builtinDict unions
69addImported :: Dict Type -> (Var.Canonical, unionInfo) -> IO (Dict Type)
70addImported dict ( var, _ ) =
72 return (Map.insert (Var.toText var) (VarN atom) dict)
75makeBuiltin :: (Text, Int) -> IO (Text, Type)
76makeBuiltin (name, _) =
77 do atom <- mkAtom (Var.builtin name)
78 return (name, VarN atom)
81builtinTypes :: [(Text, Int)]
86 , kind 0 ["Int","Float","Char","String","Bool"]
89 tuple size = ( Help.makeTuple size, size )
90 kind n names = map (\name -> (name, n)) names
97makeCtors :: Env -> [Module.CanonicalUnion] -> Dict Instantiator
101 getType env "List" <| t
103 inst :: Int -> ([Type] -> ([Type], Type)) -> Instantiator
105 do vars <- mapM (\_ -> mkVar Nothing) [1..numTVars]
106 let (args, result) = tipe (map (VarN) vars)
107 return (length args, vars, args, result)
114 (name, inst size $ \vs -> (vs, foldl (<|) (getType env name) vs))
116 builtins :: Dict Instantiator
119 [ ("[]", inst 1 $ \ [t] -> ([], list t))
120 , ("::", inst 1 $ \ [t] -> ([t, list t], list t))
122 ++ map tupleCtor [0..9]
124 List.foldl' (makeCtorsHelp env) builtins unions
128 Map.Map Text Variable
131makeCtorsHelp :: Env -> Dict Instantiator -> Module.CanonicalUnion -> Dict Instantiator
132makeCtorsHelp env constructorDict ( var, (rawTypeVars, ctors) ) =
134 inst :: Dict Instantiator -> (Var.Canonical, [T.Canonical]) -> Dict Instantiator
135 inst ctorDict (name, rawArgs) =
138 ( do ((args, tipe), dict) <- State.runStateT (go rawArgs) Map.empty
139 return ( length args, Map.elems dict, args, tipe )
144 map T.Var rawTypeVars
146 go :: [T.Canonical] -> State.StateT VarDict IO ([Type], Type)
148 do types <- mapM (instantiator Flex env) args
149 returnType <- instantiator Flex env (T.App (T.Type var) tvars)
150 return (types, returnType)
152 List.foldl' inst constructorDict ctors
160get :: Dict a -> Text -> a
165getType :: Env -> Text -> Type
170freshDataScheme :: Env -> Text -> Instantiator
171freshDataScheme env var =
175ctorNames :: Env -> [Text]
184addValues :: VarDict -> Env -> Env
185addValues newValues env =
186 env { _value = Map.union (_value env) (Map.map VarN newValues) }
193instantiateType :: Flex -> Env -> T.Canonical -> IO (Type, VarDict)
194instantiateType flex env sourceType =
195 State.runStateT (instantiator flex env sourceType) Map.empty
198instantiator :: Flex -> Env -> T.Canonical -> State.StateT VarDict IO Type
199instantiator flex env sourceType =
200 instantiatorHelp flex env Set.empty sourceType
203instantiatorHelp :: Flex -> Env -> Set.Set Text -> T.Canonical -> State.StateT VarDict IO Type
204instantiatorHelp flex env aliasVars sourceType =
207 instantiatorHelp flex env aliasVars
211 (==>) <$> go t1 <*> go t2
214 if Set.member name aliasVars then
215 return (PlaceHolder name)
219 case Map.lookup name (_value env) of
224 case Map.lookup name dict of
226 return (VarN variable)
229 do variable <- State.liftIO (mkNamedVar flex name)
230 State.put (Map.insert name variable dict)
231 return (VarN variable)
233 T.Aliased name args aliasType ->
234 do targs <- traverse (traverse go) args
238 instantiatorHelp flex env Set.empty tipe
241 instantiatorHelp flex env (Set.fromList (map fst args)) tipe
243 return (AliasN name targs realType)
246 return (getType env (Var.toText name))
250 targs <- traverse go args
251 return $ foldl (<|) tfunc targs
253 T.Record fields ext ->
254 do tfields <- traverse go (Map.fromList fields)
258 return $ TermN EmptyRecord1
263 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