Reputation: 53
Using Poly/ML, I wanted to write a function to construct an n*n identity matrix using Array of Arrays.
I wrote:
fun equiv x y = if x = y then 1 else 0;
fun idmatrix n = Array.tabulate(n, fn i => (Array.tabulate(n, equiv i)));
and it compiled successfully, giving
> val equiv = fn: ''a -> ''a -> int
> val idmatrix = fn: int -> int array array
But when I called idmatrix
idmatrix 2;
for a test, the output of the compiler was
> Exception- Cast "toAddress" raised
Can anybody explain why the exception was raised, please?
Thank you very much!
Upvotes: 1
Views: 64
Reputation: 16105
Your code works fine for me. Looking at the code that raises the exception, perhaps it's a 32/64-bit address problem? Is your Poly/ML compiled for the right architecture?
Poly/ML:
$ poly
Poly/ML 5.2 Release
> fun equiv x y = if x = y then 1 else 0;
val equiv = fn : ''a -> ''a -> int
> fun idmatrix n = Array.tabulate(n, fn i => (Array.tabulate(n, equiv i)));
val idmatrix = fn : int -> int Array.array Array.array
> idmatrix 3;
val it = fromList[fromList[1, 0, 0], fromList[0, 1, 0], fromList[0, 0, 1]]
: int Array.array Array.array
Moscow ML:
$ mosml
Moscow ML version 2.10
Enter `quit();' to quit.
- fun equiv x y = if x = y then 1 else 0;
> val ''a equiv = fn : ''a -> ''a -> int
- fun idmatrix n = Array.tabulate(n, fn i => (Array.tabulate(n, equiv i)));
> val idmatrix = fn : int -> int array array
- idmatrix 3;
> val it = <array> : int array array
And SML/NJ:
$ sml
Standard ML of New Jersey v110.76 [built: Sun Jun 29 03:29:51 2014]
- fun equiv x y = if x = y then 1 else 0;
stdIn:1.23 Warning: calling polyEqual
val equiv = fn : ''a -> ''a -> int
- fun idmatrix n = Array.tabulate(n, fn i => (Array.tabulate(n, equiv i)));
[autoloading]
[library $SMLNJ-BASIS/basis.cm is stable]
[autoloading done]
val idmatrix = fn : int -> int array array
- idmatrix 3;
val it = [|[|1,0,0|],[|0,1,0|],[|0,0,1|]|] : int array array
Upvotes: 2