Language extension idea (was Re: [Haskell-cafe] Re: OCaml list sees...)

Malcolm Wallace Malcolm.Wallace at
Sun Oct 10 06:20:00 EDT 2004

--- Tom Pledger wrote:
> > It could get more convenient, though, if we have
> > data-constructors-as-class-members and move the familiar list
> > constructors into the List class. (The following might need to be
> > built into the compiler, because of the special [] syntax.)

J.Garrett.Morris at Dartmouth.EDU (J. Garrett Morris) writes:

> Changing the subject very slightly, this brings up two questions:
> 1)  Wouldn't implementing the Views proposal allow this?

I think the answer is yes.

> 2)  Why haven't views been implemented (and why are they a "never"
>     in the HaskellTwo discussion in HaWiki)?

I can't speak to the 'never', but there /is/ an implementation in
ghc of something closely resembling Views, called Pattern Guards.

As an example, instead of the following list-only code,

    f :: List a -> ...
    f []    = ...
    f (h:t) = ...

you could write this more general version, which assumes only some
class Sequence with operations null, head, tail, etc.

    f :: Sequence s => s a -> ...
    f list | null list                       = ...
           | h <- head list, t <- tail list  = ...

Although slightly more verbose, it still achieves something like the
clarity of pattern-matching.


More information about the Haskell-Cafe mailing list