on 0cfae2e help sign in
elk/compiler/Type/Inference.hs
30 lines 1.0 kB history raw
1module Type.Inference where
3import qualified Data.Map as Map
5import qualified Type.Type as T
6import qualified Type.Environment as Env
7import qualified Type.Constrain.Expression as TcExpr
8import qualified Type.Constrain.Declaration as TcDecl
9import qualified Type.Solve as Solve
11import SourceSyntax.Module
12import Text.PrettyPrint
13import qualified Type.State as TS
14import Control.Monad.State
15import Transform.SortDefinitions as Sort
17import System.IO.Unsafe -- Possible to switch over to the ST monad instead of
18 -- the IO monad. Not sure if that'd be worthwhile.
20infer :: Module t v -> Either [Doc] (Map.Map String T.Variable)
21infer (Module _ _ _ decls) = unsafePerformIO $ do
22 env <- Env.initialEnvironment
23 var <- T.flexibleVar
24 let expr = sortDefs (TcDecl.toExpr decls)
25 constraint <- TcExpr.constrain env expr (T.VarN var)
26 (env,_,_,errors) <- execStateT (Solve.solve constraint) TS.initialState
27 if null errors
28 then return $ Right env
29 else Left `fmap` sequence errors