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?
where clause in the Haskell programming is bound to syntactic construct. but he use of where clause or function in Haskell we can remove the repetitive code as well, instead, we can hold the value into some variable and used it later.
(->) is often called the "function arrow" or "function type constructor", and while it does have some special syntax, there's not that much special about it. It's essentially an infix type operator. Give it two types, and it gives you the type of functions between those types.
Note that Haskell does not have an "elif" statement like Python.
() is very often used as the result of something that has no interesting result. For example, an IO action that is supposed to perform some I/O and terminate without producing a result will typically have type IO () .
Yes, A{}
matches any value constructed with the A
constructor, regardless of whether the type has been declared with record syntax or not.
The language report specifies
The expression F {}, where F is a data constructor, is legal whether or not F was declared with record syntax (provided F has no strict fields — see the fourth bullet above); it denotes F ⊥1 … ⊥n, where n is the arity of F.
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
apat -> qcon { fpat1 , … , fpatk } (labeled pattern, k ≥ 0)
and the semantics are given in the subsection on formal semantics of pattern-matching (3.17.3) as
(o) case v of { K {} -> e ; _ -> e′ } = case v of { K _… _ -> e ; _ -> e′ }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With