Type Theory and Coq. Herman Geuvers. Principal Types and Type Checking

Σχετικά έγγραφα
Proving with Computer Assistance Lecture 2. Herman Geuvers

The Simply Typed Lambda Calculus

About these lecture notes. Simply Typed λ-calculus. Types

C.S. 430 Assignment 6, Sample Solutions

Introduction to Type Theory February 2008 Alpha Lernet Summer School Piriapolis, Uruguay. Herman Geuvers Nijmegen & Eindhoven, NL

2 Composition. Invertible Mappings

Lecture 2. Soundness and completeness of propositional logic

Introduction to Type Theory February 2008 Alpha Lernet Summer School Piriapolis, Uruguay. Herman Geuvers Nijmegen & Eindhoven, NL

Every set of first-order formulas is equivalent to an independent set

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

The λ-calculus. Lecturer: John Wickerson. Phil Wadler

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

A Lambda Model Characterizing Computational Behaviours of Terms

Fractional Colorings and Zykov Products of graphs

EE512: Error Control Coding

From the finite to the transfinite: Λµ-terms and streams

derivation of the Laplacian from rectangular to spherical coordinates

Abstract Storage Devices

Chapter 6: Systems of Linear Differential. be continuous functions on the interval

λρ-calculus 1. each λ-variable is a λρ-term, called an atom or atomic term; 2. if M and N are λρ-term then (MN) is a λρ-term called an application;

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

Sequent Calculi for the Modal µ-calculus over S5. Luca Alberucci, University of Berne. Logic Colloquium Berne, July 4th 2008

k A = [k, k]( )[a 1, a 2 ] = [ka 1,ka 2 ] 4For the division of two intervals of confidence in R +

Other Test Constructions: Likelihood Ratio & Bayes Tests

Statistical Inference I Locally most powerful tests

Matrices and Determinants

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Homomorphism in Intuitionistic Fuzzy Automata

ST5224: Advanced Statistical Theory II

Fourier Series. MATH 211, Calculus II. J. Robert Buchanan. Spring Department of Mathematics

Lambda calculus. Part II Lambda Calculi with Types

Problem Set 3: Solutions

Example Sheet 3 Solutions

Numerical Analysis FMN011

Section 8.3 Trigonometric Equations

Areas and Lengths in Polar Coordinates

Inverse trigonometric functions & General Solution of Trigonometric Equations

4.6 Autoregressive Moving Average Model ARMA(1,1)

Section 7.6 Double and Half Angle Formulas

Finite Field Problems: Solutions

Homework 3 Solutions

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

On the Galois Group of Linear Difference-Differential Equations

Math221: HW# 1 solutions

Uniform Convergence of Fourier Series Michael Taylor

Nowhere-zero flows Let be a digraph, Abelian group. A Γ-circulation in is a mapping : such that, where, and : tail in X, head in

Concrete Mathematics Exercises from 30 September 2016

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

6.3 Forecasting ARMA processes

Foundations of Computer Science ENGR 3520 Fall 2013 Thursday, Nov 21, 2013

Areas and Lengths in Polar Coordinates

Chapter 6: Systems of Linear Differential. be continuous functions on the interval

5. Choice under Uncertainty

Exercises 10. Find a fundamental matrix of the given system of equations. Also find the fundamental matrix Φ(t) satisfying Φ(0) = I. 1.

Practice Exam 2. Conceptual Questions. 1. State a Basic identity and then verify it. (a) Identity: Solution: One identity is csc(θ) = 1

Αλγόριθμοι και πολυπλοκότητα NP-Completeness (2)

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

PARTIAL NOTES for 6.1 Trigonometric Identities

Tridiagonal matrices. Gérard MEURANT. October, 2008

Srednicki Chapter 55

ω ω ω ω ω ω+2 ω ω+2 + ω ω ω ω+2 + ω ω+1 ω ω+2 2 ω ω ω ω ω ω ω ω+1 ω ω2 ω ω2 + ω ω ω2 + ω ω ω ω2 + ω ω+1 ω ω2 + ω ω+1 + ω ω ω ω2 + ω

ORDINAL ARITHMETIC JULIAN J. SCHLÖDER

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

Solutions to Exercise Sheet 5

Lecture 15 - Root System Axiomatics

Section 9.2 Polar Equations and Graphs

Types and Logics. Programming Languages CS442. David Toman. School of Computer Science University of Waterloo

TMA4115 Matematikk 3

CRASH COURSE IN PRECALCULUS

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

ANSWERSHEET (TOPIC = DIFFERENTIAL CALCULUS) COLLECTION #2. h 0 h h 0 h h 0 ( ) g k = g 0 + g 1 + g g 2009 =?

Chapter 3: Ordinal Numbers

Commutative Monoids in Intuitionistic Fuzzy Sets

Quadratic Expressions

Reminders: linear functions

Lecture 13 - Root Space Decomposition II

CS-XXX: Graduate Programming Languages. Lecture 27 Higher-Order Polymorphism. Matthew Fluet 2012

Formal Semantics. 1 Type Logic

A Note on Intuitionistic Fuzzy. Equivalence Relation

2. Let H 1 and H 2 be Hilbert spaces and let T : H 1 H 2 be a bounded linear operator. Prove that [T (H 1 )] = N (T ). (6p)

Partial Differential Equations in Biology The boundary element method. March 26, 2013

The challenges of non-stable predicates

b. Use the parametrization from (a) to compute the area of S a as S a ds. Be sure to substitute for ds!

Μηχανική Μάθηση Hypothesis Testing

D Alembert s Solution to the Wave Equation

Second Order Partial Differential Equations

T λx. (λy. x) F λx. (λy. y) if λpca. pca

Durbin-Levinson recursive method

Econ Spring 2004 Instructor: Prof. Kiefer Solution to Problem set # 5. γ (0)

Appendix to On the stability of a compressible axisymmetric rotating flow in a pipe. By Z. Rusak & J. H. Lee

1. Introduction and Preliminaries.

12. Radon-Nikodym Theorem

Notes on the Open Economy

( y) Partial Differential Equations

Instruction Execution Times

Εγχειρίδια Μαθηµατικών και Χταποδάκι στα Κάρβουνα

Strain gauge and rosettes

Chapter 2. Ordinals, well-founded relations.

Transcript:

Type Theory and Coq Herman Geuvers Principal Types and Type Checking 1

Overview of todays lecture Simple Type Theory à la Curry (versus Simple Type Theory à la Church) Principal Types algorithm Type checking dependent tytpe theory: λp 2

Recap: Simple type theory a la Church. Formulation with contexts to declare the free variables: x 1 : σ 1,x 2 : σ 2,...,x n : σ n is a context, usually denoted by Γ. Derivation rules of λ (à la Church): x:σ Γ Γ x : σ Γ M : σ τ Γ N : σ Γ MN : τ Γ,x:σ P : τ Γ λx:σ.p : σ τ Γ λ M : σ if there is a derivation using these rules with conclusion Γ M : σ 3

Recap: Formulas-as-Types (Curry, Howard) There are two readings of a judgement M : σ 1. term as algorithm/program, type as specification: M is a function of type σ 2. type as a proposition, term as its proof: M is a proof of the proposition σ There is a one-to-one correspondence: typable terms in λ derivations in minimal proposition logic x 1 : τ 1,x 2 : τ 2,...,x n : τ n M : σ can be read as M is a proof of σ from the assumptions τ 1,τ 2,...,τ n. 4

Recap: Example [α β γ] 3 [α] 1 β γ γ 1 α γ (α β) α γ [α β] 2 [α] 1 β (α β γ) (α β) α γ 2 3 λx:α β γ.λy:α β.λz:α.xz(yz) : (α β γ) (α β) α γ 5

Untyped λ-calculus Untyped λ-calculus Λ ::= Var (ΛΛ) (λvar.λ) Examples: - K := λxy.x - S := λxyz.xz(yz) - ω := λx.xx - Ω := ωω Ω β Ω 6

Untyped λ-calculus Untyped λ-calculus is Turing complete It s power lies in the fact that you can solve recursive equations: Is there a term M such that Is there a term M such that M x = β xm x? M x = β if (Zerox)then1elseMultx(M (Predx))? Yes, because we have a fixed point combinator: - Y := λf.(λx.f(xx))(λx.f(xx)) Property: Y f = β f(y f) 7

Why do we want to add types to λ-calculus? Types give a (partial) specification Typed terms can t go wrong (Milner) Subject Reduction property Typed terms always terminate The type checking algorithm detects (simple) mistakes But: The compiler should compute the type information for us! (Why would the programmer have to type all that?) This is called a type assignment system, or also typing à la Curry: For M an untyped term, the type system assigns a type σ to M (or not) 8

STT à la Church and à la Curry λ (à la Church): x:σ Γ Γ x : σ Γ M : σ τ Γ N : σ Γ MN : τ Γ,x:σ P : τ Γ λx:σ.p : σ τ λ (à la Curry): x:σ Γ Γ x : σ Γ M : σ τ Γ N : σ Γ MN : τ Γ,x:σ P : τ Γ λx.p : σ τ 9

Examples Typed Terms: λx : α.λy : (β α) α.y(λz : β.x)) has only the type α ((β α) α) α Type Assignment: can be assigned the types λx.λy.y(λz.x)) α ((β α) α) α (α α) ((β α α) γ) γ... with α ((β α) γ) γ being the principal type 10

Connection between Church and Curry typed STT Definition The erasure map from STT à la Church to STT à la Curry is defined by erasing all type information. So, e.g. x := x M N := M N λx : σ.m := λx. M λx : α.λy : (β α) α.y(λz : β.x)) = λx.λy.y(λz.x)) Theorem If M : σ in STT à la Church, then M : σ in STT à la Curry. Theorem If P : σ in STT à la Curry, then there is an M such that M P and M : σ in STT à la Church. 11

Connection between Church and Curry typed STT Definition The erasure map from STT à la Church to STT à la Curry is defined by erasing all type information. x := x M N := M N λx : σ.m := λx. M Theorem If P : σ in STT à la Curry, then there is an M such that M P and M : σ in STT à la Church. Proof: by induction on derivations. x:σ Γ Γ x : σ Γ M : σ τ Γ N : σ Γ MN : τ Γ,x:σ P : τ Γ λx:σ.p : σ τ 12

Example of computing a principal type δ {}}{ λx α.λy β.y β (λz γ. y β x α ) }{{} ε 1. Assign type vars to all variables: x : α,y : β,z : γ. 2. Assign type vars to all applicative subterms: yx : δ, y(λz.yx) : ε. 3. Generate equations between types, necessary for the term to be typable: β = α δ β = (γ δ) ε 4. Find a most general unifier (a substitution) for the type vars that solves the equations: α := γ ε, β := (γ ε) ε, δ := ε 5. The principal type of λx.λy.y(λz.yx) is now (γ ε) ((γ ε) ε) ε 13

Exercise Compute principal types for S := λx.λy.λz.xz(yz) M := λx.λy.x(y(λz.xzz))(y(λz.xzz)). 14

Principal Types: Preliminary Definitions A type substitution (or just substitution) is a map S from type variables to types. (Note: we can compose substitutions.) A unifier of the types σ and τ is a substitution that makes σ and τ equal, i.e. an S such that S(σ) = S(τ) A most general unifier (or mgu) of the types σ and τ is the simplest substitution that makes σ and τ equal, i.e. an S such that S(σ) = S(τ) for all substitutions T such that T(σ) = T(τ) there is a substitution R such that T = R S. All these notions generalize to lists of types σ 1,...,σ n in stead of pairs σ,τ. 15

Computing a most general unifier There is an algorithm U that, when given types σ 1,...,σ n outputs A most general unifier of σ 1,...,σ n, if σ 1,...,σ n can be unified. Fail if σ 1,...,σ n can t be unified. U( α = α,...,σ n = τ n ) := U( σ 2 = τ 2,...,σ n = τ n ). U( α = τ 1,...,σ n = τ n ) := reject if α FV(τ 1 ), τ 1 α. U( σ 1 = α,...,σ n = τ n ) := U( α = σ 1,...,σ n = τ n ) U( α = τ 1,...,σ n = τ n ) := [α := V(τ 1 ),V], if α / FV(τ 1 ), where V abbreviates U( σ 2 [α := τ 1 ] = τ 2 [α := τ 1 ],...,σ n [α := τ 1 ] = τ n [α := τ 1 ] ). U( µ ν = ρ ξ,...,σ n = τ n ) := U( µ = ρ,ν = ξ,...,σ n = τ n ) 16

Principal type: Definition Definition σ is a principal type for the closed untyped λ-term M if M : σ in STT à la Curry for all types τ, if M : τ, then τ = S(σ) for some substitution S. A principal type is unique up to renaming of type variables. Both α α and β β are principal type of λx.x. 17

Principal Types Theorem Theorem There is an algorithm PT that, when given a closed untyped λ-term M, outputs A principal type σ of M if M is typable in STT à la Curry, Fail if M is not typable in STT à la Curry. This can be extended to open untyped λ-terms: There is an algorithm PP that, when given an untyped λ-term M, outputs A principal pair (Γ,σ) of M if M is typable in STT à la Curry, Fail if M is not typable in STT à la Curry. Definition (Γ,σ) is a principal pair for M if Γ M : σ and for every typing M : τ there is a substitution S such that τ = S(σ) and = S(Γ). 18

Typical problems one would like to have an algorithm for M : σ? Type Checking Problem TCP M :? Type Synthesis Problem TSP? : σ Type Inhabitation Problem (by a closed term) TIP For λ, all these problems are decidable, both for the Curry style and for the Church style presentation. TCP and TSP are (usually) equivalent: To solve MN : σ, one has to solve N :? (and if this gives answer τ, solve M : τ σ). For Curry systems, TCP and TSP soon become undecidable beyond λ. TIP is undecidable for most extensions of λ, as it corresponds to provability in some logic. 19

Rules for λp: axiom, application, abstraction, product : Γ M : Πx : A.B Γ N : A Γ MN : B[x := N] Γ, x : A M : B Γ Πx : A.B : s Γ λx : A.M : Πx : A.B Γ A : Γ, x : A B : s Γ Πx : A.B : s 20

Rules for λp: weakening, variable, conversion Γ A : B Γ C : s Γ, x : C A : B Γ A : s Γ, x : A x : A Γ A : B Γ B : s with B = β B Γ A : B 21

Properties of λp Uniqueness of types If Γ M : σ and Γ M : τ, then σ= β τ. Subject Reduction If Γ M : σ and M β N, then Γ N : σ. Strong Normalization If Γ M : σ, then all β-reductions from M terminate. Proof of SN is by defining a reduction preserving map from λp to λ. 22

Decidability Questions Γ M : σ? TCP Γ M :? TSP Γ? : σ TIP For λp: TIP is undecidable (Equivalent to provability in minimal predicate logic.) TCP/TSP: simultaneously with Context checking 23

Type Checking algorithm for λp Define algorithms Ok( ) and Type ( ) simultaneously: Ok( ) takes a context and returns true or false Type ( ) takes a context and a term and returns a term or false. Definition. The type synthesis algorithm Type ( ) is sound if Type Γ (M) = A Γ M : A for all Γ and M. Definition. The type synthesis algorithm Type ( ) is complete if for all Γ, M and A. Γ M : A Type Γ (M) = β A 24

Ok(<>) = true Ok(Γ,x:A) = Type Γ (A) {,kind}, Type Γ (x) = if Ok(Γ) and x:a Γ then A else false, Type Γ (type) = if Ok(Γ) then kind else false, Type Γ (MN) = if Type Γ (M) = C and Type Γ (N) = D then if C β Πx:A.B and A = β D then B[x := N] else false else false, 25

Type Γ (λx:a.m) = if Type Γ,x:A (M) = B then if Type Γ (Πx:A.B) {type,kind} then Πx:A.B else false else false, Type Γ (Πx:A.B) = if Type Γ (A) = type and Type Γ,x:A (B) = s then s else false 26

Soundness and Completeness Soundness Type Γ (M) = A Γ M : A Completeness Γ M : A Type Γ (M) = β A As a consequence: Type Γ (M) = false M is not typable in Γ NB 1. Completeness only makes sense if types are uniqueness upto = β (Otherwise: let Type ( ) generate a set of possible types) NB 2. Completeness only implies that Type terminates on all well-typed terms. We want that Type terminates on all pseudo terms. 27

Termination We want Type ( ) to terminate on all inputs. Interesting cases: λ-abstraction and application: Type Γ (λx:a.m) = if Type Γ,x:A (M) = B then else false, if Type Γ (Πx:A.B) {type,kind} then Πx:A.B else false! Recursive call is not on a smaller term! Replace the side condition if Type Γ (Πx:A.B) {type,kind} by if Type Γ (A) {type} 28

Termination We want Type ( ) to terminate on all inputs. Interesting cases: λ-abstraction and application: Type Γ (MN) = if Type Γ (M) = C and Type Γ (N) = D then if C β Πx:A.B and A = β D else then B[x := N] else false false,! Need to decide β-reduction and β-equality! For this case, termination follows from soundness of Type and the decidability of equality on well-typed terms (using SN and CR). 29