170 likes | 273 Views
Coinduction in a language and verifier. K. Rustan M. Leino Research in Software Engineering ( RiSE ) Microsoft Research, Redmond. IFIP WG 2.3 meeting Seattle, WA, USA 16 July 2012. Question. How to add coinductive features , like coinductive datatypes c orecursive functions
E N D
Coinduction in alanguage and verifier K. Rustan M. Leino Research in Software Engineering (RiSE) Microsoft Research, Redmond IFIP WG 2.3 meeting Seattle, WA, USA 16July 2012
Question How to add coinductive features, like • coinductivedatatypes • corecursive functions • copredicates • coinductive proofs in a programming language supported by a program verifier
Inductive datatypes datatype List = Nil | Cons(int, List); • Defined by least fixpoint • That is, List is the smallest set L such that: • NilL • x,a• x int a L Cons(x, a) L • Gives rise to finite structures
Coinductivedatatypes codatatype Stream = Nil | Cons(int, Stream); • Defined by largest fixpoint • That is, Stream is the largest set S such that: • NilS • x,a• x int a S Cons(x, a) S • Gives rise to possibly infinite structures
Constructors and destructors • One view (e.g., category theory, Charity) is that: • Inductive datatypes put emphasis on constructors • Nil : () List • Cons : int List List • Coinductivedatatypes put emphasis on destructors • head : Stream int • tail : Stream Stream • Another view (e.g., Coq) puts emphasis on constructors for both, and allows destructors to be defined for both
Recursion function Append(a: List, b: List): List{match acase Nil => bcaseCons(h, t) => Cons(h, Append(t, b))} • Well-defined? • Yes, if the function terminates • Termination implies a unique fixpoint
Corecursion function Upward(n: int): Stream{ Cons(n, Upward(n+1))} • Well-defined? • Yes, if corecursive calls are guarded • Unique fixpoint? • My conclusion: when functions are defined by computations, no need to distinguish between functions and cofunctions
Combining recursion and corecursion • (Co)recursive calls,not (co)recursive functions function F(n: nat): Stream decreases (n+4)/5 * 5- n; { ifn % 5 == 0thenCons(n, F(n+1))else F(n+1) } Corecursive call Recursive call
Predicates and copredicates • predicate P(x: T) { E }is the same as:function P(x: T): bool{ E } • Recursive calls must be terminating • copredicateC(x: T) { E }defines C to be the largest boolean function satisfying x• C(x) = E . • Corecursive calls must be in positive positions
Inductive proofs • Induction principle: n • P(n) = n • (k • k < n P(k)) P(n) • Appealing to the inductive hypothesis is like making a recursive call • “Manual” proofs by induction can be done in code • Induction hypothesis from the induction principle can be inserted heuristically
Properties and equality, induction • Showing that an inductive-datatype value satisfies a property can be proved by induction • Equality of two inductive-datatype values can be proved by induction
Coinductive proofs • Coinduction principle for a copredicatecopredicate C(x: T) { Body[C](x) }is: for any predicate Q, Q(a) (s • Q(s) Body[Q](s) ) C(a) • Inventing and using the predicate Q is a bit like inventing and using a loop invariant
Example: Coinductive proof • function Up(n: nat): Stream{ Cons(n+1, Up(n+1)) } • copredicatePos(s: Stream){ s.head > 0 Pos(s.tail) } • To prove Pos(Up(k)), choose Q(s) := n • s = Up(n)and then prove: • Q(Up(k)) • (s • Q(s) s.head > 0 Q(s.tail) )
Properties and equality, induction • Showing that a coinductive-datatype value satisfies a property can be proved using the coinduction principle • Equality of two coinductive-datatype values can be proved by bisimulation (where one has to invent a relation R)
Properties and bisimilarity • Proving a property can be done by bisimilarity • Pos(a)≡Map(a, x• x>0) = True()wherecopredicatePos(s) { s.head > 0 Pos(s.tail)) }functionMap(s, F) { Cons(F(s.head), Map(s.tail)) }functionTrue() { Cons(true, True()) } • Can bisimilarity be proved via property proofs?
Manual coinductive proofs • comethod? • cowhile, coinvariant?
Things I want to understand better • A semantic notion of guarded, analogous to the semantic notion of well-founded • Symmetry between predicate and copredicate? • Predicates are special cases of functions, which are defined computationally • Copredicates are defined declaratively • More natural language features to prove copredicates? • How to do “manual” proofs by coinduction? • What language features to support programmer-supplied relation R to prove bisimulation?