Types and Programming Languages Lecture 1 Simon Gay Department of Computing Science University of Glasgow 2006/07.

Slides:



Advertisements
Similar presentations
Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Advertisements

Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Computing Science 1P Large Group Tutorial 19 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 8 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 13 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 15 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CS1Q Computer Systems Lecture 14
Lecture 19. Reduction: More Undecidable problems
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
Compilation 2011 Static Analysis Johnni Winther Michael I. Schwartzbach Aarhus University.
David Evans CS655: Programming Languages University of Virginia Computer Science Lecture 19: Minding Ps & Qs: Axiomatic.
CSE 341, Winter Type Systems Terms to learn about types: –Type –Type system –Statically typed language –Dynamically typed language –Type error –Strongly.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
Type Checking Compiler Design Lecture (02/25/98) Computer Science Rensselaer Polytechnic.
Type Checking.
Comp 205: Comparative Programming Languages Semantics of Imperative Programming Languages denotational semantics operational semantics logical semantics.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
CIS 101: Computer Programming and Problem Solving Lecture 8 Usman Roshan Department of Computer Science NJIT.
CPSC 411, Fall 2008: Set 12 1 CPSC 411 Design and Analysis of Algorithms Set 12: Undecidability Prof. Jennifer Welch Fall 2008.
1 Module 9 Recursive and r.e. language classes –representing solvable and half-solvable problems Proofs of closure properties –for the set of recursive.
Lecture 8 Recursively enumerable (r.e.) languages
1 Undecidability Andreas Klappenecker [based on slides by Prof. Welch]
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
Introduction to Computers and Programming Lecture 5 Boolean type; if statement Professor: Evan Korth New York University.
1 Sharing Objects – Ch. 3 Visibility What is the source of the issue? Volatile Dekker’s algorithm Publication and Escape Thread Confinement Immutability.
Introduction to a Programming Environment
Describing Syntax and Semantics
Halting Problem. Background - Halting Problem Common error: Program goes into an infinite loop. Wouldn’t it be nice to have a tool that would warn us.
Cs164 Prof. Bodik, Fall Symbol Tables and Static Checks Lecture 14.
Fundamentals of Python: From First Programs Through Data Structures
Fundamentals of Python: First Programs
Cs3102: Theory of Computation Class 18: Proving Undecidability Spring 2010 University of Virginia David Evans.
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
Names Variables Type Checking Strong Typing Type Compatibility 1.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 CST 221 OBJECT ORIENTED PROGRAMMING(OOP) ( 2 CREDITS.
Introduction Algorithms and Conventions The design and analysis of algorithms is the core subject matter of Computer Science. Given a problem, we want.
Section 3.1: Proof Strategy Now that we have a fair amount of experience with proofs, we will start to prove more difficult theorems. Our experience so.
CS1Q Computer Systems Lecture 6 Simon Gay. Lecture 6CS1Q Computer Systems - Simon Gay2 Algebraic Notation Writing AND, OR, NOT etc. is long-winded and.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Chapter 2 Introducing Interfaces Summary prepared by Kirk Scott.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
CS1Q Computer Systems Lecture 6 Simon Gay. Lecture 6CS1Q Computer Systems - Simon Gay2 Algebraic Notation Writing AND, OR, NOT etc. is long-winded and.
Types and Programming Languages Lecture 11 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CMSC 202 Advanced Section Classes and Objects: Object Creation and Constructors.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
Types and Programming Languages
CSE 311 Foundations of Computing I Lecture 28 Computability: Other Undecidable Problems Autumn 2011 CSE 3111.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
Chapter 4Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapters 4 and 5: Excerpts l Class and Method Definitions l Information.
CSSE501 Object-Oriented Development. Chapter 10: Subclasses and Subtypes  In this chapter we will explore the relationships between the two concepts.
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Fundamentals of Informatics Lecture 12 The Halting Problem Bas Luttik.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
CSE 501N Fall ‘09 03: Class Members 03 September 2009 Nick Leidenfrost.
CompSci Today’s Topics Computer Science Noncomputability Upcoming Special Topic: Enabled by Computer -- Decoding the Human Genome Reading Great.
Dr. Philip Cannata 1 Programming Languages Chapters 24 – 29 Types, Type Judgments, Type Systems and Type Safety - Chapters Explicit Polymorphism.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types for Programs and Proofs
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Algorithm Analysis CSE 2011 Winter September 2018.
CSE 311 Foundations of Computing I
Halting Problem.
Impossible problems.
Presentation transcript:

Types and Programming Languages Lecture 1 Simon Gay Department of Computing Science University of Glasgow 2006/07

Types and Programming Languages Lecture 1 - Simon Gay2 Introduction Type definitions and declarations are essential aspects of high-level programming languages. Example (Java): class Example { int a; void set(int x) {a=x;} int get() {return a;} } Example e = new Example();

2006/07Types and Programming Languages Lecture 1 - Simon Gay3 Compile-time (static) typechecking is standard 7.e.set(Hello); ^ ^ *** Semantic Error: No applicable overload for the method named set was found in type Example. Perhaps you wanted the overloaded version void set(int x); instead? 8.e.show(); ^------^ *** Semantic Error: No method named show was found in type Example. From the Jikes Java compiler (applied to some other code):

2006/07Types and Programming Languages Lecture 1 - Simon Gay4 Compile-time (static) typechecking 9.i = e + 1; ^ *** Semantic Error: The type of this expression, Example, is not numeric.

2006/07Types and Programming Languages Lecture 1 - Simon Gay5 Type declarations Organize data into high-level structures essential for high-level programming Document the program basic information about the meaning of variables and functions Inform the compiler example: how much storage each value needs Specify simple aspects of the behaviour of functions types as specifications is an important idea

2006/07Types and Programming Languages Lecture 1 - Simon Gay6 Typechecking Static (compile-time) or dynamic (run-time) static is better: finds errors sooner, doesnt degrade performance (we will concentrate on static typechecking) Verifies that the programmers intentions (expressed by declarations) are observed by the program A program which typechecks is guaranteed to behave well at run-time at least: never apply an operation to the wrong type of value more: eg. security properties A program which typechecks respects the high-level abstractions eg: public/protected/private access in Java

2006/07Types and Programming Languages Lecture 1 - Simon Gay7 Why study types (and programming languages)? The type system of a language has a strong effect on the feel of programming. Examples: In original Pascal, the result type of a function cannot be an array type. In Java, an array is just an object and arrays can be used anywhere. In Haskell, programming with lists is very easy; in Java it is much less natural. To understand a language fully, we need to understand its type system. We will see underlying typing concepts appearing in different languages in different ways, helping us to compare and understand language features.

2006/07Types and Programming Languages Lecture 1 - Simon Gay8 Why study types (and programming languages)? How does a language designer (or a programmer) know that correctly-typed programs really have the desired run-time properties? To answer this question we need to see how to specify type systems, and how to prove that a type system is sound. To prove soundness we also need to specify the semantics (meaning) of programs - what happens when they are run. So studying types will lead us to a deeper understanding of the meaning of programs. Example of the issue of soundness: variant records in Pascal

2006/07Types and Programming Languages Lecture 1 - Simon Gay9 Variant records in Pascal type kind = (staff,student); type person = record name : string; case k : kind of staff : (office : string) student : (year : integer) end; The following error cannot be detected by static typechecking: var simon : person; begin simon.name := Simon; simon.k := staff; simon.office := G093; write(simon.year + 5); end

2006/07Types and Programming Languages Lecture 1 - Simon Gay10 Variant records in Ada type kind = (staff,student); type person(k : kind) is record name : String; case k is when staff => (office : String) when student => (year : Integer) end case; end record; Now we must declare simon : person(staff) and then simon.year is never allowed.

2006/07Types and Programming Languages Lecture 1 - Simon Gay11 Possibilities and limitations of typechecking If types are specifications, can typechecking be used to verify program properties beyond correct use of data and functions? Yes, for example: secrecy and authenticity properties of security protocols behavioural properties (eg. deadlock-freedom) in concurrent systems But there are limits: most interesting properties cannot be automatically verified, even in principle, so types can only ever give a safe approximation to correctness. Also, in practice we want typechecking to be efficient.

2006/07Types and Programming Languages Lecture 1 - Simon Gay12 Typechecking as a safe approximation For any static type system, and the notion of correctness which it aims to guarantee: It is essential that every typable program is correct. It is usually impossible to ensure that every correct program is typable. Typechecking must not accept any incorrect programs but always rejects some correct programs. Exercise: write down a fragment of Java code which will not typecheck but which, if executed, would not misuse any data.

2006/07Types and Programming Languages Lecture 1 - Simon Gay13 Answer to exercise if (1 == 2) { int x = Hello * 5; } The Java typechecker assumes that every branch of a conditional statement may be executed (even if the condition is a compile-time constant or even a boolean literal). In general it is impossible to predict the value of an arbitrary expression at compile-time.

2006/07Types and Programming Languages Lecture 1 - Simon Gay14 Principles Programming is difficult and we need all the automated help we can get! Static typechecking is one approach to program analysis. It has been enormously beneficial. Exact program analysis is impossible in general. Typechecking aims for limited guarantees of correctness, and inevitably rejects some correct programs. A type system restricts programming style, sometimes to an undesirable extent. The challenge in type system design: allow flexibility in programming, but not so much flexibility that incorrect programs can be expressed.

2006/07Types and Programming Languages Lecture 1 - Simon Gay15 Why exact program analysis is impossible This is probably familiar from any course on computability… Some problems are undecidable - it is impossible to construct an algorithm which will solve arbitrary instances. The basic example is the Halting Problem: does a given program halt (terminate) when presented with a certain input? Problems involving exact prediction of program behaviour are generally undecidable, for example: does a program generate a run-time type error? does a program output the string Hello? We cant just run the program and see what happens, because there is no upper limit on the execution time of programs.

2006/07Types and Programming Languages Lecture 1 - Simon Gay16 Undecidability of the Halting Problem (HP) Instance of HP: a legal program P and an input string S for P. Question: does P halt when run on S? Theorem: HP is undecidable Proof: by contradiction - we assume that we have an algorithm A that solves HP, and discover that the consequence of this assumption is a logical impossibility. Therefore the assumption must be false. Concretely, say P is a Java function: void P(String S); Let H be an implementation of algorithm A as a Java function: boolean H(String X, String S); Any language can be substituted for Java.

2006/07Types and Programming Languages Lecture 1 - Simon Gay17 Undecidability of the Halting Problem (HP) Using H we can define a Java function Q: Q is given a legal Java function W Q uses H to determine whether or not W halts when given its own text as input if W halts then Q enters an infinite loop, otherwise Q halts void Q(String W) { if (H(W,W)) { while (true) {} }

2006/07Types and Programming Languages Lecture 1 - Simon Gay18 Undecidability of the Halting Problem (HP) Now we run Q with its own text as input. What happens? Q(void Q(String W) {…}) Q calls H(Q,Q) which returns either true or false. If H returns true then: from the definition of H, Q halts on input Q. from the definition of Q, Q loops on input Q. CONTRADICTION! If H returns false then: from the definition of H, Q loops on input Q. from the definition of Q, Q halts on input Q. CONTRADICTION! Therefore Q cannot exist.

2006/07Types and Programming Languages Lecture 1 - Simon Gay19 Consequences for program analysis The question Does program P generate a run-time type error with input S? is undecidable. If R were a decision procedure for this question then we could solve HP for program P by using R to analyse void NewP(String S) { P(S); int x = 1 + Hello; } because NewP generates a type error if and only if P halts. Similarly for any other behavioural property of programs.

2006/07Types and Programming Languages Lecture 1 - Simon Gay20 All is not lost… This sounds rather bleak, but: static analysis (including type systems) is a huge and successful area incomplete analysis (remember: safe approximation) is better than no analysis, as long as not too many correct programs are ruled out A major trend in programming language development has been the inclusion of more sophisticated type systems in mainstream languages. By studying more powerful type systems, we can get a glimpse of what the next generation of languages might look like.

2006/07Types and Programming Languages Lecture 1 - Simon Gay21 Administrative details Two lectures + one tutorial per week (Mon 11, Wed 9, Fri 10). Copies of presentations will be handed out. Some additional notes will be produced. There will be an assessed exercise (worth 20%) and an exam. A sample exam paper will be produced. I can be contacted by or in my office (G093) within reason. Books: Types and Programming Languages, B. C. Pierce compiler books Type Systems, L. Cardelli (material for reading course) Course web page:

2006/07Types and Programming Languages Lecture 1 - Simon Gay22 Reading and Exercises Read Chapter 1 of Pierce. Refer to Chapter 2 of Pierce as necessary, during the rest of the course. For each hour of timetabled teaching you should expect to spend at least another hour on private study. After each lecture I will recommend reading from Pierces book; often I will also assign exercises from the book or from additional worksheets. During the tutorial sessions we can discuss any problems which arise from the reading or the exercises. For now: Read Sections 1 and 2 of Linear Types for Packet Processing.