nomos resource aware session types for programming
play

Nomos : Resource-Aware Session Types for Programming Digital - PowerPoint PPT Presentation

Programming language developed at Carnegie Mellon Nomos : Resource-Aware Session Types for Programming Digital Contracts Stephanie Balzer, Ankush Das, Jan Ho ff mann, and Frank Pfenning With some slides from Ankush. ETH 2019 Digital


  1. AARA: Use Potential Method • Assign potential functions to data structures Φ ( state ) ≥ 0 ➡ States are mapped to non-negative numbers • Potential pays the resource consumption and Φ ( before ) ≥ Φ ( after ) + cost the potential at the following program point telescoping Φ ( initial state ) ≥ P cost • Initial potential is an upper bound Clear soundness theorem. Type systems for automatic analysis Compositional. E ffi cient inference. • Fix a format of potential functions (basis like in linear algebra) • Type rules introduce linear constraint on coe ffi cients

  2. Example: Append for Persistent Lists Heap-space usage is 2n if append(x,y) ‣ n is the length of list x ‣ One list element requires two heap cells 
 (data and pointer)

  3. Example: Append for Persistent Lists Heap-space usage is 2n if append(x,y) ‣ n is the length of list x ‣ One list element requires two heap cells 
 (data and pointer) Example evaluation:

  4. Example: Append for Persistent Lists Heap-space usage is 2n if append(x,y) ‣ n is the length of list x ‣ One list element requires two heap cells 
 (data and pointer) Example evaluation: x y a d b e c

  5. Example: Append for Persistent Lists Heap-space usage is 2n if append(x,y) ‣ n is the length of list x ‣ One list element requires two heap cells 
 (data and pointer) Example evaluation: x y a d c b e c

  6. Example: Append for Persistent Lists Heap-space usage is 2n if append(x,y) ‣ n is the length of list x ‣ One list element requires two heap cells 
 (data and pointer) Example evaluation: x y a d c b b e c

  7. Example: Append for Persistent Lists Heap-space usage is 2n if append(x,y) ‣ n is the length of list x ‣ One list element requires two heap cells 
 (data and pointer) Example evaluation: x y a d c b a b e c

  8. Example: Append for Persistent Lists Heap-space usage is 2n if append(x,y) ‣ n is the length of list x ‣ One list element requires two heap cells 
 (data and pointer) Example evaluation: x y append(x,y) a d c b a b e c

  9. Example: Append for Persistent Lists Heap-space usage is 2n if append(x,y) ‣ n is the length of list x ‣ One list element requires two heap cells 
 (data and pointer) Example evaluation: x y append(x,y) a d c b a b e c Heap usage: 2*n = 2*3 = 6

  10. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y

  11. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 4 y x a d 2 4 e b 4 c z f Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  12. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 4 y x a d append(x,y) 2 4 e b 4 c z f Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  13. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 c 2 4 y x a d append(x,y) 2 4 e b c z f Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  14. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 2 c 2 4 y x a d b append(x,y) 2 e b c z f Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  15. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 2 c 2 a 2 y x a d b append(x,y) 2 e b c z f Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  16. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 2 c 2 a 2 y t x a d b 2 e b c z f Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  17. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 2 c 2 a 2 y t x a d b append(t,z) 2 e b c z f Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  18. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 2 c 2 a 2 y t x a d b append(t,z) e b c z f e Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  19. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 c 2 a 2 y t x a d b append(t,z) e b c z f e d Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  20. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y 2 a 2 y t x a d c b append(t,z) e b c z f e d c Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  21. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y a 2 y t x a d c b append(t,z) e b c z f e d c b Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  22. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y y t x a d c b a append(t,z) e b c z f e d c b a Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  23. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y y t x a d c b a e b c z append(t,z) f e d c b a Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  24. Example: Composing Calls of Append Heap usage of f(x,y,z) is 2n + 2(n+m) if f(x,y,z) = ‣ n is the length of list x let t = append(x,y) in append(t,z) ‣ m is the length of list y y t x a d c b a Implicit reasoning about size-changes. e b c z append(t,z) f e d c b a Initial potential: 4*n + 2*m = 4*3 + 2*2 = 16

  25. Example: Composing Calls of Append 4 2 2 0/0 f(x,y,z) = { append: (L (int),L (int)) ----> L (int) let t = append(x,y) in append(t,z) 2 0 0 0/0 } append: (L (int),L (int)) ----> L (int) The most general type of append is specialized at call-sites: q p r s/t Linear append: (L (int),L (int)) ----> L (int) | Φ constraints.

  26. Linear Potential Functions User-defined resource metrics 
 ✓ (i.e., by tick(q) in the code) Naturally compositional : tracks size ✓ changes, types are specifications Bound inference by reduction to ✓ efficient LP solving Type derivations prove bounds ✓ with respect to the cost semantics Polynomial Potential Functions

  27. Linear Potential Functions User-defined resource metrics 
 ✓ (i.e., by tick(q) in the code) Naturally compositional : tracks size ✓ changes, types are specifications Bound inference by reduction to ✓ efficient LP solving Type derivations prove bounds ✓ with respect to the cost semantics Strong soundness theorem. Polynomial Potential Functions

  28. Linear Potential Multivariate Polynomial Functions Potential Functions User-defined resource metrics 
 ✓ ✓ (i.e., by tick(q) in the code) Naturally compositional : tracks size ✓ ✓ changes, types are specifications Bound inference by reduction to ✓ ✓ efficient LP solving Type derivations prove bounds ✓ ✓ with respect to the cost semantics Strong soundness theorem. Polynomial Potential Functions

  29. For example m*n 2 . Linear Potential Multivariate Polynomial Functions Potential Functions User-defined resource metrics 
 ✓ ✓ (i.e., by tick(q) in the code) Naturally compositional : tracks size ✓ ✓ changes, types are specifications Bound inference by reduction to ✓ ✓ efficient LP solving Type derivations prove bounds ✓ ✓ with respect to the cost semantics Strong soundness theorem. Polynomial Potential Functions

  30. Implementations: RaML and Absynth Resource Aware ML (RaML) ‣ Based on Inria’s OCaml compiler ‣ Polymorphic and higher-order functions ‣ User-defined data types ‣ Side e ff ects (arrays and references) Absynth ‣ Based on control-flow graph IR ‣ Di ff erent front ends http://raml.co ‣ Bounds are integer expressions ‣ Supports probabilistic programs

  31. Actual Analysis 
 Computed Bound Constraints Behavior Runtime 11+22kn +13k2nv+13m +15n O(k2n+m) Sorting A-nodes (asort) 0.14 s 5656 3 -7.5nm +7.5nm2 +19.5m +16.5m2 O(nm2) Quick sort (lists of lists) 0.27 s 8712 43 + 30.5n + 8.5n2 Merge sort (list.ml) O(n log n) 0.11 s 3066 11 + 47n + 29n2 O(n2) Split and sort 0.69 s 3793 Longest common 23 + 10n + 52nm + 25m O(nm) 0.16 s 901 subsequence Matrix multiplication 3 + 2nm +18m + 22mxy +16my O(mxy) 1.11 s 3901 Evaluator for boolean 10+11n+16m+16mx+16my+20x+20y O(mx+my) 0.33 s 1864 expressions (tutorial) Dijkstra’s shortest-path 46 + 33n +111n2 O(n2) 0.11 s 2808 algorithm 8 + 43m2n + 59m + 63m2 O(nm2) Echelon form 1.81 s 8838 Binary multiplication 2+17kr+10ks+25k +8l+2+7r+8 O(kr+ks) 14.04 s 89,507 (CompCert) 13+66m+16mn +4m2 +59n +4n2 O(n2) Square root (CompCert) 18.25 s 135,529 Micro Benchmarks Evaluation-Step Bounds

  32. Evaluation-step bound vs. Quick Sort for Integers measured behavior

  33. Longest Common Evaluation-step bound vs. measured behavior Subsequence

  34. First automatically derived bound for LCS. Longest Common Evaluation-step bound vs. measured behavior Subsequence

  35. Automatic Amortized Resource Analysis (AARA) Type system for deriving symbolic resource bounds ‣ Compositional: Integrated with type systems or program logics ‣ Expressive: Bounds are multivariate resource polynomials ‣ Reliable: Formal soundness proof wrt. cost semantics ‣ Verifiable: Produces easily-checkable certificates ‣ Automatic: No user interaction required Applicable in practice ‣ Implemented: Resource Aware ML and Absynth ‣ Effective: Works for many typical programs ‣ Efficient: Inference via linear programming

  36. Automatic Amortized Resource Analysis (AARA) Type system for deriving symbolic resource bounds ‣ Compositional: Integrated with type systems or program logics ‣ Expressive: Bounds are multivariate resource polynomials ‣ Reliable: Formal soundness proof wrt. cost semantics ‣ Verifiable: Produces easily-checkable certificates ‣ Automatic: No user interaction required Applicable in practice ‣ Implemented: Resource Aware ML and Absynth ‣ Effective: Works for many typical programs Type checking in ‣ Efficient: Inference via linear programming linear time!

  37. 2. Shared (resource-aware) binary session types

  38. Binary Session Types • Implement message-passing concurrent programs • Communication via typed bidirectional channels • Curry-Howard correspondence with intuitionistic linear logic • Client and provider have dual types queue A = & { ins : A ( queue A , Example type: del : ⊕ { none : 1 , some : A ⊗ queue A }}

  39. Binary Session Types • Implement message-passing concurrent programs • Communication via typed bidirectional channels • Curry-Howard correspondence with intuitionistic linear logic • Client and provider have dual types External choice queue A = & { ins : A ( queue A , Example type: del : ⊕ { none : 1 , some : A ⊗ queue A }} Internal choice

  40. Binary Session Types • Implement message-passing concurrent programs • Communication via typed bidirectional channels • Curry-Howard correspondence with intuitionistic linear logic • Client and provider have dual types Receive msg of type A queue A = & { ins : A ( queue A , Example type: Send msg of type A del : ⊕ { none : 1 , some : A ⊗ queue A }}

  41. Binary Session Types • Implement message-passing concurrent programs • Communication via typed bidirectional channels • Curry-Howard correspondence with intuitionistic linear logic • Client and provider have dual types Receive msg of type A queue A = & { ins : A ( queue A , Example type: Send msg of type A del : ⊕ { none : 1 , some : A ⊗ queue A }} Type soundness (progress and preservation) implies deadlock freedom

  42. Example: Queue (element stored) x : A t : queue A s : queue A elem x elem z empty tail of queue head of queue queue A = & { ins : A ( queue A , del : ⊕ { none : 1 , some : A ⊗ queue A }}

  43. Example: Queue (element stored) x : A t : queue A s : queue A elem x elem z empty tail of queue head of queue queue A = & { ins : A ( queue A , del : ⊕ { none : 1 , some : A ⊗ queue A }} recv ‘ins’ and y

  44. Example: Queue (element stored) x : A t : queue A s : queue A elem x elem z empty tail of queue head of queue queue A = & { ins : A ( queue A , del : ⊕ { none : 1 , some : A ⊗ queue A }} recv ‘ins’ and y send ‘ins’ and y

  45. Example: Queue (element stored) x : A t : queue A s : queue A elem x elem z empty tail of queue head of queue queue A = & { ins : A ( queue A , del : ⊕ { none : 1 , some : A ⊗ queue A }} recv ‘ins’ and y send ‘ins’ and y recurse

  46. Example: Queue (element stored) x : A t : queue A s : queue A elem x elem z empty tail of queue head of queue queue A = & { ins : A ( queue A , del : ⊕ { none : 1 , some : A ⊗ queue A }} recv ‘ins’ and y send ‘ins’ and y recurse send ‘some’, x

  47. Example: Queue (element stored) x : A t : queue A s : queue A elem x elem z empty tail of queue head of queue queue A = & { ins : A ( queue A , del : ⊕ { none : 1 , some : A ⊗ queue A }} recv ‘ins’ and y send ‘ins’ and y recurse send ‘some’, x terminate

  48. Type checking in Example: Queue linear time! (element stored) x : A t : queue A s : queue A elem x elem z empty tail of queue head of queue queue A = & { ins : A ( queue A , del : ⊕ { none : 1 , some : A ⊗ queue A }} recv ‘ins’ and y send ‘ins’ and y recurse send ‘some’, x terminate

  49. Example: Auction

  50. Example: Auction sends status of auction

  51. Example: Auction sends status offers choice of auction of bidding

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