Introduction to type systems



Similar documents
3. Mathematical Induction

Parametric Domain-theoretic models of Linear Abadi & Plotkin Logic

Types, Polymorphism, and Type Reconstruction

CHAPTER 3. Methods of Proofs. 1. Logical Arguments and Formal Proofs

CHAPTER 7 GENERAL PROOF SYSTEMS

Summary Last Lecture. Automated Reasoning. Outline of the Lecture. Definition sequent calculus. Theorem (Normalisation and Strong Normalisation)

Deterministic Discrete Modeling

ML for the Working Programmer

This asserts two sets are equal iff they have the same elements, that is, a set is determined by its elements.

Automated Theorem Proving - summary of lecture 1

POLYTYPIC PROGRAMMING OR: Programming Language Theory is Helpful

Testing LTL Formula Translation into Büchi Automata

Adding GADTs to OCaml the direct approach

Termination Checking: Comparing Structural Recursion and Sized Types by Examples

System Description: Delphin A Functional Programming Language for Deductive Systems

A Propositional Dynamic Logic for CCS Programs

CS510 Software Engineering

Predicate Logic Review

Likewise, we have contradictions: formulas that can only be false, e.g. (p p).

Foundational Proof Certificates

Predicate Logic. For example, consider the following argument:

Programming Languages Featherweight Java David Walker

Bindings, mobility of bindings, and the -quantifier

Discrete Mathematics and Probability Theory Fall 2009 Satish Rao, David Tse Note 2

Chapter 3. Cartesian Products and Relations. 3.1 Cartesian Products

Translating a Subset of OCaml into System F. Jonatha Protzenk under the dire ion of François Po ier

6.080/6.089 GITCS Feb 12, Lecture 3

OutsideIn(X) Modular type inference with local assumptions

Andrew Pitts chapter for D. Sangorgi and J. Rutten (eds), Advanced Topics in Bisimulation and Coinduction, Cambridge Tracts in Theoretical Computer

Indiana State Core Curriculum Standards updated 2009 Algebra I

Semantic Errors in SQL Queries: A Quite Complete List

Jieh Hsiang Department of Computer Science State University of New York Stony brook, NY 11794

Linearly Used Effects: Monadic and CPS Transformations into the Linear Lambda Calculus

Integer Operations. Overview. Grade 7 Mathematics, Quarter 1, Unit 1.1. Number of Instructional Days: 15 (1 day = 45 minutes) Essential Questions

Chapter 7: Functional Programming Languages

Unboxed Polymorphic Objects for Functional Numerical Programming

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Verifying security protocols using theorem provers

Journal of Functional Programming, volume 3 number 4, Dynamics in ML

Basic Proof Techniques

Regular Expressions and Automata using Haskell

Relations: their uses in programming and computational specifications

Full and Complete Binary Trees

o-minimality and Uniformity in n 1 Graphs

First Class Overloading via Insersection Type Parameters

Computing exponents modulo a number: Repeated squaring

Undergraduate Notes in Mathematics. Arkansas Tech University Department of Mathematics

Quotient Rings and Field Extensions

COMPUTER SCIENCE TRIPOS

Introducing Formal Methods. Software Engineering and Formal Methods

2. The Language of First-order Logic

The Lean Theorem Prover

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Transformation Techniques for Constraint Logic Programs with Applications to Protocol Verification

INCIDENCE-BETWEENNESS GEOMETRY

Logic in general. Inference rules and theorem proving

Properties of Real Numbers

1 Operational Semantics for While

Continued Fractions and the Euclidean Algorithm

OPERATIONAL TYPE THEORY by Adam Petcher Prepared under the direction of Professor Aaron Stump A thesis presented to the School of Engineering of

WRITING PROOFS. Christopher Heil Georgia Institute of Technology

[Refer Slide Time: 05:10]

def: An axiom is a statement that is assumed to be true, or in the case of a mathematical system, is used to specify the system.

CAs and Turing Machines. The Basis for Universal Computation

AURA: A language with authorization and audit

Monitoring Metric First-order Temporal Properties

Types and Programming Languages

MATHEMATICAL INDUCTION. Mathematical Induction. This is a powerful method to prove properties of positive integers.

WHAT ARE MATHEMATICAL PROOFS AND WHY THEY ARE IMPORTANT?

Rigorous Software Development CSCI-GA

Regular Languages and Finite Automata

DEGREES OF ORDERS ON TORSION-FREE ABELIAN GROUPS

Regular Expressions with Nested Levels of Back Referencing Form a Hierarchy

Non-deterministic Semantics and the Undecidability of Boolean BI

Fixed-Point Logics and Computation

Computability Theory

No Solution Equations Let s look at the following equation: 2 +3=2 +7

Answer Key for California State Standards: Algebra I

5 Homogeneous systems

6.2 Permutations continued

Math 4310 Handout - Quotient Vector Spaces

Constrained optimization.

Philosophical argument

Dependent Types at Work

The Prime Numbers. Definition. A prime number is a positive integer with exactly two positive divisors.

CHAPTER 2. Logic. 1. Logic Definitions. Notation: Variables are used to represent propositions. The most common variables used are p, q, and r.

A Few Basics of Probability

Turing Machines: An Introduction

Observational Program Calculi and the Correctness of Translations

Mathematical Induction

Set theory as a foundation for mathematics

Dedekind s forgotten axiom and why we should teach it (and why we shouldn t teach mathematical induction in our calculus classes)

MATH10040 Chapter 2: Prime and relatively prime numbers

Temporal Logics. Computation Tree Logic

Recursive Algorithms. Recursion. Motivating Example Factorial Recall the factorial function. { 1 if n = 1 n! = n (n 1)! if n > 1

Transcription:

Introduction to type systems p. 1/5 Introductory Course on Logic and Automata Theory Introduction to type systems Polyvios.Pratikakis@imag.fr Based on slides by Jeff Foster, UMD

Introduction to type systems p. 2/5 The need for types Consider the lambda calculus terms: false = λx.λy.x 0 = λx.λy.x (Scott encoding) Everything is encoded using functions One can easily misuse combinators false 0, or if 0 then..., etc... It s no better than assembly language!

Introduction to type systems p. 3/5 Type system A type system is some mechanism for distinguishing good programs from bad Good programs are well typed Bad programs are ill typed or not typeable Examples: 0 + 1 is well typed false 0 is ill typed: booleans cannot be applied to numbers 1 + (if true then 0 else false) is ill typed: cannot add a boolean to an integer

Introduction to type systems p. 4/5 A definition A type system is a tractable syntactic method for proving the absence of certain program behaviors by classifying phrases according to the kinds of values they compute. Benjamin Pierce, Types and Programming Languages

Introduction to type systems p. 5/5 Simply-typed lambda calculus e ::= n x λx : τ.e e e Functions include the type of their argument We don t need this yet, but it will be useful later τ ::= int τ τ τ 1 τ 2 is a the type of a function that, given an argument of type τ 1, returns a result of type τ 2 We say τ 1 is the domain, and τ 2 is the range

Introduction to type systems p. 6/5 Typing judgements The type system will prove judgments of the form Γ e : τ In type environment Γ, expression e has type τ

Introduction to type systems p. 7/5 Type environments A type environment is a map from variables to types (similar to a symbol table) is the empty type environment A closed term e is well-typed if e : τ for some τ Also written as e : τ Γ,x : τ is just like Γ except x has type τ The type of x in Γ is τ For z x, the type of z in Γ,x : τ is the type of z in Γ (we look up variables from the end) When we see a variable in a program, we look in the type environment to find its type

Introduction to type systems p. 8/5 Type rules Γ n : int Γ,x : τ e : τ Γ λx : τ.e : τ τ x dom(γ) Γ x : Γ(x) Γ e 1 : τ τ Γ e 2 : τ Γ e 1 e 2 : τ

Introduction to type systems p. 9/5 Example Assume Γ = ( : int int) dom(γ) Γ : int int Γ 3 : int Γ 3 : int

Introduction to type systems p. 10/5 An algorithm for type checking The above type rules are deterministic For each syntactic form of a term e, only one rule is possible They define a natural type checking algorithm TypeCheck : (type_env expression) type TypeCheck(Γ, n) = int TypeCheck(Γ, x) = if x dom(γ) then Γ(x) else fail TypeCheck(Γ, λx : τ.e) = TypeCheck((Γ,x : τ), e) TypeCheck(Γ, e 1 e 2 ) = let τ 1 = TypeCheck(Γ, e 1 ) in let τ 2 = TypeCheck(Γ, e 2 ) in if dom(τ 1 ) = τ 2 then range(τ 1 ) else fail

Introduction to type systems p. 11/5 Reminder: semantics Small-step, call-by-value semantics If an expression is not a value, and cannot evaluate any more, we say it is stuck, e.g. 0 1 (λx : τ.e 1 ) v 2 e 1 [v 2 /x] e 1 e 1 e 1 e 2 e 1 e 2 e 2 e 2 where v 1 e 2 v 1 e 2 e ::= v x e e v ::= n λx : τ.e

Introduction to type systems p. 12/5 Progress theorem If e : τ then either e is a value, or there exists e such that e e Proof by induction on e Base cases (values): n,λx : τ.e, trivially true Case x: impossible, untypeable in empty environment Inductive case: e 1 e 2 If e 1 is not a value, apply the theorem inductively and then second semantic rule. If e 1 is a value but e 2 is not, similar (using the third semantic rule) If e 1 and e 2 are values, then e 1 has function type, and the only value with a function type is a lambda term, so we apply the first semantic rule

Introduction to type systems p. 13/5 Preservation theorem If e : τ and e e then e : τ Proof by induction on e e In all cases (one for each rule), e has the form e = e 1 e 2 Inversion: from e 1 e 2 : τ we get e 1 : τ τ and e 2 : τ Three semantic rules: Second or third rule: apply induction on e 1 or e 2 respectively, and type-rule for application Remaining case: (λx : τ.e) v e[v/x]

Introduction to type systems p. 14/5 Preservation continued Case (λx : τ.e) v e[v/x] From hypothesis: x : τ e : τ λx : τ.e : τ τ To finish preservation proof, we must prove e[v/x] : τ. Susbstitution lemma

Introduction to type systems p. 15/5 Substitution lemma If Γ v : τ and Γ,x : τ e : τ, then Γ e[v/x] : τ Proof by induction on e (not shown) For lazy (call by name) semantics, substitution lemma is If Γ e 1 : τ and Γ,x : τ e : τ, then Γ e[e 1 /x] : τ

Introduction to type systems p. 16/5 Soundness So far: Progress: If e : τ, then either e is a value, or there exists e such that e e Preservation: If e : τ and e e then e : τ Putting these together, we get soundness If e : τ then either there exists a value v such that e v or e doesn t terminate What does this mean? Well-typed programs don t go wrong Evaluation never gets stuck

Introduction to type systems p. 17/5 Product types (tuples) e ::=... fst e snd e τ ::=... τ τ Γ e 1 : τ Γ e 2 : τ Γ (e 1,e 2 ) : τ τ Γ e : τ τ Γ fst e : τ Γ e : τ τ Γ snd e : τ Alternatively, using function signatures pair : τ τ τ τ fst : τ τ τ snd : τ τ τ

Introduction to type systems p. 18/5 Sum types e ::=... inl τ2 e inr τ1 e (case e of x 1 : τ 1 e 1 x 2 : τ 2 e 2 ) τ ::=... τ + τ Γ e : τ 1 Γ inl τ2 e : τ 1 + τ 2 Γ e : τ 2 Γ inr τ1 e : τ 1 + τ 2 Γ e : τ 1 + τ 2 Γ,x 1 : τ 1 e 1 : τ Γ,x 2 : τ 2 e 2 : τ Γ (case e of x 1 : τ 1 e 1 x 2 : τ 2 e 2 ) : τ

Introduction to type systems p. 19/5 Self application and types Self application is not typeable in this system Γ,x :? x : τ τ Γ,x :? x : τ Γ,x :? x x :... Γ λx :?.x x :... We need a type τ such that τ = τ τ The simply-typed lambda calculus is strongly normalizing Every program has a normal form Or, every program halts!

Introduction to type systems p. 20/5 Recursive types We can type self application if we have a type to represent the solution to equations like τ = τ τ We define the type µα.τ to be the solution to the (recursive) equation α = τ Example: µα.int α

Introduction to type systems p. 21/5 Folding/unfolding recursive types Inferred: We can check type equivalence with the previous definition Standard unification, omit occurs checks (explained later) Alternative method: explicit fold/unfold The programmer inserts explicit fold and unfold operations to expand/contract a level of the type unfold µα.τ = τ[µα.τ/α] fold τ[µα.τ/α] = µα.τ

Introduction to type systems p. 22/5 Fold-based recursive types e ::=... fold e unfold e τ ::=... µα.τ Γ e : τ[µα.τ/α] Γ fold e : µα.τ Γ e : µα.τ Γ unfold e : τ[µα.τ/α]

Introduction to type systems p. 23/5 ML Datatypes Combines fold/unfold-style recursive and sum types Each occurence of a type constructor when producing a value corresponds to inr/inl and (if recursive,) also fold Each occurence of a type constructor in a pattern match corresponds to a case and (if recursive,) at least one unfold

Introduction to type systems p. 24/5 ML Datatypes examples type intlist = Int of int Cons of int intlist is equivalent to µα.int + (int α) ( Int 3) is equivalent to fold (inl int µα.int+(int α) 3)

Introduction to type systems p. 25/5 More ML Datatype examples (Cons (42, (Int 3))) is equivalent to fold (inr int (2, fold (inl int µα.int+(int α) 3))) match e with Int x e 1 Cons x e 2 is equivalent to case (unfold e) of x : int e 1 x : int (µα.int + (int α)) e 2

Introduction to type systems p. 26/5 Discussion In the pure lambda calculus, every term is typeable with recursive types Pure means only including functions and variables (the calculus from the last class) Most languages have some kind of recursive type Used to encode data structures like e.g. lists, trees,... However, usually two recursive types are differentiated by name, even when they define the same structure For example, struct foo { int x; struct foo *next; } is different from struct bar { int x; struct bar *next; }

Introduction to type systems p. 27/5 Intermission Next: Curry-Howard correspondence

Introduction to type systems p. 28/5 Classical propositional logic Formulas of the form φ ::= p φ φ φ φ φ φ Where p P is an atomic proposition, e.g. Socrates is a man Convenient abbreviations: φ means φ φ φ means (φ φ ) (φ φ)

Introduction to type systems p. 29/5 Semantics of classical logic Interpretation m : P {true, false} p m m φ φ m φ φ m φ φ m = m(p) = false = φ m φ m = φ m φ m = φ m φ m Where,, are the standard boolean operations on {true, false}

Introduction to type systems p. 30/5 Terminology A formula φ is valid if φ m = true for all m A formula φ is unsatisfiable if φ m = false for all m Law of excluded middle: Formula φ φ is valid for any φ A proof system attempts to determine the validity of a formula

Introduction to type systems p. 31/5 Proof theory for classical logic Proves judgements of the form Γ φ: For any interpretation, under assumption Γ, φ is true Syntactic deduction rules that produce proof trees of Γ φ: Natural deduction Problem: classical proofs only address truth value, not constructive Example: There are two irrational numbers x and y, such that x y is rational Proof does not include much information

Introduction to type systems p. 32/5 Intuitionistic logic Get rid of the law of excluded middle Notion of truth is not the same A proposition is true, if we can construct a proof Cannot assume predefined truth values without constructed proofs (no either true or false ) Judgements are not expression of truth, they are constructions φ means there is a proof for φ φ means there is a refutation for φ, not there is no proof (φ ) only means the absense of a refutation for φ, does not imply φ as in classical logic

Introduction to type systems p. 33/5 Proofs in intuitionistic logic Γ,φ φ Γ Γ φ Γ φ Γ ψ Γ φ ψ Γ φ ψ Γ φ Γ φ ψ Γ ψ Γ φ Γ φ ψ Γ ψ Γ φ ψ Γ,φ ρ Γ,ψ ρ Γ φ ψ Γ ρ Γ,φ ψ Γ φ ψ Does that resemble anything? Γ φ ψ Γ ψ Γ φ

Introduction to type systems p. 34/5 Curry-Howard correspondence We can mechanically translate formulas φ into type τ for every φ and the reverse E.g. replace with, with +,... If Γ e : τ in simply-typed lambda calculus, and τ translates to φ, then range(γ) φ in intuistionistic logic If Γ φ in intuitionistic logic, and φ translates to τ, then there exists e and Γ such that range(γ ) = Γ and Γ e : τ Proof by induction on the derivation Γ φ Can be simplified by fixing the logic and type languages to match

Introduction to type systems p. 35/5 Consequences Lambda terms encode proof trees Evaluation of lambda terms is proof simplification Automated proving by trying to construct a lambda term with the wanted type Verifying a proof is typechecking Increased trust in complicated proofs when machine-verifiable Proof-carrying code Certifying compilers

Introduction to type systems p. 36/5 So far... We have discussed simple types Integers, functions, pairs, unions Extension for recursive types Type systems have nice properties Type checking is straightforward (needs annotations) Well-typed programs don t go wrong (get stuck) But..., we cannot type-check all good programs Sound, but not complete Might reject correct programs (have false warnings)

Introduction to type systems p. 37/5 Next: improving types How can we build more flexible type systems? More programs type check Type checking is still tractable How can we reduce the annotation burden? Type inference: infer annotations automatically

Introduction to type systems p. 38/5 Parametric polymorphism Observation: λx.x returns its argument exactly without any constraints on the type of x The identity function works for any argument type We can express this with universal quantification λx.x : α.α α For any type α the identity function has type α α This is also known as parametric polymorphism

Introduction to type systems p. 39/5 System F: annotated polymorphism We extend the previous system: τ ::= int τ τ α α.τ e ::= n x λx.e e e Λα.e e[τ] We add polymorphic types, and we add explicit type abstraction (generalization over all α)... Explicit creation of values ov polymorphic types... and type application (instantiation of generalized types) Explicitly marks code locations where a value of polymorphic type is used System by Girard, concurrently Reynolds

Introduction to type systems p. 40/5 Defining polymorphic functions Polymorphic functions map types to terms Normal functions map terms to terms Examples Λα.λx : α.x : α.α α Λα.Λβ.λx : α.λy : β.x : α. β.α β α Λα.Λβ.λx : α.λy : β.y : α. β.α β β

Introduction to type systems p. 41/5 Instantiation When we use a parametric polymorphic type, we apply or instantiate it with a particular type In System F this is done by hand (Λα.λx : α.x)[τ 1 ] : τ 1 τ 1 (Λα.λx : α.x)[τ 2 ] : τ 2 τ 2 This is where the term parametric comes from The type α.α α is a function in the domain of types It is given a parameter at instantiation time

Introduction to type systems p. 42/5 Type rules Γ,α e : τ Γ Λα.e : α.τ Γ e : α.τ Γ e[τ ] : τ[τ /α] Notice that there are no constructs for manipulating values of polymorphic type This justifies instantiation with any type that s what for all means We add α to Γ: we use this to ensure types are well-formed

Introduction to type systems p. 43/5 Small-step semantics rules (Λα.e)[τ] e[τ/α] e e e[τ] e [τ] We have to extend the definition of substitution for types

Introduction to type systems p. 44/5 Free variables (again) We need to perform substitutions on quantified types Just like with lambda calculus, we need to think about free variables and avoid capturing with substitution Define the free variables of a type FV (α) = {α} FV (c) = FV (τ τ ) = FV (τ) FV (τ ) FV ( α.τ) = FV (τ) \ {α}

Introduction to type systems p. 45/5 Substitution (again) We define τ[u/α] as α[u/α] = u β[u/α] = β where β α (τ τ )[u/α] = τ[u/α] τ [u/α] ( β.τ)[u/α] = β.(τ[u/α]) where β α β / FV (u) We define e[u/α] as (λx : τ.e)[u/α] = (λx : τ[u/α].e[u/α] (Λβ.e)[u/α] = Λβ.e[u/α] where β α β / FV (u) (e 1 e 2 )[u/α] = e 1 [u/α] e 2 [u/α] x[u/α] = x n[u/α] = n

Introduction to type systems p. 46/5 Type inference Consider the simply typed lambda calculus with integers e ::= n x λx.τ e e Simple, no polymorphism Type inference: Given a bare term (no annotations), can we reconstruct a valid typing if there is one?

Introduction to type systems p. 47/5 Type language Problem: consider the rule for functions Γ,x : τ e : τ Γ λx : τ.e : τ τ Without type annotations on λ terms, where do we find τ? We use type variables in place of as-yet-unknown types τ ::= α int τ τ We generate equality constraints τ = τ among types and type variables We solve the constraints to compute a typing

Introduction to type systems p. 48/5 Type inference rules Γ n : int x dom(γ) Γ x : Γ(x) Γ,x : α e : τ α fresh Γ λx.e : α τ Γ e 1 : τ 1 Γ e 2 : τ 2 τ 1 = τ 2 β β fresh Γ e 1 e 2 : β

Introduction to type systems p. 49/5 Example Γ,x : α x : α Γ (λx.x) : α α Γ 3 : int α α = int β Γ (λx.x) 3 : β We collect all constraints appearing in the derivation into a set C to be solved Here C contains α α = int β Solution: α = β = int So, this program is typeable We can create a typing by replacing variables in the derivation

Introduction to type systems p. 50/5 Solving equality constraints We can solve the equality constraints using the following rewrite rules, which reduce a larger set of constraints to a smaller set C {int = int} C C {α = τ} C[τ/α] C {τ = α} C[τ/α] C {τ 1 τ 2 = τ 1 τ 2 } C {τ 1 = τ 1 } {τ 2 = τ 2 } C {int = τ 1 τ 2 } unsatisfiable C {τ 1 τ 2 = int} unsatisfiable

Introduction to type systems p. 51/5 Termination We can prove that the constraint solving algorithm terminates For each rewriting rule, either We reduce the size of the constraint set We reduce the number of arrow constructors in the constraint set As a result, the constraint always gets smaller and eventually becomes empty A similar argument is made for strong normalization of the simply-typed lambda calculus

Introduction to type systems p. 52/5 Occurs check We don t have recursive types, so we don t infer them In the operation C[τ/α] we require that α / FV (τ) In practice, it may be better to allow α FV (τ) and perform an occurs check at the end Could be difficult to implement Unification might not terminate without occurs check

Introduction to type systems p. 53/5 Unifying a variable and a type Computing C[τ/α] by substitution is inefficient Instead, use a union-find data structure to represent equal types The terms are in a union-find forest When a variable and a term are equated, we union them so they have the same equivalence class If there is a concrete type in an equivalence class, use it to represent the class Solution is the representative of each class at the end

Introduction to type systems p. 54/5 Example α = int β γ = int int α = γ

Introduction to type systems p. 55/5 Unification The process of finding a solution to a set of equality constraints is called unification Original algorithm by Robinson (inefficient) Often written in different form (algorithm W) Usually solved on-line as type rules are applied

Introduction to type systems p. 56/5 Discussion The algorithm we ve given finds the most general type of a term Any other type is more specific Formally, any other valid type can be created from the most general type by applying a substitution on type variables All this is for a monomorphic type system, no quantification Variables α stand for some particular type

Introduction to type systems p. 57/5 Inference for polymorphism We would like to have the power of System F, and the ease of use of type inference In short: given an untyped lambda calculus term, can we discover the annotations necessary for typing the term in System F, if such a typing is possible? Unfortunately, no. This problem has been shown to be undecidable. Can we at least perform some kind of parametric polymorphism Yes, a sweet spot was found by Hindley and Milner Abstraction at let-statements, instantiation on each variable use Used in ML

Introduction to type systems p. 58/5 Curry-Howard extension Does the equivalent logic get extended using polymorphism? More than enough to encode first-order logic A subset of System F (with several restrictions) is equivalent to First Order Logic Actually, we can use System F as a common language for polymorphic lambda calculus and for second-order propositional logic!

Introduction to type systems p. 59/5 Other extensions Higher-order logic is encodable in System F ω Further extension using the Calculus of Constructions can encode even more complex logics Inductive types can encode algebraic data types, inductive proofs Combining the calculus of constructions with inductive types: The Calculus of Inductive Constructions: Proof language used in the Coq theorem prover Can encode and reason about other logics, type systems Can also be used to reason about Classical Logic Just make the law of excluded middle an axiom