1{-# OPTIONS_GHC -XMultiWayIf #-}
5import qualified Data.Map as Map
6import qualified Data.List as List
7import qualified Type.Environment as Env
8import qualified Data.UnionFind.IO as UF
9import Control.Monad.State
10import Control.Applicative ((<$>),(<*>), Applicative)
11import qualified Data.Traversable as Traversable
12import Text.PrettyPrint as P
13import SourceSyntax.PrettyPrint
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 = [] }
27-- Keeps track of the environment, type variable pool, and a list of errors
28data SolverState = SS {
29 sEnv :: Map.Map String Variable,
30 sSavedEnv :: Map.Map String Variable,
38 sSavedEnv = Map.empty,
40 sMark = noMark + 1, -- The mark must never be equal to noMark!
44modifyEnv f = modify $ \state -> state { sEnv = f (sEnv state) }
45modifyPool f = modify $ \state -> state { sPool = f (sPool state) }
46addError message t1 t2 =
47 modify $ \state -> state { sErrors = err : sErrors state }
49 wordify = P.fsep . map P.text . words
51 width = maximum . map length . lines $ render msg
52 spaces = List.replicate (width - 15 - 3) ' '
53 err = makeError <$> extraPretty t1 <*> extraPretty t2
55 P.vcat [ P.text $ "Type error" ++ spaces ++ "line ???"
56 , P.text (List.replicate width '-')
58 , P.text " Expected Type:" <+> pt1
59 , P.text " Actual Type:" <+> pt2 <> P.text "\n"
60 , wordify "The error comes from:" <> P.text "\n"
61 , P.nest 3 (P.text "???")
64switchToPool pool = modifyPool (\_ -> pool)
66getPool :: StateT SolverState IO Pool
67getPool = sPool <$> get
69getEnv :: StateT SolverState IO (Map.Map String Variable)
72saveLocalEnv :: StateT SolverState IO ()
75 modify $ \state -> state { sSavedEnv = env }
77uniqueMark :: StateT SolverState IO Int
80 let mark = sMark state
81 put $ state { sMark = mark + 1 }
84nextRankPool :: StateT SolverState IO Pool
87 return $ Pool { maxRank = maxRank pool + 1, inhabitants = [] }
89register :: Variable -> StateT SolverState IO Variable
91 modifyPool $ \pool -> pool { inhabitants = variable : inhabitants pool }
94introduce :: Variable -> StateT SolverState IO Variable
95introduce variable = do
97 liftIO $ UF.modifyDescriptor variable (\desc -> desc { rank = maxRank pool })
100flatten :: Type -> StateT SolverState IO Variable
105 flatStructure <- traverseTerm flatten t
107 var <- liftIO . UF.fresh $ Descriptor {
108 structure = Just flatStructure,
117makeInstance :: Variable -> StateT SolverState IO Variable
119 alreadyCopied <- uniqueMark
120 freshVar <- makeCopy alreadyCopied var
121 restore alreadyCopied var
124makeCopy :: Int -> Variable -> StateT SolverState IO Variable
125makeCopy alreadyCopied variable = do
126 desc <- liftIO $ UF.descriptor variable
127 if | mark desc == alreadyCopied ->
130 Nothing -> error "This should be impossible."
132 | rank desc /= noRank || flex desc == Constant ->
137 newVar <- liftIO $ UF.fresh $ Descriptor {
143 name = case flex desc of
150 -- Link the original variable to the new variable. This lets us
151 -- avoid making multiple copies of the variable we are instantiating.
153 -- Need to do this before recursively copying the structure of
154 -- the variable to avoid looping on cyclic terms.
155 liftIO $ UF.modifyDescriptor variable $ \desc ->
156 desc { mark = alreadyCopied, copy = Just newVar }
158 -- Now we recursively copy the structure of the variable.
159 -- We have already marked the variable as copied, so we
160 -- will not repeat this work or crawl this variable again.
161 case structure desc of
162 Nothing -> return newVar
164 newTerm <- traverseTerm (makeCopy alreadyCopied) term
165 liftIO $ UF.modifyDescriptor newVar $ \desc ->
166 desc { structure = Just newTerm }
169restore :: Int -> Variable -> StateT SolverState IO Variable
170restore alreadyCopied variable = do
171 desc <- liftIO $ UF.descriptor variable
172 if mark desc /= alreadyCopied
176 Traversable.traverse (traverseTerm (restore alreadyCopied)) (structure desc)
177 liftIO $ UF.modifyDescriptor variable $ \desc ->
178 desc { mark = noMark, rank = noRank, structure = restoredStructure }
181traverseTerm :: (Monad f, Applicative f) => (a -> f b) -> Term1 a -> f (Term1 b)
184 App1 a b -> App1 <$> f a <*> f b
185 Fun1 a b -> Fun1 <$> f a <*> f b
186 Var1 x -> Var1 <$> f x
187 EmptyRecord1 -> return EmptyRecord1
188 Record1 fields ext ->
189 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