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

a computational understanding of classical co recursion
SMART_READER_LITE
LIVE PREVIEW

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


slide-1
SLIDE 1

PPDP 2020, September 8–10

A Computational Understanding

  • f Classical (Co)Recursion

Paul Downen and Zena M. Ariola

slide-2
SLIDE 2

Topic

slide-3
SLIDE 3

Topic

  • Both programs and proofs with loops
slide-4
SLIDE 4

Topic

  • Both programs and proofs with loops
  • (Co)Recursion and (Co)Induction
slide-5
SLIDE 5

Topic

  • Both programs and proofs with loops
  • (Co)Recursion and (Co)Induction
  • “Terminating” or “Productive”
slide-6
SLIDE 6

Topic

  • Both programs and proofs with loops
  • (Co)Recursion and (Co)Induction
  • “Terminating” or “Productive”
  • Extend to non-termination, effects
slide-7
SLIDE 7

Methodology

slide-8
SLIDE 8

Methodology

  • Duality
slide-9
SLIDE 9

Methodology

  • Duality
  • Computational
slide-10
SLIDE 10

Methodology

  • Duality
  • Computational
  • Curry-Howard
slide-11
SLIDE 11

Methodology

  • Duality
  • Computational
  • Curry-Howard
  • sequent calculus as abstract machines
slide-12
SLIDE 12

Methodology

  • Duality
  • Computational
  • Curry-Howard
  • sequent calculus as abstract machines
  • Classical
slide-13
SLIDE 13

Recursive Programs

slide-14
SLIDE 14

Recursion on Natural Numbers

In System T

slide-15
SLIDE 15

Recursion on Natural Numbers

  • Simply-typed -calculus plus inductive

λ Nat = Zero ∣ Succ Nat

In System T

slide-16
SLIDE 16

Recursion on Natural Numbers

  • Simply-typed -calculus plus inductive

λ Nat = Zero ∣ Succ Nat recA

Nat : Nat → A → (Nat → A → A) → A

In System T

slide-17
SLIDE 17

Recursion on Natural Numbers

  • Simply-typed -calculus plus inductive

λ Nat = Zero ∣ Succ Nat recA

Nat : Nat → A → (Nat → A → A) → A

rec M as {Zero → N ∣ Succ x → y . P}

where M, x : Nat; N and P, y : A

In System T

slide-18
SLIDE 18

Recursion on Natural Numbers

  • Simply-typed -calculus plus inductive

λ Nat = Zero ∣ Succ Nat recA

Nat : Nat → A → (Nat → A → A) → A

rec M as {Zero → N ∣ Succ x → y . P}

where M, x : Nat; N and P, y : A

case M of Zero → N Succ x → P := rec M of Zero → M Succ x → _ . P

In System T

slide-19
SLIDE 19

Recursion on Natural Numbers

  • Simply-typed -calculus plus inductive

λ Nat = Zero ∣ Succ Nat recA

Nat : Nat → A → (Nat → A → A) → A

rec M as {Zero → N ∣ Succ x → y . P}

where M, x : Nat; N and P, y : A

case M of Zero → N Succ x → P := rec M of Zero → M Succ x → _ . P iter M as Zero → N Succ → y . P := rec M as Zero → N Succ _ → y . P

In System T

slide-20
SLIDE 20

Examples of Recursion

In System T

slide-21
SLIDE 21

Examples of Recursion

plus Zero y = y plus (Succ x′ ) y = Succ (plus x′ y)

In System T

slide-22
SLIDE 22

Examples of Recursion

plus Zero y = y plus (Succ x′ ) y = Succ (plus x′ y)

plus = λx . λy . iter x as Zero → y Succ → z . Succ z

In System T

slide-23
SLIDE 23

Examples of Recursion

plus Zero y = y plus (Succ x′ ) y = Succ (plus x′ y)

plus = λx . λy . iter x as Zero → y Succ → z . Succ z

In System T

pred Zero = Zero pred (Succ x′ ) = x′

slide-24
SLIDE 24

Examples of Recursion

plus Zero y = y plus (Succ x′ ) y = Succ (plus x′ y)

plus = λx . λy . iter x as Zero → y Succ → z . Succ z

In System T

pred Zero = Zero pred (Succ x′ ) = x′ pred = λx . case x of Zero → Zero Succ x′ → x′

slide-25
SLIDE 25

Examples of Recursion

plus Zero y = y plus (Succ x′ ) y = Succ (plus x′ y)

plus = λx . λy . iter x as Zero → y Succ → z . Succ z

In System T

pred Zero = Zero pred (Succ x′ ) = x′ pred = λx . case x of Zero → Zero Succ x′ → x′

minus x Zero = x minus x (Succ y′ ) = pred (minus x y′ )

slide-26
SLIDE 26

Examples of Recursion

plus Zero y = y plus (Succ x′ ) y = Succ (plus x′ y)

plus = λx . λy . iter x as Zero → y Succ → z . Succ z

In System T

pred Zero = Zero pred (Succ x′ ) = x′ pred = λx . case x of Zero → Zero Succ x′ → x′

minus x Zero = x minus x (Succ y′ ) = pred (minus x y′ )

minus = λx . λy . iter y as Zero → x Succ → z . pred z

slide-27
SLIDE 27

Recursion vs Iteration

Expressiveness vs Cost

slide-28
SLIDE 28

Recursion vs Iteration

iter M as Zero → N Succ → y . P := rec M as Zero → N Succ _ → y . P

Expressiveness vs Cost

slide-29
SLIDE 29

Recursion vs Iteration

iter M as Zero → N Succ → y . P := rec M as Zero → N Succ _ → y . P rec M as Zero → N Succ x → y . P := snd(iter M as Zero → (Zero, N) Succ → (x, y) . (Succ x, P))

Expressiveness vs Cost

slide-30
SLIDE 30

Recursion vs Iteration

iter M as Zero → N Succ → y . P := rec M as Zero → N Succ _ → y . P rec M as Zero → N Succ x → y . P := snd(iter M as Zero → (Zero, N) Succ → (x, y) . (Succ x, P))

  • goes from

to time

pred (Succn Zero) O(1) O(n)

Expressiveness vs Cost

slide-31
SLIDE 31

Recursion vs Iteration

iter M as Zero → N Succ → y . P := rec M as Zero → N Succ _ → y . P rec M as Zero → N Succ x → y . P := snd(iter M as Zero → (Zero, N) Succ → (x, y) . (Succ x, P))

  • goes from

to time

pred (Succn Zero) O(1) O(n)

  • goes from

to

minus (Succn Zero) (Succm Zero) O(n) O(n2 + nm)

Expressiveness vs Cost

slide-32
SLIDE 32

Recursion vs Iteration

iter M as Zero → N Succ → y . P := rec M as Zero → N Succ _ → y . P rec M as Zero → N Succ x → y . P := snd(iter M as Zero → (Zero, N) Succ → (x, y) . (Succ x, P))

  • goes from

to time

pred (Succn Zero) O(1) O(n)

  • goes from

to

minus (Succn Zero) (Succm Zero) O(n) O(n2 + nm)

  • Native

has the same performance penalty as encoding in CBV

rec

Expressiveness vs Cost

slide-33
SLIDE 33

Recursion vs Iteration

iter M as Zero → N Succ → y . P := rec M as Zero → N Succ _ → y . P rec M as Zero → N Succ x → y . P := snd(iter M as Zero → (Zero, N) Succ → (x, y) . (Succ x, P))

  • goes from

to time

pred (Succn Zero) O(1) O(n)

  • goes from

to

minus (Succn Zero) (Succm Zero) O(n) O(n2 + nm)

  • Native

has the same performance penalty as encoding in CBV

rec

  • Recursive result always computed; full traversal is mandatory

Expressiveness vs Cost

slide-34
SLIDE 34

Recursion in an Abstract Machine

Building the Recursive Continuation

slide-35
SLIDE 35

Recursion in an Abstract Machine

Building the Recursive Continuation

⟨M| |E⟩

slide-36
SLIDE 36

Recursion in an Abstract Machine

Building the Recursive Continuation

⟨M N| |E⟩ ↦ ⟨M| |N ⋅ E⟩ ⟨λx . M| |N ⋅ E⟩ ↦ ⟨M[N/x]| |E⟩ ⟨M| |E⟩

slide-37
SLIDE 37

Recursion in an Abstract Machine

Building the Recursive Continuation

⟨M N| |E⟩ ↦ ⟨M| |N ⋅ E⟩ ⟨λx . M| |N ⋅ E⟩ ↦ ⟨M[N/x]| |E⟩

ralt := { Zero → N ∣ Succ x → y . P }

⟨M| |E⟩

slide-38
SLIDE 38

Recursion in an Abstract Machine

Building the Recursive Continuation

⟨M N| |E⟩ ↦ ⟨M| |N ⋅ E⟩ ⟨λx . M| |N ⋅ E⟩ ↦ ⟨M[N/x]| |E⟩

⟨rec M as ralt| |E⟩ ↦ ⟨M| |rec ralt with E⟩ ralt := { Zero → N ∣ Succ x → y . P }

⟨M| |E⟩

slide-39
SLIDE 39

Recursion in an Abstract Machine

Building the Recursive Continuation

⟨M N| |E⟩ ↦ ⟨M| |N ⋅ E⟩ ⟨λx . M| |N ⋅ E⟩ ↦ ⟨M[N/x]| |E⟩

⟨rec M as ralt| |E⟩ ↦ ⟨M| |rec ralt with E⟩ ⟨Zero| |rec ralt with E⟩ ↦ ⟨N| |E⟩ ralt := { Zero → N ∣ Succ x → y . P }

⟨M| |E⟩

slide-40
SLIDE 40

Recursion in an Abstract Machine

Building the Recursive Continuation

⟨M N| |E⟩ ↦ ⟨M| |N ⋅ E⟩ ⟨λx . M| |N ⋅ E⟩ ↦ ⟨M[N/x]| |E⟩

⟨rec M as ralt| |E⟩ ↦ ⟨M| |rec ralt with E⟩ ⟨Zero| |rec ralt with E⟩ ↦ ⟨N| |E⟩ ⟨Succ M| |rec ralt with E⟩ ↦ ⟨P[M/x, rec M as ralt/y]| |E⟩ ralt := { Zero → N ∣ Succ x → y . P }

⟨M| |E⟩

slide-41
SLIDE 41

Corecursive Programs

slide-42
SLIDE 42

What’s the Dual of Natural Numbers?

Nat⊥

slide-43
SLIDE 43

What’s the Dual of Natural Numbers?

  • is dual to

Zero : 1 → Nat Run : Nat⊥ → ⊥

Nat⊥

slide-44
SLIDE 44

What’s the Dual of Natural Numbers?

  • is dual to

Zero : 1 → Nat Run : Nat⊥ → ⊥

  • is dual to

Succ : Nat → Nat Tail : Nat⊥ → Nat⊥

Nat⊥

slide-45
SLIDE 45

What’s the Dual of Natural Numbers?

  • is dual to

Zero : 1 → Nat Run : Nat⊥ → ⊥

  • is dual to

Succ : Nat → Nat Tail : Nat⊥ → Nat⊥

  • is an infinite stream of computations

Nat⊥

Nat⊥

slide-46
SLIDE 46

What’s the Dual of Natural Numbers?

  • is dual to

Zero : 1 → Nat Run : Nat⊥ → ⊥

  • is dual to

Succ : Nat → Nat Tail : Nat⊥ → Nat⊥

  • is an infinite stream of computations

Nat⊥

  • You can run them, but they don’t return

Nat⊥

slide-47
SLIDE 47

What’s the Dual of Natural Numbers?

  • is dual to

Zero : 1 → Nat Run : Nat⊥ → ⊥

  • is dual to

Succ : Nat → Nat Tail : Nat⊥ → Nat⊥

  • is an infinite stream of computations

Nat⊥

  • You can run them, but they don’t return

Nat Values: Zero Succ V Nat Continuation: rec {Zero → N ∣ Succ x → y . P} with E

Nat⊥

slide-48
SLIDE 48

What’s the Dual of Natural Numbers?

  • is dual to

Zero : 1 → Nat Run : Nat⊥ → ⊥

  • is dual to

Succ : Nat → Nat Tail : Nat⊥ → Nat⊥

  • is an infinite stream of computations

Nat⊥

  • You can run them, but they don’t return

Nat Values: Zero Succ V Nat Continuation: rec {Zero → N ∣ Succ x → y . P} with E Nat⊥ Value: corec {Run → E ∣ Tail α → β . F} with V Nat⊥ Continuations: Run Tail E

Nat⊥

slide-49
SLIDE 49

Corecursion on Streams

In an Abstract Machine

slide-50
SLIDE 50

Corecursion on Streams

  • Generalize

to

Nat⊥ Stream A

In an Abstract Machine

slide-51
SLIDE 51

Corecursion on Streams

  • Generalize

to

Nat⊥ Stream A

  • Infinite stream of computations that return an A

In an Abstract Machine

slide-52
SLIDE 52

Corecursion on Streams

  • Generalize

to

Nat⊥ Stream A

  • Infinite stream of computations that return an A
  • and

Head : Stream A → A Tail : Stream A → Stream A

In an Abstract Machine

slide-53
SLIDE 53

Corecursion on Streams

  • Generalize

to

Nat⊥ Stream A

  • Infinite stream of computations that return an A
  • and

Head : Stream A → A Tail : Stream A → Stream A

Nat⊥ Value: corec {Run → E ∣ Tail β → γ . F} with V Nat⊥ Conts.: Run Tail E

In an Abstract Machine

slide-54
SLIDE 54

Corecursion on Streams

  • Generalize

to

Nat⊥ Stream A

  • Infinite stream of computations that return an A
  • and

Head : Stream A → A Tail : Stream A → Stream A

Nat⊥ Value: corec {Run → E ∣ Tail β → γ . F} with V Nat⊥ Conts.: Run Tail E

Stream A Value: corec {Head α → E ∣ Tail β → γ . F} with V Stream A Conts.: Head E Tail E

In an Abstract Machine

slide-55
SLIDE 55

Corecursion on Streams

In the λμ-Calculus

slide-56
SLIDE 56

Corecursion on Streams

  • Functional, direct-style

In the λμ-Calculus

slide-57
SLIDE 57

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”

In the λμ-Calculus

slide-58
SLIDE 58

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”
  • Contexts named by

; invoked by jumps

μα . J ⟨M| |α⟩

In the λμ-Calculus

slide-59
SLIDE 59

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”
  • Contexts named by

; invoked by jumps

μα . J ⟨M| |α⟩

Destructors: when

Head M : A Tail M : Stream A M : Stream A

In the λμ-Calculus

slide-60
SLIDE 60

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”
  • Contexts named by

; invoked by jumps

μα . J ⟨M| |α⟩

Destructors: when

Head M : A Tail M : Stream A M : Stream A

Generator: corec {Head → x . N ∣ Tail β → y . P} with M

In the λμ-Calculus

slide-61
SLIDE 61

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”
  • Contexts named by

; invoked by jumps

μα . J ⟨M| |α⟩

Destructors: when

Head M : A Tail M : Stream A M : Stream A

Generator: corec {Head → x . N ∣ Tail β → y . P} with M

  • Accumulator

, named and in the branches

M x y

In the λμ-Calculus

slide-62
SLIDE 62

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”
  • Contexts named by

; invoked by jumps

μα . J ⟨M| |α⟩

Destructors: when

Head M : A Tail M : Stream A M : Stream A

Generator: corec {Head → x . N ∣ Tail β → y . P} with M

  • Accumulator

, named and in the branches

M x y

  • Head branch: computes first element from current accumulator

N x

In the λμ-Calculus

slide-63
SLIDE 63

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”
  • Contexts named by

; invoked by jumps

μα . J ⟨M| |α⟩

Destructors: when

Head M : A Tail M : Stream A M : Stream A

Generator: corec {Head → x . N ∣ Tail β → y . P} with M

  • Accumulator

, named and in the branches

M x y

  • Head branch: computes first element from current accumulator

N x

  • Tail branch: computes one of two options

P

In the λμ-Calculus

slide-64
SLIDE 64

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”
  • Contexts named by

; invoked by jumps

μα . J ⟨M| |α⟩

Destructors: when

Head M : A Tail M : Stream A M : Stream A

Generator: corec {Head → x . N ∣ Tail β → y . P} with M

  • Accumulator

, named and in the branches

M x y

  • Head branch: computes first 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

In the λμ-Calculus

slide-65
SLIDE 65

Corecursion on Streams

  • Functional, direct-style
  • Don’t mention continuations directly; implicit “evaluation contexts”
  • Contexts named by

; invoked by jumps

μα . J ⟨M| |α⟩

Destructors: when

Head M : A Tail M : Stream A M : Stream A

Generator: corec {Head → x . N ∣ Tail β → y . P} with M

  • Accumulator

, named and in the branches

M x y

  • Head branch: computes first 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 finished

β

In the λμ-Calculus

slide-66
SLIDE 66

Examples of Corecursion

In an Abstract Machine

slide-67
SLIDE 67

Examples of Corecursion

count x = x, x + 1, x + 2, x + 3…

In an Abstract Machine

slide-68
SLIDE 68

Examples of Corecursion

count x = x, x + 1, x + 2, x + 3… count = λx . corec {Head → y . y ∣ Tail _ → z . Succ z} with x

In an Abstract Machine

slide-69
SLIDE 69

Examples of Corecursion

count x = x, x + 1, x + 2, x + 3… count = λx . corec {Head → y . y ∣ Tail _ → z . Succ z} with x scons x (y0, y1, y2…) = x, y0, y1, y2…

In an Abstract Machine

slide-70
SLIDE 70

Examples of Corecursion

count x = x, x + 1, x + 2, x + 3… count = λx . corec {Head → y . y ∣ Tail _ → z . Succ z} with x scons x (y0, y1, y2…) = x, y0, y1, y2… scons = λx . λys . corec {Head → _ . x ∣ Tail α → _ . μδ . ⟨ys| |α⟩} with _

In an Abstract Machine

slide-71
SLIDE 71

Examples of Corecursion

count x = x, x + 1, x + 2, x + 3… count = λx . corec {Head → y . y ∣ Tail _ → z . Succ z} with x scons x (y0, y1, y2…) = x, y0, y1, y2… scons = λx . λys . corec {Head → _ . x ∣ Tail α → _ . μδ . ⟨ys| |α⟩} with _ app [x0, x1, …, xn] (y0, y1, y2…) = x0, x1, …, xn, y0, y1, y2…

In an Abstract Machine

slide-72
SLIDE 72

Examples of Corecursion

count x = x, x + 1, x + 2, x + 3… count = λx . corec {Head → y . y ∣ Tail _ → z . Succ z} with x scons x (y0, y1, y2…) = x, y0, y1, y2… scons = λx . λys . corec {Head → _ . x ∣ Tail α → _ . μδ . ⟨ys| |α⟩} with _ app [x0, x1, …, xn] (y0, y1, y2…) = x0, x1, …, xn, y0, y1, y2… app = λxs . λys . corec Head → Cons x xs . x Tail _ → Cons x xs . xs Head → Nil . Head ys Tail α → Nil . μδ . ⟨Tail ys| |α⟩ with xs

In an Abstract Machine

slide-73
SLIDE 73

Corecursion vs Coiteration

Expressiveness vs Cost; CBV vs CBN

slide-74
SLIDE 74

Corecursion vs Coiteration

coiter { Head α → E Tail → γ . F} with V := corec { Head α → E Tail _ → γ . F} with V

Expressiveness vs Cost; CBV vs CBN

slide-75
SLIDE 75

Corecursion vs Coiteration

coiter { Head α → E Tail → γ . F} with V := corec { Head α → E Tail _ → γ . F} with V ⟨Left V| |[E, F]⟩ ↦ ⟨V| |E⟩ ⟨Right V| |[E, F]⟩ ↦ ⟨V| |F⟩

Expressiveness vs Cost; CBV vs CBN

slide-76
SLIDE 76

Corecursion vs Coiteration

coiter { Head α → E Tail → γ . F} with V := corec { Head α → E Tail _ → γ . F} with V ⟨Left V| |[E, F]⟩ ↦ ⟨V| |E⟩ ⟨Right V| |[E, F]⟩ ↦ ⟨V| |F⟩ corec { Head α → E Tail β → γ . F} with V := coiter { Head α → [Head α, E] Tail → [β, γ] . [Tail β, F]} with Right V

Expressiveness vs Cost; CBV vs CBN

slide-77
SLIDE 77

Corecursion vs Coiteration

coiter { Head α → E Tail → γ . F} with V := corec { Head α → E Tail _ → γ . F} with V ⟨Left V| |[E, F]⟩ ↦ ⟨V| |E⟩ ⟨Right V| |[E, F]⟩ ↦ ⟨V| |F⟩ corec { Head α → E Tail β → γ . F} with V := coiter { Head α → [Head α, E] Tail → [β, γ] . [Tail β, F]} with Right V

  • (Amortized) overhead cost; consider

:

scons x ys

Expressiveness vs Cost; CBV vs CBN

slide-78
SLIDE 78

Corecursion vs Coiteration

coiter { Head α → E Tail → γ . F} with V := corec { Head α → E Tail _ → γ . F} with V ⟨Left V| |[E, F]⟩ ↦ ⟨V| |E⟩ ⟨Right V| |[E, F]⟩ ↦ ⟨V| |F⟩ corec { Head α → E Tail β → γ . F} with V := coiter { Head α → [Head α, E] Tail → [β, γ] . [Tail β, F]} with Right V

  • (Amortized) overhead cost; consider

:

scons x ys

  • Native

: adds

  • verhead to cost of

corec Head(Tailn+1(scons x ys)) O(1) Head(Tailn ys)

Expressiveness vs Cost; CBV vs CBN

slide-79
SLIDE 79

Corecursion vs Coiteration

coiter { Head α → E Tail → γ . F} with V := corec { Head α → E Tail _ → γ . F} with V ⟨Left V| |[E, F]⟩ ↦ ⟨V| |E⟩ ⟨Right V| |[E, F]⟩ ↦ ⟨V| |F⟩ corec { Head α → E Tail β → γ . F} with V := coiter { Head α → [Head α, E] Tail → [β, γ] . [Tail β, F]} with Right V

  • (Amortized) overhead cost; consider

:

scons x ys

  • Native

: adds

  • verhead to cost of

corec Head(Tailn+1(scons x ys)) O(1) Head(Tailn ys)

  • Encoded

: adds

  • verhead to cost of

corec Head(Tailn+1(scons x ys)) O(n) Head(Tailn ys)

Expressiveness vs Cost; CBV vs CBN

slide-80
SLIDE 80

Corecursion vs Coiteration

coiter { Head α → E Tail → γ . F} with V := corec { Head α → E Tail _ → γ . F} with V ⟨Left V| |[E, F]⟩ ↦ ⟨V| |E⟩ ⟨Right V| |[E, F]⟩ ↦ ⟨V| |F⟩ corec { Head α → E Tail β → γ . F} with V := coiter { Head α → [Head α, E] Tail → [β, γ] . [Tail β, F]} with Right V

  • (Amortized) overhead cost; consider

:

scons x ys

  • Native

: adds

  • verhead to cost of

corec Head(Tailn+1(scons x ys)) O(1) Head(Tailn ys)

  • Encoded

: adds

  • verhead to cost of

corec Head(Tailn+1(scons x ys)) O(n) Head(Tailn ys)

  • Native CBN

has same overhead as encoding; Native CBV more efficient

corec corec

Expressiveness vs Cost; CBV vs CBN

slide-81
SLIDE 81

Corecursion vs Coiteration

coiter { Head α → E Tail → γ . F} with V := corec { Head α → E Tail _ → γ . F} with V ⟨Left V| |[E, F]⟩ ↦ ⟨V| |E⟩ ⟨Right V| |[E, F]⟩ ↦ ⟨V| |F⟩ corec { Head α → E Tail β → γ . F} with V := coiter { Head α → [Head α, E] Tail → [β, γ] . [Tail β, F]} with Right V

  • (Amortized) overhead cost; consider

:

scons x ys

  • Native

: adds

  • verhead to cost of

corec Head(Tailn+1(scons x ys)) O(1) Head(Tailn ys)

  • Encoded

: adds

  • verhead to cost of

corec Head(Tailn+1(scons x ys)) O(n) Head(Tailn ys)

  • Native CBN

has same overhead as encoding; Native CBV more efficient

corec corec

  • Corollary by duality of

and

rec iter

Expressiveness vs Cost; CBV vs CBN

slide-82
SLIDE 82

(Co)Inductive Reasoning

slide-83
SLIDE 83

Finite Induction

By Inversion on the Input

slide-84
SLIDE 84

Finite Induction

By Inversion on the Input

Γ, x : Bool ⊢ Φ(x)

slide-85
SLIDE 85

Finite Induction

By Inversion on the Input

Γ, x : Bool ⊢ Φ(x)

slide-86
SLIDE 86

Finite Induction

By Inversion on the Input

Γ, x : Bool ⊢ Φ(x) Γ ⊢ Φ(True)

slide-87
SLIDE 87

Finite Induction

By Inversion on the Input

Γ, x : Bool ⊢ Φ(x) Γ ⊢ Φ(True) Γ ⊢ Φ(False)

slide-88
SLIDE 88

Finite Induction

By Inversion on the Input

Γ, x : Bool ⊢ Φ(x) Γ ⊢ Φ(True) Γ ⊢ Φ(False)

slide-89
SLIDE 89

Infinite Induction

By Inversion on the Input

slide-90
SLIDE 90

Infinite Induction

By Inversion on the Input

Γ, x : Nat ⊢ Φ(x)

slide-91
SLIDE 91

Infinite Induction

By Inversion on the Input

Γ, x : Nat ⊢ Φ(x)

slide-92
SLIDE 92

Infinite Induction

By Inversion on the Input

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(0)

slide-93
SLIDE 93

Infinite Induction

By Inversion on the Input

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(0) Γ ⊢ Φ(1)

slide-94
SLIDE 94

Infinite Induction

By Inversion on the Input

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(0) Γ ⊢ Φ(1) Γ ⊢ Φ(2)

slide-95
SLIDE 95

Infinite Induction

By Inversion on the Input

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(0) Γ ⊢ Φ(1) Γ ⊢ Φ(2) …

slide-96
SLIDE 96

Infinite Induction

By Inversion on the Input

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(0) Γ ⊢ Φ(1) Γ ⊢ Φ(2) …

?

slide-97
SLIDE 97

An Induction Principle

Based on Information Flow

slide-98
SLIDE 98

An Induction Principle

Based on Information Flow

Γ, x : Nat ⊢ Φ(x)

slide-99
SLIDE 99

An Induction Principle

Based on Information Flow

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(Zero)

slide-100
SLIDE 100

An Induction Principle

Based on Information Flow

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(Zero) Γ, x : Nat, Φ(x) ⊢ Φ(Succ x)

slide-101
SLIDE 101

An Induction Principle

Based on Information Flow

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(Zero) Γ, x : Nat, Φ(x) ⊢ Φ(Succ x)

slide-102
SLIDE 102

An Induction Principle

Based on Information Flow

Γ, x : Nat ⊢ Φ(x) Γ ⊢ Φ(Zero) Γ, x : Nat, Φ(x) ⊢ Φ(Succ x)

Φ(Zero) ⇒ (∀x:Nat . Φ(x) ⇒ Φ(x + 1)) ⇒ (∀x:Nat . Φ(x))

slide-103
SLIDE 103

Finite Coinduction

By Inversion on the Output

slide-104
SLIDE 104

Finite Coinduction

By Inversion on the Output

λx . V x =η V

slide-105
SLIDE 105

Finite Coinduction

By Inversion on the Output

Γ ⊢ V = V′ : A → B

λx . V x =η V

slide-106
SLIDE 106

Finite Coinduction

By Inversion on the Output

Γ ⊢ V = V′ : A → B Γ, x : A ⊢ V x = V′ x : B

λx . V x =η V

slide-107
SLIDE 107

Finite Coinduction

By Inversion on the Output

Γ ⊢ V = V′ : A → B Γ, x : A ⊢ V x = V′ x : B

λx . V x =η V

λx . μβ . ⟨V| |x ⋅ β⟩ =η V

slide-108
SLIDE 108

Finite Coinduction

By Inversion on the Output

Γ, α ÷ A → B ⊢ Φ(α) Γ ⊢ V = V′ : A → B Γ, x : A ⊢ V x = V′ x : B

λx . V x =η V

λx . μβ . ⟨V| |x ⋅ β⟩ =η V

slide-109
SLIDE 109

Finite Coinduction

By Inversion on the Output

Γ, α ÷ A → B ⊢ Φ(α) Γ ⊢ V = V′ : A → B Γ, x : A ⊢ V x = V′ x : B

λx . V x =η V

λx . μβ . ⟨V| |x ⋅ β⟩ =η V

slide-110
SLIDE 110

Finite Coinduction

By Inversion on the Output

Γ, α ÷ A → B ⊢ Φ(α) Γ, x : A, β ÷ B ⊢ Φ(x ⋅ β) Γ ⊢ V = V′ : A → B Γ, x : A ⊢ V x = V′ x : B

λx . V x =η V

λx . μβ . ⟨V| |x ⋅ β⟩ =η V

slide-111
SLIDE 111

Infinite Coinduction

By Inversion on the Output

slide-112
SLIDE 112

Infinite Coinduction

By Inversion on the Output

Γ, α ÷ Stream A ⊢ Φ(α)

slide-113
SLIDE 113

Infinite Coinduction

By Inversion on the Output

Γ, α ÷ Stream A ⊢ Φ(α)

slide-114
SLIDE 114

Infinite Coinduction

By Inversion on the Output

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β)

slide-115
SLIDE 115

Infinite Coinduction

By Inversion on the Output

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β) Γ, β ÷ A ⊢ Φ(Tail(Head β))

slide-116
SLIDE 116

Infinite Coinduction

By Inversion on the Output

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β) Γ, β ÷ A ⊢ Φ(Tail(Head β)) Γ, β ÷ A ⊢ Φ(Tail(Tail(Head β)))

slide-117
SLIDE 117

Infinite Coinduction

By Inversion on the Output

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β) Γ, β ÷ A ⊢ Φ(Tail(Head β)) Γ, β ÷ A ⊢ Φ(Tail(Tail(Head β))) ⋮

slide-118
SLIDE 118

Infinite Coinduction

By Inversion on the Output

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β) Γ, β ÷ A ⊢ Φ(Tail(Head β)) Γ, β ÷ A ⊢ Φ(Tail(Tail(Head β))) ⋮

?

slide-119
SLIDE 119

A Coinduction Principle

Based on Control Flow

slide-120
SLIDE 120

A Coinduction Principle

Based on Control Flow

Γ, α ÷ Stream A ⊢ Φ(α)

slide-121
SLIDE 121

A Coinduction Principle

Based on Control Flow

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β)

slide-122
SLIDE 122

A Coinduction Principle

Based on Control Flow

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β) Γ, α ÷ Stream A, Φ(α) ⊢ Φ(Tail α)

slide-123
SLIDE 123

A Coinduction Principle

Based on Control Flow

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β) Γ, α ÷ Stream A, Φ(α) ⊢ Φ(Tail α)

slide-124
SLIDE 124

A Coinduction Principle

Based on Control Flow

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β) Γ, α ÷ Stream A, Φ(α) ⊢ Φ(Tail α)

Bisimulation = (∀s, s′ : Stream A . Φ(s, s′ ) ⇒ Head s = Head s′ : A) ⇒ (∀s, s′ : Stream A . Φ(s, s′ ) ⇒ Φ(Tail s, Tail s′ )) ⇒ (∀s, s′ : Stream A . Φ(s, s′ ) ⇒ s = s′ : Stream A)

slide-125
SLIDE 125

A Coinduction Principle

Based on Control Flow

Γ, α ÷ Stream A ⊢ Φ(α) Γ, β ÷ A ⊢ Φ(Head β) Γ, α ÷ Stream A, Φ(α) ⊢ Φ(Tail α)

Bisimulation = (∀s, s′ : Stream A . Φ(s, s′ ) ⇒ Head s = Head s′ : A) ⇒ (∀s, s′ : Stream A . Φ(s, s′ ) ⇒ Φ(Tail s, Tail s′ )) ⇒ (∀s, s′ : Stream A . Φ(s, s′ ) ⇒ s = s′ : Stream A)

slide-126
SLIDE 126

Proof by Coinduction

slide-127
SLIDE 127

Proof by Coinduction

repeat x = x, x, x… alt = 0,1,0,1… evens (x0, x1, x2…) = x0, x2, x4…

slide-128
SLIDE 128

Proof by Coinduction

Theorem: evens alt = repeat 0 : Stream A repeat x = x, x, x… alt = 0,1,0,1… evens (x0, x1, x2…) = x0, x2, x4…

slide-129
SLIDE 129

Proof by Coinduction

Theorem: evens alt = repeat 0 : Stream A

  • S.T.S: α ÷ Stream A ⊢ ⟨evens alt|

|α⟩ = ⟨repeat 0| |α⟩

repeat x = x, x, x… alt = 0,1,0,1… evens (x0, x1, x2…) = x0, x2, x4…

slide-130
SLIDE 130

Proof by Coinduction

Proof: By coinduction on …

α ÷ Stream A

Theorem: evens alt = repeat 0 : Stream A

  • S.T.S: α ÷ Stream A ⊢ ⟨evens alt|

|α⟩ = ⟨repeat 0| |α⟩

repeat x = x, x, x… alt = 0,1,0,1… evens (x0, x1, x2…) = x0, x2, x4…

slide-131
SLIDE 131

Proof by Coinduction

Proof: By coinduction on …

α ÷ Stream A

  • :

α = Head β ⟨evens alt| |Head β⟩ = ⟨0| |β⟩ = ⟨repeat 0| |Head β⟩

Theorem: evens alt = repeat 0 : Stream A

  • S.T.S: α ÷ Stream A ⊢ ⟨evens alt|

|α⟩ = ⟨repeat 0| |α⟩

repeat x = x, x, x… alt = 0,1,0,1… evens (x0, x1, x2…) = x0, x2, x4…

slide-132
SLIDE 132

Proof by Coinduction

Proof: By coinduction on …

α ÷ Stream A

  • :

α = Head β ⟨evens alt| |Head β⟩ = ⟨0| |β⟩ = ⟨repeat 0| |Head β⟩

  • : Assume CoIH

and show …

α = Tail β ⟨evens alt| |β⟩ = ⟨repeat 0| |β⟩ ⟨evens alt| |Tail β⟩ = ⟨repeat 0| |Tail β⟩

Theorem: evens alt = repeat 0 : Stream A

  • S.T.S: α ÷ Stream A ⊢ ⟨evens alt|

|α⟩ = ⟨repeat 0| |α⟩

repeat x = x, x, x… alt = 0,1,0,1… evens (x0, x1, x2…) = x0, x2, x4…

slide-133
SLIDE 133

Proof by Coinduction

Proof: By coinduction on …

α ÷ Stream A

  • :

α = Head β ⟨evens alt| |Head β⟩ = ⟨0| |β⟩ = ⟨repeat 0| |Head β⟩

  • : Assume CoIH

and show …

α = Tail β ⟨evens alt| |β⟩ = ⟨repeat 0| |β⟩ ⟨evens alt| |Tail β⟩ = ⟨repeat 0| |Tail β⟩ ⟨evens alt| |Tail β⟩ = ⟨evens (Tail(Tail alt))| |β⟩ (def . evens) = ⟨evens alt| |β⟩ (def . alt) = ⟨repeat 0| |β⟩ (CoIH) = ⟨repeat 0| |Tail β⟩ (def . repeat)

Theorem: evens alt = repeat 0 : Stream A

  • S.T.S: α ÷ Stream A ⊢ ⟨evens alt|

|α⟩ = ⟨repeat 0| |α⟩

repeat x = x, x, x… alt = 0,1,0,1… evens (x0, x1, x2…) = x0, x2, x4…

slide-134
SLIDE 134

Weak vs Strong (Co)Induction

And Effectful Computation

slide-135
SLIDE 135

Weak vs Strong (Co)Induction

  • Strong (co)induction proves any property Φ

And Effectful Computation

slide-136
SLIDE 136

Weak vs Strong (Co)Induction

  • Strong (co)induction proves any property Φ
  • Strong induction is unsound in CBN

And Effectful Computation

slide-137
SLIDE 137

Weak vs Strong (Co)Induction

  • Strong (co)induction proves any property Φ
  • Strong induction is unsound in CBN
  • Strong coinduction is unsound in CBV

And Effectful Computation

slide-138
SLIDE 138

Weak vs Strong (Co)Induction

  • Strong (co)induction proves any property Φ
  • Strong induction is unsound in CBN
  • Strong coinduction is unsound in CBV
  • Weak (co)induction restricts Φ

And Effectful Computation

slide-139
SLIDE 139

Weak vs Strong (Co)Induction

  • Strong (co)induction proves any property Φ
  • Strong induction is unsound in CBN
  • Strong coinduction is unsound in CBV
  • Weak (co)induction restricts Φ
  • Weak induction on : must be strict on like

x x ⟨x| |E⟩ = ⟨x| |E′ ⟩

And Effectful Computation

slide-140
SLIDE 140

Weak vs Strong (Co)Induction

  • Strong (co)induction proves any property Φ
  • Strong induction is unsound in CBN
  • Strong coinduction is unsound in CBV
  • Weak (co)induction restricts Φ
  • Weak induction on : must be strict on like

x x ⟨x| |E⟩ = ⟨x| |E′ ⟩

  • Weak induction on : must be productive on like

α α ⟨V| |α⟩ = ⟨V′ | |α⟩

And Effectful Computation

slide-141
SLIDE 141

Weak vs Strong (Co)Induction

  • Strong (co)induction proves any property Φ
  • Strong induction is unsound in CBN
  • Strong coinduction is unsound in CBV
  • Weak (co)induction restricts Φ
  • Weak induction on : must be strict on like

x x ⟨x| |E⟩ = ⟨x| |E′ ⟩

  • Weak induction on : must be productive on like

α α ⟨V| |α⟩ = ⟨V′ | |α⟩

  • Weak (co)induction is always sound

And Effectful Computation

slide-142
SLIDE 142

Lessons Learned

slide-143
SLIDE 143

Lessons Learned

  • Duality — Ideas for free!
slide-144
SLIDE 144

Lessons Learned

  • Duality — Ideas for free!
  • Impact of evaluation, computation, effects, divergence
  • CBV: strong induction and efficient corecursion
  • CBN: strong coinduction and efficient recursion
  • Future work: Call-by-push-value or polarities could get best of both worlds
slide-145
SLIDE 145

Lessons Learned

  • Duality — Ideas for free!
  • Impact of evaluation, computation, effects, divergence
  • CBV: strong induction and efficient corecursion
  • CBN: strong coinduction and efficient recursion
  • Future work: Call-by-push-value or polarities could get best of both worlds
  • (Co)Induction are both inversion principles
  • Induction: inversion on input, guided by information flow
  • Coinduction: inversion on output, guided by control flow