1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.

Slides:



Advertisements
Similar presentations
Technologies for finding errors in object-oriented software K. Rustan M. Leino Microsoft Research, Redmond, WA Lecture 1 Summer school on Formal Models.
Advertisements

Automated Theorem Proving Lecture 1. Program verification is undecidable! Given program P and specification S, does P satisfy S?
Static Contract Checking for Haskell Dana N. Xu University of Cambridge Ph.D. Supervisor: Simon Peyton Jones Microsoft Research Cambridge.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists A list is a finite sequence of elements. [3,5,9] ["a", "list" ] [] Elements may appear more than once [3,4]
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
ML Lists.1 Standard ML Lists. ML Lists.2 Lists  A list is a finite sequence of elements. [3,5,9] ["a", "list" ] []  ML lists are immutable.  Elements.
Extended Static Checking for Haskell (ESC/Haskell) Dana N. Xu University of Cambridge advised by Simon Peyton Jones Microsoft Research, Cambridge.
- Vasvi Kakkad.  Formal -  Tool for mathematical analysis of language  Method for precisely designing language  Well formed model for describing and.
TOWARDS A FINITE MODEL THEORY FOR HIGHER-ORDER PROGRAM VERIFICATION Dimitrios Vytiniotis, Koen Claessen, Simon Peyton Jones, Dan Rosén WG2.8 – Annapolis,
Patterns in ML functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are the.
0 PROGRAMMING IN HASKELL Chapter 7 - Higher-Order Functions.
0 PROGRAMMING IN HASKELL Chapter 4 - Defining Functions.
Data Abstraction COS 441 Princeton University Fall 2004.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions Most of this should be review for you.
0 PROGRAMMING IN HASKELL Chapter 6 - Recursive Functions.
Sparkle A theorem prover for the functional language Clean Maarten de Mol University of Nijmegen February 2002.
Type Inference: CIS Seminar, 11/3/2009 Type inference: Inside the Type Checker. A presentation by: Daniel Tuck.
Simon Peyton Jones Microsoft Research Joint work with Dana Xu, University of Cambridge Dec 2007.
Cse536 Functional Programming 1 7/14/2015 Lecture #2, Sept 29, 2004 Reading Assignments –Begin Chapter 2 of the Text Home work #1 can be found on the webpage,
Comp 205: Comparative Programming Languages Functional Programming Languages: More Lists Recursive definitions List comprehensions Lecture notes, exercises,
Cormac Flanagan University of California, Santa Cruz Hybrid Type Checking.
1 Static Contract Checking for Haskell Dana N. Xu INRIA France Work done at University of Cambridge Joint work with Simon Peyton Jones Microsoft Research.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
0 PROGRAMMING IN HASKELL Chapter 7 - Defining Functions, List Comprehensions.
Type Systems CS Definitions Program analysis Discovering facts about programs. Dynamic analysis Program analysis by using program executions.
Sound Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology.
1-Nov-15 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
0 PROGRAMMING IN HASKELL Chapter 9 - Higher-Order Functions, Functional Parsers.
Modeling with Haskell Scientific Seminar 03/04 Gerhard Navratil.
1 Functional Programming Lecture 6 - Algebraic Data Types.
CS5205Haskell1 CS5205: Foundations in Programming Languages FP with Haskell A pure lazy functional language that embodies many innovative ideas in language.
Semantics In Text: Chapter 3.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
CS5205Haskell1 CS5205: Foundations in Programming Languages Overview Lecturer : Chin Wei Ngan Office : S “Language.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: Higher-order Functions Dr. Hyunyoung Lee.
1 CS 457/557: Functional Languages Lists and Algebraic Datatypes Mark P Jones Portland State University.
Advanced Formal Methods Lecture 3: Simply Typed Lambda calculus Mads Dam KTH/CSC Course 2D1453, Some material from B. Pierce: TAPL + some from.
1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Haskell: More on Functions and List Comprehensions Dr. Hyunyoung Lee.
Advanced Functional Programming Tim Sheard 1 Lecture 17 Advanced Functional Programming Tim Sheard Oregon Graduate Institute of Science & Technology Lecture:
Types and Programming Languages Lecture 14 Simon Gay Department of Computing Science University of Glasgow 2006/07.
0 PROGRAMMING IN HASKELL Based on lecture notes by Graham Hutton The book “Learn You a Haskell for Great Good” (and a few other sources) Odds and Ends,
1 Testing in Haskell: using HUnit Notes, thanks to Mark P Jones Portland State University.
Haskell Chapter 4. Recursion  Like other languages  Base case  Recursive call  Author programs a number of built-in functions as examples.
Haskell Chapter 5, Part II. Topics  Review/More Higher Order Functions  Lambda functions  Folds.
An introduction to functional programming using Haskell CENG242 –Recitation 1.
Lecture 16: Advanced Topic: Functional Programming CS5363 Compiler and Programming Languages.
6-Jul-16 Haskell II Functions and patterns. Data Types Int + - * / ^ even odd Float + - * / ^ sin cos pi truncate Char ord chr isSpace isUpper … Bool.
CS314 – Section 5 Recitation 9
Functional Programming
Functional Programming
Types CSCE 314 Spring 2016.
Functional Programming Lecture 12 - more higher order functions
A lightening tour in 45 minutes
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Extended Static Checking for Haskell (ESC/Haskell)
PROGRAMMING IN HASKELL
CSCE 314: Programming Languages Dr. Dylan Shell
Haskell Types, Classes, and Functions, Currying, and Polymorphism
PROGRAMMING IN HASKELL
CSE 3302 Programming Languages
CSCE 314: Programming Languages Dr. Dylan Shell
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
Static Contract Checking for Haskell
Presentation transcript:

1 Static Contract Checking for Haskell Dana N. Xu University of Cambridge Joint work with Simon Peyton Jones Microsoft Research Cambridge Koen Claessen Chalmers University of Technology

2 Module UserPgm where f :: [Int]->Int f xs = head xs `max` 0 : … f [] … Program Errors Give Headache! Glasgow Haskell Compiler (GHC) gives at run-time Exception: Prelude.head: empty list Module Prelude where head :: [a] -> a head (x:xs) = x head [] = error “empty list”

3 Types >> > > > > Contracts >> > > > > head (x:xs) = x head :: [Int] -> Int …(head 1)… head 2 {xs | not (null xs)} -> {r | True} …(head [])… Bug! Contract (original Haskell boolean expression) Type not :: Bool -> Bool not True = False not False = True null :: [a] -> Bool null [] = True null (x:xs) = False

4 Contract Checking head 2 {xs | not (null xs)} -> {r | True} head (x:xs’) = x f xs = head xs `max` 0 Warning: f [] calls head which may fail head’s precondition! f_ok xs = if null xs then 0 else head xs `max` 0 No more warnings from compiler!

5 Satisfying a predicate contract e 2 {x | p} if (1) p[e/x] gives True and (2) e is crash-free. Arbitrary boolean-valued Haskell expression Recursive function, higher-order function, partial function can be called!

6 Expressiveness of the Specification Language data T = T1 Bool | T2 Int | T3 T T sumT :: T -> Int sumT 2 {x | noT1 x} -> {r | True} sumT (T2 a) = a sumT (T3 t1 t2) = sumT t1 + sumT t2 noT1 :: T -> Bool noT1 (T1 _) = False noT1 (T2 _) = True noT1 (T3 t1 t2) = noT1 t1 && noT1 t2

7 Expressiveness of the Specification Language sumT :: T -> Int sumT 2 {x | noT1 x} -> {r | True} sumT (T2 a) = a sumT (T3 t1 t2) = sumT t1 + sumT t2 rmT1 :: T -> T rmT1 2 {x | True} -> {r | noT1 r} rmT1 (T1 a) = if a then T2 1 else T2 0 rmT1 (T2 a) = T2 a rmT1 (T3 t1 t2) = T3 (rmT1 t1) (rmT1 t2) For all crash-free t::T, sumT (rmT1 t) will not crash.

8 Higher Order Functions all :: (a -> Bool) -> [a] -> Bool all f [] = True all f (x:xs) = f x && all f xs filter :: (a -> Bool) -> [a] -> [a] filter 2 {f | True} -> {xs | True} -> {r | all f r} filter f [] = [] filter f (x:xs’) = case (f x) of True -> x : filter f xs’ False -> filter f xs’

9 Contracts for higher-order function’s parameter f1 :: (Int -> Int) -> Int f1 2 ({x | True} -> {y | y >= 0}) -> {r | r >= 0} f1 g = (g 1) - 1 f2:: {r | True} f2 = f1 (\x -> x – 1) Error: f1’s postcondition fails because (g 1) >= 0 does not imply (g 1) – 1 >= 0 Error: f2 calls f1 which fails f1’s precondition [Findler&Felleisen:ICFP’02, Blume&McAllester:ICFP’04]

10 Functions without Contracts data T = T1 Bool | T2 Int | T3 T T noT1 :: T -> Bool noT1 (T1 _) = False noT1 (T2 _) = True noT1 (T3 t1 t2) = noT1 t1 && noT1 t2 (&&) True x = x (&&) False x = False No abstraction is more compact than the function definition itself!

11 Contract Synonym contract Ok = {x | True} contract NonNull = {x | not (null x)} head :: [Int] -> Int head 2 NonNull -> Ok head (x:xs) = x {-# contract Ok = {x | True} -#} {-# contract NonNull = {x | not (null x)} #-} {-# contract head :: NonNull -> Ok #-} Actual Syntax

12 Questions on e 2 t bot = bot  x. x 2 {x | bot} ! {r | True}?  x. error “f” 2 {x | bot} ! {r | True} ?  x. x 2 {x | True} ! {r | bot} ?  x. head [] 2 {x | True} ! {r | bot} ? (True, 2) 2 {x | (snd x) > 0} ? (head [], 3) 2 {x | (snd x) > 0} ? error “f” 2 ? ? 2 {x | False} ? 2 {x | head []}

13 Language Syntax following Haskell’s lazy semantics

14 Two special constructors  BAD is an expression that crashes. error :: String -> a error s = BAD head (x:xs) = x head [] = BAD  UNR (short for “unreachable”) is an expression that gets stuck. This is not a crash, although execution comes to a halt without delivering a result. (identifiable infinite loop)

15 Syntax of Contracts (related to [Findler:ICFP02,Blume:ICFP04,Hinze:FLOPS06,Flanagan:POPL06]) Full version: x’:{x | x >0} -> {r | r > x’} Short hand: {x | x > 0} -> {r | r > x} k:({x | x > 0} -> {y | y > 0}) -> {r | r > k 5} t 2 Contract t ::= {x | p} Predicate Contract | x:t 1 ! t 2 Dependent Function Contract | (t 1, t 2 ) Tuple Contract | Any Polymorphic Any Contract

16 Contract Satisfaction (related to [Findler:ICFP02,Blume:ICFP04,Hinze:FLOPS06]) e " means e diverges or e ! * UNR e 2 {x | p}, e " or (e is crash-free and p[e/x] ! * {BAD, False} [A1] e 2 x:t 1 ! t 2, e " or 8 e 1 2 t 1. (e e 1 ) 2 t 2 [e 1 /x] [A2] e 2 (t 1, t 2 ), e " or (e 1 2 t 1 and e 2 2 t 2 ) [A3] e 2 Any, True [A4] Given ` e ::  and ` c t :: , we define e 2 t as follows:

17 Answers on e 2 t  x. x 2 {x | bot} ! {r | True}  x. BAD 2 {x | bot} ! {r | True}  x. x 2 {x | True} ! {r | bot}  x. BAD 2 {x | True} ! {r | bot}  x. BAD 2 {x | True} ! Any (True, 2) 2 {x | (snd x) > 0} (BAD, 3) 2 {x | (snd x) > 0} BAD 2 Any UNR, bot 2 {x | False} UNR, bot 2 {x | BAD} BAD 2 (Any, Any)BAD 2 Any ! Any

18 Lattice for Contracts {x | True} Any {x | False} ({x | x>0}, Any) ({x | x>0}, {r | r>0}) {x | x>0}->{r | r>x} Any -> {r | r>0}

19 Laziness fst 2 ({x | True}, Any) -> {r | True} fst (a,b) = a …fst (5, error “f”)… fstN :: (Int, Int) -> Int fstN 2 ({x | True}, Any) -> {r | True} fstN (a, b) n = if n > 0 then fstN (a+1, b) (n-1) else a g2 = fstN (5, error “fstN”) 100 Every expression satisfies Any

20 What to Check? Does function f satisfies its contract t (written f 2 t)? At the definition of each function f, Check f 2 t assuming all functions called in f satisfy their contracts. Goal: main 2 {x | True}

21 How to Check? Define e 2 t Construct e B t (e “ensures” t) Grand Theorem e 2 t, e B t is crash-free (related to Blume&McAllester:ICFP’04) Normal form e’ Simplify (e B t) If e’ is syntactically safe, then Done! This Talk ESC/Haskell (Xu:HW’06)

22 What we can’t do? g1, g2 2 Ok -> Ok g1 x = case (prime x > square x) of True -> x False -> error “urk” g2 xs ys = case (rev (xs ++ ys) == rev ys ++ rev xs) of True -> xs False -> error “urk” Hence, three possible outcomes: (1) Definitely Safe (no crash, but may loop) (2) Definite Bug (definitely crashes) (3) Possible Bug Crash!

23 Crashing Definition (Crash). A closed term e crashes iff e ! * BAD Definition (Crash-free Expression) An expression e is crash-free iff 8 C. BAD 2 C, ` C[[e]] :: (), C[[e]] ! * BAD

24 Definition of B and C ( B pronounced ensures C pronounced requires) e B {x | p} = case p[e/x] of True -> e False -> BAD e C {x | p} = case p[e/x] of True -> e False -> UNR Example: 5 2 {x | x > 0} 5 B {x | x > 0} = case (5 > 0) of True -> 5 False -> BAD related to [Findler:ICFP02,Blume:ICFP04,Hinze:FLOPS06]

25 e B x:t 1 ! t 2 =  v. (e (v C t 1 )) B t 2 [v C t 1 /x] e C x:t 1 ! t 2 =  v. (e (v B t 1 )) C t 2 [v B t 1 /x] e B (t 1, t 2 ) = case e of (e 1, e 2 ) -> (e 1 B t 1, e 2 B t 2 ) e C (t 1, t 2 ) = case e of (e 1, e 2 ) -> (e 1 C t 1, e 2 C t 2 ) Definition of B and C (cont.)

26 Higher-Order Function f1 B ({x | True} -> {y | y >= 0}) -> {r | r >= 0} = … B C B = v 1. case (v 1 1) >= 0 of True -> case (v 1 1) - 1 >= 0 of True -> (v 1 1) -1 False -> BAD False -> UNR f1 :: (Int -> Int) -> Int f1 2 ({x | True} -> {y | y >= 0}) -> {r | r >= 0} f1 g = (g 1) - 1 f2:: {r | True} f2 = f1 (\x -> x – 1)

27 Modified Definition of B and C e B {x | p} = case p[e/x] of True -> e False -> BAD e B {x | p} = e `seq` case fin (p[e/x]) of True -> e False -> BAD old new e_1 `seq` e_2 = case e_1 of {DEFAULT -> e_2} if e ! * BAD, then (fin e) ! False else (fin e) ! e

28 Why need seq and fin ? (UNR B {x | False}) ! * BAD But UNR 2 {x | False}  x. x 2 {x | BAD} ! {x | True} But x. x B {x | BAD} ! {x | True} ! * v. (v `seq` BAD) which is NOT crash-free. e B {x | p} = e `seq` case fin (p[e/x]) of True -> e False -> BAD Without seq Without fin

29 e B Any = UNR e C Any = BAD f5 2 Any -> {r | True} f5 x = 5 error :: String -> a -- HM Type error 2 {x | True} -> Any -- Contract Definition of B and C for Any

30 Properties of B and C Key Lemma: For all closed, crash-free e, and closed t, (e C t) 2 t Projections: (related to Findler&Blume:FLOPS’06) For all e and t, if e 2 t, then (a) e ¹ e B t (b) e C t ¹ e Definition (Crashes-More-Often): e 1 ¹ e 2 iff for all C, ` C[[e i ]] :: () for i=1,2 and C[[e 2 ]] ! * BAD ) C[[e 1 ]] ! * BAD

31 About 30 Lemmas Lemma [Monotonicity of Satisfaction ]: If e 1 2 t and e 1 ¹ e 2, then e 2 2 t Lemma [Congruence of ¹ ]: e 1 ¹ e 2 ) 8 C. C[[e 1 ]] ¹ C[[e 2 ]] Lemma [Idempotence of Projection]: 8 e, t. e B t B t ≡ e B t 8 e, t. e C t C t ≡ e C t Lemma [A Projection Pair]: 8 e, t. e B t C t ¹ e Lemma [A Closure Pair]: 8 e, t. e ¹ e C t B t

32 Lattice for Expressions ( ¹ ) UNR BAD (3,BAD) 5  x. x (3, 4)  x. 6

33 Lattice for Contracts {x | True} Any {x | False} ({x | x>0}, Any) ({x | x>0}, {r | r>0}) {x | x>0}->{r | r>x} Any -> {r | r>0}

34 Conclusion Contract Haskell Program Glasgow Haskell Compiler (GHC) Where the bug is Why it is a bug

35 Contributions  Automatic static contract checking instead of dynamic contract checking.  Compared with ESC/Haskell Allow pre/post specification for higher-order function’s parameter through contracts. Reduce more false alarms caused by Laziness and in an efficient way. Allow user-defined data constructors to be used to define contracts Specifications are more type-like, so we can define contract synonym.  We develop a concise notation ( B and C ) for contract checking, which enjoys many properties. We give a new and relatively simpler proof of the soundness and completeness of dynamic contract checking, this proof is much trickier than it looks.  We implement the idea in GHC Accept full Haskell Support separate compilation as the verification is modular. Can check functions without contract through CEG unrolling.

36 Sorting sorted [] = True sorted (x:[]) = True sorted (x:y:xs) = x <= y && sorted (y : xs) insert :: {i | True} -> {xs | sorted xs} -> {r | sorted r} merge :: [Int] -> [Int] -> [Int] merge :: {xs | sorted xs} -> {ys | sorted ys} -> {r | sorted r} bubbleHelper :: [Int] -> ([Int], Bool) bubbleHelper :: {xs | True} -> {r | not (snd r) ==> sorted (fst r)} Insertsort, mergesort, bubblesort :: {xs | True} -> {r | sorted r} (==>) True x = x (==>) False x = True

37 Various Examples zip :: [a] -> [b] -> [(a,b)] zip :: {xs | True} -> {ys | sameLen xs ys} -> {rs | sameLen rs xs } sameLen [] [] = True sameLen (x:xs) (y:ys) = sameLen xs ys sameLen _ _ = False f91 :: Int -> Int f91 :: { n {r | r == 91 } f91 n = case (n <= 100) of True -> f91 (f91 (n + 11)) False -> n – 10

38 f :: {x | x > 0} -> {r | True} f x = x f B {x | x > 0} -> {r | True} =( x.x) B {x | x > 0} -> {r | True} = v. ( x.x (v C {x | x > 0})) B {r | True} = v. (case v > 0 of True -> v False -> UNR) g = … f… f C {x | x > 0} -> {r | True} = v. (case v > 0 of True -> v False -> BAD)

39 Constructor Contracts {x | x > 0} : {xs | all (< 0) xs} {x | x > 0} : Any Any : Any Support any user-defined data constructors! (related to [Hinze&Jeuring&Löh:FLOPS’06])

40 How to Check e 2 t? Given e and t  We construct a term (e B t) (pronounced e “ensures” t) related to Findler&Felleisen:ICFP’02 (dynamic contract checking algorithm)  Prove (e B t) is crash-free. simplify the term (e B t) to e’ and e’ is syntactically safe, then we are done. Theorem 1 (related to Blume&McAllester:ICFP’04) e B t is crash-free, e 2 t Theorem 2 simpl (e B t) is syntactically safe ) e B t is crash-free This Talk ESC/Haskell (HW’06)

41