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

2008-05-29 Thread Olivier Boudry
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
as:

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?

Thanks,

Olivier.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-05-29 Thread Henning Thielemann


On Thu, 29 May 2008, Olivier Boudry wrote:


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)



Is

 type Matrix monad array num = monad (array (Int,Int) num)

ok for you?
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-05-29 Thread Olivier Boudry
On Thu, May 29, 2008 at 4:38 PM, Henning Thielemann 
[EMAIL PROTECTED] wrote:

 Is

  type Matrix monad array num = monad (array (Int,Int) num)

 ok for you?


Not really what I was looking for but I may end up using it.

What I wanted is to hide the Monad and Array and have it inferred from the
context of the array

STArray or STUArray when run in the ST monad with runST(U)Array, IOArray or
IOUArray when used in the IO monad, etc...

I'm not sure it's the right thing to do. I may create more troubles than I
solve. I was just trying to keep things as generic as possible.

Thanks for your reply,

Olivier.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-05-29 Thread Antoine Latter
2008/5/29 Olivier Boudry [EMAIL PROTECTED]:
 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)

I've tried similar things before.  You may run into subtle problems later.

Such as:

 transpose :: Matrix - Matrix

won't expand into the type signature you want it to, I think.

You probably want that to be equivalent to:

transpose ::  forall m. forall a. forall i. forall n. (Ix i, MArray a
n m, Num i, Num n) = m (a (i,i) n) - m (a (i,i) n)

But you'll get:

transpose ::  forall m. forall a. forall i. forall n. (Ix i, MArray a
n m, Num i, Num n) = Matrix - m (a (i,i) n)

which means that the first argument must be a polymorphic value, which
isn't very useful.

-Antoine





-Antoine
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


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

2008-05-29 Thread Olivier Boudry
On Thu, May 29, 2008 at 7:36 PM, Antoine Latter [EMAIL PROTECTED] wrote:

 I've tried similar things before.  You may run into subtle problems later.

 Such as:

  transpose :: Matrix - Matrix

 won't expand into the type signature you want it to, I think.

 You probably want that to be equivalent to:

 transpose ::  forall m. forall a. forall i. forall n. (Ix i, MArray a
 n m, Num i, Num n) = m (a (i,i) n) - m (a (i,i) n)

 But you'll get:

 transpose ::  forall m. forall a. forall i. forall n. (Ix i, MArray a
 n m, Num i, Num n) = Matrix - m (a (i,i) n)

 which means that the first argument must be a polymorphic value, which
 isn't very useful.


Right, this is exactly what I'm getting when creating a transpose function:

*Main :t transpose
transpose :: (MArray a Double m) = Matrix Int Double - m (a (Int, Int)
Double)

Thanks for sharing your experience. I'll try to stay out of the subtle bugs
and keep it verbose but simple.

Regards,

Olivier.
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe