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
The Lifting Lemma WG2.8 Mathematicians do it . . . ( f + g )( x ) = f ( x ) + g ( x ) University of Oxford — Ralf Hinze 2-42
The Lifting Lemma WG2.8 . . . over and . . . A + B = { a + b | a ∈ A, b ∈ B } University of Oxford — Ralf Hinze 3-42
The Lifting Lemma WG2.8 . . . and over again. ( x 1 , x 2 , x 3 ) + ( y 1 , y 2 , y 3 ) = ( x 1 + y 1 , x 2 + y 2 , x 3 + y 3 ) University of Oxford — Ralf Hinze 4-42
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
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
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
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
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
The Lifting Lemma WG2.8 ( x + y ) + z = x + ( y + z ) University of Oxford — Ralf Hinze 10-42
The Lifting Lemma WG2.8 x + y = y + x University of Oxford — Ralf Hinze 11-42
The Lifting Lemma WG2.8 x ∗ 0 = 0 University of Oxford — Ralf Hinze 12-42
The Lifting Lemma WG2.8 Idioms University of Oxford — Ralf Hinze 13-42
The Lifting Lemma WG2.8 Idioms aka applicative functors class Idiom ι where pure :: α → ι α ( ⋄ ) :: ι ( α → β ) → ( ι α → ι β ) University of Oxford — Ralf Hinze 14-42
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
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
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
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
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
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
The Lifting Lemma WG2.8 The Idiomatic Calculus University of Oxford — Ralf Hinze 20-42
The Lifting Lemma WG2.8 Syntax: variables data Ix :: ∗ → ∗ → ∗ where Zero :: Ix ( ρ, α ) α Succ :: Ix ρ β → Ix ( ρ, α ) β University of Oxford — Ralf Hinze 21-42
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
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
The Lifting Lemma WG2.8 Semantics: terms I �� :: ( Idiom ι ) ⇒ Term ρ α → Env ι ρ → ι α I � Con v � η = pure v I � Var n � η = acc n η I � App e 1 e 2 � η = I � e 1 � η ⋄ I � e 2 � η University of Oxford — Ralf Hinze 24-42
The Lifting Lemma WG2.8 What about abstraction? University of Oxford — Ralf Hinze 25-42
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
The Lifting Lemma WG2.8 An idiom is very similar to an applicative structure . University of Oxford — Ralf Hinze 27-42
The Lifting Lemma WG2.8 Semantics I �� :: ( Idiom ι ) ⇒ Term ρ α → Env ι ρ → ι α I � Con v � η = pure v I � Var n � η = acc n η I � App e 1 e 2 � η = I � e 1 � η ⋄ I � e 2 � η I � Abs e � η = the unique function f such that ∀ v . f ⋄ v = I � e � � η, v � University of Oxford — Ralf Hinze 28-42
The Lifting Lemma WG2.8 Uniqueness? Extensionality: ( ∀ u . f ⋄ u = g ⋄ u ) = ⇒ f = g University of Oxford — Ralf Hinze 29-42
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
The Lifting Lemma WG2.8 Idiomatic interpretation specialised to the identity idiom: :: Term ρ α → Env Id ρ → α �� � Con v � η = v � Var n � η = acc n η � App e 1 e 2 � η = ( � e 1 � η ) ( � e 2 � η ) � Abs e � η = λ v → � e � � η, v � University of Oxford — Ralf Hinze 31-42
The Lifting Lemma WG2.8 . . . written in a pointfree style: :: Term ρ α → Env Id ρ → α �� � Con v � = K v � Var n � = acc n � App e 1 e 2 � = S � e 1 � � e 2 � � Abs e � = curry � e � University of Oxford — Ralf Hinze 32-42
The Lifting Lemma WG2.8 The Lifting Lemma University of Oxford — Ralf Hinze 33-42
The Lifting Lemma WG2.8 The lifting lemma I � e � = pure � e � University of Oxford — Ralf Hinze 34-42
The Lifting Lemma WG2.8 pure (+) ⋄ ( pure ( ∗ ) ⋄ u ⋄ w ) ⋄ ( pure ( ∗ ) ⋄ v ⋄ w ) = { definition of I } I � Abs ( 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 } I � Abs ( Abs ( Abs (( 2 + 1 ) ∗ 0 ))) � ⋄ u ⋄ v ⋄ w = { definition of I } pure ( ∗ ) ⋄ ( pure (+) u v ) ⋄ w University of Oxford — Ralf Hinze 35-42
The Lifting Lemma WG2.8 The lifting lemma, general form I � e � η = pure � e � ⋄ zip η zip :: ( Idiom ι ) ⇒ Env ι ρ → ι ( Env Id ρ ) zip �� = pure �� zip � η, v � = pure � , � ⋄ η ⋄ v University of Oxford — Ralf Hinze 36-42
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 } = I � Con v � η University of Oxford — Ralf Hinze 37-42
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 } I � Var n � η University of Oxford — Ralf Hinze 38-42
The Lifting Lemma WG2.8 Proof: Case e = App e 1 e 2 : pure � App e 1 e 2 � ⋄ zip η = { definition of �� } pure ( S � e 1 � � e 2 � ) ⋄ zip η = { idiom homomorphism } pure S ⋄ pure � e 1 � ⋄ pure � e 2 � ⋄ zip η = { combinatory model condition II } ( pure � e 1 � ⋄ zip η ) ⋄ ( pure � e 2 � ⋄ zip η ) = { ex hypothesi } I � e 1 � η ⋄ I � e 2 � η = { definition of I } I � App e 1 e 2 � η University of Oxford — Ralf Hinze 39-42
Recommend
More recommend