multi level languages are generalized arrows
play

Multi-Level Languages are Generalized Arrows Adam Megacz - PowerPoint PPT Presentation

Multi-Level Languages are Generalized Arrows Adam Megacz 12-Apr-2011 1/38 Lambda Calculus Review := Int | -> | . . . e := x | e e | x . e [Var] x: x: , x: 1 e: 2 [Lam] ( x . e ) : 1 -> 2


  1. Multi-Level Languages are Generalized Arrows Adam Megacz 12-Apr-2011 1/38

  2. Lambda Calculus Review τ := Int | τ -> τ | . . . e := x | e e | λ x . e [Var] x: τ ⊢ x: τ Γ , x: τ 1 ⊢ e: τ 2 [Lam] Γ ⊢ ( λ x . e ) : τ 1 -> τ 2 Γ 1 ⊢ e 1 : τ 1 Γ 2 ⊢ e 2 : τ 1 -> τ 2 [App] Γ 1 , Γ 2 ⊢ e 2 e 1 : τ 2 3/38

  3. Flattening, in Detail What follows is the simplified translation , which works only when higher-order functions are not used. Specifically, the context (area to the left of the turnstile) may not contain any variables with -> ’s in their types. 5/38

  4. Flattening, in Detail Let’s try an example. We’re going to flatten this expression: subtract_second_from_third :: Int -> (Int -> (Int -> Int)) subtract_second_from_third x y z = z - y But first, we need to desugar it. The expression above is really just syntactic sugar for this: subtract_second_from_third = \x -> \y -> \z -> (-) z y Also, note that this expression has (-) as a free variable, with the following type: (-) :: Int -> Int -> Int We’ll come back to this. 7/38

  5. Flattening, in Detail Let’s try an example. We’re going to flatten this expression: subtract_second_from_third :: Int -> (Int -> (Int -> Int)) subtract_second_from_third x y z = z - y But first, we need to desugar it. The expression above is really just syntactic sugar for this: subtract_second_from_third = \x -> \y -> \z -> (-) z y Also, note that this expression has (-) as a free variable, with the following type: (-) :: Int -> Int -> Int We’ll come back to this. 7/38

  6. Flattening, in Detail Let’s try an example. We’re going to flatten this expression: subtract_second_from_third :: Int -> (Int -> (Int -> Int)) subtract_second_from_third x y z = z - y But first, we need to desugar it. The expression above is really just syntactic sugar for this: subtract_second_from_third = \x -> \y -> \z -> (-) z y Also, note that this expression has (-) as a free variable, with the following type: (-) :: Int -> Int -> Int We’ll come back to this. 7/38

  7. Flattening, in Detail The (simplified) flattening process involves six steps: 1. Construct the proof that the expression is well-typed 2. Skolemize the proof 3. Make contraction, and exchange explicit in the proof 4. Make left and right expansion explicit in the proof 5. Make weakening explicit in the proof 6. Walk the proof In practice, steps 2-5 are performed simultaneously as one big step. In these slides they are presented as separate steps in an order which keeps each of the proofs small enough to fit on a slide. 9/38

  8. Flattening, in Detail The (simplified) flattening process involves six steps: 1. Construct the proof that the expression is well-typed 2. Skolemize the proof 3. Make contraction, and exchange explicit in the proof 4. Make left and right expansion explicit in the proof 5. Make weakening explicit in the proof 6. Walk the proof In practice, steps 2-5 are performed simultaneously as one big step. In these slides they are presented as separate steps in an order which keeps each of the proofs small enough to fit on a slide. 9/38

  9. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  10. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  11. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  12. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  13. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  14. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  15. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  16. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  17. Step 1: Construct the Proof λ x λ y subtract_first_from_third = \x -> λ z \y -> app \z -> ((-) z) y app y z (-) [Var] z:Int ⊢ z:Int ⊢ (-) : Int->Int->Int [Var] [App] y:Int ⊢ y:Int z : Int ⊢ (-) z:Int->Int [App] x:Int , y:Int , z:Int ⊢ (-) z y:Int [Lam] x:Int , y:Int ⊢ \z -> (-) z y:Int->Int [Lam] x:Int ⊢ \y -> \z -> (-) z y:Int->(Int->Int) [Lam] ⊢ \x -> \y -> \z -> (-) z y:Int->(Int->(Int->Int)) 11/38

  18. Step 2: Skolemize the Proof Now, we skolemize the proof to eliminate function types a->b . Wherever an expression of function type appears to the right of the turnstile, like this: Γ ⊢ e: α -> β 12/38

  19. Step 2: Skolemize the Proof Now, we skolemize the proof to eliminate function types a->b . Wherever an expression of function type appears to the right of the turnstile, like this: Γ ⊢ e: α -> β we will add a new skolem variable with a fresh name to the left of the turnstile and apply the expression to it, like this: a: α, Γ ⊢ e a: β 12/38

  20. Step 2: Skolemize the Proof Now, we skolemize the proof to eliminate function types a->b . Wherever an expression of function type appears to the right of the turnstile, like this: Γ ⊢ e: α -> β we will add a new skolem variable with a fresh name to the left of the turnstile and apply the expression to it, like this: a: α, Γ ⊢ e a: β Repeated application of the procedure above will leave us with a proof which has no -> ’s to the right of the turnstile. 12/38

  21. Skolemizing Lam y: α, Γ ⊢ e: β [Lam] Γ ⊢ ( λ y . e ) : α -> β 13/38

  22. Skolemizing Lam y: α, Γ ⊢ e: β [Lam] Γ ⊢ ( λ y . e ) : α -> β Notice what happens to the Lam rule when we skolemize it: y: α, Γ ⊢ e: β [Skolemized-Lam] a: α, Γ ⊢ ( λ y . e ) a: β 13/38

  23. Skolemizing Lam y: α, Γ ⊢ e: β [Lam] Γ ⊢ ( λ y . e ) : α -> β Notice what happens to the Lam rule when we skolemize it: y: α, Γ ⊢ e: β [Skolemized-Lam] a: α, Γ ⊢ ( λ y . e ) a: β Because ( λ y . e ) a reduces via β -reduction to e [ y := a ], Skolemized-Lam is nothing more than variable renaming. We can eliminate it entirely by changing the names of the variables in the proof tree above it: . . . . . . . [ y:=a ] . . ⇒ ⇒ y: α, Γ ⊢ e: β y: α, Γ ⊢ e: β a: α, Γ ⊢ e [ y:=a ] : β Γ ⊢ ( λ y . e ) : α -> β a: α, Γ ⊢ ( λ y . e ) a: β 13/38

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend