Why are types "a" and "Z" not the same in SML?
I have the following recursive function:
fun tester (f:'a -> 'b, tl:(string * 'a * 'b) list) =
case tl of
[] => []
| (t, c, e)::rest =>
let val tr = f (c)
in
if tr <> (e)
then ((t), (e), tr)::(tester (f, rest))
else tester (f, rest)
end;
When loading, I get "Error: Operator and operand do not agree [UBOUND match]":
lec1test.sml:17.5-19.26 Error: operator and operand don't agree [UBOUND match]
operator domain: ''Z * ''Z
operand: 'b * 'Y
in expression:
tr <> e
uncaught exception Error
raised at: ../compiler/TopLevel/interact/evalloop.sml:66.19-66.27
../compiler/TopLevel/interact/evalloop.sml:44.55
../compiler/TopLevel/interact/evalloop.sml:296.17-296.20
I figured out that I think it has something with generic tr binding, but I don't understand why this is the problem. I am assigning a tr
value to a function from f
which it returns 'b
. Then I compare the result with the last value in the tuple, which is also of type 'b
. Can someone explain why this is giving me an error?
source to share
Not all types support equality operators =
and <>
only so-called equality types. For example, int
or string list
or bool * unit
are equality types, but for example function types t -> u
never exist because there is no sane (resolvable) way to compare functions.
Values ββof polymorphic type type are 'a
also not equality types, because a type variable can be created by any type. To get a polymorphic type, limited to equality types, you need to write a double tick type variable, eg. ''a
...
In your case, changing the first line to
fun tester (f : ''a -> ''b, tl : (string * ''a * ''b) list) =
should fix it for you.
source to share