@@ -125,42 +125,6 @@ reasonToProblem orientation maybeReason =
125 Special (Error.flipReason reason)
125 Special (Error.flipReason reason)
128−badRigid :: Maybe Text -> Error.Reason
130− Error.BadVar (Just (Error.Rigid maybeName)) Nothing
133−badSuper :: Super -> Error.Reason
135− Error.BadVar (Just (errorSuper super)) Nothing
138−badSupers :: Super -> Super -> Error.Reason
139−badSupers super1 super2 =
140− Error.BadVar (Just (errorSuper super1)) (Just (errorSuper super2))
143−doubleBad :: Error.VarType -> Error.VarType -> Error.Reason
145− Error.BadVar (Just vt1) (Just vt2)
148−errorSuper :: Super -> Error.VarType
@@ -210,25 +174,28 @@ subUnify context var1 var2 =
210actuallyUnify :: Context -> Unify ()
174actuallyUnify :: Context -> Unify ()
211actuallyUnify context@(Context _ _ (Descriptor firstContent _ _ _) _ (Descriptor secondContent _ _ _)) =
175actuallyUnify context@(Context _ _ (Descriptor firstContent _ _ _) _ (Descriptor secondContent _ _ _)) =
212 case firstContent of
176 case firstContent of
213− Error _ ->
177+ FlexVar _ ->
214− -- If there was an error, just pretend it is okay. This lets us avoid
215− -- "cascading" errors where one problem manifests as multiple message.
218− Var Flex Nothing _ ->
219 unifyFlex context secondContent
178 unifyFlex context secondContent
221− Var Flex (Just super) _ ->
180+ FlexSuper super _ ->
222 unifyFlexSuper context super firstContent secondContent
181 unifyFlexSuper context super firstContent secondContent
224− Var Rigid maybeSuper maybeName ->
183+ RigidVar name ->
184+ unifyRigid context Nothing name firstContent secondContent
186+ RigidSuper super name ->
225− unifyRigid context maybeSuper maybeName firstContent secondContent
187+ unifyRigid context (Just super) name firstContent secondContent
227 Alias name args realVar ->
189 Alias name args realVar ->
228 unifyAlias context name args realVar secondContent
190 unifyAlias context name args realVar secondContent
230− Structure term ->
192+ Structure flatType ->
231− unifyStructure context term firstContent secondContent
193+ unifyStructure context flatType firstContent secondContent
196+ -- If there was an error, just pretend it is okay. This lets us avoid
197+ -- "cascading" errors where one problem manifests as multiple message.
@@ -241,10 +208,18 @@ unifyFlex context otherContent =
241 Error _ ->
208 Error _ ->
242 return ()
209 return ()
244− Var Flex _ _ ->
211+ -- TODO see if wildcarding makes a noticable perf difference
214+ merge context otherContent
217+ merge context otherContent
245 merge context otherContent
220 merge context otherContent
247− Var Rigid _ _ ->
222+ RigidSuper _ _ ->
248 merge context otherContent
223 merge context otherContent
250 Alias _ _ _ ->
225 Alias _ _ _ ->
@@ -258,35 +233,61 @@ unifyFlex context otherContent =
258-- UNIFY RIGID VARIABLES
233-- UNIFY RIGID VARIABLES
261−unifyRigid :: Context -> Maybe Super -> Maybe Text -> Content -> Content -> Unify ()
236+unifyRigid :: Context -> Maybe Super -> Text -> Content -> Content -> Unify ()
262−unifyRigid context maybeSuper maybeName content otherContent =
237+unifyRigid context maybeSuper name content otherContent =
263 case otherContent of
238 case otherContent of
264− Error _ ->
239+ FlexVar _ ->
265− return ()
240+ merge context content
267− Var Flex otherMaybeSuper _ ->
242+ FlexSuper otherSuper _ ->
268− case (maybeSuper, otherMaybeSuper) of
243+ case maybeSuper of
269− (_, Nothing) ->
244+ Just super ->
245+ if combineRigidSupers super otherSuper then
270 merge context content
246 merge context content
248+ mismatch context $ Just $
249+ Error.RigidSuperTooGeneric super name $ Error.SpecificSuper otherSuper
272− (Nothing, Just _) ->
251+ Nothing ->
273− mismatch context (Just (badRigid maybeName))
252+ mismatch context $ Just $
253+ Error.RigidVarTooGeneric name $ Error.SpecificSuper otherSuper
275− (Just super, Just otherSuper) ->
255+ RigidVar otherName ->
276− if combineRigidSupers super otherSuper then
277− merge context content
279− mismatch context (Just (badRigid maybeName))
256+ mismatch context $ Just $
257+ Error.RigidClash name otherName
281− Var Rigid _ otherMaybeName ->
259+ RigidSuper _ otherName ->
282 mismatch context $ Just $
260 mismatch context $ Just $
283− doubleBad (Error.Rigid maybeName) (Error.Rigid otherMaybeName)
261+ Error.RigidClash name otherName
285− Alias _ _ _ ->
263+ Alias otherName _ _ ->
286− mismatch context (Just (badRigid maybeName))
264+ mismatch context $ Just $
265+ maybe Error.RigidVarTooGeneric Error.RigidSuperTooGeneric maybeSuper name $
266+ Error.SpecificType otherName
288− Structure _ ->
268+ Structure flatType ->
289− mismatch context (Just (badRigid maybeName))
269+ mismatch context $ Just $
270+ maybe Error.RigidVarTooGeneric Error.RigidSuperTooGeneric maybeSuper name $
271+ flatTypeToSpecificThing flatType
277+flatTypeToSpecificThing :: FlatType -> Error.SpecificThing
278+flatTypeToSpecificThing flatType =
281+ Error.SpecificType name
284+ Error.SpecificFunction
287+ Error.SpecificRecord
290+ Error.SpecificRecord
@@ -296,49 +297,51 @@ unifyRigid context maybeSuper maybeName content otherContent =
296unifyFlexSuper :: Context -> Super -> Content -> Content -> Unify ()
297unifyFlexSuper :: Context -> Super -> Content -> Content -> Unify ()
297unifyFlexSuper context super content otherContent =
298unifyFlexSuper context super content otherContent =
298 case otherContent of
299 case otherContent of
299− Structure term ->
300+ Structure flatType ->
300− unifyFlexSuperStructure context super term
301+ unifyFlexSuperStructure context super flatType
302− Var Rigid Nothing maybeName ->
303+ RigidVar name ->
303− mismatch context (Just (doubleBad (errorSuper super) (Error.Rigid maybeName)))
304+ mismatch context $ Just $
305+ Error.RigidVarTooGeneric name (Error.SpecificSuper super)
305− Var Rigid (Just otherSuper) maybeName ->
307+ RigidSuper otherSuper name ->
306 if combineRigidSupers otherSuper super then
308 if combineRigidSupers otherSuper super then
307 merge context otherContent
309 merge context otherContent
309− mismatch context $ Just $ badRigid maybeName
311+ mismatch context $ Just $
312+ Error.RigidSuperTooGeneric otherSuper name (Error.SpecificSuper super)
311− Var Flex Nothing _ ->
314+ FlexVar _ ->
312 merge context content
315 merge context content
314− Var Flex (Just otherSuper) _ ->
317+ FlexSuper otherSuper _ ->
315 case super of
318 case super of
316 Number ->
319 Number ->
317 case otherSuper of
320 case otherSuper of
318 Number -> merge context content
321 Number -> merge context content
319 Comparable -> merge context content
322 Comparable -> merge context content
320− _ -> mismatch context $ Just $ badSupers super otherSuper
323+ _ -> mismatch context Nothing -- Error.NumberAppendableClash
322 Comparable ->
325 Comparable ->
323 case otherSuper of
326 case otherSuper of
324 Comparable -> merge context otherContent
327 Comparable -> merge context otherContent
325 Number -> merge context otherContent
328 Number -> merge context otherContent
326− Appendable -> merge context (Var Flex (Just CompAppend) Nothing)
329+ Appendable -> merge context (Type.unnamedFlexSuper CompAppend)
327 CompAppend -> merge context otherContent
330 CompAppend -> merge context otherContent
329 Appendable ->
332 Appendable ->
330 case otherSuper of
333 case otherSuper of
331 Appendable -> merge context otherContent
334 Appendable -> merge context otherContent
332− Comparable -> merge context (Var Flex (Just CompAppend) Nothing)
335+ Comparable -> merge context (Type.unnamedFlexSuper CompAppend)
333 CompAppend -> merge context otherContent
336 CompAppend -> merge context otherContent
334− Number -> mismatch context $ Just $ badSupers super otherSuper
337+ Number -> mismatch context Nothing -- Error.NumberAppendableClash
336 CompAppend ->
339 CompAppend ->
337 case otherSuper of
340 case otherSuper of
338 Comparable -> merge context content
341 Comparable -> merge context content
339 Appendable -> merge context content
342 Appendable -> merge context content
340 CompAppend -> merge context content
343 CompAppend -> merge context content
341− Number -> mismatch context $ Just $ badSupers super otherSuper
344+ Number -> mismatch context Nothing -- Error.NumberAppendableClash
343 Alias _ _ realVar ->
346 Alias _ _ realVar ->
344 subUnify context (_first context) realVar
347 subUnify context (_first context) realVar
@@ -371,54 +374,54 @@ atomMatchesSuper super name =
373unifyFlexSuperStructure :: Context -> Super -> FlatType -> Unify ()
376unifyFlexSuperStructure :: Context -> Super -> FlatType -> Unify ()
374−unifyFlexSuperStructure context super term =
377+unifyFlexSuperStructure context super flatType =
375− do case term of
378+ case flatType of
376− App1 name [] ->
379+ App1 name [] ->
377− if atomMatchesSuper super name then
380+ if atomMatchesSuper super name then
378− merge context (Structure term)
381+ merge context (Structure flatType)
380− mismatch context (Just (badSuper super))
383+ mismatch context $ Just $ Error.NotPartOfSuper super
382− App1 name [variable] | name == Var.list ->
385+ App1 name [variable] | name == Var.list ->
383− case super of
386+ case super of
384− Number ->
387+ Number ->
385− mismatch context (Just (badSuper super))
388+ mismatch context $ Just $ Error.NotPartOfSuper super
388− merge context (Structure term)
391− do comparableOccursCheck context
392− merge context (Structure term)
393− unifyComparableRecursive (_orientation context) variable
395− CompAppend ->
390+ Appendable ->
396− do comparableOccursCheck context
397− merge context (Structure term)
391+ merge context (Structure flatType)
398− unifyComparableRecursive (_orientation context) variable
400− App1 name args | Var.isTuple name ->
393+ Comparable ->
401− case super of
394+ do comparableOccursCheck context
402− Number ->
395+ merge context (Structure flatType)
403− mismatch context (Just (badSuper super))
396+ unifyComparableRecursive (_orientation context) variable
405− Appendable ->
398+ CompAppend ->
406− mismatch context (Just (badSuper super))
399+ do comparableOccursCheck context
400+ merge context (Structure flatType)
401+ unifyComparableRecursive (_orientation context) variable
408− Comparable ->
403+ App1 name args | Var.isTuple name ->
409− if length args > 6 then
404+ case super of
410− mismatch context (Just (Error.TooLongComparableTuple (length args)))
406+ mismatch context $ Just $ Error.NotPartOfSuper super
412− else
408+ Appendable ->
413− do comparableOccursCheck context
409+ mismatch context $ Just $ Error.NotPartOfSuper super
414− merge context (Structure term)
415− mapM_ (unifyComparableRecursive (_orientation context)) args
417− CompAppend ->
411+ Comparable ->
412+ if length args > 6 then
418− mismatch context (Just (badSuper super))
413+ mismatch context (Just (Error.TooLongComparableTuple (length args)))
416+ do comparableOccursCheck context
417+ merge context (Structure flatType)
418+ mapM_ (unifyComparableRecursive (_orientation context)) args
421+ mismatch context $ Just $ Error.NotPartOfSuper super
421− mismatch context (Just (badSuper super))
424+ mismatch context $ Just $ Error.NotPartOfSuper super
424-- TODO: is there some way to avoid doing this?
427-- TODO: is there some way to avoid doing this?
@@ -435,7 +438,7 @@ unifyComparableRecursive :: Orientation -> Variable -> Unify ()
435unifyComparableRecursive orientation var =
438unifyComparableRecursive orientation var =
436 do compVar <- liftIO $
439 do compVar <- liftIO $
437 do (Descriptor _ rank _ _) <- UF.descriptor var
440 do (Descriptor _ rank _ _) <- UF.descriptor var
438− UF.fresh $ Descriptor (Var Flex (Just Comparable) Nothing) rank noMark Nothing
441+ UF.fresh $ Descriptor (Type.unnamedFlexSuper Comparable) rank noMark Nothing
440 lift $ TS.register compVar
443 lift $ TS.register compVar
@@ -465,13 +468,16 @@ collectArgsHelp revArgs variable =
465unifyAlias :: Context -> Var.Canonical -> [(Text, Variable)] -> Variable -> Content -> Unify ()
468unifyAlias :: Context -> Var.Canonical -> [(Text, Variable)] -> Variable -> Content -> Unify ()
466unifyAlias context name args realVar otherContent =
469unifyAlias context name args realVar otherContent =
467 case otherContent of
470 case otherContent of
468− Error _ ->
471+ FlexVar _ ->
471− Var Flex Nothing _ ->
472 merge context (Alias name args realVar)
472 merge context (Alias name args realVar)
474− Var _ _ _ ->
474+ FlexSuper _ _ ->
475+ subUnify context realVar (_second context)
478+ subUnify context realVar (_second context)
475 subUnify context realVar (_second context)
481 subUnify context realVar (_second context)
477 Alias otherName otherArgs otherRealVar ->
483 Alias otherName otherArgs otherRealVar ->
@@ -485,31 +491,34 @@ unifyAlias context name args realVar otherContent =
485 Structure _ ->
491 Structure _ ->
486 subUnify context realVar (_second context)
492 subUnify context realVar (_second context)
490-- UNIFY STRUCTURES
499-- UNIFY STRUCTURES
493unifyStructure :: Context -> FlatType -> Content -> Content -> Unify ()
502unifyStructure :: Context -> FlatType -> Content -> Content -> Unify ()
494−unifyStructure context term content otherContent =
503+unifyStructure context flatType content otherContent =
495 case otherContent of
504 case otherContent of
496− Error _ ->
505+ FlexVar _ ->
499− Var Flex Nothing _ ->
500 merge context content
506 merge context content
502− Var Flex (Just super) _ ->
508+ FlexSuper super _ ->
503− unifyFlexSuperStructure (reorient context) super term
509+ unifyFlexSuperStructure (reorient context) super flatType
505− Var Rigid _ maybeName ->
511+ RigidVar name ->
506− mismatch context (Just (Error.flipReason (badRigid maybeName)))
512+ mismatch context $ Just $ Error.RigidVarTooGeneric name (flatTypeToSpecificThing flatType)
514+ RigidSuper super name ->
515+ mismatch context $ Just $ Error.RigidSuperTooGeneric super name (flatTypeToSpecificThing flatType)
508 Alias _ _ realVar ->
517 Alias _ _ realVar ->
509 subUnify context (_first context) realVar
518 subUnify context (_first context) realVar
511 Structure otherTerm ->
520 Structure otherTerm ->
512− case (term, otherTerm) of
521+ case (flatType, otherTerm) of
513 (App1 name args, App1 otherName otherArgs) ->
522 (App1 name args, App1 otherName otherArgs) ->
514 if name == otherName then
523 if name == otherName then
515 do zipWithM_ (subUnify context) args otherArgs
524 do zipWithM_ (subUnify context) args otherArgs
@@ -543,6 +552,10 @@ unifyStructure context term content otherContent =
544 mismatch context Nothing
553 mismatch context Nothing
547isIntFloat :: Var.Canonical -> Var.Canonical -> Bool
560isIntFloat :: Var.Canonical -> Var.Canonical -> Bool
548isIntFloat name otherName =
561isIntFloat name otherName =
@@ -595,7 +608,7 @@ unifyRecord context firstStructure secondStructure =
596 (Extension, False, Extension, False) ->
609 (Extension, False, Extension, False) ->
597 do let subFields = Map.union uniqueExpFields uniqueActFields
610 do let subFields = Map.union uniqueExpFields uniqueActFields
598− subExt <- fresh context (Var Flex Nothing Nothing)
611+ subExt <- fresh context Type.unnamedFlexVar
599 expRecord <- fresh context (Structure (Record1 uniqueActFields subExt))
612 expRecord <- fresh context (Structure (Record1 uniqueActFields subExt))
600 actRecord <- fresh context (Structure (Record1 uniqueExpFields subExt))
613 actRecord <- fresh context (Structure (Record1 uniqueExpFields subExt))
601 subUnify context expVar expRecord
614 subUnify context expVar expRecord