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
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
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
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
Corecursion on Streams In the λμ -C a lculus
Corecursion on Streams In the λμ -C a lculus • Functional, direct-style
Corecursion on Streams In the λμ -C a lculus • Functional, direct-style • Don’t mention continuations directly; implicit “evaluation contexts”
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
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
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
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
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
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
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
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 β
Examples of Corecursion In a n Abstr a ct M a chine
Examples of Corecursion In a n Abstr a ct M a chine count x = x , x + 1, x + 2, x + 3…
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
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 …
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 _
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 …
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 | | α ⟩
Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN
Corecursion vs Coiteration Expressiveness vs Cost; CBV vs CBN coiter { Tail → γ . F } with V := corec { Tail _ → γ . F } with V Head α → E Head α → E
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 ⟩
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
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 :
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
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
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
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
(Co)Inductive Reasoning
Finite Induction By Inversion on the Input
Finite Induction By Inversion on the Input Γ , x : Bool ⊢ Φ ( x )
Finite Induction By Inversion on the Input Γ , x : Bool ⊢ Φ ( x )
Finite Induction By Inversion on the Input Γ ⊢ Φ ( True ) Γ , x : Bool ⊢ Φ ( x )
Finite Induction By Inversion on the Input Γ ⊢ Φ ( True ) Γ ⊢ Φ ( False ) Γ , x : Bool ⊢ Φ ( x )
Finite Induction By Inversion on the Input Γ ⊢ Φ ( True ) Γ ⊢ Φ ( False ) Γ , x : Bool ⊢ Φ ( x )
In f inite Induction By Inversion on the Input
In f inite Induction By Inversion on the Input Γ , x : Nat ⊢ Φ ( x )
In f inite Induction By Inversion on the Input Γ , x : Nat ⊢ Φ ( x )
In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ , x : Nat ⊢ Φ ( x )
In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ ⊢ Φ (1) Γ , x : Nat ⊢ Φ ( x )
In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ ⊢ Φ (1) Γ ⊢ Φ (2) Γ , x : Nat ⊢ Φ ( x )
In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ ⊢ Φ (1) Γ ⊢ Φ (2) … Γ , x : Nat ⊢ Φ ( x )
In f inite Induction By Inversion on the Input Γ ⊢ Φ (0) Γ ⊢ Φ (1) Γ ⊢ Φ (2) … Γ , x : Nat ⊢ Φ ( x ) ?
An Induction Principle B a sed on Inform a tion Flow
An Induction Principle B a sed on Inform a tion Flow Γ , x : Nat ⊢ Φ ( x )
An Induction Principle B a sed on Inform a tion Flow Γ ⊢ Φ ( Zero ) Γ , x : Nat ⊢ Φ ( x )
An Induction Principle B a sed on Inform a tion Flow Γ ⊢ Φ ( Zero ) Γ , x : Nat , Φ ( x ) ⊢ Φ ( Succ x ) Γ , x : Nat ⊢ Φ ( x )
Recommend
More recommend