pattern-matching extension?

Abraham Egnor aegnor at antioch-college.edu
Wed Dec 3 15:10:07 EST 2003


I've occasionally wanted some sort of equivalent of an instanceOf function
in haskell, i.e. one that would let me define a function that could
dispatch on the type of its argument as well as the value.  One option
I've seen for this is
"http://okmij.org/ftp/Haskell/class-based-dispatch.lhs", but that
unfortunately has the downside of requiring you to write both a
constructor for PACK and an instance of Packable for each type you'd like
to dispatch on.

The thought occurred to me that it is (intuitively) natural to do this via
extending the pattern-matching facility to include types as well as
literal values, i.e. something like:

f :: a -> String
f (a :: Int) = "got an int, incremented: "++(show (a+1))
f (a :: Show q => q) = "got a showable: "++(show a)
f _ = "got something else"

This has a couple of nice features - it's a simple extension of the
syntax, and acts as a sort of type-safe typecast.  However, I have zero
knowledge of how hard it would be to implement this, and there may be
theoretical difficulties I haven't seen.  Thoughts?

Abe



More information about the Haskell mailing list