Chapter 4: Types. Why Have Types? Detect simple kinds of errors: int square(int x) { return x*x; }... int bogus = square(“hello”); String bogus2 = square(3);

Slides:



Advertisements
Similar presentations
CH4.1 Type Checking Md. Fahim Computer Engineering Department Jamia Millia Islamia (A Central University) New Delhi –
Advertisements

Procedural Programming in C# Chapters Objectives You will be able to: Describe the most important data types available in C#. Read numeric values.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Chapter 6 Type Checking. The compiler should report an error if an operator is applied to an incompatible operand. Type checking can be performed without.
Chapter 3 DATA: TYPES, CLASSES, AND OBJECTS. Chapter 3 Data Abstraction Abstract data types allow you to work with data without concern for how the data.
Lecture # 21 Chapter 6 Uptill 6.4. Type System A type system is a collection of rules for assigning type expressions to the various parts of the program.
Types and Arithmetic Operators
Type checking © Marcelo d’Amorim 2010.
Sets Lecture 11: Oct 24 AB C. This Lecture We will first introduce some basic set theory before we do counting. Basic Definitions Operations on Sets Set.
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
Basic Structures: Sets, Functions, Sequences, Sums, and Matrices
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Type Checking.
Compiler Construction
Fall 2003CS416 Compiler Design1 Type Checking A compiler has to do semantic checks in addition to syntactic checks. Semantic Checks –Static – done during.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Names and Scopes CS 351. Program Binding We should be familiar with this notion. A variable is bound to a method or current block e.g in C++: namespace.
Sets 1.
Sets 1.
PHYS 2020 Making Choices; Arrays. Arrays  An array is very much like a matrix.  In the C language, an array is a collection of variables, all of the.
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.
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Types in programming languages What are types, and why do we need them? Types in programming languages1.
Computer Science 209 The Strategy Pattern II: Emulating Higher-Order Functions.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
4.2 Type Checking (type-of-expression > tenv ) = bool (type-of-expression > tenv ) = x ( type-of-expression if > tenv ) = x Recall type of if statement:
Introduction to Scheme Lectures on The Scheme Programming Language, 2 nd Ed. R. Kent Dybvig.
Data Structures Chapter 1- Introduction Mohamed Mustaq.A.
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.
1 st Semester Module2 Basic C# Concept อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
College Board A.P. Computer Science A Topics Program Design - Read and understand a problem's description, purpose, and goals. Procedural Constructs.
1 Objects and types Typed languages = define a set of types in the language and assign a type to each expression in the program Type checking = how can.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
Introduction to Programming
Types in programming languages1 What are types, and why do we need them?
CompSci 102 Discrete Math for Computer Science
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
Chapter 2 With Question/Answer Animations. Section 2.1.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
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?”
1 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
CMSC 330: Organization of Programming Languages Lambda Calculus and Types.
Chapter One Lesson Three DATA TYPES ©
A: A: double “4” A: “34” 4.
Shifting the Blame A blame calculus with delimited control Taro Sekiyama* Atsushi Igarashi Soichiro Ueda Kyoto University Gradual typing.
CMSC 330: Organization of Programming Languages Operational Semantics.
CS7120 (Prasad)L13-1-Lambda-Opt1 Typed Lambda Calculus Adapted from Lectures by Profs Aiken and Necula of Univ. of California at Berkeley.
Hindley-Milner Type Inference CSE 340 – Principles of Programming Languages Fall 2015 Adam Doupé Arizona State University
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Arithmetic Instructions. Integer and Float Conversions.
 Type Called bool  Bool has only two possible values: True and False.
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Set Comprehensions In mathematics, the comprehension notation can be used to construct new sets from old sets. {x2 | x  {1...5}} The set {1,4,9,16,25}
Functional Programming
Basic concepts of C++ Presented by Prof. Satyajit De
A basic tutorial for fundamental concepts
Review Operation Bingo
Chapter 4: Types.
Lecture 28: Types of Types
Type & Typeclass Syntax in function
PROGRAMMING IN HASKELL
Compiler Construction
Java’s Central Casting
Subtype Substitution Principle
Programming Languages Dan Grossman 2013
Compiler Construction
Presentation transcript:

Chapter 4: Types

Why Have Types? Detect simple kinds of errors: int square(int x) { return x*x; }... int bogus = square(“hello”); String bogus2 = square(3);

Why Have Types? Detect simple kinds of errors: double diff(double x, double y) { return x – y; }... double d = diff(3.2, 4.0, -5.1);

Why Have Types? Recall Russell’s Paradox: The barber who shaves only those who don’t shave themselves (naïve set theory)

Static vs. Dynamic (Latent) Type-Checking Static: checked by compiler, or part of grammar (PS4 argument-count example) Dynamic: Every value is tagged with a latent type; types are checked at run-time (Scheme) (+3'abc) [int*int->int][int][string]

A Little Statically-Typed Language ::= int int-type-exp () ::= bool bool-type-exp () ::= ( {type-exp>} * ( * ) -> ) proc-type-exp (arg-texps result-texp) e.g., (int*int) -> bool

Strong Static Type Checking No program that passes the type checker (compiler) will contain a type error. Want a procedure type-of-expression to compute the value of a given type in a given type-environment. Will have to contain a separate rule (case) for every kind of construct we support; e.g., if :

Static Type Checking (type-of-expression > tenv ) = bool (type-of-expression > tenv ) = x ( type-of-expression if > tenv ) = x (Book uses t instead of x - confusing!)

Mathematical Background Type: a set OOP classes also are sets. But with types, we're more interested in functions from one type to another.

OOP Animal Mammal Dog Fish

Types Integer 1 Boolean True False positive? prime ? square incr minus square : int -> int prime? : int -> boolean

Types Integer*Integer→Boolean greater? equal? less? Integer→(Integer→Integer)‏ (lambda (x)‏ (lambda (y)‏ (+ x y)))‏