Normalization by Evaluation

Similar documents
Normalisation by evaluation

CS 4110 Programming Languages & Logics. Lecture 16 Programming in the λ-calculus

Normalization by Evaluation in the Delay Monad A Case Study for Coinduction via Copatterns and Sized Types

Normalisation by Evaluation for Dependent Types

COMP6463: λ-calculus

CS522 - Programming Language Semantics

A proof checking kernel for the λπ-calculus modulo

Strong Normalization for Guarded Types

Extending the Lambda Calculus: An Eager Functional Language

Formalising the Completeness Theorem of Classical Propositional Logic in Agda (Proof Pearl)

On the Correctness of the Krivine Machine

Operational Semantics Using the Partiality Monad

Typed Arithmetic Expressions

Normalization by Evaluation for Intuitionistic Propositional Logic

A MODULAR TYPE-CHECKING ALGORITHM FOR TYPE THEORY WITH SINGLETON TYPES AND PROOF IRRELEVANCE

λ Slide 1 Content Exercises from last time λ-calculus COMP 4161 NICTA Advanced Course Advanced Topics in Software Verification

The Lambda Calculus. Stephen A. Edwards. Fall Columbia University

On the Syntax and Semantics of Quantitative Typing

Typing λ-terms. Types. Typed λ-terms. Base Types. The Typing Relation. Advanced Formal Methods. Lecture 3: Simply Typed Lambda calculus

NICTA Advanced Course. Theorem Proving Principles, Techniques, Applications

Minimal logic for computable functionals

On the Correctness and Efficiency of the Krivine Machine

THE UNIVERSITY OF CALGARY FACULTY OF SCIENCE FINAL EXAMINATION COMPUTER SCIENCE 521

CSE 505, Fall 2009, Midterm Examination 5 November Please do not turn the page until everyone is ready.

Normalisation by Evaluation for Dependent Types

A Calculus of Definitions

Beyond First-Order Logic

Static Program Analysis

COMPUTER SCIENCE TRIPOS

λ-calculus and types

Subtyping and Intersection Types Revisited

Type Systems. Lecture 2 Oct. 27th, 2004 Sebastian Maneth.

THE UNIVERSITY OF CALGARY FACULTY OF SCIENCE FINAL EXAMINATION COMPUTER SCIENCE 521

Verified Characteristic Formulae for CakeML. Armaël Guéneau, Magnus O. Myreen, Ramana Kumar, Michael Norrish April 18, 2017

Mathematical Synthesis of Equational Deduction Systems. Marcelo Fiore. Computer Laboratory University of Cambridge

Models of computation

Lambda-Calculus (I) 2nd Asian-Pacific Summer School on Formal Methods Tsinghua University, August 23, 2010

CSCI 490 problem set 6

Operational Semantics

Midterm Exam Types and Programming Languages Frank Pfenning. October 18, 2018

Notes on Inductive Sets and Induction

Applied Logic. Lecture 1 - Propositional logic. Marcin Szczuka. Institute of Informatics, The University of Warsaw

The lambda calculus with constructors

HORSes: format, termination and confluence

Copatterns Programming Infinite Objects by Observations

Matching Logic: Syntax and Semantics

Roy L. Crole. Operational Semantics Abstract Machines and Correctness. University of Leicester, UK

Type Theory and Constructive Mathematics. Type Theory and Constructive Mathematics Thierry Coquand. University of Gothenburg

The Lifting Lemma. Ralf Hinze

CBV and CBN. Eduardo Bonelli. TP para LP 2012C1 1/55

Internship Report Game Semantics and Normalization by Evaluation for Brouwer Ordinals

Simply Typed Lambda Calculus

Type Systems Winter Semester 2006

Safety Analysis versus Type Inference

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

Kleene realizability and negative translations

Classical First-Order Logic

Formal Techniques for Software Engineering: Denotational Semantics

A call-by-name lambda-calculus machine

Foundations of Computation. Ana Bove

Justifying Algorithms for βη-conversion

Computer Proof Assistants and Univalent Foundations of Mathematics

Depending on equations

Coinductive big-step operational semantics

1. Object Calculus. Object calculus is to OO languages what lambda calculus is to functional languages

The Curry-Howard Isomorphism

The Turing Machine. Computability. The Church-Turing Thesis (1936) Theory Hall of Fame. Theory Hall of Fame. Undecidability

ADVENTURES IN TIME & SPACE. Jim Royer Syracuse University

Introduction to Kleene Algebras

Type Systems Winter Semester 2006

Introduction to lambda calculus Part 6

Simply Typed Lambda Calculus

Lecture Notes on Inductive Definitions

The Calculus of Inductive Constructions

Predicate Logic. Xinyu Feng 09/26/2011. University of Science and Technology of China (USTC)

Supplementary Notes on Inductive Definitions

Predicate Logic. x. x + 0 = x. Predicate logic over integer expressions: a language of logical assertions, for example. Why discuss predicate logic?

A probabilistic lambda calculus - Some preliminary investigations

Predicate Logic. Xinyu Feng 11/20/2013. University of Science and Technology of China (USTC)

Orthogonality and Algebraic Lambda-Calculus

Formal Methods Lecture 6. (B. Pierce's slides for the book Types and Programming Languages )

NORMALISATION BY EVALUATION FOR TYPE THEORY, IN TYPE THEORY

Isomorphism is equality

Programming Languages

Review. Principles of Programming Languages. Equality. The Diamond Property. The Church-Rosser Theorem. Corollaries. CSE 230: Winter 2007

Equality and dependent type theory. CIRM, May 31

Undecidable Problems. Z. Sawa (TU Ostrava) Introd. to Theoretical Computer Science May 12, / 65

Denotational semantics: proofs

Local computation of β-reduction A concrete presentation of Game Semantics

Extended Abstract: Reconsidering Intuitionistic Duality

CIS 500 Software Foundations Final Exam Answer key December 20, 2004

CS611 Lecture 25 Solving Domain Equations 22 October 2007 Lecturer: Andrew Myers

EDA045F: Program Analysis LECTURE 10: TYPES 1. Christoph Reichenbach

The Nuggetizer: Abstracting Away Higher-Orderness for Program Verification

Introduction to Turing Machines

Introduction to lambda calculus Part 2

Blame for All. Amal Ahmed, Robert Bruce Findler, Jeremy Siek, Philip Wadler

Non-Idempotent Typing Operators, beyond the λ-calculus

Certification of Safe Polynomial Memory Bounds (Extended Version)

Programming Language Concepts: Lecture 16

Transcription:

Normalization by Evaluation Andreas Abel Department of Computer Science and Engineering Chalmers and Gothenburg University PhD Seminar in Mathematical Engineering EAFIT University, Medellin, Colombia 9 March 2017 Andreas Abel (GU) NbE EAFIT 0217 1 / 32

Introduction Context of This Work Dependently-typed (programming) languages allow to express functional specifications in types, to prove (correctness) properties in the language, formalize and prove mathematical propositions. Prominent proof assistent: Coq (INRIA 1984 ) CompCert: Certifed compiler for C (Leroy) Formalized proof of Four Color Theorem (Gonthier, 2005) Odd-Order Theorem (Gonthier, 2012) Theorem Feit_Thompson (gt : fingrouptype) (G : {group gt}) : odd # G -> solvable G. Experimental languages: Agda, Idris,... Andreas Abel (GU) NbE EAFIT 0217 2 / 32

Introduction Behind the Veil What made Coq ready for huge developments? Benjamin Grégoire, Xavier Leroy: A compiled implementation of strong reduction. ICFP 2002 Efficient normalization! Grégoire, Leroy: Efficient checking of β-equality. This talk: Framework for βη-equality. Andreas Abel (GU) NbE EAFIT 0217 3 / 32

Introduction A Taste of Programming with Dependent Types Descending lists: [x, y,..., z] List n iff n x y z Constructor carries proof p for descent. nil : List 0 x : N p : x y xs : List y cons x p xs : List x Singleton list carries a trivial proof. singleton : (x : N) List x singleton x = cons x nil where : x 0 Andreas Abel (GU) NbE EAFIT 0217 4 / 32

Introduction Correct Insert Case: Insert into empty list. insert : (x : N) List n List (max x n) insert x nil = singleton x Inferred type singleton x : List x. Expected type singleton x : List (max x 0). Type-checker needs to ensure List x = List (max x 0). Sufficient: x = max x 0. Compare expressions with free variables! Solution: normalize max x 0 to x. Andreas Abel (GU) NbE EAFIT 0217 5 / 32

Introduction Normalization Bring an expression with unkowns into a canonical form. Unknowns = free variables. Checking equality by comparing canonical forms. Examples: Expression arithmetical expression functional programming language stack maching code SQL query Normalizer symbolic evaluator (MathLAB) term rewriting, partial evaluation JIT compiler query compiler Andreas Abel (GU) NbE EAFIT 0217 6 / 32

Introduction Evaluation Compute the value of an expression relative to an environment. Environment assigns values to free variables of expressions. Examples: Expression Environment Evaluator arithmetical expression valuation calculator functional programming language stack & heap interpreter stack machine code stack stack machine SQL-query database SQL processor Andreas Abel (GU) NbE EAFIT 0217 7 / 32

Introduction Normalization by Evaluation (NbE) Adapt an interpreter to simplify expressions with unknowns. MLTT Martin-Löf 1975: NbE for Type Theory (weak conversion) STL Berger Schwichtenberg 1991: NbE for simply-typed λ-calculus T Danvy 1996: Type-directed partial evaluation F Altenkirch Hofmann Streicher 1996: NbE for λ-free System F λ Aehlig Joachimski 2004: Untyped NbE, operationally λ Filinski Rohde 2004: Untyped NbE, denotationally LF Danielsson 2006: strongly typed NbE for LF T Altenkirch Chapman 2007: Tait in one big step Andreas Abel (GU) NbE EAFIT 0217 8 / 32

Monoids Monoids Monoid (M,, ε): set M with a binary operation that has a unit ε. a (b c) = (a b) c associativity ε a = a left unit a ε = a right unit E.g.: (N, +, 0), (N,, 1), (Bool,, true), (Bool,, false), (R n n,, I n ). Free monoid: Sequences with concatenation (List A, ++, [ ]). [a 1,..., a m ] ++ [a m+1,..., a n ] = [a 1,..., a n ] Andreas Abel (GU) NbE EAFIT 0217 9 / 32

Monoids Monoid Expressions Fix a carrier A and a set of variables X. Terms (abstract syntax trees) representing monoid elements: Exp t ::= a singleton a A x variable x X ε empty sequence t 1 t 2 concatenation, right associative Example in concrete syntax: ex := (x 0 1) (((2 (ε x 1 )) (ε 4)) x 2 ) Andreas Abel (GU) NbE EAFIT 0217 10 / 32

Monoids Interpreting Monoid Expressions Monoid values Val = List A. Environment ρ X Val. Interpretation t ρ Val. x ρ = ρ(x) ε ρ = [ ] empty list a ρ = [a] singleton list t 1 t 2 ρ = t 1 ρ ++ t 2 ρ append Example. Recall ex = (x 0 1) (((2 (ε x 1 )) (ε 4)) x 2 ) ex (x0 =0,x 1 =3,x 2 =5) = [0, 1, 2, 3, 4, 5] Andreas Abel (GU) NbE EAFIT 0217 11 / 32

Monoids Normalizing Monoid Expressions I Val = List (A X ). Reflection of variables into values. : X Val x = [x] Reification of values as expressions. : Val Exp [] = ε (a :: v) = a v (x :: v) = x v Andreas Abel (GU) NbE EAFIT 0217 12 / 32

Monoids Normalizing Monoid Expressions II Normalization: nf : Exp Exp nf(t) = t Example. Recall ex = (x 0 1) (((2 (ε x 1 )) (ε 4)) x 2 ) nf(ex) = x 0 1 2 x 1 4 x 2 ε Andreas Abel (GU) NbE EAFIT 0217 13 / 32

Untyped Lambda Calculus Untyped Lambda Calculus, Informally Calculus of functions. Everything is a function. Examples: id = λx. x identity function app = λf. λx. f x application function (also identity) twice = λf. λx. f (f x) apply f twice comp = λf. λg. λx. f (g x) compose two functions Calculation: app twice id = twice id = λx. id (id x) = λx. id x = λx. x = id. Andreas Abel (GU) NbE EAFIT 0217 14 / 32

Untyped Lambda Calculus Numbers in the Untyped Lambda Calculus Numbers n N are represented by Church numerals n. 0 = λf. λx. x 1 = λf. λx. f x 2 = λf. λx. f (f x) n = λf. λx. f n x Addition is a sort of composition. plus = λn m f x. n f (m f x) plus n m = λf x. n f (m f x) = λf x. f n (f m x) = λf x. f n+m x = n + m Andreas Abel (GU) NbE EAFIT 0217 15 / 32

Untyped Lambda Calculus Recursion in the Untyped Lambda Calculus Reduction: (λx. t) s t[s/x] A looping term: (λx. x x) (λx. x x) (x x)[(λx. x x)/x] = (λx. x x) (λx. x x) Alan Turing s fixed-point combinator. Let θ = (λx. λf. f (x x f )). θ θ f f (θ θ f ) Andreas Abel (GU) NbE EAFIT 0217 16 / 32

Untyped Lambda Calculus Untyped Lambda Calculus, Formally Grammar: Exp r, s, t ::= x variable λx. t abstracting variable x in body t r s applying r to s Equational theory (β): (λx. t) s = t[s/x] β-normal forms. Nf v ::= λx. v u normal form Ne u ::= x u v neutral term Andreas Abel (GU) NbE EAFIT 0217 17 / 32

Untyped Lambda Calculus Evaluation of Lambda-Expressions Values a, b, f D with (partial) application : D D D. Evaluation (specification): x ρ = ρ(x) r s ρ. = r ρ s ρ λx. t ρ a. = t (ρ,a/x) Instance: compiled execution. f a λx. t ρ Call f with argument a Code for function λx. t with predefined variables ρ Andreas Abel (GU) NbE EAFIT 0217 18 / 32

Untyped Lambda Calculus Implementation via Closures Instance: do nothing. λx. t ρ = (λxt)ρ Initial applicative structure: closures. D a, b, f ::= (λxt)ρ waiting for value of x Application and evaluation are mutually defined. (λxt)ρ a = t (ρ,a/x) r s ρ = r ρ s ρ Andreas Abel (GU) NbE EAFIT 0217 19 / 32

Untyped Lambda Calculus Residual Model: Adding Unknowns For normalization, we need free variables in D. Application x a of a free variable stores argument a. Need neutrals/accumulators x a in D. D a, b, f ::= (λxt)ρ e D ne e ::= x e a Application extended: (λxt)ρ a = t (ρ,a/x) x a a = x ( a, a) Andreas Abel (GU) NbE EAFIT 0217 20 / 32

Untyped Lambda Calculus Reading Back Expressions from Values Reading back values: R nf : D Nf R nf ((λxt)ρ) = λy. R nf ( t (ρ,y/x) ) where y fresh R nf (e) = R ne (e) Reading back neutrals: R ne : D ne Ne R ne (x) = x R ne (e a) = R ne (e) R nf (a) Andreas Abel (GU) NbE EAFIT 0217 21 / 32

Untyped Lambda Calculus Fresh Name Generation Freshness problem: 9 approaches. Simple solution: R nf ξ reads fresh names from supply ξ. E.g., ξ is an infinite stream of distinct identifiers. R nf (y,ξ)((λxt)ρ) = λy. Rnf ξ ( t (ρ,y/x)) R nf ξ (e) = R ne ξ (e) R ne ξ (x a) = x R nf ξ ( a) Normalization: nf ξ (t) = R nf ξ ( t ρ id ) Andreas Abel (GU) NbE EAFIT 0217 22 / 32

Untyped Lambda Calculus Summary: NbE for Untyped Lambda-Calculus Semantics D [ ] R nf D ne Syntax Exp Nf R ne Ne Var Andreas Abel (GU) NbE EAFIT 0217 23 / 32

Typed Lambda Calculus Simply-Typed Lambda Calculus Types S, T ::= N S T. Typing contexts Γ ::= x 1 :S 1,..., x n :S n. Typing Γ t : T. (x :T ) Γ Γ x : T Equational theory (βη). Γ, x :S t : T Γ λx. t : S T Γ r : S T Γ s : S Γ r s : T (β) Γ, x :S t : T Γ s : S Γ (λxt) s = t[s/x] : T (η) Γ t : S T Γ t = λx. t x : S T Andreas Abel (GU) NbE EAFIT 0217 24 / 32

Typed Lambda Calculus Bidirectional η-expansion T reflection : η-expansion inside-out T reification : η-expansion outside-in Example (terms): (N N) (N N) f = λy. N N (f ( N N y)) = λy. λx. N (f ( N N y) ( N x)) = λy. λx. N (f (λz. N (y ( N z))) ( N x)) = λy. λx. f (λz. y z) x Andreas Abel (GU) NbE EAFIT 0217 25 / 32

Typed Lambda Calculus Adding η-expansion Semantics (β) D T T Semantics (βη) [ ] D nf D ne R nf R ne Syntax Exp Nf Ne Var Andreas Abel (GU) NbE EAFIT 0217 26 / 32

Typed Lambda Calculus Eta-expansion: reflection and reification Values now include delayed η-expansions. D a, b, f ::= (λxt)ρ T e D ne e ::= x e d D nf d ::= T a Application and readback trigger these expansions. (λxt)ρ a = t (ρ,a/x) S T e a = T (e S a) R nf (y,ξ) ( S T f ) = λy. R nf ξ ( T (f S y)) R nf ξ ( N N e) = R ne ξ (e) Andreas Abel (GU) NbE EAFIT 0217 27 / 32

Typed Lambda Calculus Normalization for STL Canonical environment: ρ Γ (x) = T x where (x : T ) Γ Variable supply: ξ Γ = Var \ Γ Normalization of Γ t : T : nf T Γ (t) = Rnf ξ Γ ( T t ργ ) Andreas Abel (GU) NbE EAFIT 0217 28 / 32

Typed Lambda Calculus Correctness of Normalization Normalization is sound if for all expressions Γ t : T, Γ t = nf T Γ (t) : T. Normalization is complete if for all Γ t, t : T, Γ t = t : T = nf T Γ (t) = α nf T Γ (t ) Implies idempotence nf T Γ (t) = α nf T Γ (nft Γ (t)). Andreas Abel (GU) NbE EAFIT 0217 29 / 32

Typed Lambda Calculus Completeness of Normalization Well-typed βη-equal terms have the same normal form. Γ t = t : T = a {}}{ t ργ = a {}}{ t ργ A {}}{ T ργ = a = a A = A a = A a = R nf ξ Γ A a = α R nf ξ Γ A a Andreas Abel (GU) NbE EAFIT 0217 30 / 32

Typed Lambda Calculus Soundness of Normalization A well-typed term is βη-equal to its normal form. Γ t : T = Γ t : T R = Γ t = R nf ξ Γ A a : T a A {}}{{}}{ t ργ T ργ Γ t = nf T Γ (t) : T Andreas Abel (GU) NbE EAFIT 0217 31 / 32

Conclusions Conclusions Interpreters can be turned into normalizers in a systematic way. Normalization-by-evaluation has helped to understand η-equality. NbE is also a theoretical tool to investigate Type Theory. E.g., to prove decidability of type checking. Andreas Abel (GU) NbE EAFIT 0217 32 / 32