Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way of constraining a functor's parameter signature so the parameter can supply unspecified equality types to a structure?

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.

like image 332
Shon Avatar asked Nov 28 '15 10:11

Shon


1 Answers

You only need to specify that t is an eqtype:

signature SIG =
sig
  eqtype t
end
like image 168
Ionuț G. Stan Avatar answered Oct 16 '22 16:10

Ionuț G. Stan