Several derived forms are provided in the Core and in Modules; they are presented in Figures 27, 28, 29 and 30. Each derived form is given with its equivalent form, and each row of the tables is a rewriting rule
Derived form⟹Equivalent form
which may be applied to any subphrase, repeatedly, until the phrase is a phrase of the bare language of Chapters 2 and 3. Within a figure the rows are ordered, and the figures follow one another, so that every equivalent form uses only bare syntax and the derived forms of earlier rows. Two rows, the grouping of the definitions of a let and the grouping of the declarations of a module, are not local rewritings; their equivalent forms are defined in Sections A.1 and A.2. See Appendix B for the full grammar, including all the derived forms.
In equivalent forms, Basics.True and Basics.False are the constructors of the type Bool, Basics.negate is the negation function, and :: is the list constructor. They are looked up as any other identifier of the module in which the derived form occurs. 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 (Appendices C and D), whatever the module imports or declares; :: is bound in the initial environment E0 of those appendices, from which every module is elaborated and evaluated, and no program declares it. Being qualified, or an operator, none of them is a free occurrence of a variable in the sense below.
In the first row of Figure 27, exp1opexp2 is an infixed application as grouped by the resolution of Section 2.7: exp1 and exp2 are the two operands of op after grouping, and op is neither && nor ||. Each operator of a chain is rewritten in this way, outermost first. The infixed forms of && and || have rows of their own, which make them conditionals; the operator values (&&) and (||) are bare, and denote functions of the initial basis that take both arguments as values. In the row for negation, -atexp is a negation as defined in Section 2.5; it is atomic, so that it may be the argument of an application. In the row for field access, the atomic expressions that may precede .lab are restricted in Section 2.10 (the restrictions on records); after any other atomic expression, and after a field accessor, a . begins a field accessor (Section 2.5), so .lab1.lab2 is the application of .lab1 to .lab2. In the equivalent form of the field accessor .lab, the record pattern {lab=lab} binds the variable spelt as the label lab, and the body of the lambda is that variable. The pattern row is written in the bare syntax, lab=var with var spelt as lab, since the pattern row written as a label alone is a derived form of a later figure. An annotation var:ty and the definition of var that follows it (Chapter 2) are written together, here as in the bare grammar, as one value binding with :ty after var. Thus a definition is a value binding without and, of the form var⟨:ty⟩atpat1⋯atpatn=exp (n≥0) or atpat=exp.
The syntactic restrictions of Chapters 2 and 3 apply to a program as written, before any derived form in it is replaced by its equivalent form (Section 2.10), and are not checked again on equivalent forms. In particular the variable bound in the equivalent form of .lab may have the name of a variable in scope, and a restriction that distinguishes a definition with parameters from one without refers to the definition as written. An occurrence of a variable in a phrase is free if it is unqualified and is not within the scope of a binding of that variable, by a pattern or by a value binding, made inside the phrase. No equivalent form adds or removes a free occurrence of a variable, so the groupings of Sections A.1 and A.2 are the same whichever other derived forms have already been rewritten.
No equivalent form introduces a new identifier, so no table carries a note of fresh identifiers. The only variables bound by equivalent forms are the one bound by the equivalent form of .lab and the one bound by the equivalent form of a pattern row written as a label alone; each is spelt as its label.
Figure 28: Derived forms of Patterns and Pattern Rows
In the equivalent form for lists, each :: is an infixed application whose right operand is everything to its right:
exp1::(exp2::⋯(expn::[ ])⋯).
In the equivalent form for list patterns, each :: is the bare pattern pat::pat, the parentheses make the result an atomic pattern, and for n=1 the equivalent form is (pat1::[ ]). In the row for pattern rows, var is the variable spelt as lab: the pattern { h, n } stands for { h = h, n = n }. A program writes every pattern row in this derived form (Section 2.10); only the copies of number specialisation (Section 6.2) have pattern rows whose variable is spelt otherwise.
A let expression as written has definitions valbind1,…,valbindn (n≥2), each a value binding without and. The bare language has letdecinexp only for a single group of bindings joined by the abstract and, so the definitions are grouped and ordered as follows.
The variables bound by a definition, and its right-hand side, are given by the following table, in which m≥1 and a definition of the first two forms may carry an annotation :ty after var:
The variables bound by a pattern are its variable patterns, the variables of its pattern rows (Section 2.10), and the variables following as in it. A definition valbindirefers to a definition valbindj if a variable bound by valbindj occurs free in the right-hand side of valbindi; j=i is allowed. Two definitions are in the same component if each can be reached from the other by a chain of zero or more steps of “refers to”. The components partition the definitions. A component is recursive if it has more than one member, or if its one member refers to itself. The members of the recursive components are exactly the definitions that lie on a cycle of “refers to”, and the restriction on recursion in Section 2.10 requires each of them to be a definition with parameters; in particular a definition atpat=exp is never in a recursive component. Number the components 1,…,k so that whenever a member of component a refers to a member of component b=a, then b<a. Such a numbering exists, because a chain of “refers to” that returns to its start never leaves a component. For each a, let valbindi1,…,valbindip, with i1<⋯<ip, be the members of component a, and let
An annotation is part of the definition it annotates, and so goes with that definition into deca.
When two components are independent (neither can be reached from the other) the numbering is not unique, and which admissible numbering is chosen does not matter: independent components bind distinct variables and neither refers to the other, the bindings of a component add no free type variable to the context (Clos generalises every type variable not free in the context, Section 4.8), and evaluation has neither state nor exceptions, so each component elaborates and evaluates to the same result whichever independent components precede it.
The declarations of a module body as written stand in any order. Each declaration topdeci is one of the following, where m≥0:
typedatbinditype aliastypbindiportvar:tyvar⟨:ty⟩atpat1⋯atpatm=expa custom type, declaring one type;an alias, declaring one alias;a port;a definition.
A module body has no definitions of the form atpat=exp (Chapter 3). A module body as written is always rewritten by this row, even when its declarations already stand in an order the row could produce. The equivalent form
is built as follows, where ; is the abstract separator of top-level declarations in the bare grammar (Chapter 3); a part that is omitted below is omitted with its separator.
Custom types. If typedatbindi1,…,typedatbindip, with i1<⋯<ip, are the custom types of the body, then all of them form one group,
datbind=datbindi1and⋯anddatbindip,
whose members may refer to one another. If p=0, the declaration typedatbind is omitted.
Aliases. An alias mentions another alias of the body if the type constructor that the other binds occurs, unqualified, in its body. The aliases typbind1′,…,typbindl′ are those of the body, ordered so that each comes after every alias it mentions. Such an order exists, because the restrictions of Chapter 3 forbid a chain of mentions that returns to its start. If l=0, there are no alias declarations.
Ports and definitions. A port portvar:ty binds var and has no right-hand side, so it refers to nothing; a definition binds its var and has the right-hand side of Section A.1. Over the ports and definitions of the body, “refers to”, components and their numbering 1,…,k are defined as in Section A.1. A port is alone in its component. For each a, topdeca′ is the port if component a is a port, and otherwise is the value group
valbindi1and⋯andvalbindiq,
where valbindi1,…,valbindiq, with i1<⋯<iq, are the members of component a, each with its annotation.
The type constructors bound by datbind and by the aliases are in scope throughout these type declarations: a constructor binding in datbind may mention an alias of the body, and an alias may mention a custom type of datbind. Chapter 5 elaborates the group datbind together with the aliases that follow it.
The values that the type declarations bind (the constructors of datbind and the constructors of record aliases) are thus in scope in every port and definition. As for a let, which order is chosen among aliases neither of which mentions the other, and which admissible numbering is chosen for the components, does not matter: an alias denotes the same type function whichever unrelated aliases precede it, and a component of ports and definitions elaborates and evaluates to the same result whichever independent components precede it, for the reasons given in Section A.1.