getting the right types
Hal Daume III
hdaume@ISI.EDU
Sun, 3 Nov 2002 11:30:52 -0800 (PST)
Hi all,
I have a function which essentially looks like this:
f my_data = do
a1 <- newArray ...
...
a2 <- newArray ...
g my_data a1 a2
where f is a monadic operation essentially of type 'a -> m a'. The
problem is that when this function isn't given a type signature, you get
something like:
f :: (MArray a1 p e, MArray a2 p e) => t
where a1 and a2 aren't bound in t. Now, I could provide a type signature
on the array expressions, something like:
... (a1 :: IOArray Int Int) <- newArray ...
but i would like to be able to use unboxed arrays when possible, and i
don't want to bind the funtion to be in IO. My current solution is to
define:
data ArrayType arr prob = forall ix . ArrayType (arr ix prob)
asArray :: arr ix prob -> ArrayType arr prob -> arr ix prob
a `asArray` _ = a
Sort of like asTypeOf. Then, I change f to:
f :: (MArray arr p e) => ArrayType arr p -> t -> m t
f arr_type ... = ...
And inside f I have a function:
newArray_arr bnds init =
do a <- newArray bnds init
return (a `asArray` arr_type)
This enables me to create arrays of arbitrary representation with the same
values but different indices (which is important).
However, this strikes me as horribly hackish. Is there some better way to
do this?
- Hal
--
Hal Daume III
"Computer science is no more about computers | hdaume@isi.edu
than astronomy is about telescopes." -Dijkstra | www.isi.edu/~hdaume