• No results found

Type-and-scope safe programs and their proofs

N/A
N/A
Protected

Academic year: 2021

Share "Type-and-scope safe programs and their proofs"

Copied!
14
0
0

Loading.... (view fulltext now)

Full text

(1)

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

(2)

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

(3)

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 ๐œ†.

๐–ฝ๐–บ๐—๐–บ๐–ณ๐—†โˆถ๐–ณ๐—’โ†’๐–ข๐—‘ ๐–ณ๐—’โ†’๐–ฒ๐–พ๐—๐—๐—๐–พ๐—‹๐–พ โ€ต๐—๐–บ๐—‹ โˆถ [ ๐–ต๐–บ๐—‹๐œŽ โ†’ฬ‡ ๐–ณ๐—†๐œŽ ] _โ€ต$_ โˆถ [ ๐–ณ๐—†(๐œŽโ€ตโ†’๐œ) โ†’ ๐–ณ๐—†ฬ‡ ๐œŽ โ†’ฬ‡ ๐–ณ๐—†๐œ ] โ€ต๐œ† โˆถ [๐œŽโŠข ๐–ณ๐—†๐œ โ†’ฬ‡ ๐–ณ๐—†(๐œŽโ€ตโ†’๐œ) ] โ€ตโŸจโŸฉ โˆถ [ ๐–ณ๐—†โ€ต๐Ÿฃ ] โ€ต๐—๐—โ€ต๐–ฟ๐–ฟ โˆถ [ ๐–ณ๐—†โ€ต๐Ÿค ] โ€ต๐—‚๐–ฟ โˆถ [ ๐–ณ๐—†โ€ต๐Ÿค โ†’ ๐–ณ๐—†ฬ‡ ๐œŽ โ†’ ๐–ณ๐—†ฬ‡ ๐œŽ โ†’ ๐–ณ๐—†ฬ‡ ๐œŽ ]

(4)

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

(5)

โ–กโˆถ (๐–ข๐—‘๐‘ก๐‘ฆโ†’๐–ฒ๐–พ๐—) โ†’ (๐–ข๐—‘๐‘ก๐‘ฆโ†’๐–ฒ๐–พ๐—) (โ–ก๐‘†)๐›ค = {๐›ฅโˆถ๐–ข๐—‘_} โ†’๐›ค โІ๐›ฅโ†’๐‘† ๐›ฅ

๐—๐—โ–กโˆถ๐–ณ๐—๐—‚๐—‡๐—‡๐–บ๐–ป๐—…๐–พ(โ–ก๐‘†)

๐—๐—โ–ก๐‘–๐‘›๐‘ ๐‘ =๐‘ โˆ˜ ๐—Œ๐–พ๐—…๐–พ๐–ผ๐—๐‘–๐‘›๐‘

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๐–ฒ๐–พ๐—†๐–บ๐—‡๐—๐—‚๐–ผ๐—Œ. ๐—‹๐–พ๐–ผ๐—ˆ๐—‹๐–ฝ๐–ฒ๐—’๐—‡๐—๐–บ๐–ผ๐—๐—‚๐–ผ(๐’ฑโˆถ๐–ฌ๐—ˆ๐–ฝ๐–พ๐—…) โˆถ๐–ฒ๐–พ๐—๐—๐—๐–พ๐—‹๐–พ

๐–ฟ ๐—‚๐–พ๐—…๐–ฝ ๐—๐— โˆถ (๐œŽโˆถ๐–ณ๐—’) โ†’๐–ณ๐—๐—‚๐—‡๐—‡๐–บ๐–ป๐—…๐–พ(๐’ฑ ๐œŽ)

๐—๐–บ๐—‹๐Ÿข โˆถ[ ๐œŽโŠข๐’ฑ ๐œŽ ]

(6)

๐—Œ๐—’๐—‡๐—๐–บ๐–ผ๐—๐—‚๐–ผโˆถ๐–ฒ๐—’๐—‡๐—๐–บ๐–ผ๐—๐—‚๐–ผ๐’ฑโ†’๐–ฒ๐–พ๐—†๐–บ๐—‡๐—๐—‚๐–ผ๐—Œ๐’ฑ๐–ณ๐—† ๐—Œ๐—’๐—‡๐—๐–บ๐–ผ๐—๐—‚๐–ผ๐‘ ๐‘ฆ๐‘›=๐—…๐–พ๐— ๐—ˆ๐—‰๐–พ๐—‡๐–ฒ๐—’๐—‡๐—๐–บ๐–ผ๐—๐—‚๐–ผ๐‘ ๐‘ฆ๐‘›๐—‚๐—‡ ๐—‹๐–พ๐–ผ๐—ˆ๐—‹๐–ฝ

{๐—๐— =๐—๐—;โŸฆ๐—๐–บ๐—‹โŸง =โŸฆ๐—๐–บ๐—‹โŸง

;โŸฆ๐œ†โŸง = ๐œ†๐‘กโ†’โ€ต๐œ†(๐‘ก(๐—Œ๐—๐–พ๐—‰ ๐—‹๐–พ๐–ฟ๐—…)๐—๐–บ๐—‹๐Ÿข) ;_โŸฆ$โŸง_=_โ€ต$_

;โŸฆโŸจโŸฉโŸง=โ€ตโŸจโŸฉ;โŸฆ๐—๐—โŸง=โ€ต๐—๐—;โŸฆ๐–ฟ๐–ฟโŸง=โ€ต๐–ฟ๐–ฟ;โŸฆ๐—‚๐–ฟโŸง =โ€ต๐—‚๐–ฟ}

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

(7)

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

(8)

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.

๐–ช๐—‹โˆถ๐–ฌ๐—ˆ๐–ฝ๐–พ๐—… ๐–ช๐—‹๐œŽ =๐–ณ๐—†๐œŽร—ฬ‡ (๐–ถ๐—๐—‡๐–พ๐œŽโŠŽ ๐–ฆ๐—ˆฬ‡ ๐œŽ) ๐–ฆ๐—ˆโˆถ๐–ฌ๐—ˆ๐–ฝ๐–พ๐—… ๐–ฆ๐—ˆโ€ต๐Ÿฃ =๐–ผ๐—ˆ๐—‡๐—Œ๐— โŠค ๐–ฆ๐—ˆโ€ต๐Ÿค =๐–ผ๐—ˆ๐—‡๐—Œ๐— ๐–ก๐—ˆ๐—ˆ๐—… ๐–ฆ๐—ˆ(๐œŽโ€ตโ†’๐œ) =โ–ก(๐–ช๐—‹๐œŽ โ†’ ๐–ช๐—‹ฬ‡ ๐œ)

(9)

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.

(10)

๐—‹๐–พ๐–ผ๐—ˆ๐—‹๐–ฝ๐–ฒ๐—‚๐—†๐—Ž๐—…๐–บ๐—๐—‚๐—ˆ๐—‡

(๐’ฎ๐– โˆถ๐–ฒ๐–พ๐—†๐–บ๐—‡๐—๐—‚๐–ผ๐—Œ๐’ฑ๐– ๐’ž๐– ) (๐’ฎ๐–กโˆถ๐–ฒ๐–พ๐—†๐–บ๐—‡๐—๐—‚๐–ผ๐—Œ๐’ฑ๐–ก๐’ž๐–ก)

(๐’ฑ๐–ฑ โˆถ๐–ฑ๐–ฌ๐—ˆ๐–ฝ๐–พ๐—…๐’ฑ๐– ๐’ฑ๐–ก) (๐’ž๐–ฑ โˆถ๐–ฑ๐–ฌ๐—ˆ๐–ฝ๐–พ๐—…๐’ž๐– ๐’ž๐–ก) โˆถ๐–ฒ๐–พ๐—๐—๐—๐–พ๐—‹๐–พ

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๐–ช๐—‹.

(11)

๐—๐—‹๐–บ๐—‡๐—Œ๐–ฏ๐–ค๐–ฑโˆถ๐–ฏ๐–ค๐–ฑ๐œŽ ๐‘† ๐‘‡ โ†’๐–ฏ๐–ค๐–ฑ๐œŽ ๐‘‡ ๐‘ˆ โ†’๐–ฏ๐–ค๐–ฑ๐œŽ ๐‘† ๐‘ˆ

๐—๐—๐–ฏ๐–ค๐–ฑโˆถ ๐–ฏ๐–ค๐–ฑ๐œŽ ๐‘‡ ๐‘ˆโ†’๐–ฏ๐–ค๐–ฑ๐œŽ(๐—๐—๐–ช๐—‹๐œŽ ๐‘–๐‘›๐‘ ๐‘‡) (๐—๐—๐–ช๐—‹๐œŽ ๐‘–๐‘›๐‘ ๐‘ˆ)

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โ€ต๐œ†.

๐–ฑโŸฆ๐œ†โŸง โˆถ (๐‘กโˆถ๐–ณ๐—†๐œ(๐›ค โˆ™๐œŽ))

( โˆ€๐‘–๐‘›๐‘โ†’๐—‹๐—†๐—ˆ๐–ฝ๐–พ๐—…๐’ฑ๐–ฑ๐–ก๐–ข๐‘ข๐–ก๐‘ข๐–ขโ†’

(12)

๐œŒ๐–กโ€ฒ= ๐—๐—[ ๐’ฎ๐–ก.๐—๐— ]๐‘–๐‘›๐‘ ๐œŒ๐–กโ€ตโˆ™๐‘ข๐–ก ๐œŒ๐–ขโ€ฒ= ๐—๐—[ ๐’ฎ๐–ข.๐—๐— ]๐‘–๐‘›๐‘ ๐œŒ๐–ขโ€ตโˆ™๐‘ข๐–ข ๐—‚๐—‡๐“ก๐‘ก ๐œŒ๐– โ€ฒ๐œŒ๐–กโ€ฒ๐œŒ๐–ขโ€ฒ) โ†’

๐’ฑ๐–ฑ๐œŒ๐– ๐œŒ๐–ก๐œŒ๐–ขโ†’๐“ก(โ€ต๐œ†๐‘ก)๐œŒ๐– ๐œŒ๐–ก๐œŒ๐–ข

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.

References

Related documents