on master help sign in
cosmetic: clean up Type.State quite a bit
commit5a332597f9839cd0ac557611cf409f14769c7759 authorEvan Czaplicki <evancz@users.noreply.github.com> dateThu 11 May 2017, 21:44 (9 years ago) parent6854570
2 files changed+51−31
src/Type/Solve.hs+2−4 src/Type/State.hs+49−27
src/Type/Solve.hs+2−4
@@ -3,8 +3,7 @@
33module Type.Solve (solve) where
44
55import Control.Monad
6−import Control.Monad.State (execStateT, liftIO)
7−import Control.Monad.Except (ExceptT, throwError)
6+import Control.Monad.Except (ExceptT, liftIO, throwError)
87import qualified Data.Foldable as F
98import qualified Data.Map as Map
109import qualified Data.Text as Text
@@ -153,8 +152,7 @@ adjustRankContent youngMark visitedMark groupRank content =
153152
154153solve :: TypeConstraint -> ExceptT [A.Located Error.Error] IO TS.SolverState
155154solve constraint =
156− do state <-
157− liftIO (execStateT (actuallySolve constraint) TS.initialState)
155+ do state <- liftIO (TS.run (actuallySolve constraint))
158156 case TS.sError state of
159157 [] ->
160158 return state
src/Type/State.hs+49−27
@@ -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) }