3import Control.Applicative ( (<$>), (<*>), Applicative, (<|>) )
4import Control.Monad.State
5import qualified Data.Map as Map
6import qualified Data.Traversable as Traversable
7import qualified Data.UnionFind.IO as UF
9import qualified AST.Annotation as A
11import Text.PrettyPrint as P
15-- Holds a bunch of variables
16-- The rank of each variable is less than or equal to the pool's "maxRank"
17-- The young pool exists to make it possible to identify these vars in constant time.
21 , inhabitants :: [Variable]
24emptyPool = Pool { maxRank = outermostRank, inhabitants = [] }
26type Env = Map.Map String Variable
28-- Keeps track of the environment, type variable pool, and a list of errors
29data SolverState = SS {
39 sSavedEnv = Map.empty,
41 sMark = noMark + 1, -- The mark must never be equal to noMark!
45modifyEnv f = modify $ \state -> state { sEnv = f (sEnv state) }
46modifyPool f = modify $ \state -> state { sPool = f (sPool state) }
52 -> UF.Point Descriptor
53 -> UF.Point Descriptor
54 -> StateT SolverState IO ()
55addError region hint t1 t2 =
56 do err <- liftIO makeError
57 modify $ \state -> state { sErrors = err : sErrors state }
60 t1' <- pretty <$> toSrcType t1
61 t2' <- pretty <$> toSrcType t2
62 return . foldr ($+$) P.empty $
63 [ P.text "Type mismatch between the following types:"
69 , maybe P.empty (P.nest 4 . P.text) hint
70 , P.text " It is related to the expression" <+> pretty region
72 , P.nest 8 $ A.getRegionDocs region
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
114 VarN maybeAlias v -> do
115 liftIO $ UF.modifyDescriptor v $ \desc -> desc { alias = maybeAlias <|> alias desc }
117 TermN maybeAlias t -> do
118 flatStructure <- traverseTerm flatten t
120 var <- liftIO . UF.fresh $ Descriptor
121 { structure = Just flatStructure
122 , rank = maxRank pool
131makeInstance :: Variable -> StateT SolverState IO Variable
133 alreadyCopied <- uniqueMark
134 freshVar <- makeCopy alreadyCopied var
135 restore alreadyCopied var
138makeCopy :: Int -> Variable -> StateT SolverState IO Variable
139makeCopy alreadyCopied variable = do
140 desc <- liftIO $ UF.descriptor variable
142 () | mark desc == alreadyCopied ->
145 Nothing -> error $ "Error copying type variable. This should be impossible." ++
146 " Please report an error to the github repo!"
148 | rank desc /= noRank || flex desc == Constant ->
153 newVar <- liftIO $ UF.fresh $ Descriptor
154 { structure = Nothing
155 , rank = maxRank pool
157 , flex = case flex desc of
161 , name = case flex desc of
168 -- Link the original variable to the new variable. This lets us
169 -- avoid making multiple copies of the variable we are instantiating.
171 -- Need to do this before recursively copying the structure of
172 -- the variable to avoid looping on cyclic terms.
173 liftIO $ UF.modifyDescriptor variable $ \desc ->
174 desc { mark = alreadyCopied, copy = Just newVar }
176 -- Now we recursively copy the structure of the variable.
177 -- We have already marked the variable as copied, so we
178 -- will not repeat this work or crawl this variable again.
179 case structure desc of
180 Nothing -> return newVar
182 newTerm <- traverseTerm (makeCopy alreadyCopied) term
183 liftIO $ UF.modifyDescriptor newVar $ \desc ->
184 desc { structure = Just newTerm }
187restore :: Int -> Variable -> StateT SolverState IO Variable
188restore alreadyCopied variable = do
189 desc <- liftIO $ UF.descriptor variable
190 if mark desc /= alreadyCopied
194 Traversable.traverse (traverseTerm (restore alreadyCopied)) (structure desc)
195 liftIO $ UF.modifyDescriptor variable $ \desc ->
196 desc { mark = noMark, rank = noRank, structure = restoredStructure }
199traverseTerm :: (Monad f, Applicative f) => (a -> f b) -> Term1 a -> f (Term1 b)
202 App1 a b -> App1 <$> f a <*> f b
203 Fun1 a b -> Fun1 <$> f a <*> f b
204 Var1 x -> Var1 <$> f x
205 EmptyRecord1 -> return EmptyRecord1
206 Record1 fields ext ->
207 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