# CSc 520. Principles of Programming Languages 25: Types Introduction

Size: px
Start display at page:

## Transcription

1 CSc 520 Principles of Programming Languages 25: Types Introduction Christian Collberg Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg April 22, Why types? Types save typing. What does a+b mean? In Java it could be 1. a + int b. 2. a + float b. 3. a concat string b. 4. int2float(a) + float b. 5. a + float int2float(b). 6. int2string(a) concat string b. etc, all depending on the types of a and b. 2 Why types...? In Icon variables are not given explicit types. Instead, operations carry the types: 1. a b means binary or on integers. 2. a b means string concatenation. 3. a b means list concatenation. Icon has lots of operators... In other words, without types, we would have to be much more explicit about which operations are performed where. 1

2 3 Why types...? Icon programs become a bit wordier since every operator effectively encode the required type of the operands. On the other hand, it also becomes more readable since we can see directly from the operator what operation will be performed. global x,y,z procedure p() x := x + y x := x y x := x y end # integer addition # string concatenation # list concatenation 4 Why types...? To figure out which operation is performed in a Java program, we have to find the declarations of all variables to find their declared type: int x; String y; float z; void p() { x = x + 5; /* integer addition */ z = z + 5.0; /* float addition */ y = y + "X"; /* string concatenation */ 5 Why types...? Types prevent errors. Types save the programmer from himself. Types prevent us from adding a character and a record. int A[20]; float x; void p() { A[5] = x; A[x] = 5; x = x + A; 6 Why types...? Types permit optimization. A compiler can generate better code for a+b if it knows that both variables must be integers, than if the exact types aren t known until runtime: 2

3 global a,b procedure p() { a = new array [20]... b = new array [20]... a = a + b /* what operation is performed here? */ 7 Type Systems A type system consists of a mechanism for defining types, rules for type equivalence, rules for type compatibility, rules for type inference. 8 Type Systems... Type equivalence determines when the types of two values are the same: TYPE A = ARRAY [0..10] OF CHAR; TYPE B = ARRAY [0..10] OF CHAR; VAR a : A; VAR b : B; BEGIN a := b; (* legal? *) END Are the types of a and b the same? 9 Type Systems... Type compatibility determines when a value of a given type can be used in a given context: VAR a : float; VAR b : int; BEGIN a := a + b; END Can you add an int and a float? 10 Type Systems... Type inference defines the type of an expression based on its parts and surrounding context: 3

4 global a,b,c procedure p(x) if x = 5 then a := x else a := "hello" write(a) end procedure main() p(5) end What type of data can be written here? 11 Type Checking Type checking ensures that a program obeys a language s type rules. A type clash is a violation of the typing rules. class C { void p() { int x = new C(); 12 Type Checking Strong Typing Language L is strongly typed if is an operator in L that expects an object of type T, L prohibits from accepting objects of any other type, and L requires an implementation (a compiler, interpreter, etc) to enforce this prohibition. In other words, a strongly typed language does not allow us to perform operations on the wrong type of data. 13 Type Checking Weak Typing In a weakly typed language there are ways to escape the type system. In C, for example, it is possible to cast a pointer to a float, add 3.14 to it, and cast it back to a pointer: int main() { int* p = (int*) malloc (sizeof(int)); float f = *((float*) &p) ; p = (int*)(*(int *)&f); Such operations are probably meaningless and a strongly typed language would prohibit them. 4

5 14 Type Checking Static/Dynamic Typing A language statically typed if type checking is done at compile-time. A language dynamically typed if type checking is done at run-time. In practice, even languages which are considered statically typed do some checking at run-time. Languages can usually be classified as mostly strongly typed, mostly statically typed, etc. 15 Terminology Benjamin C. Pierce has said: I spent a few weeks... trying to sort out the terminology of strongly typed, statically typed, safe, etc., and found it amazingly difficult.... The usage of these terms is so various as to render them almost useless. It is possible to say My language is more strongly typed than your language. but harder to argue that My language is strongly typed/statically typed, etc. 16 Examples Pascal Pascal is mostly strongly and statically typed. Untagged variant records are a loophole. They allow us to turn a value of one type into an object of some unrelated type. Unlike C, array bounds are checked. 17 Pascal Untagged Variant Records type rec = record a : integer; case boolean of true : (x : integer); false : (y : char); end; var r: rec; begin r.x := 55; r.y := A ; write(r.x); end. This construct is used to bypass Pascal s strong typing. 5

6 18 Examples C C is weakly and statically typed. Pointers can be cast willy-nilly which makes it easy to bypass the type system. Array references are not checked: int main() { int A[20]; int B[20]; A[25] = 5; Negative indices were used in the old days to overwrite the operating systems. Today, buffer overflows are how most viruses compromise security. 19 Examples Ada Ada is strongly and mostly statically typed. Unlike Pascal, variant records must be tagged: type Device is (Printer, Disk, Drum); type Peripheral(Unit : Device := Disk) is record case Unit is when Printer => Line_Count : Integer ; when others => Cylinder : CIndex; end case; end record; 20 Examples Ada... It is, however, possible to do non-converting casts (similar to C), but in a very explicit way: function float2int is new unchecked_conversion(float,integer);... f := float2int(i); Some errors can t be checked at compile-time: I, J : Integer range := 5; K : Integer range := 15; I := J; -- identical ranges K := J; -- compatible ranges J := K; -- will raise an exception if K>10 6

7 21 Examples Scheme Scheme is completely dynamically typed, so programmers often insert extra checks: (define (sum l) (cond ((null? l) 0) ((not (list? l)) (error "list expected")) ((not (number? (car l))) (error "list of numbers expected")) (else (+ (car l) (sum (cdr l)))) )) 22 Examples Java Java is strongly and mostly statically typed. An exception is thrown here because an A-object can t be cast to a B-object: class A { class B extends A { int x; void p() { B b = (B) new A(); 23 Typing typing weak strong static dynamic static dynamic Pascal C Modula 2 Scheme Smalltalk Ada Java Haskell 24 Type Inference In statically typed languages types are inferred in the compiler, before the program is run: procedure p (x : integer); var z : real; var c : char; 7

8 begin write(x + z); /* convert x to real, write a real */ write(c + z); /* type error */ end 25 Type Inference... Haskell and similar languages don t require the programmer to give types to variables and functions. Instead, the compiler infers types. Given len [] = 0 len _:xs = 1 + len xs the Haskell translator will infer a most general type: len :: [a] -> Int Haskell is strongly and statically typed, although the programmer rarely have to provide explicit type information. 26 So, What is a Type? There are three ways to think about types: 1. denotational view a type is a set of values; 2. constructive view a type is what we can construct from the type constructors in the language; 3. abstraction-based view a type denotes a data object and a well-defined set of allowable operators on this object. At different times, we may look at a type in any of these ways. 27 Denotational View A type T is a set of values {t 0, t 1, t A value v is of type T if it belongs to the set. A variable v is of type T if it is guaranteed to always hold a value in the set. A char type in Pascal is the set of 128 seven-bit ASCII characters: {..., "0",...,"9",..., "A",...,"Z",..., "a"...,"z",... 8

9 28 Constructive View A Pascal type is (roughly) type ::= integer real char boolean... [ expr.. expr ] SET OF type ARRAY type OF type RECORD [field list] END I.e., a Pascal type is either one of the built-in types, or ones we define ourselves by composing type constructors, such as ARRAY, RECORD, etc: END T = RECORD a : real; b : ARRAY ["a".."z"] OF SET OF char; END; 29 Abstraction-Based View A type is an abstract data type. The next slides shows what the Modula-3 language manual says about the operations that are allowed on Words. The allowed operations include arithmetic and logical operations. There is no pointer dereferencing operation defined, however, so apparently this operation is not allowed. 30 Abstraction-Based View... INTERFACE Word; TYPE T = INTEGER; PROCEDURE Plus (x,y: T): T; PROCEDURE Times (x,y: T): T; PROCEDURE Minus (x,y: T): T; PROCEDURE Divide(x,y: T): T; PROCEDURE Mod(x,y: T): T; PROCEDURE LT(x,y: T): BOOLEAN; PROCEDURE LE(x,y: T): BOOLEAN; PROCEDURE GT(x,y: T): BOOLEAN; PROCEDURE GE(x,y: T): BOOLEAN; PROCEDURE And(x,y: T): T; PROCEDURE Or (x,y: T): T; PROCEDURE Xor(x,y: T): T; PROCEDURE Not (x: T): T; PROCEDURE Shift(x: T; n: INTEGER): T; PROCEDURE Rotate(x: T; n: INTEGER): T; PROCEDURE Extract(x: T; i, n: CARDINAL): T; PROCEDURE Insert(x: T; y: T; i, n: CARDINAL): T; END Word. 9

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 16 : Types Polymorphism Christian Collberg collberg+520@gmail.com Department of Computer Science University of Arizona Copyright c 2008 Christian Collberg [1]

### 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,

### Topic 9: Type Checking

Recommended Exercises and Readings Topic 9: Type Checking From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6 and

### Topic 9: Type Checking

Topic 9: Type Checking 1 Recommended Exercises and Readings From Haskell: The craft of functional programming (3 rd Ed.) Exercises: 13.17, 13.18, 13.19, 13.20, 13.21, 13.22 Readings: Chapter 13.5, 13.6

### 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

### Types. Chapter Six Modern Programming Languages, 2nd ed. 1

Types Chapter Six Modern Programming Languages, 2nd ed. 1 A Type Is A Set int n; When you declare that a variable has a certain type, you are saying that the values the variable can have are elements of

### 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,

### CSc 520 Principles of Programming Languages. 26 : Control Structures Introduction

CSc 520 Principles of Programming Languages 26 : Control Structures Introduction Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian

### 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:

### The List Datatype. CSc 372. Comparative Programming Languages. 6 : Haskell Lists. Department of Computer Science University of Arizona

The List Datatype CSc 372 Comparative Programming Languages 6 : Haskell Lists Department of Computer Science University of Arizona collberg@gmail.com All functional programming languages have the ConsList

### CSc 372. Comparative Programming Languages. 36 : Scheme Conditional Expressions. Department of Computer Science University of Arizona

1/26 CSc 372 Comparative Programming Languages 36 : Scheme Conditional Expressions Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/26 Comparison

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 50: Semantics Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg

### 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

### 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

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 36 : Scheme Conditional Expressions Christian Collberg Department of Computer Science University of Arizona collberg+520@gmail.com Copyright c 2008 Christian

### 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

### Intermediate Code Generation

Intermediate Code Generation In the analysis-synthesis model of a compiler, the front end analyzes a source program and creates an intermediate representation, from which the back end generates target

### 520 Principles of Programming Languages. Arithmetic. Variable Declarations. 19: Pascal

Structure of a Pascal Program 520 Principles of Programming Languages 19: Pascal Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona PROGRAM Name (list of files);

### Lecture 12: Data Types (and Some Leftover ML)

Lecture 12: Data Types (and Some Leftover ML) COMP 524 Programming Language Concepts Stephen Olivier March 3, 2009 Based on slides by A. Block, notes by N. Fisher, F. Hernandez-Campos, and D. Stotts Goals

### Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types

### 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

### Type Checking and Type Equality

Type Checking and Type Equality Type systems are the biggest point of variation across programming languages. Even languages that look similar are often greatly different when it comes to their type systems.

### 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

### CSc 453. Semantic Analysis III. Basic Types... Basic Types. Structured Types: Arrays. Compilers and Systems Software. Christian Collberg

Slide 11 2 Basic s... Enumeration types TYPE E1 = (white,blue,yellow,green,red); TYPE E2 = (apple=4,pear=9,kumquat=99); Pascal, Ada, Modula-2, C have some variant of enumeration types. Subrange types TYPE

### 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

### CSI33 Data Structures

Outline Department of Mathematics and Computer Science Bronx Community College October 24, 2018 Outline Outline 1 Chapter 8: A C++ Introduction For Python Programmers Expressions and Operator Precedence

### Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

### 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 /

### CSc 372 Comparative Programming Languages. 4 : Haskell Basics

CSc 372 Comparative Programming Languages 4 : Haskell Basics Christian Collberg Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2011 Christian Collberg August 23, 2011

### 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?

### Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

CSE 3302 Programming Languages Data Types (cont.) Chengkai Li Fall 2007 Subset U = { v v satisfies certain conditions and v V} Ada subtype Example 1 type Digit_Type is range 0..9; subtype IntDigit_Type

### CSc 372. Comparative Programming Languages. 4 : Haskell Basics. Department of Computer Science University of Arizona

1/40 CSc 372 Comparative Programming Languages 4 : Haskell Basics Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/40 The Hugs Interpreter The

### CSc 372. Comparative Programming Languages. 15 : Haskell List Comprehension. Department of Computer Science University of Arizona

1/20 CSc 372 Comparative Programming Languages 15 : Haskell List Comprehension Department of Computer Science University of Arizona collberg@gmail.com Copyright c 2013 Christian Collberg 2/20 List Comprehensions

### 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

### 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

### Expressions and Casting

Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

### The SPL Programming Language Reference Manual

The SPL Programming Language Reference Manual Leonidas Fegaras University of Texas at Arlington Arlington, TX 76019 fegaras@cse.uta.edu February 27, 2018 1 Introduction The SPL language is a Small Programming

### The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

### Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

https://lambda.mines.edu Evaluating programming languages based on: Writability: How easy is it to write good code? Readability: How easy is it to read well written code? Is the language easy enough to

### Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

### 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

### UMBC CMSC 331 Final Exam

UMBC CMSC 331 Final Exam Name: UMBC Username: You have two hours to complete this closed book exam. We reserve the right to assign partial credit, and to deduct points for answers that are needlessly wordy

### Data Types (cont.) Administrative Issues. Academic Dishonesty. How do we detect plagiarism? Strongly Typed Languages. Type System

CSE 3302 Programming Languages Data Types (cont.) Chengkai Li Fall 2007 1 Administrative Issues Midterm Exam (in class) Tuesday, Oct. 16 th Schedule Change HW1 HW1 part1 & HW1 part2 Due at the same time,

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

Outline Type Checking General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

### 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

### 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

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

Type Checking Outline General properties of type systems Types in programming languages Notation for type rules Logical rules of inference Common type rules 2 Static Checking Refers to the compile-time

### 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

### 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

### Information Science 1

Topics covered Information Science 1 Terms and concepts from Week 8 Simple calculations Documenting programs Simple Calcula,ons Expressions Arithmetic operators and arithmetic operator precedence Mixed-type

### COMP6700/2140 Data and Types

COMP6700/2140 Data and Types Alexei B Khorev and Josh Milthorpe Research School of Computer Science, ANU February 2017 Alexei B Khorev and Josh Milthorpe (RSCS, ANU) COMP6700/2140 Data and Types February

### 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

### Lectures 5-6: Introduction to C

Lectures 5-6: Introduction to C Motivation: C is both a high and a low-level language Very useful for systems programming Faster than Java This intro assumes knowledge of Java Focus is on differences Most

### Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

### Information Science 1

Information Science 1 Simple Calcula,ons Week 09 College of Information Science and Engineering Ritsumeikan University Topics covered l Terms and concepts from Week 8 l Simple calculations Documenting

### SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### Lexical Considerations

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

### ECE 250 / CS 250 Computer Architecture. C to Binary: Memory & Data Representations. Benjamin Lee

ECE 250 / CS 250 Computer Architecture C to Binary: Memory & Data Representations Benjamin Lee Slides based on those from Alvin Lebeck, Daniel Sorin, Andrew Hilton, Amir Roth, Gershon Kedem Administrivia

### Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal

Expressions and Data Types CSC 121 Fall 2015 Howard Rosenthal Lesson Goals Understand the basic constructs of a Java Program Understand how to use basic identifiers Understand simple Java data types and

### Francesco Nidito. Programmazione Avanzata AA 2007/08

Francesco Nidito in the Programmazione Avanzata AA 2007/08 Outline 1 2 3 in the in the 4 Reference: Micheal L. Scott, Programming Languages Pragmatics, Chapter 7 What is a type? in the What is a type?

### Scheme: Strings Scheme: I/O

Scheme: Strings Scheme: I/O CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Wednesday, April 5, 2017 Glenn G. Chappell Department of Computer Science University of

### 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

### CSC324 Principles of Programming Languages

CSC324 Principles of Programming Languages http://mcs.utm.utoronto.ca/~324 November 14, 2018 Today Final chapter of the course! Types and type systems Haskell s type system Types Terminology Type: set

### Compilers CS S-05 Semantic Analysis

Compilers CS414-2003S-05 Semantic Analysis David Galles Department of Computer Science University of San Francisco 05-0: Syntax Errors/Semantic Errors A program has syntax errors if it cannot be generated

### Semantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler Front-End

Outline Semantic Analysis The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

### 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

### Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Expressions and Casting C# Programming Rob Miles Data Manipulation We know that programs use data storage (variables) to hold values and statements to process the data The statements are obeyed in sequence

### Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

### The role of semantic analysis in a compiler

Semantic Analysis Outline The role of semantic analysis in a compiler A laundry list of tasks Scope Static vs. Dynamic scoping Implementation: symbol tables Types Static analyses that detect type errors

### COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

### Review Chapters 1 to 4. Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013

Review Chapters 1 to 4 Instructor: Scott Kristjanson CMPT 125/125 SFU Burnaby, Fall 2013 Introduction to Java Chapters 1 and 2 The Java Language Section 1.1 Data & Expressions Sections 2.1 2.5 Instructor:

### 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

### And Parallelism. Parallelism in Prolog. OR Parallelism

Parallelism in Prolog And Parallelism One reason that Prolog is of interest to computer scientists is that its search mechanism lends itself to parallel evaluation. In fact, it supports two different kinds

### CSc 520. Principles of Programming Languages 46: OO Languages Polymorphism

CSc 520 Principles of Programming Languages 46: OO Languages Polymorphism Christian Collberg Department of Computer Science University of Arizona collberg@cs.arizona.edu Copyright c 2005 Christian Collberg

### 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

### If we have a call. Now consider fastmap, a version of map that uses futures: Now look at the call. That is, instead of

If we have a call (map slow-function long-list where slow-function executes slowly and long-list is a large data structure, we can expect to wait quite a while for computation of the result list to complete.

### Introduction to Programming, Aug-Dec 2006

Introduction to Programming, Aug-Dec 2006 Lecture 3, Friday 11 Aug 2006 Lists... We can implicitly decompose a list into its head and tail by providing a pattern with two variables to denote the two components

### Data Types. CSE 307 Principles of Programming Languages Stony Brook University

Data Types CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Data Types We all have developed an intuitive notion of what types are; what's behind

### Expressions & Flow Control

Objectives Distinguish between instance and local variables 4 Expressions & Flow Control Describe how instance variables are initialized Identify and correct a Possible reference before assignment compiler

### Chapter 2: Using Data

Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

### Hacking in C. Pointers. Radboud University, Nijmegen, The Netherlands. Spring 2019

Hacking in C Pointers Radboud University, Nijmegen, The Netherlands Spring 2019 Allocation of multiple variables Consider the program main(){ char x; int i; short s; char y;... } What will the layout of

### Defining Functions. CSc 372. Comparative Programming Languages. 5 : Haskell Function Definitions. Department of Computer Science University of Arizona

Defining Functions CSc 372 Comparative Programming Languages 5 : Haskell Function Definitions Department of Computer Science University of Arizona collberg@gmail.com When programming in a functional language

### CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages 3: Scheme Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

### Types, Type Inference and Unification

Types, Type Inference and Unification Mooly Sagiv Slides by Kathleen Fisher and John Mitchell Cornell CS 6110 Summary (Functional Programming) Lambda Calculus Basic ML Advanced ML: Modules, References,

### 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

### 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,

### Processor. Lecture #2 Number Rep & Intro to C classic components of all computers Control Datapath Memory Input Output

CS61C L2 Number Representation & Introduction to C (1) insteecsberkeleyedu/~cs61c CS61C : Machine Structures Lecture #2 Number Rep & Intro to C Scott Beamer Instructor 2007-06-26 Review Continued rapid

### Chapter 5 Names, Binding, Type Checking and Scopes

Chapter 5 Names, Binding, Type Checking and Scopes Names - We discuss all user-defined names here - Design issues for names: -Maximum length? - Are connector characters allowed? - Are names case sensitive?

### Chapter 1 INTRODUCTION SYS-ED/ COMPUTER EDUCATION TECHNIQUES, INC.

hapter 1 INTRODUTION SYS-ED/ OMPUTER EDUATION TEHNIQUES, IN. Objectives You will learn: Java features. Java and its associated components. Features of a Java application and applet. Java data types. Java

### Discussion. Type 08/12/2016. Language and Type. Type Checking Subtypes Type and Polymorphism Inheritance and Polymorphism

Type Joseph Spring Discussion Languages and Type Type Checking Subtypes Type and Inheritance and 7COM1023 Programming Paradigms 1 2 Type Type denotes the kind of values that programs can manipulate: Simple

### 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

### Functional Programming - 2. Higher Order Functions

Functional Programming - 2 Higher Order Functions Map on a list Apply Reductions: foldr, foldl Lexical scoping with let s Functional-11, CS5314, Sp16 BGRyder 1 Higher Order Functions Functions as 1st class

### CSC 533: Organization of Programming Languages. Spring 2005

CSC 533: Organization of Programming Languages Spring 2005 Language features and issues variables & bindings data types primitive complex/structured expressions & assignments control structures subprograms

### n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

### LECTURE 17. Expressions and Assignment

LECTURE 17 Expressions and Assignment EXPRESSION SYNTAX An expression consists of An atomic object, e.g. number or variable. An operator (or function) applied to a collection of operands (or arguments)

### Zheng-Liang Lu Java Programming 45 / 79

1 class Lecture2 { 2 3 "Elementray Programming" 4 5 } 6 7 / References 8 [1] Ch. 2 in YDL 9 [2] Ch. 2 and 3 in Sharan 10 [3] Ch. 2 in HS 11 / Zheng-Liang Lu Java Programming 45 / 79 Example Given a radius