Designing Programming Languages to Improve Software Quality David J. Pearce Software Quality New Zealand, August

Slides:



Advertisements
Similar presentations
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
You can do more than what you think ……… If you believe you can.
IT 325 OPERATING SYSTEM C programming language. Why use C instead of Java Intermediate-level language:  Low-level features like bit operations  High-level.
Introduction to Computing Concepts Note Set 7. Overview Variables Data Types Basic Arithmetic Expressions ▫ Arithmetic.
The Java Modeling Language JML Erik Poll Digital Security Radboud University Nijmegen.
Type Checking.
Compiler Construction
Nov 10, Fall 2006IAT 8001 Debugging. Nov 10, Fall 2006IAT 8002 How do I know my program is broken?  Compiler Errors –easy to fix!  Runtime Exceptions.
Java Generics.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 18 Program Correctness To treat programming.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Introduction to Primitives. Overview Today we will discuss: –The eight primitive types, especially int and double –Declaring the types of variables –Operations.
1 Advanced Material The following slides contain advanced material and are optional.
Java Syntax Primitive data types Operators Control statements.
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.
Comparing Objects in Java. The == operator When you define an object, for instance Person p = new Person("John", 23); we talk about p as if its value.
CS/ENGRD 2110 SPRING 2013 Lecture 2: Introduction to Java 1.
David Streader Computer Science Victoria University of Wellington Copyright: David Streader, Victoria University of Wellington Objects Real and Java COMP.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Murali Sitaraman
11 Values and References Chapter Objectives You will be able to: Describe and compare value types and reference types. Write programs that use variables.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Presented by: Mojtaba Khezrian. Agenda Object Creation Object Storage More on Arrays Parameter Passing For Each VarArgs Spring 2014Sharif University of.
Software Engineering Prof. Dr. Bertrand Meyer March 2007 – June 2007 Chair of Software Engineering Static program checking and verification Slides: Based.
P Object type and wrapper classes p Object methods p Generic classes p Interfaces and iterators Generic Programming Data Structures and Other Objects Using.
Implementing a Language with Flow-Sensitive and Structural Subtyping on the JVM David J. Pearce and James Noble Victoria University of Wellington.
Programming Language C++ Xulong Peng CSC415 Programming Languages.
Basics of Java IMPORTANT: Read Chap 1-6 of How to think like a… Lecture 3.
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth
Today’s Agenda  Quick Review  Continue on JML Formal Methods in Software Engineering1.
CompSci 100E 2.1 Java Basics - Expressions  Literals  A literal is a constant value also called a self-defining term  Possibilities: o Object: null,
Generic Programming  Object Type  Autoboxing  Bag of Objects  JCL Collections  Nodes of Objects  Iterators.
The Whiley Programming Language David J. Pearce School of Engineering and Computer Science, Victoria University of Wellington, New Zealand.
Introduction to Java Java Translation Program Structure
Types in programming languages1 What are types, and why do we need them?
Copyright Curt Hill Variables What are they? Why do we need them?
Java Basics Opening Discussion zWhat did we talk about last class? zWhat are the basic constructs in the programming languages you are familiar.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
©SoftMoore ConsultingSlide 1 Generics “Generics constitute the most significant change in the Java programming language since the 1.0 release.” – Cay Horstmann.
SOEN 343 Software Design Section H Fall 2006 Dr Greg Butler
C++ for Java Programmers Chapter 2. Fundamental Daty Types Timothy Budd.
Programmeren 1 6 september 2010 HOORCOLLEGE 2: INTERACTIE EN CONDITIES PROGRAMMEREN 1 6 SEPTEMBER 2009 Software Systems - Programming - Week.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
David Evans CS201j: Engineering Software University of Virginia Computer Science Lecture 10: Programming Exceptionally.
 Data Type is a basic classification which identifies different types of data.  Data Types helps in: › Determining the possible values of a variable.
Java Bytecode Verification Types Chris Male, David J. Pearce, Alex Potanin and Constantine Dymnikov Victoria University of Wellington, New.
Objects and Memory Mehdi Einali Advanced Programming in Java 1.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Chair of Software Engineering Void safety these slides contain advanced material and are optional.
Functional Programming
Information and Computer Sciences University of Hawaii, Manoa
Objects Real and Java COMP T1 3
Types CSCE 314 Spring 2016.
Lecture 5: Some more Java!
Accessible Formal Methods A Study of the Java Modeling Language
C Basics.
Lesson 2: Building Blocks of Programming
Java Programming Language
Hoare-style program verification
JPure: a Modular Purity System for Java
Compiler Construction
Java Programming Language
Java Modeling Language (JML)
Assertions References: internet notes; Bertrand Meyer, Object-Oriented Software Construction; 4/25/2019.
Programming Languages 2nd edition Tucker and Noonan
Compiler Construction
Problem 1 Given n, calculate 2n
Presentation transcript:

Designing Programming Languages to Improve Software Quality David J. Pearce Software Quality New Zealand, August

Null Pointers “I call it my billion-dollar mistake. It was the invention of the null reference in 1965” -- Tony Hoare “Of all the exceptions a Java programmer might encounter, the null-pointer exception is among the most dreaded, and for good reason: it is one of the least informative exceptions that a program can signal.” -- Eric Allen

(from java/lang/String.java) Experiences with Null Pointers /** * … NullPointerException if sb is * null */ boolean contentEquals(StringBuffer sb) { synchronized(sb) { … } }

(also from java/lang/String.java) /** * Tests if this string starts with the * specified prefix. * prefix the prefix. true if the character sequence… */ public boolean startsWith(String prefix) { … char pa[] = prefix.value; … }

/** * Tests if this string starts with the * specified prefix. * prefix the prefix. true if the character sequence… */ public boolean startsWith(String prefix) { … char pa[] = prefix.value; … } Found 83/1101 java.lang methods were misdocumented!

Thoughts Why is documentation bad? –Because programmers write it –Programmers have deadlines –Documentation doesn’t help meet deadlines When is documentation not bad? –Think about types in Java –Method types automatically documented! –Compiler ensures they are accurate

@NonNull Types void Integer x) { x.toString(); // safe! } void g(Integer x) { x.toString(); // Syntax Error } void h(Integer x) { if(x != null) { x.toString(); // Safe! }

JML Specifications public class IntVec { private int data[]; private int length = 0; invariant data != null; invariant length >= 0; invariant length <= data.length; requires size > 0; public IntVec(int size) { data = new int[size]; } ensures \result >= 0; public int size() { return length; } …

More JML Specifications public requires s1 != null && s2 != requires s2.length==0 && s1.length==0; ensures !\result; ensures \result == (\exists int i; 0<=i && i<s1.length && s1[i]<s2[i] && (s1.length<s2.length && public static pure model boolean lessThan(char[] s1, char[] s2); (from JML spec of java/lang/String.java)

So … ? PROBLEM: modern languages make compile-time verification unnecessarily hard… ANSWER: design a language to simplify verification: –Pure Functions vs Impure Methods –Value Semantics –Structural subtyping & Flow Typing –Unbound Integers and Rationals –First-class Collections (sets, lists, maps) –Strict Concurrency Model (e.g. non-reentrancy)

Whiley

Overview What is Whiley? –Hybrid functional / imperative language –Designed specifically for verification –Compiles to JVM (also prototype C backend) Why another language? –Verification is really hard –Many features of Java it even harder! –I think it’s basically impossible for Java –See ESC/Java and JML as good efforts here

A Zoo of Unusual Types! Primitives: –e.g. Collections (lists, sets, maps): –e.g. Records and Tuples: –e.g. Unions and Intersections: –e.g. Negations –e.g. [int]{string}{int=>string} !int anyintrealcharnullbool{int x, int y}(int,int)int|nullint&null

Flow Typing

A flow-sensitive approach to type checking Types declared only for parameters and returns Variables can have different types! Conditionals and/or assignments cause retyping int sum([int] items): r = 0 for item in items: r = r + item return r

Flow Typing Type tests automatically retype variables! –(even on the false branch) define Circle as {int x, int y, int r} define Rect as {int x, int y, int w, int h} define Shape as Circle | Rect real area(Shape s): if s is Circle: return PI * s.r * s.r else: return s.w * s.h

Flow Typing & Unions Cannot treat null|int like an int Must distinguish cases by explicit type testing null|int indexOf(string str, char c): … [string] split(string str, char c): idx = indexOf(str,c) if idx is int: below = str[0..idx] above = str[idx..] return [below,above] else: return [str] Can safely treat x as int here

Verification

Function f() : –Accepts an arbitrary integer … –Should return a natural number … –But, this implementation is broken! define nat as int where $ >= 0 nat f(int x): return x A compile time error!

define nat as int where $ >= 0 nat f(int x): if x >= 0: return x else: return 0 Function f() : –Accepts an arbitrary integer … –Returns a natural number … –This implementation satisfies the spec! OK, because x implicitly a nat

Verification Function g() : –Accepts a positive number … –And returns a natural number … –But, how to know pos subtypes nat ? define nat as int where $ >= 0 define pos as int where $ > 0 nat g(pos x): return x OK, because pos implies nat

Verification Function h() : –Accepts a natural number … –And returns a positive number … –But, how to know nat+1 gives pos ? define nat as int where $ >= 0 define pos as int where $ > 0 pos h(nat x): return x + 1 OK, because nat+1 gives pos

Verification Function h1() and h2() are identical define nat as int where $ >= 0 define pos as int where $ > 0 pos h1(nat x): return x + 1 int h2(int x) requires x>=0, ensures $>0: return x + 1

Verification Function sum() : –Accepts a list of natural numbers … –Then adds them together … –And returns a natural number. define nat as int where $ >= 0 nat sum([nat] list): r = 0 for x in list where r >= 0: r = r + x return r Ok, because adding nat to nat gives nat

Implementation

Compiler Overview Developed 2009 – present 304 classes (80KLOC) with 934 end-end tests Also, 133 verifier tests and type system unit tests

Performance

Eclipse Plugin Update Site: