1{-# OPTIONS_GHC -Wall #-}
25import Control.Monad (liftM, liftM2)
26import Control.Monad.Trans (MonadIO, liftIO)
27import qualified Data.Map as Map
29import Data.Text (Text)
31import qualified Reporting.Annotation as A
32import qualified Reporting.Error.Type as Error
33import qualified Reporting.Region as R
35import qualified Type.UnionFind as UF
43 Solver { _solve :: State -> IO (a, State) }
50run :: Solver () -> IO State
52 do (_, state) <- solver initialState
60 , sSavedEnv = Map.empty
61 , sPool = Pool outermostRank []
62 , sMark = noMark + 1 -- The mark must never be equal to noMark!
77 , sError :: [A.Located Error.Error]
81{-| A pool holds a bunch of variables
82The rank of each variable is less than or equal to the pool's "maxRank"
83The young pool exists to make it possible to identify these vars in constant time.
88 , _inhabitants :: [Variable]
92type Env = Map.Map Text (A.Located Variable)
99{-# INLINE modifyEnv #-}
100modifyEnv :: (Env -> Env) -> Solver ()
103 return ( (), state { sEnv = f (sEnv state) } )
106{-# INLINE modifyPool #-}
107modifyPool :: (Pool -> Pool) -> Solver ()
110 return ( (), state { sPool = f (sPool state) } )
113{-# INLINE addError #-}
114addError :: R.Region -> Error.Error -> Solver ()
117 return ( (), state { sError = A.A region err : sError state } )
120{-# INLINE switchToPool #-}
121switchToPool :: Pool -> Solver ()
123 modifyPool (\_ -> pool)
126{-# INLINE getPool #-}
127getPool :: Solver Pool
130 return ( sPool state, state )
137 return ( sEnv state, state )
140{-# INLINE saveLocalEnv #-}
141saveLocalEnv :: Solver ()
144 return ( (), state { sSavedEnv = sEnv state } )
147{-# INLINE uniqueMark #-}
148uniqueMark :: Solver Int
155 return ( mark, state { sMark = mark + 1 } )
158{-# INLINE nextRankPool #-}
159nextRankPool :: Solver Pool
166 return ( Pool (maxRank + 1) [], state )
173{-# INLINE register #-}
174register :: Variable -> Solver Variable
176 {-# SCC elm_compiler_type_register #-}
179 (Pool maxRank inhabitants) =
182 return ( variable, state { sPool = Pool maxRank (variable : inhabitants) } )
185{-# INLINE introduce #-}
186introduce :: Variable -> Solver Variable
188 {-# SCC elm_compiler_type_introduce #-}
191 (Pool maxRank inhabitants) =
194 do UF.modifyDescriptor variable (\desc -> desc { _rank = maxRank })
195 return ( variable, state { sPool = Pool maxRank (variable : inhabitants) } )
202flatten :: Type -> Solver Variable
204 {-# SCC elm_compiler_type_flatten #-}
205 flattenHelp Map.empty term
208flattenHelp :: Map.Map Text Variable -> Type -> Solver Variable
209flattenHelp aliasDict termN =
212 return (aliasDict ! name)
214 AliasN name args realType ->
215 do flatArgs <- mapM (traverse (flattenHelp aliasDict)) args
216 flatVar <- flattenHelp (Map.fromList flatArgs) realType
217 makeFlatType (Alias name flatArgs flatVar)
223 do flatArgs <- traverse (flattenHelp aliasDict) args
224 makeFlatType (Structure (App1 name flatArgs))
227 do flatA <- flattenHelp aliasDict a
228 flatB <- flattenHelp aliasDict b
229 makeFlatType (Structure (Fun1 flatA flatB))
232 makeFlatType (Structure EmptyRecord1)
234 RecordN fields ext ->
235 do flatFields <- traverse (flattenHelp aliasDict) fields
236 flatExt <- flattenHelp aliasDict ext
237 makeFlatType (Structure (Record1 flatFields flatExt))
240makeFlatType :: Content -> Solver Variable
241makeFlatType content =
243 variable <- liftIO $ UF.fresh $
246 , _rank = _maxRank pool
257makeInstance :: Variable -> Solver Variable
259 do alreadyCopiedMark <- uniqueMark
260 freshVar <- makeCopy alreadyCopiedMark var
261 _ <- restore alreadyCopiedMark var
269makeCopy :: Int -> Variable -> Solver Variable
270makeCopy alreadyCopiedMark variable =
271 {-# SCC elm_compiler_type_copy #-}
272 do desc <- liftIO $ UF.descriptor variable
273 makeCopyHelp desc alreadyCopiedMark variable
276makeCopyHelp :: Descriptor -> Int -> Variable -> Solver Variable
277makeCopyHelp (Descriptor content rank mark copy) alreadyCopiedMark variable =
278 if mark == alreadyCopiedMark then
280 Just copiedVariable ->
281 return copiedVariable
285 "Error copying type variable. This should be impossible.\
286 \ Please report this at <https://github.com/elm-lang/elm-compiler/issues>\
287 \ with a <http://sscce.org> and information on your OS, how you installed,\
288 \ and any other configuration information that might be helpful."
290 else if rank /= noRank || not (needsCopy content) then
296 liftIO $ UF.fresh $ Descriptor
297 { _content = content -- place holder!
298 , _rank = _maxRank pool
304 -- Link the original variable to the new variable. This lets us
305 -- avoid making multiple copies of the variable we are instantiating.
307 -- Need to do this before recursively copying the content of
308 -- the variable to avoid looping on cyclic terms.
309 liftIO $ UF.modifyDescriptor variable $ \desc ->
310 desc { _mark = alreadyCopiedMark, _copy = Just newVar }
313 let setContent newContent =
314 liftIO $ UF.modifyDescriptor newVar $ \desc ->
315 desc { _content = newContent }
317 -- Now we recursively copy the content of the variable.
318 -- We have already marked the variable as copied, so we
319 -- will not repeat this work or crawl this variable again.
322 do newTerm <- traverseFlatType (makeCopy alreadyCopiedMark) term
323 setContent (Structure newTerm)
325 Var Rigid maybeSuper maybeName ->
326 setContent (Var Flex maybeSuper maybeName)
331 Alias name args realType ->
334 <$> mapM (traverse (makeCopy alreadyCopiedMark)) args
335 <*> makeCopy alreadyCopiedMark realType
344needsCopy :: Content -> Bool
364restore :: Int -> Variable -> Solver Variable
365restore alreadyCopiedMark variable =
366 do desc <- liftIO $ UF.descriptor variable
367 if _mark desc /= alreadyCopiedMark
372 do restoredContent <-
373 restoreContent alreadyCopiedMark (_content desc)
374 liftIO $ UF.setDescriptor variable $ Descriptor
375 { _content = restoredContent
384restoreContent :: Int -> Content -> Solver Content
385restoreContent alreadyCopiedMark content =
387 go = restore alreadyCopiedMark
391 Structure <$> traverseFlatType go term
396 Alias name args var ->
398 <$> mapM (traverse go) args
409traverseFlatType :: (Variable -> Solver Variable) -> FlatType -> Solver FlatType
410traverseFlatType f flatType =
413 liftM (App1 name) (traverse f args)
416 liftM2 Fun1 (f a) (f b)
421 Record1 fields ext ->
422 liftM2 Record1 (traverse f fields) (f ext)
429instance Functor Solver where
431 fmap f (Solver solver) =
433 fmap (\(a, s2) -> (f a, s2)) (solver s1)
436instance Applicative Solver where
439 Solver $ \s -> return (a, s)
442 Solver solverFunc <*> Solver solverValue =
444 do (f, s2) <- solverFunc s1
445 (x, s3) <- solverValue s2
449instance Monad Solver where
451 Solver solver >>= k =
453 do (a, s2) <- solver s1
454 (b, s3) <- _solve (k a) s2
459 Solver $ \_ -> fail msg
462instance MonadIO Solver where
463 {-# INLINE liftIO #-}
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