4import Control.Applicative ((<$>),(<*>), Applicative)
5import Control.Monad.State
6import qualified Data.Map as Map
7import qualified Data.Traversable as Traversable
8import qualified Data.UnionFind.IO as UF
10import qualified SourceSyntax.Annotation as A
11import SourceSyntax.PrettyPrint
12import Text.PrettyPrint as P
13import qualified Type.Alias as Alias
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.
23 inhabitants :: [Variable]
26emptyPool = Pool { maxRank = outermostRank, inhabitants = [] }
28type Env = Map.Map String Variable
30-- Keeps track of the environment, type variable pool, and a list of errors
31data SolverState = SS {
36 sErrors :: [Alias.Rules -> IO P.Doc]
41 sSavedEnv = Map.empty,
43 sMark = noMark + 1, -- The mark must never be equal to noMark!
47modifyEnv f = modify $ \state -> state { sEnv = f (sEnv state) }
48modifyPool f = modify $ \state -> state { sPool = f (sPool state) }
50addError region hint t1 t2 =
51 modify $ \state -> state { sErrors = makeError : sErrors state }
54 let prettiest = pretty . Alias.realias rules
55 t1' <- prettiest <$> toSrcType t1
56 t2' <- prettiest <$> toSrcType t2
58 [ P.text "Type error" <+> pretty region <> P.colon
59 , maybe P.empty P.text hint
61 , P.nest 8 $ A.getRegionDocs region
63 , P.text " Expected Type:" <+> t1'
64 , P.text " Actual Type:" <+> t2'
67switchToPool pool = modifyPool (\_ -> pool)
69getPool :: StateT SolverState IO Pool
70getPool = sPool <$> get
72getEnv :: StateT SolverState IO Env
75saveLocalEnv :: StateT SolverState IO ()
78 modify $ \state -> state { sSavedEnv = env }
80uniqueMark :: StateT SolverState IO Int
83 let mark = sMark state
84 put $ state { sMark = mark + 1 }
87nextRankPool :: StateT SolverState IO Pool
90 return $ Pool { maxRank = maxRank pool + 1, inhabitants = [] }
92register :: Variable -> StateT SolverState IO Variable
94 modifyPool $ \pool -> pool { inhabitants = variable : inhabitants pool }
97introduce :: Variable -> StateT SolverState IO Variable
98introduce variable = do
100 liftIO $ UF.modifyDescriptor variable (\desc -> desc { rank = maxRank pool })
103flatten :: Type -> StateT SolverState IO Variable
108 flatStructure <- traverseTerm flatten t
110 var <- liftIO . UF.fresh $ Descriptor {
111 structure = Just flatStructure,
120makeInstance :: Variable -> StateT SolverState IO Variable
122 alreadyCopied <- uniqueMark
123 freshVar <- makeCopy alreadyCopied var
124 restore alreadyCopied var
127makeCopy :: Int -> Variable -> StateT SolverState IO Variable
128makeCopy alreadyCopied variable = do
129 desc <- liftIO $ UF.descriptor variable
131 () | mark desc == alreadyCopied ->
134 Nothing -> error $ "Error copying type variable. This should be impossible." ++
135 " Please report an error to the github repo!"
137 | rank desc /= noRank || flex desc == Constant ->
142 newVar <- liftIO $ UF.fresh $ Descriptor {
146 flex = case flex desc of
150 name = case flex desc of
156 -- Link the original variable to the new variable. This lets us
157 -- avoid making multiple copies of the variable we are instantiating.
159 -- Need to do this before recursively copying the structure of
160 -- the variable to avoid looping on cyclic terms.
161 liftIO $ UF.modifyDescriptor variable $ \desc ->
162 desc { mark = alreadyCopied, copy = Just newVar }
164 -- Now we recursively copy the structure of the variable.
165 -- We have already marked the variable as copied, so we
166 -- will not repeat this work or crawl this variable again.
167 case structure desc of
168 Nothing -> return newVar
170 newTerm <- traverseTerm (makeCopy alreadyCopied) term
171 liftIO $ UF.modifyDescriptor newVar $ \desc ->
172 desc { structure = Just newTerm }
175restore :: Int -> Variable -> StateT SolverState IO Variable
176restore alreadyCopied variable = do
177 desc <- liftIO $ UF.descriptor variable
178 if mark desc /= alreadyCopied
182 Traversable.traverse (traverseTerm (restore alreadyCopied)) (structure desc)
183 liftIO $ UF.modifyDescriptor variable $ \desc ->
184 desc { mark = noMark, rank = noRank, structure = restoredStructure }
187traverseTerm :: (Monad f, Applicative f) => (a -> f b) -> Term1 a -> f (Term1 b)
190 App1 a b -> App1 <$> f a <*> f b
191 Fun1 a b -> Fun1 <$> f a <*> f b
192 Var1 x -> Var1 <$> f x
193 EmptyRecord1 -> return EmptyRecord1
194 Record1 fields ext ->
195 Record1 <$> Traversable.traverse (mapM f) fields <*> f ext
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list