1 CPS Transform for Dependent ML Hongwei Xi University of Cincinnati and Carsten Schürmann Yale 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

Types and Programming Languages Lecture 7 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Kathleen Fisher cs242 Reading: “Concepts in Programming Languages”, Chapter 6.
An Abstract Interpretation Framework for Refactoring P. Cousot, NYU, ENS, CNRS, INRIA R. Cousot, ENS, CNRS, INRIA F. Logozzo, M. Barnett, Microsoft Research.
Static and User-Extensible Proof Checking Antonis StampoulisZhong Shao Yale University POPL 2012.
Certified Typechecking in Foundational Certified Code Systems Susmit Sarkar Carnegie Mellon University.
Cs776 (Prasad)L4Poly1 Polymorphic Type System. cs776 (Prasad)L4Poly2 Goals Allow expression of “for all types T” fun I x = x I : ’a -> ’a Allow expression.
Dependent Types in Practical Programming Hongwei Xi Oregon Graduate Institute.
Current Techniques in Language-based Security David Walker COS 597B With slides stolen from: Steve Zdancewic University of Pennsylvania.
Imperative Programming with Dependent Types Hongwei Xi University of Cincinnati.
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.
Functional Design and Programming Lecture 11: Functional reasoning.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
CS 355 – Programming Languages
An Integration of Program Analysis and Automated Theorem Proving Bill J. Ellis & Andrew Ireland School of Mathematical & Computer Sciences Heriot-Watt.
CLF: A Concurrent Logical Framework David Walker Princeton (with I. Cervesato, F. Pfenning, K. Watkins)
1 Meta-Programming through Typeful Code Representation Chiyan Chen and Hongwei Xi Boston University.
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.
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.
10 September Implementing Staged Computation Chiyan Chen and Hongwei Xi Boston University.
1 Dependent Types in Practical Programming Hongwei Xi University of Cincinnati.
Facilitating Programming Verification with Dependent Types Hongwei Xi University of Cincinnati.
Mechanized Metatheory for User- Defined Type Extensions Dan Marino, Brian Chin, Todd Millstein UCLA Gang Tan Boston College Robert J. Simmons, David Walker.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Compiling with Dependent Types Hongwei Xi University of Cincinnati.
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
Types for Programs and Proofs Lecture 1. What are types? int, float, char, …, arrays types of procedures, functions, references, records, objects,...
Verification and Validation in the Context of Domain-Specific Modelling Janne Merilinna.
CS 363 Comparative Programming Languages Semantics.
Computer Science School of Computing Clemson University Discrete Math and Reasoning about Software Correctness Joseph E. Hollingsworth
Towards Automatic Verification of Safety Architectures Carsten Schürmann Carnegie Mellon University April 2000.
Introduction to Problem Solving. Steps in Programming A Very Simplified Picture –Problem Definition & Analysis – High Level Strategy for a solution –Arriving.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L06-1 September 26, 2006http:// Type Inference September.
Semantics In Text: Chapter 3.
Implementing a Dependently Typed λ -Calculus Ali Assaf Abbie Desrosiers Alexandre Tomberg.
12/9/20151 Programming Languages and Compilers (CS 421) Elsa L Gunter 2112 SC, UIUC Based in part on slides by Mattox.
13 Aug 2013 Program Verification. Proofs about Programs Why make you study logic? Why make you do proofs? Because we want to prove properties of programs.
SAFE KERNEL EXTENSIONS WITHOUT RUN-TIME CHECKING George C. Necula Peter Lee Carnegie Mellon U.
COMP 412, FALL Type Systems II C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Types and Programming Languages Lecture 3 Simon Gay Department of Computing Science University of Glasgow 2006/07.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Prof. Necula CS 164 Lecture 171 Operational Semantics of Cool ICOM 4029 Lecture 10.
CS5205Semantics1 CS5205: Foundation in Programming Languages Semantics Static Semantics Dynamic Semantics Operational Semantics Big-step Small-Step Denotational.
CSE-321 Programming Languages Dependent Types POSTECH June 5, 2006 박성우.
COMP 412, FALL Type Systems C OMP 412 Rice University Houston, Texas Fall 2000 Copyright 2000, Robert Cartwright, all rights reserved. Students.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Language-Based Security: Overview of Types Deepak Garg Foundations of Security and Privacy October 27, 2009.
Type Checking and Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE-321 Programming Languages Simply Typed -Calculus
Types for Programs and Proofs
Imperative Programming with Dependent Types
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Organization of Programming Languages
Language-based Security
CSE341: Programming Languages Lecture 11 Type Inference
Program correctness Axiomatic semantics
Programming Languages and Compilers (CS 421)
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Presentation transcript:

1 CPS Transform for Dependent ML Hongwei Xi University of Cincinnati and Carsten Schürmann Yale University

2 Overview Motivation Program error detection at compile-time Compilation certification Dependent ML (DML) Programming Examples Theoretical Foundation CPS Transform for DML Conclusion

3 Program Error Detection Unfortunately one often pays a price for [languages which impose no disciplines of types] in the time taken to find rather inscrutable bugs — anyone who mistakenly applies CDR to an atom in LISP and finds himself absurdly adding a property list to an integer, will know the symptoms. -- Robin Milner A Theory of Type Polymorphism in Programming Therefore, a stronger type discipline allows for capturing more program errors at compile-time.

4 Some Advantages of Types Detecting program errors at compile-time Enabling compiler optimizations Facilitating program verification Using types to encode program properties Verifying the encoded properties through type- checking Serving as program documentation Unlike informal comments, types are formally verified and can thus be fully trusted

5 Compiler Correctness How can we prove the correctness of a (realistic) compiler? Verifying that the semantics of e is the same as the semantics of |e| for every program e But this simply seems too challenging (and is unlikely to be feasible) Source program e Target code | e | compilation |. ||. |

6 Compilation Certification Assume that  e  holds, i.e., e has the property  e.g., memory safety, termination, etc.  Then  e  should hold, too A compiler can be designed to produce a certificate to assert that  e  does have the property  Target code  e  :  e  holds Source program e:  e  holds compilation |. ||. |

7 Semantics-preserving Compilation e > |e|  D of e  v --> |D| of |e|  |v|  This seems unlikely to be feasible in practice

8 Type-preserving Compilation e > |e|   e:  > |e|:|  |   D of e:  ----> |D| of |e|:|  | D and |D| are both represented in LF The LF type-checker does all type-checking!

9 Limitations of (Simple) Types Not general enough Many correct programs cannot be typed For instance, downcasts are widely used in Java Not specific enough Many interesting program properties cannot be captured For instance, types in Java cannot guarantee safe array access

10 Narrowing the Gap NuPrl Coq Program Extraction Proof Synthesis ML Dependent ML

11 Some Design Decisions Practical type-checking Realistic programming features Conservative extension Pay-only-if-you-use policy

12 Ackermann Function in DML fun ack (m, n) = if m = 0 then n+1 else if n = 0 then ack (m-1, 1) else ack (m-1, ack (m, n-1)) withtype {a:nat,b:nat} int(a) * int(b) -> nat (* Note: nat = [a:int | a >=0] int(a) *)

13 Binary Search in DML fun bs (vec, key) = let fun loop (l, u) = if l > u then –1 else let val m = (l + u) / 2 val x = sub (vec, m) (* m needs to be within bounds *) in if x = key then m else if x < key then loop (m+1, u) else loop (l, m-1) end in loop (0, length (vec) – 1) end (* length: {n:nat} ‘a array(n) -> int(n) *) (* sub: {n:nat,i:nat | i ‘a *) withtype {i:int,j:int | 0 int withtype {n:nat} ‘a array(n) * ‘a -> int

14 ML 0: start point base types  ::= int | bool | (user defined datatypes) types  ::=  |       |      patterns p ::= x | c(p) | <> | match clauses ms ::= (p  e) | (p  e | ms) expressions e ::= x | f | c | if (e, e 1, e 2 ) | <> | | lam x: . e | fix f: . e | e 1 (e 2 ) | let x=e 1 in e 2 end | case e of ms values v ::= x | c | | lam x: . e context  ::=. | , x:  | , f: 

15 Integer Constraint Domain We use a for index variables index expressions i, j ::= a | c | i + j | i – j | i * j | i / j | … index propositions P, Q ::= i j | i >= j | i = j | i <> j | P  Q | P  Q index sorts  ::= int | {a :  | P } index variable contexts  ::=. | , a:  | , P index constraints  ::= P | P   |  a:  

16 Dependent Types dependent types  ::=... |  (i) |  a: .  |  a: .  For instance, int(0), bool array(16); nat = [a:int | a >= 0] int(a); {a:int | a >= 0} int list(a) -> int list(a)

17 DML 0  ML 0 + dependent types expressions e ::=... | a: .v | e[i] | | open e 1 as in e 2 end values v ::=... | a: .v | typing judgment  e 

18 Some Typing Rules  a  e   type-ilam   a  e  a   a  e  a  i   type-iapp   e  i  a  i 

19 Some Typing Rules (cont’d)  e  a  i  i   type-pack    a   e 1  a    a  x    e 2    type-open   open e 1 as in e 2 end:  

20 Some Typing Rules (cont’d)  e  bool(i)  i  e 1:  i  e 2:   type-if   if (e, e 1, e 2 ): 

21 Erasure: from DML 0 to ML 0 The erasure function erases all syntax related to type index | bool(1) | = |bool(0)| = bool | [a:int | a >= 0] int(a) | = int | {n:nat} ‘a list(n) -> ‘a list(n) | = ‘a list -> ‘a list | open e 1 as in e 2 end | = let x = |e 1 | in |e 2 | end

22 Relating DML 0 to ML 0 answer:type in DML 0 program:type in DML 0 |program|:|type| in ML 0 |answer|:|type| in ML 0 evaluation erasure Type preservation holds in DML 0 A program is already typable in ML 0 if it is typable in DML 0

23 Polymorphism Polymorphism is largely orthogonal to dependent types We have adopted a two phase type- checking algorithm

24 References and Exceptions A straightforward combination of effects with dependent types leads to unsoundness We have adopted a form of value restriction to restore the soundness

25 Quicksort in DML fun qs [] = [] | qs (x :: xs) = par (x, xs, [], []) withtype {n:nat} int list(n) -> int list(n) and par (x, [], l, g) = qs (x :: qs (g)) | par (x, y :: ys, l, g) = if y <= x then par (x, ys, y :: l, g) else par (x, ys, l, y :: g) withtype {p:nat,q:nat,r:nat} int * int list(p) * int list(q) * int list(r) -> int list(p+q+r+1)

26 CPS transformation for DML (I) Transformation on types: ||  (i) || * =  (i) ||  1 ->  2 || = ||  1  || * -> ||  2 || ||  a: .  || * =  a: . ||  || * ||  a: .  || * =  a: . ||  || * ||  || = ||  || * -> ans -> ans (* ans is some newly introduced type *)

27 CPS Transformation for DML (II) Transformation on expressions: ||c|| * = c ||x|| * = x || e x.e|| * = e x. ||e|| ||v|| = e k. k(||v|| * ) ||e 1 (e 2 )|| = e k.||e 1 ||( e x 1.||e 2 ||( e x 2. x 1 (x 2 )(k)) ||e[i]|| = e k.||e||( e x. x[i](k)) ||fix f.v|| = e k.(fix f. ||v||)(k)...

28 CPS Transformation for DML Theorem Assume D ::  |- e :  Then D can be transformed to ||D|| such that ||D|| ::  ||  || |- ||e|| : ||  ||  where  ||  ||(x) = ||  (x)|| * and ||  ||(f) = ||  (f)|| for all x,f in the domain of  This theorem can be readily encoded into LF We have done this in Twelf.

29 Contributions A CPS transform for DML The transform can be lifted to the level of typing derivation The notion of typing derivation compilation A novel approach to compilation certification in the presence of dependent types

30 End of the Talk Thank You! Questions?