Skip to content

Commit

Permalink
Branch reasons.
Browse files Browse the repository at this point in the history
  • Loading branch information
athas committed Oct 13, 2024
1 parent fba5782 commit 29293af
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/Language/Futhark/TypeChecker/Constraints.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ data Reason
ReasonAscription Loc Type Type
| ReasonRetType Loc Type Type
| ReasonApply Loc (Maybe (QualName VName)) Exp Type Type
| ReasonBranches Loc Type Type
deriving (Eq, Show)

instance Located Reason where
Expand All @@ -77,6 +78,7 @@ instance Located Reason where
locOf (ReasonAscription l _ _) = l
locOf (ReasonRetType l _ _) = l
locOf (ReasonApply l _ _ _ _) = l
locOf (ReasonBranches l _ _) = l

data Ct
= CtEq Reason Type Type
Expand Down Expand Up @@ -359,6 +361,14 @@ cannotUnify reason notes bcs t1 t2 = do
<+> "to"
<+> dquotes (align $ shorten $ group $ pretty e)
<> " (invalid type)."
ReasonBranches loc former latter -> do
former' <- enrichType former
latter' <- enrichType latter
typeError loc notes . stack $
[ "Branches differ in type.",
"Former:" <+> pretty former',
"Latter:" <+> pretty latter'
]

-- Precondition: 'v' is currently flexible.
subTyVar :: Reason -> BreadCrumbs -> VName -> Type -> SolveM ()
Expand Down
6 changes: 3 additions & 3 deletions src/Language/Futhark/TypeChecker/Terms2.hs
Original file line number Diff line number Diff line change
Expand Up @@ -810,7 +810,7 @@ checkCases mt rest_cs =
(c, Just cs) -> do
(c', c_t) <- checkCase mt c
(cs', cs_t) <- checkCases mt cs
ctEq (Reason (locOf c)) c_t cs_t
ctEq (ReasonBranches (locOf c) c_t cs_t) c_t cs_t
pure (NE.cons c' cs', c_t)

-- | An unmatched pattern. Used in in the generation of
Expand Down Expand Up @@ -1203,8 +1203,8 @@ checkExp (AppExp (If e1 e2 e3 loc) _) = do
if_t <- newType loc SizeLifted "if_t" NoUniqueness

ctEq (Reason (locOf e1')) e1_t (Scalar (Prim Bool))
ctEq (Reason (locOf loc)) e2_t if_t
ctEq (Reason (locOf loc)) e3_t if_t
ctEq (ReasonBranches (locOf loc) e2_t e3_t) e2_t if_t
ctEq (ReasonBranches (locOf loc) e2_t e3_t) e3_t if_t

if_t' <- asStructType if_t
pure $ AppExp (If e1' e2' e3' loc) (Info $ AppRes if_t' [])
Expand Down

0 comments on commit 29293af

Please sign in to comment.