programs in context
play

Programs in context Monday 12 th November 2012 Dominic Orchard, - PowerPoint PPT Presentation

Programs in context Monday 12 th November 2012 Dominic Orchard, Cambridge Programming Research Group, University of Cambridge The Four Rs of effective PLs Riting Reading Running Reasoning (Orchard D, The Four Rs of programming


  1. Programs in context Monday 12 th November 2012 Dominic Orchard, Cambridge Programming Research Group, University of Cambridge

  2. The Four Rs of effective PLs • ‘Riting • Reading • Running • Reasoning (Orchard D, The Four Rs of programming language design, Addendum to ONWARD '11 Proceedings)

  3. Programming idioms Pure Visitor Partial Compiler implementations Language designs Universe 2006, Claude Heath

  4. Programming idioms Pure Visitor Contextual Partial Compiler implementations Language designs Universe 2006, Claude Heath

  5. Context: Physical environment 52.205337,0.121817 52.205349,0.121901 52.205340,0.121837

  6. Context: Distributed computing λ x. ... λ x. ... λ x. ... λ x. ...

  7. Context: Data structure traversal for i = 0 to n b b b a a a for j = 0 to m b b b a a a B[i][j] = f(A, i, j) b b b a a a global traversal f(A, i, j) = (A(i-1,j) + A(i+1,j) + A(i,j-1) + A(i, j+1) + A(i, j)) / 5 local operation, at some context

  8. Context: Data structure traversal f f local operation global traversal

  9. Context: environment ! = for i = 0 to n {n,m,f,A,B,i} for j = 0 to m {n,m,f,A,B,i,j} B[i][j] = 0 {n,m,f,A,B,i,j} for x = 1 to (n-1) {n,m,f,A,B,x} for y = 1 to (m-1) {n,m,f,A,B,x,y} B[x][y] = f(A, x, y) {n,m,f,A,B,x,y}

  10. Context: “program counter” for i = 0 to n i = <0, ..., n, for j = 0 to m 1, ..., n-1> B[i][j] = 0 j = <0, ..., m, for i = 1 to (n-1) 1, ..., m-1> for j = 1 to (m-1) B[i][j] = f(A, i, j)

  11. Classifying computations 1). Types expression : type x + 1 : ?

  12. Classifying computations 1). Types scope ⊢ expression : type x : Int ⊢ x + 1 : Int y : Int ⊢ ( fn x . x + y ) : Int " Int

  13. Classifying computations 1I). Common semantic structure encodes context t ⟦ ⟧ : v 0 :t 0 , ..., v n :t n ⊢ e : t C (t 0, ..., t n ) • Mathematical structure on C (monoidal comonads) • Parameterise semantics by a particular C + operations (Uustalu, Vene - 2008 )

  14. Classifying computations 1I). Common semantic structure • Coeffects - analysis of context-dependence scope ? coeffect ⊢ expression : type ∅ ? {gps} ⊢ access gps : Coord {gps} ∅ ? {} ⊢ ( fn x . access gps + x ) : Int " Coord latent coeffect • Parameterise analysis by a coeffect algebra ( C , ⊔ , ⊕ , ⊓ ,e) (Petricek, Orchard, Mycroft - 2012)

  15. Classifying computations 1I). Common semantic structure • Coeffects - analysis of context-dependence encodes context that satisfies R : ⟦ ⟧ v 0 :t 0 , ..., v n :t n ? R ⊢ e : t t C R (t 0, ..., t n ) • Further mathematical structure (not today!) (Petricek, Orchard, Mycroft - 2012 )

  16. “ what ” “ how ” expr coeffects types

  17. Reminder: inductive inference rules ! 1 ? c 1 ⊢ e 1 : t 1 ! 2 ? c 2 ⊢ e 2 : t 2 premise # ( ! 1, ! 2 ) ? c 1 ⊛ c 2 ⊢ E(e 1 , e 2 ) : T(t 1, t 2 ) conclusion syntax tree E e 1 e 2

  18. Example 1 • Simple functional language • Context = environment • Coeffect = liveness ( F / T ) ! ? T ⊢ x : t ! ? F ⊢ 3 : Int dead context live context • Informs “dead code” elimination ⟦ ( fn x . 3)(2) ⟧ = ⟦ 3 ⟧

  19. Example 1I • Simple functional language + distributed resources • e.g. send client ( fn name . insert ( access db) “location” name ( access gps)) • Coeffect = sets of required resources ! ? {r} ⊢ access r : t f 2 ! ? f 1 ⊢ e : t 1 " t 2 ( device has f 2 resources) ! ? f 1 ⊢ send device e : ()

  20. Coeffects : combining sub-term requirements • Shared context ⊢ e 2 : t 2 ⊢ e 1 : t 1 ! ! ⊢ (e 1, e 2 ) : (t 1, t 2 ) !

  21. Coeffects : combining sub-term requirements • Shared context ⊢ e 2 : t 2 ⊢ e 1 : t 1 ! ? x ? y ! ⊢ (e 1, e 2 ) : (t 1, t 2 ) ! ? x ⊔ y • e.g. ⊔ = ⋃ resources ! ? {r} ⊢ e 1 : t 1 ! ? {r,s} ⊢ e 2 : t 2 ! ? {r,s} ⊢ (e 1, e 2 ) : (t 1, t 2 )

  22. Coeffects : combining sub-term requirements • Shared context ⊢ e 2 : t 2 ⊢ e 1 : t 1 ! ? x ? y ! ⊢ (e 1, e 2 ) : (t 1, t 2 ) ! ? x ⊔ y • e.g. • e.g. ⊔ = ⋃ ⊔ = ⋁ liveness resources ! ? {r} ⊢ e 1 : t 1 ! ? F ⊢ e 1 : t 1 ! ? {r,s} ⊢ e 2 : t 2 ! ? T ⊢ e 2 : t 2 ! ? {r,s} ⊢ (e 1, e 2 ) : (t 1, t 2 ) ! ? T ⊢ (e 1, e 2 ) : (t 1, t 2 )

  23. Coeffects : application ! ⊢ e 1 : s " t ! ⊢ e 2 : s ! ⊢ e 1 (e 2 ) : t

  24. Coeffects : application z ? y ! ? x ⊢ e 1 : s " t ! ⊢ e 2 : s ? x ⊔ (y ⊕ z) ⊢ e 1 (e 2 ) : t ! • e.g. resources ⊔ = ⋃ , ⊕ = ⋃ (i.e. x ⋃ y ⋃ z) {r,u} ! ? {r} ⊢ e 1 : t 1 " t 2 ! ? {s,t} ⊢ e 2 : t 1 ! ? {r,s,t,u} ⊢ e 1 (e 2 ) : t 2

  25. Coeffects : application z ! ? y ? x ⊢ e 1 : s " t ! ⊢ e 2 : s ! ? x ⊔ (y ⊕ z) ⊢ e 1 (e 2 ) : t • e.g. liveness ⊔ = ⋁ , ⊕ = ⋀ (i.e. x ⋁ (y ⋀ z)) F ! ? F ⊢ e 1 : t 1 " t 2 ! ? T ⊢ e 2 : t 1 ⊢ e 1 (e 2 ) : t 2 ! ? F

  26. Coeffect : abstraction ! , x : t 1 ⊢ e : t 2 ! ⊢ ( fn x . e) : t 1 " t 2

  27. Coeffect : abstraction ! , x : t 1 ⊢ e : t 2 ? r r = x ⨅ y y ? x ! ⊢ ( fn x . e) : t 1 " t 2 • e.g. resources ⨅ = ⋃ ! , x : t 1 ? {r,s} ⊢ e : t 2 (possibility 1) {s} ! ? {r} ⊢ ( fn x . e) : t 1 " t 2

  28. Coeffect : abstraction ! , x : t 1 ⊢ e : t 2 ? r r = x ⨅ y y ? x ! ⊢ ( fn x . e) : t 1 " t 2 • e.g. resources ⨅ = ⋃ ! , x : t 1 ? {r,s} ⊢ e : t 2 (possibility 2) {r} ! ? {s} ⊢ ( fn x . e) : t 1 " t 2

  29. Coeffect : abstraction ! , x : t 1 ⊢ e : t 2 ? r r = x ⨅ y y ? x ! ⊢ ( fn x . e) : t 1 " t 2 • e.g. resources ⨅ = ⋃ and so on... ! , x : t 1 ? {r,s} ⊢ e : t 2 (possibility 3) {} ! ? {r,s} ⊢ ( fn x . e) : t 1 " t 2

  30. Coeffect : abstraction ! , x : t 1 ⊢ e : t 2 ? r r = x ⨅ y y ? x ! ⊢ ( fn x . e) : t 1 " t 2 • e.g. resources ⨅ = ⋃ • “sending” a function reduces the possibilities f 2 ! ? f 1 ⊢ e : t 1 " t 2 ( device has f 2 resources) ! ? f 1 ⊢ send device e : ()

  31. Coeffect : abstraction ! , x : t 1 ⊢ e : t 2 ? r r = x ⨅ y y ? x ! ⊢ ( fn x . e) : t 1 " t 2 • e.g. ⨅ = ⋀ liveness ! , x : t 1 ? T ⊢ e : t 2 T ! ? T ⊢ ( fn x . e) : t 1 " t 2

  32. Coeffect : abstraction ! , x : t 1 ⊢ e : t 2 ? r r = x ⨅ y y ? x ! ⊢ ( fn x . e) : t 1 " t 2 • e.g. ⨅ = ⋀ liveness ! , x : t 1 ? F ⊢ e : t 2 (possibility 1) etc. T ! ? F ⊢ ( fn x . e) : t 1 " t 2

  33. Coeffects For a coeffect algebra ( C , ⊔ , ⊕ , ⊓ ,e ) z ! , x : t 1 ? x ⨅ y ⊢ e : t 2 ! ? x ⊢ e 1 : s " t ! ? y ⊢ e 2 : s y ! ⊢ ( fn x . e) : t 1 " t 2 ! ? x ⊔ (y ⊕ z) ⊢ e 1 (e 2 ) : t ? x x : t 1 ∈ ! ! ? x ⊢ e 1 : t 1 ! ? y ⊢ e 2 : t 2 ! ? e ⊢ x : t 1 ! ? x ⊔ y ⊢ (e 1, e 2 ) : (t 1, t 2 ) • Captures lots of different kinds of contextual computation

  34. In practice: Ypnos • Ypnos: language for scientific computing • Based on sub-language of “stencils” • Parameterisable by different data structures • Coeffect analysis tracks data access • Various safety and optimisation guarantees (Orchard, Mycroft - 2010)

  35. Ypnos • At boundaries? ? • Solution: exterior elements exterior provides boundary values traverse interior • Requires array with adequate exterior

  36. Ypnos • Recall f(A, i, j) = (A(i-1,j) + A(i+1,j) + A(i,j-1) + A(i, j+1) + A(i, j)) / 5 local operation, at some context • Relative data access as a coeffect, e.g.: A : Float ? {(0,0),(-1,0),(1,0),(0,1),(0,-1)} ⊢ ( A[0][0] + A[-1][0] + A[1][0] + A[0][1] + A[0][-1])/5.0 : Float

  37. Ypnos • Two-level language • Haskell (outer) ! ⊢ e : t • Ypnos stencils (inner) ! ; ! ’ ? r ⊢ e : t • Interface between the two: data access ! ; x : t 1 ? r ⊢ e : t 2 ! ⊢ ( stencil x . e) : Array r t 1 " t 2 exterior size

  38. Ypnos - Grid Patterns • Array access only by pattern matching • e.g. stencil A . ( A[0][0] + A[-1][0] + A[1][0] + A[0][1] + A[0][-1]) /5.0 stencil | _ t _ | | l @c r | | _ b _ | . ( t + l + c + r + b)/5.0 • Patterns are static => decidable coeffects

  39. Conclusions • Goals to improve 4 Rs • Focus on contextual computations • Coeffects: general class of contextual computation May appear in your work • Provide safety + optimisations (e.g. Ypnos)

  40. “ Thanks ” http://dorchard.co.uk More details in: “Coeffects: The Essence of Context-Dependence”, Petricek, Orchard, Mycroft 2012 “Programming in context” (thesis), Orchard, Coming Soon

Recommend


More recommend