[Haskell-cafe] Question on type synonym definition and language extensions

Olivier Boudry olivier.boudry at gmail.com
Thu May 29 16:34:32 EDT 2008

Hi all,

I'm trying to define a type synonym for 2D MArray instances. The goal is to
keep the function signature simple and readable using type `Matrix` instead
of something like `(Ix i, MArray a e m) => m (a i e)`.

After some "read, guess, try, error" cycles I came up with this:

    type Matrix = forall m. forall a. forall i. forall n. (Ix i, MArray a n
m, Num i, Num n) => m (a (i,i) n)

it requires option -XRank2Types to work. But then I can write my function

    test :: Matrix
    test = do { a <- newArray ((0,0),(5,8)) 0; writeArray a (0,0) 1; return
a }

Then I wanted to be able to give the Index and Value types in the type
synonym but keep it flexible in terms of which MArray instance is used. I
changed it to:

    type Matrix i n = forall m. forall a. (MArray a n m) => m (a (i,i) n)

and the type signature of the test function becomes:

    test :: Matrix Int Double

For this one I had to add an extra -XFlexibleContexts option to build it.

It works and I'm quite happy with the look of my new function type
signatures, but I'm just wondering if I'm doing something bad or if there is
a cleaner/simpler way to define the same type synonym without requiring
language extensions. What are the risks associated with using these two
language extensions, non compatibility with other compilers or more?


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20080529/8ce36f3e/attachment.htm

More information about the Haskell-Cafe mailing list