SML# Document Version 4.2.0
24 Module language declarations and interface

24.1 Structure declarations : ⟨strDecl⟩

Syntax for structure declaration is given below.

⟨strDecl⟩ :=  structure ⟨strbind⟩
⟨strbind⟩ ::= ⟨strid⟩ = ⟨strexp⟩ (and ⟨strbind⟩)?
 | ⟨strid⟩ : ⟨sigexp⟩ = ⟨strexp⟩ (and ⟨strbind⟩)?
 | ⟨strid⟩ :> ⟨sigexp⟩ = ⟨strexp⟩ (and ⟨strbind⟩)?

If there are multiple bindings separated with and then the structure names are bound simultaneously, whose scope is the rest of declarations that follow this declaration. So, in the following example, y is bound to 2 and not 1.

structure A = struct val a = 1 end;
structure B = struct val b = 1 end;
structure A = struct val a = 2 end and B = struct val b = A.a end;
val x = A.a;
val y = B.b;

Binding structure names with signature constraints are translated to binding to structure expressions with signature constraints as follows.

source translated to
⟨strid⟩ : ⟨sigexp⟩ = ⟨strexp⟩ ⟨strid⟩ = ⟨strexp⟩: ⟨sigexp⟩
⟨strid⟩ :> ⟨sigexp⟩ = ⟨strexp⟩ ⟨strid⟩ = ⟨strexp⟩:> ⟨sigexp⟩

Evaluation of a structure declaration is done as follows.

  1. 1.

    Evaluate the structure expression and obtain a static type environment Γ and a runtime environment E, as defined in the following section.

  2. 2.

    Obtain Γ′ and E′ from Γ′ and E′ by prefixing the long names defined in them with the structure name S, and add them to the current environments.

For example, if structure expression ⟨strexp⟩ generates a static type environment {longId1:τ1,...,longidn:τn} and a runtime value environment {longId1:v1,...,longidn:vn}, then evaluating the structure declaration structure ⟨strid⟩ = ⟨strexp⟩ has the effect of extending the current type environment and runtime environment with the bindings {S.longId1:τ1,...,S.longidn:τn} and {S.longId1:v1,...,S.longidn:vn}.