3

I want to place signature/structure pair inside a structure, like so:

structure Outer :> OUTER =
struct
    signature INNER =
    sig
        ...
    end

    structure Inner :> INNER =
    struct
    ...
    end
end

but even the simplest of examples produces an error:

../test.sml:1.18-2.6 Error: syntax error: replacing  STRUCT with  EQUALOP
../test.sml:5.6 Error: syntax error found at END

It appears that signatures are not allowed inside structures. What is the best way to achieve this functionality?

Andrew Keeton
  • 22,195
  • 6
  • 45
  • 72

2 Answers2

3

Although structures nest in SML, signatures do not. It's not clear what functionality you want to achieve:

  • Hiding a named signature is impossible.

  • Having the INNER signature depend on types declared in structure Outer is achieved through fibration (the where type clause). There's a length section on fibration in the tutorial by Harper and Pierce in Benjamin Pierce's book on advanced types in programming languages.

Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533
  • 1
    The question the OP have, makes sense in this way: this is like when one define a function type before defining a body for it. One can define types in a structure, signatures are very like types, but unfortunately, there is no way to define a signature and later a body for it, inside of a structure. If a signature needs to refer to types defined in the intended outer structure, that’s an issue. Of course, one can define a nested structure with a signature constraint, but the structure body must come immediately with it, can’t be deferred. – Hibou57 Sep 25 '20 at 10:39
2

You could also inline the inner signature, as in

structure Outer :> OUTER =
struct
    structure Inner :> sig
      ...
    end
      =
    struct
    ...
    end
end
Norman Ramsey
  • 198,648
  • 61
  • 360
  • 533