The Lifting Lemma Ralf Hinze Computing Laboratory, University of - - PowerPoint PPT Presentation

the lifting lemma
SMART_READER_LITE
LIVE PREVIEW

The Lifting Lemma Ralf Hinze Computing Laboratory, University of - - PowerPoint PPT Presentation

The Lifting Lemma WG2.8 The Lifting Lemma Ralf Hinze Computing Laboratory, University of Oxford Wolfson Building, Parks Road, Oxford, OX1 3QD, England ralf.hinze@comlab.ox.ac.uk http://www.comlab.ox.ac.uk/ralf.hinze/ June 2009 University of


slide-1
SLIDE 1

The Lifting Lemma WG2.8

The Lifting Lemma

Ralf Hinze

Computing Laboratory, University of Oxford Wolfson Building, Parks Road, Oxford, OX1 3QD, England ralf.hinze@comlab.ox.ac.uk http://www.comlab.ox.ac.uk/ralf.hinze/

June 2009

University of Oxford — Ralf Hinze 1-42

slide-2
SLIDE 2

The Lifting Lemma WG2.8

Mathematicians do it . . .

(f + g)(x) = f(x) + g(x)

University of Oxford — Ralf Hinze 2-42

slide-3
SLIDE 3

The Lifting Lemma WG2.8

. . . over and . . .

A + B = { a + b | a ∈ A, b ∈ B}

University of Oxford — Ralf Hinze 3-42

slide-4
SLIDE 4

The Lifting Lemma WG2.8

. . . and over again.

(x1, x2, x3) + (y1, y2, y3) = (x1 + y1, x2 + y2, x3 + y3)

University of Oxford — Ralf Hinze 4-42

slide-5
SLIDE 5

The Lifting Lemma WG2.8

Haskell programmers do it . . .

data Maybe α = Nothing | Just α (+) :: Maybe N → Maybe N → Maybe N Nothing + n = Nothing m + Nothing = Nothing Just a + Just b = Just (a + b)

University of Oxford — Ralf Hinze 5-42

slide-6
SLIDE 6

The Lifting Lemma WG2.8

. . . over and over again.

(+) :: IO N → IO N → IO N m + n = do {a ← m; b ← n; return (a + b)}

University of Oxford — Ralf Hinze 6-42

slide-7
SLIDE 7

The Lifting Lemma WG2.8

I do it: lifting

data Stream α = Cons {head :: α, tail :: Stream α} (+) :: Stream N → Stream N → Stream N s + t = Cons (head s + head t) (tail s + tail t)

University of Oxford — Ralf Hinze 7-42

slide-8
SLIDE 8

The Lifting Lemma WG2.8

Since the arithmetic operations are defined point-wise, the familiar arithmetic laws also hold for streams.

University of Oxford — Ralf Hinze 8-42

slide-9
SLIDE 9

The Lifting Lemma WG2.8

More general, given a point-level identity, does the lifted version hold as well?

University of Oxford — Ralf Hinze 9-42

slide-10
SLIDE 10

The Lifting Lemma WG2.8

(x + y) + z = x + (y + z)

University of Oxford — Ralf Hinze 10-42

slide-11
SLIDE 11

The Lifting Lemma WG2.8

x + y = y + x

University of Oxford — Ralf Hinze 11-42

slide-12
SLIDE 12

The Lifting Lemma WG2.8

x ∗ 0 = 0

University of Oxford — Ralf Hinze 12-42

slide-13
SLIDE 13

The Lifting Lemma WG2.8

Idioms

University of Oxford — Ralf Hinze 13-42

slide-14
SLIDE 14

The Lifting Lemma WG2.8

Idioms aka applicative functors

class Idiom ι where pure :: α → ι α (⋄) :: ι (α → β) → (ι α → ι β)

University of Oxford — Ralf Hinze 14-42

slide-15
SLIDE 15

The Lifting Lemma WG2.8

instance Idiom (τ→) where pure a = λx → a f ⋄ g = λx → (f x) (g x)

University of Oxford — Ralf Hinze 15-42

slide-16
SLIDE 16

The Lifting Lemma WG2.8

instance Idiom (τ→) where pure a = λx → a f ⋄ g = λx → (f x) (g x) So, pure is the K combinator and ⋄ is the S combinator.

University of Oxford — Ralf Hinze 15-42

slide-17
SLIDE 17

The Lifting Lemma WG2.8

instance Idiom Stream where pure a = s where s = a ≺ s s ⋄ t = Cons ((head s) (head t)) (tail s ⋄ tail t)

University of Oxford — Ralf Hinze 16-42

slide-18
SLIDE 18

The Lifting Lemma WG2.8

Lifting, generically

(+) :: (Idiom ι) ⇒ ι N → ι N → ι N u + v = pure (+) ⋄ u ⋄ v (⋆) :: (Idiom ι) ⇒ ι α → ι β → ι (α, β) u ⋆ v = pure (, ) ⋄ u ⋄ v

University of Oxford — Ralf Hinze 17-42

slide-19
SLIDE 19

The Lifting Lemma WG2.8

Idiom laws

pure id ⋄ u = u (identity) pure (·) ⋄ u ⋄ v ⋄ w = u ⋄ (v ⋄ w) (composition) pure f ⋄ pure x = pure (f x) (homomorphism) u ⋄ pure x = pure (⋄x) ⋄ u (interchange)

University of Oxford — Ralf Hinze 18-42

slide-20
SLIDE 20

The Lifting Lemma WG2.8

pure f ⋄ (u ⋆ v) = { definition of ⋆ } pure f ⋄ (pure (, ) ⋄ u ⋄ v) = { idiom composition } pure (·) ⋄ pure f ⋄ (pure (, ) ⋄ u) ⋄ v = { idiom homomorphism } pure (f ·) ⋄ (pure (, ) ⋄ u) ⋄ v = { idiom composition } pure (·) ⋄ pure (f ·) ⋄ pure (, ) ⋄ u ⋄ v = { idiom homomorphism } pure ((f ·) · (, )) ⋄ u ⋄ v = { ((f ·) · (, )) x y = (f · (, ) x) y = f ((, ) x y) = curry f x y } pure (curry f) ⋄ u ⋄ v

University of Oxford — Ralf Hinze 19-42

slide-21
SLIDE 21

The Lifting Lemma WG2.8

The Idiomatic Calculus

University of Oxford — Ralf Hinze 20-42

slide-22
SLIDE 22

The Lifting Lemma WG2.8

Syntax: variables

data Ix :: ∗ → ∗ → ∗ where Zero :: Ix (ρ, α) α Succ :: Ix ρ β → Ix (ρ, α) β

University of Oxford — Ralf Hinze 21-42

slide-23
SLIDE 23

The Lifting Lemma WG2.8

Syntax: terms

data Term :: ∗ → ∗ → ∗ where Con :: α → Term ρ α Var :: Ix ρ α → Term ρ α App :: Term ρ (α → β) → Term ρ α → Term ρ β

University of Oxford — Ralf Hinze 22-42

slide-24
SLIDE 24

The Lifting Lemma WG2.8

Semantics: variables

data Env :: (∗ → ∗) → (∗ → ∗) where Empty :: Env ι () Push :: Env ι ρ → ι α → Env ι (ρ, α) We write Empty as and Push η u as η, u. acc :: Ix ρ α → Env ι ρ → ι α acc Zero η, v = v acc (Succ n) η, v = acc n η

University of Oxford — Ralf Hinze 23-42

slide-25
SLIDE 25

The Lifting Lemma WG2.8

Semantics: terms

I :: (Idiom ι) ⇒ Term ρ α → Env ι ρ → ι α ICon vη = pure v IVar nη = acc n η IApp e1 e2η = Ie1η ⋄ Ie2η

University of Oxford — Ralf Hinze 24-42

slide-26
SLIDE 26

The Lifting Lemma WG2.8

What about abstraction?

University of Oxford — Ralf Hinze 25-42

slide-27
SLIDE 27

The Lifting Lemma WG2.8

Syntax

data Term :: ∗ → ∗ → ∗where Con :: α → Term ρ α Var :: Ix ρ α → Term ρ α App :: Term ρ (α → β) → Term ρ α → Term ρ β Abs :: Term (ρ, α) β → Term ρ (α → β)

University of Oxford — Ralf Hinze 26-42

slide-28
SLIDE 28

The Lifting Lemma WG2.8

An idiom is very similar to an applicative structure.

University of Oxford — Ralf Hinze 27-42

slide-29
SLIDE 29

The Lifting Lemma WG2.8

Semantics

I :: (Idiom ι) ⇒ Term ρ α → Env ι ρ → ι α ICon vη = pure v IVar nη = acc n η IApp e1 e2η = Ie1η ⋄ Ie2η IAbs eη = the unique function f such that ∀v . f ⋄ v = Ieη, v

University of Oxford — Ralf Hinze 28-42

slide-30
SLIDE 30

The Lifting Lemma WG2.8

Uniqueness?

Extensionality: (∀u . f ⋄ u = g ⋄ u) = ⇒ f = g

University of Oxford — Ralf Hinze 29-42

slide-31
SLIDE 31

The Lifting Lemma WG2.8

Existence?

Combinatory model condition: pure K ⋄ u ⋄ v = u pure S ⋄ u ⋄ v ⋄ w = (u ⋄ w) ⋄ (v ⋄ w) Ensures that ι has enough points.

University of Oxford — Ralf Hinze 30-42

slide-32
SLIDE 32

The Lifting Lemma WG2.8

Idiomatic interpretation specialised to the identity idiom:

  • :: Term ρ α → Env Id ρ → α

Con vη = v Var nη = acc n η App e1 e2η = (e1η) (e2η) Abs eη = λv → eη, v

University of Oxford — Ralf Hinze 31-42

slide-33
SLIDE 33

The Lifting Lemma WG2.8

. . . written in a pointfree style:

  • :: Term ρ α → Env Id ρ → α

Con v = K v Var n = acc n App e1 e2 = S e1 e2 Abs e = curry e

University of Oxford — Ralf Hinze 32-42

slide-34
SLIDE 34

The Lifting Lemma WG2.8

The Lifting Lemma

University of Oxford — Ralf Hinze 33-42

slide-35
SLIDE 35

The Lifting Lemma WG2.8

The lifting lemma

Ie = pure e

University of Oxford — Ralf Hinze 34-42

slide-36
SLIDE 36

The Lifting Lemma WG2.8

pure (+) ⋄ (pure (∗) ⋄ u ⋄ w) ⋄ (pure (∗) ⋄ v ⋄ w) = { definition of I } IAbs (Abs (Abs (2 ∗ 0 + 1 ∗ 0))) ⋄ u ⋄ v ⋄ w = { lifting lemma } pure Abs (Abs (Abs (2 ∗ 0 + 1 ∗ 0))) ⋄ u ⋄ v ⋄ w = { definition of } pure (λx y z → x ∗ z + y ∗ z) ⋄ u ⋄ v ⋄ w = { arithmetic } pure (λx y z → (x + y) ∗ z) ⋄ u ⋄ v ⋄ w = { definition of } pure Abs (Abs (Abs ((2 + 1) ∗ 0))) ⋄ u ⋄ v ⋄ w = { lifting lemma } IAbs (Abs (Abs ((2 + 1) ∗ 0))) ⋄ u ⋄ v ⋄ w = { definition of I } pure (∗) ⋄ (pure (+) u v) ⋄ w

University of Oxford — Ralf Hinze 35-42

slide-37
SLIDE 37

The Lifting Lemma WG2.8

The lifting lemma, general form

Ieη = pure e ⋄ zip η zip :: (Idiom ι) ⇒ Env ι ρ → ι (Env Id ρ) zip = pure zip η, v = pure , ⋄ η ⋄ v

University of Oxford — Ralf Hinze 36-42

slide-38
SLIDE 38

The Lifting Lemma WG2.8

Proof: Case e = Con v:

pure Con v ⋄ zip η = { definition of } pure (K v) ⋄ zip η = { idiom homomorphism } pure K ⋄ pure v ⋄ zip η = { combinatory model condition I } pure v = { definition of I } ICon vη

University of Oxford — Ralf Hinze 37-42

slide-39
SLIDE 39

The Lifting Lemma WG2.8

Proof: Case e = Var n:

pure Var n ⋄ zip η = { definition of } pure (acc n) ⋄ zip η = { Lemma: pure (acc n) ⋄ zip η = acc n η } acc n η = { definition of I } IVar nη

University of Oxford — Ralf Hinze 38-42

slide-40
SLIDE 40

The Lifting Lemma WG2.8

Proof: Case e = App e1 e2:

pure App e1 e2 ⋄ zip η = { definition of } pure (S e1 e2) ⋄ zip η = { idiom homomorphism } pure S ⋄ pure e1 ⋄ pure e2 ⋄ zip η = { combinatory model condition II } (pure e1 ⋄ zip η) ⋄ (pure e2 ⋄ zip η) = { ex hypothesi } Ie1η ⋄ Ie2η = { definition of I } IApp e1 e2η

University of Oxford — Ralf Hinze 39-42

slide-41
SLIDE 41

The Lifting Lemma WG2.8

Proof: Case e = Abs e:

pure Abs e ⋄ zip η = { definition of } pure (curry e) ⋄ zip η = { proof obligation (see next slide) } f = { definition of I } IAbs eη

University of Oxford — Ralf Hinze 40-42

slide-42
SLIDE 42

The Lifting Lemma WG2.8

Proof obligation

pure (curry e) ⋄ zip η = f ⇐ ⇒ { extensionality } pure (curry e) ⋄ zip η ⋄ v = f ⋄ v ⇐ ⇒ { definition of f } pure (curry e) ⋄ zip η ⋄ v = Ieη, v ⇐ ⇒ { curry-lemma (see above) } pure e ⋄ (zip η ⋆ v) = Ieη, v ⇐ ⇒ { definition of zip } pure e ⋄ (zip η, v) = Ieη, v ⇐ ⇒ { ex hypothesi } True

University of Oxford — Ralf Hinze 41-42

slide-43
SLIDE 43

The Lifting Lemma WG2.8

What about . . .

  • τ→: √;
  • Set: , but λI-calculus;
  • Vector: √;
  • Maybe: , but λI-calculus;
  • IO: , but NF Lemma;
  • Stream: √.

University of Oxford — Ralf Hinze 42-42