The Problem:
I can't find my way to understand the error in this code:
import Prelude
import Data.Array.ST (STArray, modify, run, thaw, freeze)
mpi :: forall a. Array a -> Array a
mpi array = run do
mutableArray <- thaw array
freeze mutableArray
The error:
Could not match type
Array
with type
STArray h0
while trying to match type Array a2
with type STArray h0 t1
while checking that expression (bind (thaw array)) (\mutableArray ->
freeze mutableArray
)
has type ST h0 (STArray h0 t1)
in value declaration mpi
where a2 is a rigid type variable
bound at (line 0, column 0 - line 0, column 0)
h0 is a rigid type variable
bound at (line 9, column 17 - line 11, column 22)
t1 is an unknown type
It says t1 is an unknown type, but I'm pretty sure it should be a2. I'm not sure how or where t1 is introduced. thaw
should return type ST h (STArray h a)
which gets bound to mutableArray :: STArray h a
If I specialize this function, it becomes clearer but no less confusing
mpi :: Array Int -> Array Int
mpi array = run do
mutableArray <- thaw array
freeze mutableArray
I get this error:
Could not match type
Array
with type
STArray h0
while trying to match type Array Int
with type STArray h0 t1
while checking that expression (bind (thaw array)) (\mutableArray ->
freeze mutableArray
)
has type ST h0 (STArray h0 t1)
in value declaration mpi
where h0 is a rigid type variable
bound at (line 9, column 17 - line 11, column 22)
t1 is an unknown type
If I explicitly type the left-hand side,
mpi :: Array Int -> Array Int
mpi array = run do
(mutableArray :: STArray _ Int) <- thaw array
freeze mutableArray
or write it without do
notation:
mpi :: Array Int -> Array Int
mpi array = run $ thaw array >>= freeze
The error doesn't really change. In each case, I have trouble understanding where t1 is introduced.
The Question:
- What's wrong with what I've written?
- What steps could I be taking with similar problems in the future to debug this on my own?