Proposal: pattern synonyms
Conor McBride
ctm at Cs.Nott.AC.UK
Thu Feb 16 06:10:59 EST 2006
Malcolm Wallace wrote:
>Conor McBride <ctm at cs.nott.ac.uk> writes:
>
>
>
>> P x y z = C x (y, [z])
>>
>>
>
>Isn't this idea very similar to views, and pattern-guards?
>
>
Clearly related, but rather cheaper. I don't want to make remarks,
positive or negative about either of those proposals at this juncture. I
see pattern synonyms, like type synonyms, as a convenient abbreviation
mechanism in a part of the language where verbosity is currently compulsory.
>For instance, you could rewrite your example thus:
>
>data EffectView x = Bang
> | Bing x
> | Dull x
>
>view :: MyEffect x -> EffectView x
>view (Comp Nothing) = Bang
>view (Comp (Just (Prod (Any True), Id x))) = Bing x
>view (Comp (Just (Prod (Any False), Id x))) = Dull x
>
>my_function e
> | Bang <- view effect = ...
> | Bing x <- view effect = ... x ...
> | Dull x <- view effect = ... x ...
>
>
You forgot to define the term behaviour also. Pattern synonyms may be
used to construct as well as to match values. Of course, I could also write
weiv :: EffectView x -> MyEffect x
weiv Bang = ...
weiv (Bing x) = ...
weiv (Dull x) = ...
or I could just add them as definitions with lower-case initials.
The pattern synonym gives me both at once, and their relationship.
Moreover, I don't have to either (a) cover the entire type with pattern
synonyms or (b) write a partial view; I just abbreviate whenever I find
it convenient. More moreover, your way, the need to interpolate the view
encoder forces me to do my matching in a guard, rather than an argument
pattern, hence the need to say 'view effect' three times, or however
many it happens to be, and some irritation in mixing matching on
MyEffect x with matching on other arguments.
But perhaps constructing explicit embedding-projection pairs is a better
solution than an abbreviation mechanism, for most people's needs.
All the best
Conor
This message has been checked for viruses but the contents of an attachment
may still contain software viruses, which could damage your computer system:
you are advised to perform your own checks. Email communications with the
University of Nottingham may be monitored as permitted by UK legislation.
More information about the Haskell-prime
mailing list