Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General...

42
Efficient normalization by evaluation Mathieu Boespflug ´ Ecole Polytechnique 15 August 2009 1 / 26

Transcript of Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General...

Page 1: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Efficient normalization by evaluation

Mathieu Boespflug

Ecole Polytechnique

15 August 2009

1 / 26

Page 2: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Convertibility

t1 →β . . .←β . . .→β . . .←β t2

2 / 26

Page 3: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

3 / 26

Page 4: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Convertibility

t1 t2

↓∗β ↓∗β

t ′1?≡ t ′2

4 / 26

Page 5: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

M : 55 < 1337

M : (λx . 55) 10 < 1337

M : fib 10 < 1337

5 / 26

Page 6: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

M : 55 < 1337

M : (λx . 55) 10 < 1337

M : fib 10 < 1337

5 / 26

Page 7: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

M : 55 < 1337

M : (λx . 55) 10 < 1337

M : fib 10 < 1337

5 / 26

Page 8: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

The conversion test

Γ ` t : A A ≡β BΓ ` t : B

Seek:

I simplicity

I efficiency

6 / 26

Page 9: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

fast

cheap general

7 / 26

Page 10: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

fast cheap

general

7 / 26

Page 11: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

fast cheap general

7 / 26

Page 12: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

General overview

Λ Code(GHC)

I Plenty of existing (fast) reduction devices.

I Solution: reuse them!

I Advantage: separation of concerns.

8 / 26

Page 13: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

General overview

Λ Code(GHC)

I Plenty of existing (fast) reduction devices.

I Solution: reuse them!

I Advantage: separation of concerns.

8 / 26

Page 14: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

General overview

Λ Code(GHC)

I Plenty of existing (fast) reduction devices.

I Solution: reuse them!

I Advantage: separation of concerns.

8 / 26

Page 15: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Interpretation of terms

[[x ]] n = x if x < n

[[x ]] n = Con x otherwise

[[λx . t]] n = Abs (λn→ [[t]] (n + 1))

[[t1 t2]] n = App ([[t1]] n) ([[t2]] n)

9 / 26

Page 16: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Interpretation of terms (example)

[[(λx . (λy . y x)) z ]]

ap (Abs (λx → Abs (λy → ap y x))) (Con "0")

10 / 26

Page 17: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Interpretation of terms (example)

[[(λx . (λy . y x)) z ]]

ap (Abs (λx → Abs (λy → ap y x))) (Con "0")

10 / 26

Page 18: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

A simple HOAS normalizer

norm n (App t1 t2) =case norm n t1 ofAbs t ′1 → norm n (t ′1 t2)t ′1 → t ′1 @ (norm n t2)

norm n (Abs t) =λ. (norm (n + 1) (t (Con (show n))))

norm n (Con c) = c

11 / 26

Page 19: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Towards normalization by evaluation

ap t1 t2 = case norm n t1 ofAbs t ′1 → norm n (t ′1 t2)t ′1 → t ′1 @ (norm n t2)

norm n (App t1 t2) = ap t1 t2

norm n (Abs t) =λ. (norm (n + 1) (t (Con (show n))))

norm n (Con c) = c

12 / 26

Page 20: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Interpretation of terms (revised)

[[x ]] n = x if x < n

[[x ]] n = Con x otherwise

[[λx . t]] n = Abs (λn→ [[t]] (n + 1))

[[t1 t2]] n = ap ([[t1]] n) ([[t2]] n)

13 / 26

Page 21: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Normalizer (revised)

ap (Abs f ) t = f tap t1 t2 = t1 @ t2

norm n (Abs t) =λ. (norm (n + 1) (t (Con (show n))))

norm n (Con c) = c

14 / 26

Page 22: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Optimizations

15 / 26

Page 23: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Intermediate closures (example)

[[map id nil ]]= ap (ap map id) nil

= ap (ap (Abs (λf → Abs (λl → ...))) id) nil→β ap (Abs (λl → ...)) nilnil

16 / 26

Page 24: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Intermediate closures (example)

[[map id nil ]]= ap (ap map id) nil= ap (ap (Abs (λf → Abs (λl → ...))) id) nil

→β ap (Abs (λl → ...)) nilnil

16 / 26

Page 25: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Intermediate closures (example)

[[map id nil ]]= ap (ap map id) nil= ap (ap (Abs (λf → Abs (λl → ...))) id) nil→β ap (Abs (λl → ...)) nil

nil

16 / 26

Page 26: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Intermediate closures (example)

[[map id nil ]]= ap (ap map id) nil= ap (ap (Abs (λf → Abs (λl → ...))) id) nil→β ap (Abs (λl → ...)) nilnil

16 / 26

Page 27: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Supernumerary arguments (example)

(λx . (λy . x)) (λz . z) 1 2

17 / 26

Page 28: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Uncurrying

[[x ]] n = x if x < n

[[x ]] n = Con x otherwise

[[λ. · · ·λ. t]] n = Absm (λn . . . n + m→ [[t]] (n + m))

[[t1 . . . tm]] n = apm ([[t1]] n) ... ([[tm]] n)

A family of ap operators

1. apn (Absm f ) t1 . . . tn =Absm−n (f t1 . . . tn)

2. apn (Absm f ) t1 . . . tn = f t1 . . . tn

3. apn (Absm f ) t1 . . . tn =apn−m (f t1 . . . tm) tm+1 . . . tn

Condition on (1): n < mCondition on (2): n = mCondition on (3): n > m.

18 / 26

Page 29: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Intermediate closures (revised example)

[[map id nil ]]= ap2 map id nil

= ap2 (Abs2 (λf l → ...)) id nilnil

19 / 26

Page 30: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Intermediate closures (revised example)

[[map id nil ]]= ap2 map id nil= ap2 (Abs2 (λf l → ...)) id nil

nil

19 / 26

Page 31: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Intermediate closures (revised example)

[[map id nil ]]= ap2 map id nil= ap2 (Abs2 (λf l → ...)) id nilnil

19 / 26

Page 32: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Uncurrying: Remarks

I Drastically reduces number of intermediate closuresconstructed in the common case.

I No help in pathological cases. But they are rare.

I Only need (small) finite number of ap operators.

20 / 26

Page 33: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Embedding pattern matching

Many runtime environments compile patternmatching problems to efficient backtrackingautomata or decision trees.

Idea: Extend model with constructors for all constructorsin all datatypes introduced in the object language.

−→ Space and time efficient representation of data.

21 / 26

Page 34: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Embedding pattern matching

Many runtime environments compile patternmatching problems to efficient backtrackingautomata or decision trees.

Idea: Extend model with constructors for all constructorsin all datatypes introduced in the object language.−→ Space and time efficient representation of data.

21 / 26

Page 35: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Microbenchmarks

append even sort exp38 queens

0

1

2

3

ahn singlearity evalapply constructors ucea whnf

flavor append % even % sort % exp3-8 % queens %

ahn 3.61 1031 3.17 253 1.04 433 1.23 261 1.34 670evalapply 3.21 917 1.50 120 1.03 429 1.05 223 0.35 175singlearity 3.49 997 2.29 183 1.03 429 1.37 191 0.76 380constructors 0.44 125 2.26 180 0.53 220 0.66 140 0.68 340ucea 0.45 128 1.50 120 0.28 116 0.47 100 0.25 120whnf 0.35 100 1.25 100 0.24 100 0.47 100 0.20 100

22 / 26

Page 36: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Macrobenchmarks

variables 2 % 3 % 4 % 5 %

no conv 0.68 94 1.40 93 2.25 77 3.92 3.11nbe 0.70 97 1.42 94 2.30 79 27.27 20.02Coq VM 0.72 100 1.50 100 2.92 100 136.2 100

Table: Solving formulae of n variables with Cooper’s quantifierelimination.

23 / 26

Page 37: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

fast

X

cheap

X

general

X

24 / 26

Page 38: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

fast X cheap

X

general

X

24 / 26

Page 39: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

fast X cheap X general

X

24 / 26

Page 40: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

fast X cheap X general X

24 / 26

Page 41: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Related work

fast cheap general

Isabelle NbE X XTDPE X XCoq VM X X

Isabelle NbE: compilation to SML (Aehlig et al 2008)

TDPE: type directed partial evaluation (Danvy 1998)

Coq VM: extended version of OCaml virtual machine (Gregoireand Leroy 2002)

25 / 26

Page 42: Mathieu Boesp ug Ecole Polytechnique 15 August 2009mboes/talks/padl10/slides.pdf · General overview I Plenty of existing (fast) reduction devices. I Solution: reuse them! I Advantage:

Final words

Limitations:

I Fixed evaluation order

I Potential impedance mismatch between object-level patternmatching and meta-level pattern matching.

Future work:

I Short-circuit evaluation.

26 / 26