May 1, 2003May 1, 2003 1 Imperative Programming with Dependent Types Hongwei Xi Boston University.

Slides:



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

CS3012: Formal Languages and Compilers Static Analysis the last of the analysis phases of compilation type checking - is an operator applied to an incompatible.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
Intermediate Code Generation
Exercise 1 Generics and Assignments. Language with Generics and Lots of Type Annotations Simple language with this syntax types:T ::= Int | Bool | T =>
Dependent Types in Practical Programming Hongwei Xi Oregon Graduate Institute.
Imperative Programming with Dependent Types Hongwei Xi University of Cincinnati.
U NIVERSITY OF D ELAWARE C OMPUTER & I NFORMATION S CIENCES D EPARTMENT Optimizing Compilers CISC 673 Spring 2009 Potential Languages of the Future Chapel,
March 4, 2005Susmit Sarkar 1 A Cost-Effective Foundational Certified Code System Susmit Sarkar Thesis Proposal.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
The Design and Implementation of a Certifying Compiler [Necula, Lee] A Certifying Compiler for Java [Necula, Lee et al] David W. Hill CSCI
Compiler Construction
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
1 CPS Transform for Dependent ML Hongwei Xi University of Cincinnati and Carsten Schürmann Yale University.
Extensible Verification of Untrusted Code Bor-Yuh Evan Chang, Adam Chlipala, Kun Gao, George Necula, and Robert Schneck May 14, 2004 OSQ Retreat Santa.
CSE 331 SOFTWARE DESIGN & IMPLEMENTATION TESTING II Autumn 2011.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 5 Types Types are the leaven of computer programming;
Dependently Typed Pattern Matching Hongwei Xi Boston University.
Facilitating Program Verification with Dependent Types Hongwei Xi Boston University.
ML: a quasi-functional language with strong typing Conventional syntax: - val x = 5; (*user input *) val x = 5: int (*system response*) - fun len lis =
1 A Dependently Typed Assembly Language Hongwei Xi University of Cincinnati and Robert Harper Carnegie Mellon University.
Safe Programming with Pointers through Stateful Views Dengping Zhu Hongwei Xi Boston University.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
A Dependently Typed Assembly Language Robert Harper (Joint work with Robert Harper)
A Type System for Expressive Security Policies David Walker Cornell University.
1 Dependent Types in Practical Programming Hongwei Xi University of Cincinnati.
Facilitating Programming Verification with Dependent Types Hongwei Xi University of Cincinnati.
Dependent Types for Reasoning About Distributed Systems Paul Sivilotti - Ohio State Hongwei Xi - Cincinnati.
CSE202: Lecture 14The Ohio State University1 Arrays.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Program Analysis Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
Dependently Typed Data Structures Hongwei Xi presented by James Hook Pacific Software Research Center Oregon Graduate.
Compiling with Dependent Types Hongwei Xi University of Cincinnati.
Copyright © 2006 The McGraw-Hill Companies, Inc. Programming Languages 2nd edition Tucker and Noonan Chapter 12 Imperative Programming I really hate this.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
Imperative Programming Part One. 2 Overview Outline the characteristics of imperative languages Discuss other features of imperative languages that are.
Chapter 5: Control Structures II (Repetition)
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Proof Carrying Code Zhiwei Lin. Outline Proof-Carrying Code The Design and Implementation of a Certifying Compiler A Proof – Carrying Code Architecture.
Extended Static Checking for Java  ESC/Java finds common errors in Java programs: null dereferences, array index bounds errors, type cast errors, race.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Introduction to Java Java Translation Program Structure
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Secure Compiler Seminar 4/11 Visions toward a Secure Compiler Toshihiro YOSHINO (D1, Yonezawa Lab.)
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
RUBY by Ryan Chase.
 Programming - the process of creating computer programs.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Semantic Analysis II Type Checking EECS 483 – Lecture 12 University of Michigan Wednesday, October 18, 2006.
SAFE KERNEL EXTENSIONS WITHOUT RUN-TIME CHECKING George C. Necula Peter Lee Carnegie Mellon U.
This Week Lecture on relational semantics Exercises on logic and relations Labs on using Isabelle to do proofs.
FUNCTIONS. Midterm questions (1-10) review 1. Every line in a C program should end with a semicolon. 2. In C language lowercase letters are significant.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
CS412/413 Introduction to Compilers Radu Rugina Lecture 11: Symbol Tables 13 Feb 02.
Operational Semantics Mooly Sagiv Tel Aviv University Sunday Scrieber 8 Monday Schrieber.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Types John Mitchell CS 242. Type A type is a collection of computable values that share some structural property. uExamples Integers Strings int  bool.
CSE-321 Programming Languages Dependent Types POSTECH June 5, 2006 박성우.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Types CSCE 314 Spring 2016.
ML: a quasi-functional language with strong typing
GC211Data Structure Lecture2 Sara Alhajjam.
Imperative Programming with Dependent Types
FP Foundations, Scheme In Text: Chapter 14.
Organization of Programming Languages
The Java switch Statement
Java Modeling Language (JML)
Presentation transcript:

May 1, 2003May 1, Imperative Programming with Dependent Types Hongwei Xi Boston University

May 1, Talk Overview Motivation – Detecting program errors (at compile-time) – Generating proof-carrying code Programming language Xanadu: – Design decisions – Dependent type system – Programming examples Current Status and Future Work

May 1, A Wish List We would like to have a programming language that should – be simple and general – support extensive error checking – facilitate proofs of program properties – possess correct and efficient implementation –......

May 1, Reality Invariably, there are many conflicts among this wish list These conflicts must be resolved with careful attention to the needs of the user

May 1, Advantages of Types Capturing errors at compile-time Enabling compiler optimizations Facilitating program verification – Using types to encode program properties and verifying the encoded properties through type- checking Serving as program documentation – Unlike informal comments, types can be fully trusted after type-checking

May 1, Limitations of (Simple) Types Not general enough – Many correct programs cannot be typed – For instance, type casts are widely used in C Not specific enough – Many interesting properties cannot be captured – For instance, types in Java cannot handle safe array access

May 1, Narrowing the Gap NuPrl Coq Program ExtractionProof synthesis ML with Dependent Types ML

May 1, Informal Program Comments /* the function should not be applied to a negative integer */ int factorial (x: int) { if (x < 0) exit(1); /*defensive programming*/ if (x == 0) return 1; else return (x * factorial (x-1)); }

May 1, Formalizing Program Comments {n:nat} int factorial (x: int(n)) { if (x == 0) return 1; else return (x * factorial (x-1)); } Note: factorial (-1) is ill-typed and rejected!

May 1, Informal Program Comments /* arrays a and b are of equal size */ float dotprod (float a[], float b[]) { int i; float sum = 0.0; if (a.size != b.size) exit(1); for (i = 0; i < a.size; i = i + 1) { sum = sum + a[i] * b[i]; } return sum; }

May 1, Formalizing Program Comments {n:nat} float dotprod (a: array(n), b: array(n)) { /* dotprod is assigned the following type: {n:nat}. ( array(n), array(n)) -> float */ /* function body */ … … … }

May 1, Dependent Types Dependent types are types that are – more refined – dependent on the values of expressions Examples – int(i): singleton type containing only integer i – array(n): type for integer arrays of size n

May 1, Type System Design A practically useful type system should be – Scalable – Applicable – Comprehensible – Unobtrusive – Flexible

May 1, Xanadu Xanadu is a dependently typed imperative programming language with C-like syntax The type of a variable in Xanadu can change during execution The programmer may need to provide dependent type annotations for type- checking purpose

May 1, Early Design Decisions Practical type-checking Realistic programming features Conservative extension Pay-only-if-you-use policy

May 1, Examples of Dependent Types (I) int(a): singleton types containing the only integer equal to a, where a ranges over all integers array(a): types for arrays of size a in which all elements are of type ‘a, where ‘a ranges over all natural numbers

May 1, Examples of Dependent Types (II) int(i,j) is defined as [a:int | i < a < j] int(a), that is, the sum of all types int(a) for i < a < j int[i,j), int(i,j], int[i,j] are defined similarly nat is defined as [a:int | a >=0] int(a)

May 1, A Xanadu Program {n:nat} unit init (int vec[n]) { var: int ind, size;; /* arraysize: {n:nat} array(n) -> int(n) */ size = arraysize(vec); invariant: [i:nat] (ind: int(i)) for (ind=0; ind<size; ind=ind+1){ vec[ind] = ind; }

May 1, A Slight Variation {n:nat} unit init (int vec[n]) { var: nat ind, size;; /* arraysize: {n:nat} array(n)-> int(n) */ size = arraysize(vec); for (ind=0; ind<size; ind=ind+1){ vec[ind] = ind; }

May 1, Dependent Record Types A polymorphic type for arrays {n:nat} array(n) { size: int(n); data[n]: ‘a }

May 1, Binary Search in Xanadu {n:nat} int bs(key: int, vec: array(n)) { var: l: int [0, n], h: int [-1, n); int m, x;; l = 0; h = vec.size - 1; while (l <= h) { m = (l + h) / 2; x = vec.data[m]; if (x < key) { l = m - 1; } else if (x > key) { h = m + 1; } else { return m; } } return –1; }

May 1, Dependent Record Types A polymorphic type for 2-dimensional arrays: {m:nat,n:nat} array2(m,n) { row: int(m); col: int(n); data[m][n]: ‘a }

May 1, Dependent Record Types A polymorphic type for sparse arrays: {m:nat,n:nat} sparseArray(m,n) { row: int(m); col: int(n); data[m]: list }

May 1, Dependent Union Types A polymorphic type for lists: union list with nat = { Nil(0); {n:nat} Cons(n+1) of ‘a * list(n) } Nil: list(0) Cons: {n:nat}‘a * list(n)-> list(n+1)

May 1, Dependent Union Types A polymorphic type for binary trees: union tree with (nat,nat) = { E(0,0); {sl:nat,sr:nat,hl:nat,hr:nat} B(sl+sr+1,1+max(hl,hr)) of tree(sl,hl) * ‘a * tree(sr,hr) }

May 1, Typing Judgment in Xanadu  e   Context for index variables   Context for variables with mutable types   Context for variables with fixed types

May 1, Typing Assignment       e            x  e      x  unit 

May 1, Typing Loop          e 1      bool  i     i    e       unit             while  e   e     i    unit 

May 1, Reverse Append in Xanadu (‘a) {m:nat,n:nat} list(m+n) revApp (xs: list(m),ys: list(n)) { var: ‘a x;; invariant: [m1:nat,n1:nat | m1+n1=m+n] (xs: list(m1), ys: list(n1)) while (true) { switch (xs) { case Nil: return ys; case Cons (x, xs): ys = Cons(x, ys); } } exit; /* can never be reached */ }

May 1, Constraint Generation in Type- checking The following integer constraint is generated when the revApp example is type-checked: m:nat,n:nat, m1:nat,n1:nat, m1+n1=m+n, a:nat, m1=a+1 |= a+(n1+1)=m+n

May 1, Current Status of Xanadu A prototype implementation of Xanadu in Objective Caml that – performs two-phase type-checking, and – generates assembly level code An interpreter for interpreting assembly level code A variety of examples at

May 1, Conclusion (I) It is still largely an elusive goal in practice to verify the correctness of a program It is therefore important to identify those program properties that can be effectively verified for realistic programs

May 1, Conclusion (II) We have designed a type-theoretic approach to capturing simple arithmetic reasoning The preliminary studies indicate that this approach allows the programmer to capture many more properties in realistic programs while retaining practical type-checking

May 1, Future Work Adding more programming features into Xanadu – in particular, OO features Type-preserving compilation: constructing a compiler for Xanadu that can translate dependent types from source level into bytecode level Incorporating dependent types into (a subset of) Java and …

May 1, Related Work Here is a (partial) list of some closely related work. – Dependent types in practical programming (Xi & Pfenning) – TALC Compiler (Morrisett et al at Cornell) – Safe C compiler (Necula & Lee) – TIL compiler (the Fox project at CMU)