[Haskell-cafe] How does one delare a 2D STUArray in Haskell?
Daniel Fischer
daniel.is.fischer at web.de
Fri Sep 25 06:07:29 EDT 2009
Am Freitag 25 September 2009 09:22:25 schrieb Casey Hawthorne:
> Well that makes sense, but for a learner, how is he/she supposed to
> know that 'i' could be '(i,i)' or for that matter a tuple of n of
> those i's?
minh thu already explained this very well.
>
> "STUArray s i e"
>
> Could you also have a tuple of states?
You can't choose the state 's', the documentation says
"The strict state-transformer monad. A computation of type ST s a transforms an internal
state indexed by s, and returns a value of type a. The s parameter is either
* an uninstantiated type variable (inside invocations of runST), or
* RealWorld (inside invocations of Control.Monad.ST.stToIO). "
Without evil hackery (or stToIO), you can only use ST actions/ST(U)Arrays via
runST :: (forall s. ST s a) -> a
or
runST(U)Array :: Ix i => (forall s. ST s (ST(U)Array s i e)) -> (U)Array i e
which have rank 2 types (universally qualified type as type of argument [result]),
the 'forall s' within the parentheses says it has to work whatever type the rts chooses
(actually none), so if you write
myFancyArray :: forall s1, s2. ST (s1,s2) (STUArray (s1,s2) Int Int)
you can't use it.
>
> Obviosly, 'e' could be a tuple, for instance (Int,Char)
Not for STUArrays, but for STArrays, there's no problem.
>
> --
> Regards,
> Casey
More information about the Haskell-Cafe
mailing list