4import qualified Data.Map as Map
5import qualified Data.List as List
6import qualified Type.Environment as Env
7import qualified Data.UnionFind.IO as UF
8import Control.Monad.State
9import Control.Applicative ((<$>),(<*>), Applicative)
10import qualified Data.Traversable as Traversable
11import Text.PrettyPrint as P
12import SourceSyntax.PrettyPrint
13import SourceSyntax.Location
14import qualified SourceSyntax.Type as Src
15import qualified Type.Alias as Alias
18-- Holds a bunch of variables
19-- The rank of each variable is less than or equal to the pool's "maxRank"
20-- The young pool exists to make it possible to identify these vars in constant time.
24 inhabitants :: [Variable]
27emptyPool = Pool { maxRank = outermostRank, inhabitants = [] }
29type Env = Map.Map String Variable
31-- Keeps track of the environment, type variable pool, and a list of errors
32data SolverState = SS {
37 sErrors :: [Alias.Rules -> IO P.Doc]
42 sSavedEnv = Map.empty,
44 sMark = noMark + 1, -- The mark must never be equal to noMark!
48modifyEnv f = modify $ \state -> state { sEnv = f (sEnv state) }
49modifyPool f = modify $ \state -> state { sPool = f (sPool state) }
51addError span message t1 t2 =
52 modify $ \state -> state { sErrors = makeError : sErrors state }
55 let prettiest = pretty . Alias.realias rules
56 t1' <- prettiest <$> toSrcType t1
57 t2' <- prettiest <$> toSrcType t2
59 [ P.text $ "Type error" ++ location ++ ":"
60 , P.vcat . map P.text . lines $ if null message then defaultMessage else message
62 , P.text " Expected Type:" <+> t1'
63 , P.text " Actual Type:" <+> t2' <> P.text "\n"
66 location = case span of
69 if line p1 == line p2 then " on line " ++ show (line p1)
70 else " between lines " ++ show (line p1) ++ " and " ++ show (line p2)
72 display msg = if null msg then "\n"
73 else "\n" ++ unlines (map (" "++) $ lines msg)
76 NoSpan msg -> display msg
77 Span _ _ msg -> display msg
79 defaultMessage = "Something weird is happening with this value:"
82switchToPool pool = modifyPool (\_ -> pool)
84getPool :: StateT SolverState IO Pool
85getPool = sPool <$> get
87getEnv :: StateT SolverState IO Env
90saveLocalEnv :: StateT SolverState IO ()
93 modify $ \state -> state { sSavedEnv = env }
95uniqueMark :: StateT SolverState IO Int
98 let mark = sMark state
99 put $ state { sMark = mark + 1 }
102nextRankPool :: StateT SolverState IO Pool
105 return $ Pool { maxRank = maxRank pool + 1, inhabitants = [] }
107register :: Variable -> StateT SolverState IO Variable
108register variable = do
109 modifyPool $ \pool -> pool { inhabitants = variable : inhabitants pool }
112introduce :: Variable -> StateT SolverState IO Variable
113introduce variable = do
115 liftIO $ UF.modifyDescriptor variable (\desc -> desc { rank = maxRank pool })
118flatten :: Type -> StateT SolverState IO Variable
123 flatStructure <- traverseTerm flatten t
125 var <- liftIO . UF.fresh $ Descriptor {
126 structure = Just flatStructure,
135makeInstance :: Variable -> StateT SolverState IO Variable
137 alreadyCopied <- uniqueMark
138 freshVar <- makeCopy alreadyCopied var
139 restore alreadyCopied var
142makeCopy :: Int -> Variable -> StateT SolverState IO Variable
143makeCopy alreadyCopied variable = do
144 desc <- liftIO $ UF.descriptor variable
146 () | mark desc == alreadyCopied ->
149 Nothing -> error $ "Error copying type variable. This should be impossible." ++
150 " Please report an error to the github repo!"
152 | rank desc /= noRank || flex desc == Constant ->
157 newVar <- liftIO $ UF.fresh $ Descriptor {
161 flex = case flex desc of
165 name = case flex desc of
171 -- Link the original variable to the new variable. This lets us
172 -- avoid making multiple copies of the variable we are instantiating.
174 -- Need to do this before recursively copying the structure of
175 -- the variable to avoid looping on cyclic terms.
176 liftIO $ UF.modifyDescriptor variable $ \desc ->
177 desc { mark = alreadyCopied, copy = Just newVar }
179 -- Now we recursively copy the structure of the variable.
180 -- We have already marked the variable as copied, so we
181 -- will not repeat this work or crawl this variable again.
182 case structure desc of
183 Nothing -> return newVar
185 newTerm <- traverseTerm (makeCopy alreadyCopied) term
186 liftIO $ UF.modifyDescriptor newVar $ \desc ->
187 desc { structure = Just newTerm }
190restore :: Int -> Variable -> StateT SolverState IO Variable
191restore alreadyCopied variable = do
192 desc <- liftIO $ UF.descriptor variable
193 if mark desc /= alreadyCopied
197 Traversable.traverse (traverseTerm (restore alreadyCopied)) (structure desc)
198 liftIO $ UF.modifyDescriptor variable $ \desc ->
199 desc { mark = noMark, rank = noRank, structure = restoredStructure }
202traverseTerm :: (Monad f, Applicative f) => (a -> f b) -> Term1 a -> f (Term1 b)
205 App1 a b -> App1 <$> f a <*> f b
206 Fun1 a b -> Fun1 <$> f a <*> f b
207 Var1 x -> Var1 <$> f x
208 EmptyRecord1 -> return EmptyRecord1
209 Record1 fields ext ->
210 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