pattern-matching with labelled types

Jorge Adriano jadrian@mat.uc.pt
Fri, 8 Mar 2002 14:38:29 +0000


On Friday 08 March 2002 01:52, you wrote:
> Andre W B Furtado writes:
>  | Of course, it is possible to do something like
>  |
>  | > update :: MyType -> Int -> MyType
>  | > update mt newValue = MT {x = newValue, y = oldValue}
>  | >     where oldValue = y mt
>  |
>  | but this really annoys me when MyType has too many fields. Suggestions?
>
> update mt newValue = mt {x = newValue}

Since we are on the subject, I have some questions about labeled datatypes 
updates.
You can in deed update like that "mt {x = newValue}", but whenever I use 
labeled datatypes I always end up with Andres problem, having to define not 
only an 'update' function but also an 'apply' function for every field.
I need them becouse I want to pass them as arguments to other funtions.

This happens to me all the time, but one very good example IMO is having a 
STRef to a labeled data type. Now you want to update some field and you want 
to to be able to use,
modifySTRef (updField1 3)
or
modifySTRef (appField1 (+1))

It would be extremely usefull IMO to have not only field projection funtions, 
but also apply and update funtions. That, and not beeing possible to specify 
contexts partialy, makes it particulary complicated for me to follow John 
Hughes strategy to simulate global variables in haskell 
(http://www.cs.chalmers.se/~rjmh/Globals.ps)

J.A.