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

Size: px
Start display at page:

Transcription

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

2 Goals Define the simple language of expressions A small subset of Lisp, with minor modifications Define the type system of this language Mathematical definition using standard machinery: inference rules from formal logic Understand the relationship between semantics and type systems And more generally, the role of types in programming languages 2

3 A Simple Subset of Lisp (with minor tweaks) <E> ::= const T F (IF <E> THEN <E> ELSE <E>) (INT <E>) (EQ <E> <E>) (PLUS <E> <E>) (LESS <E> <E>) const represents a numeric atom for a natural number (0, 1, 2, ) T and F are literal atoms for true and false (not like real Lisp) IF-THEN-ELSE is part of real Lisp. In our fake language, if the first expression evaluates to T, we evaluate the second expression; if the first expression evaluates to F, we evaluate the third expression; otherwise undefined INT, EQ, PLUS, LESS were defined in Project 3 3

4 Other Ways to Define the Syntax Inductive definition: the smallest set S such that { const, T, F } S if E S, then (INT E) S if E 1,E 2 S, then { (EQ E 1 E 2 ), (PLUS E 1 E 2 ), (LESS E 1 E 2 ) } S if E 1,E 2,E 3 S, then (IF E 1 THEN E 2 ELSE E 3 ) S Now the same thing, written as inference rules (formal logic) const S T S F S axioms (no premises) E S E 1 S E 2 S E 1 S E 2 S E 3 S (INT E) S (EQ E 1 E 2 ) S (IF E 1 THEN E 2 ELSE E 3 ) S 4 If we have established the premises (above the line), we can derive the conclusion (below the line)

5 Language Semantics Defined in Project 3 through function eval Running a program : expression evaluates to value, which is an atom T or F or const (IF F THEN 0 ELSE 31) evaluates to 31 (INT (PLUS 5 6)) evaluates to T Run-time error: when eval(e) is undefined (PLUS (EQ 1 2) 5) (IF 8 THEN 1 ELSE 2) Can we prevent some of these errors? 5

6 Type System and Type Checking A type system can help us prove that certain programs are good without running them If a program is well-typed, it will not go wrong at run time Guarantees are only for some run-time errors, not all: E.g. cannot assure the absence of division by zero or array index out of bounds or CAR applied to an empty list - they depend on particular values from a type But can catch PLUS with a boolean operand error 6

7 Typed Expressions Goal: without evaluating an expression, can we guarantee that its evaluation will not produce a runtime error? (static a.k.a. compile-time analysis) Solution: define types, and establish a relationship between expressions and types For our simple language Type Bool = set of all expressions that evaluate to T or F Type Nat = set of all expressions that evaluate to const To determine that an expression E has type T (i.e., E T), will only look at the structure of E but will not evaluate E 7

8 Typing Relation Relation : S { Bool, Nat } E : T is just another notation for E T T : Bool F : Bool const : Nat E 1 : Bool E 2 : T E 3 : T (IF E 1 THEN E 2 ELSE E 3 ) : T E 1 : T 1 E 2 : T 2 (EQ E 1 E 2 ) : Bool E : T (INT E) : Bool E 1 : Nat E 2 : Nat (PLUS E 1 E 2 ) : Nat E 1 : Nat E 2 : Nat (LESS E 1 E 2 ) : Bool 8

9 Example: Typing Derivation (IF (INT 5) THEN 6 ELSE (PLUS 7 8)) :? 5 : Nat (INT 5) : Bool 6 : Nat 7 : Nat 8 : Nat (PLUS 7 8) : Nat (IF (INT 5) THEN 6 ELSE (PLUS 7 8)) : Nat This structure is a derivation tree: the leaves are instances of axioms, the inner nodes are instances of non-axiom rules 9

10 More on the Typing Relation E is typable (or well-typed) if exists T such that E : T In our type system, each well-typed expression has one type; in general, an expression may have multiple types (e.g., when the type system has subtypes) Safety (a.k.a. soundness) of a type system: a welltyped program will not have a run-time error For our type system: for a well- typed E : T we know that eval(e) is defined and is a value of type T This property does not work in the other direction: an expression which is not well-typed may or may have a run-time error (conservative static analysis) (IF (INT 33) THEN 44 ELSE (PLUS T F)) is not well-typed but runs fine 10

11 Lists <E> ::= NIL (NULL <E>) (CONS <E> <E>) (CAR <E>) (CDR <E>) Semantics: a value now can also be a list value Either NIL, or CONS applied to a value and a list value Typing: need to add list types of the form List (T) Example: List (List (Nat)) Note that lists will be homogeneous all elements will have the same type. This is not the case for real Lisp lists there are heterogeneous. 11

12 Typing Relation NIL : List (T) for any T E 1 : T E 2 : List (T) (CONS E 1 E 2 ) : List (T) E : List (T) E : List (T) (CAR E) : T E : List (T) (CDR E) : List(T) (NULL E) : Bool Example 1: (CONS (NULL NIL) (CONS F NIL)) Example 2: (CONS F T) Example 3: (NULL NIL) Example 4: (CONS (NULL NIL) (CONS 8 NIL)) 12

13 Brief Overview of Terminology Polymorphism Statically vs dynamically typed languages Type safety vs language safety

14 Polymorphism Poly = many, morph = form A piece of code has multiple types Example 1: subtype polymorphism An expression has multiple types Typical for object-oriented languages (class Y extends X) Example 2: parametric polymorphism E.g. f(x)=x has types Bool Bool, Nat Nat, Use a type parameter T; define type type T T Generics in C++ and Java e.g. Map<K,V> ML and similar functional languages Example 3: coercion and overloading 14

15 15 Coercion and Overloading Automatic coercion (conversion) to another type is performed silently: e.g. in Java byte can be widened to short, int, long, float, or double E.g. in assignment conversion, the right-hand-side is converted to the type of the left-hand-side var E.g. numeric promotion converts operands of a numeric operator to a common type, e.g. for + Overloading: multiple definitions of the same name e.g. in Java name + has several types: double double double long long long double double long long float float float int int int float float int int

16 Terminology Statically typed language: expressions have static (compile-time) types, and we do static type checking Goal: prove the absence of certain type-related bad behaviors before running the program Declared types: C/C++/Java/ (programmer gives types) Inferred types: ML/Haskell no programmer-declared types; compiler infers, based on use in operators/functions Type safety: all bad behaviors of certain type-related kinds are excluded - e.g., Java, but not C (due to arbitrary typecasting in C) Dynamically typed language: run-time checks to catch type-related bad behaviors (e.g. Lisp, Perl) E.g., in our projects, PLUS must be applied to numbers 16

17 Terminology Want more than static type safety want language safety Cannot break the abstractions of the language (typerelated and otherwise); e.g. no buffer overflows, seg faults, return address overriding, garbage values, etc. Example: C is unsafe for many reasons, one of which is the lack of type safety: e.g., double pi = 3.14; int* ptr = (int*) π int x = *ptr; Other reasons: null pointers lead to seg faults (OS concept, not PL concept); buffer overflows lead to stack smashing & garbage values Example: Java is safe combination of static type safety & run-time checks. Static type safety ensures that an well-typed program will not do type-related bad things. Run-time checks catch things that cannot be caught statically via types: null pointers, array index out of bounds, etc. Example: Lisp is safe checks for type-related correctness ( operands of PLUS must be numbers ) and special bad values (e.g. divide by 0) 17

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

Type Systems Pierce Ch. 3, 8, 11, 15 CSE 6341 1 A Simple Language ::= true false if then else 0 succ pred iszero Simple untyped expressions Natural numbers encoded as succ succ

### CS558 Programming Languages

CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

### COS 320. Compiling Techniques

Topic 5: Types COS 320 Compiling Techniques Princeton University Spring 2016 Lennart Beringer 1 Types: potential benefits (I) 2 For programmers: help to eliminate common programming mistakes, particularly

### Outline. Introduction Concepts and terminology The case for static typing. Implementing a static type system Basic typing relations Adding context

Types 1 / 15 Outline Introduction Concepts and terminology The case for static typing Implementing a static type system Basic typing relations Adding context 2 / 15 Types and type errors Type: a set of

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

Every program uses data, either explicitly or implicitly to arrive at a result. Data in a program is collected into data structures, and is manipulated by algorithms. Algorithms + Data Structures = Programs

### Type Checking and Type Inference

Type Checking and Type Inference Principles of Programming Languages CSE 307 1 Types in Programming Languages 2 Static Type Checking 3 Polymorphic Type Inference Version: 1.8 17:20:56 2014/08/25 Compiled

### CSCI-GA Scripting Languages

CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

### Functional Programming. Pure Functional Programming

Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

### Principles of Programming Languages

Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

### CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CS 430 Spring 2015 Mike Lam, Professor Data Types and Type Checking Type Systems Type system Rules about valid types, type compatibility, and how data values can be used Benefits of a robust type system

### Type Systems. Today. 1. Organizational Matters. 1. Organizational Matters. Lecture 1 Oct. 20th, 2004 Sebastian Maneth. 1. Organizational Matters

Today Type Systems 1. Organizational Matters 2. What is this course about? 3. Where do types come from? 4. Def. of the small language Expr. Its syntax and semantics. Lecture 1 Oct. 20th, 2004 Sebastian

### CSCE 314 Programming Languages. Type System

CSCE 314 Programming Languages Type System Dr. Hyunyoung Lee 1 Names Names refer to different kinds of entities in programs, such as variables, functions, classes, templates, modules,.... Names can be

### Note 3. Types. Yunheung Paek. Associate Professor Software Optimizations and Restructuring Lab. Seoul National University

Note 3 Types Yunheung Paek Associate Professor Software Optimizations and Restructuring Lab. Seoul National University Topics Definition of a type Kinds of types Issues on types Type checking Type conversion

### Motivation for typed languages

Motivation for typed languages Untyped Languages: perform any operation on any data. Example: Assembly movi 5 r0 // Move integer 5 (some representation) to r0 addf 3.6 r0 // Treat bit representation in

### CS558 Programming Languages

CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016 Values and Types We divide the universe of values according to types A type is a set of values and a

### CS558 Programming Languages

CS558 Programming Languages Winter 2018 Lecture 7b Andrew Tolmach Portland State University 1994-2018 Dynamic Type Checking Static type checking offers the great advantage of catching errors early And

### (Advanced) topics in Programming Languages

Spring 2012 (Advanced) topics in Programming Languages Instructor: Mooly Sagiv TA: Shachar Itzhaky http://www.cs.tau.ac.il/~msagiv/courses/apl12.html Inspired by John Mitchell CS 242 Compilation course

### G Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University

G22.2110-001 Programming Languages Spring 2010 Lecture 6 Robert Grimm, New York University 1 Review Last week Function Languages Lambda Calculus SCHEME review 2 Outline Promises, promises, promises Types,

### Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

### Lecture 7: Type Systems and Symbol Tables. CS 540 George Mason University

Lecture 7: Type Systems and Symbol Tables CS 540 George Mason University Static Analysis Compilers examine code to find semantic problems. Easy: undeclared variables, tag matching Difficult: preventing

### Topics Covered Thus Far CMSC 330: Organization of Programming Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages Names & Binding, Type Systems Programming languages Ruby Ocaml Lambda calculus Syntax specification Regular expressions Context free

### Topics Covered Thus Far. CMSC 330: Organization of Programming Languages. Language Features Covered Thus Far. Programming Languages Revisited

CMSC 330: Organization of Programming Languages Type Systems, Names & Binding Topics Covered Thus Far Programming languages Syntax specification Regular expressions Context free grammars Implementation

### Programming Languages

CSE 230: Winter 2008 Principles of Programming Languages Ocaml/HW #3 Q-A Session Push deadline = Mar 10 Session Mon 3pm? Lecture 15: Type Systems Ranjit Jhala UC San Diego Why Typed Languages? Development

### Types and Type Inference

Types and Type Inference Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on the course homepage Outline General discussion

### CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages Type Systems, Names and Binding CMSC 330 - Spring 2013 1 Topics Covered Thus Far! Programming languages Ruby OCaml! Syntax specification Regular expressions

### INF 212 ANALYSIS OF PROG. LANGS Type Systems. Instructors: Crista Lopes Copyright Instructors.

INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright Instructors. What is a Data Type? A type is a collection of computational entities that share some common property Programming

### Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

### TYPES, VALUES AND DECLARATIONS

COSC 2P90 TYPES, VALUES AND DECLARATIONS (c) S. Thompson, M. Winters 1 Names, References, Values & Types data items have a value and a type type determines set of operations variables Have an identifier

### INF 212/CS 253 Type Systems. Instructors: Harry Xu Crista Lopes

INF 212/CS 253 Type Systems Instructors: Harry Xu Crista Lopes What is a Data Type? A type is a collection of computational entities that share some common property Programming languages are designed to

### HANDLING NONLOCAL REFERENCES

SYMBOL TABLE A symbol table is a data structure kept by a translator that allows it to keep track of each declared name and its binding. Assume for now that each name is unique within its local scope.

### The Compiler So Far. CSC 4181 Compiler Construction. Semantic Analysis. Beyond Syntax. Goals of a Semantic Analyzer.

The Compiler So Far CSC 4181 Compiler Construction Scanner - Lexical analysis Detects inputs with illegal tokens e.g.: main 5 (); Parser - Syntactic analysis Detects inputs with ill-formed parse trees

### Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

### Simply-Typed Lambda Calculus

#1 Simply-Typed Lambda Calculus #2 Back to School What is operational semantics? When would you use contextual (small-step) semantics? What is denotational semantics? What is axiomatic semantics? What

### Formal Semantics of Programming Languages

Formal Semantics of Programming Languages Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson http://www.daimi.au.dk/~bra8130/wiley_book/wiley.html Benefits of formal

### Programming Languages and Compilers (CS 421)

Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC http://courses.engr.illinois.edu/cs421 Based in part on slides by Mattox Beckman, as updated by Vikram Adve and Gul Agha 10/3/17

### Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Introduction to Typed Racket The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Getting started Find a machine with DrRacket installed (e.g. the

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

Types Type checking What is a type? The notion varies from language to language Consensus A set of values A set of operations on those values Classes are one instantiation of the modern notion of type

### 22c:111 Programming Language Concepts. Fall Types I

22c:111 Programming Language Concepts Fall 2008 Types I Copyright 2007-08, The McGraw-Hill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified

CS313D: ADVANCED PROGRAMMING LANGUAGE Computer Science department Lecture 2 : C# Language Basics Lecture Contents 2 The C# language First program Variables and constants Input/output Expressions and casting

### G Programming Languages - Fall 2012

G22.2110-003 Programming Languages - Fall 2012 Lecture 6 Thomas Wies New York University Review Last week Functional Languages Lambda Calculus Scheme Outline Types Sources: PLP, ch. 7 Types What is a type?

### 301AA - Advanced Programming [AP-2017]

301AA - Advanced Programming [AP-2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP-2017-19: Type

### Intermediate Code Generation Part II

Intermediate Code Generation Part II Chapter 6: Type checking, Control Flow Slides adapted from : Robert van Engelen, Florida State University Static versus Dynamic Checking Static checking: the compiler

### Programming Languages

Programming Languages Types CSCI-GA.2110-001 Summer 2011 What is a type? A type consists of a set of values The compiler/interpreter defines a mapping of these values onto the underlying hardware. 2 /

### Formal Syntax and Semantics of Programming Languages

Formal Syntax and Semantics of Programming Languages Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson http://www.daimi.au.dk/~bra8130/wiley_book/wiley.html The While

### CS 330 Lecture 18. Symbol table. C scope rules. Declarations. Chapter 5 Louden Outline

CS 0 Lecture 8 Chapter 5 Louden Outline The symbol table Static scoping vs dynamic scoping Symbol table Dictionary associates names to attributes In general: hash tables, tree and lists (assignment ) can

### CPSC 3740 Programming Languages University of Lethbridge. Data Types

Data Types A data type defines a collection of data values and a set of predefined operations on those values Some languages allow user to define additional types Useful for error detection through type

### Types. What is a type?

Types What is a type? Type checking Type conversion Aggregates: strings, arrays, structures Enumeration types Subtypes Types, CS314 Fall 01 BGRyder 1 What is a type? A set of values and the valid operations

### MIDTERM EXAMINATION - CS130 - Spring 2005

MIDTERM EAMINATION - CS130 - Spring 2005 Your full name: Your UCSD ID number: This exam is closed book and closed notes Total number of points in this exam: 231 + 25 extra credit This exam counts for 25%

### Typed Lambda Calculus. Chapter 9 Benjamin Pierce Types and Programming Languages

Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages t ::= x x. t t t Call-by-value small step perational Semantics terms variable v ::= values abstraction x. t abstraction values

### CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

### Principles of Programming Languages 2017W, Functional Programming

Principles of Programming Languages 2017W, Functional Programming Assignment 3: Lisp Machine (16 points) Lisp is a language based on the lambda calculus with strict execution semantics and dynamic typing.

### Programming Languages

Programming Languages Types CSCI-GA.2110-003 Fall 2011 What is a type? An interpretation of numbers Consists of a set of values The compiler/interpreter defines a mapping of these values onto the underlying

### Formal Semantics of Programming Languages

Formal Semantics of Programming Languages Mooly Sagiv Reference: Semantics with Applications Chapter 2 H. Nielson and F. Nielson http://www.daimi.au.dk/~bra8130/wiley_book/wiley.html Benefits of formal

### Type Systems, Type Inference, and Polymorphism

6 Type Systems, Type Inference, and Polymorphism Programming involves a wide range of computational constructs, such as data structures, functions, objects, communication channels, and threads of control.

### CS558 Programming Languages

CS558 Programming Languages Fall 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Type Inference Some statically typed languages, like ML (and to a lesser extent Scala), offer alternative

### Semantic Analysis Type Checking

Semantic Analysis Type Checking Maryam Siahbani CMPT 379 * Slides are modified version of Schwarz s compiler course at Stanford 4/8/2016 1 Type Checking Type errors arise when operations are performed

### Programming Languages Lecture 15: Recursive Types & Subtyping

CSE 230: Winter 2008 Principles of Programming Languages Lecture 15: Recursive Types & Subtyping Ranjit Jhala UC San Diego News? Formalize first-order type systems Simple types (integers and booleans)

### Semantic Analysis. How to Ensure Type-Safety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413

CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 13: Types and Type-Checking 19 Feb 07 Semantic Analysis Last time: Semantic errors related to scopes Symbol tables Name resolution This lecture:

### G Programming Languages - Fall 2012

G22.2110-003 Programming Languages - Fall 2012 Lecture 3 Thomas Wies New York University Review Last week Names and Bindings Lifetimes and Allocation Garbage Collection Scope Outline Control Flow Sequencing

### C Language Part 1 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

C Language Part 1 (Minor modifications by the instructor) References C for Python Programmers, by Carl Burch, 2011. http://www.toves.org/books/cpy/ The C Programming Language. 2nd ed., Kernighan, Brian,

### Compiler Construction

Compiler Construction Thomas Noll Software Modeling and Verification Group RWTH Aachen University https://moves.rwth-aachen.de/teaching/ss-16/cc/ Recap: Static Data Structures Outline of Lecture 18 Recap:

### Functional Languages. Hwansoo Han

Functional Languages Hwansoo Han Historical Origins Imperative and functional models Alan Turing, Alonzo Church, Stephen Kleene, Emil Post, etc. ~1930s Different formalizations of the notion of an algorithm

### CS321 Languages and Compiler Design I Winter 2012 Lecture 13

STATIC SEMANTICS Static Semantics are those aspects of a program s meaning that can be studied at at compile time (i.e., without running the program). Contrasts with Dynamic Semantics, which describe how

### Recursive Types and Subtyping

Recursive Types and Subtyping #1 One-Slide Summary Recursive types (e.g., list) make the typed lambda calculus as powerful as the untyped lambda calculus. If is a subtype of then any expression of type

### Boolean expressions. Elements of Programming Languages. Conditionals. What use is this?

Boolean expressions Elements of Programming Languages Lecture 3: Booleans, conditionals, and types James Cheney So far we ve considered only a trivial arithmetic language L Arith Let s extend L Arith with

### Software II: Principles of Programming Languages. Why Expressions?

Software II: Principles of Programming Languages Lecture 7 Expressions and Assignment Statements Why Expressions? Expressions are the fundamental means of specifying computations in a programming language

### Expressions and Assignment

Expressions and Assignment COS 301: Programming Languages Outline Other assignment mechanisms Introduction Expressions: fundamental means of specifying computations Imperative languages: usually RHS of

### Chapter 7. Expressions and Assignment Statements ISBN

Chapter 7 Expressions and Assignment Statements ISBN 0-321-49362-1 Chapter 7 Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit

### A Small Interpreted Language

A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

### Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

### Types and Type Inference

CS 242 2012 Types and Type Inference Notes modified from John Mitchell and Kathleen Fisher Reading: Concepts in Programming Languages, Revised Chapter 6 - handout on Web!! Outline General discussion of

### COMP 181. Agenda. Midterm topics. Today: type checking. Purpose of types. Type errors. Type checking

Agenda COMP 181 Type checking October 21, 2009 Next week OOPSLA: Object-oriented Programming Systems Languages and Applications One of the top PL conferences Monday (Oct 26 th ) In-class midterm Review

### Chapter 7. Expressions and Assignment Statements

Chapter 7 Expressions and Assignment Statements Chapter 7 Topics Introduction Arithmetic Expressions Overloaded Operators Type Conversions Relational and Boolean Expressions Short-Circuit Evaluation Assignment

### Example Scheme Function: equal

ICOM 4036 Programming Languages Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: LISP Introduction to

### Formal Systems and their Applications

Formal Systems and their Applications Dave Clarke (Dave.Clarke@cs.kuleuven.be) Acknowledgment: these slides are based in part on slides from Benjamin Pierce and Frank Piessens 1 Course Overview Introduction

### Primitive Data Types: Intro

Primitive Data Types: Intro Primitive data types represent single values and are built into a language Java primitive numeric data types: 1. Integral types (a) byte (b) int (c) short (d) long 2. Real types

### Functional Programming. Big Picture. Design of Programming Languages

Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

### Symbolic Computation and Common Lisp

Symbolic Computation and Common Lisp Dr. Neil T. Dantam CSCI-56, Colorado School of Mines Fall 28 Dantam (Mines CSCI-56) Lisp Fall 28 / 92 Why? Symbolic Computing: Much of this course deals with processing

### Compilers. Type checking. Yannis Smaragdakis, U. Athens (original slides by Sam

Compilers Type checking Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Summary of parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser:

### Pointers (continued), arrays and strings

Pointers (continued), arrays and strings 1 Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2 Pointer arithmetic

### Principles of Programming Languages

Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 23! Type systems Type safety Type checking Equivalence,

### Short Notes of CS201

#includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

### Example: Haskell algebraic data types (1)

Example: Haskell algebraic data types (1) Type declaration: data Number = Exact Int Inexact Float Set of values: Each Number value consists of a tag, together with either an Integer variant (if the tag

### AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS PAUL L. BAILEY Abstract. This documents amalgamates various descriptions found on the internet, mostly from Oracle or Wikipedia. Very little of this

### COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

### Topic 6: Types COS 320. Compiling Techniques. Princeton University Spring Prof. David August. Adapted from slides by Aarne Ranta

Topic 6: Types COS 320 Compiling Techniques Princeton University Spring 2015 Prof. David August 1 Adapted from slides by Aarne Ranta Types What is a type? Type Checking: Helps find language-level errors:

### CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

### Semantic Analysis. CSE 307 Principles of Programming Languages Stony Brook University

Semantic Analysis CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Role of Semantic Analysis Syntax vs. Semantics: syntax concerns the form of a

### Lecture Overview. [Scott, chapter 7] [Sebesta, chapter 6]

1 Lecture Overview Types 1. Type systems 2. How to think about types 3. The classification of types 4. Type equivalence structural equivalence name equivalence 5. Type compatibility 6. Type inference [Scott,

### MISRA-C. Subset of the C language for critical systems

MISRA-C Subset of the C language for critical systems SAFETY-CRITICAL SYSTEMS System is safety-critical if people might die due to software bugs Examples Automobile stability / traction control Medical

### Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Introduction to ML Mooly Sagiv Cornell CS 3110 Data Structures and Functional Programming Typed Lambda Calculus Chapter 9 Benjamin Pierce Types and Programming Languages Call-by-value Operational Semantics

### CS1622. Semantic Analysis. The Compiler So Far. Lecture 15 Semantic Analysis. How to build symbol tables How to use them to find

CS1622 Lecture 15 Semantic Analysis CS 1622 Lecture 15 1 Semantic Analysis How to build symbol tables How to use them to find multiply-declared and undeclared variables. How to perform type checking CS

### Parametric types. map: ( a (a b) a list b list. filter: ( a bool) a list a list. Why? a and b are type variables!

What is the deal with a? Parametric types aka: what s up with those a? These meta-functions have strange types: map: ( a b) a list b list filter: ( a bool) a list a list Why? Polymorphism Poly = many,

### Static Checking and Type Systems

1 Static Checking and Type Systems Chapter 6 COP5621 Compiler Construction Copyright Robert van Engelen, Florida State University, 2007-2009 2 The Structure of our Compiler Revisited Character stream Lexical

### CS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and Type-Checking 25 Feb 08

CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 17: Types and Type-Checking 25 Feb 08 CS 412/413 Spring 2008 Introduction to Compilers 1 What Are Types? Types describe the values possibly

### Imperative languages

Imperative languages Von Neumann model: store with addressable locations machine code: effect achieved by changing contents of store locations instructions executed in sequence, flow of control altered

### Pointers (continued), arrays and strings

Pointers (continued), arrays and strings 1 Last week We have seen pointers, e.g. of type char *p with the operators * and & These are tricky to understand, unless you draw pictures 2 Pointer arithmetic

### (Not Quite) Minijava

(Not Quite) Minijava CMCS22620, Spring 2004 April 5, 2004 1 Syntax program mainclass classdecl mainclass class identifier { public static void main ( String [] identifier ) block } classdecl class identifier