PDF hosted at the Radboud Repository of the Radboud University
Nijmegen
The following full text is a preprint version which may differ from the publisher's version.
For additional information about this publication click this link.
http://hdl.handle.net/2066/168735
Please be advised that this information was generated on 2017-12-06 and may be subject to
change.
CORE Metadata, citation and similar papers at core.ac.uk
Type-and-Scope Safe Programs and Their Proofs
Guillaume Allais
[email protected] Radboud University
James Chapman
Conor McBride
{james.chapman,conor.mcbride}@strath.ac.uk University of Strathclyde
James McKinna
[email protected]University of Edinburgh
Categories and Subject Descriptors D.2.4 [Software/Program Veri๏ฌcation]: Correctness Proofs; D.3.2 [Language Classi-๏ฌcations]: Applicative (functional) languages; F.3.2 [Se-mantics of Programming Languages]: Denotational seman-tics, Partial evaluation
Keywords Lambda-calculus, Mechanized Meta-Theory, Normalisation by Evaluation, Semantics, Generic Program-ming, Agda
Abstract
We abstract the common type-and-scope safe structure from computations on ๐-terms that deliver, e.g., renaming, sub-stitution, evaluation, CPS-transformation, and printing with a name supply. By exposing this structure, we can prove generic simulation and fusion lemmas relating operations built this way. This work has been fully formalised in Agda.
1.
Introduction
A programmer implementing an embedded language with bindings has a wealth of possibilities. However, should she want to be able to inspect the terms produced by her users in order to optimise or even compile them, she will have to work with a deep embedding. Which means that she will have to (re)implement a great number of traversals doing such mundane things as renaming, substitution, or partial evaluation. Should she want to get help from the typechecker in order to fend o๏ฌ common bugs, she can opt for inductive families (Dybjer 1991) to enforce precise invariants. But the traversals now have to be invariant preserving too!
In an unpublished manuscript, McBride (2005) observes the similarity between the types and implementations of re-naming and substitution for simply typed ๐-calculus (ST๐C) in a dependently typed language as shown in ๏ฌg. 1. There are three di๏ฌerences between the implemenations of renam-ing and substitution: (1) in the variable case, after renamrenam-ing
๐๐พ๐โถ (โ๐.๐ต๐บ๐๐ ๐ค โ๐ต๐บ๐๐ ๐ฅ) โ (โ๐.๐ณ๐๐ ๐ค โ๐ณ๐๐ ๐ฅ) ๐๐พ๐๐(โต๐๐บ๐๐ฃ) =โต๐๐บ๐(๐ ๐ฃ) ๐๐พ๐๐(๐ โต$๐ก) =๐๐พ๐๐ ๐โต$๐๐พ๐๐ ๐ก ๐๐พ๐๐(โต๐๐) =โต๐(๐๐พ๐((๐๐โ๐)โ,๐๐พ)๐) ๐๐๐ปโถ (โ๐.๐ต๐บ๐๐ ๐คโ๐ณ๐๐ ๐ฅ) โ (โ๐.๐ณ๐๐ ๐ค โ๐ณ๐๐ ๐ฅ) ๐๐๐ป๐(โต๐๐บ๐๐ฃ) =๐ ๐ฃ ๐๐๐ป๐(๐ โต$๐ก) =๐๐๐ป๐ ๐โต$๐๐๐ป๐ ๐ก ๐๐๐ป๐(โต๐๐) =โต๐(๐๐๐ป((๐๐พ๐๐๐โ๐)โ,โต๐๐บ๐ ๐๐พ)๐)
Figure 1. Renaming and Substitution for the ST๐C
๐๐๐โถ (โ๐.๐ต๐บ๐๐ ๐คโโฆ ๐ ๐ฅ) โ (โ๐.๐ณ๐๐ ๐ค โ๐ณ๐๐ ๐ฅ) ๐๐๐๐(โต๐๐บ๐๐ฃ) =๐ช๐๐.๐๐บ๐๐ (๐ ๐ฃ)
๐๐๐๐(๐โต$๐ก) =๐๐๐๐ ๐โต$๐๐๐๐ ๐ก
๐๐๐๐(โต๐๐) =โต๐(๐๐๐((๐ช๐๐.๐๐๐๐ โ๐)โ,๐ช๐๐.๐๐๐๐ )๐)
Figure 2. Kit traversal for the ST๐C, for ฮบ of type๐ช๐๐โฆ ๐๐ป๐พโถ (โ๐.๐ต๐บ๐๐ ๐คโ๐ต๐บ๐ ๐ ๐ฅ) โ (โ๐.๐ณ๐๐ ๐ค โ๐ต๐บ๐ ๐ ๐ฅ) ๐๐ป๐พ๐(โต๐๐บ๐๐ฃ) =๐ ๐ฃ
๐๐ป๐พ๐(๐โต$๐ก) =๐ ๐ฏ๐ฏ(๐๐ป๐พ๐ ๐) (๐๐ป๐พ๐ ๐ก)
๐๐ป๐พ๐(โต๐๐ก) =๐ซ๐ ๐ฌ(๐๐๐ ๐ฃโ๐๐ป๐พ((๐๐๐๐โ๐)โ,๐ฃ)๐ก)
Figure 3. Normalisation by Evaluation for the ST๐C
a variable we must wrap it in aโต๐๐บ๐constructor whereas a substitution directly produces a term; (2) when weakening a renaming to push it under a ๐ we need only post-compose the remaning with the De Bruijn variable successor construc-tor๐๐(which is essentially weakening for variables) whereas for a substitution we need a weakening operation for terms which can be given by renaming via the successor construc-tor๐๐พ๐๐๐. (3) also in the ๐ case when pushing a renaming or substitution under a binder we must extend it to ensure that the variable bound by the ๐ mapped to itself. For renaming this involves extended by the zeroth variable๐๐พwhereas for subsitutions we must extend by the zeroth variable seen as a termโต๐๐บ๐ ๐๐พ. He de๏ฌnes a notion of โKitโ abstracting these di๏ฌerences. The uses of๐ช๐๐.โoperations in the generalising
the traversal function๐๐๐are shown (in pink) in ๏ฌg. 2. The contributions of the present paper are twofold: โขWe generalise the โKitโ approach from syntax to
seman-tics bringing operations like normalisation (cf. ๏ฌg. 3) and printing with a name supply into our framework. โขWe prove generic results about simulations between and
fusions of semantics given by, and enabled by, Kit. Outline We start by de๏ฌning the simple calculus we will use as a running example. We then introduce a notion of en-vironments and one well known instance: the category of re-namings. This leads us to de๏ฌning a generic notion of type and scope-preserving Semantics together with a generic eval-uation function. We then showcase the ground covered by these Semantics: from the syntactic ones corresponding to renaming and substitution to printing with names, variations of Normalisation by Evaluation or CPS transformations. Fi-nally, given the generic de๏ฌnition of Semantics, we can prove fundamental lemmas about these evaluation functions: we characterise the semantics which can simulate one another and give an abstract treatment of composition yielding com-paction and reuse of proofs compared to Benton et al. (2012). Notation This article is a literate Agda ๏ฌle. We hide tele-scopes of implicit arguments and๐ฒ๐พ๐levels, and properly dis-play (super / sub)-scripts as well as special operators such as >>=or ++. Colours help:๐๐๐พ๐พ๐identi๏ฌers are data con-structors,๐๐๐๐names refer to record ๏ฌelds,๐ป๐ ๐๐พis character-istic of de๏ฌned symbols, and comments areredtypewrite font. Underscores have a special status: when de๏ฌning mix๏ฌx identi๏ฌers (Danielsson and Norell 2011), they mark positions where arguments may be inserted.
Formalisation This whole development1has been checked by Agda (Norell 2009) which guarantees that all construc-tions are indeed well typed, and all funcconstruc-tions are total. Nonetheless, it should be noted that the generic model con-structions and the various examples of๐ฒ๐พ๐๐บ๐๐๐๐ผ๐given here, although not the proofs, can and have been fully replicated in Haskell using type families, higher rank polymorphism and GADTs to build singletons (Eisenberg and Weirich 2013) providing the user with the runtime descriptions of their types or their contextsโ shapes. This yields, to the best of our knowledge, the ๏ฌrst tagless and typeful implementation of a Kripke-style Normalisation by Evaluation in Haskell.
2.
The Calculus and its Embedding
๐, ๐ โท= ๐ท | ๐ธ | ๐ โ ๐ ๐, ๐ก, ๐ข โท= ๐ฅ | ๐ก ๐ข | ๐๐ฅ. ๐ | โจโฉ
| ๐๐ | ๐๐ | ๐๐ ๐ ๐๐๐๐ ๐ก ๐๐๐๐ ๐ข We work with a deeply embedded simply typed ๐-calculus. It has 1 and 2 as base types and serves as a minimal example of a system with a record type equipped with an ฮท-rule and 1https://github.com/gallais/type-scope-semantics
a sum type. This grammar is represented in Agda as follows: ๐ฝ๐บ๐๐บ๐ณ๐โถ๐ฒ๐พ๐๐๐๐พ๐๐พ โต๐ฃโต๐ค โถ๐ณ๐ _โตโ_ โถ๐ณ๐โ๐ณ๐โ๐ณ๐ ๐ฝ๐บ๐๐บ๐ข๐(๐ก๐ฆโถ๐ฒ๐พ๐) โถ๐ฒ๐พ๐๐๐๐พ๐๐พ ๐ โถ๐ข๐๐ก๐ฆ _โ_ โถ๐ข๐๐ก๐ฆโ๐ก๐ฆโ๐ข๐๐ก๐ฆ To talk about the types of the variables in scope, we need contexts. We choose to represent them as โsnocโ lists of types;๐denotes the empty context and ๐ค โ๐ the context ๐ค extended with a fresh variable of type ๐.
To make type signatures more readabale, we introduce combinators acting on context-indexed types. The most straightforward ones are pointwise lifting of existing oper-ators on types, and we denote them as dotted versions of their counterparts: the de๏ฌnition of the pointwise function space_ ฬโ _is shown here and the reader will infer the cor-responding one for pointwise disjoint sums (_ ฬโ_) and prod-ucts (_ ฬร_). The โuniversallyโ operator [_] turn a context-indexed type into a type using an (implicit) universal quan-ti๏ฌcation. Last but not least, the operator_โข_ mechanizes the mathematical convention of only mentioning context ex-tensions when presenting judgements (Martin-Lรถf 1982).
_ ฬโ _โถ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) โ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) โ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) (๐ โฬ ๐)๐ค =๐ ๐คโ๐ ๐ค [_]โถ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) โ๐ฒ๐พ๐ [๐ ]= โ {๐ค} โ๐ ๐ค _โข_โถ๐ก๐ฆโ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) โ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) (๐โข๐)๐ค =๐(๐ค โ๐)
Variables are then positions in such a context represented as typed de Bruijn (1972) indices. As shown in the com-ments, this amounts to an inductive de๏ฌnition of context membership. We use the combinators de๏ฌned above to show only local changes to the context.
๐ฝ๐บ๐๐บ๐ต๐บ๐(๐โถ๐ก๐ฆ) โถ๐ข๐๐ก๐ฆโ๐ฒ๐พ๐๐๐๐พ๐๐พ
๐๐พ โถ โ โ ฮ. Var ฯ (ฮ โ ฯ)
[ ๐โข ๐ต๐บ๐๐]
๐๐ โถ โ โ ฮ ฯ. Var ฯ ฮ โ Var ฯ (ฮ โ ฯ)
[ ๐ต๐บ๐๐ โฬ (๐โข ๐ต๐บ๐๐)]
The syntax for this calculus guarantees that terms are well scoped-and-typed by construction. This presentation due to Altenkirch and Reus (1999) relies heavily on Dyb-jerโs (1991) inductive families. Rather than having untyped pre-terms and a typing relation assigning a type to them, the typing rules are here enforced in the syntax. Notice that the only use of_โข_to extend the context is for the body of a ๐.
๐ฝ๐บ๐๐บ๐ณ๐โถ๐ณ๐โ๐ข๐ ๐ณ๐โ๐ฒ๐พ๐๐๐๐พ๐๐พ โต๐๐บ๐ โถ [ ๐ต๐บ๐๐ โฬ ๐ณ๐๐ ] _โต$_ โถ [ ๐ณ๐(๐โตโ๐) โ ๐ณ๐ฬ ๐ โฬ ๐ณ๐๐ ] โต๐ โถ [๐โข ๐ณ๐๐ โฬ ๐ณ๐(๐โตโ๐) ] โตโจโฉ โถ [ ๐ณ๐โต๐ฃ ] โต๐๐โต๐ฟ๐ฟ โถ [ ๐ณ๐โต๐ค ] โต๐๐ฟ โถ [ ๐ณ๐โต๐ค โ ๐ณ๐ฬ ๐ โ ๐ณ๐ฬ ๐ โ ๐ณ๐ฬ ๐ ]
3.
A Generic Notion of Environment
All the semantics we are interested in de๏ฌning associate to a term ๐ก of type๐ณ๐๐ ๐ค , a value of type ๐ ๐ ๐ฅ given an inter-pretation โฐ ๐ฅ ฯ for each one of its free variables ๐ in ๐ค . We call the collection of these interpretations an โฐ-(evaluation) environment. We leave out โฐ when it can easily be inferred from the context. The content of environments may vary wildly between di๏ฌerent semantics: when de๏ฌning renam-ing, the environments will carry variables whilst the ones used for normalisation by evaluation contain elements of the model. But their structure stays the same which prompts us to de๏ฌne the notion generically for a notion of ๐ฌ๐๐ฝ๐พ๐ .
๐ฌ๐๐ฝ๐พ๐ โถ๐ฒ๐พ๐
๐ฌ๐๐ฝ๐พ๐ =๐ก๐ฆโ๐ข๐๐ก๐ฆโ๐ฒ๐พ๐
Formally, this translates to โฐ-environments being the pointwise lifting of the relation โฐ between contexts and types to a relation between two contexts. Rather than us-ing a datatype to represent such a liftus-ing, we choose to use a function space. This decision is based on Je๏ฌreyโs obser-vation (2011) that one can obtain associativity of append for free by using di๏ฌerence lists. In our case the interplay between various combinators (e.g.๐๐พ๐ฟ๐ and ๐๐พ๐ ๐พ๐ผ๐) de๏ฌned later on is vastly simpli๏ฌed by this rather simple decision.
๐๐พ๐ผ๐๐๐ฝ_โ๐ค๐๐(๐ค โถ๐ข๐๐ก๐ฆ) (๐ฑโถ๐ฌ๐๐ฝ๐พ๐ ) (๐ฅโถ๐ข๐๐ก๐ฆ) โถ๐ฒ๐พ๐
๐๐๐พ๐๐พ ๐ผ๐๐๐๐๐๐๐ผ๐๐๐๐๐บ๐ผ๐
๐ฟ๐๐พ๐ ๐ฝ๐ ๐๐๐๐๐โถ๐ต๐บ๐๐ ๐คโ๐ฑ ๐ ๐ฅ
Just as an environment interprets variables in a model, a computation gives a meaning to terms into a model.
_โ๐ข๐๐๐โถ๐ข๐ ๐ณ๐โ (๐โถ๐ฌ๐๐ฝ๐พ๐ ) โ๐ข๐ ๐ณ๐โ๐ฒ๐พ๐ (๐ค โ๐ข๐๐๐)๐ ๐ฅ=๐ณ๐๐ ๐ค โ๐ ๐ ๐ฅ
An appropriate notion of semantics for the calculus is one that will map environments to computations. In other words, a set of constraints on ๐ฑ and ๐ guaranteeing the existence of a function of type:[(๐ค โ๐ค๐๐) ๐ฑ โฬ (๐ค โ๐ข๐๐๐) ๐]
These environments naturally behave like the contexts they are indexed by: there is a trivial environment for the empty context and one can easily extend an existing one by providing an appropriate value. The packaging of the function representing to the environment in a record allows for two things: it helps the typechecker by stating explic-itly which ๐ฌ๐๐ฝ๐พ๐ the values correspond to and it empow-ers us to de๏ฌne environments by copattern-matching (Abel et al. 2013) thus de๏ฌning environments by their use cases.
โต๐โถ[(๐โ๐ค๐๐)๐ฑ]
_โตโ_โถ [(๐ค โ๐ค๐๐)๐ฑ โฬ ๐ฑ ๐ โฬ (๐ค โ๐โ๐ค๐๐)๐ฑ]
๐ ๐๐๐๐๐โต๐ ()
๐ ๐๐๐๐๐(๐โตโ๐ ) ๐๐พ =๐
๐ ๐๐๐๐๐(๐โตโ๐ ) (๐๐๐) =๐ ๐๐๐๐๐๐ ๐
The Category of Renamings A key instance of environ-ments playing a predominant role in this paper is the notion of renaming. The reader may be accustomed to the more re-strictive notion of renamings as described variously as Order Preserving Embeddings (Chapman 2009), thinnings (which we use) or context inclusions, or just weakenings (Altenkirch et al. 1995). Writing non-injective or non-order preserving renamings would take perverse e๏ฌort given that we only implement generic interpretations. In practice, although the type of renamings is more generous, we only introduce weak-enings (skipping variables at the beginning of the context) that become thinnings (skipping variables at arbitrary points in the context) when we push them under binders.
A thinning ๐ค โ๐ฅ is an environment pairing each variable of type ๐ in ๐ค to one of the same type in ๐ฅ.
_โ_โถ (๐ค ๐ฅโถ๐ข๐๐ก๐ฆ) โ๐ฒ๐พ๐
๐ค โ๐ฅ= (๐ค โ๐ค๐๐)๐ต๐บ๐๐ฅ
We formulate a thinning principle usingโ. By a โthin-ning principleโ, we mean that if ๐ holds of ๐ค and ๐ค โ๐ฅ then ๐ holds for ๐ฅ too. In the case of variables, thinning merely corresponds to applying the renaming function in order to obtain a new variable. The environmentsโ case is also quite simple: being a pointwise lifting of a relation ๐ฑ between contexts and types, they enjoy thinning if ๐ฑ does.
๐ณ๐๐๐๐๐บ๐ป๐ ๐พโถ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) โ๐ฒ๐พ๐ ๐ณ๐๐๐๐๐บ๐ป๐ ๐พ๐=๐ค โ๐ฅโ (๐ ๐คโ๐ ๐ฅ) ๐๐๐ต๐บ๐โถ (๐โถ๐ก๐ฆ) โ๐ณ๐๐๐๐๐บ๐ป๐ ๐พ(๐ต๐บ๐๐) ๐๐๐ต๐บ๐๐ ๐๐๐ ๐ฃ=๐ ๐๐๐๐๐๐๐๐ ๐ฃ ๐๐[_]โถ ((๐โถ๐ก๐ฆ) โ๐ณ๐๐๐๐๐บ๐ป๐ ๐พ(๐ฑ ๐)) โ ๐ณ๐๐๐๐๐บ๐ป๐ ๐พ((๐ค โ๐ค๐๐)๐ฑ) ๐ ๐๐๐๐๐(๐๐[๐กโ]๐๐๐ ๐) =๐กโ_๐๐๐โ๐ ๐๐๐๐๐๐
These simple observations allow us to prove that thin-nings form a category which, in turn, lets us provide the user with the constructors Altenkirch, Hofmann and Streicherโs โCategory of Weakeningโ (1995) is based on.
๐๐พ๐ฟ ๐ โถ๐ค โ๐ค ๐๐พ๐ฟ ๐ =๐๐บ๐ผ๐๐๐ฝ ๐๐พ๐ ๐พ๐ผ๐โถ๐ค โ๐ฅโ (๐ฅโ๐ค๐๐)๐ฑ ๐ฉโ (๐ค โ๐ค๐๐)๐ฑ ๐ฉ ๐ ๐๐๐๐๐(๐๐พ๐ ๐พ๐ผ๐๐๐๐ ๐) =๐ ๐๐๐๐๐๐โ๐ ๐๐๐๐๐๐๐๐ ๐๐๐พ๐โถ๐ค โ๐ฅโ๐ค โ(๐ฅโ๐) ๐๐๐พ๐๐๐๐=๐๐พ๐ ๐พ๐ผ๐๐๐๐(๐๐บ๐ผ๐ ๐๐) ๐๐๐!โถ๐ค โ๐ฅโ (๐ค โ๐)โ(๐ฅโ๐) ๐๐๐!๐๐๐=๐๐๐พ๐๐๐๐โตโ๐๐พ
The modal operatorโกstates that a given predicate holds for all thinnings of a context. It is a closure operator for
โกโถ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) โ (๐ข๐๐ก๐ฆโ๐ฒ๐พ๐) (โก๐)๐ค = {๐ฅโถ๐ข๐_} โ๐ค โ๐ฅโ๐ ๐ฅ
๐๐โกโถ๐ณ๐๐๐๐๐บ๐ป๐ ๐พ(โก๐)
๐๐โก๐๐๐ ๐ =๐ โ ๐๐พ๐ ๐พ๐ผ๐๐๐๐
Now that we are equipped with the notion of inclusion, we have all the pieces necessary to describe the Kripke structure of our models of the simply typed ๐-calculus.
4.
Semantics and their Generic Evaluators
The upcoming sections demonstrate that renaming, substitu-tion, printing with names, and normalisation by evaluation all share the same structure. We start by abstracting away a no-tion of๐ฒ๐พ๐๐บ๐๐๐๐ผ๐encompassing all these constructions. This approach will make it possible for us to implement a generic traversal parametrised by such a๐ฒ๐พ๐๐บ๐๐๐๐ผ๐once and for all and to focus on the interesting model constructions instead of repeating the same pattern over and over again.A๐ฒ๐พ๐๐บ๐๐๐๐ผ๐is indexed by two relations ๐ฑ and ๐ describ-ing respectively the values in the environment and the ones in the model. In cases such as substitution or normalisation by evaluation, ๐ฑ and ๐ will happen to coincide but keep-ing these two relations distinct is precisely what makes it possible to go beyond these and also model renaming or printing with names. The record packs the properties of these relations necessary to de๏ฌne the evaluation function.
๐๐พ๐ผ๐๐๐ฝ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐(๐ฑโถ๐ฌ๐๐ฝ๐พ๐ ) (๐โถ๐ฌ๐๐ฝ๐พ๐ ) โถ๐ฒ๐พ๐๐๐๐พ๐๐พ
The ๏ฌrst method of a ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐ deals with environ-ment values. They need to be thinnable (๐๐) so that the traversal may introduce fresh variables when going un-der a binun-der whilst keeping the environment well-scoped.
๐๐ โถ (๐โถ๐ณ๐) โ๐ณ๐๐๐๐๐บ๐ป๐ ๐พ(๐ฑ ๐)
The structure of the model is quite constrained: each con-structor in the language needs a semantic counterpart. We start with the two most interesting cases:โฆ๐๐บ๐โงandโฆ๐โง. The variable case bridges the gap between the fact that the envi-ronment translates variables into values ๐ฑ but the evaluation function returns computations ๐.
โฆ๐๐บ๐โงโถ [๐ฑ ๐ โฬ ๐ ๐]
The semantic ๐-abstraction is notable for two reasons: ๏ฌrst, following Mitchell and Moggi (1991), itsโก-structure is typical of models ร la Kripke allowing arbitrary extensions of the context; and second, instead of being a function in the host language taking computations to computations, it takes values to computations. It matches precisely the fact that the body of a ๐-abstraction exposes one extra free variable, prompting us to extend the environment with a value for it. In the special case where ๐ฑ = ๐ (normalisation by evalu-ation for instance), we recover the usual Kripke structure.
โฆ๐โงโถ [ โก(๐ฑ ๐ โฬ ๐ ๐) โฬ ๐(๐โตโ๐)]
The remaining ๏ฌeldsโ types are a direct translation of the types of the constructor they correspond to: substructures have simply been replaced with computations thus mak-ing these operators ideal to combine induction hypotheses.
_โฆ$โง_ โถ [๐(๐โตโ๐) โฬ ๐ ๐ โฬ ๐ ๐ ]
โฆโจโฉโง โถ [ ๐โต๐ฃ ]
โฆ๐๐โง โถ [ ๐โต๐ค ]
โฆ๐ฟ ๐ฟ โง โถ [ ๐โต๐ค ]
โฆ๐๐ฟ โง โถ [๐โต๐ค โฬ ๐ ๐ โฬ ๐ ๐ โฬ ๐ ๐ ]
The type we chose for โฆ๐โงmakes the ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐notion powerful enough that even logical predicates are instances of it. And we indeed exploit this power when de๏ฌning nor-malisation by evaluation as a semantics: the model construc-tion is, after all, nothing but a logical predicate. As a con-sequence it seems rather natural to call๐๐พ๐, the fundamen-tal lemma of semantics. We prove it in a module parame-terised by a๐ฒ๐พ๐๐บ๐๐๐๐ผ๐, which would correspond to using a Section in Coq. It is de๏ฌned by structural recursion on the term. Each constructor is replaced by its semantic counter-part which combines the induction hypotheses for its sub-terms. ๐๐๐ฝ๐๐ ๐พ๐ค๐๐บ๐ (๐ฎ โถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฑ ๐)๐๐๐พ๐๐พ ๐๐๐พ๐๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฎ ๐๐พ๐โถ[(๐ค โ๐ค๐๐)๐ฑ โฬ (๐ค โ๐ข๐๐๐)๐] ๐๐พ๐๐(โต๐๐บ๐๐ฃ) =โฆ๐๐บ๐โง(๐ ๐๐๐๐๐๐ ๐ฃ) ๐๐พ๐๐(๐กโต$๐ข) =๐๐พ๐๐ ๐กโฆ$โง๐๐พ๐๐ ๐ข ๐๐พ๐๐(โต๐๐) =โฆ๐โง (๐๐ ๐ฃโ ๐๐พ๐(๐๐[๐๐]๐ ๐โตโ๐ฃ)๐) ๐๐พ๐๐โตโจโฉ =โฆโจโฉโง ๐๐พ๐๐โต๐๐ =โฆ๐๐โง ๐๐พ๐๐โต๐ฟ ๐ฟ =โฆ๐ฟ๐ฟ โง ๐๐พ๐๐(โต๐๐ฟ ๐ ๐ ๐) =โฆ๐๐ฟโง(๐๐พ๐๐ ๐) (๐๐พ๐๐ ๐) (๐๐พ๐๐ ๐)
5. Syntax is the Identity Semantics
As we have explained earlier, this work has been directly in๏ฌuenced by McBrideโs (2005) manuscript. It seems ap-propriate to start our exploration of ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐ with the two operations he implements as a single traversal. We call these operations syntactic because the computations in the model are actual terms and almost all term constructors are kept as their own semantic counterpart. As observed by McBride, it is enough to provide three operations de-scribing the properties of the values in the environment to get a full-blown๐ฒ๐พ๐๐บ๐๐๐๐ผ๐. This fact is witnessed by our simple ๐ฒ๐๐๐๐บ๐ผ๐๐๐ผ record type together with the ๐๐๐๐๐บ๐ผ๐๐๐ผ
function turning its inhabitants into associated๐ฒ๐พ๐๐บ๐๐๐๐ผ๐. ๐๐พ๐ผ๐๐๐ฝ๐ฒ๐๐๐๐บ๐ผ๐๐๐ผ(๐ฑโถ๐ฌ๐๐ฝ๐พ๐ ) โถ๐ฒ๐พ๐๐๐๐พ๐๐พ
๐ฟ ๐๐พ๐ ๐ฝ ๐๐ โถ (๐โถ๐ณ๐) โ๐ณ๐๐๐๐๐บ๐ป๐ ๐พ(๐ฑ ๐)
๐๐บ๐๐ข โถ[ ๐โข๐ฑ ๐ ]
๐๐๐๐๐บ๐ผ๐๐๐ผโถ๐ฒ๐๐๐๐บ๐ผ๐๐๐ผ๐ฑโ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฑ๐ณ๐ ๐๐๐๐๐บ๐ผ๐๐๐ผ๐ ๐ฆ๐=๐ ๐พ๐ ๐๐๐พ๐๐ฒ๐๐๐๐บ๐ผ๐๐๐ผ๐ ๐ฆ๐๐๐ ๐๐พ๐ผ๐๐๐ฝ
{๐๐ =๐๐;โฆ๐๐บ๐โง =โฆ๐๐บ๐โง
;โฆ๐โง = ๐๐กโโต๐(๐ก(๐๐๐พ๐ ๐๐พ๐ฟ๐ )๐๐บ๐๐ข) ;_โฆ$โง_=_โต$_
;โฆโจโฉโง=โตโจโฉ;โฆ๐๐โง=โต๐๐;โฆ๐ฟ๐ฟโง=โต๐ฟ๐ฟ;โฆ๐๐ฟโง =โต๐๐ฟ}
The shape of โฆ๐โง or โฆโจโฉโง should not trick the reader into thinking that this de๏ฌnition performs some sort of ฮท-expansion: ๐๐พ๐ indeed only ever uses one of these when the evaluated termโs head constructor is already respectively a โต๐ or a โตโจโฉ. It is therefore absolutely possible to de๏ฌne renaming or substitution using this approach. We can now port McBrideโs de๏ฌnitions to our framework.
Functoriality, also known as Renaming Our ๏ฌrst example of a ๐ฒ๐๐๐๐บ๐ผ๐๐๐ผ operation works with variables as environ-ment values. We have already de๏ฌned thinning earlier (see Section 3) and we can turn a variable into a term by using theโต๐๐บ๐constructor. The type of๐๐พ๐specialised to this se-mantics is then precisely the proof that terms are thinnable.
๐๐๐ณ๐โถ (๐โถ๐ณ๐) โ๐ณ๐๐๐๐๐บ๐ป๐ ๐พ(๐ณ๐๐)
๐๐๐ณ๐๐ ๐ ๐ก=๐ ๐พ๐ ๐๐๐พ๐๐ค๐๐บ๐ ๐ฑ๐พ๐๐บ๐๐๐๐๐๐๐๐พ๐๐ ๐ก
Simultaneous Substitution Our second example of a se-mantics is another spin on the syntactic model: environment values are now terms. We get thinning for terms from the previous example. Again, specialising the type of ๐๐พ๐ re-veals that it delivers precisely the simultaneous substitution.
๐๐๐ป๐๐โถ (๐ค โ๐ค๐๐)๐ณ๐๐ฅโ๐ณ๐๐ ๐ค โ๐ณ๐๐ ๐ฅ
๐๐๐ป๐๐๐ ๐ก=๐ ๐พ๐ ๐๐๐พ๐๐ค๐๐บ๐ ๐ฒ๐๐ป๐๐๐๐๐๐๐๐๐๐๐๐๐พ๐๐ ๐ก
6.
Printing with Names
Before considering the various model constructions involved in de๏ฌning normalisation functions deciding di๏ฌerent equa-tional theories, let us make a detour to a perhaps slightly more surprising example of a๐ฒ๐พ๐๐บ๐๐๐๐ผ๐: printing with names. A user-facing project would naturally avoid directly building a
๐ฒ๐๐๐๐๐and rather construct an inhabitant of a more sophisti-cated datatype in order to generate a prettier output (Hughes 1995; Wadler 2003). But we stick to the simpler setup as pretty printing is not our focus here.
This example is interesting for two reasons. Firstly, the distinction between values and computations is once more instrumental: we get to give the procedure a precise type guiding our implementation. The environment carries names for the variables currently in scope whilst the computa-tions thread a name-supply (a stream of strings) to be used to generate fresh names for bound variables. If the values in the environment had to be computations too, we would not root out some faulty implementations e.g a program picking a new name each time a variable is mentioned.
๐๐พ๐ผ๐๐๐ฝ๐ญ๐บ๐๐พ(๐โถ๐ณ๐) (๐ค โถ๐ข๐ ๐ณ๐) โถ๐ฒ๐พ๐๐๐๐พ๐๐พ
๐ผ๐๐๐๐๐๐๐ผ๐๐๐๐๐๐ญ;๐ฟ๐๐พ๐ ๐ฝ๐๐พ๐๐ญโถ๐ฒ๐๐๐๐๐
๐๐พ๐ผ๐๐๐ฝ๐ฏ๐๐๐๐๐พ๐(๐โถ๐ณ๐) (๐ค โถ๐ข๐ ๐ณ๐) โถ๐ฒ๐พ๐๐๐๐พ๐๐พ ๐ผ๐๐๐๐๐๐๐ผ๐๐๐๐๐๐ฏ
๐ฟ ๐๐พ๐ ๐ฝ๐๐๐๐ฏโถ๐ฒ๐๐บ๐๐พ(๐ฒ๐๐๐พ๐บ๐ ๐ฒ๐๐๐๐๐)๐ฒ๐๐๐๐๐
Secondly, the fact that the modelโs computation type is a monad and that this poses no problem whatsoever in this framework means it is appropriate for handling lan-guages with e๏ฌects (Moggi 1991), or e๏ฌectful semantics e.g. logging the various function calls. Here is the full def-inition of the printer assuming the existence of various
๐ฟ ๐๐๐๐บ๐ primitives picking a way to display โต๐, โต$ andโต๐๐ฟ.
๐ฏ๐๐๐๐๐๐๐โถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐ ๐ญ๐บ๐๐พ ๐ฏ๐๐๐๐๐พ๐ ๐ฏ๐๐๐๐๐๐๐=๐๐พ๐ผ๐๐๐ฝ {๐๐ = ๐_ _โ๐๐๐ญโ๐๐พ๐๐ญ ;โฆ๐๐บ๐โง =๐๐๐ฏโ ๐๐พ๐๐๐๐ โ๐๐พ๐๐ญ ;_โฆ$โง_ = ๐๐๐ ๐๐กโ๐๐๐ฏ( ๐ฟ ๐๐๐๐บ๐$ <$>๐๐๐๐ฏ๐๐โ๐๐๐๐ฏ๐๐ก) ;โฆ๐โง = ๐๐๐โ๐๐๐ฏ( ๐๐พ๐ >>=๐๐๐ โ๐ ๐พ๐๐ฅโฒ=๐๐พ๐บ๐ฝ๐๐ ๐๐ ๐๐๐(๐๐บ๐๐ ๐๐ ) >>=๐_โ ๐๐๐๐ฏ(๐๐(๐๐๐พ๐ ๐๐พ๐ฟ ๐ ) (๐๐๐ญ๐ฅโฒ)) >>=๐๐โฒโ ๐๐พ๐๐๐๐(๐ฟ๐๐๐๐บ๐๐๐ฅโฒ๐โฒ)) ;โฆโจโฉโง =๐๐๐ฏ(๐๐พ๐๐๐๐โโจโฉโ) ;โฆ๐๐โง =๐๐๐ฏ(๐๐พ๐๐๐๐โttโ) ;โฆ๐ฟ ๐ฟ โง =๐๐๐ฏ(๐๐พ๐๐๐๐โffโ) ;โฆ๐๐ฟ โง = ๐๐๐ ๐๐ ๐๐โ๐๐๐ฏ( ๐ฟ๐๐๐๐บ๐๐จ๐ฟ <$>๐๐๐๐ฏ๐๐โ๐๐๐๐ฏ๐๐โ๐๐๐๐ฏ๐๐) }
The evaluation function๐๐พ๐will deliver a printer which needs to be run on a๐ฒ๐๐๐พ๐บ๐of distinct๐ฒ๐๐๐๐๐s. Our de๏ฌnition of๐๐บ๐๐พ๐(not shown here) simply cycles through the letters of the alphabet and guarantess uniqueness by appending a natural number incremented each time we are back at the beginning of the cycle. This crude name generation strategy would naturally be replaced with a more sophisticated one in a user-facing language: we could e.g. use naming hints for user-introduced binders and type-based schemes otherwise (๐ or ๐ for function, ๐s or ๐s for integers, etc.).
In order to kickstart the evaluation, we still need to pro-vide ๐ญ๐บ๐๐พs for each one of the free variables in scope. We deliver that environment by a simple stateful compu-tation๐๐๐๐chopping o๏ฌ an initial segment of the name supply of the appropriate length. The de๏ฌnition of ๐๐๐๐๐ follows:
๐๐๐๐โถ๐ฒ๐๐บ๐๐พ(๐ฒ๐๐๐พ๐บ๐ ๐ฒ๐๐๐๐๐) ((๐ค โ๐ค๐๐)๐ญ๐บ๐๐พ๐ค) ๐๐๐๐๐โถ๐ณ๐๐ ๐คโ๐ฒ๐๐๐๐๐
๐๐๐๐๐๐ก=๐ ๐พ๐ ๐๐๐พ๐๐ค๐๐บ๐ ๐ฏ๐๐๐๐๐๐๐๐๐
๐๐๐๐๐ฃ((๐๐๐๐ >>=๐๐โ๐๐๐๐ฏ(๐๐พ๐๐ ๐ก))๐๐บ๐๐พ๐)
We can observe ๐๐๐๐๐โs behaviour by writing a test; we state it as a propositional equality and prove it using๐๐พ๐ฟ๐ , forcing the typechecker to check that both expressions indeed compute to the same normal form. Here we display the
iden-tity function de๏ฌned in a context of size 2. As we can see, the binder receives the nameโcโ because โaโ and โbโ
have already been assigned to the free variables in scope.
๐๐๐พ๐๐๐๐จ๐ฝโถ (โต๐(โต๐๐บ๐ ๐๐พ))โกโฮปc. cโ
๐๐๐พ๐๐๐๐จ๐ฝ=๐ฏ๐ค๐.๐๐พ๐ฟ๐
7.
Normalisation by Evaluation
Normalisation by Evaluation (NBE) is a technique leverag-ing the computational power of a host language in order to normalise expressions of a deeply embedded one. The pro-cess is based on a๐ฌ๐๐ฝ๐พ๐ construction describing a family of types by induction on its๐ณ๐index. Two procedures are then de๏ฌned: the ๏ฌrst (๐พ๐๐บ๐ ) constructs an element of ๐ ๐ ๐ค pro-vided a well typed term of the corresponding๐ณ๐๐ ๐ค type whilst the second (๐๐พ๐๐ฟ๐) extracts, in a type-directed manner, normal forms ๐ค โข๐๐ฟ ๐ from elements of the model ๐ ๐ ๐ค . NBE composes the two procedures. The de๏ฌnition of this
๐พ๐๐บ๐ function is a natural candidate for our๐ฒ๐พ๐๐บ๐๐๐๐ผ๐ frame-work. NBE is always de๏ฌned for a given equational theory; we start by recalling the various rules a theory may satisfy.
Thanks to๐ฑ๐พ๐๐บ๐๐๐๐and๐ฒ๐๐ป๐๐๐๐๐๐๐๐๐respectively, we can formally de๏ฌne ฮท-expansion and ฮฒ-reduction. The ฮท-rules say that for some types, terms have a canonical form: func-tions will all be ฮป-headed whilst records will collect their ๏ฌelds โ here this makes all elements of โต๐ฃ equal to โตโจโฉ.
๐พ๐๐บโถ[ ๐ณ๐(๐โตโ๐) โ ๐ณ๐ฬ (๐โตโ๐)] ๐พ๐๐บ๐ก=โต๐(๐๐๐ณ๐_ (๐๐๐พ๐ ๐๐พ๐ฟ๐ )๐กโต$โต๐๐บ๐ ๐๐พ) _โจ_/๐๐บ๐๐ขโฉโถ[๐โข ๐ณ๐๐ โ ๐ณ๐ฬ ๐ โ ๐ณ๐ฬ ๐] ๐กโจ๐ข/๐๐บ๐๐ขโฉ=๐๐๐ป๐๐(๐๐บ๐ผ๐ โต๐๐บ๐โตโ๐ข)๐ก ๐กโถ๐ณ๐(๐โตโ๐) ๐ค ๐ก โ๐พ๐๐บ๐ก ๐1 ๐กโถ๐ณ๐โต๐ฃ๐ค ๐ก โโตโจโฉ ๐2 (โต๐๐ก)โต$๐ข โ ๐กโจ๐ข/๐๐บ๐๐ขโฉ ๐ฝ
The ฮฒ-rule is the main driver for actual computation, but the presence of an inductive data type (โต๐ค) and its eliminator (โต๐๐ฟ) means we have further redexes: whenever the boolean the eliminator branches on is in canonical form, we may apply a ฮน-rule. Finally, the ฮพ-rule lets us reduce under ๐-abstractions โ the distinction between weak-head normal-isation and strong normalnormal-isation.
โต๐๐ฟโต๐๐๐ ๐ โ ๐๐1 โต๐๐ฟโต๐ฟ๐ฟ๐ ๐ โ ๐๐2
๐ก โ ๐ข
โต๐๐ก โโต๐๐ข๐
Now that we have recalled all these rules, we can talk precisely about the sort of equational theory decided by the model construction we choose to perform. We start with the usual de๏ฌnition of NBE which goes under ฮปs and produces ฮท-long ฮฒฮน-short normal forms.
7.1 Normalisation by Evaluation for ฮฒฮนฮพฮท
In the case of NBE, the environment values and the com-putations in the model will both have the same type ๐ช๐
(standing for โKripkeโ), de๏ฌned by induction on the๐ณ๐ ar-gument. The ฮท-rules allow us to represent functions (resp. inhabitants ofโต๐ฃ) in the source language as function spaces (resp.โค). In Agda, there are no such rules for boolean val-ues. We thus need a notion of syntactic normal forms. We parametrise the mutually de๏ฌned inductive families๐ญ๐พand
๐ญ๐ฟby a predicate ๐ constraining the types at which one may embed a neutral as a normal form. This make it possible to control the way NBE ๐-expands all terms at certain types.
๐ฝ๐บ๐๐บ๐ญ๐พโถ๐ฌ๐๐ฝ๐พ๐ ๐๐๐พ๐๐พ โต๐๐บ๐ โถ [ ๐ต๐บ๐๐ โฬ ๐ญ๐พ๐] _โต$_ โถ [ ๐ญ๐พ(๐โตโ๐) โ ๐ญ๐ฟฬ ๐ โฬ ๐ญ๐พ๐] โต๐๐ฟ โถ [ ๐ญ๐พโต๐ค โ ๐ญ๐ฟฬ ๐ โ ๐ญ๐ฟฬ ๐ โ ๐ญ๐พฬ ๐] ๐ฝ๐บ๐๐บ๐ญ๐ฟโถ๐ฌ๐๐ฝ๐พ๐ ๐๐๐พ๐๐พ โต๐๐พ โถ๐ ๐โ [ ๐ญ๐พ๐ โฬ ๐ญ๐ฟ๐ ] โตโจโฉ โถ [ ๐ญ๐ฟโต๐ฃ ] โต๐๐โต๐ฟ ๐ฟ โถ [ ๐ญ๐ฟโต๐ค ] โต๐ โถ [๐โข ๐ญ๐ฟ๐ โ ๐ญ๐ฟฬ (๐โตโ๐) ]
Once more, the expected notions of thinning๐๐๐๐พand๐๐๐๐ฟ
are induced as๐ญ๐ฟ and๐ญ๐พare syntaxes. We omit their purely structural implementation here and wish we could do so in source code, too: our constructions so far have been syntax-directed and could surely be leveraged by a generic account of syntaxes with binding. We now de๏ฌne the model. The
๐ฑpredicate characterising the types for which neutral terms may be considered normal is here equivalent to the unit type forโต๐คand the empty type otherwise. This makes us use ฮท-rules eagerly: all inhabitants of๐ญ๐ฟ ๐ค โต๐ฃand๐ญ๐ฟ ๐ค (๐ โตโ๐) are equal toโตโจโฉandโต๐-headed respectively.
The model construction then follows the usual pattern pioneered by Berger (1993) and formally analysed and thor-oughly explained by Catarina Coquand (2002). We work by induction on the type and describe ฮท-expanded values: all in-habitants of๐ช๐โต๐ฃ๐ค are equal and all elements of๐ช๐(๐โตโ๐) ๐ค are functions in Agda.
๐ช๐โถ๐ฌ๐๐ฝ๐พ๐ ๐ช๐โต๐ฃ =๐ผ๐๐๐๐ โค ๐ช๐โต๐ค =๐ญ๐ฟโต๐ค
๐ช๐(๐โตโ๐) =โก(๐ช๐๐ โ ๐ช๐ฬ ๐)
This model is de๏ฌned by induction on the type in terms ei-ther of syntactic objects (๐ญ๐ฟ) or using theโก-operator which is a closure operator for Thinnings. As such, it is trivial to prove that for all type ๐,๐ช๐ ๐ is๐ณ๐๐๐๐๐บ๐ป๐ ๐พ. Applicationโs semantic counterpart is easy to de๏ฌne: given that ๐ฑ and ๐ are equal in this instance de๏ฌnition, we just feed the argu-ment directly to the function, with the identity renaming: ๐ $$๐ก=๐ ๐๐พ๐ฟ ๐ ๐ก. Conditional branching however is more subtle: the boolean valueโต๐๐ฟ branches on may be a neutral term in which case the whole elimination form is stuck. This forces us to de๏ฌne๐๐พ๐๐ฟ๐and ๐๐พ๐ฟ ๐ ๐พ๐ผ๐๏ฌrst. These functions, also known as quote and unquote respectively, give the
inter-play between neutral terms, model values and normal forms.
๐๐พ๐ฟ๐ ๐พ๐ผ๐performs a form of semantic ฮท-expansion: all stuck
โต๐ฃ terms are equated and all functions are ๐-headed. It
al-lows us to de๏ฌne๐๐บ๐๐ข, the semantic counterpart of
โต๐๐บ๐ ๐๐พ. ๐๐พ๐๐ฟ ๐ โถ (๐โถ๐ณ๐) โ[ ๐ช๐๐ โ ๐ญ๐ฟฬ ๐] ๐๐พ๐ฟ ๐ ๐พ๐ผ๐ โถ (๐โถ๐ณ๐) โ[ ๐ญ๐พ๐ โ ๐ช๐ฬ ๐] ๐๐พ๐ฟ ๐ ๐พ๐ผ๐โต๐ฃ ๐ก=โจโฉ ๐๐พ๐ฟ๐ ๐พ๐ผ๐โต๐ค ๐ก=โต๐๐พ_๐ก ๐๐พ๐ฟ๐ ๐พ๐ผ๐(๐โตโ๐) ๐ก= ๐๐ ๐ขโ๐ ๐พ๐๐=๐๐๐๐พ(๐โตโ๐)๐ ๐ก ๐๐๐๐พ๐ฟ๐ ๐พ๐ผ๐๐(๐โต$๐๐พ๐๐ฟ๐๐ ๐ข) ๐๐พ๐๐ฟ๐โต๐ฃ ๐ =โตโจโฉ ๐๐พ๐๐ฟ๐โต๐ค ๐ =๐ ๐๐พ๐๐ฟ๐(๐โตโ๐) ๐ =โต๐(๐๐พ๐๐ฟ๐๐(๐ (๐๐๐พ๐ ๐๐พ๐ฟ๐ ) (๐๐บ๐๐ข๐)))
We can then give the semantics ofโต๐๐ฟ: if the boolean is a value, the appropriate branch is picked; if it is stuck the whole expression is re๏ฌected in the model.
๐๐ฟโถ[ ๐ช๐โต๐ค โ ๐ช๐ฬ ๐ โ ๐ช๐ฬ ๐ โ ๐ช๐ฬ ๐] ๐๐ฟโต๐๐ ๐ ๐=๐
๐๐ฟโต๐ฟ๐ฟ ๐ ๐=๐
๐๐ฟ(โต๐๐พ_๐) ๐ ๐=๐๐พ๐ฟ๐ ๐พ๐ผ๐๐(โต๐๐ฟ๐ (๐๐พ๐๐ฟ๐๐ ๐) (๐๐พ๐๐ฟ๐๐ ๐))
We can then combine these components. The semantics of a ๐-abstraction is simply the identity function: the struc-ture of the functional case in the de๏ฌnition of the model matches precisely the shape expected in a ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐. Be-cause the environment carries model values, the variable case is trivial. We obtain a normaliser by kickstarting the evaluation with a dummy environment of re๏ฌected variables.
๐ญ๐๐๐๐บ๐ ๐๐๐พโถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐ ๐ช๐ ๐ช๐ ๐ญ๐๐๐๐บ๐ ๐๐๐พ=๐๐พ๐ผ๐๐๐ฝ {๐๐=๐๐๐ช๐;โฆ๐๐บ๐โง=๐๐ฝ;_โฆ$โง_=_$$_;โฆ๐โง=๐๐ฝ ;โฆโจโฉโง=โจโฉ;โฆ๐๐โง=โต๐๐;โฆ๐ฟ๐ฟโง=โต๐ฟ ๐ฟ;โฆ๐๐ฟโง=๐๐ฟ } ๐๐ป๐พโถ[(๐ค โ๐ค๐๐)๐ช๐ โฬ (๐ค โ๐ข๐๐๐)๐ช๐ ] ๐๐ป๐พ๐ ๐ก=๐ ๐พ๐ ๐๐๐พ๐๐ค๐๐บ๐ ๐ญ๐๐๐๐บ๐ ๐๐๐พ๐๐๐๐พ๐๐ ๐ก ๐๐๐๐โถ (๐โถ๐ณ๐) โ[ ๐ณ๐๐ โ ๐ญ๐ฟฬ ๐] ๐๐๐๐๐ ๐ก=๐๐พ๐๐ฟ๐๐(๐๐ป๐พ(๐๐บ๐ผ๐(๐๐พ๐ฟ๐ ๐พ๐ผ๐_โโต๐๐บ๐))๐ก)
7.2 Normalisation by Evaluation for ฮฒฮนฮพ
As seen above, the traditional typed model construction leads to an NBE procedure outputting ฮฒฮน-normal ฮท-long terms. However actual proof systems rely on evaluation strategies that avoid applying ฮท-rules as much as possible: unsurpris-ingly, it is a rather bad idea to ฮท-expand proof terms which are already large when typechecking complex developments. In these systems, normal forms are neither long nor ฮท-short: the ฮท-rule is never deployed except when comparing a neutral and a constructor-headed term for equality. Instead of declaring them distinct, the algorithm does one step of ฮท-expansion on the neutral term and compares their subterms structurally. The conversion test fails only when confronted
with neutral terms with distinct head variables or normal forms with di๏ฌerent head constructors.
To reproduce this behaviour, NBE must be amended. It is possible to alter the model de๏ฌnition described earlier so that it avoids unnecessary ฮท-expansions. We proceed by en-riching the traditional model with extra syntactical artefacts in a manner reminiscent of Coquand and Dybjerโs (1997) ap-proach to de๏ฌning an NBE procedure for the SK combinator calculus. Their resorting to glueing terms to elements of the model was dictated by the sheer impossibily to write a sen-sible rei๏ฌcation procedure but, in hindsight, it provides us with a powerful technique to build models internalizing al-ternative equational theories.
This leads us to using a predicate๐ฑallowing embedding of neutrals into normal forms at all types and mutually de๏ฌn-ing the model (๐ช๐) together with the acting model (๐ฆ๐):
๐ช๐โถ๐ฌ๐๐ฝ๐พ๐ ๐ช๐๐=๐ญ๐พ๐โ ๐ฆ๐ฬ ๐ ๐ฆ๐โถ๐ฌ๐๐ฝ๐พ๐ ๐ฆ๐โต๐ฃ =๐ผ๐๐๐๐ โค ๐ฆ๐โต๐ค =๐ผ๐๐๐๐ ๐ก๐๐๐ ๐ฆ๐(๐โตโ๐) =โก(๐ช๐๐ โ ๐ช๐ฬ ๐)
Most combinators acting on this model follow a pattern similar to their counterpartโs in the previous section. Seman-tic application is more interesting: in case the function is a stuck term, we grow its spine by reifying its argument; otherwise we have an Agda function ready to be applied. We proceed similarly for the de๏ฌnition of the semantical โifโ (omitted here). Altogether, we get another normaliser which is, this time, not producing ฮท-long normal forms.
_$$_โถ[ ๐ช๐(๐โตโ๐) โ ๐ช๐ฬ ๐ โ ๐ช๐ฬ ๐] (๐๐๐๐ฃ๐๐) $$๐ข=๐๐๐๐ฃ(๐๐โต$๐๐พ๐๐ฟ๐_๐ข)
(๐๐๐๐ค๐น) $$๐ข=๐น ๐๐พ๐ฟ ๐ ๐ข
7.3 Normalisation by Evaluation for ฮฒฮน
The decision to apply the ฮท-rule lazily can be pushed even further: one may forgo using the ฮพ-rule too and simply per-form weak-head normalisation. This drives computation only when absolutely necessary, e.g. when two terms compared for equality have matching head constructors and one needs to inspect these constructorsโ arguments to conclude.
The model construction is much like the previous one except that source terms are now stored in the model too. This means that from an element of the model, one can pick either the reduced version of the input term (i.e. a stuck term or the termโs computational content) or the original. We exploit this ability most notably in rei๏ฌcation where once we have obtained either a head constructor or a head variable, no subterms need be evaluated.
๐ช๐โถ๐ฌ๐๐ฝ๐พ๐ ๐ช๐๐ =๐ณ๐๐รฬ (๐ถ๐๐๐พ๐โ ๐ฆ๐ฬ ๐) ๐ฆ๐โถ๐ฌ๐๐ฝ๐พ๐ ๐ฆ๐โต๐ฃ =๐ผ๐๐๐๐ โค ๐ฆ๐โต๐ค =๐ผ๐๐๐๐ ๐ก๐๐๐ ๐ฆ๐(๐โตโ๐) =โก(๐ช๐๐ โ ๐ช๐ฬ ๐)
8.
CPS Transformation
In their generic account of continuation passing styles, Hat-cli๏ฌ and Danvy (1994) decompose both call by name and call by value CPS transformations in two phases. The ๏ฌrst one, an embedding of the source language into Moggiโs Meta Language (1991), picks an evaluation strategy whilst the sec-ond one is a generic erasure from Moggiโs ML back to the original language. Looking closely at the structure of the ๏ฌrst pass, we can see that it is an instance of our Seman-tics framework. Let us start with the de๏ฌnition of Moggiโs Meta Language. Its types are fairly straightforward, we sim-ply have an extra constructor#_ for computations and the arrow has been turned into a computational arrow meaning that its codomain is considered to be a computational type:
๐ฝ๐บ๐๐บ๐ข๐ณ๐โถ๐ฒ๐พ๐๐๐๐พ๐๐พ
โต๐ฃโต๐ค โถ๐ข๐ณ๐
_โตโ#_ โถ๐ข๐ณ๐โ๐ข๐ณ๐โ๐ข๐ณ๐
#_ โถ๐ข๐ณ๐โ๐ข๐ณ๐
Then comes the Meta-Language itself. It incorporates๐ณ๐
constructors and eliminators with slightly di๏ฌerent types: value constructors are associated to value types whilst elim-inators (and their branches) have computational types. Two new term constructors have been added: โต๐๐พ๐ and _โต>>=_
make#_a monad. They can be used to explicitly schedule the evaluation order of various subterms.
๐ฝ๐บ๐๐บ๐ฌ๐ โถ๐ข๐ณ๐โ๐ข๐ ๐ข๐ณ๐โ๐ฒ๐พ๐๐๐๐พ๐๐พ โต๐๐บ๐ โถ [ ๐ต๐บ๐๐ โ ๐ฌ๐ ฬ ๐ ] _โต$_ โถ [ ๐ฌ๐ (๐โตโ#๐) โ ๐ฌ๐ ฬ ๐ โ ๐ฌ๐ ฬ (#๐) ] โตโจโฉ โถ [ ๐ฌ๐ โต๐ฃ ] โต๐๐โต๐ฟ๐ฟ โถ [ ๐ฌ๐ โต๐ค ] โต๐๐พ๐ โถ [ ๐ฌ๐ ๐ โ ๐ฌ๐ ฬ (#๐) ] _โต>>=_ โถ [ ๐ฌ๐ (#๐) โ ๐ฌ๐ ฬ (๐โตโ#๐) โ ๐ฌ๐ ฬ (#๐) ] โต๐ โถ [๐โข ๐ฌ๐ (#๐) โ ๐ฌ๐ ฬ (๐โตโ#๐) ] โต๐๐ฟ โถ[ ๐ฌ๐ โต๐ค โ ๐ฌ๐ ฬ (#๐) โ ๐ฌ๐ ฬ (#๐) โ ๐ฌ๐ ฬ (#๐)]
As explained in Hatcli๏ฌ and Danvyโs paper, the transla-tion from๐ณ๐ to๐ข๐ณ๐ ๏ฌxes the calling convention the CPS translation will have. Both call by name (๐ข๐ก๐ต) and call by value (๐ข๐ก๐ต) can be encoded. They behave the same way on base types (and we group the corresponding equations under the ๐ข๐ก๐ท name) but di๏ฌer in case of the function space. In๐ข๐ก๐ญthe argument of a function is a computation whilst it is expected to have been fully evaluated in๐ข๐ก๐ต.
๐ข๐ก๐ทโถ๐ณ๐โ๐ข๐ณ๐ ๐ข๐ก๐ทโต๐ฃ =โต๐ฃ
๐ข๐ก๐ทโต๐ค =โต๐ค
๐ข๐ก๐ญ(๐โตโ๐) = (# ๐ข๐ก๐ญ๐) โตโ#๐ข๐ก๐ญ๐
๐ข๐ก๐ต(๐โตโ๐) = ๐ข๐ก๐ต๐ โตโ#๐ข๐ก๐ต๐
From these translations, we can described the respective interpretations of variables and terms for the two CPS trans-formations. In both cases the return type of the compiled term is a computational type: the source term is a simple
๐ณ๐ and as such can contain redexes. Variables then play di๏ฌerent roles: in the by name strategy, they are all compu-tations whereas in the by value one they are expected to be evaluated already. This leads to the following de๏ฌnitions:
๐ต๐บ๐๐ญ ๐ ๐ค =๐ต๐บ๐ (#๐ข๐ก๐ญ๐) (๐๐บ๐๐ข๐(#_โ ๐ข๐ก๐ญ)๐ค)
๐ฌ๐ ๐ญ ๐ ๐ค =๐ฌ๐ (#๐ข๐ก๐ญ๐) (๐๐บ๐๐ข๐(#_โ ๐ข๐ก๐ญ)๐ค)
๐ต๐บ๐๐ต ๐ ๐ค =๐ต๐บ๐ (๐ข๐ก๐ต๐) (๐๐บ๐๐ข๐๐ข๐ก๐ต๐ค)
๐ฌ๐ ๐ต ๐ ๐ค =๐ฌ๐ (#๐ข๐ก๐ต๐) (๐๐บ๐๐ข๐๐ข๐ก๐ต๐ค)
Finally, the corresponding๐ฒ๐พ๐๐บ๐๐๐๐ผ๐can be de๏ฌned (code omitted here) and we get the two CPS transformations by creating dummy environments to kickstart the evaluation:
๐ข๐ฏ๐ฒ๐ญโถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐ ๐ต๐บ๐๐ญ๐ฌ๐ ๐ญ ๐ข๐ฏ๐ฒ๐ตโถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐ ๐ต๐บ๐๐ต๐ฌ๐ ๐ต ๐ผ๐๐๐ญโถ[ ๐ณ๐๐ โ ๐ฌ๐ ฬ ๐ญ๐] ๐ผ๐๐๐ญ=๐ ๐พ๐ ๐๐๐พ๐๐ค๐๐บ๐ ๐ข๐ฏ๐ฒ๐ญ๐๐๐๐พ๐ ๐ฝ๐๐๐๐ ๐๐๐พ๐๐พ๐ฝ๐๐๐๐=๐๐บ๐ผ๐(๐๐บ๐๐ต๐บ๐(#_โ ๐ข๐ก๐ญ)) ๐ผ๐๐๐ตโถ[ ๐ณ๐๐ โ ๐ฌ๐ ฬ ๐ต๐] ๐ผ๐๐๐ต=๐ ๐พ๐ ๐๐๐พ๐๐ค๐๐บ๐ ๐ข๐ฏ๐ฒ๐ต๐๐๐๐พ๐ ๐ฝ๐๐๐๐ ๐๐๐พ๐๐พ๐ฝ๐๐๐๐=๐๐บ๐ผ๐(๐๐บ๐๐ต๐บ๐๐ข๐ก๐ต)
9. Proving Properties of Semantics
Thanks to๐ฒ๐พ๐๐บ๐๐๐๐ผ๐, we have already saved work by not re-iterating the same traversals. Moreover, this disciplined ap-proach to building models and de๏ฌning the associated evalu-ation functions can help us refactor the proofs of some prop-erties of these semantics.
Instead of using proof scripts as Benton et al. (2012) do, we describe abstractly the constraints the logical rela-tions (Reynolds 1983) de๏ฌned on computarela-tions (and envi-ronment values) have to respect to ensure that evaluating a term in related environments produces related outputs. This gives us a generic framework to state and prove, in one go, properties about all of these semantics.
Our ๏ฌrst example of such a framework will stay simple on purpose. However it is no mere bureaucracy: the result proven here will actually be useful in the next section when considering more complex properties.
9.1 The Simulation Relation
This ๏ฌrst example is describing the relational interpretation of the terms. It should give the reader a good introduction to the setup before we take on more complexity. The types involved might look a bit scarily abstract but the idea is rather simple: we have a๐ฒ๐๐๐๐ ๐บ๐๐๐๐between two๐ฒ๐พ๐๐บ๐๐๐๐ผ๐
when evaluating a term in related environments yields related values. The bulk of the work is to make this intuition formal. The evidence that we have a ๐ฒ๐๐๐๐ ๐บ๐๐๐๐ between two
๐ฒ๐พ๐๐บ๐๐๐๐ผ๐is packaged in a record indexed by the semantics as well as two relations. We call๐ฑ๐ฌ๐๐ฝ๐พ๐ (for Relational Model) the type of these relations; the ๏ฌrst one (๐ฑ๐ ) relates values in the respective environments and the second one (๐๐ ) de-scribes simulation for computations.
๐๐พ๐ผ๐๐๐ฝ๐ฒ๐๐๐๐ ๐บ๐๐๐๐
(๐ฎ๐ โถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฑ๐ ๐๐ ) (๐ฎ๐กโถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฑ๐ก๐๐ก)
(๐ฑ๐ฑ โถ๐ฑ๐ฌ๐๐ฝ๐พ๐ ๐ฑ๐ ๐ฑ๐ก) (๐๐ฑ โถ๐ฑ๐ฌ๐๐ฝ๐พ๐ ๐๐ ๐๐ก) โถ๐ฒ๐พ๐๐๐๐พ๐๐พ
The recordโs ๏ฌelds say what structure these relations need to have. ๐ฑ๐ฑ๐๐ states that two similar environments can be
thinned whilst staying in simulation. It is stated using the
โตโ[_]predicate transformer (omitted here) which lifts ๐ฑ๐ to
contexts in a pointwise manner.
๐ฑ๐ฑ๐๐ โถ โตโ[๐ฑ๐ฑ]๐๐ ๐๐กโ
โตโ[๐ฑ
๐ฑ](๐๐[ ๐ฎ๐ .๐๐ ]๐๐๐ ๐๐ ) (๐๐[ ๐ฎ๐ก.๐๐ ]๐๐๐ ๐๐ก) We then have the relational counterparts of the term con-structors. To lighten the presentation we introduce๐ก, which states that the evaluation of a term in distinct contexts yields related computations. And we focus on the most interesting combinators, giving only one characteristic example of the remaining ones.
๐กโถ๐ณ๐๐ ๐คโ (๐ค โ๐ค๐๐)๐ฑ๐ ๐ฅโ (๐ค โ๐ค๐๐)๐ฑ๐ก๐ฅโ๐ฒ๐พ๐
๐ก๐ก ๐๐ ๐๐ก=๐๐๐๐ฝ๐พ๐ ๐๐ฑ(๐๐พ๐๐ ๐๐ ๐ก) (๐๐พ๐๐ก๐๐ก๐ก)
Our ๏ฌrst interesting case is the relational counterpart of
โต๐๐บ๐: a variable evaluated in two related environments yields
related computations. In other wordsโฆ๐๐บ๐โงturns related val-ues in related computations.
๐ฑโฆ๐๐บ๐โง โถ โตโ[๐ฑ๐ฑ]๐๐ ๐๐กโ๐ก(โต๐๐บ๐๐ฃ)๐๐ ๐๐ก
The second, and probably most interesting case, is the relational counterpart to theโฆ๐โงcombinator. The ability to evaluate the body of aโต๐in thinned environments, each ex-tended by related values, and deliver similar values is enough to guarantee that evaluating the ๐s in the original environ-ments will produce similar values.
๐ฑโฆ๐โงโถ (๐โถ โ๐๐๐โ๐๐๐๐ฝ๐พ๐ ๐ฑ๐ฑ๐ข๐ ๐ข๐กโ ๐ ๐พ๐ ๐๐ โฒ=๐๐[ ๐ฎ๐ .๐๐ ]๐๐๐ ๐๐ โตโ๐ข๐
๐๐กโฒ=๐๐[ ๐ฎ๐ก.๐๐ ]๐๐๐ ๐๐กโตโ๐ข๐ก ๐๐๐ก๐ ๐๐ โฒ๐๐กโฒ) โ
โตโ[๐ฑ๐ฑ]๐๐ ๐๐กโ๐ก(โต๐๐)๐๐ ๐๐ก
All the remaining cases follow suit: assuming that the evaluation of subterms produces related computations and that the current environments are related, we conclude that the evaluation of the whole term should yield related compu-tations. We show here the relational counterpart of the appli-cation constructor and omit the remaining ones:
๐ฑโฆ$โง โถ ๐ก๐ ๐๐ ๐๐กโ๐ก๐ก ๐๐ ๐๐กโ
โตโ[๐ฑ๐ฑ]๐๐ ๐๐กโ๐ก(๐ โต$๐ก)๐๐ ๐๐ก
This speci๏ฌcation is only useful if some semantics satisfy it and if given that these constraints are satis๏ฌed we can prove the fundamental lemma of simulations stating that the evaluation of a term on related inputs yields related output.
Theorem 1 (Fundamental Lemma of Simulations). Given
two Semantics ๐ฎ๐ด and ๐ฎ๐ต in simulation with respect to relations ๐ฑ๐ for values and ๐๐ for computations, we have: For any term ๐ก and environments ๐๐ด and ๐๐ต, if the two environments are ๐ฑ๐ -related in a pointwise manner then the semantics associated to ๐ก by ๐ฎ๐ด using ๐๐ด is ๐๐ -related to the one associated to ๐ก by ๐ฎ๐ต using ๐๐ต.
Proof. The proof is a structural induction on ๐ก like the one used to de๏ฌne๐๐พ๐. It uses the combinators provided by the constraint that ๐ฎ๐ด and ๐ฎ๐ต are in simulation to make use of the induction hypotheses.
Corollary 1 (Renaming is a Substitution). Applying a
re-naming ๐ to a term ๐ก amounts to applying the substitution
๐๐บ๐๐ค๐๐โต๐๐บ๐๐ to that same term ๐ก.
Proof. This is shown by instantiating the fundamental lemma of simulations for the special case where: ๐ฎ๐ด is๐ฑ๐พ๐๐บ๐๐๐๐, ๐ฎ๐ต is๐ฒ๐๐ป๐๐๐๐๐๐๐๐๐, ๐ฑ๐ ๐ฃ ๐ก isโต๐๐บ๐๐ฃโก๐ก (in other words: the terms in the substitution are precisely the variables in the renaming), and ๐๐ is propositional equality.
The constraints corresponding to the various combinators are mundane: propositional equality is a congruence.
Another corollary of the simulation lemma relates NBE to itself. This may seem bureaucratic but it is crucial: the model de๏ฌnition๐ช๐uses the host languageโs function space which contains more functions than simply the ones obtained by evaluating a ๐-term. These exotic functions have undesirable behaviours and need to be ruled out to ensure that normali-sation has good properties. This is done by de๏ฌning a Partial Equivalence Relation (Mitchell 1996) (PER) on the model: the elements equal to themselves will be guaranteed to be well behaved. We show that given an environment of values PER-related to themselves, the evaluation of a ๐-term pro-duces a computation equal to itself too.
We start by de๏ฌning the PER for the model. It is con-structed by induction on the type and ensures that terms which behave the same extensionally are declared equal. Two values of type โต๐ฃ are always trivially equal; values of type โต๐ค are normal forms and are declared equal when they are e๏ฌectively syntactically the same; ๏ฌnally func-tions are equal whenever equal inputs map to equal outputs.
๐ฏ๐ค๐ฑโถ (๐โถ๐ณ๐) โ[ ๐ช๐๐ โ ๐ช๐ฬ ๐ โ ๐ผ๐๐๐๐ ๐ฒ๐พ๐ ]ฬ ๐ฏ๐ค๐ฑโต๐ฃ ๐ ๐=โค
๐ฏ๐ค๐ฑโต๐ค ๐ ๐=๐ โก๐
๐ฏ๐ค๐ฑ(๐โตโ๐) ๐ ๐= โ๐๐๐โ๐ฏ๐ค๐ฑ๐ ๐ ๐ โ ๐ฏ๐ค๐ฑ๐(๐ ๐๐๐ ๐) (๐ ๐๐๐ ๐)
It is indeed a PER as witnessed by the (omitted here) proofs that๐ฏ๐ค๐ฑ๐ is symmetric and transitive. It also respects the notion of thinning de๏ฌned for๐ช๐.
๐๐๐บ๐๐๐ฏ๐ค๐ฑโถ๐ฏ๐ค๐ฑ๐ ๐ ๐ โ๐ฏ๐ค๐ฑ๐ ๐ ๐ โ๐ฏ๐ค๐ฑ๐ ๐ ๐
๐๐๐ฏ๐ค๐ฑโถ ๐ฏ๐ค๐ฑ๐ ๐ ๐โ๐ฏ๐ค๐ฑ๐(๐๐๐ช๐๐ ๐๐๐ ๐) (๐๐๐ช๐๐ ๐๐๐ ๐)
The interplay of re๏ฌect and reify with this notion of equality has to be described in one go because of their mutual de๏ฌnition. It con๏ฌrms that ๐ฏ๐ค๐ฑ is an appropriate notion of semantic equality:๐ฏ๐ค๐ฑ-related values are rei๏ฌed to propositionally equal normal forms whilst proposition-ally equal neutral terms are re๏ฌected to๐ฏ๐ค๐ฑ-related values.
๐๐พ๐๐ฟ๐๐ฏ๐ค๐ฑ โถ ๐ฏ๐ค๐ฑ๐ ๐ ๐โ๐๐พ๐๐ฟ๐๐ ๐ โก ๐๐พ๐๐ฟ ๐๐ ๐
๐๐พ๐ฟ๐ ๐พ๐ผ๐๐ฏ๐ค๐ฑ โถ ๐กโก๐ขโ๐ฏ๐ค๐ฑ๐(๐๐พ๐ฟ๐ ๐พ๐ผ๐๐ ๐ก) (๐๐พ๐ฟ๐ ๐พ๐ผ๐๐ ๐ข)
That su๏ฌces to show that evaluating a term in two envi-ronments related pointwise by๐ฏ๐ค๐ฑyields two semantic ob-jects themselves related by๐ฏ๐ค๐ฑ.
Corollary 2 (No exotic values). The evaluation of a term ๐ก
in an environment of values equal to themselves according to
๐ฏ๐ค๐ฑyields a value equal to itself according to๐ฏ๐ค๐ฑ
Proof. By the fundamental lemma of simulations with ๐ฎ๐ด and ๐ฎ๐ต equal to๐ญ๐๐๐๐บ๐ ๐๐๐พ, ๐ฑ๐ and ๐๐ to๐ฏ๐ค๐ฑ.
We can move on to the more complex example of a proof framework built generically over our notion of๐ฒ๐พ๐๐บ๐๐๐๐ผ๐
9.2 Fusions of Evaluations
When studying the meta-theory of a calculus, one systemat-ically needs to prove fusion lemmas for various semantics. For instance, Benton et al. (2012) prove six such lemmas re-lating renaming, substitution and a typeful semantics embed-ding their calculus into Coq. This observation naturally led us to de๏ฌning a fusion framework describing how to relate three semantics: the pair we sequence and their sequential composition. The fundamental lemma we prove can then be instantiated six times to derive the corresponding corollaries. The evidence that ๐ฎ๐ด, ๐ฎ๐ต and ๐ฎ๐ถ are such that ๐ฎ๐ด followed by ๐ฎ๐ต is equivalent to ๐ฎ๐ถ (e.g.๐ฒ๐๐ป๐๐๐๐๐๐๐๐๐ fol-lowed by๐ฑ๐พ๐๐บ๐๐๐๐can be reduced to๐ฒ๐๐ป๐๐๐๐๐๐๐๐๐) is packed in a record๐ฅ๐๐๐บ๐ป๐ ๐พindexed by the three semantics but also three relations. The ๏ฌrst one (๐ฑ๐ ๐ต๐ถ) states what it means
for two environment values of ๐ฎ๐ต and ๐ฎ๐ถ respectively to be related. The second one (๐ฑ๐ ) characterises the triples of environments (one for each one of the semantics) which are compatible. The last one (๐๐ ) relates values in ๐ฎ๐ต and ๐ฎ๐ถโs models. ๐๐พ๐ผ๐๐๐ฝ๐ฅ๐๐๐บ๐ป๐ ๐พ(๐ฎ๐ โถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฑ๐ ๐๐ ) (๐ฎ๐กโถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฑ๐ก๐๐ก) (๐ฎ๐ขโถ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฑ๐ข๐๐ข) (๐ฑ๐ฑ๐ก๐ขโถ๐ฑ๐ฌ๐๐ฝ๐พ๐ ๐ฑ๐ก๐ฑ๐ข) (๐ฑ๐ฑโถ (๐ค โ๐ค๐๐)๐ฑ๐ ๐ฅโ (๐ฅโ๐ค๐๐)๐ฑ๐ก๐ฉโ (๐ค โ๐ค๐๐)๐ฑ๐ข๐ฉโ๐ฒ๐พ๐) (๐๐ฑโถ๐ฑ๐ฌ๐๐ฝ๐พ๐ ๐๐ก๐๐ข) โถ๐ฒ๐พ๐๐๐๐พ๐๐พ
As before, most of the ๏ฌelds of this record describe what structure these relations need to have. However, we start with
something slightly di๏ฌerent: given that we are planing to run the๐ฒ๐พ๐๐บ๐๐๐๐ผ๐๐ฎ๐ต after having run ๐ฎ๐ด, we need two compo-nents: a way to extract a term from an ๐ฎ๐ด and a way to man-ufacture a dummy ๐ฎ๐ด value when going under a binder. Our ๏ฌrst two ๏ฌelds are therefore:
๐๐พ๐๐ฟ ๐๐ โถ[ ๐๐ ๐ โ ๐ณ๐ฬ ๐ ]
๐๐บ๐๐ ๐ข โถ[ ๐โข๐ฑ๐ ๐ ]
Then come two constraints dealing with the relations talk-ing about evaluation environments.๐ฑ๐ฑโtells us how to
ex-tend related environments: one should be able to push related values onto the environments for ๐ฎ๐ต and ๐ฎ๐ถ whilst merely extending the one for ๐ฎ๐ด with the token value๐๐บ๐๐ข๐ .
๐ฑ๐ฑ๐๐guarantees that it is always possible to thin the en-vironments for ๐ฎ๐ต and ๐ฎ๐ถ in a ๐ฑ๐ preserving manner.
๐ฑ๐ฑโ โถ ๐ฑ๐ฑ๐๐ ๐๐ก๐๐ขโ๐๐๐๐ฝ๐พ๐ ๐ฑ๐ฑ๐ก๐ข๐ข๐ก๐ข๐ขโ ๐ ๐พ๐๐๐ โฒ=๐๐[ ๐ฎ๐ .๐๐ ](๐๐๐พ๐ ๐๐พ๐ฟ๐ )๐๐ โตโ๐๐บ๐๐ ๐ข
๐๐๐ฑ๐ฑ๐๐ โฒ(๐๐กโตโ๐ข๐ก) (๐๐ขโตโ๐ข๐ข)
๐ฑ๐ฑ๐๐ โถ ๐ฑ๐ฑ๐๐ ๐๐ก๐๐ขโ
๐ฑ๐ฑ๐๐ (๐๐[ ๐ฎ๐ก.๐๐ ]๐๐๐ ๐๐ก) (๐๐[ ๐ฎ๐ข.๐๐ ]๐๐๐ ๐๐ข)
Then we have the relational counterpart of the vari-ous term constructors. We can once more introduce an ex-tra de๏ฌnition ๐ก which will make the type of the com-binators de๏ฌned later on clearer. ๐ก relates a term and three environments by stating that the computation one gets by sequentially evaluating the term in the ๏ฌrst and then the second environment is related to the one obtained by directly evaluating the term in the third environment.
๐ก๐ก ๐๐ ๐๐ก๐๐ข=๐๐๐๐ฝ๐พ๐ ๐๐ฑ (๐๐พ๐๐ก๐๐ก(๐๐พ๐๐ฟ ๐๐ (๐๐พ๐๐ ๐๐ ๐ก)))
(๐๐พ๐๐ข๐๐ข๐ก)
As with the previous section, only a handful of these com-binators are out of the ordinary. We will start with theโต๐๐บ๐
case. It states that fusion indeed happens when evaluating a variable using related environments.
๐ฑโฆ๐๐บ๐โง โถ โ๐ฃโ๐ฑ๐ฑ๐๐ ๐๐ก๐๐ขโ๐ก(โต๐๐บ๐๐ฃ)๐๐ ๐๐ก๐๐ข
The โต๐-case puts some rather strong restrictions on the
way the ๐-abstractionโs body may be used by ๐ฎ๐ด: we assume it is evaluated in an environment thinned by one variable and extended using๐๐บ๐๐ข๐ . But it is quite natural to have these
restrictions: given that๐๐พ๐๐ฟ ๐๐ quotes the result back, we are expecting this type of evaluation in an extended context (i.e. under one lambda). And it turns out that this is indeed enough for all of our examples. The evaluation environments used by the semantics ๐ฎ๐ต and ๐ฎ๐ถ on the other hand can be arbitrarily thinned before being extended with related values to be substituted for the variable bound by theโต๐.
๐ฑโฆ๐โง โถ (๐กโถ๐ณ๐๐(๐ค โ๐))
( โ๐๐๐โ๐๐๐๐ฝ๐พ๐ ๐ฑ๐ฑ๐ก๐ข๐ข๐ก๐ข๐ขโ
๐๐กโฒ= ๐๐[ ๐ฎ๐ก.๐๐ ]๐๐๐ ๐๐กโตโ๐ข๐ก ๐๐ขโฒ= ๐๐[ ๐ฎ๐ข.๐๐ ]๐๐๐ ๐๐ขโตโ๐ข๐ข ๐๐๐ก๐ก ๐๐ โฒ๐๐กโฒ๐๐ขโฒ) โ
๐ฑ๐ฑ๐๐ ๐๐ก๐๐ขโ๐ก(โต๐๐ก)๐๐ ๐๐ก๐๐ข
The other cases (omitted here) are just stating that, given the expected induction hypotheses, and the assumption that the three environments are ๐ฑ๐ -related we can deliver a proof that fusion can happen on the compound expression.
As with simulation, we measure the utility of this frame-work by the way we can prove its fundamental lemma and then obtain useful corollaries. Once again, having carefully identi๏ฌed what the constraints should be, proving the funda-mental lemma is not a problem:
Theorem 2 (Fundamental Lemma of Fusable Semantics).
Given three Semantics ๐ฎ๐ด, ๐ฎ๐ต and ๐ฎ๐ถ which are fusable with respect to the relations ๐ฑ๐ ๐ก๐ขfor values of ๐ฎ๐ต and ๐ฎ๐ถ, ๐ฑ๐ for environemnts and ๐๐ for computations, we have that: For any term ๐ก and environments ๐๐ด, ๐๐ต, and ๐๐ถ, if the three environments are ๐ฑ๐ -related then the semantics associated to ๐ก by ๐ฎ๐ด using ๐๐ด followed by ๐ฎ๐ต using ๐๐ต is ๐๐ -related to the one associated to ๐ก by ๐ฎ๐ถ using ๐๐ถ. Proof. The proof is by structural induction on ๐ก using the combinators to assemble the induction hypotheses.
The Special Case of Syntactic Semantics The translation from๐ฒ๐๐๐๐บ๐ผ๐๐๐ผto๐ฒ๐พ๐๐บ๐๐๐๐ผ๐uses a lot of constructors as their own semantic counterpart, it is hence possible to generate evidence of๐ฒ๐๐๐๐บ๐ผ๐๐๐ผtriplets being fusable with much fewer assumptions. We isolate them and prove the result gener-ically to avoid repetition. A ๐ฒ๐๐๐๐บ๐ผ๐๐๐ผ๐ฅ๐๐๐บ๐ป๐ ๐พrecord packs the evidence for๐ฒ๐๐๐๐บ๐ผ๐๐๐ผsemantics ๐ ๐ฆ๐๐ด, ๐ ๐ฆ๐๐ต and ๐ ๐ฆ๐๐ถ. It is indexed by these three๐ฒ๐๐๐๐บ๐ผ๐๐๐ผs as well as two rela-tions corresponding to the ๐ฑ๐ ๐ต๐ถand ๐ฑ๐ ones of the๐ฅ๐๐๐บ๐ป๐ ๐พ
framework. It contains the same๐ฑ๐ฑโ,๐ฑ๐ฑ๐๐and๐ฑโฆ๐๐บ๐โง๏ฌelds
as a๐ฅ๐๐๐บ๐ป๐ ๐พas well as a fourth one (๐๐บ๐๐ข๐ก๐ข) saying that ๐ ๐ฆ๐๐ต and ๐ ๐ฆ๐๐ถโs respective ๐๐บ๐๐ขs are producing related values.
๐๐บ๐๐ก๐ข๐ขโถ๐๐๐๐ฝ๐พ๐ ๐ฑ๐ฑ๐ก๐ข๐ฒ๐๐๐ก.๐๐บ๐๐ข๐ฒ๐๐๐ข.๐๐บ๐๐ข
Theorem 3 (Fundamental Lemma of Fusable Syntactics).
Given a๐ฒ๐๐๐๐บ๐ผ๐๐๐ผ๐ฅ๐๐๐บ๐ป๐ ๐พrelating three๐ฒ๐๐๐๐บ๐ผ๐๐๐ผsemantics, we get a๐ฅ๐๐๐บ๐ป๐ ๐พrelating the corresponding๐ฒ๐พ๐๐บ๐๐๐๐ผ๐where ๐๐ is the propositional equality.
Proof. The proof relies on the way the translation from
๐ฒ๐๐๐๐บ๐ผ๐๐๐ผto๐ฒ๐พ๐๐บ๐๐๐๐ผ๐is formulated in section 5.
Corollary 3 (Renaming-Renaming fusion). Given two
re-namings ๐ from ๐ค to ๐ฅ and ๐โฒfrom ๐ฅ to ๐ฉ and a term ๐ก of type ๐ with free variables in ๐ค , we have that:
๐๐๐ณ๐๐ ๐โฒ(๐๐๐ณ๐๐ ๐ ๐ก)โก ๐๐๐ณ๐๐(๐๐พ๐ ๐พ๐ผ๐๐ ๐โฒ)๐ก
Corollary 4 (Renaming-Substitution fusion). Given a
re-naming ๐ from ๐ค to ๐ฅ, a substitution ๐โฒfrom ๐ฅ to ๐ฉ and a term ๐ก of type ๐ with free variables in ๐ค , we have that:
๐๐๐ป๐๐๐โฒ(๐๐๐ณ๐๐ ๐ ๐ก)โก ๐๐๐ป๐๐(๐๐พ๐ ๐พ๐ผ๐๐ ๐โฒ)๐ก
Corollary 5 (Substitution-Renaming fusion). Given a
sub-stitution ๐ from ๐ค to ๐ฅ, a renaming ๐โฒ from ๐ฅ to ๐ฉ and a term ๐ก of type ๐ with free variables in ๐ค , we have that:
๐๐๐ณ๐๐ ๐โฒ(๐๐๐ป๐๐๐ ๐ก)โก ๐๐๐ป๐๐(๐๐บ๐๐ค๐๐(๐๐๐ณ๐_๐โฒ)๐)๐ก
Corollary 6 (Substitution-Substitution fusion). Given two
substitutitons, ๐ from ๐ค to ๐ฅ and ๐โฒ from ๐ฅ to ๐ฉ, and a term ๐ก of type ๐ with free variables in ๐ค , we have that:
๐๐๐ป๐๐๐โฒ(๐๐๐ป๐๐๐ ๐ก)โก ๐๐๐ป๐๐(๐๐บ๐๐ค๐๐(๐๐๐ป๐๐๐โฒ)๐)๐ก
These four lemmas are usually proven in painful separa-tion. Here we discharged them by rapid successive instantia-tion of our framework, using the earlier results to satisfy the later constraints. We are not limited to๐ฒ๐๐๐๐บ๐ผ๐๐๐ผstatements: Examples of Fusable Semantics The most simple example of๐ฅ๐๐๐บ๐ป๐ ๐พ ๐ฒ๐พ๐๐บ๐๐๐๐ผ๐involving a non๐ฒ๐๐๐๐บ๐ผ๐๐๐ผone is prob-ably the proof that๐ฑ๐พ๐๐บ๐๐๐๐followed by๐ญ๐๐๐๐บ๐ ๐๐๐พ๐ฝ๐๐๐is equivalent to NBE with an adjusted environment.
Corollary 7 (Renaming-Normalise fusion). Given a
renam-ing ๐ from ๐ค to ๐ฅ, an environment of values ๐โฒfrom ๐ฅ to ๐ฉ such that they are all equal to themselves in the๐ฏ๐ค๐ฑand a term ๐ก of type ๐ with free variables in ๐ค , we have that:
๐ฏ๐ค๐ฑ๐(๐๐ป๐พ๐โฒ(๐๐๐ณ๐๐ ๐ ๐ก)) (๐๐ป๐พ(๐๐พ๐ ๐พ๐ผ๐๐ ๐โฒ)๐ก)
Then, we use the framework to prove that to๐ญ๐๐๐๐บ๐ ๐๐๐พ๐ฝ๐๐๐
by Evaluation after a๐ฒ๐๐ป๐๐๐๐๐๐๐๐๐amounts to normalising the original term where the substitution has been evaluated ๏ฌrst. The constraints imposed on the environments might seem quite restrictive but they are actually similar to the Unifor-mity condition described by C. Coquand (2002) in her de-tailed account of NBE for a ST๐C with explicit substitution.
Corollary 8 (Substitution-Normalise fusion). Given a
sub-stitution ๐ from ๐ค to ๐ฅ, an environment of values ๐โฒfrom ๐ฅ to ๐ฉ such that all these values are equal to themselves and thinning and evaluation in ๐โฒcommute, and a term ๐ก of type ๐ with free variables in ๐ค , we have that:
๐ฏ๐ค๐ฑ๐(๐๐ป๐พ๐โฒ(๐๐๐ป๐๐๐ ๐ก)) (๐๐ป๐พ(๐๐บ๐๐ค๐๐(๐๐ป๐พ๐โฒ)๐)๐ก)
10. Future and Related Work
The programming part of this work can be replicated in Haskell and a translation of the de๏ฌnitions is available in the paperโs repository2. The subtleties of working with de-pendent types in Haskell (Lindley and McBride 2014) are outside the scope of this paper.