301AA  Advanced Programming [AP2017]


 Claude Walton
 2 years ago
 Views:
Transcription
1 301AA  Advanced Programming [AP2017] Lecturer: Andrea Corradini Tutor: Lillo GalleBa Department of Computer Science, Pisa Academic Year 2017/18 AP : Type inference
2 Type Inference Type inference: the process of associanng a type with each symbol of a program, if possible, ensuring type safety. Recalling several relevant defininons: (Data) types Type system Type error Type safety Dynamic vs. stanc type checking 2
3 Data types A (data) type is a homogeneous collecnon of values, effecnvely presented, equipped with a set of operanons which manipulate these values Various perspecnves: collecnon of values from a domain (the denotanonal approach) internal structure of a bunch of data, described down to the level of a small set of fundamental types (the structural approach) collecnon of welldefined operanons that can be applied to objects of that type (the abstracnon approach) 3
4 Type system A type system consists of 1. The set of predefined types of the language. 2. The mechanisms which permit the defini2on of new types. 3. The mechanisms for the control (checking) of types, which include: 1. Equivalence rules which specify when two formally different types correspond to the same type. 2. Compa2bility rules specifying when a value of a one type can be used in given context. 3. Rules and techniques for type inference which specify how the language assigns a type to a complex expression based on informanon about its components (and somenmes on the context). 4. The specificanon as to whether (or which) constraints are sta2cally or dynamically checked. 4
5 Type errors A type error occurs when a value is used in a way that is inconsistent with its defininon ImplementaNons can react in various ways Hardware interrupt, e.g. apply fp addi1on to nonlegal bit configura1on OS excepnon, e.g. segmenta1on fault when dereferencing 0 in C ConNnue execunon possibly with wrong values Examples Array out of bounds access C/C++: runnme errors Java: dynamic type error Null pointer dereferencing C/C++: runnme errors Java: dynamic type error Haskell/ML: pointers are hidden inside datatypes Null pointer dereferences would be incorrect use of these datatypes, therefore stanc type errors 5
6 Type safety A language is type safe (strongly typed) when no program can violate the disnncnons between types defined in its type system That is, when no program, during its execunon, can generate an unsignalled type error Also: if code accesses data, it is handled with the type associated with the creanon and previous manipulanon of that data 6
7 Type checking To prevent type errors, before any operanon is performed, its operands must be typechecked to ensure that they comply with the companbility rules of the type system E.g., indexing opera1on: check that the leg operand is an array, and that the right operand is a value of the array s index type. Sta2cally typed languages: (most) type checking is done during compilanon Dynamically typed languages: type checking is done at runnme 7
8 StaNc vs dynamic typing In a sta2cally typed PL: all variables and expressions have fixed types (either stated by the programmer or inferred by the compiler) most operands are typechecked at compile1me. Most PLs are called stancally typed, including Ada, C, C++, Java, Haskell, even if some typechecking is done at runnme (e.g. access to arrays) In a dynamically typed PL: values have fixed types, but variables and expressions do not operands must be typechecked when they are computed at run1me. Some PLs and many scripnng languages are dynamically typed, including Smalltalk, Lisp, Prolog, Perl, Python. 8
9 Polymorphism in Haskell Coercion Type Inference Implicit Parametric Universal Explicit Bounded Polymorphism Inclusion Covariant Invariant Contravariant Ad hoc Overriding Overloading Type classes
10 Universal vs Adhoc Polymorphism Universal polymorphism Single algorithm may be given many types Type variables (implicit or explicit) may be replaced by any type (almost > bounded polymorphism) head::[a] a thus head::[int] Int, head:[bool] Bool,... Adhoc polymorphism (overloading) A single symbol may refer to more than one algorithm. Each algorithm may have different type. Choice of algorithm determined by type context. + has types Int Int Int and Float Float Float, but not t t t for arbitrary t. 10
11 Type Checking vs Type Inference Standard type checking: int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2; }; Examine body of each funcnon Use declared types to check agreement Type inference: int f(int x) { return x+1; }; int g(int y) { return f(y+1)*2; }; Examine code without type informanon. Infer the most general types that could have been declared. ML and Haskell are designed to make type inference feasible. 11
12 Why study type inference? Types and type checking Improved steadily since Algol 60 Eliminated sources of unsoundness. Become substannally more expressive. Important for modularity, reliability and compilanon Type inference Reduces syntacnc overhead of expressive types. Guaranteed to produce most general type. Increasingly used also in imperanve/oo languages. IllustraNve example of a flowinsensinve stanc analysis algorithm. 12
13 uhaskell Subset of Haskell to explain type inference. Will do not consider overloading now <decl> ::= <name> <pat> = <exp> <pat> ::= Id (<pat>, <pat>) <pat> : <pat> [] <exp> ::= Int Bool [] Id (<exp>) <exp> <op> <exp> <exp> <exp> (<exp>, <exp>) if <exp> then <exp> else <exp> 13
14 Type Inference: Basic Idea Example f x = 2 + x  a simple declaration What is the type of f? + has type: Int Int Int (with overloading would be Num a => a a a) 2 has type: Int Since we are applying + to x we need x :: Int Therefore f x = 2 + x implies that f has type Int Int f x = 2 + x  a simple declaration > f :: Int > Int 14
15 Step 1: Parse Program Parse program text to construct parse tree. f x = 2 + x to represent application Ternary Funnode for function definitions Infix operators are converted to Curried function application during parsing: 2 + x è (+) 2 x 15
16 Step 2: Assign type variables to nodes f x = 2 + x Variables are given same type as binding occurrence. 16
17 Constraints from ApplicaNon Nodes f x t_0 = t_1 > t_2 FuncNon applicanon (apply f to x) Type of f (t_0 in figure) must be domain range. Domain of f must be type of argument x (t_1 in fig) Range of f must be result of applicanon (t_2 in fig) Constraint: t_0 = t_1 > t_2 17
18 Constraints from AbstracNons f x = e t_0 = t_1 > t_2 FuncNon declaranon: Type of f (t_0 in figure) must be domain range Domain is type of abstracted variable x (t_1 in fig) Range is type of funcnon body e (t_2 in fig) Constraint: t_0 = t_1 > t_2 Idem for lambdas: f = \x > e 18
19 Step 3: Add Constraints f x = 2 + x t_0 = t_1 > t_6 t_4 = t_1 > t_6 t_2 = t_3 > t_4 t_2 = Int > Int > Int t_3 = Int 19
20 Step 4: Solve Constraints t_0 = t_1 > t_6 t_4 = t_1 > t_6 t_2 = t_3 > t_4 t_2 = Int > Int > Int t_3 = Int t_0 = t_1 > t_6 t_4 = t_1 > t_6 t_4 = Int > Int t_2 = Int > Int > Int t_3 = Int t_3 > t_4 = Int > (Int > Int) t_3 = Int t_4 = Int > Int t_1 > t_6 = Int > Int t_0 = Int > Int t_1 = Int t_6 = Int t_4 = Int > Int t_2 = Int > Int > Int t_3 = Int t_1 = Int t_6 = Int 20
21 Step 5: Determine type of declaranon t_0 = Int > Int t_1 = Int t_6 = Int t_4 = Int > Int t_2 = Int > Int > Int t_3 = Int f x = 2 + x > f :: Int > Int 21
22 Type Inference Algorithm Parse program to build parse tree Assign type variables to nodes in tree Generate constraints: From environment: constants (2), builtin operators (+), known funcnons (tail). From form of parse tree: e.g., applicanon and abstracnon nodes. Solve constraints using Determine types of toplevel declaranons 22
23 Inferring Polymorphic Types Example: Step 1: Build Parse Tree f g = g 2 > f :: (Int > t_4) > t_4 23
24 Inferring Polymorphic Types Example: Step 2: Assign type variables f g = g 2 > f :: (Int > t_4) > t_4 24
25 Inferring Polymorphic Types Example: Step 3: Generate constraints t_0 = t_1 > t_4 t_1 = t_3 > t_4 t_3 = Int f g = g 2 > f :: (Int > t_4) > t_4 25
26 Inferring Polymorphic Types Example: Step 4: Solve constraints t_0 = t_1 > t_4 t_1 = t_3 > t_4 t_3 = Int f g = g 2 > f :: (Int > t_4) > t_4 t_0 = (Int > t_4) > t_4 t_1 = Int > t_4 t_3 = Int 26
27 Inferring Polymorphic Types Example: Step 5: Determine type of toplevel declaranon Unconstrained type variables become polymorphic types. f g = g 2 > f :: (Int > t_4) > t_4 t_0 = (Int > t_4) > t_4 t_1 = Int > t_4 t_3 = Int 27
28 Using Polymorphic FuncNons FuncNon: f g = g 2 > f :: (Int > t_4) > t_4 Possible applicanons: add x = 2 + x > add :: Int > Int f add > 4 :: Int iseven x = mod (x, 2) == 0 > iseven:: Int > Bool f iseven > True :: Int 28
29 Recognizing Type Errors FuncNon: f g = g 2 > f :: (Int > t_4) > t_4 Incorrect use not x = if x then True else False > not :: Bool > Bool f not > Error: operator and operand don t agree operator domain: Int > a operand: Bool > Bool Type error: cannot unify Bool Bool and Int t 29
30 Polymorphic Datatypes FuncNons may have mulnple clauses, and may be recursive length [] = 0 length (x:rest) = 1 + (length rest) Type inference Infer separate type for each clause Add constraint that all clauses must have the same type Recursive calls: funcnon has same type as its defininon 30
31 Type Inference with Datatypes Example: Step 1: Build Parse Tree length (x:rest) = 1 + (length rest) 31
32 Type Inference with Datatypes Example: length (x:rest) = 1 + (length rest) Step 2: Assign type variables 32
33 Type Inference with Datatypes Example: length (x:rest) = 1 + (length rest) Step 3: Generate constraints t_0 = t_3 > t_10 t_3 = t_2 t_3 = [t_1] t_6 = t_9 > t_10 t_4 = t_5 > t_6 t_4 = Int > Int > Int t_5 = Int t_0 = t_2 > t_9 33
34 Type Inference with Datatypes Example: Step 3: Solve Constraints length (x:rest) = 1 + (length rest) t_0 = t_3 > t_10 t_3 = t_2 t_3 = [t_1] t_6 = t_9 > t_10 t_4 = t_5 > t_6 t_4 = Int > Int > Int t_5 = Int t_0 = t_2 > t_9 t_0 = [t_1] > Int 34
35 MulNple Clauses FuncNon with mulnple clauses Infer type of each clause First clause: append ([],r) = r append (x:xs, r) = x : append (xs, r) > append :: ([t_1], t_2) > t_2 Second clause: > append :: ([t_3], t_4) > [t_3] Combine by equanng types of two clauses > append :: ([t_1], [t_1]) > [t_1] 35
36 Most General Type Type inference produces the most general type map (f, [] ) = [] map (f, x:xs) = f x : map (f, xs) > map :: (t_1 > t_2, [t_1]) > [t_2] FuncNons may have many less general types > map :: (t_1 > Int, [t_1]) > [Int] > map :: (Bool > t_2, [Bool]) > [t_2] > map :: (Char > Int, [Char]) > [Int] Less general types are all instances of most general type, also called the principal type 36
37 History Original type inference algorithm Invented by Haskell Curry and Robert Feys for the simply typed lambda calculus in 1958 In 1969, Hindley extended the algorithm to a richer language and proved it always produced the most general type In 1978, Milner independently developed equivalent algorithm, called algorithm W, during his work designing ML. In 1982, Damas proved the algorithm was complete. Currently used in many languages: ML, Ada, Haskell, C# 3.0, F#, Visual Basic.Net 9.0. Have been plans for Fortress, Perl 6, C+ +0x, A bit also in Java. 37
38 Complexity of Type Inference Algorithm When Hindley/Milner type inference algorithm was developed, its complexity was unknown In 1989, Kanellakis, Mairson, and Mitchell proved that the problem was exponennal Nme complete Usually linear in pracnce though Running Nme is exponennal in the depth of polymorphic declaranons 38
Principles of Programming Languages
Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp16/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 24! Type inference in ML / Haskell 1 Type Checking vs
More informationTypes 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
More informationTypes 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
More informationTypes, 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,
More informationcs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides.
cs242 Kathleen Fisher Reading: Concepts in Programming Languages, Chapter 6 Thanks to John Mitchell for some of these slides. We are looking for homework graders. If you are interested, send mail to cs242cs.stanford.edu
More informationPrinciples 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,
More information301AA  Advanced Programming [AP2017]
301AA  Advanced Programming [AP2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP201704: Run&me
More informationPolymorphism and Type Inference
Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.) Compiletime
More informationPolymorphism and Type Inference
Polymorphism and Type Inference Volker Stolz stolz@ifi.uio.no INF 31102008 Department of Informatics University of Oslo Initially by Gerardo Schneider. Based on John C. Mitchell s slides (Stanford U.)
More information301AA  Advanced Programming [AP2017]
301AA  Advanced Programming [AP2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP201712: Polymorphisms
More informationType 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.
More informationPierce Ch. 3, 8, 11, 15. Type Systems
Pierce Ch. 3, 8, 11, 15 Type Systems 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
More informationType 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
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 19942017 Values and Types We divide the universe of values according to types A type is a set of values and
More informationINF 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
More informationData 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
More information1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.
1. true / false By a compiler we mean a program that translates to code that will run natively on some machine. 2. true / false ML can be compiled. 3. true / false FORTRAN can reasonably be considered
More informationINF 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
More informationPrinciples 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 Introduc;on to Hakell Lesson 27! 1 The origins: ML programming
More informationG Programming Languages Spring 2010 Lecture 6. Robert Grimm, New York University
G22.2110001 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,
More informationLecture #23: Conversion and Type Inference
Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). Last modified: Fri Oct 20 10:46:40 2006 CS164:
More informationCS 360: Programming Languages Lecture 12: More Haskell
CS 360: Programming Languages Lecture 12: More Haskell Geoffrey Mainland Drexel University Adapted from Brent Yorgey s course Introduction to Haskell. Section 1 Administrivia Administrivia Homework 5 due
More informationConversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";
Lecture #23: Conversion and Type Inference Administrivia. Due date for Project #2 moved to midnight tonight. Midterm mean 20, median 21 (my expectation: 17.5). In Java, this is legal: Object x = "Hello";
More informationCSCIGA Scripting Languages
CSCIGA.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
More informationTypes. 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
More informationTopics 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
More informationCS558 Programming Languages
CS558 Programming Languages Winter 2018 Lecture 7b Andrew Tolmach Portland State University 19942018 Dynamic Type Checking Static type checking offers the great advantage of catching errors early And
More informationCS412/CS413. Introduction to Compilers Tim Teitelbaum. Lecture 17: Types and TypeChecking 25 Feb 08
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 17: Types and TypeChecking 25 Feb 08 CS 412/413 Spring 2008 Introduction to Compilers 1 What Are Types? Types describe the values possibly
More informationCS558 Programming Languages
CS558 Programming Languages Fall 2017 Lecture 7b Andrew Tolmach Portland State University 19942017 Type Inference Some statically typed languages, like ML (and to a lesser extent Scala), offer alternative
More information301AA  Advanced Programming [AP2017]
301AA  Advanced Programming [AP2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP201716: Haskell,
More informationStatic Semantics. Winter /3/ Hal Perkins & UW CSE I1
CSE 401 Compilers Static Semantics Hal Perkins Winter 2009 2/3/2009 200209 Hal Perkins & UW CSE I1 Agenda Static semantics Types Symbol tables General ideas for now; details later for MiniJava project
More informationTopics 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
More informationLists. Michael P. Fourman. February 2, 2010
Lists Michael P. Fourman February 2, 2010 1 Introduction The list is a fundamental datatype in most functional languages. ML is no exception; list is a builtin ML type constructor. However, to introduce
More information22c:111 Programming Language Concepts. Fall Types I
22c:111 Programming Language Concepts Fall 2008 Types I Copyright 200708, The McGrawHill Company and Cesare Tinelli. These notes were originally developed by Allen Tucker, Robert Noonan and modified
More informationCS558 Programming Languages
CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 19942016 Values and Types We divide the universe of values according to types A type is a set of values and a
More informationCMSC 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
More informationType 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
More informationOverloading, Type Classes, and Algebraic Datatypes
Overloading, Type Classes, and Algebraic Datatypes Delivered by Michael Pellauer Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. September 28, 2006 September 28, 2006 http://www.csg.csail.mit.edu/6.827
More informationProgramming Languages, Summary CSC419; Odelia Schwartz
Programming Languages, Summary CSC419; Odelia Schwartz Chapter 1 Topics Reasons for Studying Concepts of Programming Languages Programming Domains Language Evaluation Criteria Influences on Language Design
More informationAccording to Larry Wall (designer of PERL): a language by geniuses! for geniuses. Lecture 7: Haskell. Haskell 98. Haskell (cont)  Typesafe!
Lecture 7: Haskell CSC 131 Fall, 2014 Kim Bruce According to Larry Wall (designer of PERL): a language by geniuses for geniuses He s wrong at least about the latter part though you might agree when we
More informationCS 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
More informationSimplyTyped Lambda Calculus
#1 SimplyTyped Lambda Calculus #2 Back to School What is operational semantics? When would you use contextual (smallstep) semantics? What is denotational semantics? What is axiomatic semantics? What
More informationExample: 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
More informationCS 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
More informationIntroduction to Programming, AugDec 2006
Introduction to Programming, AugDec 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
More informationDiscussion. 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
More informationProgramming Language Concepts: Lecture 14
Programming Language Concepts: Lecture 14 Madhavan Mukund Chennai Mathematical Institute madhavan@cmi.ac.in http://www.cmi.ac.in/~madhavan/courses/pl2009 PLC 2009, Lecture 14, 11 March 2009 Function programming
More informationType Processing by Constraint Reasoning
, Martin Sulzmann, Jeremy Wazny 8th November 2006 Chameleon Chameleon is Haskellstyle language treats type problems using constraints gives expressive error messages has a programmable type system Developers:
More informationSemantic Analysis. How to Ensure TypeSafety. What Are Types? Static vs. Dynamic Typing. Type Checking. Last time: CS412/CS413
CS412/CS413 Introduction to Compilers Tim Teitelbaum Lecture 13: Types and TypeChecking 19 Feb 07 Semantic Analysis Last time: Semantic errors related to scopes Symbol tables Name resolution This lecture:
More information301AA  Advanced Programming [AP2017]
301AA  Advanced Programming [AP2017] Lecturer: Andrea Corradini andrea@di.unipi.it Tutor: Lillo GalleBa galleba@di.unipi.it Department of Computer Science, Pisa Academic Year 2017/18 AP201715: Func'onal
More informationCSCI312 Principles of Programming Languages!
CSCI312 Principles of Programming Languages! Chapter 5 Types Xu Liu! ! 5.1!Type Errors! 5.2!Static and Dynamic Typing! 5.3!Basic Types! 5.4!NonBasic Types! 5.5!Recursive Data Types! 5.6!Functions as Types!
More informationSEMANTIC ANALYSIS TYPES AND DECLARATIONS
SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether
More informationProcessadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)
Processadors de Llenguatge II Functional Paradigm Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra Paradigm Shift Imperative Paradigm State Machine
More informationCS 320: Concepts of Programming Languages
CS 320: Concepts of Programming Languages Wayne Snyder Computer Science Department Boston University Lecture 04: Basic Haskell Continued o Polymorphic Types o Type Inference with Polymorphism o Standard
More informationLecture 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
More informationNote 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
More informationCOS 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
More informationG Programming Languages  Fall 2012
G22.2110003 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
More informationFunctional Paradigm II
Processadors de Llenguatge II Functional Paradigm II Pratt A.7 Robert Harper s SML tutorial (Sec II) Rafael Ramirez Dep Tecnologia Universitat Pompeu Fabra UserDefined Types How to define the new type.
More informationCONTROL FLOW ANALYSES!
PROGRAMMING LANGUAGES LABORATORY! Universidade Federal de Minas Gerais  Department of Computer Science CONTROL FLOW ANALYSES! PROGRAM ANALYSIS AND OPTIMIZATION DCC888! Fernando Magno Quintão Pereira!
More informationLecture 19: Functions, Types and Data Structures in Haskell
The University of North Carolina at Chapel Hill Spring 2002 Lecture 19: Functions, Types and Data Structures in Haskell Feb 25 1 Functions Functions are the most important kind of value in functional programming
More informationHow does ML deal with +?
How does ML deal with +? Moscow ML version 2.00 (June 2000)  op +; > val it = fn : int * int > int  1 + 1; > val it = 2 : int  1.0 + 1.0; > val it = 2.0 : real  false + false;! Overloaded + cannot
More informationProgramming Languages Fall 2013
Programming Languages Fall 2013 Lecture 2: types Prof. Liang Huang huang@qc.cs.cuny.edu Recap of Lecture 1 functional programming vs. imperative programming basic Haskell syntax function definition lazy
More informationG Programming Languages  Fall 2012
G22.2110003 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?
More information02157 Functional Programming. Michael R. Ha. Lecture 2: Functions, Types and Lists. Michael R. Hansen
Lecture 2: Functions, Types and Lists nsen 1 DTU Compute, Technical University of Denmark Lecture 2: Functions, Types and Lists MRH 13/09/2018 Outline Functions as firstclass citizens Types, polymorphism
More informationCS 345. Functions. Vitaly Shmatikov. slide 1
CS 345 Functions Vitaly Shmatikov slide 1 Reading Assignment Mitchell, Chapter 7 C Reference Manual, Chapters 4 and 9 slide 2 Procedural Abstraction Can be overloaded (e.g., binary +) Procedure is a named
More informationIntroduction 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 Callbyvalue Operational Semantics
More informationA First Look at ML. Chapter Five Modern Programming Languages, 2nd ed. 1
A First Look at ML Chapter Five Modern Programming Languages, 2nd ed. 1 ML Meta Language One of the more popular functional languages (which, admittedly, isn t saying much) Edinburgh, 1974, Robin Milner
More informationCOP4020 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
More informationCS321 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
More informationPolymorphic lambda calculus Princ. of Progr. Languages (and Extended ) The University of Birmingham. c Uday Reddy
0602552 Princ. of Progr. Languages (and Extended ) The University of Birmingham Spring Semester 201617 School of Computer Science c Uday Reddy201617 Handout 6: Polymorphic Type Systems 1. Polymorphic
More informationBack to OCaml. Summary of polymorphism. Example 1. Type inference. Example 2. Example 2. Subtype. Polymorphic types allow us to reuse code.
Summary of polymorphism Subtype Parametric Bounded Fbounded Back to OCaml Polymorphic types allow us to reuse code However, not always obvious from staring at code But... Types never entered w/ program!
More informationSemantic Analysis. Outline. The role of semantic analysis in a compiler. Scope. Types. Where we are. The Compiler FrontEnd
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
More informationInformal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility
Informal Semantics of Data semantic specification names (identifiers) attributes binding declarations scope rules visibility 1 Ways to Specify Semantics Standards Documents (Language Definition) Language
More informationTail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)
CMSC 330: Organization of Programming Languages Tail Calls A tail call is a function call that is the last thing a function does before it returns let add x y = x + y let f z = add z z (* tail call *)
More informationFunctional 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 subexpressions (if any).
More informationRecursive Types and Subtyping
Recursive Types and Subtyping #1 OneSlide 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
More informationLecture 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. HernandezCampos, and D. Stotts Goals
More informationHarvard School of Engineering and Applied Sciences Computer Science 152
Harvard School of Engineering and Applied Sciences Computer Science 152 Lecture 17 Tuesday, March 30, 2010 1 Polymorph means many forms. Polymorphism is the ability of code to be used on values of different
More informationData 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,
More informationMore Lambda Calculus and Intro to Type Systems
More Lambda Calculus and Intro to Type Systems Plan Heavy Class Participation Thus, wake up! Lambda Calculus How is it related to real life? Encodings Fixed points Type Systems Overview Static, Dyamic
More informationThe 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
More informationSubtyping. Lecture 13 CS 565 3/27/06
Subtyping Lecture 13 CS 565 3/27/06 Polymorphism Different varieties of polymorphism: Parametric (ML) type variables are abstract, and used to encode the fact that the same term can be used in many different
More informationProgramming 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
More informationLanguage Sequence. The Algol Family and ML. Algol 60 Sample. Algol 60. Some trouble spots in Algol 60. Algol Joke. John Mitchell
CS 4 Language Sequence The Algol Family and ML Lisp Algol 60 John Mitchell Algol 68 Pascal ML Modula Reading: Chapter 5 Many other languages: Algol 58, Algol W, Euclid, EL, Mesa (PARC), Modula, Oberon,
More informationPolymorphism. Chapter Eight Modern Programming Languages, 2nd ed. 1
Polymorphism Chapter Eight Modern Programming Languages, 2nd ed. 1 Introduction Compare these function types The ML function is more flexible, since it can be applied to any pair of the same (equalitytestable)
More informationData Types The ML Type System
7 Data Types 7.2.4 The ML Type System The following is an ML version of the tailrecursive Fibonacci function introduced Fibonacci function in ML in Section 6.6.1: EXAMPLE 7.96 1. fun fib (n) = 2. let
More informationCSE 431S Type Checking. Washington University Spring 2013
CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks
More informationAdvanced oo concepts Specialization of behaviour? Multiple inheritance  alternatives to. Inner classes Classes
Types and Classes I II From predefined (simple) and userdefined (composite) types via Abstract data types Advanced oo concepts Specialization of behaviour? Multiple inheritance  alternatives to Inner
More informationint f(char a, char b) {
Polymorphism Chapter Eight Modern Programming Languages 1 Introduction Compare these function types The ML function is more flexible, since it can be applied to any pair of the same (equalitytestable)
More informationCSCC24 Functional Programming Typing, Scope, Exceptions ML
CSCC24 Functional Programming Typing, Scope, Exceptions ML Carolyn MacLeod 1 winter 2012 1 Based on slides by Anya Tafliovich, with many thanks to Gerald Penn and Sheila McIlraith. motivation Consider
More informationLecture #13: Type Inference and Unification. Typing In the Language ML. Type Inference. Doing Type Inference
Lecture #13: Type Inference and Unification Typing In the Language ML Examples from the language ML: fun map f [] = [] map f (a :: y) = (f a) :: (map f y) fun reduce f init [] = init reduce f init (a ::
More informationProgramming Languages Third Edition. Chapter 7 Basic Semantics
Programming Languages Third Edition Chapter 7 Basic Semantics Objectives Understand attributes, binding, and semantic functions Understand declarations, blocks, and scope Learn how to construct a symbol
More informationAnnouncements. CSCI 334: Principles of Programming Languages. Lecture 5: Fundamentals III & ML
Announcements CSCI 334: Principles of Programming Languages Lecture 5: Fundamentals III & ML Instructor: Dan Barowy Claire Booth Luce info session tonight for women interested in summer research (hopefully
More informationProgramming Languages
Programming Languages Types CSCIGA.2110001 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 /
More informationPrinciples of Programming Languages COMP251: Functional Programming in Scheme (and LISP)
Principles of Programming Languages COMP251: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology
More informationMotivation 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
More informationLecture 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,
More information1 Terminology. 2 Environments and Static Scoping. P. N. Hilfinger. Fall Static Analysis: Scope and Types
and Computer Sciences Computer Science Division CS 164 Fall 2006 P. N. Hilfinger Static Analysis: Scope and Types 1 Terminology Programs, in general, are simply collections of definitions of terms, which
More informationQuestions? Static Semantics. Static Semantics. Static Semantics. Next week on Wednesday (5 th of October) no
Questions? First exercise is online: http://www.win.tue.nl/~mvdbrand/courses/glt/1112/ Deadline 17 th of October Next week on Wednesday (5 th of October) no lectures!!! Primitive types Primitive value
More information