@@ -1,5 +1,25 @@
11{-# OPTIONS_GHC -Wall #-}
2−module Type.State where
2+module Type.State
3+ ( Solver
4+ , run
5+ , SolverState(..)
6+ , Pool(..)
7+ , Env
8+ , modifyEnv
9+ , addError
10+ , switchToPool
11+ , getPool
12+ , getEnv
13+ , saveLocalEnv
14+ , uniqueMark
15+ , nextRankPool
16+ , register
17+ , introduce
18+ , flatten
19+ , makeInstance
20+ , makeCopy
21+ )
22+ where
323
424import qualified Control.Monad.State as State
525import qualified Data.Map as Map
@@ -13,33 +33,28 @@ import qualified Reporting.Region as R
1333import Type.Type
1434
1535
16−-- Pool
17−-- Holds a bunch of variables
18−-- The rank of each variable is less than or equal to the pool's "maxRank"
19−-- The young pool exists to make it possible to identify these vars in constant time.
36+-- SOLVER
2037
21−data Pool =
22− Pool
23− { maxRank :: Int
24− , inhabitants :: [Variable]
25− }
2638
39+type Solver = State.StateT SolverState IO
2740
28−emptyPool :: Pool
29−emptyPool =
30− Pool
31− { maxRank = outermostRank
32− , inhabitants = []
33− }
3441
42+run :: Solver () -> IO SolverState
43+run solver =
44+ State.execStateT solver $
45+ SS
46+ { sEnv = Map.empty
47+ , sSavedEnv = Map.empty
48+ , sPool = Pool outermostRank []
49+ , sMark = noMark + 1 -- The mark must never be equal to noMark!
50+ , sError = []
51+ }
3552
36−type Env = Map.Map Text (A.Located Variable)
3753
3854
39−type Solver = State.StateT SolverState IO
55+-- SOLVER STATE
4056
4157
42−-- Keeps track of the environment, type variable pool, and a list of errors
4358data SolverState =
4459 SS
4560 { sEnv :: Env
@@ -50,17 +65,24 @@ data SolverState =
5065 }
5166
5267
53−initialState :: SolverState
54−initialState =
55− SS
56− { sEnv = Map.empty
57− , sSavedEnv = Map.empty
58− , sPool = emptyPool
59− , sMark = noMark + 1 -- The mark must never be equal to noMark!
60− , sError = []
68+{-| A pool holds a bunch of variables
69+The rank of each variable is less than or equal to the pool's "maxRank"
70+The young pool exists to make it possible to identify these vars in constant time.
71+-}
72+data Pool =
73+ Pool
74+ { maxRank :: Int
75+ , inhabitants :: [Variable]
6176 }
6277
6378
79+type Env = Map.Map Text (A.Located Variable)
80+
81+
82+
83+-- HELPERS
84+
85+
6486modifyEnv :: (Env -> Env) -> Solver ()
6587modifyEnv f =
6688 State.modify $ \state -> state { sEnv = f (sEnv state) }