12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.

Slides:



Advertisements
Similar presentations
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Advertisements

Intermediate Code Generation
Semantic Analysis Chapter 6. Two Flavors  Static (done during compile time) –C –Ada  Dynamic (done during run time) –LISP –Smalltalk  Optimization.
Overview of Previous Lesson(s) Over View  Front end analyzes a source program and creates an intermediate representation from which the back end generates.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Chapter 5: Elementary Data Types Properties of types and objects –Data objects, variables and constants –Data types –Declarations –Type checking –Assignment.
Type Checking.
Compiler Construction
CS 330 Programming Languages 10 / 16 / 2008 Instructor: Michael Eckmann.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Denotational Semantics Syntax-directed approach, generalization of attribute grammars: –Define context-free abstract syntax –Specify syntactic categories.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
Chapter 15 Other Functional Languages. Copyright © 2007 Addison-Wesley. All rights reserved. Functional Languages Scheme and LISP have a simple syntax.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
Type Checking in Cool Alex Aiken (Modified by Mooly Sagiv)
Run time vs. Compile time
Programming Language Semantics Mooly SagivEran Yahav Schrirber 317Open space html://
Approaches to Typing Programming Languages Robert Dewar.
CSE S. Tanimoto Syntax and Types 1 Representation, Syntax, Paradigms, Types Representation Formal Syntax Paradigms Data Types Type Inference.
1 Run time vs. Compile time The compiler must generate code to handle issues that arise at run time Representation of various data types Procedure linkage.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
CS 330 Programming Languages 10 / 21 / 2008 Instructor: Michael Eckmann.
Imperative Programming
CSC3315 (Spring 2009)1 CSC 3315 Programming Languages Hamid Harroud School of Science and Engineering, Akhawayn University
Computer Science and Engineering College of Engineering The Ohio State University Interfaces The credit for these slides goes to Professor Paul Sivilotti.
10/14/20151 Programming Languages and Compilers (CS 421) Grigore Rosu 2110 SC, UIUC Slides by Elsa Gunter, based.
CSE 425: Data Types I Data and Data Types Data may be more abstract than their representation –E.g., integer (unbounded) vs. 64-bit int (bounded) A language.
Arithmetic Expressions
MT311 Java Application Development and Programming Languages Li Tak Sing( 李德成 )
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Polymorphism Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Section 7.3.
Structure of Programming Languages Names, Bindings, Type Checking, and Scopes.
Principles of programming languages 6: Types Isao Sasano Department of Information Science and Engineering.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
2/6/20161 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
CS 330 Programming Languages 10 / 23 / 2007 Instructor: Michael Eckmann.
CMSC 330: Organization of Programming Languages Operational Semantics.
6/21/20161 Programming Languages and Compilers (CS 421) Reza Zamani Based in part on slides by Mattox Beckman,
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Type Checking and Type Inference
Programming Languages and Compilers (CS 421)
Semantic Analysis Type Checking
Principles of programming languages 8: Types
Programming Languages and Compilers (CS 421)
Representation, Syntax, Paradigms, Types
Programming Languages and Compilers (CS 421)
Type Systems Terms to learn about types: Related concepts: Type
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Representation, Syntax, Paradigms, Types
Programming Languages and Compilers (CS 421)
Representation, Syntax, Paradigms, Types
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Madhusudan Parthasarathy (madhu) 3112 Siebel Center
Representation, Syntax, Paradigms, Types
Type Systems Terms to learn about types: Related concepts: Type
Programming Languages and Compilers (CS 421)
Programming Languages and Compilers (CS 421)
Presentation transcript:

12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha

12/9/ Why Data Types? Data types play a key role in: Data abstraction in the design of programs Type checking in the analysis of programs Compile-time code generation in the translation and execution of programs Data layout (how many words; which are data and which are pointers) dictated by type

12/9/ Terminology Type: A type t defines a set of possible data values E.g. short in C is {x|  x  } A value in this set is said to have type t Type system: rules of a language assigning types to expressions

12/9/ Types as Specifications Types describe properties Different type systems describe different properties, eg Data is read-write versus read-only Operation has authority to access data Data came from “right” source Operation might or could not raise an exception Common type systems focus on types describing same data layout and access methods

12/9/ Sound Type System If an expression is assigned type t, and it evaluates to a value v, then v is in the set of values defined by t SML, OCAML, Scheme and Ada have sound type systems Most implementations of C and C++ do not

12/9/ Strongly Typed Language When no application of an operator to arguments can lead to a run-time type error, language is strongly typed Eg: ;; Depends on definition of “type error”

12/9/ Strongly Typed Language C++ claimed to be “strongly typed”, but Union types allow creating a value at one type and using it at another Type coercions may cause unexpected (undesirable) effects No array bounds check (in fact, no runtime checks at all) SML, OCAML “strongly typed” but still must do dynamic array bounds checks, runtime type case analysis, and other checks

12/9/ Static vs Dynamic Types Static type: type assigned to an expression at compile time Dynamic type: type assigned to a storage location at run time Statically typed language: static type assigned to every expression at compile time Dynamically typed language: type of an expression determined at run time

12/9/ Type Checking When is op(arg1,…,argn) allowed? Type checking assures that operations are applied to the right number of arguments of the right types Right type may mean same type as was specified, or may mean that there is a predefined implicit coercion that will be applied Used to resolve overloaded operations

12/9/ Type Checking Type checking may be done statically at compile time or dynamically at run time Dynamically typed (aka untyped) languages (eg LISP, Prolog) do only dynamic type checking Statically typed languages can do most type checking statically

12/9/ Dynamic Type Checking Performed at run-time before each operation is applied Types of variables and operations left unspecified until run-time Same variable may be used at different types

12/9/ Dynamic Type Checking Data object must contain type information Errors aren’t detected until violating application is executed (maybe years after the code was written)

12/9/ Static Type Checking Performed after parsing, before code generation Type of every variable and signature of every operator must be known at compile time

12/9/ Static Type Checking Can eliminate need to store type information in data object if no dynamic type checking is needed Catches many programming errors at earliest point Can’t check types that depend on dynamically computed values Eg: array bounds

12/9/ Static Type Checking Typically places restrictions on languages Garbage collection References instead of pointers All variables initialized when created Variable only used at one type Union types allow for work-arounds, but effectively introduce dynamic type checks

12/9/ Type Declarations Type declarations: explicit assignment of types to variables (signatures to functions) in the code of a program Must be checked in a strongly typed language Often not necessary for strong typing or even static typing (depends on the type system)

12/9/ Type Inference Type inference: A program analysis to assign a type to an expression from the program context of the expression Fully static type inference first introduced by Robin Miller in ML Haskle, OCAML, SML all use type inference Records are a problem for type inference

12/9/ Format of Type Judgments A type judgement has the form  |- exp :   is a typing environment Supplies the types of variables and functions  is a set of the form { x : ,... } For any x at most one  such that (x :    ) exp is a program expression  is a type to be assigned to exp |- pronounced “turnstyle”, or “entails” (or “satisfies” or, informally, “shows”)

12/9/ Axioms - Constants  |- n : int (assuming n is an integer constant)  |- true : bool  |- false : bool These rules are true with any typing environment , n are meta-variables

12/9/ Axioms – Variables (Monomorphic Rule) Notation: Let  (x) =  if x :    Note: if such  exits, its unique Variable axiom:  |- x :  if  (x) = 

12/9/ Simple Rules - Arithmetic Primitive operators (   { +, -, *, …}) :  |- e 1 :  1  |- e 2 :  2 (  ):  1   2   3  |- e 1  e 2 :  3 Relations ( ˜  {, =, = }):  |- e 1 :   |- e 2 :   |- e 1 ˜ e 2 :bool For the moment, think  is int

Example: {x:int} |- x + 2 = 3 :bool {x:int} |- x:int {x:int} |- 2:int {x : int} |- x + 2 : bool {x:int} |- 3 :int {x:int} |- x + 2 = 3 : bool 12/9/ What do we need to show first?

Example: {x:int} |- x + 2 = 3 :bool {x:int} |- x:int {x:int} |- 2:int {x : int} |- x + 2 : int {x:int} |- 3 :int {x:int} |- x + 2 = 3 : bool 12/9/ Rel What do we need for the left side?

Example: {x:int} |- x + 2 = 3 :bool {x:int} |- x:int {x:int} |- 2:int {x : int} |- x + 2 : int {x:int} |- 3 :int {x:int} |- x + 2 = 3 : bool 12/9/ Rel AO How to finish?

Example: {x:int} |- x + 2 = 3 :bool {x:int} |- x:int {x:int} |- 2:int {x : int} |- x + 2 : int {x:int} |- 3 :int {x:int} |- x + 2 = 3 : bool 12/9/ Rel AO Const Var Complete Proof (type derivation)

12/9/ Simple Rules - Booleans Connectives  |- e 1 : bool  |- e 2 : bool  |- e 1 && e 2 : bool  |- e 1 : bool  |- e 2 : bool  |- e 1 || e 2 : bool

12/9/ Type Variables in Rules If_then_else rule:  |- e 1 : bool  |- e 2 :   |- e 3 :   |- (if e 1 then e 2 else e 3 ) :   is a type variable (meta-variable) Can take any type at all All instances in a rule application must get same type Then branch, else branch and if_then_else must all have same type

12/9/ Function Application Application rule:  |- e 1 :  1   2  |- e 2 :  1  |- (e 1 e 2 ) :  2 If you have a function expression e 1 of type  1   2 applied to an argument e 2 of type  1, the resulting expression e 1 e 2 has type  2

12/9/ Fun Rule Rules describe types, but also how the environment  may change Can only do what rule allows! fun rule: {x :  1 } +  |- e :  2  |- fun x -> e :  1   2

12/9/ Fun Examples {y : int } +  |- y + 3 : int  |- fun y -> y + 3 : int  int {f : int  bool} +  |- f 2 :: [true] : bool list  |- (fun f -> f 2 :: [true]) : (int  bool)  bool list

12/9/ (Monomorphic) Let and Let Rec let rule:  |- e 1 :  1 {x :  1 } +  |- e 2 :  2  |- (let x = e 1 in e 2 ) :  2 let rec rule: {x:  1 } +  |- e 1 :  1 {x:  1 } +  |- e 2 :  2  |- (let rec x = e 1 in e 2 ) :  2

12/9/ Example Which rule do we apply? ? |- (let rec one = 1 :: one in let x = 2 in fun y -> (x :: y :: one) ) : int  int list

12/9/ Example Let rec rule: 2 {one : int list} |- 1 (let x = 2 in {one : int list} |- fun y -> (x :: y :: one)) (1 :: one) : int list : int  int list |- (let rec one = 1 :: one in let x = 2 in fun y -> (x :: y :: one) ) : int  int list

12/9/ Proof of 1 Which rule? {one : int list} |- (1 :: one) : int list

12/9/ Proof of 1 Application 3 4 {one : int list} |- {one : int list} |- ((::) 1): int list  int list one : int list {one : int list} |- (1 :: one) : int list

12/9/ Proof of 3 Constants Rule {one : int list} |- (::) : int  int list  int list 1 : int {one : int list} |- ((::) 1) : int list  int list

12/9/ Proof of 4 Rule for variables {one : int list} |- one:int list

12/9/ Proof of 2 5 {x:int; one : int list} |- Constant fun y -> (x :: y :: one)) {one : int list} |- 2:int : int  int list {one : int list} |- (let x = 2 in fun y -> (x :: y :: one)) : int  int list

12/9/ Proof of 5 ? {x:int; one : int list} |- fun y -> (x :: y :: one)) : int  int list

12/9/ Proof of 5 ? {y:int; x:int; one : int list} |- (x :: y :: one) : int list {x:int; one : int list} |- fun y -> (x :: y :: one)) : int  int list

12/9/ Proof of {y:int; x:int; one:int list} |- ((::) x):int list  int list |- (y :: one) : int list {y:int; x:int; one : int list} |- (x :: y :: one) : int list {x:int; one : int list} |- fun y -> (x :: y :: one)) : int  int list

12/9/ Proof of 6 Constant Variable {…} |- (::) : int  int list  int list {…; x:int;…} |- x:int {y:int; x:int; one : int list} |- ((::) x) :int list  int list

12/9/ Proof of 7 Pf of 6 [y/x] Variable {y:int; …} |- ((::) y) {…; one: int list} |- :int list  int list one: int list {y:int; x:int; one : int list} |- (y :: one) : int list

12/9/ Curry - Howard Isomorphism Type Systems are logics; logics are type systems Types are propositions; propositions are types Terms are proofs; proofs are terms Function space arrow corresponds to implication; application corresponds to modus ponens

12/9/ Curry - Howard Isomorphism Modus Ponens A  B A B Application  |- e 1 :     |- e 2 :   |- (e 1 e 2 ) : 

12/9/ Mia Copa The above system can’t handle polymorphism as in OCAML No type variables in type language (only meta- variable in the logic) Would need: Object level type variables and some kind of type quantification let and let rec rules to introduce polymorphism Explicit rule to eliminate (instantiate) polymorphism