Pattern match checking across monadic bind
As noted by @barci2, it would be nice if the following kind of programs didn't cause any pattern match warnings to be emitted:
data T = T1 { fld :: Int } | T2
foo :: [T]
foo = do { T1 {} <- [a,b]; v <- [ b { fld = 4 }, a { fld = 3 } ]; return v }
bar :: Maybe T
bar = do { T1 {} <- Just c; return $ c { fld = 5 } }
In these examples, a
, b
, and c
should all be considered to be scrutinees from the point of view of the function matchSinglePatVar
. Doing so would then mean that the call to addCoreScrutTmCs
inside that function will propagate the fact that a
, b
, and c
are all of the form T1 _
, which will allow GHC to see that the record updates are not partial.
To do this, in the compiler, we would need some kind of function:
bindStmtRhsScruts :: XBindStmtTc -> CoreExpr -> [CoreExpr]
which would be able to extract out scrutinees from an expression, e.g.
bindStmtRhsScruts ( BindStmt for List ) ( Core of explicit list "[a,b]" ) = [a,b]
bindStmtRhsScruts ( BindStmt for Maybe ) ( Core of "Just c" ) = [c]
I'm not sure how we would implement this function in general, without implementing ad-hoc logic for known container types such as lists.
@sgraf812, do you have any thoughts?