6
votes

If I try to write a parameterized module that calls = on an unspecified type supplied by the parameter, SML/NJ throws a type error. E.g., if I have a signature

signature SIG =
sig
  type t
end

and try to parameterize a module F over a module S with the signature SIG

functor F (S : SIG) =
struct
  fun f (x:S.t) (y:S.t) = (x = y)
end

I will trigger the following compilation error:

Error: operator and operand don't agree [equality type required]
  operator domain: ''Z * ''Z
  operand:         S.t * S.t
  in expression:
    x = y

How can I specify that S.t should be an equality type?

The only workaround I have been able to figure out so far is to also supply the equality function in the structure over which the functor is parameterized i.e.:

signature SIG' =
sig
  type t
  val eq : (''a * ''a) -> bool
end

functor F' (S' : SIG') =
struct
  fun f' x y = S'.eq (x, y)
end


structure A = F'( struct
                    type t = int
                    val eq = (op =)
                  end )

It seems like there must be a better way, although I may also be misunderstanding something basic and important about how functors are meant to work.

1

1 Answers

6
votes

You only need to specify that t is an eqtype:

signature SIG =
sig
  eqtype t
end