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

Σχετικά έγγραφα
The Simply Typed Lambda Calculus

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

Lecture 2. Soundness and completeness of propositional logic

Proving with Computer Assistance Lecture 2. Herman Geuvers

C.S. 430 Assignment 6, Sample Solutions

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

2 Composition. Invertible Mappings

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

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

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

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

EE512: Error Control Coding

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

Models for Probabilistic Programs with an Adversary

Instruction Execution Times

derivation of the Laplacian from rectangular to spherical coordinates

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

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

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

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

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

Section 8.3 Trigonometric Equations

CRASH COURSE IN PRECALCULUS

4.6 Autoregressive Moving Average Model ARMA(1,1)

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

Example Sheet 3 Solutions

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

TMA4115 Matematikk 3

Other Test Constructions: Likelihood Ratio & Bayes Tests

Abstract Storage Devices

Math 6 SL Probability Distributions Practice Test Mark Scheme

LAP 2013 Problems in formulating the consecution calculus of contraction less relevant logics

Fractional Colorings and Zykov Products of graphs

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

The challenges of non-stable predicates

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

Jesse Maassen and Mark Lundstrom Purdue University November 25, 2013

Notes on the Open Economy

Formal Semantics. 1 Type Logic

Solutions to Exercise Sheet 5

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

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

Finite Field Problems: Solutions

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

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

6.1. Dirac Equation. Hamiltonian. Dirac Eq.

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

The Probabilistic Method - Probabilistic Techniques. Lecture 7: The Janson Inequality

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

Statistical Inference I Locally most powerful tests

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

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

Concrete Mathematics Exercises from 30 September 2016

Homomorphism in Intuitionistic Fuzzy Automata

If we restrict the domain of y = sin x to [ π, π ], the restrict function. y = sin x, π 2 x π 2

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

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

If we restrict the domain of y = sin x to [ π 2, π 2

Χρειάζεται να φέρω μαζί μου τα πρωτότυπα έγγραφα ή τα αντίγραφα; Asking if you need to provide the original documents or copies Ποια είναι τα κριτήρια

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)

Congruence Classes of Invertible Matrices of Order 3 over F 2

Matrices and Determinants

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ. Ψηφιακή Οικονομία. Διάλεξη 7η: Consumer Behavior Mαρίνα Μπιτσάκη Τμήμα Επιστήμης Υπολογιστών

Chapter 3: Ordinal Numbers

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

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

Tridiagonal matrices. Gérard MEURANT. October, 2008

Block Ciphers Modes. Ramki Thurimella

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

Homework 3 Solutions

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

Math221: HW# 1 solutions

Finitary proof systems for Kozen s µ

Διπλωματική Εργασία του φοιτητή του Τμήματος Ηλεκτρολόγων Μηχανικών και Τεχνολογίας Υπολογιστών της Πολυτεχνικής Σχολής του Πανεπιστημίου Πατρών

9.09. # 1. Area inside the oval limaçon r = cos θ. To graph, start with θ = 0 so r = 6. Compute dr

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

DESIGN OF MACHINERY SOLUTION MANUAL h in h 4 0.

CE 530 Molecular Simulation

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

Jordan Journal of Mathematics and Statistics (JJMS) 4(2), 2011, pp

Section 9.2 Polar Equations and Graphs

Elements of Information Theory

ΕΙΣΑΓΩΓΗ ΣΤΗ ΣΤΑΤΙΣΤΙΚΗ ΑΝΑΛΥΣΗ

ΕΙΣΑΓΩΓΗ ΣΤΟN ΠΡΟΓΡΑΜΜΑΤΙΣΜΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΑΤΡΩΝ ΠΟΛΥΤΕΧΝΙΚΗ ΣΧΟΛΗ ΤΜΗΜΑ ΜΗΧΑΝΙΚΩΝ Η/Υ ΚΑΙ ΠΛΗΡΟΦΟΡΙΚΗΣ

A Note on Intuitionistic Fuzzy. Equivalence Relation

Συστήματα Διαχείρισης Βάσεων Δεδομένων

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

Strain gauge and rosettes

Bounding Nonsplitting Enumeration Degrees

Problem Set 9 Solutions. θ + 1. θ 2 + cotθ ( ) sinθ e iφ is an eigenfunction of the ˆ L 2 operator. / θ 2. φ 2. sin 2 θ φ 2. ( ) = e iφ. = e iφ cosθ.

ΓΡΑΜΜΙΚΟΣ & ΔΙΚΤΥΑΚΟΣ ΠΡΟΓΡΑΜΜΑΤΙΣΜΟΣ

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

Εργαστήριο Ανάπτυξης Εφαρμογών Βάσεων Δεδομένων. Εξάμηνο 7 ο

Main source: "Discrete-time systems and computer control" by Α. ΣΚΟΔΡΑΣ ΨΗΦΙΑΚΟΣ ΕΛΕΓΧΟΣ ΔΙΑΛΕΞΗ 4 ΔΙΑΦΑΝΕΙΑ 1

Space-Time Symmetries

Στο εστιατόριο «ToDokimasesPrinToBgaleisStonKosmo?» έξω από τους δακτυλίους του Κρόνου, οι παραγγελίες γίνονται ηλεκτρονικά.

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

Reminders: linear functions

6. MAXIMUM LIKELIHOOD ESTIMATION

Iterated trilinear fourier integrals with arbitrary symbols

Transcript:

Types and Logics Programming Languages CS442 David Toman School of Computer Science University of Waterloo David Toman (University of Waterloo) Types and Logics 1 / 20

Curry-Howard Isomorphism Idea types = formulæ programs = proofs Implication fragment of Propositional Logic and Simply-typed λ-calculus: π {x : θ} T : θ π λx.t : θ θ ( I) π T 1 : θ θ π T 2 : θ ( E) π (T 1 T 2 ) : θ What is the role of the β-rule? simplification of proofs! Simplification of proofs = cut elimination David Toman (University of Waterloo) Types and Logics 2 / 20

Conjunction Similar for the (, ) fragment: π T 1 : θ 1 π T 2 : θ 2 ( I) π T 1, T 2 : θ 1 θ 2 π T : θ 1 θ 2 π (pr 1 T ) : θ 1 ( E 1 ) we can now can prove, e.g., π T : θ 1 θ 2 π (pr 2 T ) : θ 2 ( E 2 ) ((A B) C) (A (B C)) Idea Conjunctions = Pairs David Toman (University of Waterloo) Types and Logics 3 / 20

Disjunction Can we use disjunction? π T : θ 1 π inr T : θ 1 θ 2 ( I 1 ) π T : θ 2 π inr T : θ 1 θ 2 ( I 2 ) π T : θ 1 θ 2 π T 1 : θ 1 θ π T 2 : θ 2 θ E π case T of inl(x) : (T 1 x) inr(x) : (T 2 x) : θ this is the canonical way of dealing with the inl and inr should be tagged by the disjunction. Idea Disjunctions = Variants David Toman (University of Waterloo) Types and Logics 4 / 20

What Happened to Negation? Is there any use for negation? not really (for program construction) can be introduced in the logic weak negation via a constant for false ( ): θ θ this only allows intuitionistic proofs there is a witness for every valid formula not true in classical logic ( θ θ) David Toman (University of Waterloo) Types and Logics 5 / 20

Type Variables and Quantification Idea We extend the grammar for types with type variables τ ::= ι τ τ α what do the free variables α stand for? only closed terms are valid types! we need quantifiers: τ ::= ι τ τ α α.τ α.τ ( α.τ universal type α.τ existential type) David Toman (University of Waterloo) Types and Logics 6 / 20

Universal Types Idea A universal type can be specialized by substituting a type for the quantified variable How is such a substitution manifested in a program/proof? similar to abstraction/application except this time the actual parameter is a type π, α T : τ π Λα.T : α.τ ( I) π T : α.τ π (T τ ) : [τ /α]τ ( E) needs a generalization of type assignments David Toman (University of Waterloo) Types and Logics 7 / 20

System F [Girard 1972] What is this good for? a clean way to understand complex types Definition (System F) Syntax: terms (T ) and types (τ): Reductions: T ::= x λx.t (T T ) Λα.T (T τ) τ ::= α τ τ α.τ (λx.t T ) [T /x]t (Λα.T τ) [τ/α]t David Toman (University of Waterloo) Types and Logics 8 / 20

How good is F? polymorphic identity: Λα.λx : α.x : α.α α unlike simply typed λ-calculus, it can type λx.(x x): λx : α.α α. (x α.α α x) : ( α.α α) ( α.α α) can it assign a type to Ω? NO! we can still prove strong normalization David Toman (University of Waterloo) Types and Logics 9 / 20

Church-style Encodings Idea We encode standard data types using Church-style terms and still assign types in system F Booleans: true = Λα.λx : α.λy : α.x : α.α α α false = Λα.λx : α.λy : α.y : α.α α α if = λb : α.α α α.λx : α.λy.α.(b α x y) : α not = λb : α.α α α.λβ.λt : β.λf : β.(b β f t) : ( α.α α α) ( α.α α α) David Toman (University of Waterloo) Types and Logics 10 / 20

Encoding of Products Pairs: pair = λx : α.λy : β.λγ.λz : α β γ.(z x y) : α β ( γ.α β γ) proj 1 = λp : Pair.(p α (λx : α.λy : β.x)) : α proj 2 = λp : Pair.(p β (λx : α.λy : β.y)) : β Reductions: proj 1 (pair u v) = (Λγ.λz : γ α β.z u v) α (λx : α.λy : β.x) = (λz : α β α.z u v) (λx : α.λy : β.x) = (λx : α.λy : β.x) u v = (λy : β.u) v = u David Toman (University of Waterloo) Types and Logics 11 / 20

Encoding of Natural Numbers Natural Numbers (Church numerals): Nat = α.α (α α) α Iterator: zero = Λα.λz : α.λs : α α.x succ = λn : Nat.Λα.λz : α.λs : α α.(s (n α z s)) iter = λn : Nat.λu : β.λv : β β.(n β u v) David Toman (University of Waterloo) Types and Logics 12 / 20

Encoding of Polymorphic Lists, Trees,... Lists (of β s): List(β) = α.α (β α α) α Binary Trees (β s in nodes and γ s in leaves): BinTree(β, γ) = α.(γ α) (β α α α) α Trees of branching type β: β-tree = α.α ((β α) α) α David Toman (University of Waterloo) Types and Logics 13 / 20

Existential Types What does α.τ stand for? Idea Allow to hide the type used e.g., in an implementation types used locally in modules/classes How do elements of α.τ look like? {τ, T } : α.τ if T : [τ /α]τ the type τ is hidden from users of α.τ David Toman (University of Waterloo) Types and Logics 14 / 20

Example (ML-like modules) We want a counter module with signature: Count. Count, Count Nat, Count Count with operations zero, get, and inc and we can have an implementation: {Nat, 0, λx.x, λx.x + 1 } {NatList, nil, λx.length x, λx.0 :: x } Given a counter c we would like to be able to write: (Pr 2 c)((pr 3 c)(pr1 1 c)) = 1 : Nat we need to associate the implementation with the type idea (fictional syntax!): open T : β.τ as{α, x} in T David Toman (University of Waterloo) Types and Logics 15 / 20

Typing Rules and Reductions Typing rules: π T : [τ /α]τ π {τ, T } : α.τ ( I) π T : α.τ π {α, x : τ} T : τ π open T as{α, x} in T : τ ( E) Reduction: (open T as{α, x} in T ) {τ, T } [τ/α, T /x]t David Toman (University of Waterloo) Types and Logics 16 / 20

Coding α.τ as an Universal Type the existential type α.τ can be coded: α.τ = β.( α.τ β) β operations (T : [θ/α]τ ): {θ, T } = Λβ.λx : α.τ β.x θ T open T 1 as{α, x} in T 2 : τ = T 1 τ (Λα.λx : τ.t 2 ) reduction: open{θ, T } as{α, x} in T : τ = (Λβ.λx : α.τ β.x θ T ) τ (Λα.λx : τ.t ) = (λx : α.τ τ.x θ T ) (Λα.λx : τ.t ) = (Λα.λx : τ.t ) θ T = [θ/α, T /x]t David Toman (University of Waterloo) Types and Logics 17 / 20

Types as Parameters and Kinds So far we can pass types as parameters to terms what would happen if we parametrized types themselves? Idea Add a level of kinds: K ::= K K ( is a proper type) Now type (functions) can be applied on types: Pair is now properly a type constructor... similarly List :, BinTree :, etc. We can construct System F ω (, ) polymorphism (in addition to (, ) in System F) still strongly normalizing Can we repeat the construction again? no: adding 3rd level = inconsistent (logical) system David Toman (University of Waterloo) Types and Logics 18 / 20

(Almost) Dependent Types OK is there (, ) polymorphism (and what would that be?) Idea Parametrize types by values. for example λx : Nat : Nat n array[n] of Nat true dependent types are proper (saturated) types! and the above is a type constructor (so a coercion is needed) David Toman (University of Waterloo) Types and Logics 19 / 20

Summary (Intuitionistic) Logics give natural explanations of many (if not most) constructs in programming languages differences = variant syntax Several surprising results: provable termination for powerful languages (F ω ) Basis for industry-strength languages: SML/NJ, Haskell polymorphic extensions of JAVA David Toman (University of Waterloo) Types and Logics 20 / 20