2module Type.Inference where
4import qualified Data.Map as Map
6import qualified Type.Type as T
7import qualified Type.Environment as Env
8import qualified Type.Constrain.Expression as TcExpr
9import qualified Type.Solve as Solve
11import AST.Module as Module
12import AST.Annotation (noneNoDocs)
13import AST.Type (CanonicalType)
14import qualified AST.Variable as Var
15import Text.PrettyPrint
16import qualified Type.State as TS
17import qualified Type.ExtraChecks as Check
18import Control.Arrow (first, second)
19import Control.Monad.State (execStateT, forM)
20import Control.Monad.Error (ErrorT, runErrorT, liftIO)
22import System.IO.Unsafe -- Possible to switch over to the ST monad instead of
23 -- the IO monad. I don't think that'd be worthwhile.
26infer :: Interfaces -> CanonicalModule -> Either [Doc] (Map.Map String CanonicalType)
27infer interfaces modul = unsafePerformIO $ do
28 let moduleBody = body modul
30 env <- Env.initialEnvironment (canonicalizeAdts interfaces modul)
31 ctors <- forM (Map.keys (Env.constructor env)) $ \name ->
32 do (_, vars, args, result) <- Env.freshDataScheme env name
33 return (name, (vars, foldr (T.==>) result args))
35 attemptConstraint <- runErrorT $ do
36 importedVars <- mapM (canonicalizeValues env) (Map.toList interfaces)
38 let allTypes = concat (ctors : importedVars)
39 vars = concatMap (fst . snd) allTypes
40 header = Map.map snd (Map.fromList allTypes)
41 environ = noneNoDocs . T.CLet [ T.Scheme vars [] (noneNoDocs T.CTrue) header ]
43 fvar <- liftIO $ T.variable T.Flexible
44 c <- TcExpr.constrain env (program moduleBody) (T.varN fvar)
45 return (header, environ c)
47 case attemptConstraint of
48 Left err -> return $ Left err
49 Right (header, constraint) -> do
50 state <- execStateT (Solve.solve constraint) TS.initialState
51 case TS.sErrors state of
52 errors@(_:_) -> return (Left errors)
53 [] -> case Check.portTypes (program moduleBody) of
54 Left err -> return (Left err)
55 Right () -> Check.mainType types
57 header' = Map.delete "::" header
58 types = Map.difference (TS.sSavedEnv state) header'
60canonicalizeValues :: Env.Environment -> (String, Interface)
61 -> ErrorT [Doc] IO [(String, ([T.Variable], T.Type))]
62canonicalizeValues env (moduleName, iface) =
63 forM (Map.toList (iTypes iface)) $ \(name,tipe) -> do
64 tipe' <- Env.instantiateType env tipe Map.empty
65 return (moduleName ++ "." ++ name, tipe')
67canonicalizeAdts :: Interfaces -> CanonicalModule -> [CanonicalAdt]
68canonicalizeAdts interfaces modul =
69 localAdts ++ importedAdts
71 localAdts :: [CanonicalAdt]
72 localAdts = format (Module.getName modul, datatypes (body modul))
74 importedAdts :: [CanonicalAdt]
75 importedAdts = concatMap (format . second iAdts) (Map.toList interfaces)
77 format :: (String, Module.ADTs) -> [CanonicalAdt]
79 map canonical (Map.toList adts)
81 canonical :: (String, AdtInfo String) -> CanonicalAdt
82 canonical (name, (tvars, ctors)) =
84 , (tvars, map (first toVar) ctors)
87 toVar :: String -> Var.Canonical
88 toVar = Var.Canonical (Var.Module home)
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