They are not equivalent. The case version has one readMaybe , the presentation template version has two. For each readMaybe compiler must determine what type is the target of the read attempt. When the code says
parse xs x = case readMaybe x of Just x -> Right (x : xs) Nothing -> Left "Syntax error
the GHC detective notes that in your case, Just x x ends with xs , and therefore it must accept any type of xs element. And this is a good job.
But when you write
parse xs (readMaybe -> Just x ) = Right (x : xs) parse xs (readMaybe -> Nothing) = Left "Syntax error"
you create two separate find-target problems, one for each use of readMaybe . The first of them is solved exactly the same as in the case case , but for the second it is read separately,
parse xs (readMaybe -> Nothing) = Left "Syntax error"
there is no idea what it is that you are not reading, and there is no reason to believe that it is the same as in the line above.
It is generally not practical to use presentation templates unless there is only one result of interest. They are the wrong syntax if you want to do an intermediate calculation once, but analyze the result in several cases. I am happy to stay on the record that I believe that they are erroneous for this reason.
pigworker
source share