@@ -78,20 +78,46 @@ data Mismatch
78mismatch :: Context -> Maybe Error.Reason -> Unify a
78mismatch :: Context -> Maybe Error.Reason -> Unify a
79mismatch (Context orientation first _ second _) maybeReason =
79mismatch (Context orientation first _ second _) maybeReason =
81− (expected, actual) =
81+ (expected, actual, orientedReason) =
82 case orientation of
82 case orientation of
83 ExpectedActual ->
83 ExpectedActual ->
84− (first, second)
84+ (first, second, maybeReason)
86 ActualExpected ->
86 ActualExpected ->
87− (second, first)
87+ (second, first, Error.flipReason <$> maybeReason)
89− throwError (Mismatch expected actual maybeReason)
89+ throwError (Mismatch expected actual orientedReason)
92−rigidityError :: Context -> Maybe String -> Unify ()
92+badRigid :: Maybe String -> Error.Reason
93−rigidityError context maybeName =
93+badRigid maybeName =
94− mismatch context (Just (Error.Rigid maybeName))
94+ Error.BadVar (Just (Error.Rigid maybeName)) Nothing
97+badSuper :: Super -> Error.Reason
99+ Error.BadVar (Just (errorSuper super)) Nothing
102+doubleBad :: Error.VarType -> Error.VarType -> Error.Reason
104+ Error.BadVar (Just vt1) (Just vt2)
107+errorSuper :: Super -> Error.VarType
@@ -218,19 +244,20 @@ unifyRigid context maybeSuper maybeName otherContent =
218 if maybeSuper == otherMaybeSuper then
244 if maybeSuper == otherMaybeSuper then
219 merge context (Var Rigid maybeSuper maybeName)
245 merge context (Var Rigid maybeSuper maybeName)
221− rigidityError context maybeName
247+ mismatch context (Just (badRigid maybeName))
223− Var Rigid _ _ ->
249+ Var Rigid _ otherMaybeName ->
224− mismatch context (Just Error.DoubleRigid)
250+ mismatch context $ Just $
251+ doubleBad (Error.Rigid maybeName) (Error.Rigid otherMaybeName)
226 Atom _ ->
253 Atom _ ->
227− rigidityError context maybeName
254+ mismatch context (Just (badRigid maybeName))
229 Alias _ _ _ ->
256 Alias _ _ _ ->
230− rigidityError context maybeName
257+ mismatch context (Just (badRigid maybeName))
232 Structure _ ->
259 Structure _ ->
233− rigidityError context maybeName
260+ mismatch context (Just (badRigid maybeName))
236-- UNIFY SUPER VARIABLES
263-- UNIFY SUPER VARIABLES
@@ -246,26 +273,26 @@ unifySuper context super otherContent =
246 if atomMatchesSuper super name then
273 if atomMatchesSuper super name then
247 merge context otherContent
274 merge context otherContent
249− mismatch context (Just (superReason super))
276+ mismatch context (Just (badSuper super))
251 Var Rigid Nothing maybeName ->
278 Var Rigid Nothing maybeName ->
252− rigidityError context maybeName
279+ mismatch context (Just (doubleBad (errorSuper super) (Error.Rigid maybeName)))
254 Var Rigid (Just otherSuper) maybeName ->
281 Var Rigid (Just otherSuper) maybeName ->
255 if super == otherSuper then
282 if super == otherSuper then
256 merge context otherContent
283 merge context otherContent
258− rigidityError context maybeName
285+ mismatch context (Just (doubleBad (errorSuper super) (Error.Rigid maybeName)))
260 Var Flex Nothing _ ->
287 Var Flex Nothing _ ->
261 merge context (Var Flex (Just super) Nothing)
288 merge context (Var Flex (Just super) Nothing)
263 Var Flex (Just otherSuper) _ ->
290 Var Flex (Just otherSuper) _ ->
264 case combineSupers super otherSuper of
291 case combineSupers super otherSuper of
265− Nothing ->
292+ Left reason ->
266− mismatch context Nothing
293+ mismatch context (Just reason)
268− Just newSuper ->
295+ Right newSuper ->
269 merge context (Var Flex (Just newSuper) Nothing)
296 merge context (Var Flex (Just newSuper) Nothing)
271 Alias _ _ realVar ->
298 Alias _ _ realVar ->
@@ -275,42 +302,27 @@ unifySuper context super otherContent =
275 return ()
302 return ()
278−combineSupers :: Super -> Super -> Maybe Super
305+combineSupers :: Super -> Super -> Either Error.Reason Super
279combineSupers firstSuper secondSuper =
306combineSupers firstSuper secondSuper =
280 case (firstSuper, secondSuper) of
307 case (firstSuper, secondSuper) of
281− (Number , Number ) -> Just Number
308+ (Number , Number ) -> Right Number
282− (Comparable, Number ) -> Just Number
309+ (Comparable, Number ) -> Right Number
283− (Number , Comparable) -> Just Number
310+ (Number , Comparable) -> Right Number
285− (Comparable, Comparable) -> Just Comparable
286− (Appendable, Appendable) -> Just Appendable
288− (Appendable, Comparable) -> Just CompAppend
312+ (Comparable, Comparable) -> Right Comparable
289− (Comparable, Appendable) -> Just CompAppend
313+ (Appendable, Appendable) -> Right Appendable
291− (CompAppend, CompAppend) -> Just CompAppend
292− (CompAppend, Comparable) -> Just CompAppend
315+ (Appendable, Comparable) -> Right CompAppend
293− (Comparable, CompAppend) -> Just CompAppend
316+ (Comparable, Appendable) -> Right CompAppend
294− (CompAppend, Appendable) -> Just CompAppend
295− (Appendable, CompAppend) -> Just CompAppend
297− (_ , _ ) -> Nothing
318+ (CompAppend, CompAppend) -> Right CompAppend
319+ (CompAppend, Comparable) -> Right CompAppend
320+ (Comparable, CompAppend) -> Right CompAppend
321+ (CompAppend, Appendable) -> Right CompAppend
322+ (Appendable, CompAppend) -> Right CompAppend
300−superReason :: Super -> Error.Reason
325+ Left $ doubleBad (errorSuper firstSuper) (errorSuper secondSuper)
316isPrimitiveFrom :: [String] -> Var.Canonical -> Bool
328isPrimitiveFrom :: [String] -> Var.Canonical -> Bool
@@ -339,12 +351,12 @@ unifySuperStructure context super term =
339 do appStructure <- liftIO (collectApps (Structure term))
351 do appStructure <- liftIO (collectApps (Structure term))
340 case appStructure of
352 case appStructure of
342− mismatch context (Just (superReason super))
354+ mismatch context (Just (badSuper super))
344 List variable ->
356 List variable ->
345 case super of
357 case super of
346 Number ->
358 Number ->
347− mismatch context (Just Error.NotNumber)
359+ mismatch context (Just (badSuper super))
349 Appendable ->
361 Appendable ->
350 merge context (Structure term)
362 merge context (Structure term)
@@ -360,10 +372,10 @@ unifySuperStructure context super term =
360 Tuple entries ->
372 Tuple entries ->
361 case super of
373 case super of
362 Number ->
374 Number ->
363− mismatch context (Just Error.NotNumber)
375+ mismatch context (Just (badSuper super))
365 Appendable ->
377 Appendable ->
366− mismatch context (Just Error.NotAppendable)
378+ mismatch context (Just (badSuper super))
368 Comparable ->
380 Comparable ->
369 if length entries > 6 then
381 if length entries > 6 then
@@ -374,7 +386,7 @@ unifySuperStructure context super term =
374 mapM_ (unifyComparableRecursive (_orientation context)) entries
386 mapM_ (unifyComparableRecursive (_orientation context)) entries
376 CompAppend ->
388 CompAppend ->
377− mismatch context (Just Error.NotCompAppend)
389+ mismatch context (Just (badSuper super))
380unifyComparableRecursive :: Orientation -> Variable -> Unify ()
392unifyComparableRecursive :: Orientation -> Variable -> Unify ()
@@ -443,10 +455,10 @@ unifyAtom context name otherContent =
443 merge context (Atom name)
455 merge context (Atom name)
446− mismatch context (Just (superReason super))
458+ mismatch context (Just ((Error.flipReason (badSuper super))))
448 Var Rigid _ maybeName ->
460 Var Rigid _ maybeName ->
449− rigidityError context maybeName
461+ mismatch context (Just (Error.flipReason (badRigid maybeName)))
451 Atom otherName ->
463 Atom otherName ->
452 if name == otherName then
464 if name == otherName then
@@ -519,7 +531,7 @@ unifyStructure context term otherContent =
519 unifySuper (reorient context) super (Structure term)
531 unifySuper (reorient context) super (Structure term)
521 Var Rigid _ maybeName ->
533 Var Rigid _ maybeName ->
522− rigidityError context maybeName
534+ mismatch context (Just (Error.flipReason (badRigid maybeName)))
524 Atom _ ->
536 Atom _ ->
525 mismatch context Nothing
537 mismatch context Nothing