This chapter defines the semantic objects of the static semantics of the Core and the rules by which Core phrases elaborate. The simple objects are type variables, each with a class, row variables, type names and identifier status descriptors. From them we build types, record types, type functions, type schemes, environments and contexts, and on these we define substitution, instantiation and closure. The rules infer sentences , in which is a context and is a type, a row, a value environment, or a value environment paired with a type. A Core phrase is legal when it satisfies the syntactic restrictions of Chapter 2, elaborates by the rules of Section 4.9, and its principal derivation (Section 4.10) meets the further restrictions of Section 4.11.
4.1Simple Objects¶
All semantic objects of the static semantics are built from identifiers (Chapter 2), labels among them, and four further kinds of simple object: type variables, row variables, type names and identifier status descriptors, together with the classes that type variables carry. Type names are the values taken by type constructors; they are distinct from type variables and from type constructors. The simple object classes, and the metavariables ranging over them, are shown in Figure 12. We include in the figure to show that ranges over it.
| type variables | ||||
| row variables | ||||
| type names | ||||
| classes | ||||
| identifier status descriptors |
The classes are
Each has a class , and there are infinitely many type variables of each class. A row variable has no class. Each has an arity ; we write for the type names of arity .
A type variable of the syntax names one type variable, also written , whose class is fixed by the beginning of its name: if the name begins with number, if it begins with comparable, if it begins with appendable, if it begins with compappend, and otherwise. When that class is , also names one row variable; a type variable of the syntax of any other class names no row variable. We write and when , respectively , is the variable named by . No other type or row variable is named by a type variable of the syntax.
A label and a value variable may be spelt alike. For a label we write when is spelt as .
The rules mention a few type names directly. They are shown in Figure 13; they belong to the initial basis (Appendix C), and a rule that names one of them means that type name, whatever a program binds to the type constructor of the same spelling.
| integers, floats | ||||
| characters, strings | ||||
| unit | ||||
| lists | ||||
| pairs | ||||
| triples |
4.2Compound Objects¶
When and are sets, denotes the set of finite subsets of , and denotes the set of finite maps (partial functions with finite domain) from to . The domain and range of a finite map are denoted and . A finite map is written explicitly as , ; in particular the empty map is . We write for the finite map whose domain is the set of satisfying the condition and whose value on that domain is . A finite map is regarded as the set of its pairs, so means that and that agrees with on .
When and are finite maps, the map , called modified by , is the finite map with domain and values
The compound objects of the static semantics of the Core are shown in Figure 14. We take to mean disjoint union over semantic object classes, and we understand all the defined object classes to be disjoint. The classes , , , and are the smallest sets satisfying their equations; hence every type is finite, and no type occurs within itself. A sequence of types is also written , and a sequence of type and row variables ; the metavariable ranges over . In a context, and .
A record type is a pair of a row and either a row variable or . The record type , written , is closed: its fields are exactly those of . The record type , written , is open: its fields are those of together with those that stands for, none of which has a label in (Section 4.5). The unit type is the constructed type , and the type of -tuples with components is . When has arity 0 we write for the type ; thus , and so on are types.
Note that and bind type and row variables. For any semantic object , and denote respectively the set of type names and the set of type and row variables occurring free in . For a context we take . For a type expression , denotes the set of type variables of the syntax occurring in .
A value environment maps value variables and value constructors to a pair of a type scheme and an identifier status. If or , we say that , respectively , has status in . Value variables have status . The constructors of custom types have status , and only they may occur in patterns; the constructor that a record type alias defines has status (Chapter 5). We take to contain, besides the constructors spelt by identifiers, the two list constructor names [] and ::, which no identifier spells; the initial basis binds them with status to and , where (Appendix C).
A qualified environment maps each qualifier to the environment of the names it qualifies; every environment in the range of a qualified environment has the empty qualified environment. In a context , is the set of type names generated so far, and is the set of explicit type and row variables in scope (Section 4.7).
4.3Projection, Injection and Modification¶
Projection: We often select a component of a tuple of objects, and we rely on metavariable names to say which component. For instance “” means the value environment component of , and, for a record type , “” means its row.
When a tuple contains a finite map we apply the tuple to an argument, relying on the syntactic class of the argument to select the map. Thus means , and likewise ; means . A long identifier is looked up through the qualified environment: if then means , and likewise for and ; an unqualified is a . means . The identifiers that the equivalent forms of derived forms introduce (Appendix A) are looked up in the same way. Because only an import of the module Basics provides the qualifier Basics (Section 3.7), Basics.True, Basics.False and Basics.negate always denote the bindings of the initial basis (Appendix C); the constructor :: is bound in the initial environment of that appendix, from which every module is elaborated. Operators are not in the domain of an environment, and is not defined. Since no program declares an operator, the operators are fixed by the initial basis: Appendix C gives the static operator table
which maps each operator of Figure 3 to the type scheme and status of the identifier that the operator stands for; in particular is the type scheme of the list constructor ::, with status . The operators of library modules outside the initial basis are outside the definition. Injection: Components may be injected into tuple classes; for example, “ in ” means the environment .
Modification: The modification of one map by another has been defined above. It is extended to environments component by component, , and empty components are left implicit: means . For set components modification means union, so that means
and and leave the other components unchanged. Finally, means : the type environment is added and is extended with its type names.
4.4Classes and Admissible Types¶
The classes are ordered by , the least partial order on such that
For each class , the set of types admissible for is defined in Figure 15; is the smallest set satisfying its equation. A type variable of class may stand only for a type in (Section 4.5). A type variable belongs to exactly when its class is at most ; no record type and no function type is admissible for any class but .
There is no class of types that admit equality. The initial basis gives the equality operators the type scheme with (Appendix C), so equality elaborates at every type, function types included.
4.5Types and Type Functions¶
Tested by: CompilerIssue2182ExtensibleFunctionAliasCrashTest
Substitution. Let be distinct type and row variables, and types such that whenever . For a type , the type is the result of replacing each by throughout . Writing for , and for , it is defined by
The result is defined only if, wherever a row variable is replaced, . Thus a row variable can never give a record type a second field with a label it already has. The substitution respects classes if for each . Substitution is extended to rows, as above, and to other objects in the obvious way, renaming bound variables where necessary.
The rules rely on the following property, which may easily be demonstrated.
If then . If , and is defined and respects classes, then .
Type functions. A type function has arity ; the bound variables must be distinct. Two type functions are equal if they differ only in their choice of bound variables, a type variable being renamed to a type variable and a row variable to a row variable. The class of a bound type variable of a type function has no significance. If has arity , then we write for the type function , where the are distinct type variables; thus .
The application of to types is
defined when the substitution is defined; it need not respect classes. In particular a parameter of that is a row variable accepts only a record type.
4.6Type Schemes¶
A type scheme generalises a type , written , if for some for which the substitution is defined and respects classes; is then an instance of . If , then generalises , written , if and no occurs free in . By the property of Section 4.5, if and then .
Two type schemes are equal if they can be obtained from each other by renaming and reordering bound variables, and by deleting bound variables that do not occur in the body, other than the variables of class that closure adds to the scheme of a binding of a group (Section 4.8), which are kept. of the same class and a row variable by a row variable. We consider a type to be a type scheme, identifying it with . For a finite set of type and row variables, means where is any enumeration of .
With each special constant we associate a type scheme : it is , where , if is an integer constant, and the type , or if is a floating point, character or string constant. Thus the instances of for an integer constant are the types in .
4.7Scope of Explicit Type Variables¶
The type variables written in a type annotation are explicit: within the annotated definition each stands for one type or row variable that the definition may not instantiate. Explicit variables in scope are recorded in .
For a set of type and row variables, is the set of type variables of the syntax such that for some . The set is unambiguous if no names two members of . We consider only contexts in which is unambiguous; the rules preserve this.
Let be the type expression of an annotation elaborated in context . A set of type and row variables is new for in if
Thus holds, for each type variable of that is not already explicit in , the type variable or the row variable that it names: the row variable when uses it as the extension of a record type ({ r | … }), and the type variable otherwise. The rules for type expressions accept only variables of , and both kinds could elaborate a name written alone (rules (22) and (23)), so the kind is fixed here: in f : a -> a the name a is a type variable, and in f : { r | x : Int } -> r the name r is a row variable. The members of are added to while the annotation and the body of its definition elaborate, so that an annotation nested within the body that writes one of these names means the same variable. They are bound in the type scheme of the annotated variable, and they must not occur free in the context of the definition: an explicit variable does not escape its annotation.
4.8Closure¶
Tested by: StandardA93MutualNumberDefaultTest
Let be a semantic object and a type scheme. Then , the closure of with respect to , is the type scheme , where enumerates . In particular, for a type , where enumerates . For a value environment, is the value environment obtained from by replacing each pair in its range by .
Commonly is a context . Since , closure never generalises an explicit variable in scope. Closure applies to every group of value bindings, pattern bindings included, whatever the form of their expressions.
A group of value bindings is closed as one. When rules (19) and (56) take of the value environment of a group, the scheme of each binding is closed also over the type variables of class in : those of the other bindings of the group that its own type does not mention. Since such a variable does not occur in the binding’s type, it changes no instance of the scheme. It matters only to number specialisation (Section 6.2): an occurrence of the binding gives it a type that nothing fixes, which is then .
4.9Inference Rules¶
Each rule of the semantics allows inferences among sentences of the form
where is usually a context, is a phrase of the Core, and is a semantic object, usually a type, a row, a value environment, or a pair of a value environment and a type. It is pronounced “ elaborates to in (context) ”. Some rules have extra hypotheses not of this form; they are called side conditions.
In the presentation of the rules, phrases within single angle brackets are called first options, and those within double angle brackets are called second options. To reduce the number of rules, we have adopted the following convention:
In each instance of a rule, the first options must be either all present or all absent; similarly the second options must be either all present or all absent.
No Core phrase generates type names, so is the same in every sentence of a derivation for a Core phrase; it is used by the static semantics of Modules (Chapter 5). A phrase elaborates to when some derivation of the sentence exists; where the premises of a rule leave a type in its conclusion undetermined, the derivation may choose any type that satisfies them. The relation is overloaded for all syntactic classes.
The syntactic restrictions ensure .
The relational symbol is overloaded for all syntactic classes (here atomic expressions and expressions).
This rule lets new free type variables enter the context: the types of the variables of are chosen in elaborating , and they may have to agree with the explicit variables of annotations within .
A declaration is one group of mutually dependent bindings; a let with several definitions is a derived form that divides them into groups (Appendix A). Modifying by on the left puts every member of the group in scope in every member’s expression. Since is the same on both sides, an unannotated member has one type throughout the group: it is monomorphic within the group, and is generalised only by the closure, for the body of the let. An annotated member enters with the type scheme of its annotation, so it may be used polymorphically within the group, in its own body included.
The closure applies to every member whatever its expression, so there is no value restriction: in let n = 5 in ( n // 2, n / 2 ) the variable n is used at and at . The closure leaves the scheme of an annotated member unchanged, since its free variables are explicit variables of .
When the second option is present, by the syntactic restrictions. With an annotation, holds the variables of not already explicit in (Section 4.7). They are in while and elaborate, so an annotation nested in that writes one of their names means the same variable. Since is fixed by , must have exactly that type, with the variables of uninstantiated: an annotation may be less general than the type could have, but never more general. Thus f : a -> a with f x = x + 1 does not elaborate, nor does g : number -> number with g x = x // 2.
The condition prevents an explicit variable from escaping its annotation. In f x = let g : a -> a ; g y = x in g x, the body of g has type a -> a only if x has the explicit type a, which is then free in the context of g; so the expression does not elaborate.
Rules (22) and (23) both cover the phrase . Since is unambiguous, at most one of them applies, according to whether the variable of named by is a type variable or a row variable. A type variable that is not explicit in does not elaborate: every type variable of an annotation is made explicit by rule (20), and Chapter 5 makes the parameters of type declarations explicit in the same way.
A row variable written alone denotes the open record type with no fields of its own: the record of the fields that stands for.
Only a type variable of class names a row variable, so { number | x : Int } does not elaborate.
By the grammar is 2 or 3.
For to be defined, must have arity , so a type constructor is never partially applied. Application of a type function performs the substitution, so a type alias denotes its expansion, and an alias with a row parameter applies only to a record type.
The syntactic restrictions ensure .
The side conditions give an integer constant the type in a pattern, never or a type variable of class ; a character or string constant has its own type. Thus case 1.5 of 1 -> 0 ; _
-> 1 does not elaborate. Floating point constants are not patterns, by the grammar.
The variable receives a type, not a type scheme. Constructors and variables are spelt differently, so no status test is needed here.
Only status is accepted, so the constructor of a record type alias, of status , cannot occur in a pattern. The instance must be a constructed type, so this rule accepts only a constructor without arguments; a constructor with arguments is covered by rule (43).
By the grammar is 2 or 3, and by the syntactic restrictions the domains of the are disjoint.
The record type may have fields besides those the pattern names, and it may be open: a record pattern matches every record with the fields it names.
A field of a record pattern binds the variable of its row to the type of the field. In a program the row is written as a label alone, whose equivalent form has (Appendix A); a copy made by number specialisation gives a new name and keeps (Section 6.2). The syntactic restrictions ensure . That follows, in a program, from the restriction that a binding site binds no variable twice, and in a copy from the distinct new names it gives.
The instance must be a constructed type after exactly arguments, so a constructor in a pattern is applied to as many patterns as it takes. Thus a constructed pattern with too few or too many arguments does not elaborate. The syntactic restrictions ensure that the domains of the are disjoint.
The type name is of the initial basis, whatever the program binds to List.
The syntactic restrictions ensure .
4.10Principal Derivations¶
A phrase may elaborate in more than one way. An integer constant may take any type in (rule (1)), and an occurrence of a variable any instance of its type scheme (rule (2)), so two derivations for one phrase may give its integer constants and its occurrences different types. Whether a phrase elaborates does not depend on the choice, but evaluation does: 2 ^ -1 is at and at . The dynamic semantics is therefore given for one derivation, the principal one, which gives no type that the phrase does not force; the further restrictions (Section 4.11) are judged on it too, although for them the choice makes no difference. In a derivation for a phrase, each node is a sentence about an occurrence of a phrase within it; the nodes of two derivations for the same phrase correspond when they are about the same occurrence. Let and be derivations for the same phrase in the same context , the type and row variables of that are not free in having been renamed apart from those of . Then is an instance of when there is a substitution (Section 4.5) that respects classes, with no free in , defined on every type of , such that at each pair of corresponding nodes, with contexts in and in ,
takes each variable in to a variable, and is the set of the variables so obtained;
each type in the sentence of is applied to the corresponding type in the sentence of ; and
each type scheme in the sentence of is no more general than , for the corresponding type scheme of : every type with has .
Since is defined on every type of , it never gives a record type a second field with a label it already has; no Core phrase generates type names, so . The third clause allows a derivation to give a binding a less general scheme than another derivation gives it, as when y = 2 ^ 64 * 4 is given the type where another derivation gives it the scheme with of class . A derivation for a phrase in a context is principal when every derivation for the phrase in that context is an instance of . Chapter 5 extends the definition to modules (Section 5.6).
Every Core phrase that elaborates in a context has a principal derivation in , and any two of its principal derivations are instances of each other, so that they differ only by a renaming of type and row variables not free in .
This is the principal type property of Damas and Milner, extended to classes, rows and annotations. Unification that respects classes has a most general solution whenever it has a solution, since two classes have a greatest common subclass or none: and give , and give , and and none (Section 4.4). The labels of a row are distinct (Section 4.5), the explicit variables of an annotation are fixed throughout its scope (Section 4.7), and a group of value bindings is monomorphic within itself (rule (19)), so the inference algorithm of Damas and Milner, extended in these ways, finds a principal derivation. The proof is not given here. Only the variables of class of a principal derivation affect evaluation: the value of an integer constant, and the result of a number operation of the initial basis, depend on whether a type is or , while no value depends on how a variable of another class, or of none, is instantiated. Number specialisation (Section 6.2) starts from the principal derivation and fixes its variables of class .
4.11Further Restrictions¶
There are a few restrictions on programs which are better expressed apart from the inference rules. They are judged on the principal derivation (Section 4.10): the type with which the patterns of a match elaborate, and the type of a pattern, are those that derivation gives. A phrase that elaborates by the rules but whose principal derivation breaks one of them is not legal; it is treated as a phrase that does not elaborate. In fact every derivation for the phrase gives the same answer. A pattern that the principal derivation elaborates at a type variable, at the top of a match or within another pattern, is a wildcard, a variable, or one of these with as or parentheses, since every other pattern (a special constant, a constructor, (), a tuple, [ ], :: or a record pattern) elaborates only at a type that is not a variable; and such a pattern covers every inhabitant. Another derivation may replace the variable by any type, which has inhabitants (below), and the same patterns cover them. The first two restrictions speak of the inhabitants of a type, which are phrases, possibly infinite, built from constructors and special constants, and of the patterns that cover them. The constructors of a type name are those that the declaration of the custom type with type name binds with status (Chapter 5), or, for a type name of the initial basis, those that Appendix C gives it; the constructors of are [] and ::. The inhabitants of a type are given by the first of the following clauses that applies:
if is , or , each special constant such that ;
if , the phrase
();if , the phrases , where each is an inhabitant of ;
if and has constructors, the phrases (written when is
::), where is a constructor of whose type scheme satisfies , and each is an inhabitant of ;otherwise (a function type, a record type, a type variable, or a type whose type name has no constructors), the single phrase
_.
The clauses are read coinductively: an inhabitant may be infinite, provided each of its parts is given by a clause. So type V = V V has the one inhabitant V (V (V …)), and Never, whose one constructor JustOneMore takes a Never (Appendix C), has the one inhabitant JustOneMore (JustOneMore …). Every type has an inhabitant, and so every constructor of a type gives inhabitants of it, at every instance. Inhabitants are not values: no value has type Never. An inhabitant describes what a pattern may be asked to match, which depends only on the constructors of a type. Thus with r of type Result Never Int, the second rule of case r of Ok n -> n ; Err e -> never e covers Err (JustOneMore …) and is not redundant, while with m of type Maybe Never, case m of Nothing -> 0 is not exhaustive. Where every type that a match involves has a finite inhabitant, the infinite inhabitants change neither restriction, since a pattern examines only a finite part of an inhabitant. The wildcard _, a variable and a record pattern cover every inhabitant. A special constant covers each special constant that denotes the same character or string or, for integer constants, has the same value as an (Chapter 2), however the two are written. A pattern () covers when is or and each covers ; likewise [ ] covers [], and covers when covers and covers . The pattern () covers (), a tuple pattern covers a tuple component by component, and and cover what covers. The restrictions are:
Irredundancy. In a match whose patterns elaborate with type , each must cover some inhabitant of that no with covers.
core-irredundancyExhaustiveness. In a match as above, every inhabitant of must be covered by some . Likewise, in a pattern binding and in a lambda , the pattern must cover every inhabitant of its type. The restrictions are inherited by derived forms: in a function definition , each must be exhaustive by itself. In deciding exhaustiveness, no set of special constants covers every inhabitant of , or , whatever the range of (Appendix E).
core-exhaustiveness
For an integer constant, τ may be any type in Admnumber: Int, Float or a type variable of class number. A floating point, character or string constant has exactly its type. The type that counts for evaluation is the one the principal derivation gives (Section 4.10); where it is a variable that nothing binds, number specialisation takes it to be Float (Section 6.2).
The instantiation of type schemes allows different occurrences of one longvar to take different types. The identifier status is not used in this rule, nor in rules (3) and (4); in particular the constructor of a record type alias, of status v, elaborates by rule (3) as any constructor does.
Infixed applications of operators are derived forms of this phrase (Appendix A), so this rule gives the type of every application of an operator. The context C is absent from the premise, so an operator has the same type scheme in every module, whatever the module declares or imports: a declaration of
addin a program does not change the meaning of+.By the grammar k is 2 or 3.
Note that τ does not occur in any premise; thus
[ ]has type τList for every τ.A record expression has a closed record type: its fields are exactly those written.
The side conditions require the type of var to be a record type that has every updated field, each at the type of its new value. The result has the type of var, so an update neither adds a field nor changes the type of one: if r has type {{x↦Int}} then
{ r | x = "s" }does not elaborate. The type of var may be open, so a function may update a field of any record that has it.The variables bound by dec are in scope in exp at the type schemes that rule (19) gives them, so exp may use them polymorphically.