4

In the following given code what does the eqtype means? I know that there exists a type keyword in SML which lets you alias the types but eqtype is something new to me.

signature SYMBOL =
sig
  eqtype symbol
  val symbol : string -> symbol
  val name : symbol -> string
  type 'a table
  val empty : 'a table
  val enter : 'a table * symbol * 'a -> 'a table
  val look  : 'a table * symbol -> 'a option
end

I tried to find the explanation in the documentation but couldn't find anything about it. Can anybody explain it to me? Thanks.

Intuitively, it seems that eqtype let's you declare a type which will we need to specify when we create the structure of this signature.

  • 3
    A type for which equality is defined. – L3viathan Apr 04 '19 at 16:34
  • I am a beginner @L3viathan. Would you please explain what do you mean by `equality is defined`? – KAUSHAL KISHORE Apr 04 '19 at 16:37
  • 4
    To see the difference between equality types and nonequality types, evaluate `1 = 1` vs. `1.0 = 1.0`. ints are equality types. floats are not. – John Coleman Apr 04 '19 at 16:37
  • Does that also mean that I can't set my `eqtype symbol` to floats? – KAUSHAL KISHORE Apr 04 '19 at 16:40
  • Yes, that is what it means. – John Coleman Apr 04 '19 at 16:42
  • 1
    See [this answer](https://stackoverflow.com/a/41394993/4996248) for why reals work that way. SML aims for provably correct code. Checking reals for equality is more often than not a bug ([Is floating point math broken?](https://stackoverflow.com/q/588004/4996248) is one of the most duplicated questions on Stack Overflow). SML doesn't even allow for such bugs to compile. – John Coleman Apr 04 '19 at 16:48

1 Answers1

2

eqtype is the keyword for equality type. Equality types in sml are defined as types of values that can be compared with =. For example, int, string list, bool * bool option are all equality types, whereas real, int -> int are not.

In a signature, when you have eqtype, you are adding an extra layer of constraint telling sml the type is equality type. Any structure ascribe the this sig need to define the type as an equal type.