MLton / mlton

The MLton repository
http://mlton.org
Other
953 stars 127 forks source link

Nested `withtype` not translated correctly? #548

Open andreasabel opened 8 months ago

andreasabel commented 8 months ago

SML/NJ understands this:

datatype Entry =
    e_val of string*Clos
  | e_gen of string*int
withtype Env = Entry list
and Clos = Tm*Env

MLton 20210117 (latest on homebrew) gives this error:

  Undefined type: Env.

So, it seems that the types in withtype do not know each other.

pclayton commented 8 months ago

This behavior is in line with SML as specified by the Definition. Poly/ML, for example, would give you a similar response. You are not the first to observe this - see this response. Unfortunately compiler extensions lead to non-portable code.

andreasabel commented 8 months ago

It is of course possible to work around this limitation (by inlining either Clos or Env), but it is not super pretty.

But I understand that standards are not easily changed, even if they made suboptimal decisions...

Maybe an extension could be implemented, similar to the -default-ann 'allowSigWithtype true', e.g. allowRecursiveWithtype.

MatthewFluet commented 8 months ago

Using an annotation to support the SML/NJ-style elaboration of withtype would be a reasonable approach. PR welcome.