1{-# LANGUAGE MultiWayIf #-}
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) }
47addError message t1 t2 =
48 modify $ \state -> state { sErrors = err : sErrors state }
50 wordify = P.fsep . map P.text . words
52 width = maximum . map length . lines $ render msg
53 spaces = List.replicate (width - 15 - 3) ' '
54 err = makeError <$> extraPretty t1 <*> extraPretty t2
56 P.vcat [ P.text $ "Type error" ++ spaces ++ "line ???"
57 , P.text (List.replicate width '-')
59 , P.text " Expected Type:" <+> pt1
60 , P.text " Actual Type:" <+> pt2 <> P.text "\n"
63switchToPool pool = modifyPool (\_ -> pool)
65getPool :: StateT SolverState IO Pool
66getPool = sPool <$> get
68getEnv :: StateT SolverState IO (Map.Map String Variable)
71saveLocalEnv :: StateT SolverState IO ()
74 modify $ \state -> state { sSavedEnv = env }
76uniqueMark :: StateT SolverState IO Int
79 let mark = sMark state
80 put $ state { sMark = mark + 1 }
83nextRankPool :: StateT SolverState IO Pool
86 return $ Pool { maxRank = maxRank pool + 1, inhabitants = [] }
88register :: Variable -> StateT SolverState IO Variable
90 modifyPool $ \pool -> pool { inhabitants = variable : inhabitants pool }
93introduce :: Variable -> StateT SolverState IO Variable
94introduce variable = do
96 liftIO $ UF.modifyDescriptor variable (\desc -> desc { rank = maxRank pool })
99flatten :: Type -> StateT SolverState IO Variable
104 flatStructure <- traverseTerm flatten t
106 var <- liftIO . UF.fresh $ Descriptor {
107 structure = Just flatStructure,
116makeInstance :: Variable -> StateT SolverState IO Variable
118 alreadyCopied <- uniqueMark
119 freshVar <- makeCopy alreadyCopied var
120 restore alreadyCopied var
123makeCopy :: Int -> Variable -> StateT SolverState IO Variable
124makeCopy alreadyCopied variable = do
125 desc <- liftIO $ UF.descriptor variable
126 if | mark desc == alreadyCopied ->
129 Nothing -> error "This should be impossible."
131 | rank desc /= noRank || flex desc == Constant ->
136 newVar <- liftIO $ UF.fresh $ Descriptor {
140 flex = case flex desc of
144 name = case flex desc of
151 -- Link the original variable to the new variable. This lets us
152 -- avoid making multiple copies of the variable we are instantiating.
154 -- Need to do this before recursively copying the structure of
155 -- the variable to avoid looping on cyclic terms.
156 liftIO $ UF.modifyDescriptor variable $ \desc ->
157 desc { mark = alreadyCopied, copy = Just newVar }
159 -- Now we recursively copy the structure of the variable.
160 -- We have already marked the variable as copied, so we
161 -- will not repeat this work or crawl this variable again.
162 case structure desc of
163 Nothing -> return newVar
165 newTerm <- traverseTerm (makeCopy alreadyCopied) term
166 liftIO $ UF.modifyDescriptor newVar $ \desc ->
167 desc { structure = Just newTerm }
170restore :: Int -> Variable -> StateT SolverState IO Variable
171restore alreadyCopied variable = do
172 desc <- liftIO $ UF.descriptor variable
173 if mark desc /= alreadyCopied
177 Traversable.traverse (traverseTerm (restore alreadyCopied)) (structure desc)
178 liftIO $ UF.modifyDescriptor variable $ \desc ->
179 desc { mark = noMark, rank = noRank, structure = restoredStructure }
182traverseTerm :: (Monad f, Applicative f) => (a -> f b) -> Term1 a -> f (Term1 b)
185 App1 a b -> App1 <$> f a <*> f b
186 Fun1 a b -> Fun1 <$> f a <*> f b
187 Var1 x -> Var1 <$> f x
188 EmptyRecord1 -> return EmptyRecord1
189 Record1 fields ext ->
190 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