1{-# OPTIONS_GHC -Wall #-}
2module Type.Unify (unify) where
4import Control.Monad (zipWithM_)
5import Control.Monad.Except (ExceptT, lift, liftIO, throwError, runExceptT)
6import qualified Data.Map as Map
7import qualified Data.UnionFind.IO as UF
9import qualified AST.Variable as Var
10import qualified Reporting.Region as R
11import qualified Reporting.Error.Type as Error
12import qualified Type.State as TS
13import Type.Type as Type
17-- KICK OFF UNIFICATION
20unify :: Error.Hint -> R.Region -> Variable -> Variable -> TS.Solver ()
21unify hint region expected actual =
22 do result <- runExceptT (guardedUnify ExpectedActual expected actual)
27 Left (Mismatch _subExpected _subActual maybeReason) ->
30 do expectedSrcType <- Type.toSrcType expected
31 actualSrcType <- Type.toSrcType actual
32 mergeHelp expected actual Error
33 let info = Error.MismatchInfo hint expectedSrcType actualSrcType maybeReason
34 return (Error.Mismatch info)
36 TS.addError region =<< liftIO mkError
44 ExceptT Mismatch TS.Solver
48 { _orientation :: Orientation
50 , _firstDesc :: Descriptor
52 , _secondDesc :: Descriptor
56data Orientation = ExpectedActual | ActualExpected
59reorient :: Context -> Context
60reorient (Context orientation var1 desc1 var2 desc2) =
64 ExpectedActual -> ActualExpected
65 ActualExpected -> ExpectedActual
67 Context otherOrientation var2 desc2 var1 desc1
75 = Mismatch Variable Variable (Maybe Error.Reason)
78mismatch :: Context -> Maybe Error.Reason -> Unify a
79mismatch (Context orientation first _ second _) maybeReason =
81 (expected, actual, orientedReason) =
84 (first, second, maybeReason)
87 (second, first, Error.flipReason <$> maybeReason)
89 throwError (Mismatch expected actual orientedReason)
92badRigid :: Maybe String -> Error.Reason
94 Error.BadVar (Just (Error.Rigid maybeName)) Nothing
97badSuper :: Super -> Error.Reason
99 Error.BadVar (Just (errorSuper super)) Nothing
102doubleBad :: Error.VarType -> Error.VarType -> Error.Reason
104 Error.BadVar (Just vt1) (Just vt2)
107errorSuper :: Super -> Error.VarType
127merge :: Context -> Content -> Unify ()
128merge (Context _ first _ second _) content =
129 liftIO $ mergeHelp first second content
132mergeHelp :: Variable -> Variable -> Content -> IO ()
133mergeHelp first second content =
134 UF.union' first second $ \desc1 desc2 ->
138 , _rank = min (_rank desc1) (_rank desc2)
144fresh :: Context -> Content -> Unify Variable
145fresh (Context _ _ desc1 _ desc2) content =
150 , _rank = min (_rank desc1) (_rank desc2)
154 lift (TS.register freshVariable)
158-- ACTUALLY UNIFY THINGS
161guardedUnify :: Orientation -> Variable -> Variable -> Unify ()
162guardedUnify orientation left right =
163 do equivalent <- liftIO $ UF.equivalent left right
167 do leftDesc <- liftIO $ UF.descriptor left
168 rightDesc <- liftIO $ UF.descriptor right
169 actuallyUnify (Context orientation left leftDesc right rightDesc)
172subUnify :: Context -> Variable -> Variable -> Unify ()
173subUnify context var1 var2 =
174 guardedUnify (_orientation context) var1 var2
177actuallyUnify :: Context -> Unify ()
178actuallyUnify context@(Context _ _ firstDesc _ secondDesc) =
180 secondContent = _content secondDesc
182 case _content firstDesc of
184 -- If there was an error, just pretend it is okay. This lets us avoid
185 -- "cascading" errors where one problem manifests as multiple message.
188 Var Flex Nothing _ ->
189 unifyFlex context secondContent
191 Var Flex (Just super) _ ->
192 unifySuper context super secondContent
194 Var Rigid maybeSuper maybeName ->
195 unifyRigid context maybeSuper maybeName secondContent
198 unifyAtom context name secondContent
200 Alias name args realVar ->
201 unifyAlias context name args realVar secondContent
204 unifyStructure context term secondContent
208-- UNIFY FLEXIBLE VARIABLES
211unifyFlex :: Context -> Content -> Unify ()
212unifyFlex context otherContent =
218 merge context otherContent
221 merge context otherContent
224 merge context otherContent
227 merge context otherContent
230 merge context otherContent
234-- UNIFY RIGID VARIABLES
237unifyRigid :: Context -> Maybe Super -> Maybe String -> Content -> Unify ()
238unifyRigid context maybeSuper maybeName otherContent =
243 Var Flex otherMaybeSuper _ ->
244 case (maybeSuper, otherMaybeSuper) of
246 merge context (Var Rigid maybeSuper maybeName)
249 mismatch context (Just (badRigid maybeName))
251 (Just super, Just otherSuper) ->
252 case combineSupers super otherSuper of
253 Right newSuper | newSuper == otherSuper ->
254 merge context otherContent
257 mismatch context (Just (badRigid maybeName))
259 Var Rigid _ otherMaybeName ->
260 mismatch context $ Just $
261 doubleBad (Error.Rigid maybeName) (Error.Rigid otherMaybeName)
264 mismatch context (Just (badRigid maybeName))
267 mismatch context (Just (badRigid maybeName))
270 mismatch context (Just (badRigid maybeName))
273-- UNIFY SUPER VARIABLES
276unifySuper :: Context -> Super -> Content -> Unify ()
277unifySuper context super otherContent =
280 unifySuperStructure context super term
283 if atomMatchesSuper super name then
284 merge context otherContent
286 mismatch context (Just (badSuper super))
288 Var Rigid Nothing maybeName ->
289 mismatch context (Just (doubleBad (errorSuper super) (Error.Rigid maybeName)))
291 Var Rigid (Just otherSuper) maybeName ->
292 case combineSupers super otherSuper of
293 Right newSuper | newSuper == super ->
294 merge context otherContent
297 mismatch context $ Just $
298 doubleBad (errorSuper super) (Error.Rigid maybeName)
300 Var Flex Nothing _ ->
301 merge context (Var Flex (Just super) Nothing)
303 Var Flex (Just otherSuper) _ ->
304 case combineSupers super otherSuper of
306 mismatch context (Just reason)
309 merge context (Var Flex (Just newSuper) Nothing)
312 subUnify context (_first context) realVar
318combineSupers :: Super -> Super -> Either Error.Reason Super
319combineSupers firstSuper secondSuper =
320 case (firstSuper, secondSuper) of
321 (Number , Number ) -> Right Number
322 (Comparable, Number ) -> Right Number
323 (Number , Comparable) -> Right Number
325 (Comparable, Comparable) -> Right Comparable
326 (Appendable, Appendable) -> Right Appendable
328 (Appendable, Comparable) -> Right CompAppend
329 (Comparable, Appendable) -> Right CompAppend
331 (CompAppend, CompAppend) -> Right CompAppend
332 (CompAppend, Comparable) -> Right CompAppend
333 (Comparable, CompAppend) -> Right CompAppend
334 (CompAppend, Appendable) -> Right CompAppend
335 (Appendable, CompAppend) -> Right CompAppend
338 Left $ doubleBad (errorSuper firstSuper) (errorSuper secondSuper)
341isPrimitiveFrom :: [String] -> Var.Canonical -> Bool
342isPrimitiveFrom prims var =
343 any (\p -> Var.isPrim p var) prims
346atomMatchesSuper :: Super -> Var.Canonical -> Bool
347atomMatchesSuper super name =
350 isPrimitiveFrom ["Int", "Float"] name
353 isPrimitiveFrom ["Int", "Float", "Char", "String"] name
356 Var.isPrim "String" name || Var.isText name
359 Var.isPrim "String" name
362unifySuperStructure :: Context -> Super -> Term1 Variable -> Unify ()
363unifySuperStructure context super term =
364 do appStructure <- liftIO (collectApps (Structure term))
367 mismatch context (Just (badSuper super))
372 mismatch context (Just (badSuper super))
375 merge context (Structure term)
378 do merge context (Structure term)
379 unifyComparableRecursive (_orientation context) variable
382 do merge context (Structure term)
383 unifyComparableRecursive (_orientation context) variable
388 mismatch context (Just (badSuper super))
391 mismatch context (Just (badSuper super))
394 if length entries > 6 then
395 mismatch context (Just (Error.TooLongComparableTuple (length entries)))
398 do merge context (Structure term)
399 mapM_ (unifyComparableRecursive (_orientation context)) entries
402 mismatch context (Just (badSuper super))
405unifyComparableRecursive :: Orientation -> Variable -> Unify ()
406unifyComparableRecursive orientation var =
409 do desc <- UF.descriptor var
412 { _content = Var Flex (Just Comparable) Nothing
418 guardedUnify orientation compVar var
427collectApps :: Content -> IO AppStructure
429 collectAppsHelp [] content
432collectAppsHelp :: [Variable] -> Content -> IO AppStructure
433collectAppsHelp args content =
434 case (content, args) of
435 (Structure (App1 func arg), _) ->
436 collectAppsHelp (args ++ [arg]) =<< getContent func
438 (Atom name, [arg]) | Var.isList name ->
441 (Atom name, _) | Var.isTuple name ->
448getContent :: Variable -> IO Content
450 _content <$> UF.descriptor variable
457unifyAtom :: Context -> Var.Canonical -> Content -> Unify ()
458unifyAtom context name otherContent =
463 Var Flex Nothing _ ->
464 merge context (Atom name)
466 Var Flex (Just super) _ ->
467 if atomMatchesSuper super name then
468 merge context (Atom name)
471 mismatch context (Just ((Error.flipReason (badSuper super))))
473 Var Rigid _ maybeName ->
474 mismatch context (Just (Error.flipReason (badRigid maybeName)))
477 if name == otherName then
478 merge context otherContent
482 if isIntFloat name otherName || isIntFloat otherName name then
488 subUnify context (_first context) realVar
491 mismatch context Nothing
494isIntFloat :: Var.Canonical -> Var.Canonical -> Bool
495isIntFloat name otherName =
496 Var.isPrim "Int" name && Var.isPrim "Float" otherName
502unifyAlias :: Context -> Var.Canonical -> [(String, Variable)] -> Variable -> Content -> Unify ()
503unifyAlias context name args realVar otherContent =
508 Var Flex Nothing _ ->
509 merge context (Alias name args realVar)
512 subUnify context realVar (_second context)
515 subUnify context realVar (_second context)
518 Alias otherName otherArgs otherRealVar ->
519 if name == otherName then
520 do merge context otherContent
521 zipWithM_ (subUnify context) (map snd args) (map snd otherArgs)
524 subUnify context realVar otherRealVar
527 subUnify context realVar (_second context)
534unifyStructure :: Context -> Term1 Variable -> Content -> Unify ()
535unifyStructure context term otherContent =
540 Var Flex Nothing _ ->
541 merge context (Structure term)
543 Var Flex (Just super) _ ->
544 unifySuper (reorient context) super (Structure term)
546 Var Rigid _ maybeName ->
547 mismatch context (Just (Error.flipReason (badRigid maybeName)))
550 mismatch context Nothing
553 subUnify context (_first context) realVar
555 Structure otherTerm ->
556 case (term, otherTerm) of
557 (App1 func arg, App1 otherFunc otherArg) ->
558 do subUnify context func otherFunc
559 subUnify context arg otherArg
560 merge context otherContent
562 (Fun1 arg result, Fun1 otherArg otherResult) ->
563 do subUnify context arg otherArg
564 subUnify context result otherResult
565 merge context otherContent
567 (EmptyRecord1, EmptyRecord1) ->
568 merge context otherContent
570 (Record1 fields ext, EmptyRecord1) | Map.null fields ->
571 subUnify context ext (_second context)
573 (EmptyRecord1, Record1 fields ext) | Map.null fields ->
574 subUnify context (_first context) ext
576 (Record1 fields extension, Record1 otherFields otherExtension) ->
577 do firstStructure <- gatherFields context fields extension
578 secondStructure <- gatherFields context otherFields otherExtension
579 unifyRecord context firstStructure secondStructure
582 mismatch context Nothing
589unifyRecord :: Context -> RecordStructure -> RecordStructure -> Unify ()
590unifyRecord context firstStructure secondStructure =
591 do let (RecordStructure expFields expVar expStruct) = firstStructure
592 let (RecordStructure actFields actVar actStruct) = secondStructure
594 -- call after unifying extension, make sure record shape matches before
595 -- looking into whether the particular field types match.
596 let unifySharedFields otherFields ext =
597 do let sharedFields = Map.intersectionWith (,) expFields actFields
598 _ <- traverse (uncurry (subUnify context)) sharedFields
599 let allFields = Map.union (Map.map fst sharedFields) otherFields
600 merge context (Structure (Record1 allFields ext))
602 let uniqueExpFields = Map.difference expFields actFields
603 let uniqueActFields = Map.difference actFields expFields
605 case (expStruct, Map.null uniqueExpFields, actStruct, Map.null uniqueActFields) of
606 (_, True, _, True) ->
607 do subUnify context expVar actVar
608 unifySharedFields Map.empty expVar
610 (Empty, _, _, False) ->
611 mismatch context (Just (Error.MessyFields (Map.keys uniqueExpFields) (Map.keys uniqueActFields)))
613 (_, False, Empty, _) ->
614 mismatch context (Just (Error.MessyFields (Map.keys uniqueExpFields) (Map.keys uniqueActFields)))
616 (_, False, _, True) ->
617 do subRecord <- fresh context (Structure (Record1 uniqueExpFields expVar))
618 subUnify context subRecord actVar
619 unifySharedFields Map.empty subRecord
621 (_, True, _, False) ->
622 do subRecord <- fresh context (Structure (Record1 uniqueActFields actVar))
623 subUnify context expVar subRecord
624 unifySharedFields Map.empty subRecord
626 (Extension, False, Extension, False) ->
627 do let subFields = Map.union uniqueExpFields uniqueActFields
628 subExt <- fresh context (Var Flex Nothing Nothing)
629 expRecord <- fresh context (Structure (Record1 uniqueActFields subExt))
630 actRecord <- fresh context (Structure (Record1 uniqueExpFields subExt))
631 subUnify context expVar expRecord
632 subUnify context actRecord actVar
633 unifySharedFields subFields subExt
637-- GATHER RECORD STRUCTURE
640data RecordStructure = RecordStructure
641 { _fields :: Map.Map String Variable
642 , _extVar :: Variable
643 , _extStruct :: ExtensionStructure
647data ExtensionStructure
652gatherFields :: Context -> Map.Map String Variable -> Variable -> Unify RecordStructure
653gatherFields context fields variable =
654 do desc <- liftIO (UF.descriptor variable)
655 case _content desc of
656 Structure (Record1 subFields subExt) ->
657 gatherFields context (Map.union fields subFields) subExt
659 Structure EmptyRecord1 ->
660 return (RecordStructure fields variable Empty)
663 -- TODO may be dropping useful alias info here
664 gatherFields context fields var
667 return (RecordStructure fields variable Extension)
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