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

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

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

The Simply Typed Lambda Calculus

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

C.S. 430 Assignment 6, Sample Solutions

Lecture 2. Soundness and completeness of propositional logic

A Lambda Model Characterizing Computational Behaviours of Terms

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

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

2 Composition. Invertible Mappings

EE512: Error Control Coding

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

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

Fractional Colorings and Zykov Products of graphs

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

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

Chapter 3: Ordinal Numbers

Finite Field Problems: Solutions

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

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

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

Reminders: linear functions

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

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

Homework 3 Solutions

TMA4115 Matematikk 3

Example Sheet 3 Solutions

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

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

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

Matrices and Determinants

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

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

Other Test Constructions: Likelihood Ratio & Bayes Tests

CRASH COURSE IN PRECALCULUS

λρ-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;

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

Solutions to Exercise Sheet 5

2. THEORY OF EQUATIONS. PREVIOUS EAMCET Bits.

SCITECH Volume 13, Issue 2 RESEARCH ORGANISATION Published online: March 29, 2018

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

Math221: HW# 1 solutions

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

Numerical Analysis FMN011

Bounding Nonsplitting Enumeration Degrees

ORDINAL ARITHMETIC JULIAN J. SCHLÖDER

Formal Semantics. 1 Type Logic

Srednicki Chapter 55

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

derivation of the Laplacian from rectangular to spherical coordinates

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Homomorphism in Intuitionistic Fuzzy Automata

Jesse Maassen and Mark Lundstrom Purdue University November 25, 2013

1. Introduction and Preliminaries.

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

5. Choice under Uncertainty

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

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

Congruence Classes of Invertible Matrices of Order 3 over F 2

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

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

PARTIAL NOTES for 6.1 Trigonometric Identities

Abstract Storage Devices

forms This gives Remark 1. How to remember the above formulas: Substituting these into the equation we obtain with

Trigonometric Formula Sheet

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

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

THE SECOND ISOMORPHISM THEOREM ON ORDERED SET UNDER ANTIORDERS. Daniel A. Romano

4.6 Autoregressive Moving Average Model ARMA(1,1)

Models for Probabilistic Programs with an Adversary

Second Order Partial Differential Equations

Homework 8 Model Solution Section

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

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

Section 8.3 Trigonometric Equations

Problem Set 3: Solutions

Statistical Inference I Locally most powerful tests

Bayesian statistics. DS GA 1002 Probability and Statistics for Data Science.

Lecture 34 Bootstrap confidence intervals

A Hierarchy of Theta Bodies for Polynomial Systems

Parametrized Surfaces

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

Approximation of distance between locations on earth given by latitude and longitude

Uniform Convergence of Fourier Series Michael Taylor

SOLUTIONS TO MATH38181 EXTREME VALUES AND FINANCIAL RISK EXAM

5.4 The Poisson Distribution.

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)

Instruction Execution Times

An Inventory of Continuous Distributions

SOLUTIONS TO MATH38181 EXTREME VALUES AND FINANCIAL RISK EXAM

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

Inverse trigonometric functions & General Solution of Trigonometric Equations

12. Radon-Nikodym Theorem

ST5224: Advanced Statistical Theory II

F A S C I C U L I M A T H E M A T I C I

Απόκριση σε Μοναδιαία Ωστική Δύναμη (Unit Impulse) Απόκριση σε Δυνάμεις Αυθαίρετα Μεταβαλλόμενες με το Χρόνο. Απόστολος Σ.

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

Section 7.6 Double and Half Angle Formulas

Solution Series 9. i=1 x i and i=1 x i.

Transcript:

Introduction to Type Theory February 2008 Alpha Lernet Summer School Piriapolis, Uruguay Herman Geuvers Nijmegen & Eindhoven, NL Lecture 3: Polymorphic Type Theory: Full polymorphism and ML style polymorphism 1

Why Polymorphic λ-calculus? Simple type theory λ is not very expressive In simple type theory, we can not reuse a function. E.g. λx:α.x : α α and λx:β.x : β β. We want to define functions that can treat types polymorphically: add types α.σ: Examples α.α α If M : α.α α, then M can map any type to itself. α. β.α β α If M : α. β.α β α, then M can take two inputs (of arbitrary types) and return a value of the first input type. 2

Derivation rules for Weak (ML-style) polymorphism Typ : add α 1.... α n.σ for σ a λ -type. 1. Curry style: Γ M : σ α / FV(Γ) Γ M : α.σ 2. Church style: Γ M : σ α / FV(Γ) Γ λα.m : α.σ Γ M : α.σ for τ a λ -type Γ M : σ[α := τ] Γ M : α.σ for τ a λ -type Γ Mτ : σ[α := τ] only occurs on the outside and is therefore usually left out: all type variables are implicitly universally quantified With weak polymorphism, type checking is still decidable: the principal types algorithm still works. 3

Derivation rules for Weak (ML-style) polymorphism NB! Also the abstraction rule is restricted to λ -types: 1. Curry style: Γ, x : τ M : σ τ a λ -type Γ λx.m : τ σ 2. Church style: Γ, x : τ M : σ τ a λ -type Γ λx:τ.m : τ σ 4

Examples λ2 à la Curry: λx.λy.x : α. β.α β α. λ2 à la Church: λα.λβ.λx:α.λy:β.x : α. β.α β α. λ2 à la Curry: z : α.α α z z : α.α α. λ2 à la Church: z : α.α α λα.z (α α) (z α) : α.α α. But NOT λz.z z :... 5

Derivation rules of λ2 with full (system F-style) polymorphism Typ := TVar (Typ Typ) α.typ 1. Curry style: Γ M : σ Γ M : α.σ α / FV(Γ) Γ M : α.σ Γ M : σ[α := τ] for τ any λ2-type 2. Church style: Γ M : σ Γ M : α.σ α / FV(Γ) Γ λα.m : α.σ Γ Mτ : σ[α := τ] can also occur deeper in a type. for τ any λ2-type With full polymorphism, type checking becomes undecidable! [Wells 1993] 6

Derivation rules of λ2 with full (system F-style) polymorphism Typ := TVar (Typ Typ) α.typ NB: In the abstraction rule all types are λ2-types: 1. Curry style: Γ, x : τ M : σ σ, τ λ2-types Γ λx.m : τ σ 2. Church style: Γ, x : τ M : σ σ, τ λ2-types Γ λx:τ.m : τ σ 7

Erasure from λ2 à la Church to λ2 à la Curry x := x λx:σ.m := λx.m λα.m := M MN := M N Mσ := M Theorem If Γ M : σ in λ2 à la Church, then Γ M : σ in λ2 à la Curry. Theorem If Γ P : σ in λ2 à la Curry, then there is an M such that M P and Γ M : σ in λ2 à la Church. 8

Derivation rules of λ2 with full (system F-style) polymorphism Typ := TVar (Typ Typ) α.typ 1. Curry style: Γ M : σ Γ M : α.σ α / FV(Γ) for τ any λ2-type Γ M : α.σ Γ M : σ[α := τ] 2. Church style: Γ M : σ Γ M : α.σ α / FV(Γ) for τ any λ2-type Γ λα.m : α.σ Γ Mτ : σ[α := τ] Examples valid only with full polymorphism: λ2 à la Curry: λx.λy.x : ( α.α) σ τ. λ2 à la Church: λx:( α.α).λy:σ.xτ : ( α.α) σ τ. 9

Let polymorphism in ML To regain some of the full polymorphism, ML has let polymorphism Γ M : σ Γ, x : σ N : τ for τ a λ -type, σ a λ2-type Γ let x = M in N : τ This allows the formation of a β-redex for σ a polymorphic type. But not λx:σ.n : σ τ (λx:σ.n)m 10

Recall: Important Properties Γ M : σ? Γ M :? TCP TSP? : σ TIP Properties of polymorphic λ-calculus TIP is undecidable, TCP and TSP are equivalent & decidable. TCP à la Church à la Curry ML-style decidable decidable System F-style decidable undecidable With full polymorphism (system F), untyped terms contain too little information to compute the type. 11

Some examples of typing in λ2 Abbreviate := α.α, := α.α α. Curry λ2: λx.xx : Church λ2: λx:.x( )x :. Church λ2: λx:.λα.x(α α)(xα) :. Exercises: Verify that in Church λ2: λx:.x x :. Verify that in Curry λ2: λx.xx : Find a type in Curry λ2 for λx.xxx Find a type in Curry λ2 for λx.(xx)(xx) 12

Formulas-as-types for λ2 There is a formulas-as-types isomorphism between λ2 and second order proposition logic, PROP2 Derivation rules of PROP2: Γ σ Γ α.σ α / FV(Γ) Γ α.σ Γ σ[α := τ] NB This is constructive second order proposition logic: is not derivable. α. β.((α β) α) α Peirce s law 13

Definability of the other connectives σ τ σ τ α.σ := α.α := α.(σ τ α) α := α.(σ α) (τ α) α := β.( α.σ β) β and all the standard constructive derivation rules are derivable. Example ( -elimination): α.(σ τ α) α (σ τ σ) σ σ [σ] 1 τ σ σ τ σ 1 14

Definability of connectives and derivation rules σ τ σ τ α.σ := α.α := α.(σ τ α) α := α.(σ α) (τ α) α := β.( α.σ β) β Example ( -elimination) with λ-terms: [x : σ] 1 M : α.(σ τ α) α λy:τ.x : τ σ Mσ : (σ τ σ) σ λx:σ.λy:τ.x : σ τ σ Mσ(λx:σ.λy:τ.x) : σ So the following term is a witness for the -elimination. 1 λz:σ τ.z σ (λx:σ.λy:τ.x) : (σ τ) σ 15

Data types in λ2 Nat := α.α (α α) α This type uses the encoding of natural numbers as Church numerals n c n := λx.λf.f(... (fx)) n-times f 0 := λα.λx:α.λf:α α.x S := λn:nat.λα.λx:α.λf:α α.f(nαxf) Iteration: if c : σ and g : σ σ, then It c g : Nat σ is defined as λn:nat.nσ c g Then Itcg n = g(...(g c)) (n times g), i.e. Itcg 0 = c and It c g (S x) = g(it c g x) 16

Why is this a good/useful type for the natural numbers? It s the straightforward type for the Church numerals. It represents the type of proofs that a number is inductive in second order predicate logic: 0 : D, S : D D N(x) := P.P 0 ( y.p y P (S y)) P x N(x) iff x is in the smallest set containing 0 and closed under S. E.g. N(0), (N(S 0),..., N(S p (0)). Stripping all first order information (moving from PRED2 to PROP2): N := P.P (P P) P The normal proof of N(S p (0)) is the Church numeral c n under a suitable Curry-Howard embedding. 17

Examples Addition Plus := λn:nat.λm:nat.itms n or Plus := λn:nat.λm:nat.n Nat m S Multiplication Mult := λn:nat.λm:nat.it 0 (λx:nat.plus m x) n Predecessor is difficult! This requires defining primitive recursion in terms of iteration. As a consequence: Pred(n + 1) β n in a number of steps of O(n). 18

Data types in λ2 ctd. List A := α.α (A α α) α the type of lists over A, using the following encoding [a 1, a 2,...,a n ] λx.λf.fa 1 (fa 2 (...(fa n x))) n-times f Nil := λα.λx:α.λf:a α α.x Cons := λa:a.λl:list A.λα.λx:α.λf:A α α.f a(l α xf) Iteration: if c : σ and g : A σ σ, then It c g : List A σ is def. as λl:list A.l σ c g Then, for l = [a 1,...,a n ], Itcg l = g a 1 (...(g a n c)) (n times g) i.e. It c g Nil = c and It c g (Cons a l) = g a (Itcg l) 19

Example Map, given f : σ τ, Mapf : List σ List τ applies f to all elements in a list. Then Map := λf:σ τ.it Nil(λx:σ.λl:List τ.cons(f x)l). Map f Nil = Nil Mapf (Cons a k) = It Nil(λx:σ.λl:List τ.cons(f x)l) (Consak) = (λx:σ.λl:list τ.cons(f x)l)a(mapf k) = Cons(f a)(map f k) 20

Many data-types can be defined in λ2 Product of two data-types: σ τ := α.(σ τ α) α Sum of two data-types: σ+τ := α.(σ α) (τ α) α Unit type: Unit := α.α α Binary trees with nodes in A and leaves in B: Tree A,B := α.(b α) (A α α α) α Exercise: Define inl : σ σ + τ Define the first projection: π 1 : σ τ σ Define join : Tree A,B Tree A,B A Tree A,B 21

Properties of λ2 For λ2 à la Church: 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. 22

Strong Normalization of β for λ2 There are two kinds of β-reductions (λx:σ.m)p β M[x := P] (λα.m)τ β M[α := τ] The second does no harm, so we can just look at λ2 à la Curry Recall the proof for λ : [α] := SN. [σ τ ] := {M N [σ](mn [τ ])}. Question: How to define [ α.σ]?? [ α.σ] := Π X U [σ] α:=x?? 23

Strong Normalization of β for λ2 Question: How to define [ α.σ]?? [ α.σ] := Π X U [σ] α:=x?? What should be U? The collection of all possible interpretations of types (?) Π X U [σ] α:=x gets too big: card(π X U [σ] α:=x ) > card(u) Girard: [ α.σ] should be small Girard: Definition of U. X U [σ] α:=x 24

Strong Normalization of β for λ2 U := SAT, the collection of saturated sets of (untyped) λ-terms. X Λ is saturated if xp 1...P n X (for all x Var, P 1,...,P n SN) X SN If M[x := N] P X and N SN, then (λx.m)n P X. Let ρ : TVar SAT be a valuation of type variables. Define [σ] ρ by: [α] ρ := ρ(α) [σ τ ] ρ := {M N [σ] ρ (MN [τ ] ρ )} [ α.σ] ρ := X SAT [σ] ρ,α:=x 25

Proposition x 1 : τ 1,...,x n : τ n M : σ M[P 1 /x 1,...,P n /x n ] [σ] ρ for all valuations ρ and P 1 [τ 1 ] ρ,...,p n [τ n ] ρ Proof By induction on the derivation of Γ M : σ. Corollary λ2 is SN (Proof: take P 1 to be x 1,..., P n to be x n.) 26

A little bit on semantics Theorem: If λ2 does not have a set-theoretic model! [Reynolds] [σ τ ] := [τ ] [σ] ( set theoretic function space ) then [σ] is a singleton set for every σ. So: in a λ2-model, [σ τ ] must be small. 27