[Haskell-cafe] The Good, the Bad and the GUI

Tom Ellis tom-lists-haskell-cafe-2013 at jaguarpaw.co.uk
Wed Aug 13 10:40:20 UTC 2014


On Tue, Aug 12, 2014 at 09:23:35PM +0200, Wojciech Narczyński wrote:
> W dniu 2014-08-12 15:01, Daniel Gorín pisze:
> >On 11 Aug 2014, at 23:16, Wojtek Narczyński <wojtek at power.com.pl> wrote:
> >
> >>If you declare Person class in Java, you automatically get a thingy that you can readily use in UI construction, because all the fields can temporarily be null, even the required ones. In Haskell you'd need two data types: the usual proper Haskell data type, and another which wraps every field in Maybe, facilitates editing, validation, etc. Perhaps it would be possible to generate one data type from the other, or generate both from a common specification.
> >At least this part you can achieve rather easily by parametrizing each field in your record by a functor. E.g.:
> >
> >data Person f
> >   = Person {
> >     firstName :: f String
> >   ,lastName :: f String
> >   ,birthDate :: f Date
> >   ,height :: f Int
> >   }
> >
> >Then you get:
> >
> >  - "Person Id" is a person with every field set in stone.
> >
> >  - "Person Maybe" is a person with missing information.
> >
> >  - Other functors can be easily defined (and then composed) to represent things such as Mandatory/Optional, Valid/Invalid, etc.
>
> I saw in the presentation submitted in another post that a similar
> thing has been done in Vinyl. But this won't work so well, because
> in the "rigid" type some fields are still supposed to remain wrapped
> in Maybe.

Then make the Person type completely freely polymorphic

    data Person f l b h = Person {
       firstName :: f
     , lastName :: l
     , birthDate :: b
     , height :: h
    }

I do this a lot, and with suitible type synonyms is really a nice way of
working (especially with lenses).

Tom


More information about the Haskell-Cafe mailing list