1module Type.Inference where
3import Control.Arrow (first, second)
4import Control.Monad.Except (Except, forM, liftIO, runExceptT, throwError)
5import qualified Data.Map as Map
6import qualified Data.Traversable as Traverse
8import AST.Module as Module
9import qualified AST.Type as Type
10import qualified AST.Variable as Var
11import qualified Reporting.Annotation as A
12import qualified Reporting.Error.Type as Error
13import qualified Type.Constrain.Expression as TcExpr
14import qualified Type.Environment as Env
15import qualified Type.Solve as Solve
16import qualified Type.State as TS
17import qualified Type.Type as T
19import System.IO.Unsafe
20 -- Maybe possible to switch over to ST instead of IO.
21 -- I don't think that'd be worthwhile though.
27 -> Except [A.Located Error.Error] (Map.Map String Type.Canonical)
28infer interfaces modul =
29 either throwError return $ unsafePerformIO $ runExceptT $
30 do (header, constraint) <-
31 liftIO (genConstraints interfaces modul)
33 state <- Solve.solve constraint
35 let header' = Map.delete "::" header
36 let types = Map.difference (TS.sSavedEnv state) header'
38 liftIO (Traverse.traverse T.toSrcType types)
44 -> IO (Env.TypeDict, T.TypeConstraint)
45genConstraints interfaces modul =
47 Env.initialEnvironment (canonicalizeAdts interfaces modul)
50 forM (Map.keys (Env.constructor env)) $ \name ->
51 do (_, vars, args, result) <- Env.freshDataScheme env name
52 return (name, (vars, foldr (T.==>) result args))
55 mapM (canonicalizeValues env) (Map.toList interfaces)
57 let allTypes = concat (ctors : importedVars)
58 let vars = concatMap (fst . snd) allTypes
59 let header = Map.map snd (Map.fromList allTypes)
60 let environ = T.CLet [ T.Scheme vars [] T.CTrue header ]
62 fvar <- T.variable T.Flexible
65 TcExpr.constrain env (program (body modul)) (T.varN fvar)
67 return (header, environ constraint)
72 -> (Module.Name, Interface)
73 -> IO [(String, ([T.Variable], T.Type))]
74canonicalizeValues env (moduleName, iface) =
75 forM (Map.toList (iTypes iface)) $ \(name,tipe) ->
76 do tipe' <- Env.instantiateType env tipe Map.empty
77 return (Module.nameToString moduleName ++ "." ++ name, tipe')
80canonicalizeAdts :: Interfaces -> CanonicalModule -> [CanonicalAdt]
81canonicalizeAdts interfaces modul =
82 localAdts ++ importedAdts
84 localAdts :: [CanonicalAdt]
85 localAdts = format (Module.names modul, datatypes (body modul))
87 importedAdts :: [CanonicalAdt]
88 importedAdts = concatMap (format . second iAdts) (Map.toList interfaces)
90 format :: (Module.Name, Module.ADTs) -> [CanonicalAdt]
92 map canonical (Map.toList adts)
94 canonical :: (String, AdtInfo String) -> CanonicalAdt
95 canonical (name, (tvars, ctors)) =
97 , (tvars, map (first toVar) ctors)
100 toVar :: String -> Var.Canonical
101 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