Compiling with Dependent Types Hongwei Xi University of Cincinnati.

Slides:



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

Question Bank. Explain the syntax of if else statement? Define Union Define global and local variables with example Concept of recursion with example.
Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
CSE Winter 2008 Introduction to Program Verification refining an interface.
Type Analysis and Typed Compilation Stephanie Weirich Cornell University.
Dependent Types in Practical Programming Hongwei Xi Oregon Graduate Institute.
Carnegie Mellon Lecture 7 Instruction Scheduling I. Basic Block Scheduling II.Global Scheduling (for Non-Numeric Code) Reading: Chapter 10.3 – 10.4 M.
Imperative Programming with Dependent Types Hongwei Xi University of Cincinnati.
1 Dependent Types for Termination Verification Hongwei Xi University of Cincinnati.
1 Today’s lecture  Last lecture we started talking about control flow in MIPS (branches)  Finish up control-flow (branches) in MIPS —if/then —loops —case/switch.
CS1104 – Computer Organization PART 2: Computer Architecture Lecture 4 Assembly Language Programming 2.
IT253: Computer Organization Lecture 6: Assembly Language and MIPS: Programming Tonga Institute of Higher Education.
101.  When you communicate with people you use a language that you both understand.  The trick is that the computer does not speak English.  To communicate.
Program Representations. Representing programs Goals.
Nicholas Moore Bianca Curutan Pooya Samizadeh McMaster University March 30, 2012.
1 Resource Bound Certification Stephanie Weirich Cornell University Joint work with Karl Crary, CMU.
CS 355 – Programming Languages
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 18.
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.
Typed Assembly Languages COS 441, Fall 2004 Frances Spalding Based on slides from Dave Walker and Greg Morrisett.
Dependently Typed Pattern Matching Hongwei Xi Boston University.
May 1, 2003May 1, Imperative Programming with Dependent Types Hongwei Xi Boston University.
Facilitating Program Verification with Dependent Types Hongwei Xi Boston University.
01/17/20031 Guarded Recursive Datatype Constructors Hongwei Xi and Chiyan Chen and Gang Chen Boston University.
Typed Memory Management in a Calculus of Capabilities David Walker (with Karl Crary and Greg Morrisett)
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.
STAL David Walker (joint work with Karl Crary, Neal Glew and Greg Morrisett)
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
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.
Advanced Type Systems for Low-Level Languages Greg Morrisett Cornell University.
Dependently Typed Data Structures Hongwei Xi presented by James Hook Pacific Software Research Center Oregon Graduate.
Compiled by Benjamin Muganzi 3.2 Functions and Purposes of Translators Computing 9691 Paper 3 1.
Proving termination conditions Name Country, City, University Omer Subasi Turkey, Istanbul, Koc University Anton Dergunov
Language Systems Chapter FourModern Programming Languages 1.
UNIVERSITI TENAGA NASIONAL “Generates Professionals” CHAPTER 4 : Part 2 INTRODUCTION TO SOFTWARE DEVELOPMENT: PROGRAMMING & LANGUAGES.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 11: Compiler II: Code Generation slide 1www.idc.ac.il/tecs.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
LANGUAGE SYSTEMS Chapter Four Modern Programming Languages 1.
Theory of Programming Languages Introduction. What is a Programming Language? John von Neumann (1940’s) –Stored program concept –CPU actions determined.
Secure Compiler Seminar 4/11 Visions toward a Secure Compiler Toshihiro YOSHINO (D1, Yonezawa Lab.)
11 Speed & Debug.  Lisp is really two languages:  A language for writing fast programs  A language for writing programs fast  In the early stage,
CS412/413 Introduction to Compilers and Translators Spring ’99 Lecture 11: Functions and stack frames.
Advanced Formal Methods Lecture 4: Isabelle – Types and Terms Mads Dam KTH/CSC Course 2D1453, Some material from Paulson.
C Programming Chapters 11, . . .
CS412/413 Introduction to Compilers and Translators April 2, 1999 Lecture 24: Introduction to Optimization.
CSE-321 Programming Languages Dependent Types POSTECH June 5, 2006 박성우.
FORTRAN Boz. Before FORTRAN Machine language and assembly code. Programmers had many problems to overcome.  Lack of instruction sets  Primitive stream.
Programming Languages Salihu Ibrahim Dasuki (PhD) CSC102 INTRODUCTION TO COMPUTER SCIENCE.
What Do Compilers Do 1 A compiler acts as a translator, transforming human-oriented programming languages into computer-oriented machine languages. Ignore.
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
SPACE COMPLEXITY & TIME COMPLEXITY 1. ALGORITHMS COMPLEXITY 2.
A Single Intermediate Language That Supports Multiple Implemtntation of Exceptions Delvin Defoe Washington University in Saint Louis Department of Computer.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Principles of programming languages 12: Functional programming
Imperative Programming with Dependent Types
Pick up the handout on your way in!!
The Metacircular Evaluator
Closure Representations in Higher-Order Programming Languages
Chapter 15 Debugging.
CSE-321 Programming Languages Introduction to Functional Programming
Course Overview PART I: overview material PART II: inside a compiler
Java Modeling Language (JML)
Programming Languages and Compilers (CS 421)
Presentation transcript:

Compiling with Dependent Types Hongwei Xi University of Cincinnati

What do we want to do? To produce (optimized) self-certified code that can guarantee both type safety and memory safety Self-certification is done through the use of a form of low-level dependent types Such low-level dependent types are to be translated from high-level dependent types in source programs

Source Language: Xanadu Xanadu (Xi, 2000) 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

A Program in Xanadu {n:nat} float dotprod (len: int(n), a[n]: float, b[n]: float) { var: nat i; float sum;; /* nat is [a: int | a >= 0] int(a) */ sum = 0.0; for (i = 0; i < len; i = i + 1) { sum = sum +. a[i] *. b [i]; } return sum; }

A Datatype in Xanadu A polymorphic datatype for lists: union list with nat = { Nil (0); Cons (n+1) of ‘a * list(n) }

Another Program in Xanadu (‘a){n:nat} int(n) length (xs: list(n)) { var: int x;; invariant: [i:nat,j:nat | i+j=n] (xs: list(i), x:int(j)) while (true) { switch (xs) { case Nil: return x; case Cons (_, xs): x = x+1; } } exit; /* can never be reached */ }

Target Language: DTAL DTAL is a dependently typed assembly language (Xi and Harper, 1999) DTAL was originally designed to extend TAL (Morrisett et al, 1998) with a form of dependent types adopted from DML (Xi and Pfenning, 1999) for certifying memory safety.

What is in this talk? This talk is focused on low-level data representation In particular, we show how dependent datatypes are represented We also briefly explain how a state type annotation in Xanadu is transformed into a state type in DTAL

Low-Level Types  = int | float |  | top (s) |    …  n   |  d;   |  i;  1, …,  n   | …  1 + … +  n =  a:nat n.  a    …  n  where nat n is {a:int | 0<= a < n}

Sizes of Types |int| = 1, |float| = 2 |top (s)| = s, |  | = 1 |  (d;  )| = 1 |  1,…,  n  | = |  1 | + … + |  n | |  (i;  1,…,  n ) | = max (|  1 |, …, |  n |) (currently, we require |   |  …  |  n |)

Tuple Type Representation  1, …,  n ) is represented flatly as 11 … nn

Pointer Type Representation  (d;  ) is represented as  d

An Example of Boxing The high-level type Int * float may be translated into the low-level type: ,  int,  float 

Tag Functions A tag function for (  1,…,  n ) has the following type  a:nat n.  (a;  1,…,  n )  int(a)

An Example of Tag Function Let  1 =  (1,  (int(0), int))  2 =  (1,  (int(1), float)) Then the following is a tag function for (  1,  2 ): tag (x) = { var y; load y, x(-1); ret y; }

An Example of Tagging (I) The high-level type int + float may be translated into the low-level type:  a:nat 2.  (a;  1,  2 ) where    int(0), int     int(1), float 

An Example of Tagging (II) int(0) / int(1)Int / float  a:nat 2.  (a;  (1,T(int(0), int)),  (1,T(int(1), float)))

An Interesting Question Would it be still useful to form  i    …   n  even if we could not find a tag function for    …  n) 

Another Example of Tagging The high-level type Int + float may also be translated into the low-level type:  a:nat 2. int(a)   (a;  1,   ) where  1 = int and  2 =  (0, float)

Recursive Sum Types (I) (  )list = unit +  (  )list  list   a:nat 2.  a;  int   int  list   list may be translated into  list

Recursive Sum Types (II) (  )list = unit +  (  )list null =  top  /* null pointer */  list   a:nat 2.  a; null  list   list may be translated into  list

Dependent Datatypes (I) union list with nat = { Nil (0); Cons (n+1) of ‘a * list(n) } Nil: list(0) Cons: {n:nat} ‘a * list(n)  list(n+1)

Dependent Datatypes (II) list = forall  n  nat.    unit    list  a  where   is n  and    is a:nat  n  a 

Dependent Datatypes (III) list = forall  n  nat.    int     int  list  a  where   is n  and    is a:nat  n  a 

A Code Fragment in Xanadu We now explain how the following code fragment is compiled invariant: [i:nat,j:nat | i+j=n] (xs: list(i), x:int(j)) while (true) { switch (xs) { case Nil: return x; case Cons (_, xs): x = x+1; } }

A Code Fragment in DTAL loop: (‘a,’r){i:nat,j:nat | i + j = n} [r1: list(i), r2:int(j), sp: [sp: int(n) :: ‘r] :: ‘r] unfold r1 /* for type-checking purpose */ load r3, r1(-1) beq r3, finish load r1, r1(1) add r2, r2, 1 jmp loop

Some Questions Would it be beneficial to introduce a low-level language with some structures instead of handling native code directly? In particular, would it be beneficial to introduce the notion of macros? How?

Conclusion Translating (dependent) types from source level to target level presents an effective approach to establishing properties on low-level code, which would often be difficult to prove from scratch This practice can also be of great help for debugging a compiler