[Haskell-cafe] F# active patterns versus GHC's view

Peter Verswyvelen bugfact at gmail.com
Thu Jan 15 17:48:52 EST 2009


When I first read about active patterns in F#, I found it really cool idea,
since it allows creating fake data constructors that can be used for pattern
matching, giving many views to a single piece of data, and allowing
backwards compatibility when you completely change or hide a data structure.
So for example one could define a Polar pattern and a Rect pattern that give
different views of a Complex number, e.g (pseudo code follows)

pattern Polar c = (mag c, phase c)
pattern Rect c = (real c, imag c)

This seems handy:

polarMul (Polar m1 p1) (Polar m2 p2) = mkComplexFromPolar (m1*m2) (p1+p2)

However, I think it is flawed, since the following

case c of
     Polar _ _ -> "it's polar!"
     Rect _ _ -> "it's rect!"

seems like valid code but does not make any sense.

So I think the GHC extension of view patterns is better than the active
patterns in F#?

A good coding style is to provide constructor functions and hide data
constructors. But then one looses the ability to perform pattern matching,
which is so cool in Haskell. Would I have to conclude that it would be good
coding style to use view patterns as much as possible in Haskell,
creating auxiliary data constructors to expose the "public members" of the
hidden data constructors?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20090115/2db935f3/attachment.htm


More information about the Haskell-Cafe mailing list