Type Systems COMP 311 Rice University Houston, Texas

Similar documents
Lambda Calculus and Type Inference

1.3. Conditional expressions To express case distinctions like

Polymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy

Type Systems, Type Inference, and Polymorphism

The Typed λ Calculus and Type Inferencing in ML

CS 242. Fundamentals. Reading: See last slide

Chapter 22: Type Reconstruction (Type Inference)

Lambda Calculus and Type Inference

Types and Type Inference

Type Checking and Type Inference

This is already grossly inconvenient in present formalisms. Why do we want to make this convenient? GENERAL GOALS

Polymorphism and Type Inference

Propositional Logic. Part I

What if current foundations of mathematics are inconsistent? Vladimir Voevodsky September 25, 2010

(Refer Slide Time: 4:00)

Introduction to the Lambda Calculus. Chris Lomont

Type Inference. Prof. Clarkson Fall Today s music: Cool, Calm, and Collected by The Rolling Stones

Haskell 98 in short! CPSC 449 Principles of Programming Languages

3.4 Deduction and Evaluation: Tools Conditional-Equational Logic

Types and Type Inference

Com S 541. Programming Languages I

Programming Language Concepts: Lecture 14

(Refer Slide Time: 01:01)

Chapter 11 :: Functional Languages

Lesson 4 Typed Arithmetic Typed Lambda Calculus

Recursive Types and Subtyping

Johns Hopkins Math Tournament Proof Round: Point Set Topology

λ calculus is inconsistent

Programming Languages and Compilers (CS 421)

Logic as a framework for NL semantics. Outline. Syntax of FOL [1] Semantic Theory Type Theory

JOURNAL OF OBJECT TECHNOLOGY

COMP 4161 NICTA Advanced Course. Advanced Topics in Software Verification. Toby Murray, June Andronick, Gerwin Klein

Programming Language Concepts: Lecture 19

Programming Languages

Formal Predicate Calculus. Michael Meyling

The Formal Semantics of Programming Languages An Introduction. Glynn Winskel. The MIT Press Cambridge, Massachusetts London, England

Reasoning About Programs Panagiotis Manolios

Handout 9: Imperative Programs and State

6.001 Notes: Section 8.1

J. Barkley Rosser, 81, a professor emeritus of mathematics and computer science at the University of Wisconsin who had served in government, died

Typed Lambda Calculus for Syntacticians

Chapter 3. Semantics. Topics. Introduction. Introduction. Introduction. Introduction

Formal Systems and their Applications

Lecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference

CSCI.6962/4962 Software Verification Fundamental Proof Methods in Computer Science (Arkoudas and Musser) Chapter p. 1/27

SOFTWARE ENGINEERING DESIGN I

CSCI-GA Scripting Languages

Lambda Calculus. Variables and Functions. cs3723 1

Programming Language Pragmatics

Data Types. Every program uses data, either explicitly or implicitly to arrive at a result.

Chapter 3. Describing Syntax and Semantics

Polymorphism and Type Inference

Logical reconstruction of RDF and ontology languages

Typed Lambda Calculus

Pierce Ch. 3, 8, 11, 15. Type Systems

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

11/6/17. Outline. FP Foundations, Scheme. Imperative Languages. Functional Programming. Mathematical Foundations. Mathematical Foundations

Formal Semantics. Aspects to formalize. Lambda calculus. Approach

Hoare Logic. COMP2600 Formal Methods for Software Engineering. Rajeev Goré

Foundations. Yu Zhang. Acknowledgement: modified from Stanford CS242

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

6.001 Notes: Section 6.1

Formal Languages and Automata

Type Checking. Outline. General properties of type systems. Types in programming languages. Notation for type rules.

Outline. General properties of type systems. Types in programming languages. Notation for type rules. Common type rules. Logical rules of inference

Recursive Definitions, Fixed Points and the Combinator

cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.

Introduction to OCaml

Context-Free Grammars

A computer implemented philosophy of mathematics

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

CSC 501 Semantics of Programming Languages

The University of Nottingham SCHOOL OF COMPUTER SCIENCE A LEVEL 4 MODULE, SPRING SEMESTER MATHEMATICAL FOUNDATIONS OF PROGRAMMING ANSWERS

The discussion of Chapter 1 will be split into two sessions; the first will cover 1.1 and 1.2; the second will cover 1.3, 1.4, and 1.5.

Complexity Theory. Compiled By : Hari Prasad Pokhrel Page 1 of 20. ioenotes.edu.np

Manifest Safety and Security. Robert Harper Carnegie Mellon University

Functional Languages. Hwansoo Han

Type Systems. Pierce Ch. 3, 8, 11, 15 CSE

Pure Lambda Calculus. Lecture 17

Types. Type checking. Why Do We Need Type Systems? Types and Operations. What is a type? Consensus

The semantics of a programming language is concerned with the meaning of programs, that is, how programs behave when executed on computers.

Flang typechecker Due: February 27, 2015

Introduction to Homotopy Type Theory

9.5 Equivalence Relations

Chapter 21: Metatheory of Recursive Types. Induction and Coinduction Finite and Infinite Types/Subtyping Membership Checking

Axiomatic Specification. Al-Said, Apcar, Jerejian

TYPE INFERENCE. François Pottier. The Programming Languages Mentoring ICFP August 30, 2015

Reasoning About Programs Panagiotis Manolios

Lecture 2: SML Basics

The Turing Machine. Unsolvable Problems. Undecidability. The Church-Turing Thesis (1936) Decision Problem. Decision Problems

The design of a programming language for provably correct programs: success and failure

Context-Free Grammars

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Basics

A Theory of Parallel Computation The π-calculus

Formal Methods of Software Design, Eric Hehner, segment 1 page 1 out of 5

Types, Type Inference and Unification

1 Scope, Bound and Free Occurrences, Closed Terms

Introduction to Axiomatic Semantics

Simple Unification-based Type Inference for GADTs

Types. Lecture Notes on. for Part II of the Computer Science Tripos. Prof. Andrew M. Pitts University of Cambridge Computer Laboratory

Transcription:

Rice University Houston, Texas 1

Type Systems for Programming Language were invented by mathematicians before electronic computers were invented. What is a type? A meaningful subset of the set of the domain of data values used in a program. Types are used to describe the intended behavior of program operations. The concept is derived from mathematics; functions have specified domain and co-domains sets which are often called types. In fact, a function with domain A and co-domain B is often said to have type A Æ B. Moveover, the variables used in mathematical formulas typically have specified types Mathematicians informally check that uses of functions and variables in formulas are respected just as they informally check that reasoning used in proofs is sound. In computer programs, some forms of type checking can be formalized and automated. 2

Foundation: type systems for functional languages Canonical functional language: the l-calculus M ::= c x (M M) (lx M) c Œ C (a set of constants) x Œ V (a set of variables) So the l-calculus is really a family of languages that differ only in the constants and variables. A simple type system for the l-calculus. A type has the form t ::= b t Æ t b Œ B (a set of base types) Augmented language syntax: M ::= c x (M M) (lx:t M) 3

Typing rules for the (simply) typed l-calculus Each constant c Œ C has a given type c(c) Since an expression M inside a l-calculus program may contain free variables, type rules keep track of the types of these variables using a type environment G Õ V T where T denotes the set of types t. This type environment is simply a symbol table that records a symbol s type! Assume M:s Æ t and N: s. Then (M N): t. Assume x: s implies that M: t. Then (lx: s M): s Æ t The process of assigning types to l-calculus programs can be rigorously formalized as a natural deduction system where the formal sentences are typing judgements of the form G - M: t and the mechanisms for generating true sentences are axioms of the form G - M: t and inference rules of the form G1 - M1: t1,, GN - MN: tn fi G - M: t 4

Explanation: Axioms are typing judgements that are manifestly true. Inference rules produce true consequences given true premises Common notation for rules G1 - M1: t1,, GN - MN:tN G - M: t Natural deduction rules for (simply typed l-calculus) G, x:t - x: t G - c: c(c) [G is arbitrary] G - M:s Æ t, G - N:s (Æ elimination or application) G - (M N): t. G, x:s - M: t. (Æ introduction or abstraction) G - (lx:s M): s Æ t 5

Example l-calculus language: C = I» F where I = {, -2, -1, 0, 1, 2 } F = {+, -, *, / } V = {a, b, z, aa, } B = {int} c(i) = int for i Œ I; c(f) = int Æ (int Æ int) What is type of (lf: int Æ int (lg: int Æ int (lx: int (f (g x)))))? (int Æ int) Æ ((int Æ int) Æ (int Æ int)) which is the curried form of (int Æ int) (int Æ int) Æ (int Æ int) 6

: Sample Typing Proof Show: (lf: intæint. (lg: intæint. (lx:int. (f (g x))))): (intæint) Æ ((intæint) Æ (intæint)) Tree1: f :intæint, g :intæint, x:int g :intæint, f :intæint, g :intæint, x:int x :int f :intæint, g :intæint, x:int (g x): int Tree2: f :intæint, g :intæint, x:int f :intæint, Tree1 f :intæint, g :intæint, x:int (f (g x)): int f :intæint, g :intæint (l x:int. (f (g x))): intæint f :intæint ((l g :intæint. (l x:int. (f (g x)))) : (intæint) Æ (intæint) (l f :intæint. (l g :intæint. (l x:int. (f (g x))))) : (intæint) Æ ((intæint) Æ (intæint)) 7

Question: what is the relationship between the (untyped) l-calculus and the (simply) typed l-calculus? _ Many expressions (and complete programs) in the (untyped) l-calculus cannot be typed in the (simply) typed l-calculus! Example: (lx (x x)) x requires a type s = s Æ t, but no such type exists _ If the constant operations terminate for all inputs, then every typable program terminates for all inputs! Question: is there a tractable algorithm for determining the type of an expression in the (untyped) l-calculus and rejecting the expression if no such type exists? Yes! The standard algorithm (called the type reconstruction algorithm) is based on a very simple idea: generate a distinct type variable for every subexpression of the given expression M, record the equality constraints between these variables dictated by the typing rule that matches the program context in which the subexpression associated with each variable appears, and solve these constraints. 8

More details: _ Create only one type variable for each distinct program variable; all occurrences of a given variable must have the same type! _ Create a compound type variable s Æ t for each subexpression that appears as the head M of an application (M N). _ For each constant c, assign it the type c(c). Every subexpression now has a symbolic type. _ For each application (M N) where M has symbolic type s Æ t, equate s with the symbolic type of N and equate t with the symbolic type of (M N). _ For each abstraction (lx M) where x has symbolic type s and M has symbolic type t, equate s Æ t with the symbolic type of (lx M). _ Solve the resulting set of equations on symbolic types (which are just symbolic expressions that can be represented as trees) using the unification algorithm (invented by John Alan Robinson, a professor of philosophy at Rice in the 1960 s). The generated solution is a substitution mapping type variables to symbolic types. 9

What is unification? Tree pattern matching where match variables only appear as leaves. A naïve recursive algorithm can be written in a few lines of Scheme or ML. The common practical algorithm relies on a union-find representation of finite sets to record equivalent symbolic types. Every set contains at least one symbolic type that is just a variable. This algorithm runs in essentially linear time. _ A linear algorithm exists but it is not as efficient for problems of practical size as the union-find based algorithm. _ Question: is the reconstructed type unique? Many l-calculus programs have multiple typings. Consider the program (lx x). It can be assigned the type sæs, for any type s Œ T, e.g., (int Æ int), (int Æ int) Æ (int Æ int), The type reconstruction algorithm deftly addresses this problem by returning the most general symbolic typing; all of the possible ground typings (containing no type variables) are substitution instances of this typing. For this reason, the typing produced by the type reconstruction algorithm is called the principal typing (or type) of the program. 10

Robin Milner s Creative Leaps _ The simple type system for the l-calculus is truly onerous because polymorphic functions (those with variables in their principal types) have to be rewritten for each different typing. The original Pascal language suffers from precisely this problem. Milner recognized that a surprisingly useful form of polymorphism could be added to the (simply) typed l-calculus by adding a let construct to the language family. The extension adds one new form to the family syntax M ::= (let (x M) M) Given an expression of the form (let (x M) N) x can be used polymorphically in N without breaking the principal typing property. Type reconstruction can first infer the principal type of M and subsequently use a renamed version of this type (a fresh name for each distinct type variable) for each distinct occurrence of x in N. 11

Robin Milner s Creative Leaps continued Explanation: in essence, the definition of x is treated like a macro (abbreviation) that is replicated for each occurrence of x in N. Of course, a language implementation only needs one copy of the code for M provided that all the different instantiations of the principal type use the same data layout. Example: In our simple l language: (let (i (lx x)) (((i +) (i 2)) 2)) In a richer language of the family: (let (append (l x,y (if (empty? x) y (cons (first x) (append (rest x) y)))) (append (cons (append (cons 1 empty) (cons 2 empty)) empty) empty)) Note: empty and cons are polymorphic constants; let is recursive. _ Milner also realized he could rigorously prove a that typable programs cannot generate certain errors. Type reconstruction proves that run-time type-errors cannot occur --- provided that we define the notion of typeerror rather narrowly. A similar theorem holds for Java (we think). 12