[Haskell] View patterns in GHC: Request for feedback

ajb at spamcop.net ajb at spamcop.net
Wed Jul 25 07:21:35 EDT 2007


G'day all.

Quoting Claus Reinke <claus.reinke at talk21.com>:

> different =/= wrong !-)

[...]

> but that is not what you're saying there at all! you're saying that -within
> view 'view' of Typ- Typ is mapped to either Unit or Arrow, if the mapping
> is successfull. there can be other views of Typ, and the types do not
> guarantee that 'view' itself is exhaustive over Typ (there can be variants
> of Typ that 'view' fails to map to TypView).

data TypView = Unit | Arrow Typ Typ | Other

I still vote for "wrong". :-)

The problem, and we've been through this before, is that it's very
tempting to use types like Maybe because it's there, when it's better
replaced with a custom algebraic data type.

Even probably 90% of uses of Bool are better replaced with a two-element
enumerated type.  I see a lot of things like this:

    data Something = Something { ... leftHanded :: Bool ... }

Where this is safer, more expressive, more future-proof and more
meaningful:

    data Handedness = LeftHanded | RightHanded
    data Something = Something { ... handedness :: Handedness ... }

> even with concrete view types, you still have to consider the case that
> the mapping into that view type can be partial or non-exhaustive (if you
> add a constructor to Typ, but forget to update 'view', the type system
> will not complain, and matches over TypView will still be 'exhaustive'..).

There should be a compiler warning you can turn on in the implementation
of "view" which tells you if the pattern matching is exhaustive or not.

Cheers,
Andrew Bromage


More information about the Haskell mailing list