on b540fb4 help sign in
elk/compiler/Type/Inference.hs
41 lines 1.5 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 qualified SourceSyntax.Expression as Expr
13import Text.PrettyPrint
14import qualified Type.State as TS
15import Control.Monad.State
16import Control.Arrow (second)
17import Transform.SortDefinitions as Sort
19import System.IO.Unsafe -- Possible to switch over to the ST monad instead of
20 -- the IO monad. Not sure if that'd be worthwhile.
23infer :: MetadataModule t v -> Either [Doc] (Map.Map String T.Variable)
24infer modul = unsafePerformIO $ do
25 env <- Env.initialEnvironment (datatypes modul)
26 var <- T.flexibleVar
27 ctors <- forM (Map.keys (Env.constructor env)) $ \name ->
28 do (_, vars, tipe) <- Env.freshDataScheme env name
29 return (name, (vars, tipe))
31 let expr = Expr.dummyLet $ defs modul
32 vars = concatMap (fst . snd) ctors
33 header = Map.map snd (Map.fromList ctors)
34 environ = T.CLet [ T.Scheme vars [] T.CTrue header ]
35 constraint <- environ `fmap` TcExpr.constrain env expr (T.VarN var)
36 print =<< T.extraPretty constraint
37 (env,_,_,errors) <- execStateT (Solve.solve constraint) TS.initialState
38 if null errors
39 then return $ Right env
40 else Left `fmap` sequence errors