I just ran across the following syntax in a piece of Haskell code –
data A = A Int Int | B
m :: A -> Int
m a = case a of
A{} -> 1
_ -> 2
What is the A{} doing here? Does the {} automatically match for any number of arguments?
I have a feeling that this is exploiting the fact that Haskell record syntax desugars to a bunch of functions and a regular Algebraic Datatype. Is that the case?
Yes,
A{}matches any value constructed with theAconstructor, regardless of whether the type has been declared with record syntax or not.The language report specifies
The ‘fourth bullet’ mentioned in the parenthesis states that it is a static error to construct a value with record syntax which omits a strict field.
And in the section on pattern matching, one of the grammar rules for patterns is
and the semantics are given in the subsection on formal semantics of pattern-matching (3.17.3) as