Unlifted type variables in GHC

What kind of problems could crop up if I try to create type variables with an unlifted kind inside GHC (not in user code, just inside the compiler)? I want to make ByteArray# and MutableByteArray# parameterized over their element types. ByteArray# would have kind # -> #, and MutableByteArray#, * -> # -> # . indexByteArray# would have the type (in pseudo haskell) forall (e::#). ByteArray# e -> Int# -> e. This would put all the ByteArray# operations (along with tagToEnum#) in that weird class of primops that have a polymorphic type but must be instantiated with a monomorphic type by the time it hits the code generator but that shouldn't be a big deal. Don't get too hung up on the ByteArray# example. I'm not actually proposing we change ByteArray# (I know it'll cause a few problems with the current UArray implementation). I'm just wondering if something like this is possible, and if so, what I should watch out for if I try to do it. Thanks, -Brian

| I want to make ByteArray# and MutableByteArray# parameterized over | their element types. ByteArray# would have kind # -> #, and | MutableByteArray#, * -> # -> # . indexByteArray# would have the type | (in pseudo haskell) forall (e::#). ByteArray# e -> Int# -> e. Probably a bad idea. The point is that you could then write polymorphic functions that would apparently work both on values of type Int# and Double#... but they are of different sizes. Seg-fault city! This stuff is not impossible. Microsoft's CLR allows exactly this, and compiles fresh code for each instantiation of a function at a type of different size or pointer-hood than the ones already compiled. But GHC does not do that, and won't anytime soon. The paper I wrote with John Launchbury "Unboxed types as first class citizens" elaborates, I think. Simon

On Fri, Dec 08, 2006 at 03:48:22PM +0000, Simon Peyton-Jones wrote:
| I want to make ByteArray# and MutableByteArray# parameterized over | their element types. ByteArray# would have kind # -> #, and | MutableByteArray#, * -> # -> # . indexByteArray# would have the type | (in pseudo haskell) forall (e::#). ByteArray# e -> Int# -> e.
Probably a bad idea. The point is that you could then write polymorphic functions that would apparently work both on values of type Int# and Double#... but they are of different sizes. Seg-fault city!
Right... which is why you'd need to make sure you never used those functions in a polymorphic way. The code generator should choke if it sees an unlifted type variable. I'm basically looking for a way to avoid enumerating every possible unlifted type in the ByteArray# operations. I'm probably not giving enough details for this to make any sense. I'm actually writing a new ghc backend and I have way more primitive types than the normal dozen or so (actually an infinite number since you can add new ones, I resurrected that "foreign type" code that was leftover from the .NET backend). I'll just try it and see how I make out. -Brian
participants (2)
-
Brian Alliet
-
Simon Peyton-Jones