Skip to content

Commit

Permalink
Flatten TypeError
Browse files Browse the repository at this point in the history
  • Loading branch information
Ian Grant Jeffries committed Nov 10, 2019
1 parent 8f09c74 commit 497f51b
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
8 changes: 4 additions & 4 deletions bowtie/src/Bowtie/Infer/Unify.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import Bowtie.Surface.AST
import qualified Data.Set as Set

data UnifyError
= UnifyError Type Type
| OccursCheckFailed Id Type
= TypeMismatch Type Type
| IdOccursInType Id Type
deriving (Eq, Show)

-- | Implementation based on <doc.md#AlgorithmWStepByStep>.
Expand Down Expand Up @@ -67,13 +67,13 @@ unify t1 t2 =
Right (s1 <> s2)

_ ->
Left (UnifyError t1 t2)
Left (TypeMismatch t1 t2)
where
unifyVariable :: Id -> Type -> Either UnifyError Substitution
unifyVariable id typ =
if Set.member id (freeVars typ)
then
Left (OccursCheckFailed id typ)
Left (IdOccursInType id typ)

else
Right (singleSub id typ)
12 changes: 9 additions & 3 deletions bowtie/src/Bowtie/Surface/Infer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import qualified Data.Set as Set

data TypeError
= SolveStuck
| SolveUnifyError UnifyError
| UnifyError Type Type
| OccursCheckFailed Id Type
| AssumptionsRemain Assumptions
deriving (Eq, Show)

Expand Down Expand Up @@ -96,8 +97,13 @@ instance CanSolveStuck Infer where

instance CanUnifyError Infer where
failUnifyError :: UnifyError -> Infer a
failUnifyError =
throwError . SolveUnifyError
failUnifyError e =
throwError (case e of
TypeMismatch t1 t2 ->
UnifyError t1 t2

IdOccursInType id t ->
OccursCheckFailed id t)

instance CanAssumptionsRemain Infer where
failAssumptionsRemain :: Assumptions -> Infer a
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SolveUnifyError (UnifyError (TConstructor (Id "Int")) (TConstructor (Id "Bool")))
UnifyError (TConstructor (Id "Int")) (TConstructor (Id "Bool"))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SolveUnifyError (UnifyError (TConstructor (Id "Int")) (TConstructor (Id "Bool")))
UnifyError (TConstructor (Id "Int")) (TConstructor (Id "Bool"))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SolveUnifyError (UnifyError (TConstructor (Id "Int")) (TConstructor (Id "Bool")))
UnifyError (TConstructor (Id "Int")) (TConstructor (Id "Bool"))
Original file line number Diff line number Diff line change
@@ -1 +1 @@
SolveUnifyError (UnifyError (TConstructor (Id "Int")) (TConstructor (Id "Bool")))
UnifyError (TConstructor (Id "Int")) (TConstructor (Id "Bool"))

0 comments on commit 497f51b

Please sign in to comment.