I want to compare whether the values of two sets are equal, but when I pass the set into the equals function, the display type does not match.
Error: value type in structure does not match signature spec
name: equals
spec: 'a ?.set * 'a ?.set -> bool
actual: ''a list * ''a list -> bool
signature SET =
sig
type 'a set
val emptyset: 'a set
val insert: 'a * 'a set -> 'a set
val equals: 'a set * 'a set -> bool
end;
functor createSet (Element : sig type t end) :> SET =
struct
type 'a set = 'a list
val emptyset = []
fun insert (x, s) = x :: s
fun equals (s1, s2) =
if s1 = s2 then
let
val x = hd(s1)
val y = hd(s2)
in
if x = y then
equals(tl(s1), tl(s2))
else
false
end
else
false
end;
Can you help me solve this problem? And What is the difference between 'a and ''a ? I understand that 'a is a variable that can accept any type
In order to compare things with
=, they must be of an equality type.For example, integers and strings are equality types;
reals and functions are not because they don't have a meaningful sense of equality.(The SML view is more or less that
realis just an approximation of real numbers, so can't meaningfully be "equal".)Given
createSet's parameter, I would expect to see something more like this, with the element type being provided by that structure instead of being arbitrary:Example:
You can get rid of the equality type requirement by - for instance - adding a function to the
Elementsignature that you use for determining the "equality" of elements.(Doing it left as an exercise.)