Last time: generic programming val show : a data a string 1/ 65 - - PowerPoint PPT Presentation

last time generic programming val show a data a string
SMART_READER_LITE
LIVE PREVIEW

Last time: generic programming val show : a data a string 1/ 65 - - PowerPoint PPT Presentation

Last time: generic programming val show : a data a string 1/ 65 This time: staging . < e > . 2/ 65 Review: abstraction Row polymorphism Lambda abstraction x : A . M [ > `A | `B] A :: K . M A :: K . B


slide-1
SLIDE 1

Last time: generic programming val show : ’a data → ’a → string

1/ 65

slide-2
SLIDE 2

This time: staging .< e >.

2/ 65

slide-3
SLIDE 3

Review: abstraction

Lambda abstraction λx : A.M ΛA :: K.M λA :: K.B Modular abstraction module F(X : T) = . . . First-class ∀ and ∃ type t = { f : ’ a . . . . } type t = E : ’ a s → t Row polymorphism [> `A | `B] Abstraction of type equalities a ≡ b Interfaces to computation m > > = k f ⊗ p Abstraction over data shape v a l show : ’ a data → ’ a → s t r i n g

3/ 65

slide-4
SLIDE 4

The cost of ignorance

Fewer opportunities for optimization

l e t both eq1 : i n t ∗ i n t → i n t ∗ i n t → bool = fun ( x1 , y1 ) ( x2 , y2 ) → x1 = x2 && y1 = y2 l e t both eq2 : ( i n t → i n t → bool ) → i n t ∗ i n t → i n t ∗ i n t → bool = fun eq ( x1 , y1 ) ( x2 , y2 ) → eq x1 x2 && eq y1 y2 both eq2 ( fun x y → x = y ) type eq = { eq : ’ a . ’ a → ’ a → bool } l e t both eq {eq} ( x1 , y1 ) ( x2 , y2 ) = eq x1 x2 && eq y1 y2

4/ 65

slide-5
SLIDE 5

The cost of ignorance

Interpretative overhead

l e t p r i n t i n t p a i r ( x , y ) = p r i n t c h a r ’ ( ’ ; p r i n t i n t x ; p r i n t c h a r ’ , ’ ; p r i n t i n t y ; p r i n t c h a r ’) ’ l e t p r i n t i n t p a i r 2 ( x , y ) = P r i n t f . s p r i n t f ”(%d,%d)” x y l e t p r i n t i n t p a i r 3 ( x , y ) = p r i n t s t r i n g ( gshow ( p a i r i n t i n t ) ( x , y ))

5/ 65

slide-6
SLIDE 6

Abstraction wants to be free

l e t pow2 x = x ∗ x (∗ x2 ∗) l e t pow3 x = x ∗ x ∗ x (∗ x3 ∗) l e t pow5 x = x ∗ x ∗ x ∗ x ∗ x (∗ x5 ∗) l e t rec pow x n = (∗ xn ∗) i f n = 0 then 1 e l s e x ∗ pow x (n − 1)

6/ 65

slide-7
SLIDE 7

Power, staged

l e t rec pow x n = i f n = 0 then .< 1 >. e l s e .< .˜x ∗ .˜(pow x (n − 1)) >.

7/ 65

slide-8
SLIDE 8

Power, staged

l e t rec pow x n = i f n = 0 then .< 1 >. e l s e .< .˜x ∗ .˜(pow x (n − 1)) >. v a l pow : i n t code → i n t → i n t code

8/ 65

slide-9
SLIDE 9

Power, staged

l e t rec pow x n = i f n = 0 then .< 1 >. e l s e .< .˜x ∗ .˜(pow x (n − 1)) >. v a l pow : i n t code → i n t → i n t code l e t pow code n = .< fun x → .˜(pow .<x>. n) >.

9/ 65

slide-10
SLIDE 10

Power, staged

l e t rec pow x n = i f n = 0 then .< 1 >. e l s e .< .˜x ∗ .˜(pow x (n − 1)) >. v a l pow : i n t code → i n t → i n t code l e t pow code n = .< fun x → .˜(pow .<x>. n) >. v a l pow code : i n t → ( i n t → i n t ) code

10/ 65

slide-11
SLIDE 11

Power, staged

l e t rec pow x n = i f n = 0 then .< 1 >. e l s e .< .˜x ∗ .˜(pow x (n − 1)) >. v a l pow : i n t code → i n t → i n t code l e t pow code n = .< fun x → .˜(pow .<x>. n) >. v a l pow code : i n t → ( i n t → i n t ) code # pow code 3 ; ; .<fun x → x ∗ x ∗ x ∗ 1>.

11/ 65

slide-12
SLIDE 12

Power, staged

l e t rec pow x n = i f n = 0 then .< 1 >. e l s e .< .˜x ∗ .˜(pow x (n − 1)) >. v a l pow : i n t code → i n t → i n t code l e t pow code n = .< fun x → .˜(pow .<x>. n) >. v a l pow code : i n t → ( i n t → i n t ) code # pow code 3 ; ; .<fun x → x ∗ x ∗ x ∗ 1>. # l e t pow3 ’ = ! . ( pow code 3 ) ; ; v a l pow3 ’ : i n t → i n t = <fun>

12/ 65

slide-13
SLIDE 13

Power, staged

l e t rec pow x n = i f n = 0 then .< 1 >. e l s e .< .˜x ∗ .˜(pow x (n − 1)) >. v a l pow : i n t code → i n t → i n t code l e t pow code n = .< fun x → .˜(pow .<x>. n) >. v a l pow code : i n t → ( i n t → i n t ) code # pow code 3 ; ; .<fun x → x ∗ x ∗ x ∗ 1>. # l e t pow3 ’ = ! . ( pow code 3 ) ; ; v a l pow3 ’ : i n t → i n t = <fun> # pow3 ’ 4 ; ; − : i n t = 64

13/ 65

slide-14
SLIDE 14

MetaOCaml basics

14/ 65

slide-15
SLIDE 15

Quoting

l e t x = ”w” in l e t y = ”x” in p r i n t s t r i n g ( x ˆ y ) l e t x = ”w” in l e t y = x in p r i n t s t r i n g ( x ˆ y ) l e t x = ”w” in l e t y = x in p r i n t s t r i n g (” x ˆ y ”) l e t x = ”w” in l e t y = x in p r i n t s t r i n g (” x” ˆ y )

Quoting prevents evaluation.

15/ 65

slide-16
SLIDE 16

Quoting code

MetaOCaml: multi-stage programming with code quoting. Stages: current (available now) and delayed (available later). (Also double-delayed, triple-delayed, etc.)

Brackets .< e >. Escaping (within brackets) .˜e Running code ! . e Cross-stage persistence .< x >.

16/ 65

slide-17
SLIDE 17

Quoting and escaping: some examples

.< 3 >. .< 1 + 2 >. .< [1; 2; 3] >. .< x + y >. .< fun x → x >. .< (.˜f) 3 >. .< .˜(f 3) >. .< fun x → .˜(f .< x >.) >.

17/ 65

slide-18
SLIDE 18

Quoting: typing

Γ ⊢n e : τ Γ ⊢n+ e : τ T-bracket Γ ⊢n .< e >. : τ code Γ ⊢n e : τ code T-escape Γ ⊢n+ .˜e : τ Γ+ ⊢n e : τ code T-run Γ ⊢n !. e : τ Γ(x) = τ (n−m) T-var Γ ⊢n : τ

18/ 65

slide-19
SLIDE 19

Quoting: open code

Open code l e t pow code n = .< fun x → .˜(pow .<x>. n) >. Cross-stage persistence l e t p r i n t i n t p a i r ( x , y ) = P r i n t f . p r i n t f ”(%d,%d)” x y l e t p a i r s = .< [ ( 3 , 4 ) ; (5 , 6 ) ] >. .< L i s t . i t e r p r i n t i n t p a i r .˜p a i r s >.

19/ 65

slide-20
SLIDE 20

Quoting: scoping

Scoping is lexical, just as in OCaml. .< fun x → .˜( l e t x = 3 in .<x>. ) >. l e t x = 3 in .< fun x → .˜( .<x >.) >. MetaOCaml renames variables to avoid clashes: .< l e t x = 3 in .˜( l e t y = .<x>. in .< fun x → .˜y + x >. ) >.

20/ 65

slide-21
SLIDE 21

Quoting: scoping

Scoping is lexical, just as in OCaml. .< fun x → .˜( l e t x = 3 in .<x>. ) >. l e t x = 3 in .< fun x → .˜( .<x >.) >. MetaOCaml renames variables to avoid clashes: # .< l e t x = 3 in .˜( l e t y = .<x>. in .< fun x → .˜y + x >. ) >.; ; − : ( i n t → i n t ) code = .<l e t x 1 = 3 in fun x 2 → x 1 + x 2>.

21/ 65

slide-22
SLIDE 22

Learning from mistakes

22/ 65

slide-23
SLIDE 23

Error: quoting nonsense

.< 1 + ”two” >.

23/ 65

slide-24
SLIDE 24

Error: quoting nonsense

# .< 1 + ”two” >.;; Characters 7−12: .< 1 + ”two” >.;; ˆˆˆˆˆ Error : This e x p r e s s i o n has type s t r i n g but an e x p r e s s i o n was expected

  • f

type i n t

24/ 65

slide-25
SLIDE 25

Error: looking into the future

.< fun x → .˜( x ) >.

25/ 65

slide-26
SLIDE 26

Error: looking into the future

# .< fun x → .˜( x ) >.; ; Characters 14−19: .< fun x → .˜( x ) >.; ; ˆˆˆˆˆ Wrong l e v e l : v a r i a b l e bound at l e v e l 1 and used at l e v e l

26/ 65

slide-27
SLIDE 27

Error: escape from nowhere

l e t x = .< 3 >. in .˜x

27/ 65

slide-28
SLIDE 28

Error: escape from nowhere

# l e t x = .< 3 >. in .˜ x ; ; Characters 22−23: l e t x = .< 3 >. in .˜ x ; ; ˆ Wrong l e v e l : escape at l e v e l

28/ 65

slide-29
SLIDE 29

Error: running open code

.< fun x → .˜( ! . .<x>. ) >.

29/ 65

slide-30
SLIDE 30

Error: running open code

# .< fun x → . ˜ ( ! . .<x>. ) >.;; Exception : F a i l u r e ”The code b u i l t at Characters 7−8:\n .< fun x → . ˜ ( ! . .<x>. ) >.;;\ n ˆ\n i s not c l o s e d : i d e n t i f i e r x 2 bound at Characters 7−8:\n .< fun x → . ˜ ( ! . .<x>. ) >.;;\ n ˆ\n i s f r e e ”.

30/ 65

slide-31
SLIDE 31

Learning by doing

31/ 65

slide-32
SLIDE 32

Power again

Reducing the number of multiplications: x0 = 1 x2n+2 = (xn+1)2 x2n+1 = x(x2n) l e t even x = x mod 2 = 0 l e t sqr x = x ∗ x l e t rec pow x n = i f n = 0 then 1 e l s e i f even n then sqr (pow x (n / 2)) e l s e x ∗ pow (n − 1) x

32/ 65

slide-33
SLIDE 33

Power again, staged

Reducing the number of multiplications: x0 = 1 x2n+2 = (xn+1)2 x2n+1 = x(x2n) l e t even x = x mod 2 = 0 l e t sqr x = .< l e t y = .˜x in y ∗ y >. l e t rec pow ’ x n = i f n = 0 then .<1>. e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e .< .˜x ∗ .˜(pow ’ x (n − 1)) >.

33/ 65

slide-34
SLIDE 34

Power again, staged

l e t rec pow ’ x n = i f n = 0 then .<1>. e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e .< .˜x ∗ .˜(pow ’ x (n − 1)) >.

34/ 65

slide-35
SLIDE 35

Power again, staged

l e t rec pow ’ x n = i f n = 0 then .<1>. e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e .< .˜x ∗ .˜(pow ’ x (n − 1)) >. v a l pow ’ : i n t code → i n t → i n t code

35/ 65

slide-36
SLIDE 36

Power again, staged

l e t rec pow ’ x n = i f n = 0 then .<1>. e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e .< .˜x ∗ .˜(pow ’ x (n − 1)) >. v a l pow ’ : i n t code → i n t → i n t code l e t pow code ’ n = .< fun x → .˜(pow ’ .<x>. n) >.

36/ 65

slide-37
SLIDE 37

Power again, staged

l e t rec pow ’ x n = i f n = 0 then .<1>. e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e .< .˜x ∗ .˜(pow ’ x (n − 1)) >. v a l pow ’ : i n t code → i n t → i n t code l e t pow code ’ n = .< fun x → .˜(pow ’ .<x>. n) >. v a l pow code ’ : i n t → ( i n t → i n t ) code

37/ 65

slide-38
SLIDE 38

Power again, staged

l e t rec pow ’ x n = i f n = 0 then .<1>. e l s e i f even n then sqr (pow ’ x (n / 2)) e l s e .< .˜x ∗ .˜(pow ’ x (n − 1)) >. v a l pow ’ : i n t code → i n t → i n t code l e t pow code ’ n = .< fun x → .˜(pow ’ .<x>. n) >. v a l pow code ’ : i n t → ( i n t → i n t ) code # pow code ’ 5 ; ; − : ( i n t → i n t ) code = .< fun x → x ∗ ( l e t y = l e t y ’ = x ∗ 1 in y ’ ∗ y ’ in y ∗ y )>.

38/ 65

slide-39
SLIDE 39

The staging process, idealized

  • 1. Write the program as usual:

v a l program : t s t a → t dyn → t

39/ 65

slide-40
SLIDE 40

The staging process, idealized

  • 1. Write the program as usual:

v a l program : t s t a → t dyn → t

  • 2. Add staging annotations:

v a l staged program : t s t a → t dyn code → t code

39/ 65

slide-41
SLIDE 41

The staging process, idealized

  • 1. Write the program as usual:

v a l program : t s t a → t dyn → t

  • 2. Add staging annotations:

v a l staged program : t s t a → t dyn code → t code

  • 3. Compile using back:

v a l back : ( ’ a code → ’b code ) → ( ’ a → ’b) code v a l code generator : t s t a → ( t dyn → t )

39/ 65

slide-42
SLIDE 42

The staging process, idealized

  • 1. Write the program as usual:

v a l program : t s t a → t dyn → t

  • 2. Add staging annotations:

v a l staged program : t s t a → t dyn code → t code

  • 3. Compile using back:

v a l back : ( ’ a code → ’b code ) → ( ’ a → ’b) code v a l code generator : t s t a → ( t dyn → t )

  • 4. Construct static inputs:

v a l s : t s t a

39/ 65

slide-43
SLIDE 43

The staging process, idealized

  • 1. Write the program as usual:

v a l program : t s t a → t dyn → t

  • 2. Add staging annotations:

v a l staged program : t s t a → t dyn code → t code

  • 3. Compile using back:

v a l back : ( ’ a code → ’b code ) → ( ’ a → ’b) code v a l code generator : t s t a → ( t dyn → t )

  • 4. Construct static inputs:

v a l s : t s t a

  • 5. Apply code generator to static inputs:

v a l s p e c i a l i z e d c o d e : ( t dyn → t ) code

39/ 65

slide-44
SLIDE 44

The staging process, idealized

  • 1. Write the program as usual:

v a l program : t s t a → t dyn → t

  • 2. Add staging annotations:

v a l staged program : t s t a → t dyn code → t code

  • 3. Compile using back:

v a l back : ( ’ a code → ’b code ) → ( ’ a → ’b) code v a l code generator : t s t a → ( t dyn → t )

  • 4. Construct static inputs:

v a l s : t s t a

  • 5. Apply code generator to static inputs:

v a l s p e c i a l i z e d c o d e : ( t dyn → t ) code

  • 6. Run specialized code to build a specialized function:

v a l s p e c i a l i z e d f u n c t i o n : t dyn → t

39/ 65

slide-45
SLIDE 45

Inner product

l e t dot : i n t → f l o a t array → f l o a t array → f l o a t = fun n l r → l e t rec loop i = i f i = n then 0. e l s e l . ( i ) ∗. r . ( i ) +. loop ( i + 1) in loop 0

40/ 65

slide-46
SLIDE 46

Inner product, loop unrolling

l e t dot ’ : i n t → f l o a t array code → f l o a t a rray code → f l o a t code = fun n l r → l e t rec loop i = i f i = n then .< 0. >. e l s e .< ((.˜l ) . ( i ) ∗. (.˜r ) . ( i )) +. .˜( loop ( i + 1)) >. in loop 0

41/ 65

slide-47
SLIDE 47

Inner product, loop unrolling

# .< fun l r → .˜( dot ’ 3 .<l>..<r>. ) >.; ; − : ( f l o a t a rray → f l o a t a rray → f l o a t ) code = .< fun l r → ( l . ( 0 ) ∗. r . ( 0 ) ) +. (( l . ( 1 ) ∗. r . ( 1 ) ) +. (( l . ( 2 ) ∗. r . ( 2 ) ) +. 0 . ) )>.

42/ 65

slide-48
SLIDE 48

Inner product, eliding no-ops

l e t dot ’ ’ : f l o a t array → f l o a t array code → f l o a t code = fun l r → l e t n = Array . length l in l e t rec loop i = i f i = n then .< 0. >. e l s e match l . ( i ) with 0.0 → loop ( i + 1) | 1.0 → .< (.˜r ) . ( i ) +. .˜( loop ( i + 1)) >. | x → .< ( x ∗. (.˜r ) . ( i )) +. .˜( loop ( i + 1)) >. in loop 0

43/ 65

slide-49
SLIDE 49

Inner product, eliding no-ops

# .< fun r → .˜( dot ’ ’ [ | 1 . 0 ; 0 . 0 ; 3.5 | ] .<r>. ) >.; ; − : ( f l o a t a rray → f l o a t ) code = .< fun r → r . ( 0 ) +. ( ( 3 . 5 ∗. r . ( 2 ) ) +. 0 . )>.

44/ 65

slide-50
SLIDE 50

Binding-time analysis

Classify variables into dynamic (’a code) / static (’a)

l e t dot ’ : i n t → f l o a t array code → f l o a t a rray code → f l o a t code = fun n l r →

dynamic: l, r static: n Classify expressions into static (no dynamic variables) / dynamic

i f i = n then 0 e l s e l . ( i ) ∗. r . ( i )

dynamic: l .( i) ∗. r .( i) static: i = n Goal: reduce static expressions during code generation.

45/ 65