A Coinductive Monad for Prop -bounded Recursion Adam Megacz - - PowerPoint PPT Presentation

a coinductive monad for prop bounded recursion
SMART_READER_LITE
LIVE PREVIEW

A Coinductive Monad for Prop -bounded Recursion Adam Megacz - - PowerPoint PPT Presentation

A Coinductive Monad for Prop -bounded Recursion Adam Megacz megacz@cs.berkeley.edu PLPV07 October 5th, 2007 Freiburg, Germany Entire Talk In One Slide Coqs type theory can directly represent side-effect free, obviously-terminating


slide-1
SLIDE 1

A Coinductive Monad for Prop-bounded Recursion

Adam Megacz megacz@cs.berkeley.edu PLPV’07 October 5th, 2007 Freiburg, Germany

slide-2
SLIDE 2

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions
slide-3
SLIDE 3

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality

slide-4
SLIDE 4

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions

slide-5
SLIDE 5

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

slide-6
SLIDE 6

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

slide-7
SLIDE 7

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

slide-8
SLIDE 8

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

◮ Allow optional termination proofs, which should:

slide-9
SLIDE 9

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

◮ Allow optional termination proofs, which should: ◮ convert computations to functions.

slide-10
SLIDE 10

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

◮ Allow optional termination proofs, which should: ◮ convert computations to functions. ◮ be in Prop

slide-11
SLIDE 11

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

◮ Allow optional termination proofs, which should: ◮ convert computations to functions. ◮ be in Prop ◮ be conventional (follow prose arguments)

slide-12
SLIDE 12

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

◮ Allow optional termination proofs, which should: ◮ convert computations to functions. ◮ be in Prop ◮ be conventional (follow prose arguments) ◮ not require advance planning; “after the fact”

slide-13
SLIDE 13

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

◮ Allow optional termination proofs, which should: ◮ convert computations to functions. ◮ be in Prop ◮ be conventional (follow prose arguments) ◮ not require advance planning; “after the fact”

◮ This talk: a coinductive type whose constructors are the

  • perators of a monad.
slide-14
SLIDE 14

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

◮ Allow optional termination proofs, which should: ◮ convert computations to functions. ◮ be in Prop ◮ be conventional (follow prose arguments) ◮ not require advance planning; “after the fact”

◮ This talk: a coinductive type whose constructors are the

  • perators of a monad.

◮ Achieves goals above, except: sacrifices reduction and equality.

slide-15
SLIDE 15

Entire Talk In One Slide

◮ Coq’s type theory can directly represent side-effect free,

  • bviously-terminating functions

◮ using built-in abstraction, reduction, equality ◮ can prove properties of such functions ◮ can extract efficient implementations of such functions

◮ Goals: we would like to

◮ Represent potentially-nonterminating computations in a

manner which retains advantages above.

◮ Allow optional termination proofs, which should: ◮ convert computations to functions. ◮ be in Prop ◮ be conventional (follow prose arguments) ◮ not require advance planning; “after the fact”

◮ This talk: a coinductive type whose constructors are the

  • perators of a monad.

◮ Achieves goals above, except: sacrifices reduction and equality.

slide-16
SLIDE 16

Running Example: McCarthy’s Function

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-17
SLIDE 17

Running Example: McCarthy’s Function

Actual behavior: M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-18
SLIDE 18

First Attempt: Direct Representation

Fixpoint mccarthy (n:nat) {struct n} : nat := match le_gt_dec n 100 with | left _ => mccarthy (mccarthy (11+n)) | right _ => n-10 end. M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-19
SLIDE 19

First Attempt: Direct Representation

Fixpoint mccarthy (n:nat) {struct n} : nat := match le_gt_dec n 100 with | left _ => mccarthy (mccarthy (11+n)) | right _ => n-10 end.

◮ problem: 11+n is not structurally smaller than n

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-20
SLIDE 20

First Attempt: Direct Representation

Fixpoint mccarthy (n:nat) {struct n} : nat := match le_gt_dec n 100 with | left _ => mccarthy (mccarthy (11+n)) | right _ => n-10 end.

◮ problem: 11+n is not structurally smaller than n ◮ problem: mccarthy (11+n) is not structurally smaller than n

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-21
SLIDE 21

First Attempt: Direct Representation

Fixpoint mccarthy (n:nat) {struct n} : nat := match le_gt_dec n 100 with | left _ => mccarthy (mccarthy (11+n)) | right _ => n-10 end.

◮ problem: 11+n is not structurally smaller than n ◮ problem: mccarthy (11+n) is not structurally smaller than n ◮ problem: any decreasing metric will more complex than the

function itself M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-22
SLIDE 22

Solution #1: Set-bounded recursion

Fixpoint mccarthy ( n:nat) {struct n} : nat := match le_gt_dec n 100 with | left _ => mccarthy (mccarthy (11+n)) | right _ => (n-10) end. M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-23
SLIDE 23

Solution #1: Set-bounded recursion

Notation "a =<< b" := (match b with None => None | Some x => a x end) (at level 100). Fixpoint mccarthy (d n:nat) {struct d} : option nat := match d with | 0 => None | (S d’) => match le_gt_dec n 100 with | left _ => mccarthy d’ =<< (mccarthy d’ (11+n)) | right _ => Some (n-10) end end. M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-24
SLIDE 24

Typical Solution: Set-bounded evaluation

◮ good: determination of recursion bound is separated from

function definition

slide-25
SLIDE 25

Typical Solution: Set-bounded evaluation

◮ good: determination of recursion bound is separated from

function definition

◮ bad: recursion bound is in Set; will be included in extracted

code

slide-26
SLIDE 26

Typical Solution: Set-bounded evaluation

◮ good: determination of recursion bound is separated from

function definition

◮ bad: recursion bound is in Set; will be included in extracted

code

Later we modified the whole formalization and we used the Prop-sorted accessibility. Our tests showed a 25% to 30% decrease in both time and memory usage of the extracted algorithms. – Niqui and Bertot (2003)

slide-27
SLIDE 27

Other Approaches

◮ Domain predicate

◮ multi-constructor type (in Set) [Bove, Capretta 2001] ◮ often with Dybjer’s simultaneous inductive-recursive definitions

◮ Accessibility predicate

◮ single-constructor predicate (in Prop) [Coq.Init.Wf]

◮ Extensions to the type theory (Y, bar types, etc)

slide-28
SLIDE 28

Proposed Solution: A Coinductive Computation Monad

CoInductive Computation (A:Set) : Type := | Return : A

  • > #A

| Bind : (A->#A) -> #A -> #A where "# A" := (Computation A).

slide-29
SLIDE 29

Programming mccarthy Using the Monad

CoFixpoint mccarthy (n:nat) : #nat := match le_gt_dec n 100 with | left _ => Bind mccarthy (mccarthy (11+n)) | right _ => Return (n-10) end.

◮ Use of CoFixpoint circumvents usual syntactic check of

recursive references.

◮ Making monad operators constructors of the coinductive type

ensures generativity.

slide-30
SLIDE 30

Termination Proofs

Inductive TerminatesWith : #A -> A -> Prop := | TerminateReturnWith : forall (a:A), TerminatesWith (Return a) a | TerminateBindWith : forall (a:A) (a’:A) (f:A->#A) (c:#A), (TerminatesWith c a)

  • > TerminatesWith (f a) a’
  • > TerminatesWith (Bind f c) a’
slide-31
SLIDE 31

Termination Proofs

Inductive TerminatesWith : #A -> A -> Prop := | TerminateReturnWith : forall (a:A), TerminatesWith (Return a) a | TerminateBindWith : forall (a:A) (a’:A) (f:A->#A) (c:#A), (TerminatesWith c a)

  • > TerminatesWith (f a) a’
  • > TerminatesWith (Bind f c) a’
slide-32
SLIDE 32

Termination Proofs

Inductive TerminatesWith : #A -> A -> Prop := | TerminateReturnWith : forall (a:A), TerminatesWith (Return a) a | TerminateBindWith : forall (a:A) (a’:A) (f:A->#A) (c:#A), (TerminatesWith c a)

  • > TerminatesWith (f a) a’
  • > TerminatesWith (Bind f c) a’
slide-33
SLIDE 33

Termination Proofs

Inductive TerminatesWith : #A -> A -> Prop := | TerminateReturnWith : forall (a:A), TerminatesWith (Return a) a | TerminateBindWith : forall (a:A) (a’:A) (f:A->#A) (c:#A), (TerminatesWith c a)

  • > TerminatesWith (f a) a’
  • > TerminatesWith (Bind f c) a’
slide-34
SLIDE 34

Termination Proofs

Inductive TerminatesWith : #A -> A -> Prop := | TerminateReturnWith : forall (a:A), TerminatesWith (Return a) a | TerminateBindWith : forall (a:A) (a’:A) (f:A->#A) (c:#A), (TerminatesWith c a)

  • > TerminatesWith (f a) a’
  • > TerminatesWith (Bind f c) a’
slide-35
SLIDE 35

Termination Proofs

Inductive TerminatesWith : #A -> A -> Prop := | TerminateReturnWith : forall (a:A), TerminatesWith (Return a) a | TerminateBindWith : forall (a:A) (a’:A) (f:A->#A) (c:#A), (TerminatesWith c a)

  • > TerminatesWith (f a) a’
  • > TerminatesWith (Bind f c) a’

Note that facts about the return value (a) of one computation (c) may be used in the termination argument for some other computation (f a).

slide-36
SLIDE 36

Base Case: M(101) terminates

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-37
SLIDE 37

First Induction: 90 ≤ n ≤ 100 ⇒ M(n) = M(n + 1)

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-38
SLIDE 38

Second Induction: 90 ≤ n ≤ 100 ⇒ M(n) = 91

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-39
SLIDE 39

Third Induction: each “block of eleven” same

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-40
SLIDE 40

Third Induction: each “block of eleven” same

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-41
SLIDE 41

Third Induction: each “block of eleven” same

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-42
SLIDE 42

Third Induction: each “block of eleven” same

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-43
SLIDE 43

Therefore M(n) terminates for n ≤ 100

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-44
SLIDE 44

Termination for n > 100 is immediate

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-45
SLIDE 45

Summary of Termination Argument for mccarthy

◮ We can show by downward induction that for 90 ≤ n ≤ 100,

M(n) = M(n + 1) (taking M(100) = M(101) as the base case).

◮ By a second induction we can show that M(n) = 91 over this

range.

◮ By a third downward induction we can show that M(n) = 91

holds for each chunk of eleven integers less than 100, using the initial chunk 90 ≤ n ≤ 100 as the base case.

◮ Therefore the function terminates for n ≤ 100. ◮ Termination for n > 100 is immediate from the definition of

the function.

slide-46
SLIDE 46

Summary of Termination Argument for mccarthy

◮ We can show by downward induction that for 90 ≤ n ≤ 100,

M(n) = M(n + 1) (taking M(100) = M(101) as the base case).

◮ By a second induction we can show that M(n) = 91 over this

range.

◮ By a third downward induction we can show that M(n) = 91

holds for each chunk of eleven integers less than 100, using the initial chunk 90 ≤ n ≤ 100 as the base case.

◮ Therefore the function terminates for n ≤ 100. ◮ Termination for n > 100 is immediate from the definition of

the function. Unlike metric-based techniques, the proof of termination using a coinductive monad can follow the conventional prose argument.

slide-47
SLIDE 47

Proving forall n, Terminates (mccarthy n)

We can show by downward induction that for 90 ≤ n ≤ 100, M(n) = M(n + 1) (taking M(100) = M(101) as the base case). Lemma mccarthy_is_m_of_n_plus_1_for_90_n_100 : forall n k:nat, 90 <= n <= 100

  • > TerminatesWith (mccarthy (n+1)) k
  • > TerminatesWith (mccarthy (n

)) k. M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-48
SLIDE 48

Proving forall n, Terminates (mccarthy n)

By a second induction we can show that M(n) = 91 over this range. Lemma mccarthy_n_is_91_for_90_n_100 : forall n:nat, 90 <= n <= 100

  • > TerminatesWith (mccarthy n) 91.

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-49
SLIDE 49

Proving forall n, Terminates (mccarthy n)

By a third downward induction we can show that M(n) = 91 holds for each chunk of eleven integers less than 100, using the initial chunk 90 ≤ n ≤ 100 as the base case. Lemma mccarthy_n_is_91_for_blocks_of_11 : forall k:nat, 100 > k*11

  • > forall n:nat,

90-k*11 <= n <= 100-k*11

  • > TerminatesWith (mccarthy n) 91.

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-50
SLIDE 50

Proving forall n, Terminates (mccarthy n)

Therefore the function terminates for n ≤ 100. Lemma mccarthy_terminates_for_n_le_100 : forall n:nat, n <= 100

  • > TerminatesWith (mccarthy n) 91.

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-51
SLIDE 51

Proving forall n, Terminates (mccarthy n)

Termination for n > 100 is immediate from the definition of the function. Lemma mccarthy_terminates_for_n_gt_100 : forall n:nat, n > 100

  • > Terminates (mccarthy n).

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-52
SLIDE 52

Proving forall n, Terminates (mccarthy n)

Therefore, mccarthy is total. Theorem mccarthy_terminates : forall n:nat,

  • > Terminates (mccarthy n).

M(n) =

  • n − 10

if n > 100 M(M(n + 11)) if n ≤ 100

slide-53
SLIDE 53

Evaluation and Extraction

eval : forall (A:Set) (c:#A) (t:Terminates c), A Functions produced by eval yield efficient extractions; the Terminates term (Prop bound) is completely omitted. bounded_eval : forall (A:Set) (c:#A) (n:nat), option A

slide-54
SLIDE 54

Summary So Far

◮ We can represent potentially-nonterminating computations ◮ We can write proofs about the properties (such as

termination) of such computations,

◮ Proofs can be used to convert a computation to a function

(via eval)

◮ Proofs are in Prop ◮ Proofs are conventional (follow prose) ◮ Proofs are “after the fact”

◮ Coq’s extraction mechanism produces efficient code for

applications of eval.

slide-55
SLIDE 55

Prior work: [Capretta 2005]

Different encoding of computations as coinductive values; closer connection to operational semantics: CoInductive Computation (A:Set) : Type := | Return : A -> #A | Step : #A -> #A where "# A" := (Computation A). CoFixpoint bind (A B:Set)(f:A->#B)(x:#A) : #B := match x with | Return a => f a | Step x’ => Step (bind A B f x’) end.

slide-56
SLIDE 56

Prior work: [Capretta 2005]

Different encoding of computations as coinductive values; closer connection to operational semantics: CoInductive Computation (A:Set) : Type := | Return : A -> #A | Step : #A -> #A where "# A" := (Computation A).

◮ Disadvantage: bind is no longer a constructor, so programs

with nested recursion fail the generativity requirement.

slide-57
SLIDE 57

Prior work: [Capretta 2005]

Different encoding of computations as coinductive values; closer connection to operational semantics: CoInductive Computation (A:Set) : Type := | Return : A -> #A | Step : #A -> #A where "# A" := (Computation A).

◮ Disadvantage: bind is no longer a constructor, so programs

with nested recursion fail the generativity requirement.

slide-58
SLIDE 58

Generalizing to Different Range/Domain

CoInductive Computation (A:Set) : Type := | Return : A

  • > #A

| Bind : (A->#A) -> #A -> #A. where "# A" := (Computation A).

slide-59
SLIDE 59

Generalizing to Different Range/Domain

CoInductive Computation (A:Set) : Type := | Return : A

  • > #A

| Bind : forall (B:Set), (B->#A) -> #B -> #A. where "# A" := (Computation A).

◮ Proof of eval safety requires JMeq axiom [McB00] in this case

slide-60
SLIDE 60

Mutual Recursion

CoFixpoint isEven (isOdd:nat->#bool) (n:nat) : #bool := match n with | 0 => Return true | (S n’) => x <- isOdd n’; Return (negb x) end. CoFixpoint isOdd (isEven:nat->#bool) (n:nat) : #bool := match n with | 0 => Return false | (S n’) => x <- isEven n’; Return (negb x) end. CoFixpoint isEven’ := (kludge isEven) isOdd’ with isOdd’ := (kludge isOdd) isEven’ with kludge := (fun x=>x).

slide-61
SLIDE 61

Higher-Order Computations

CoFixpoint foldc (A B:Set)(la:list A)(b:B)(f:A->B->(#B)) : #B := match la with | nil => Return b | (cons a la’) => b’ <- f a b ; foldc A B la’ b’ f end.

slide-62
SLIDE 62

First-Class Termination Proofs

Lemma foldc_termination : forall (A B:Set) (la:list A) (b0:B) (f:A->B->#B), (forall (a:A)(b:B), (In a la) -> (Terminates (f a b)))

  • > Terminates (foldc la b0 f).
slide-63
SLIDE 63

Thank you