Let's say I have the following code (text in <>
is a shorthand, not actually part of the code):
data A = <something>
defaultA :: A
defaultA = <Really complicated expression of type A>
Now I want to have a function pattern match on defaultA
, like this:
f defaultA = <case 1>
f _ = <case 2>
However, defaultA
in the first line becomes a new variable, not a condition that means the parameter will equal defaultA
. The best way I know to achieve something like what I want is:
f x | x == defaultA = <case 1>
f _ = <case 2>
Does anyone know a better way?
If the definiton of
defaultA
consists only of constructor calls, you could use a pattern synonym.This isn't a particularly idiomatic deployment of
PatternSynonyms
though. I'd probably stick with Haskell 98, using a very-slightly-more-verbose guard clause with an equality test.Where pattern synonyms do come in useful is for wrapping up noisy library constructor calls imposed on you when you're doing datatype-generic programming with a pattern like free monads or Data Types a la Carte.