Proof Pearl: Substitution Revisited, Again

Similar documents
Nominal Techniques in Coq

Formal Systems and their Applications

An experiment with variable binding, denotational semantics, and logical relations in Coq. Adam Chlipala University of California, Berkeley

Abstracting Syntax. ScholarlyCommons. University of Pennsylvania. Brian Aydemir University of Pennsylvania

3.7 Denotational Semantics

Lecture Notes on Data Representation

Inductive Definitions, continued

A Canonical 1 Locally Named Representation of Binding. α -equivalence is identity. Randy Pollack. Masahiko Sato. LFCS, University of Edinburgh

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

λ calculus Function application Untyped λ-calculus - Basic Idea Terms, Variables, Syntax β reduction Advanced Formal Methods

1.3. Conditional expressions To express case distinctions like

CITS3211 FUNCTIONAL PROGRAMMING

The Typed λ Calculus and Type Inferencing in ML

Exploiting Uniformity in Substitution: the Nuprl Term Model. Abhishek Anand

Analysis of dependent types in Coq through the deletion of the largest node of a binary search tree

The Formal Semantics of Programming Languages An Introduction. Glynn Winskel. The MIT Press Cambridge, Massachusetts London, England

Lambda Calculus. Type Systems, Lectures 3. Jevgeni Kabanov Tartu,

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Sections p.

Variables. Substitution

(Refer Slide Time: 4:00)

From Types to Sets in Isabelle/HOL

CS 4110 Programming Languages & Logics. Lecture 28 Recursive Types

Lambda Calculus. Lecture 4 CS /26/10

Type Systems Winter Semester 2006

HIGHER-ORDER ABSTRACT SYNTAX IN TYPE THEORY

Last class. CS Principles of Programming Languages. Introduction. Outline

Eta-equivalence in Core Dependent Haskell

Introduction to the Lambda Calculus

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS522 Programming Language Semantics

Programming Language Features. CMSC 330: Organization of Programming Languages. Turing Completeness. Turing Machine.

CMSC 330: Organization of Programming Languages

1 The language χ. Models of Computation

Introduction to Homotopy Type Theory

Rule Formats for Nominal Modal Transition Systems

c constructor P, Q terms used as propositions G, H hypotheses scope identifier for a notation scope M, module identifiers t, u arbitrary terms

Coq quick reference. Category Example Description. Inductive type with instances defined by constructors, including y of type Y. Inductive X : Univ :=

Lecture Notes on Real-world SMT

5. Introduction to the Lambda Calculus. Oscar Nierstrasz

VU Semantik von Programmiersprachen

CONVENTIONAL EXECUTABLE SEMANTICS. Grigore Rosu CS422 Programming Language Semantics

Substitution in Structural Operational Semantics and value-passing process calculi

Pure Lambda Calculus. Lecture 17

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Lecture Notes on Static and Dynamic Semantics

Nested Abstract Syntax in Coq

Functional programming languages

Lecture 13 CIS 341: COMPILERS

Importing HOL-Light into Coq

Computing Fundamentals 2 Introduction to CafeOBJ

Lecture 2: SML Basics

Calculus of Inductive Constructions

From natural numbers to the lambda calculus

INCREMENTAL SOFTWARE CONSTRUCTION WITH REFINEMENT DIAGRAMS

Proving OCaml s type system? Jacques Garrigue Nagoya University

Type Systems COMP 311 Rice University Houston, Texas

Introduction to the Lambda Calculus. Chris Lomont

Beluga: A Framework for Programming and Reasoning with Deductive Systems (System Description)

Dependent Object Types - A foundation for Scala s type system

Handout 9: Imperative Programs and State

CS 6110 S14 Lecture 1 Introduction 24 January 2014

CS422 - Programming Language Design

Introduction to Lambda Calculus. Lecture 7 CS /08/09

axiomatic semantics involving logical rules for deriving relations between preconditions and postconditions.

HOL DEFINING HIGHER ORDER LOGIC LAST TIME ON HOL CONTENT. Slide 3. Slide 1. Slide 4. Slide 2 WHAT IS HIGHER ORDER LOGIC? 2 LAST TIME ON HOL 1

One of a number of approaches to a mathematical challenge at the time (1930): Constructibility

Lambda Calculus and Extensions as Foundation of Functional Programming

Towards Coq Formalisation of {log} Set Constraints Resolution

Assistant for Language Theory. SASyLF: An Educational Proof. Corporation. Microsoft. Key Shin. Workshop on Mechanizing Metatheory

On Generalization of Fuzzy Concept Lattices Based on Change of Underlying Fuzzy Order

1 Scope, Bound and Free Occurrences, Closed Terms

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Where is ML type inference headed?

9.5 Equivalence Relations

Lost in translation. Leonardo de Moura Microsoft Research. how easy problems become hard due to bad encodings. Vampire Workshop 2015

CIS 500 Software Foundations Fall September 25

Proving the Genericity Lemma by Leftmost Reduction is Simple

A Typed Lambda Calculus for Input Sanitation

Programming Languages Lecture 15: Recursive Types & Subtyping

A computer implemented philosophy of mathematics

A Simple Supercompiler Formally Verified in Coq

CHAPTER 8. Copyright Cengage Learning. All rights reserved.

Inductive datatypes in HOL. lessons learned in Formal-Logic Engineering

Symmetry in Type Theory

Mathematics for Computer Scientists 2 (G52MC2)

Automated Reasoning. Natural Deduction in First-Order Logic

CSE-321 Programming Languages 2011 Final

Lambda Calculus and Type Inference

Lecture 19 Thursday, March 29. Examples of isomorphic, and non-isomorphic graphs will be given in class.

Lecture Note: Types. 1 Introduction 2. 2 Simple Types 3. 3 Type Soundness 6. 4 Recursive Types Subtyping 17

Mechanizing Metatheory with LF and Twelf

Recursive Types and Subtyping

Semantic Subtyping. Alain Frisch (ENS Paris) Giuseppe Castagna (ENS Paris) Véronique Benzaken (LRI U Paris Sud)

EXTENSIONS OF FIRST ORDER LOGIC

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Soundness and principality of type inference for structural polymorphism

Lambda Calculus and Type Inference

Mathematical and Algorithmic Foundations Linear Programming and Matchings

THREE LECTURES ON BASIC TOPOLOGY. 1. Basic notions.

3.4 Denotational Semantics

dynamically typed dynamically scoped

Transcription:

Proof Pearl: Substitution Revisited, Again Gyesik Lee Hankyong National University, Korea gslee@hknu.ac.kr Abstract. We revisit Allen Stoughton s 1988 paper Substitution Revisited and use it as our test case in exploring a nominal approach to variable binding in Coq. In our nominal approach, we use only onesorted variable names as in pen-and-paper work. We show that it is not only feasible, but also convenient to work with the nominal approach in Coq. Furthermore, we figure out a light infrastructure which provides the base in proving a series of results about simultaneous substitution and α-congruence in the untyped λ-calculus. 1 Introduction Stoughton (1988) demonstrated a noncanonical, but elegant and sound nominal approach to variable binding. Any substitution (including identity substitution) puts terms in a substitution normal form, and α-congruence is equality of substitution normal forms. In spite of the noncanonical aspect, we find Stoughton s representation very interesting because we believe it is closely related to Barendregt s Variable Convention: VARIABLE CONVENTION. If M 1,..., M n occur in a certain mathematical context (e.g. definition, proof), then in these terms all bound variables are chosen to be different from the free variables. (Barendregt 1984) This convention tacitly assume that we can rename bound variables by fresh ones when necessary, for example, in case of possible variable capture. On the other hand, in Stoughton s representation we rename bound variables whenever we do substitutions without thinking of whether variable capture occurs or not. From the standpoint of mechanization, it is much more convenient to do reasoning without being based on tacit conventions. Indeed, as long as we stick to one-sorted variable names, there is no direct way to formalize reasoning based on such tacit assumptions like

2 Barendregt s Variable Convention. Such a formalization would work entirely at the level of terms quotiented by α-congruence. Pitts s Nominal Logic (Pitts 2003) and nominal techniques in Isabelle/HOL (Urban and Tasson 2005), e.g., can be regarded as methods of formalizing the spirit of Barendregt. Moreover, as Barendregt s Variable Convention leads to slick informal proofs, we find that the unrestricted substitution is adequate for formalization of reasoning in a proof assistant like Coq. Therefore, we decided to use (Stoughton 1988) as our test case in exploring a nominal approach to variable binding in Coq. This approach is characterized by a close correspondence between the original reasoning on paper and ours within Coq. To the best of our knowledge, there are two previous works where the idea of unrestricted substitution are adopted. O Connor (2005) used it to formalize the Gödel-Rosser incompleteness theorem. But he did not use simultaneous substitution, hence could not maximize the efficiency of the unrestricted substitution, cf. O Connor (2005, Section 2.4). On the other hand, Garillot and Werner (2007) applied the original unrestricted substitution in a formal treatment of normalization by evaluation in type theory. There are, however, two main differences from ours. First, we use Coq s internal equality when we do reasoning about sets of free variables while it is not the case in Garillot and Werner s work. Second, we use Coq s basic terminology and techniques while they use the Ssreflect plugin. Their source code can be found at https://github.com/huitseeker/nbe. The file nbe_alpha_conversion.v contains the part about α-congruence and can be compared with ours at http://formal.hknu.ac.kr/ Stoughton/. We find that these previous works provide an enough evidence for the adequacy of the Stoughton s unrestricted substitution in formal work. Our primary contribution is to demonstrate that it is not only feasible, but also convenient to work with the nominal approach in Coq. Most of all, our formalization is very compact and close to the original reasoning on paper. 1 1 We even believe that it would satisfy the three criteria proposed by (Aydemir et al. 2005), i.e., reasonable overheads, cost of entry, and transparency in evaluating mechanization of formal metatheory. However, this goes beyond the scope of this paper.

The rest of the pearl gives explanations about our formalization in Coq. Section 2 explains our choice of the formal representation of the untyped λ-calculus, substitution, and α-congruence. In Section 3, it is demonstrated how basic, but important properties about substitution and α-congruence can be formally proved. Section 4 deals with denotational semantics of the untyped λ-calculus. Finally, Section 5 concludes. 2 Definitions We assume a denumerable and decidable set var of variable names. A set is decidable when the equality of two elements are decidable. For simplicity, we use nat, the type of natural numbers, as var. In the rest of the paper, we use the following notations. Notation " x @ L " := (In x L) (at level 70). Notation " x # L " := (~ In x L) (at level 70). Notation " L \ { x } " := (remove eq_nat_dec x L) (at level 59). Terms and free variables. Untyped terms are defined as usual: Inductive term : Set := Var : var -> term App : term -> term -> term Lam : var -> term -> term. The set FV M of free variables in a term M is defined recursively and realized by Coq s standard (finite) lists of variables. A variable x is free in a term M iff (x @ FV M). Fixpoint FV (M : term) : list var := match M with Var x => x :: nil App M0 M1 => (FV M0) ++ (FV M1) Lam x M => (FV M ) \ {x} end. Unrestricted simultaneous substitution. (var -> term) denotes the set of simultaneous substitutions. The identity substitution iota is defined by iota x = Var x. 3

4 Definition substitution := var -> term. Notation iota := (fun x : var => Var x). Defining the set of substitutions as a function type is another difference from Garillot and Werner s formalization where they used finite lists of pairs of variables and terms. Whether to use functions or finite lists to represent simultaneous substitutions is an important choice factor for implementation. We also started first using finite lists, then found it a little bit too much when equality of substitutions is involved. We could save a lot after we took the function type instead. Moreover, it made our formalization closer to the reasoning on paper. Note then that functional extensionality is necessary to deal with functional equality. As already mentioned, the unrestricted substitution always renames all the bound variables in order to avoid variable capture. For that we first need to know which variables occur during substitution. Then we use three operations to choose fresh ones. First, fv_coll collects all the free variables from the codomain of a substitution function with respect to a finite list. Second, old uses fv_coll to collect free variables which come up during a substitution. Note that old is the dual function of Stoughton s new. It builds cofinite sets of variables which do not come up during a substitution. Finally, given a finite set L of variables, choice chooses a fresh variable which does not belong to L. We take the least one bigger than any element from L. In fact, it does not matter how to choose a fresh one, cf. Corollary C3_11 on page 9. Fixpoint fv_coll (L : list var)(rho : substitution) : list var := match L with nil => nil x :: l0 => FV (rho x) ++ fv_coll l0 rho end. Definition old (x : var)(m : term)(rho : substitution) : list var := fv_coll (FV M \ {x}) rho. Definition choice (L : list var) : var := S (fold_right max 0 L).

These three operations guarantee the freshness of the new variables in the definition of unrestricted simultaneous substitution. We also define compositions of simultaneous substitutions. Fixpoint subst (M : term)(rho : substitution) : term := match M with Var x => rho x App M0 M1 => App (subst M0 rho) (subst M1 rho) Lam x M => let y := choice (old x M rho) in Lam y (subst M (rho [[Var y => x]])) end. Notation " M {{ rho }} " := (subst M rho) (at level 55). Definition comp (eta rho : substitution) := fun x => (rho x) {{eta}}. Here rho [[Var y => x]] stands for the substitution which differs from rho only by the value at x. That is, it renames every occurrence of the variable Var x by Var y. This formal version of the unrestricted substitution shows clearly the importance of simultaneous substitution. If we had used a standard, single substitution, then we needed to first rename Var x by Var y, then apply the substitution. This, however, means that we could not have structural recursion. See O Connor (2005, Section 2.4) for further detail. The following lemma is most crucial in the whole development. It reduces properties about substitution and free variables to the level of collecting free variables. So we do not need worry about the complex mechanism of substitution. Furthermore, we use Coq s internal equality instead of any kind of set-theoretic equalities or setoid equalities. Lemma fv_coll_subst : forall (M : term) (rho : substitution), FV (M {{rho}}) = fv_coll (FV M) rho. α-congruence. Stoughton defined the α-congruence relation, = α, as the least equivalence relation over term such that 5

6 (µ) M N = α M N if M = α M and N = α N ; and (α) Lam x M = α Lam y N if either (i) x = y and M = α N, or (ii) y FV M and M {{iota [[Var y => x]]}} = α N. and showed that it can be characterized by a structural induction. Here we take this structural characterization of α-congruence as our definition. We find it mathematically more natural to use a structural characterization. Furthermore, defining α-congruence this way is more appropriate for a formal development in Coq where inductive reasoning is the most powerful tool. It provides not only a structural induction principle, but also reduces the number of cases to be considered. Inductive a_cong : term -> term -> Prop := al_var : forall (x : var), (Var x) Eq (Var x) al_app : forall (M N M0 N0 : term), M Eq M0 -> N Eq N0 -> (App M N) Eq (App M0 N0) al_lam_eq : forall (x y : var) (M N : term), x = y -> M Eq N -> (Lam x M) Eq (Lam y N) al_lam_notin : forall (x y : var) (M N : term), y # FV M -> (M {{iota [[Var y => x]]}}) Eq N -> (Lam x M) Eq (Lam y N) where " M Eq N " := (a_cong M N). We also need to talk about the α-congruence of substitutions. Definition subst_cong (rho eta : substitution) (X : list var) := forall x, x @ X -> (rho x) Eq (eta x). 3 Substitution and α-congruence Stoughton (1988) implicitely used the fact that the α-congruence relation is an equivalence relation. Here we will make it explicit where the reflexivity, symmetry, and transitivity of the relation of α-congruence are necessary. And they will be proved when it is possible. The reflexivity is a basic one needed everywhere. Lemma a_cong_refl (M : term) : M Eq M.

Remark. In this and next section we use the same numbering for lemmas, theorems, and corollaries as in the original paper. For example, L3_1_ii corresponds to Lemma 3.1.(ii) in (Stoughton 1988). As already mentioned just before Lemma fv_coll_subst, properties about substitution and free variables can be reduced to those about collecting free variables. For instance, in order to prove Lemma L3_1_ii : forall (M : term)(x : var) (rho : substitution), x @ FV (M {{rho}}) <-> exists y :var, y @ FV M /\ x @ FV (rho y). we just need to show the following which is much more convenient to deal with. It can be proved by a simple induction on lists. Lemma L3_1_ii : forall (L : list var)(x : var) (rho : substitution), x @ fv_coll L rho <-> exists y : var, y @ L /\ x @ FV (rho y). Except for Lemma 3.1.(vi) in Stoughton (1988), most of the properties can be formally proved almost in the same way as on paper. Lemma L3_1_vi : forall (M : term), M {{iota}} Eq M. The original short proof of Lemma L3_1_vi is based on the fact that the α-congruence relation is symmetric. Note however that we do not have the symmetry yet. In our formalization, Lemma L3_1_vi cannot be proved until we have the following syntactic Substitution Lemma. Theorem T3_2 : forall (M : term)(rho eta : substitution), (M {{rho}}) {{eta}} = M {{comp eta rho}}. And this Substitution Lemma is necessary to prove Theorem T3_5 which says that applying a substitution to congruent terms yields equal, not just α-congruent, terms. Theorem T3_5 : forall (M N : term), M Eq N -> forall rho, M {{rho}} = N {{rho}}. 7

8 An immediate consequence is that identity substitutions normalize terms with respect to α-congruence. Corollary C3_6_i : forall (M N : term), M Eq N <-> M {{iota}} = N {{iota}}. However, in order to prove this corollary from Theorem T3_5, we first need to prove that the relation of α-congruence is an equivalence relation. Surprisingly, the symmetry and transitivity can be proved only after we have Theorem T3_5. Therefore, in our formalization, Lemma L3_1_vi is necessary to prove the symmetry and transitivity of the α-congruence relation, not the other way around. The transitivity is one of the most difficult properties to show in our formalization. It needs an auxiliary lemma a_cong_trans_iota which says that the transitivity holds with respect to the normalized terms. Lemma a_cong_sym (M N : term) : M Eq N -> N Eq M. Lemma a_cong_trans_iota : forall (M N : term), M {{iota}} Eq N -> M Eq N. Lemma a_cong_trans : forall (M N P : term), M Eq N -> N Eq P -> M Eq P. We close this section by listing some other important properties. First, α-equivalent terms contain the same free variables: Lemma L3_1_iii : forall (M N : term), M Eq N -> FV M = FV N. Second, substitution and alpha-congruence are compatible: Corollary C3_8 : forall (M N : term) (rho eta : substitution), M Eq N -> subst_cong rho eta (FV M) -> M {{rho}} Eq N {{eta}}.

Third, in the definition of Lam x M {{rho}}, the choice of fresh variables can be arbitrary up to α-congruence as long as it does not belong to old x M rho: Corollary C3_11 : forall (M : term)(x y : var) (rho : substitution), y # old x M rho -> (Lam x M) {{rho}} Eq Lam y (M {{rho [[Var y => x]]}}). 4 Substitution and denotational semantics This section consists of a proof of the semantic analogue of the syntactic Substitution Lemma in a denotational semantics. The denotational semantics is based on a complete partial order (cpo): E, a cpo of expression values, is a nontrivial solution to the isomorphism equation E = (E -> E) ; In : (E -> E) -> E and Out : E -> (E -> E) are continuous functions such that Out In = id (E -> E) and In Out = id E. Here we just assume that such a cpo E with two continuous functions In and Out are given. Parameter E : Type. Parameter In : (E -> E) -> E. Parameter Out : E -> (E -> E). Axiom OutIn : forall (f : E -> E), Out (In f) = f. Axiom InOut : forall (x : E), In (Out x) = x. We now define a denotational semantics for the untyped λ-calculus by a structural recursion. We let U stand for the cpo of environments fvar -> E, ordered componentwise. Fixpoint DS (M : term) : U -> E := match M with Var x => fun tau => tau x App M1 M2 => fun tau => (Out (DS M1 tau)) (DS M2 tau) Lam x M => fun tau => In (fun e => DS M (tau [[e => x]])) end. 9

10 Then the semantic analogue of the syntactic Substitution Lemma holds. The expression e_comp tau rho denotes the composition of an environment tau and a substitution rho. Definition e_comp (tau : U) (rho : substitution) : U := fun x => DS (rho x) tau. Theorem T4_2 : forall (M : term)(tau : U) (rho : substitution), DS (M {{rho}}) tau = DS M (e_comp tau rho). Finally, we can show that α-equivalent terms are equal in the denotational semantics. Corollary C4_5 : forall (M N : term), M Eq N -> DS M = DS N. 5 Conclusion Our proof pearl introduces a formalization of Substitution Revisited (Stoughton 1988) in Coq. The formalization is done as close as possible to the original reasoning on paper and show that it is not only feasible, but also convenient to work with the nominal approach in Coq: We apply nominal approach with one-sorted variable names to variable binding in Coq. We use Coq s internal equality instead of any kind of set-theoretic equalities or setoid equalities. We employ only Coq s basic terminology and techniques. Our future work is to extend this work by applying the unrestricted simultaneous substitution to more serious tasks. We hope that this work encourages others to get more interested in nominal approach in Coq. Availability The sources which this paper is based on are available from http://formal.hknu.ac.kr/stoughton/.

11 References Brian E. Aydemir, Aaron Bohannon, Matthew Fairbairn, J. Nathan Foster, Benjamin C. Pierce, Peter Sewell, Dimitrios Vytiniotis, Geoffrey Washburn, Stephanie Weirich, and Steve Zdancewic. Mechanized Metatheory for the Masses: The PoplMark Challenge. In TPHOLs, volume 3603 of Lecture Notes in Computer Science, pages 50 65. Springer, 2005. H.P. Barendregt. The Lambda Calculus: Its Syntax and Semantics. Studies in Logic and the Foundations of Mathematics. North- Holland, 1984. François Garillot and Benjamin Werner. Simple Types in Type Theory: Deep and Shallow Encodings. In TPHOLs, volume 4732 of Lecture Notes in Computer Science, pages 368 382. Springer, 2007. Russell O Connor. Essential incompleteness of arithmetic verified by coq. In TPHOLs, volume 3603 of Lecture Notes in Computer Science, pages 245 260. Springer, 2005. Andrew M. Pitts. Nominal logic, a first order theory of names and binding. Inf. Comput., 186(2):165 193, 2003. Allen Stoughton. Substitution Revisited. Theor. Comput. Sci., 59: 317 325, 1988. Christian Urban and Christine Tasson. Nominal Techniques in Isabelle/HOL. In CADE, volume 3632 of Lecture Notes in Computer Science, pages 38 53. Springer, 2005.