I have a binding for a type [ST s (Int, [Int])] and I am trying to apply runST to each element using map as follows:
name :: [ST s (Int, [Int])] --Of Course there is a real value here
map runST name
This gives me an error message
Couldn't match expected type `forall s. ST s b0'
with actual type `ST s0 (Int, [Int])'
Expected type: [forall s. ST s b0]
Actual type: [ST s0 (Int, [Int])]
In the second argument of `map', namely `name'
In the expression: map runST name
There must be something I am misunderstanding. I am aware of runST and function composition, but am unsure if this applies.
Thanks for everyones time!
Each time you run a state transformer with
runST, it operates on some local state that is separate from all other state transformers.runSTcreates a new state type and calls its argument with that type. So, for example, if you executethen the first
return ()and secondreturn ()will have different types:ST s1 ()andST s2 (), for some unknown typess1ands2that are created byrunST.You are trying to call
runSTwith an argument that has state types. That is not the state type thatrunSTcreates, nor is any other type you can choose. To callrunST, you must pass an argument that can have any state type. Here is an example:Because
r1is polymorphic, its state can have any type, including whatever type is selected byrunST. You can maprunSTover a list of polymorphicr1s (with-XImpredicativeTypes):However, you cannot map
runSTover a list of non-polymorphicr1s.The type
forall t. [ST t ()]says that all list elements have state typet. But they need to all have independent state types becauserunSTis called on each one. That is what the error message means.If it is okay to give the same state to all list elements, then you can call
runSTonce as shown below. The explicit type signature is not required.