COMP 412, FALL 2000 1 Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

Completeness and Expressiveness
Type Inference David Walker COS 320. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Semantics Static semantics Dynamic semantics attribute grammars
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Introducing Formal Methods, Module 1, Version 1.1, Oct., Formal Specification and Analytical Verification L 5.
1 PROPERTIES OF A TYPE ABSTRACT INTERPRETATER. 2 MOTIVATION OF THE EXPERIMENT § a well understood case l type inference in functional programming à la.
Rigorous Software Development CSCI-GA Instructor: Thomas Wies Spring 2012 Lecture 11.
Type checking © Marcelo d’Amorim 2010.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © Cengage Learning. All rights reserved. CHAPTER 1 SPEAKING MATHEMATICALLY SPEAKING MATHEMATICALLY.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
CS 330 Programming Languages 09 / 13 / 2007 Instructor: Michael Eckmann.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Twelf: The Quintessential Proof Assistant for Language Metatheory Karl Crary Carnegie Mellon University Joint work with Robert Harper and Michael Ashley-Rollman.
Catriel Beeri Pls/Winter 2004/5 type reconstruction 1 Type Reconstruction & Parametric Polymorphism  Introduction  Unification and type reconstruction.
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Catriel Beeri Pls/Winter 2004/5 inductive-revisited 1 Inductive definitions revisited  Generated and Freely generated sets oPattern match, unification.
Type Inference David Walker COS 441. Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs useful.
Type Inference David Walker CS 510, Fall Criticisms of Typed Languages Types overly constrain functions & data polymorphism makes typed constructs.
Semantics with Applications Mooly Sagiv Schrirber html:// Textbooks:Winskel The.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Copyright © Cengage Learning. All rights reserved. CHAPTER 11 ANALYSIS OF ALGORITHM EFFICIENCY ANALYSIS OF ALGORITHM EFFICIENCY.
Notes for Chapter 12 Logic Programming The AI War Basic Concepts of Logic Programming Prolog Review questions.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Introduction to Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Formal Verification Lecture 9. Formal Verification Formal verification relies on Descriptions of the properties or requirements Descriptions of systems.
1 Relational Algebra and Calculas Chapter 4, Part A.
Propositional Calculus CS 270: Mathematical Foundations of Computer Science Jeremy Johnson.
© Kenneth C. Louden, Chapter 11 - Functional Programming, Part III: Theory Programming Languages: Principles and Practice, 2nd Ed. Kenneth C. Louden.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Chapter 3 Part II Describing Syntax and Semantics.
Programming Languages and Design Lecture 3 Semantic Specifications of Programming Languages Instructor: Li Ma Department of Computer Science Texas Southern.
Semantics In Text: Chapter 3.
CSE Winter 2008 Introduction to Program Verification January 31 proofs through simplification.
Copyright © Cengage Learning. All rights reserved.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CS412/413 Introduction to Compilers Radu Rugina Lecture 13 : Static Semantics 18 Feb 02.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Logics for Data and Knowledge Representation ClassL (part 1): syntax and semantics.
1 Proving Properties of Recursive List Functions CS 270 Math Foundations of CS Jeremy Johnson.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Lesson 10 Type Reconstruction
CS5205: Foundation in Programming Languages Type Reconstruction
Introduction to Optimization
Programming Languages and Compilers (CS 421)
Corky Cartwright January 18, 2017
Programming Languages and Compilers (CS 421)
Introduction to Optimization
Logics for Data and Knowledge Representation
Semantics In Text: Chapter 3.
Introduction to Parsing
Introduction to Parsing
MA/CSSE 474 More Math Review Theory of Computation
Introduction to Optimization
This Lecture Substitution model
CSE S. Tanimoto Lambda Calculus
Presentation transcript:

COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use.

COMP 412, FALL Type Systems 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.

COMP 412, FALL Type Systems Foundation: type systems for functional languages • Canonical functional language: the -calculus M ::= c | x | (M M) | ( x M) c  C (a set of constants) x  V (a set of variables) So the -calculus is really a family of languages that differ only in the constants and variables.  A simple type system for the -calculus.  A type has the form  ::= b |    b  B (a set of base types)  Augmented language syntax: M ::= c | x | (M M) | ( x:  M)

COMP 412, FALL Type Systems Typing rules for the (simply) typed -calculus  Each constant c  C has a given type  (c)  Since an expression M inside a -calculus program may contain free variables, type rules keep track of the types of these variables using a type environment   V  T where T denotes the set of types . This type environment is simply a symbol table that records a symbol’s type!  Assume M:    and N: . Then (M N): .  Assume x:  implies that M: . Then ( x:  M):    The process of assigning types to -calculus programs can be rigorously formalized as a natural deduction system where the formal sentences are typing judgements of the form  | M:  and the mechanisms for generating true sentences are  axioms of the form  | M:  and  inference rules of the form  1 | M 1 :  1, …,  N | M N :  N   | M: 

COMP 412, FALL Type Systems Explanation:  Axioms are typing judgements that are manifestly true.  Inference rules produce true consequences given true premises • Common notation for rules  1 | M 1 :  1, …,  N | M N :  N  | M:  Natural deduction rules for (simply typed -calculus) [  is arbitrary]  , x:  | x:    | c:  (c)   | M:   ,  | N:  (  elimination)  | (M N): .  , x:  | M: . (  introduction)  | ( x:  M):   

COMP 412, FALL Type Systems Example -calculus language:  C = I  F where I = {…, -2, -1, 0, 1, 2 …} F = {+, -, *, / }  V = {a, b, … z, aa, … }  B = {int}   (i) = int for i  I;  (f) = int  (int  int)  What is type of ( f: int  int ( g: int  int ( x: int (f (g x)))))? (int  int)  ((int  int)  (int  int)) which is the “curried” form of (int  int)  (int  int)  (int  int)

COMP 412, FALL Type Systems : Sample Typing Proof Show:  | ( f: int  int. ( g: int  int. ( x: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 | ( x:int. (f (g x))): int  int f :int  int | (( g :int  int. ( x:int. (f (g x)))) : (int  int)  (int  int)  | ( f :int  int. ( g :int  int. ( x:int. (f (g x))))) : (int  int)  ((int  int)  (int  int))

COMP 412, FALL Type Systems Question: what is the relationship between the (untyped) -calculus and the (simply) typed -calculus?  Many expressions (and complete programs) in the (untyped) -calculus cannot be typed in the (simply) typed -calculus!  Example: ( x (x x)) x requires a type  =   , 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) -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.

COMP 412, FALL Type Systems 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”    for each subexpression that appears as the head M of an application (M N).  For each constant c, assign it the type  (c). Every subexpression now has a symbolic type.  For each application (M N) where M has symbolic type   , equate  with the symbolic type of N and equate  with the symbolic type of (M N).  For each abstraction ( x M) where x has symbolic type  and M has symbolic type , equate    with the symbolic type of ( x 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.

COMP 412, FALL Type Systems 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 -calculus programs have multiple typings. Consider the program ( x x). It can be assigned the type , for any type   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.

COMP 412, FALL Type Systems Robin Milner’s Creative Leaps  The simple type system for the -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 -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.

COMP 412, FALL Type Systems 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 language: (let (i ( x x)) (((i +) (i 2)) 2)) In a richer language of the family: (let (append ( 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 type- error rather narrowly. A similar theorem holds for Java (we think).