The dynamic semantics of Modules defines the dynamic interfaces that modules evaluate to, and the environments that imports and top-level declarations produce. Its sentences infer that a module evaluates to an interface in a basis, that a sequence of imports evaluates to an environment, that an exposing list cuts an interface down to the part it exposes, and that a top-level declaration evaluates to an interface in an environment. Modules are flat: a module makes its top-level values and constructors available by name, and there are no signatures or functors. Every module evaluated here is assumed to have elaborated in the static semantics of Chapter 5. The order in which the modules of a program are evaluated, and the meaning of main, are given in Chapter 9.
7.1Reduced Syntax¶
Types play no part in evaluation, except that a port must know what it sends or receives. For the dynamic semantics the syntax of Modules is reduced as follows, in addition to the reduction of the Core (Section 6.1):
The type annotations of top-level values are omitted, as in the Core. Since a module has no pattern bindings at the top level (Section 3.7), every top-level value declaration has the form
The type-variable sequence is omitted from every custom-type binding and every type-alias binding; the class TyVarSeq is omitted.
Each constructor binding is replaced by , whose arity is (Section 6.3). Thus a constructor binding has the form .
A type-alias binding is replaced by if it is a record alias (Section 3.7) whose record type has the labels , in the order written; every other type-alias binding is replaced by alone.
A port declaration is replaced by for an outgoing port and by for an incoming one, where is the payload type of the port; the direction and the payload type are those that elaboration records in the port environment (Chapter 5, Section 5.4).
After number specialisation (Section 6.2), an exposing item naming a top-level variable whose declaration has been copied stands for the items naming and each of the new variables that the copies bind, so that a module that uses at an instance other than the default one finds the copy for that instance.
An import without
asis read as : it provides its own as qualifier (Section 3.2).
As everywhere in the semantics, the derived forms of Appendix A have been applied, so the top-level declarations of a module stand in the order of Section A.2; and the imports of a module are the default imports of Figure 11 followed by the imports written in it.
7.2Compound Objects¶
There are no simple objects beyond those of the Core. The compound objects for the dynamic semantics of Modules, extra to those of the Core (Figure 20), are shown in Figure 21.
A constructor environment maps the type constructor of each custom type to a value environment of its constructors. A dynamic interface records what a module makes available to other modules: binds every value it exposes, namely its exposed values and ports, the constructors of each custom type whose constructors it exposes, and the constructor of each record alias it exposes; maps every custom type that it exposes to the constructors of that type that it exposes, which is the empty map for a type exposed closed; and is the set of the type constructors of the record aliases whose constructors it exposes. For a type constructor we write when is spelt as , as in Chapter 5: the constructor of a record alias is bound in to the constructor with . A module environment maps each module identifier to the interface of that module, and a dynamic basis is a module environment. Interface, ModEnv and Basis are the dynamic objects of the names that Chapter 5 uses for static objects; Chapter 9 distinguishes them where both are in play.
Projection, injection and modification are as for the Core. In particular “” is the second component of ; means ; “ in ” means ; and interfaces are modified component by component, the set components by union. The interfaces inferred by the rules of this chapter have the following property, on which rule (121) relies.
If is an interface inferred by the rules, then for every and , and whenever .
7.3Merging Environments¶
Several imports of one module may provide the same qualifier, and the names they provide under it are then all available under that qualifier. Imports are therefore combined by the following modification, which merges, rather than replaces, the environment of a qualifier common to both of its arguments. For environments and ,
where , and is if , and otherwise whichever of and is defined. Where both arguments bind a name, the binding of is kept.
7.4Record Constructors¶
The constructor of a record alias is a function of the fields of the record, taken in the order in which the alias writes them. For and distinct labels we extend BasVal with the basic value , of arity , and extend to it by
For we let stand for the empty record , which is a value but not a basic value, since every basic value has arity at least 1. These values are not bound in the initial basis (Appendix D); they are bound only by the record aliases of modules.
BasVal includes also the values of ports, the basic values and for a port name and a payload type . They are defined in Chapter 8 (Section 8.7); this chapter uses only the fact that they are basic values.
7.5Top-level Value Groups¶
A recursive group of top-level values need not consist of functions: the restrictions of Section 3.7 admit any group in which every cycle passes through an occurrence within a lambda expression. The operation (Section 6.6) gives the bindings of a group only to the closures that the group binds, not to a closure held within a record, a tuple or a constructor value. We have therefore adopted the following translation, under which every member of a group is reached through a function of the unit value.
Let be a top-level value declaration, and let be distinct variables that occur nowhere in the program. For a phrase of the Core, is the phrase obtained from it by replacing simultaneously, for ,
each free occurrence of the atomic expression by the atomic expression , and
each atomic expression in which is free by ,
where “free” is as in Section A.1. These are the only positions at which a variable occurs free in an expression. Then
Every binding of is a lambda expression, so ties the knot for it exactly as for a recursive declaration of the Core, and then evaluates . When no is free in any , the declaration is not recursive and .
7.6Inference Rules¶
The rules infer sentences of the forms
where is a basis, an environment, an interface, and is the result: a basis with a value environment, a module identifier with an interface, an environment, an interface, a constructor environment with a value environment, or a value environment. Each is pronounced “ evaluates to ”, followed by “in ”, “in ” or “in ”. The hypotheses of a rule that are not sentences are its side conditions; some premises are sentences of the Core (Chapter 6). The convention for options is the same as for the Core static semantics.
As in the Core, sentences have no states and there are no exception packets, so every rule is given in full, and the order of the premises of a rule carries no meaning (Section 6.7).
The word port has no dynamic content: a port module and a plain module evaluate alike.
is defined, since a module is evaluated only after every module it imports (Chapter 9). The qualifier gives access to every value that the imported module exposes, its constructors included; the exposing list makes a part of them available unqualified. The environment of later imports stands on the right of , so a name exposed by a later import replaces the same name exposed by an earlier one, and in particular a written import replaces a default import. Where elaboration has found an unqualified name ambiguous, the name is not used, so which binding is kept is immaterial.
The variable is a value or a port.
An operator is not in the domain of a value environment: its value is given by the operator table , and is the same in every module (rule (68)). Exposing or importing an operator therefore binds nothing.
A custom type exposed without (..) exposes no value, even when one of its constructors is spelt as the type: in a module with type Id = Id Int and header exposing (Id), the constructor Id is not exposed. The type is still exposed, closed: the constructor environment maps it to the empty map, so that an import of Id(..) has a meaning (rule (123)). An alias of a type other than a closed record is in neither nor , and exposes nothing.
By elaboration, names a custom type that exposes, so is defined; an item for an alias is rejected (Chapters 3 and 5). In a header, is the module’s own interface, which holds every constructor of . In an import, holds the constructors that the imported module exposes, and is the empty map when it exposes closed: with module A exposing (T), type T = T Int and import A exposing (T(..)), the import makes no constructor available.
Here is , and and are as in Section 7.5; the first premise is a sentence of the Core (Value Bindings), and the choice of the new variables does not affect the result. Each is bound to a value obtained once, when its module is evaluated. A reference to within the group evaluates again in the same environment, which gives again, there being no state, unless relies on the unspecified result of an equality of functions (Section 6.5). A top-level value is thus evaluated once only up to that non-determinism: where compares functions, a reference to within its group may give another of the values that the equality of functions allows, and so differ from . Thus with ones = { head = 1, tail = \_ -> ones }, the expression (ones.tail ()).head evaluates to 1. In x = let f _ = x in f () the occurrence of x is delayed, so the restrictions of Section 3.7 admit it, but evaluating needs again, without end: no sentence about the declaration can be inferred, and the module has no meaning.
Rules (127) and (128): the payload type comes from elaboration through the reduced syntax; it has no type variables, so the value of a port is the same at every use. Applied to a value, the value of an outgoing port gives a command that sends its encoding at ; applied to a function, the value of an incoming port gives a subscription (Section 8.7). Port names are unique in a program, so the name identifies the port.
By the derived forms of Section A.2, consists of the custom-type group, then the aliases, then the ports and value groups in dependency order, so every declaration is evaluated after each declaration it refers to. Where that order leaves two declarations unordered, either order gives the same result.
Here , and by the syntactic restrictions the labels are distinct. The constructor takes the fields in the order in which the alias writes them: with type alias P = { y : Int, x : Int }, P 1 2 evaluates to { y = 1, x = 2 }. An alias of the empty record binds its name to the empty record itself (Section 7.4). An alias of any other type binds nothing, and its type constructor is not in ; so rule (126) records in exactly the record aliases.
Here E0 is the environment of Appendix D that binds the list constructor names
[]and::; every module starts from it, as its elaboration starts from π0 (rule (46)). The top-level declarations are evaluated in E0 modified by the environment that the imports give, so a name that a module declares replaces, within it, an imported name spelt alike (rule (129)). The basis {modid↦I′} holds only what the header exposes; the value environment VEofI of all the module’s own values is the second result, as in the static semantics, because Chapter 9 takes the value ofmainfrom it, andmainneed not be exposed. A module is evaluated once, after the modules it imports, and its top-level values are evaluated then (Chapter 9).