2013-07-15 22:38:31 +00:00
|
|
|
module Type.Inference where
|
|
|
|
|
|
|
|
import qualified Data.Map as Map
|
|
|
|
|
|
|
|
import qualified Type.Type as T
|
|
|
|
import qualified Type.Environment as Env
|
|
|
|
import qualified Type.Constrain.Expression as TcExpr
|
|
|
|
import qualified Type.Constrain.Declaration as TcDecl
|
|
|
|
import qualified Type.Solve as Solve
|
|
|
|
|
2013-07-26 10:56:36 +00:00
|
|
|
import SourceSyntax.Module as Module
|
2013-07-16 19:42:37 +00:00
|
|
|
import qualified SourceSyntax.Expression as Expr
|
2013-08-03 18:41:33 +00:00
|
|
|
import SourceSyntax.Location (Located, noneNoDocs)
|
2013-07-19 16:02:24 +00:00
|
|
|
import SourceSyntax.PrettyPrint
|
2013-07-15 22:38:31 +00:00
|
|
|
import Text.PrettyPrint
|
|
|
|
import qualified Type.State as TS
|
2013-08-20 00:47:37 +00:00
|
|
|
import Type.ExtraChecks (extraChecks)
|
2013-07-15 22:38:31 +00:00
|
|
|
import Control.Monad.State
|
2013-07-17 17:29:27 +00:00
|
|
|
import Control.Arrow (second)
|
2013-07-15 22:38:31 +00:00
|
|
|
import Transform.SortDefinitions as Sort
|
|
|
|
|
2013-07-16 12:52:50 +00:00
|
|
|
import System.IO.Unsafe -- Possible to switch over to the ST monad instead of
|
2013-07-29 09:53:45 +00:00
|
|
|
-- the IO monad. I don't think that'd be worthwhile.
|
2013-07-15 22:38:31 +00:00
|
|
|
|
2013-07-17 17:29:27 +00:00
|
|
|
|
2013-07-21 20:50:48 +00:00
|
|
|
infer :: Interfaces -> MetadataModule t v -> Either [Doc] (Map.Map String T.Variable)
|
2013-07-29 09:53:45 +00:00
|
|
|
infer interfaces modul = unsafePerformIO $ do
|
2013-07-26 15:35:48 +00:00
|
|
|
env <- Env.initialEnvironment
|
|
|
|
(datatypes modul ++ concatMap iAdts (Map.elems interfaces))
|
|
|
|
(aliases modul ++ concatMap iAliases (Map.elems interfaces))
|
2013-07-17 17:29:27 +00:00
|
|
|
ctors <- forM (Map.keys (Env.constructor env)) $ \name ->
|
2013-07-23 13:32:08 +00:00
|
|
|
do (_, vars, args, result) <- Env.freshDataScheme env name
|
|
|
|
return (name, (vars, foldr (T.==>) result args))
|
2013-07-17 17:29:27 +00:00
|
|
|
|
2013-07-21 20:50:48 +00:00
|
|
|
importedVars <-
|
2013-07-26 10:56:36 +00:00
|
|
|
forM (concatMap (Map.toList . iTypes) $ Map.elems interfaces) $ \(name,tipe) ->
|
2013-07-26 10:22:15 +00:00
|
|
|
(,) name `fmap` Env.instantiateType env tipe Map.empty
|
2013-07-21 20:50:48 +00:00
|
|
|
|
|
|
|
let allTypes = ctors ++ importedVars
|
|
|
|
vars = concatMap (fst . snd) allTypes
|
|
|
|
header = Map.map snd (Map.fromList allTypes)
|
2013-08-03 18:41:33 +00:00
|
|
|
environ = noneNoDocs . T.CLet [ T.Scheme vars [] (noneNoDocs T.CTrue) header ]
|
2013-07-23 13:32:08 +00:00
|
|
|
|
2013-07-25 16:07:04 +00:00
|
|
|
fvar <- T.var T.Flexible
|
|
|
|
constraint <- environ `fmap` TcExpr.constrain env (program modul) (T.VarN fvar)
|
2013-07-23 13:32:08 +00:00
|
|
|
|
2013-07-19 16:02:24 +00:00
|
|
|
state <- execStateT (Solve.solve constraint) TS.initialState
|
|
|
|
let errors = TS.sErrors state
|
2013-07-15 22:38:31 +00:00
|
|
|
if null errors
|
2013-08-20 05:39:41 +00:00
|
|
|
then extraChecks $ Map.difference (TS.sSavedEnv state) header
|
2013-07-26 22:53:34 +00:00
|
|
|
else Left `fmap` sequence (reverse errors)
|
2013-07-15 22:38:31 +00:00
|
|
|
|