on 9cda2b2 help sign in
elk/src/Type/Inference.hs
93 lines 3.4 kB history raw
1{-# OPTIONS_GHC -W #-}
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 qualified AST.Annotation as A
12import AST.Module as Module
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, throwError)
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 $ runErrorT $ do
28 (header, constraint) <- genConstraints interfaces modul
30 state <- liftIO $ execStateT (Solve.solve constraint) TS.initialState
32 () <- case TS.sErrors state of
33 errors@(_:_) -> throwError errors
34 [] -> return ()
36 () <- Check.portTypes (program (body modul))
38 let header' = Map.delete "::" header
39 types = Map.difference (TS.sSavedEnv state) header'
41 Check.mainType types
44genConstraints :: Interfaces -> CanonicalModule
45 -> ErrorT [Doc] IO (Env.TypeDict, T.TypeConstraint)
46genConstraints interfaces modul = do
47 env <- liftIO $ Env.initialEnvironment (canonicalizeAdts interfaces modul)
49 ctors <- forM (Map.keys (Env.constructor env)) $ \name -> do
50 (_, vars, args, result) <- liftIO $ Env.freshDataScheme env name
51 return (name, (vars, foldr (T.==>) result args))
53 importedVars <- mapM (canonicalizeValues env) (Map.toList interfaces)
55 let allTypes = concat (ctors : importedVars)
56 vars = concatMap (fst . snd) allTypes
57 header = Map.map snd (Map.fromList allTypes)
58 environ = A.noneNoDocs . T.CLet [ T.Scheme vars [] (A.noneNoDocs T.CTrue) header ]
60 fvar <- liftIO $ T.variable T.Flexible
61 c <- TcExpr.constrain env (program (body modul)) (T.varN fvar)
62 return (header, environ c)
65canonicalizeValues :: Env.Environment -> (Module.Name, Interface)
66 -> ErrorT [Doc] IO [(String, ([T.Variable], T.Type))]
67canonicalizeValues env (moduleName, iface) =
68 forM (Map.toList (iTypes iface)) $ \(name,tipe) -> do
69 tipe' <- Env.instantiateType env tipe Map.empty
70 return (Module.nameToString moduleName ++ "." ++ name, tipe')
72canonicalizeAdts :: Interfaces -> CanonicalModule -> [CanonicalAdt]
73canonicalizeAdts interfaces modul =
74 localAdts ++ importedAdts
75 where
76 localAdts :: [CanonicalAdt]
77 localAdts = format (Module.names modul, datatypes (body modul))
79 importedAdts :: [CanonicalAdt]
80 importedAdts = concatMap (format . second iAdts) (Map.toList interfaces)
82 format :: (Module.Name, Module.ADTs) -> [CanonicalAdt]
83 format (home, adts) =
84 map canonical (Map.toList adts)
85 where
86 canonical :: (String, AdtInfo String) -> CanonicalAdt
87 canonical (name, (tvars, ctors)) =
88 ( toVar name
89 , (tvars, map (first toVar) ctors)
90 )
92 toVar :: String -> Var.Canonical
93 toVar = Var.Canonical (Var.Module home)