a computational understanding of classical co recursion
play

A Computational Understanding of Classical (Co)Recursion P a ul - PowerPoint PPT Presentation

A Computational Understanding of Classical (Co)Recursion P a ul Downen a nd Zen a M. Ariol a PPDP 2020, September 8 10 Topic Topic Both programs and proofs with loops Topic Both programs and proofs with loops (Co)Recursion and


  1. Corecursion on Streams In a n Abstr a ct M a chine • Generalize Nat ⊥ to Stream A • In fi nite stream of computations that return an A

  2. Corecursion on Streams In a n Abstr a ct M a chine • Generalize Nat ⊥ to Stream A • In fi nite stream of computations that return an A • Head : Stream A → A Tail : Stream A → Stream A and

  3. Corecursion on Streams In a n Abstr a ct M a chine • Generalize Nat ⊥ to Stream A • In fi nite stream of computations that return an A • Head : Stream A → A Tail : Stream A → Stream A and Nat ⊥ Value: corec { Run → E ∣ Tail β → γ . F } with V Nat ⊥ Conts.: Run Tail E

  4. Corecursion on Streams In a n Abstr a ct M a chine • Generalize Nat ⊥ to Stream A • In fi nite stream of computations that return an A • Head : Stream A → A Tail : Stream A → Stream A and Nat ⊥ Value: corec { Run → E ∣ Tail β → γ . F } with V Nat ⊥ Conts.: Run Tail E corec { Head α → E ∣ Tail β → γ . F } with V Stream A Value: Stream A Conts.: Head E Tail E

  5. Corecursion on Streams In the λμ -C a lculus

  6. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style

  7. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts”

  8. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts” • Contexts named by ⟨ M | | α ⟩ μα . J ; invoked by jumps

  9. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts” • Contexts named by ⟨ M | | α ⟩ μα . J ; invoked by jumps Destructors: Head M : A Tail M : Stream A when M : Stream A

  10. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts” • Contexts named by ⟨ M | | α ⟩ μα . J ; invoked by jumps Destructors: Head M : A Tail M : Stream A when M : Stream A Generator: corec { Head → x . N ∣ Tail β → y . P } with M

  11. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts” • Contexts named by ⟨ M | | α ⟩ μα . J ; invoked by jumps Destructors: Head M : A Tail M : Stream A when M : Stream A Generator: corec { Head → x . N ∣ Tail β → y . P } with M • Accumulator M , named and in the branches x y

  12. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts” • Contexts named by ⟨ M | | α ⟩ μα . J ; invoked by jumps Destructors: Head M : A Tail M : Stream A when M : Stream A Generator: corec { Head → x . N ∣ Tail β → y . P } with M • Accumulator M , named and in the branches x y • Head branch: computes fi rst element from current accumulator N x

  13. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts” • Contexts named by ⟨ M | | α ⟩ μα . J ; invoked by jumps Destructors: Head M : A Tail M : Stream A when M : Stream A Generator: corec { Head → x . N ∣ Tail β → y . P } with M • Accumulator M , named and in the branches x y • Head branch: computes fi rst element from current accumulator N x • Tail branch: computes one of two options P

  14. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts” • Contexts named by ⟨ M | | α ⟩ μα . J ; invoked by jumps Destructors: Head M : A Tail M : Stream A when M : Stream A Generator: corec { Head → x . N ∣ Tail β → y . P } with M • Accumulator M , named and in the branches x y • Head branch: computes fi rst element from current accumulator N x • Tail branch: computes one of two options P • Continue: return a new accumulator value from current used for next corecursive loop y

  15. Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts” • Contexts named by ⟨ M | | α ⟩ μα . J ; invoked by jumps Destructors: Head M : A Tail M : Stream A when M : Stream A Generator: corec { Head → x . N ∣ Tail β → y . P } with M • Accumulator M , named and in the branches x y • Head branch: computes fi rst element from current accumulator N x • Tail branch: computes one of two options P • Continue: return a new accumulator value from current used for next corecursive loop y • End: send a fully-formed stream to context ; this corecursive loop is fi nished β

  16. Examples of Corecursion In a n Abstr a ct M a chine

  17. Examples of Corecursion In a n Abstr a ct M a chine count x = x , x + 1, x + 2, x + 3…

  18. Examples of Corecursion In a n Abstr a ct M a chine count x = x , x + 1, x + 2, x + 3… count = λ x . corec { Head → y . y ∣ Tail _ → z . Succ z } with x

  19. Examples of Corecursion In a n Abstr a ct M a chine count x = x , x + 1, x + 2, x + 3… count = λ x . corec { Head → y . y ∣ Tail _ → z . Succ z } with x scons x ( y 0 , y 1 , y 2 …) = x , y 0 , y 1 , y 2 …

  20. Examples of Corecursion In a n Abstr a ct M a chine count x = x , x + 1, x + 2, x + 3… count = λ x . corec { Head → y . y ∣ Tail _ → z . Succ z } with x scons x ( y 0 , y 1 , y 2 …) = x , y 0 , y 1 , y 2 … scons = λ x . λ ys . corec { Head → _ . x ∣ Tail α → _ . μδ . ⟨ ys | | α ⟩ } with _

  21. Examples of Corecursion In a n Abstr a ct M a chine count x = x , x + 1, x + 2, x + 3… count = λ x . corec { Head → y . y ∣ Tail _ → z . Succ z } with x scons x ( y 0 , y 1 , y 2 …) = x , y 0 , y 1 , y 2 … scons = λ x . λ ys . corec { Head → _ . x ∣ Tail α → _ . μδ . ⟨ ys | | α ⟩ } with _ app [ x 0 , x 1 , …, x n ] ( y 0 , y 1 , y 2 …) = x 0 , x 1 , …, x n , y 0 , y 1 , y 2 …

  22. Examples of Corecursion In a n Abstr a ct M a chine count x = x , x + 1, x + 2, x + 3… count = λ x . corec { Head → y . y ∣ Tail _ → z . Succ z } with x scons x ( y 0 , y 1 , y 2 …) = x , y 0 , y 1 , y 2 … scons = λ x . λ ys . corec { Head → _ . x ∣ Tail α → _ . μδ . ⟨ ys | | α ⟩ } with _ app [ x 0 , x 1 , …, x n ] ( y 0 , y 1 , y 2 …) = x 0 , x 1 , …, x n , y 0 , y 1 , y 2 … Head → Cons x xs . x Tail _ → Cons x xs . xs app = λ xs . λ ys . corec with xs Head → Nil . Head ys Tail α → Nil . μδ . ⟨ Tail ys | | α ⟩

  23. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN

  24. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E

  25. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E ⟨ Left V | | [ E , F ] ⟩ ↦ ⟨ V | | E ⟩ ⟨ Right V | | [ E , F ] ⟩ ↦ ⟨ V | | F ⟩

  26. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E ⟨ Left V | | [ E , F ] ⟩ ↦ ⟨ V | | E ⟩ ⟨ Right V | | [ E , F ] ⟩ ↦ ⟨ V | | F ⟩ corec { Tail β → γ . F } with V := coiter { Tail → [ β , γ ] . [ Tail β , F ] } with Right V Head α → [ Head α , E ] Head α → E

  27. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E ⟨ Left V | | [ E , F ] ⟩ ↦ ⟨ V | | E ⟩ ⟨ Right V | | [ E , F ] ⟩ ↦ ⟨ V | | F ⟩ corec { Tail β → γ . F } with V := coiter { Tail → [ β , γ ] . [ Tail β , F ] } with Right V Head α → [ Head α , E ] Head α → E • (Amortized) overhead cost; consider scons x ys :

  28. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E ⟨ Left V | | [ E , F ] ⟩ ↦ ⟨ V | | E ⟩ ⟨ Right V | | [ E , F ] ⟩ ↦ ⟨ V | | F ⟩ corec { Tail β → γ . F } with V := coiter { Tail → [ β , γ ] . [ Tail β , F ] } with Right V Head α → [ Head α , E ] Head α → E • (Amortized) overhead cost; consider scons x ys : • Native Head ( Tail n ys ) corec Head ( Tail n +1 ( scons x ys )) : adds O (1) overhead to cost of

  29. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E ⟨ Left V | | [ E , F ] ⟩ ↦ ⟨ V | | E ⟩ ⟨ Right V | | [ E , F ] ⟩ ↦ ⟨ V | | F ⟩ corec { Tail β → γ . F } with V := coiter { Tail → [ β , γ ] . [ Tail β , F ] } with Right V Head α → [ Head α , E ] Head α → E • (Amortized) overhead cost; consider scons x ys : • Native Head ( Tail n ys ) corec Head ( Tail n +1 ( scons x ys )) : adds O (1) overhead to cost of • Encoded Head ( Tail n ys ) corec Head ( Tail n +1 ( scons x ys )) : adds O ( n ) overhead to cost of

  30. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E ⟨ Left V | | [ E , F ] ⟩ ↦ ⟨ V | | E ⟩ ⟨ Right V | | [ E , F ] ⟩ ↦ ⟨ V | | F ⟩ corec { Tail β → γ . F } with V := coiter { Tail → [ β , γ ] . [ Tail β , F ] } with Right V Head α → [ Head α , E ] Head α → E • (Amortized) overhead cost; consider scons x ys : • Native Head ( Tail n ys ) corec Head ( Tail n +1 ( scons x ys )) : adds O (1) overhead to cost of • Encoded Head ( Tail n ys ) corec Head ( Tail n +1 ( scons x ys )) : adds O ( n ) overhead to cost of • Native CBN corec corec has same overhead as encoding; Native CBV more e ffi cient

  31. Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E ⟨ Left V | | [ E , F ] ⟩ ↦ ⟨ V | | E ⟩ ⟨ Right V | | [ E , F ] ⟩ ↦ ⟨ V | | F ⟩ corec { Tail β → γ . F } with V := coiter { Tail → [ β , γ ] . [ Tail β , F ] } with Right V Head α → [ Head α , E ] Head α → E • (Amortized) overhead cost; consider scons x ys : • Native Head ( Tail n ys ) corec Head ( Tail n +1 ( scons x ys )) : adds O (1) overhead to cost of • Encoded Head ( Tail n ys ) corec Head ( Tail n +1 ( scons x ys )) : adds O ( n ) overhead to cost of • Native CBN corec corec has same overhead as encoding; Native CBV more e ffi cient • Corollary by duality of rec iter and

  32. (Co)Inductive Reasoning

  33. Finite Induction By Inversion on the Input

  34. Finite Induction By Inversion on the Input Γ , x : Bool ⊢ Φ ( x )

  35. Finite Induction By Inversion on the Input Γ , x : Bool ⊢ Φ ( x )

  36. Finite Induction By Inversion on the Input Γ ⊢ Φ ( True ) Γ , x : Bool ⊢ Φ ( x )

  37. Finite Induction By Inversion on the Input Γ ⊢ Φ ( True ) Γ ⊢ Φ ( False ) Γ , x : Bool ⊢ Φ ( x )

  38. Finite Induction By Inversion on the Input Γ ⊢ Φ ( True ) Γ ⊢ Φ ( False ) Γ , x : Bool ⊢ Φ ( x )

  39. In f inite Induction By Inversion on the Input

  40. In f inite Induction By Inversion on the Input Γ , x : Nat ⊢ Φ ( x )

  41. In f inite Induction By Inversion on the Input Γ , x : Nat ⊢ Φ ( x )

  42. In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ , x : Nat ⊢ Φ ( x )

  43. In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ ⊢ Φ (1) Γ , x : Nat ⊢ Φ ( x )

  44. In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ ⊢ Φ (1) Γ ⊢ Φ (2) Γ , x : Nat ⊢ Φ ( x )

  45. In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ ⊢ Φ (1) Γ ⊢ Φ (2) … Γ , x : Nat ⊢ Φ ( x )

  46. In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ ⊢ Φ (1) Γ ⊢ Φ (2) … Γ , x : Nat ⊢ Φ ( x ) ?

  47. An Induction Principle B a sed on Inform a tion Flow

  48. An Induction Principle B a sed on Inform a tion Flow Γ , x : Nat ⊢ Φ ( x )

  49. An Induction Principle B a sed on Inform a tion Flow Γ ⊢ Φ ( Zero ) Γ , x : Nat ⊢ Φ ( x )

  50. An Induction Principle B a sed on Inform a tion Flow Γ ⊢ Φ ( Zero ) Γ , x : Nat , Φ ( x ) ⊢ Φ ( Succ x ) Γ , x : Nat ⊢ Φ ( x )

Recommend


More recommend