5import qualified Data.Map as Map
6import qualified Data.UnionFind.IO as UF
7import Control.Monad.State
8import Control.Applicative ((<$>),(<*>), Applicative)
9import qualified Data.Traversable as Traversable
10import Text.PrettyPrint as P
11import SourceSyntax.PrettyPrint
12import SourceSyntax.Location
13import qualified Type.Alias as Alias
16-- Holds a bunch of variables
17-- The rank of each variable is less than or equal to the pool's "maxRank"
18-- The young pool exists to make it possible to identify these vars in constant time.
22 inhabitants :: [Variable]
25emptyPool = Pool { maxRank = outermostRank, inhabitants = [] }
27type Env = Map.Map String Variable
29-- Keeps track of the environment, type variable pool, and a list of errors
30data SolverState = SS {
35 sErrors :: [Alias.Rules -> IO P.Doc]
40 sSavedEnv = Map.empty,
42 sMark = noMark + 1, -- The mark must never be equal to noMark!
46modifyEnv f = modify $ \state -> state { sEnv = f (sEnv state) }
47modifyPool f = modify $ \state -> state { sPool = f (sPool state) }
49addError span hint t1 t2 =
50 modify $ \state -> state { sErrors = makeError : sErrors state }
53 let prettiest = pretty . Alias.realias rules
54 t1' <- prettiest <$> toSrcType t1
55 t2' <- prettiest <$> toSrcType t2
57 [ P.text $ "Type error" ++ location ++ ":"
58 , maybe P.empty P.text hint
59 , display $ case span of { NoSpan msg -> msg ; Span _ _ msg -> msg }
60 , P.text " Expected Type:" <+> t1'
61 , P.text " Actual Type:" <+> t2'
64 location = case span of
67 if line p1 == line p2 then " on line " ++ show (line p1)
68 else " between lines " ++ show (line p1) ++ " and " ++ show (line p2)
71 P.vcat [ P.text $ concatMap ("\n "++) (lines msg)
75switchToPool pool = modifyPool (\_ -> pool)
77getPool :: StateT SolverState IO Pool
78getPool = sPool <$> get
80getEnv :: StateT SolverState IO Env
83saveLocalEnv :: StateT SolverState IO ()
86 modify $ \state -> state { sSavedEnv = env }
88uniqueMark :: StateT SolverState IO Int
91 let mark = sMark state
92 put $ state { sMark = mark + 1 }
95nextRankPool :: StateT SolverState IO Pool
98 return $ Pool { maxRank = maxRank pool + 1, inhabitants = [] }
100register :: Variable -> StateT SolverState IO Variable
101register variable = do
102 modifyPool $ \pool -> pool { inhabitants = variable : inhabitants pool }
105introduce :: Variable -> StateT SolverState IO Variable
106introduce variable = do
108 liftIO $ UF.modifyDescriptor variable (\desc -> desc { rank = maxRank pool })
111flatten :: Type -> StateT SolverState IO Variable
116 flatStructure <- traverseTerm flatten t
118 var <- liftIO . UF.fresh $ Descriptor {
119 structure = Just flatStructure,
128makeInstance :: Variable -> StateT SolverState IO Variable
130 alreadyCopied <- uniqueMark
131 freshVar <- makeCopy alreadyCopied var
132 restore alreadyCopied var
135makeCopy :: Int -> Variable -> StateT SolverState IO Variable
136makeCopy alreadyCopied variable = do
137 desc <- liftIO $ UF.descriptor variable
139 () | mark desc == alreadyCopied ->
142 Nothing -> error $ "Error copying type variable. This should be impossible." ++
143 " Please report an error to the github repo!"
145 | rank desc /= noRank || flex desc == Constant ->
150 newVar <- liftIO $ UF.fresh $ Descriptor {
154 flex = case flex desc of
158 name = case flex desc of
164 -- Link the original variable to the new variable. This lets us
165 -- avoid making multiple copies of the variable we are instantiating.
167 -- Need to do this before recursively copying the structure of
168 -- the variable to avoid looping on cyclic terms.
169 liftIO $ UF.modifyDescriptor variable $ \desc ->
170 desc { mark = alreadyCopied, copy = Just newVar }
172 -- Now we recursively copy the structure of the variable.
173 -- We have already marked the variable as copied, so we
174 -- will not repeat this work or crawl this variable again.
175 case structure desc of
176 Nothing -> return newVar
178 newTerm <- traverseTerm (makeCopy alreadyCopied) term
179 liftIO $ UF.modifyDescriptor newVar $ \desc ->
180 desc { structure = Just newTerm }
183restore :: Int -> Variable -> StateT SolverState IO Variable
184restore alreadyCopied variable = do
185 desc <- liftIO $ UF.descriptor variable
186 if mark desc /= alreadyCopied
190 Traversable.traverse (traverseTerm (restore alreadyCopied)) (structure desc)
191 liftIO $ UF.modifyDescriptor variable $ \desc ->
192 desc { mark = noMark, rank = noRank, structure = restoredStructure }
195traverseTerm :: (Monad f, Applicative f) => (a -> f b) -> Term1 a -> f (Term1 b)
198 App1 a b -> App1 <$> f a <*> f b
199 Fun1 a b -> Fun1 <$> f a <*> f b
200 Var1 x -> Var1 <$> f x
201 EmptyRecord1 -> return EmptyRecord1
202 Record1 fields ext ->
203 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