hybrid contract checking via symbolic simplification
play

Hybrid contract checking via symbolic simplification Dana N. Xu - PowerPoint PPT Presentation

Hybrid contract checking via symbolic simplification Dana N. Xu INRIA Paris-Rocquencourt Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 1 / 30 From Types to Contracts (* val inc : int -> int *)


  1. Hybrid contract checking via symbolic simplification Dana N. Xu INRIA Paris-Rocquencourt Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 1 / 30

  2. From Types to Contracts (* val inc : int -> int *) contract inc = {x | x > 0} -> {y | y > x} let inc x = x + 1 Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 2 / 30

  3. From Types to Contracts (* val inc : int -> int *) contract inc = {x | x > 0} -> {y | y > x} let inc x = x + 1 let h1 = inc true (* type error *) Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 2 / 30

  4. From Types to Contracts (* val inc : int -> int *) contract inc = {x | x > 0} -> {y | y > x} let inc x = x + 1 let h1 = inc true (* type error *) let h2 = inc 0 (* contract error *) Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 2 / 30

  5. Example - append contract len = {xs | true} -> {n | n >= 0} let rec len (xs: int list) = match xs with | [] -> 0 | (h::t) -> 1 + len t contract append = {xs | true} -> {ys | true} -> {rs | len xs + len ys = len rs} let rec append (xs: int list) (ys: int list) = match xs with | [] -> ys | x::l -> x :: append l ys Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 3 / 30

  6. Example - filter let rec for_all (p : int -> bool) (xs : int list) = match xs with | [] -> true | a::l -> p a && for_all p l contract filter = {p | true} -> {xs | true} -> {zs | for_all p xs} let rec filter (p : int -> bool) (xs : int list) = match xs with | [] -> [] | (a::l) -> let res = filter p l in if p a then a::res else res Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 4 / 30

  7. Example - map, rev map contract map = {f | true} -> {xs | true} -> {ys | len xs = len ys} let rec map f (xs: int list) = match xs with | [] -> [] | (h::t) -> f h :: map f t contract rmap_f = {f | true} -> {xs | true} -> {ys | true} -> {zs | len zs = len xs + len ys} let rec rmap_f f (accu : int list) (ys: int list) = match ys with | [] -> accu | a::l -> rmap_f f (f a :: accu) l contract rev_map = {f | true} -> {xs | true} -> {ys | len xs = len ys} let rev_map f l = rmap_f f [] l Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 5 / 30

  8. Example - flatten let rec sum_len (xs: (int list) list) = match xs with | [] -> 0 | (a::l) -> len a + sum_len l contract flatten = {xs | true} -> {ys | len ys = sum_len xs} let rec flatten (xs : (int list) list) = match xs with | [] -> [] | l::r -> append l (flatten r) Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 6 / 30

  9. Example - rev contract rev_append = {xs | true} -> {ys | true} -> {zs | len xs + len ys = len zs} let rec rev_append (l1 : int list) (l2 : int list) = match l1 with | [] -> l2 | a :: l -> rev_append l (a :: l2) contract rev = {xs | true} -> {ys | len xs = len ys} let rev l = rev_append l [] Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 7 / 30

  10. Example - McCarthy’s 91 function contract mc91 = {n | true} -> {z | if n <=101 then z = 91 else z = n- 10} let rec mc91 x = if x > 100 then x - 10 else mc91 (mc91 (x + 11)) Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 8 / 30

  11. Error message reporting (* val f1 : (int -> int) -> int *) contract f1 = ({x | x >= 0} -> {y | y >= 0}) -> {z | z >= 0} let f1 g = (g 1) - 1 let f2 = f1 (fun x -> x - 1) f1 does not satisfy its postcondition Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 9 / 30

  12. The Language Exp Expressions a , e , p ∈ a , e , p ::= n integers | r blame | x | λ ( x τ ) . e | e 1 e 2 match e 0 with − → | alt pattern-matching K − → | e constructor Alternatives alt ::= K ( x τ 1 1 , . . . , x τ n n ) → e BAD l | UNR l Blames r ::= Label l ::= ( n 1 , n 2 , String ) n | x | r | K − → Values val | λ ( x τ ) . e val ::= n | x | K − → tv ::= tv Trivial values tv | λ ( x τ ) . e tval ::= Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 10 / 30

  13. Contracts Contracts t ∈ t ::= { x | p } predicate contract | x : t 1 → t 2 dependent function contract | ( x : t 1 , t 2 ) dependent tuple contract | Any polymorphic Anycontract E.g., k : ( { x | x > 0 } → { y | y > x } ) → { z | k 5 > z } E.g., ( { x | x > 0 } , { y | y > x } ) Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 11 / 30

  14. Contract Satisfaction For a well-typed expression e , define e ∈ t thus: e ∈ { x | p } ⇐ ⇒ e ↑ or ( e is crash-free and [A1] p [ e / x ] → ∗ true ) e ↑ or ( e → ∗ λ x . e 2 and e ∈ x : t 1 → t 2 ⇐ ⇒ [A2] ∀ val ∈ t 1 . ( e val ) ∈ t 2 [ val / x ]) e ↑ or ( e → ∗ ( val 1 , val 2 ) and e ∈ ( x : t 1 , t 2 ) ⇐ ⇒ [A3] val 1 ∈ t 1 and val 2 ∈ t 2 [ val 1 / x ]) e ∈ Any ⇐ ⇒ true [A4] Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 12 / 30

  15. Contract Wrappers BAD l1 UNR l2 e ⊲ t = e UNR l2 t e ⊳ t = e BAD l1 t ⊲ ⊳ ⊲ ⊳ r 1 [ P1 ] e r 2 { x | p } = let x = e in if p then x else r 1 ⊲ ⊳ r 1 [ P2 ] e r 2 x : t 1 → t 2 = let y = e in ⊲ ⊳ r 2 r 2 r 1 λ x 1 . (( y ( x 1 r 1 t 1 )) r 2 t 2 [( x 1 r 1 t 1 ) / x ]) ⊲ ⊳ ⊲ ⊳ ⊲ ⊳ r 1 [ P3 ] r 2 ( x : t 1 , t 2 ) = match e with e ⊲ ⊳ r 2 r 1 r 1 ( x 1 , x 2 ) → ( x 1 r 2 t 1 , x 2 r 2 t 2 [( x 1 r 1 t 1 ) / x ]) ⊲ ⊳ ⊲ ⊳ ⊲ ⊳ r 1 [ P4 ] e r 2 Any = r 2 ⊲ ⊳ Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 13 / 30

  16. Main Theorem in Theory Definition (Total contract) A contract t is total iff t is { x | p } and λ x . p is total (i.e. crash-free, terminating) or t is x : t 1 → t 2 and t 1 is total and for all val 1 ∈ t 1 , t 2 [ val 1 / x ] is total or t is ( x : t 1 , t 2 ) and t 1 is total and for all val 1 ∈ t 1 , t 2 [ val 1 / x ] is total or t is Any Theorem (Soundness and completeness of contract checking) For all closed expression e τ , closed and total contract t τ , ( e ⊲ t ) is crash-free ⇐ ⇒ e ∈ t Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 14 / 30

  17. Crash-free Expressions Definition (Crash-free Expression) A (possibly-open) expression e is crash-free iff : ]) bool ⇒ C [ ] �→ ∗ BAD ∀C . BAD / ∈ s C and ( C [ [ e ] [ e ] ( 2 , BAD ) No ( 2 , 3 ) Yes λ x . if x ∗ x ≥ 0 then x else BAD Yes Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 15 / 30

  18. Main Theorem in Practice Theorem (Soundness of contract checking) For all closed expression e τ , closed and terminating contract t τ , ( e ⊲ t ) is crash-free ⇒ e ∈ t Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 16 / 30

  19. SL machine: symbolic simplification with a logical store �H | | e | | S | | L� means “simplify e ” � �H | | e | | S | | L� � means “rebuild e ” H is an environment mapping variables to trivial values. e is the expression under simplification (or being rebuilt). S is a stack. S ::= [ ] | ( • e ) :: S | ( e • ) :: S | ( λ x . • ) :: S | ( let x = • in e ) :: S | ( match • with alt ) :: S | ( let x = e in • ) :: S − − − − − − − − − − − − → K − → | ( match e 0 with x → ( • , S , L )) :: S L is a logical store which contains the ctx-info. L ::= ∅ | ∀ x : τ, L | φ, L Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 17 / 30

  20. Example | λ x . if x > 0 then ( if x + 1 > 0 �∅ | | [ ] | | | ∅� then 5 else BAD ) else UNR | if x > 0 then ( if x + 1 > 0 �∅ | then 5 else BAD ) else UNR | | ( λ x . • ) :: [ ] | | ∀ x : int � � ( if • then ( if x + 1 > 0 � �∅ | | x > 0 | | then 5 else BAD ) | | ∀ x : int � � � else UNR ) :: ( λ x . • ) :: [ ] | if x + 1 > 0 | ( if x > 0 then • ) | ∀ x : int , [ �∅ | then 5 else BAD | | � ; � :: ( λ x . • ) :: [ ] x > 0 �∅ | | UNR | | ( if x > 0 else • ) :: S | | ∀ x : int , not ( x > 0 ) � ] Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 18 / 30

  21. Example (cont.) | ( if x > 0 then • ) | ∀ x : int , x > 0 , [ � �∅ | | 5 | | � � ; � :: ( λ x . • ) :: [ ] ( x + 1 > 0 ) | ( if x > 0 else • ) | ∀ x : int , � �∅ | | UNR | | not ( x > 0 ) � � ] :: ( λ x . • ) :: [ ] � �∅ | | if x > 0 then 5 else UNR | | ( λ x . • ) :: [ ] | | ∀ x : int � � � � �∅ | | λ x . if x > 0 then 5 else UNR | | [ ] | | ∀ x : int � � � λ x . if x > 0 then 5 else UNR � Dana N. Xu (INRIA Paris-Rocquencourt) Hybrid contract checking via symbolic simplification 19 / 30

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend