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 language design, Addendum to ONWARD '11 Proceedings)
Programming idioms Pure Visitor Partial Compiler implementations Language designs Universe 2006, Claude Heath
Programming idioms Pure Visitor Contextual Partial Compiler implementations Language designs Universe 2006, Claude Heath
Context: Physical environment 52.205337,0.121817 52.205349,0.121901 52.205340,0.121837
Context: Distributed computing λ x. ... λ x. ... λ x. ... λ x. ...
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
Context: Data structure traversal f f local operation global traversal
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}
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)
Classifying computations 1). Types expression : type x + 1 : ?
Classifying computations 1). Types scope ⊢ expression : type x : Int ⊢ x + 1 : Int y : Int ⊢ ( fn x . x + y ) : Int " Int
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 )
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)
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 )
“ what ” “ how ” expr coeffects types
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
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 ⟧
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 : ()
Coeffects : combining sub-term requirements • Shared context ⊢ e 2 : t 2 ⊢ e 1 : t 1 ! ! ⊢ (e 1, e 2 ) : (t 1, t 2 ) !
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 )
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 )
Coeffects : application ! ⊢ e 1 : s " t ! ⊢ e 2 : s ! ⊢ e 1 (e 2 ) : t
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
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
Coeffect : abstraction ! , x : t 1 ⊢ e : t 2 ! ⊢ ( fn x . e) : t 1 " t 2
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
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
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
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 : ()
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
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
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
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)
Ypnos • At boundaries? ? • Solution: exterior elements exterior provides boundary values traverse interior • Requires array with adequate exterior
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
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
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
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)
“ 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