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

programs in context
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

Programs in context

Dominic Orchard,

Cambridge Programming Research Group, University of Cambridge

Monday 12th November 2012

slide-2
SLIDE 2
slide-3
SLIDE 3

The Four Rs of effective PLs

  • ‘Riting
  • Reading
  • Running
  • Reasoning

(Orchard D, The Four Rs of programming language design, Addendum to ONWARD '11 Proceedings)

slide-4
SLIDE 4

Universe 2006, Claude Heath

Pure Visitor Partial Programming idioms Language designs Compiler implementations

slide-5
SLIDE 5

Universe 2006, Claude Heath

Pure Visitor Partial Programming idioms Language designs Compiler implementations Contextual

slide-6
SLIDE 6

Context: Physical environment

52.205337,0.121817 52.205340,0.121837 52.205349,0.121901

slide-7
SLIDE 7

Context: Distributed computing

λx. ... λx. ... λx. ... λx. ...

slide-8
SLIDE 8

Context: Data structure traversal

for i = 0 to n for j = 0 to m B[i][j] = f(A, i, j)

a a a a a a a a a b b b b b b b b b

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 global traversal

slide-9
SLIDE 9

Context: Data structure traversal

f global traversal f local operation

slide-10
SLIDE 10

Context: environment

for i = 0 to n for j = 0 to m B[i][j] = 0 for x = 1 to (n-1) for y = 1 to (m-1) B[x][y] = f(A, x, y)

! =

{n,m,f,A,B,i} {n,m,f,A,B,i,j} {n,m,f,A,B,i,j} {n,m,f,A,B,x} {n,m,f,A,B,x,y} {n,m,f,A,B,x,y}

slide-11
SLIDE 11

Context: “program counter”

i = <0, ..., n, 1, ..., n-1> j = <0, ..., m, 1, ..., m-1> for i = 0 to n for j = 0 to m B[i][j] = 0 for i = 1 to (n-1) for j = 1 to (m-1) B[i][j] = f(A, i, j)

slide-12
SLIDE 12

Classifying computations 1). Types

expression : type

x + 1 : ?

slide-13
SLIDE 13

Classifying computations 1). Types

y : Int ⊢ (fn x . x + y) : Int " Int

expression : type

x + 1 :

scope ⊢

x : Int ⊢ Int

slide-14
SLIDE 14

v0:t0, ..., vn:tn ⊢ e : t ⟦ ⟧ :

Classifying computations 1I). Common semantic structure

(t0, ..., tn) t C

encodes context

  • Mathematical structure on C (monoidal comonads)
  • Parameterise semantics by a particular C +
  • perations

(Uustalu, Vene - 2008)

slide-15
SLIDE 15

? coeffect scope

Classifying computations 1I). Common semantic structure

  • Coeffects - analysis of context-dependence

⊢ expression : type

∅?{gps} ⊢ access gps : Coord ∅?{} ⊢ (fn x . access gps + x) : Int " Coord

{gps}

(Petricek, Orchard, Mycroft - 2012)

  • Parameterise analysis by a coeffect algebra (C,⊔,⊕,⊓,e)

latent coeffect

slide-16
SLIDE 16

encodes context that satisfies R

⟦ ⟧

Classifying computations 1I). Common semantic structure

  • Coeffects - analysis of context-dependence

v0:t0, ..., vn:tn ? R ⊢ e : t : (t0, ..., tn) t CR

(Petricek, Orchard, Mycroft - 2012)

  • Further mathematical structure (not today!)
slide-17
SLIDE 17

coeffects types

expr

“what” “how”

slide-18
SLIDE 18

!1 ? c1 ⊢ e1 : t1 !2 ? c2 ⊢ e2 : t2 #(!1, !2) ? c1⊛c2 ⊢ E(e1,e2) : T(t1,t2)

premise conclusion

Reminder: inductive inference rules

syntax tree

e1 e2 E

slide-19
SLIDE 19

Example 1

  • Simple functional language
  • Context = environment
  • Coeffect = liveness (F / T)

! ? F ⊢ 3 : Int

dead context

! ? T ⊢ x : t

live context

⟦ (fn x . 3)(2) ⟧ = ⟦ 3 ⟧

  • Informs “dead code” elimination
slide-20
SLIDE 20

!?{r} ⊢ access r : t ! ? f1 ⊢ e : t1 " t2

f2

! ? f1 ⊢ send device e : ()

(device has f2 resources)

Example 1I

  • Simple functional language + distributed resources

send client (fn name . insert (access db) “location” name (access gps))

  • e.g.
  • Coeffect = sets of required resources
slide-21
SLIDE 21

⊢ e1 : t1 !

Coeffects : combining sub-term requirements

⊢ e2 : t2 ⊢ (e1, e2) : (t1, t2) ! !

  • Shared context
slide-22
SLIDE 22

⊢ e1 : t1 ! ? x ⊔ y ? x

Coeffects : combining sub-term requirements

⊢ e2 : t2 ⊢ (e1, e2) : (t1, t2) ! ! ? y

  • Shared context
  • e.g.

resources

⊔ = ⋃ ! ? {r} ⊢ e1 : t1 ! ? {r,s} ⊢ e2 : t2 ! ? {r,s} ⊢ (e1, e2) : (t1, t2)

slide-23
SLIDE 23

⊢ e1 : t1 ! ? x ⊔ y ? x

Coeffects : combining sub-term requirements

⊢ e2 : t2 ⊢ (e1, e2) : (t1, t2) ! ! ? y

  • Shared context
  • e.g.

resources

⊔ = ⋃ ! ? {r} ⊢ e1 : t1 ! ? {r,s} ⊢ e2 : t2 ! ? {r,s} ⊢ (e1, e2) : (t1, t2) ⊔ = ⋁

  • e.g.

liveness

! ? F ⊢ e1 : t1 ! ? T ⊢ e2 : t2 ! ? T ⊢ (e1, e2) : (t1, t2)

slide-24
SLIDE 24

⊢ e1(e2) : t

Coeffects : application

⊢ e1 : s " t ⊢ e2 : s ! ! !

slide-25
SLIDE 25

⊢ e1(e2) : t ? x ⊔(y ⊕ z)

Coeffects : application

⊢ e1 : s " t ⊢ e2 : s ! ! ! ? x ? y z

  • e.g. resources

⊔ = ⋃, ⊕ = ⋃ (i.e. x ⋃y ⋃ z) ⊢ e1(e2) : t2 ! ? {r,s,t,u} ! ? {r} ⊢ e1 : t1 " t2 ! ? {s,t} ⊢ e2 : t1

{r,u}

slide-26
SLIDE 26

? y ? x ⊢ e1(e2) : t ? x ⊔(y ⊕ z)

Coeffects : application

⊢ e1 : s " t ⊢ e2 : s ! ! ! z

  • e.g. liveness

⊔ = ⋁, ⊕ = ⋀ (i.e. x ⋁(y ⋀ z)) ! ? F ⊢ e1 : t1 " t2 ! ? T ⊢ e2 : t1 ⊢ e1(e2) : t2 ! ? F

F

slide-27
SLIDE 27

Coeffect : abstraction

!, x : t1 ⊢ e : t2 ! ⊢ (fn x . e) : t1 " t2

slide-28
SLIDE 28

Coeffect : abstraction

!, x : t1 ⊢ e : t2 ! ⊢ (fn x . e) : t1 " t2 ? r ? x

r = x ⨅ y

  • e.g. resources

⨅ = ⋃

!, x : t1 ? {r,s} ⊢ e : t2 ! ? {r} ⊢ (fn x . e) : t1 " t2

{s}

(possibility 1)

y

slide-29
SLIDE 29

Coeffect : abstraction

!, x : t1 ⊢ e : t2 ! ⊢ (fn x . e) : t1 " t2 ? r ? x

r = x ⨅ y

  • e.g. resources

⨅ = ⋃

!, x : t1 ? {r,s} ⊢ e : t2 ! ? {s} ⊢ (fn x . e) : t1 " t2

{r}

(possibility 2)

y

slide-30
SLIDE 30

Coeffect : abstraction

!, x : t1 ⊢ e : t2 ! ⊢ (fn x . e) : t1 " t2 ? r ? x

r = x ⨅ y

  • e.g. resources

⨅ = ⋃

!, x : t1 ? {r,s} ⊢ e : t2 ! ? {r,s} ⊢ (fn x . e) : t1 " t2

{}

(possibility 3) and so on...

y

slide-31
SLIDE 31

Coeffect : abstraction

!, x : t1 ⊢ e : t2 ! ⊢ (fn x . e) : t1 " t2 ? r ? x

r = x ⨅ y

! ? f1 ⊢ e : t1 " t2

f2

! ? f1 ⊢ send device e : ()

(device has f2 resources)

  • e.g. resources

⨅ = ⋃

  • “sending” a function reduces the possibilities

y

slide-32
SLIDE 32

Coeffect : abstraction

!, x : t1 ⊢ e : t2 ! ⊢ (fn x . e) : t1 " t2 ? r ? x

r = x ⨅ y

  • e.g.

liveness ⨅ = ⋀

!, x : t1 ? T ⊢ e : t2 ! ? T ⊢ (fn x . e) : t1 " t2

T y

slide-33
SLIDE 33

Coeffect : abstraction

!, x : t1 ⊢ e : t2 ! ⊢ (fn x . e) : t1 " t2 ? r ? x

y

r = x ⨅ y

  • e.g.

liveness ⨅ = ⋀

!, x : t1 ? F ⊢ e : t2 ! ? F ⊢ (fn x . e) : t1 " t2

T

(possibility 1) etc.

slide-34
SLIDE 34

Coeffects

!, x : t1 ? x ⨅ y ⊢ e : t2 ! ⊢ (fn x . e) : t1 " t2 ? x

y

For a coeffect algebra (C,⊔,⊕,⊓,e)

⊢ e1(e2) : t ! ? x ⊔ (y ⊕ z) ! ? x ⊢ e1 : s " t !? y ⊢ e2 : s

z

! ? x ⊢ e1 : t1 ! ? y ⊢ e2 : t2 ! ? x ⊔ y ⊢ (e1, e2) : (t1, t2) x : t1 ∈ ! ! ? e ⊢ x : t1

  • Captures lots of different kinds of contextual computation
slide-35
SLIDE 35

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)

slide-36
SLIDE 36
  • Solution: exterior elements
  • Requires array with adequate exterior

Ypnos

  • At boundaries?

? traverse interior

exterior provides boundary values

slide-37
SLIDE 37

Ypnos

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

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.:
  • Recall
slide-38
SLIDE 38

Ypnos

!; x : t1 ? r ⊢ e : t2 ! ⊢(stencil x . e) : Array r t1 " t2

  • Two-level language
  • Haskell (outer)

! ⊢ e : t

  • Ypnos stencils (inner)

!; !’ ? r ⊢ e : t

  • Interface between the two:

exterior size data access

slide-39
SLIDE 39

Ypnos - Grid Patterns

  • Array access only by pattern matching

stencil A . (A[0][0] + A[-1][0] +

A[1][0] + A[0][1] + A[0][-1]) /5.0

  • e.g.

stencil | _ t _ |

| l @c r | | _ b _ | . (t + l + c + r + b)/5.0

  • Patterns are static => decidable coeffects
slide-40
SLIDE 40

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)

slide-41
SLIDE 41

“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