Coinductive Reasoning in Dependent Type Theory - Copatterns, - - PowerPoint PPT Presentation

coinductive reasoning in dependent type theory copatterns
SMART_READER_LITE
LIVE PREVIEW

Coinductive Reasoning in Dependent Type Theory - Copatterns, - - PowerPoint PPT Presentation

Coinductive Reasoning in Dependent Type Theory - Copatterns, Objects, Processes Anton Setzer http://www.cs.swan.ac.uk/~csetzer/index.html Swansea University http://www.swansea.ac.uk/compsci/ (Part on Processes presented by Bashar Igried on


slide-1
SLIDE 1

Coinductive Reasoning in Dependent Type Theory - Copatterns, Objects, Processes

Anton Setzer http://www.cs.swan.ac.uk/~csetzer/index.html Swansea University http://www.swansea.ac.uk/compsci/ (Part on Processes presented by Bashar Igried on separate slides, Remaining parts with contributions by Peter Hancock, Andreas Abel, Brigitte Pientka, David Thibodeau) Talk given at JAIST, Japan 6 September 2016

Anton Setzer Coinductive Reasoning 1/ 65

slide-2
SLIDE 2

Motivation (Co)Iteration – (Co)Recursion – (Co)Induction Generalisation (Petersson-Synek Trees) Schemata for Corecursive Definitions and Coinductive Proofs Objects Conclusion Bibliography

Anton Setzer Coinductive Reasoning 2/ 65

slide-3
SLIDE 3

Motivation

Motivation (Co)Iteration – (Co)Recursion – (Co)Induction Generalisation (Petersson-Synek Trees) Schemata for Corecursive Definitions and Coinductive Proofs Objects Conclusion Bibliography

Anton Setzer Coinductive Reasoning 3/ 65

slide-4
SLIDE 4

Motivation

Need for Coinductive Proofs

◮ In the beginning of computing, computer programs were batch

programs.

◮ One input one output ◮ Correct programs correspond to well-founded structures

(termination).

◮ Nowadays most programs are interactive;

◮ A possibly infinite sequence of interactions, often concurrently. ◮ Correspond to non-well-founded structures. ◮ For instance non-concurrent computations can be represented as

IO-trees.

◮ A simple form of objects in object-oriented programs can be

represented as non-well-founded trees.

Anton Setzer Coinductive Reasoning 4/ 65

slide-5
SLIDE 5

Motivation

IO-Trees (Non-State Dependent)

  • p ∈ IO

c ∈ C (r ∈ R(c)) p′ ∈ IO c′ ∈ C (r ′ ∈ R(c′)) p′′ ∈ IO c′′ ∈ C

Anton Setzer Coinductive Reasoning 5/ 65

slide-6
SLIDE 6

Motivation

IO-Trees State Dependent

  • p ∈ IO(s)

c ∈ C(s) (r ∈ R(s, c)) p′ ∈ IO(s′) (s′ = n(s, c, r)) c′ ∈ C(s′) (r ′ ∈ R(s′, c′)) p′′ ∈ IO(s′′) (s′′ = n(s′, c′, r ′)) c′′ ∈ C(s′′)

Anton Setzer Coinductive Reasoning 6/ 65

slide-7
SLIDE 7

Motivation

Objects (State Dependent)

  • ∈ Object(s)

(m ∈ Method(s)) (m′ ∈ Method(s′))

  • ′′ ∈ Object(s′′) (s′′ = next(s′, m′, r′))
  • ′ ∈ Object(s′) (s′ = next(s, m, r))

r′ ∈ Result(s′, m′) r ∈ Result(s, m) Anton Setzer Coinductive Reasoning 7/ 65

slide-8
SLIDE 8

Motivation

Need for Good Framework for Coinductive Structures

◮ Non-well-founded trees are defined coinductively. ◮ Relations between coinductive structures are coinductively defined ◮ Need suitable notion of reasoning coinductively.

Anton Setzer Coinductive Reasoning 8/ 65

slide-9
SLIDE 9

Motivation

Coinductive Proofs

◮ Reasoning about bisimulation is often very formalist. Consider an

unlabelled Transition system:

1 2 · · · ∗

◮ For showing ∗ ∼ n one defines

◮ R := {(∗, n) | n ∈ N} ◮ Shows that R is a bisimulation relation: ◮ Let (a, b) ∈ R. Then a = ∗, b = n ∈ N for some n. ◮ Assume a = ∗ −

→ a′. Then a′ = ∗. We have b = n − → n + 1 and (∗, n + 1) ∈ R.

◮ Assume b = n −

→ b′. Then b′ = n + 1. We have a = ∗ − → ∗ and (∗, n + 1) ∈ R.

◮ Therefore x ∼ y for (x, y) ∈ R. Anton Setzer Coinductive Reasoning 9/ 65

slide-10
SLIDE 10

Motivation

Comparison

◮ Above is similar when carrying an inductive proof, e.g. of

ϕ := ∀n, m, k.(n + m) + k = n + (m + k) to defining A := {k | (n + m) + k = n + (m + k)} and showing that A is closed under 0 and successor.

◮ Instead we prove ϕ by induction on k using in the successor case the

IH.

◮ Both proofs amount the same, but the second one would be far more

difficult to teach and cumbersome to use.

Anton Setzer Coinductive Reasoning 10/ 65

slide-11
SLIDE 11

Motivation

Desired Coinductive Proof

1 2 · · · ∗

◮ We show ∀n ∈ N.∗ ∼ n by coinduction on ∼.

◮ Assume ∗ −

→ x. We need to find y s.t. n − → y and x ∼ y. Choose y = n + 1. By co-IH ∗ ∼ n + 1.

◮ Assume n −

→ y. We need to find x s.t. ∗ − → x and x ∼ y. Choose x = ∗. By co-IH ∗ ∼ n + 1.

◮ In essence same proof, but hopefully easier to teach and use.

Anton Setzer Coinductive Reasoning 11/ 65

slide-12
SLIDE 12

Motivation

Desired Coinductive Proof for Streams

◮ Consider Stream : Set given coinductively by

head : Stream → N , tail : Stream → Stream .

◮ Consider 3 versions of the stream n, n + 1, n + 2, . . .

inc, inc′, inc′′ : N → Stream head(inc(n)) = head(inc′(n)) = head(inc′′(n)) = n tail(inc(n)) = inc(n + 1) tail(inc′(n)) = inc′′(n + 1) tail(inc′′(n)) = inc′(n + 1)

Anton Setzer Coinductive Reasoning 12/ 65

slide-13
SLIDE 13

Motivation

Desired Coinductive Proof for Streams

◮ We show

∀n ∈ N.inc(n) = inc′(n) ∧ inc(n) = inc′′(n) by coinduction on Stream.

◮ head(inc(n)) = n = head(inc′(n)) = head(inc′′(n)) ◮ tail(inc(n)) = inc(n + 1)

co−IH

= inc′′(n + 1) = tail(inc′(n))

◮ tail(inc(n)) = inc(n + 1)

co−IH

= inc′(n + 1) = tail(inc′′(n))

Anton Setzer Coinductive Reasoning 13/ 65

slide-14
SLIDE 14

Motivation

Goal

◮ Identify the precised dual of iteration, primitive recursion, induction. ◮ Identify the correct use of co-IH. ◮ Use of coalgebras as defined by their elimination rules. ◮ Generalise to indexed coinductively defined sets.

Anton Setzer Coinductive Reasoning 14/ 65

slide-15
SLIDE 15

(Co)Iteration – (Co)Recursion – (Co)Induction

Motivation (Co)Iteration – (Co)Recursion – (Co)Induction Generalisation (Petersson-Synek Trees) Schemata for Corecursive Definitions and Coinductive Proofs Objects Conclusion Bibliography

Anton Setzer Coinductive Reasoning 15/ 65

slide-16
SLIDE 16

(Co)Iteration – (Co)Recursion – (Co)Induction

Introduction/Elimination of Inductive/Coinductive Sets

◮ Introduction rules for Natural numbers means that we have

0 ∈ N S : N → N so we have an N-algebra (N, 0, S) ∈ (X ∈ Set) × X × (X → X)

◮ Dually, coinductive sets are given by their elimination rules i.e. by

  • bservations or eliminators.

As an example we consider Stream: head : Stream → N tail : Stream → Stream We obtain a Stream-coalgebra (Stream, head, tail) ∈ (X ∈ Set) × (X → N) × (X → X)

Anton Setzer Coinductive Reasoning 16/ 65

slide-17
SLIDE 17

(Co)Iteration – (Co)Recursion – (Co)Induction

Problem of Defining Coalgebras by their Introduction Rules

◮ Commonly one defines coalgebras by their introduction rules:

Stream is the largest set closed under cons : Stream × N → Stream

◮ Problem:

◮ In set theory cons cannot be defined as a constructor such as

cons(n, s) := ⌈cons⌉, n, s as for inductively defined sets, since we would need non-well-founded sets. We can define a set Stream closed under a function cons, but that’s no longer the same operation one would use for defining a corresponding inductively defined set.

◮ In a term model we obtain non-normalisation:

We get elements such as zerostream := cons(0, cons(0, cons(0, · · · ))) ∈ Stream

Anton Setzer Coinductive Reasoning 17/ 65

slide-18
SLIDE 18

(Co)Iteration – (Co)Recursion – (Co)Induction

Problem of Defining Coalgebras by their Introduction Rules

◮ If we define Stream by its elimination rules, problems vanish:

◮ In set theory Stream is a set which allows operations

head : Stream → N, tail : Stream → Set. For instance we can take Stream := N → N head(f ) := f (0) tail(f ) := f ◦ S and obtain a largest set in the sense given below.

◮ In a term model we can define the streams as the largest set which

allows to define head and tail. zerostream can be a term such that head(zerostream) − → 0, tail(zerostream) − → zerostream. zerostream itself is in normal form.

◮ In both cases cons can now be defined by the principle of coiteration.

Anton Setzer Coinductive Reasoning 18/ 65

slide-19
SLIDE 19

(Co)Iteration – (Co)Recursion – (Co)Induction

Unique Iteration

◮ That (N, 0, S) are minimal can be given by:

◮ Assume another N-algebra (X, z, s), i.e.

z ∈ X s : X → X

◮ Then there exist a unique homomorphism g : (N, 0, S) → (X, z, s),

i.e. g : N → X g(0) = z g(S(n)) = s(g(n))

◮ This is the same as saying N is an initial FN-algebra. ◮ This means we can define uniquely

g : N → X g(0) = x for some x ∈ X g(S(n)) = x′ for some x′ ∈ X depending on g(n)

◮ This is the principle of unique iteration. ◮ Definition by pattern matching. Anton Setzer Coinductive Reasoning 19/ 65

slide-20
SLIDE 20

(Co)Iteration – (Co)Recursion – (Co)Induction

Unique Coiteration

◮ Dually, that (Stream, head, tail) is maximal can be given by:

◮ Assume another Stream-coalgebra (X, h, t):

h : X → N t : X → X

◮ Then there exist a unique homomorphism

g : (X, h, t) → (Stream, head, tail), i.e.: g : X → Stream head(g(x)) = h(x) tail(g(x)) = g(t(x))

◮ Means we can define uniquely

g : X → Stream head(g(x)) = n for some n ∈ N depending on x tail(g(x)) = g(x′) for some x′ ∈ X depending on x This is the principle of unique coiteration.

◮ Definition by copattern matching.

Anton Setzer Coinductive Reasoning 20/ 65

slide-21
SLIDE 21

(Co)Iteration – (Co)Recursion – (Co)Induction

Comparison

◮ When using iteration the instance of g we can use is restricted, but we

can apply an arbitrary function to it.

◮ When using coiteration we can choose any instance a of g, but cannot

apply any function to g(a).

Anton Setzer Coinductive Reasoning 21/ 65

slide-22
SLIDE 22

(Co)Iteration – (Co)Recursion – (Co)Induction

Duality

Inductive Definition Coinductive Definition Determined by Introduction Determined by Observation/Elimination Iteration Coiteration Pattern matching Copattern matching Primitive Recursion ? Induction ? Induction-Hypothesis ?

1

1Part of this table is due to Peter Hancock, see acknowledgements at the end. Anton Setzer Coinductive Reasoning 22/ 65

slide-23
SLIDE 23

(Co)Iteration – (Co)Recursion – (Co)Induction

Unique Primitive Recursion

◮ From unique iteration for N we can derive principle of

unique primitive recursion

◮ We can define uniquely

g : N → X g(0) = x for some x ∈ X g(S(n)) = x′ for some x′ ∈ X depending on n, g(n)

Anton Setzer Coinductive Reasoning 23/ 65

slide-24
SLIDE 24

(Co)Iteration – (Co)Recursion – (Co)Induction

Unique Primitive Corecursion

◮ From unique coiteration we can derive principle of

unique primitive corecursion

◮ We can define uniquely

g : X → Stream head(g(x)) = n for some n ∈ N depending on x tail(g(x))) = g(x′) for some x′ ∈ X depending on x

  • r

= s for some s ∈ Stream depending on x

Anton Setzer Coinductive Reasoning 24/ 65

slide-25
SLIDE 25

(Co)Iteration – (Co)Recursion – (Co)Induction

Duality

◮ For primitive recursion we could make use of the pair (n, g(n))

consisting of n and the IH, i.e. an element of N × X

◮ For primitive corecursion we can make use of either s ∈ Stream or

g(x′), i.e. of an element of Stream + X

◮ + is the dual of ×.

Anton Setzer Coinductive Reasoning 25/ 65

slide-26
SLIDE 26

(Co)Iteration – (Co)Recursion – (Co)Induction

Duality

Inductive Definition Coinductive Definition Determined by Introduction Determined by Observation/Elimination Iteration Coiteration Pattern matching Copattern matching Primitive Recursion Primitive Corecursion Induction ? Induction-Hypothesis ?

Anton Setzer Coinductive Reasoning 26/ 65

slide-27
SLIDE 27

(Co)Iteration – (Co)Recursion – (Co)Induction

Example

s ∈ Stream head(s) = tail(s) = s s′ : N → Stream head(s′(n)) = tail(s′(n)) = s′(n + 1) cons : (N × Stream) → Stream head(cons(n, s)) = n tail(cons(n, s)) = s

Anton Setzer Coinductive Reasoning 27/ 65

slide-28
SLIDE 28

(Co)Iteration – (Co)Recursion – (Co)Induction

Induction

◮ From unique iteration one can derive principle of induction:

We can prove ∀n ∈ N.ϕ(n) by proving ϕ(0) ∀n ∈ N.ϕ(n) → ϕ(S(n))

◮ Using induction we can prove (assuming extensionality of functions)

uniqueness of iteration and primitive recursion.

Anton Setzer Coinductive Reasoning 28/ 65

slide-29
SLIDE 29

(Co)Iteration – (Co)Recursion – (Co)Induction

Equivalence

Theorem

Let (N, 0, S) be an N-algebra. The following is equivalent

  • 1. The principle of unique iteration.
  • 2. The principle of unique primitive recursion.
  • 3. The principle of iteration + induction.
  • 4. The principle of primitive recursion + induction.

Anton Setzer Coinductive Reasoning 29/ 65

slide-30
SLIDE 30

(Co)Iteration – (Co)Recursion – (Co)Induction

Coinduction

◮ Uniqueness in coiteration is equivalent to the principle:

Bisimulation implies equality

◮ Bisimulation on Stream is the largest relation ∼ on Stream s.t.

s ∼ s′ → head(s) = head(s′) ∧ tail(s) ∼ tail(s′)

◮ Largest can be expressed as ∼ being an indexed coinductively defined

set.

◮ Primitive corecursion over ∼ means:

We can prove ∀s, s′.X(s, s′) → s ∼ s′ by showing X(s, s′) → head(s) = head(s′) X(s, s′) → X(tail(s), tail(s′)) ∨ tail(s) ∼ tail(s′)

Anton Setzer Coinductive Reasoning 30/ 65

slide-31
SLIDE 31

(Co)Iteration – (Co)Recursion – (Co)Induction

Coinduction

◮ Combining

◮ bisimulation implies equality ◮ bisimulation can be shown corecursively

we obtain the following principle of coinduction

Anton Setzer Coinductive Reasoning 31/ 65

slide-32
SLIDE 32

(Co)Iteration – (Co)Recursion – (Co)Induction

Schema of Coinduction

◮ We can prove

∀s, s′.X(s, s′) → s = s′ by showing ∀s, s′.X(s, s′) → head(s) = head(s′) ∀s, s′.X(s, s′) → tail(s) = tail(s′) where tail(s) = tail(s′) can be derived

◮ directly or ◮ from a proof of

X(tail(s), tail(s′)) invoking the co-induction-hypothesis X(tail(s), tail(s′)) → tail(s) = tail(s′)

◮ Note: Only direct use of co-IH allowed.

Anton Setzer Coinductive Reasoning 32/ 65

slide-33
SLIDE 33

(Co)Iteration – (Co)Recursion – (Co)Induction

Equivalence

Theorem

Let (Stream, head, tail) be a Stream-coalgebra. The following is equivalent

  • 1. The principle of unique coiteration.
  • 2. The principle of unique primitive corecursion.
  • 3. The principle of coiteration + coinduction
  • 4. The principle of primitive corecursion + coinduction

Anton Setzer Coinductive Reasoning 33/ 65

slide-34
SLIDE 34

(Co)Iteration – (Co)Recursion – (Co)Induction

Duality

Inductive Definition Coinductive Definition Determined by Introduction Determined by Observation/Elimination Iteration Coiteration Pattern matching Copattern matching Primitive Recursion Primitive Corecursion Induction Coinduction Induction-Hypothesis Coinduction-Hypothesis

Anton Setzer Coinductive Reasoning 34/ 65

slide-35
SLIDE 35

Generalisation (Petersson-Synek Trees)

Motivation (Co)Iteration – (Co)Recursion – (Co)Induction Generalisation (Petersson-Synek Trees) Schemata for Corecursive Definitions and Coinductive Proofs Objects Conclusion Bibliography

Anton Setzer Coinductive Reasoning 35/ 65

slide-36
SLIDE 36

Generalisation (Petersson-Synek Trees)

General Strictly Positive Indexed Inductive Definitions

◮ Strictly positive indexed inductively defined sets over index set I are

collection of sets D : I → Set closed under constructors Cj : (x1 ∈ A1) × (x2 ∈ A2(x1)) × · · · × (xn ∈ An(x1, . . . , xn−1)) → D(i(x1, . . . , xn))

◮ Here Ak(

x) is

◮ either a non-inductive argument, i.e. a set independent of A, ◮ or it is an inductive argument, i.e.

Ak( x) = (b ∈ B( x)) → D(i′

k(

x, b))

◮ Later arguments cannot depend on inductive arguments, only on

non-inductive arguments.

Anton Setzer Coinductive Reasoning 36/ 65

slide-37
SLIDE 37

Generalisation (Petersson-Synek Trees)

Simplification

◮ Therefore we can move the inductive arguments to the end

( x := x1, . . . , xk) Cj : (x1 ∈ A1) × (x2 ∈ A2(x1)) × · · · × xk ∈ Ak(x1, . . . , xk−1)

  • non-inductive arguments

× (b ∈ B1( x)) → D(i′

1(

x, b)) × · · · × (b ∈ Bl( x)) → D(i′

l(

x, b))

  • inductive arguments

) → D(ij( x))

Anton Setzer Coinductive Reasoning 37/ 65

slide-38
SLIDE 38

Generalisation (Petersson-Synek Trees)

Simplification

Cj : (x1 ∈ A1) × (x2 ∈ A2(x1)) × · · · × xk ∈ Ak(x1, . . . , xk−1)

  • non-inductive arguments

× (b ∈ B1( x)) → D(i′

1(

x, b)) × · · · × (b ∈ Bl( x)) → D(i′

l(

x, b))

  • inductive arguments

) → A(ij( x))

◮ We can form now the product of the non-inductive arguments and

  • btain a single non-inductive argument.

◮ We can replace the inductive arguments by one non-inductive

argument (b ∈ (B1( x) + · · · + Bl( x))) → D(i′′( x, b)) for some i′′.

Anton Setzer Coinductive Reasoning 38/ 65

slide-39
SLIDE 39

Generalisation (Petersson-Synek Trees)

Simplification

◮ We obtain for some new sets Aj, Bj(x) and function j, i

Cj : ((a ∈ Aj) × ((b ∈ Bj(a)) → D(j(a, b)))) → D(i(a))

◮ We can replace all constructors C1, . . . , Cn by one constructor C by

adding an additional argument j ∈ {1, . . . , n} selecting the constructor, and then combine it with the non-inductive argument.

◮ So we have one constructor

C : ((a ∈ A) × ((b ∈ B(a)) → D(j(a, b)))) → D(i(a))

Anton Setzer Coinductive Reasoning 39/ 65

slide-40
SLIDE 40

Generalisation (Petersson-Synek Trees)

Restricted Indexed (Co)Inductively Defined Sets

C : ((a ∈ A) × ((b ∈ B(a)) → D(j(a, b)))) → D(i(a))

◮ In order to obtain the corresponding observations/eliminators for the

corresponding co-inductive definitions, we need to invert the arrows.

◮ The more natural dual is obtained if we use restricted indexed

inductive definitions: C : (i ∈ I) → ((a ∈ A(i)) × ((b ∈ B(i, a)) → D(j(i, a, b)))) → D(i)

◮ The corresponding observations/eliminators are

E : (i ∈ I) → D(i) → ((a ∈ A(i)) × ((b ∈ B(i, a)) → D(j(i, a, b))))

  • r

E : ((i ∈ I) × D(i)) → ((a ∈ A(i)) × ((b ∈ B(i, a)) → D(j(i, a, b))))

Anton Setzer Coinductive Reasoning 40/ 65

slide-41
SLIDE 41

Generalisation (Petersson-Synek Trees)

Petersson-Synek Trees

◮ D(i) form the Petersson-Synek trees (observation by Hancock), which

correspond as well to the containers by Abbott, Altenkirch and Ghani.

◮ Replacing D by the more meaningful name Tree we obtain

data Tree : I → Set where C : ((i ∈ I)× (a ∈ A(i)) × ((b ∈ B(i, a)) → Tree(j(i, a, b)))) → Tree(i)

◮ For the corresponding coinductive defined set Tree∞ we divide E into

its two components and obtain coalg Tree∞ : I → Set where E1 : ((i ∈ I) × Tree∞(i)) → A(i) E2 : ((i ∈ I) × (t ∈ Tree∞(i)) × (b ∈ B(i, E1(i, t)))) → Tree∞(j(i, E1(i, t), b))

Anton Setzer Coinductive Reasoning 41/ 65

slide-42
SLIDE 42

Generalisation (Petersson-Synek Trees)

Petersson-Synek Trees

  • t ∈ Tree(i)

a ∈ A(i) (b ∈ B(i, a)) t′ ∈ Tree(i′) (i′ = i′(i, a, b)) a′ ∈ A(i′) (b′ ∈ B(i′, a′)) t′′ ∈ Tree(i′′) (i′′ = i(i′, a′, b′)) a′′ ∈ A(i′′)

Anton Setzer Coinductive Reasoning 42/ 65

slide-43
SLIDE 43

Generalisation (Petersson-Synek Trees)

Equivalence of unique (Co)induction, (Co)recursion, (Co)induction

◮ The notions of (co)iteration, primitive (co)recursion, (co)induction can

be generalised in a straightforward way to Petersson-Synek Trees and Co-Trees.

◮ One can show the equivalence of

◮ unique iteration, unique primitive recursion, iteration + induction,

primitive recursion + induction

◮ unique coiteration, unique primitive corecursion, coiteration +

coinduction, primitive corecursion + coinduction

◮ We call Petersson-Synek algebras fulfilling unique iteration initial

Petersson-Synek algebras.

◮ We call Petersson-Synek coalgebras fulfilling unique coiteration final

Petersson-Synek coalgebras.

Anton Setzer Coinductive Reasoning 43/ 65

slide-44
SLIDE 44

Generalisation (Petersson-Synek Trees)

Concrete Model of Tree∞

◮ Tree can be modelled in a straightforward way set theoretically. ◮ A very concrete model of Tree∞ can be defined by following the

principle that a coalgebra is given by its observations.

◮ The result of E1 can be observed directly. ◮ The result of E2 is an element of Tree∞(i′) for some i′ which can be

  • bserved by carrying out more observations.

Anton Setzer Coinductive Reasoning 44/ 65

slide-45
SLIDE 45

Generalisation (Petersson-Synek Trees)

Concrete Model of Tree∞

◮ Let for i ∈ I

Path[[ Tree∞ ]](i) := {i0, a0, b0, i1, a1, b1, . . . , in, an | n ≥ 0, i0 = i, (∀k ∈ {0, . . . , n − 1}.bk ∈ B(ik, ak)∧ ik+1 = j(ik, ak, bk)), ∀k ∈ {0, . . . , n}.ak ∈ A(ik)}

◮ Let [[ Tree∞ ]](i) be the set of t ⊆ Path[[ Tree∞ ]](i) which form the set

  • f paths of a tree:

◮ i0, a0, b0, . . . , in+1, an+1 ∈ t → i0, a0, b0, . . . , in, an ∈ t ◮ ∃!a.i, a ∈ t, ◮ i0, a0, b0, . . . , in, an ∈ t ∧ bn ∈ B(in, an) ∧ in+1 = j(in, an, bn)

→ ∃!an+1.i0, a0, b0, . . . , in, an, bn, in+1, an+1 ∈ t

Anton Setzer Coinductive Reasoning 45/ 65

slide-46
SLIDE 46

Generalisation (Petersson-Synek Trees)

Concrete Model of Tree∞

◮ Define

E1 : (i ∈ I) → [[ Tree∞ ]](i) → A(i) E1(i, t) := a if i, a ∈ t

◮ Define

E2 : ((i ∈ I) → (t ∈ [[ Tree∞ ]](i)) → (b ∈ B(i, E1(i, t))) → [[ Tree∞ ]](j(i, E1(i, t), b)) E2(i, t, b) := {i1, a1, b1, . . . , in+1, an+1 | i, E1(i, t), b, i1, a1, b1, . . . , in+1, an+1 ∈ t}

Anton Setzer Coinductive Reasoning 46/ 65

slide-47
SLIDE 47

Generalisation (Petersson-Synek Trees)

Concrete Model of Tree∞

Theorem

([[ Tree∞ ]], E1, E2) is a final Tree∞-coalgebra.

Anton Setzer Coinductive Reasoning 47/ 65

slide-48
SLIDE 48

Schemata for Corecursive Definitions and Coinductive Proofs

Motivation (Co)Iteration – (Co)Recursion – (Co)Induction Generalisation (Petersson-Synek Trees) Schemata for Corecursive Definitions and Coinductive Proofs Objects Conclusion Bibliography

Anton Setzer Coinductive Reasoning 48/ 65

slide-49
SLIDE 49

Schemata for Corecursive Definitions and Coinductive Proofs

Schema for Primitive Corecursion

◮ Assume A : I → Set, [[ Tree∞ ]], E1, E2 as before. We can define a

function f : (i ∈ I) → X(i) → [[ Tree∞ ]](i) corecursively by defining for i ∈ I, x ∈ X(i)

◮ a value a′ := E1(i, f (i, x)) ∈ A(i) ◮ and for b ∈ B(i, a) a value E2(i, f (i, x), b) ∈ [[ Tree∞ ]](i′, b)

where i′ := j(i, a′, b) and we can define E2(i, f (i, x), b)

◮ as an element of [

[ Tree∞ ]](i′) defined before

◮ or corecursively define E2(i, f (i, x), b) = f (i′, x′)

for some x′ ∈ X(i′). Here f (i′, x′) will be called the corecursion hypothesis.

Anton Setzer Coinductive Reasoning 49/ 65

slide-50
SLIDE 50

Schemata for Corecursive Definitions and Coinductive Proofs

Example

◮ Define the set of increasing streams IncStream : N → Set starting

with at least n coinductively by head : (n : N) → IncStream(n) → N≥n tail : (n : N) → (s : IncStream(n)) → IncStream(head(n, s) + 1) where N≥n := {m : N | m ≥ n}. Define inc, inc′, inc′′ : (n : N) → IncStream(n) head(n, inc(n)) = head(n, inc′(n)) = head(n, inc′′(n)) = n tail(n, inc(n)) = inc(n + 1) tail(n, inc′(n)) = inc′′(n + 1) tail(n, inc′′(n)) = inc′(n + 1)

Anton Setzer Coinductive Reasoning 50/ 65

slide-51
SLIDE 51

Schemata for Corecursive Definitions and Coinductive Proofs

Schema for Indexed Corecursively Defined Functions

◮ Assume X ∈ Set,

j : X → I. We can define f : (x ∈ X) → [[ Tree∞ ]]( j(x)) corecursively by determining for x ∈ X with i := j(x),

◮ a := E1(i, f (x)) ∈ A(i) ◮ and for b ∈ B(i, a) with i′ := j(i, a, b) the value

E2(i, f (x), b) ∈ [[ Tree∞ ]](i′) where we can define E2(i, f (x), b) as

◮ a previously defined value of [

[ Tree∞ ]](i′)

◮ or corecursively define E2(i, f (x), b) = f (x′) for some x′ such that

  • j(x′) = i′.

f (x′) will be called the corecursion hypothesis.

Anton Setzer Coinductive Reasoning 51/ 65

slide-52
SLIDE 52

Schemata for Corecursive Definitions and Coinductive Proofs

Example

◮ Define Stack : N → Set coinductively with destructors

top : ((n ∈ N) × (n > 0) × Stack(n)) → N pop : ((n ∈ N) × (n > 0) × Stack(n)) → Stack(n − 1)

◮ We can define empty : Stack(0), where we do not need to define

anything since 0 > 0 = ∅.

◮ We can define

push : (n, m ∈ N) → Stack(n) → Stack(n + 1) s.t. top(n + 1, ∗, push(n, m, s)) = m pop(n + 1, ∗, push(n, m, s)) = s

Anton Setzer Coinductive Reasoning 52/ 65

slide-53
SLIDE 53

Schemata for Corecursive Definitions and Coinductive Proofs

Schema for Coinduction

◮ Assume

J : Set

  • i

: J → I x0, x1 : (j ∈ J) → [[ Tree∞ ]]( i(j)) We can show ∀j ∈ J.x0(j) = x0(j′) coinductively by showing

◮ E0(

i(j), x0(j)) and E0( i(j), x1(j)) are equal

◮ and for all b that

E1( i(j), x0(j), b) and E1( i(j), x0(j), b) are equal, where we can use either the fact that

◮ this was shown before, ◮ or we can use the coinduction-hypothesis, which means using the fact

E1( i(j), x0(j), b) = x0(j′) and E1( i(j), x1(j), b) = x1(j′) for some j′ ∈ J.

Anton Setzer Coinductive Reasoning 53/ 65

slide-54
SLIDE 54

Schemata for Corecursive Definitions and Coinductive Proofs

Example

◮ Let

s ∈ Stream head(s) = tail(s) = s s′ : N → Stream head(s′(n)) = tail(s′(n)) = s′(n + 1) cons : N → Stream → Stream head(cons(n, s)) = n tail(cons(n, s)) = s

◮ We show ∀n ∈ N.s = s′(n) by coinduction:

Assume n ∈ N. head(s) = head(s′(n)) and tail(s) = s = s′(n + 1) = tail(s′(n)), where s = s′(n + 1) follows by the coinduction hypothesis.

◮ We show cons(0, s) = s by coinduction:

head(cons(0, s)) = 0 = head(s) and tail(cons(0, s)) = s = tail(s), where we did not use the coinduction hypothesis.

Anton Setzer Coinductive Reasoning 54/ 65

slide-55
SLIDE 55

Schemata for Corecursive Definitions and Coinductive Proofs

Schema for Bisimulation on Labelled Transition Systems

◮ Bisimulation is an indexed coinductively defined relation and therefore

proofs of bisimulation can be shown by corecursion.

◮ Assume a labelled transition system with states S, labels L and a

relation − →⊆ S × L × S

Anton Setzer Coinductive Reasoning 55/ 65

slide-56
SLIDE 56

Schemata for Corecursive Definitions and Coinductive Proofs

Schema for Bisimulation on Labelled Transition Systems

◮ Let I ∈ Set, s, s′ : I → S. ◮ We can prove ∀i ∈ I.Bisim(s(i), s′(i)) coinductively by defining for

any i ∈ I

◮ for any l ∈ L, s0 ∈ S s.t. s(i)

l

− → s0 an s′

0 ∈ S s.t.

◮ s′(i) l

− → s′

◮ and s.t. Bisim(s0, s′ 0)

where one can for prove the latter by invoking the Coinduction Hypothesis Bisim(s(i′), s′(i′)) for some i′ such that s(i′) = s0, s′(i′) = s′

0. ◮ for any l ∈ L, s′

0 ∈ S s.t. s′(i) l

− → s′

0 an

s0 ∈ S s.t.

◮ s(i) l

− → s0

◮ and s.t. Bisim(s0, s′ 0)

where one can prove the latter by invoking the Coinduction Hypothesis Bisim(s(i′), s′(i′)) for some i′ such that s(i′) = s0, s′(i′) = s′

0. Anton Setzer Coinductive Reasoning 56/ 65

slide-57
SLIDE 57

Schemata for Corecursive Definitions and Coinductive Proofs

Example from Introduction

1 2 · · · ∗

◮ We show ∀n ∈ N.∗ ∼ n by coinduction on ∼.

◮ Assume ∗ −

→ x. We need to find y s.t. n − → y and x ∼ y. Choose y = n + 1. By co-IH ∗ ∼ n + 1.

◮ Assume n −

→ y. We need to find x s.t. ∗ − → x and x ∼ y. Choose x = ∗. By co-IH ∗ ∼ n + 1.

◮ In essence same proof, but hopefully easier to teach and use.

Anton Setzer Coinductive Reasoning 57/ 65

slide-58
SLIDE 58

Schemata for Corecursive Definitions and Coinductive Proofs

Generalisation

◮ The previous example can be generalised to arbitrary coinductively

defined relations.

Anton Setzer Coinductive Reasoning 58/ 65

slide-59
SLIDE 59

Objects

Motivation (Co)Iteration – (Co)Recursion – (Co)Induction Generalisation (Petersson-Synek Trees) Schemata for Corecursive Definitions and Coinductive Proofs Objects Conclusion Bibliography

Anton Setzer Coinductive Reasoning 59/ 65

slide-60
SLIDE 60

Objects

Object-Oriented/Based Programming

◮ Object-oriented (OO) programming is currently main programming

paradigm.

◮ OO programming has lots of components, we will here only look at

the notion of an object.

◮ The component of OO programming dealing with objects only is

called object-based programming.

Anton Setzer Coinductive Reasoning 60/ 65

slide-61
SLIDE 61

Objects

Example: cell in Java

class cell <A> { /∗ Instance Variable ∗/ A content; /∗ Constructor ∗/ cell (A s) { content = s; } /∗ Method put ∗/ public void put (A s) { content = s; } /∗ Method get ∗/ public A get () { return content; } }

Anton Setzer Coinductive Reasoning 61/ 65

slide-62
SLIDE 62

Objects

Modelling Methods as Objects

◮ The type of objects cell is modelled as

◮ a coalgebra Cell ◮ with observations being the methods.

◮ A method m with argument A and return type B is modelled as

  • bservation

m : Cell → A → B × Cell which for a cell and an argument A returns the return type and the updated cell.

◮ Return type void is modelled as the one element type Unit. ◮ Access to instant variables is not needed, since we can use get and put

for it.

◮ A constructor with argument A is modelled as a function defined by

guarded recursion cell : A → Cell

Anton Setzer Coinductive Reasoning 62/ 65

slide-63
SLIDE 63

Objects

Object as a Coalgebra

We define the cell using the notation we would like to have: coalg Cell (A : Set) where put : Cell A → A → (Unit × Cell A) get : Cell A → Unit → (A × Cell A) cell : {A : Set} → A → Cell A put (cell a) b = (unit , cell b) get (cell a) _ = (a , cell a)

◮ {A : Set} denotes a hidden argument which can be omitted and

inferred by the system.

Anton Setzer Coinductive Reasoning 63/ 65

slide-64
SLIDE 64

Objects

Object as a Coalgebra

◮ Official Agda code uses record instead of coalg

◮ in the fields one needs to add as argument the record being defined. ◮ there is a bit more generality which results in more beaurocracy.

record Cell (X : Set) : Set where coinductive field put : X → Unit × Cell X get : Unit → X × Cell X cell : {X : Set} → X → Cell X put (cell x) y = (unit , cell y) get (cell x) _ = (x , cell x)

◮ More details see Kyoto Talk.

Anton Setzer Coinductive Reasoning 64/ 65

slide-65
SLIDE 65

Conclusion

Conclusion

◮ Coiteration, primitive corecursion, coinduction are the duals

  • f iteration, primitive recursion, induction.

◮ In iteration/recursion/induction,

◮ the instances of the IH used are restricted, ◮ the result can be used in arbitrary functions and formulas.

◮ In coiteration/corecursion/coinduction,

◮ the instances of the co-IH are unrestricted, ◮ but the result can be only used only directly.

◮ General case of indexed coinductive definitions can be reduced

to Petersson-Synek Cotrees.

Anton Setzer Coinductive Reasoning 65/ 65

slide-66
SLIDE 66

Conclusion

Conclusion

◮ Schemata for primitive corecursion and coinduction. ◮ Schemata can be applied to indexed coinductively defined sets and

relations.

◮ Relations on coinductively defined sets seem to be often

coinductively defined indexed relations and can be shown by indexed corecursion which can be regarded as coinduction.

◮ Objects as in OOprogramming are naturally occurring

coalgebras.

◮ Objects are determined by their observations and can be defined

in a natural way in Agda

Anton Setzer Coinductive Reasoning 66/ 65

slide-67
SLIDE 67

Bibliography

References

◮ Most of this talk (on coalgebras) was based on [Set16]. ◮ Article on coalgebras in Martin-Löf Type Theory [Set12]. ◮ Copatterns: [APTS13, SAPT14]. ◮ Objects in Martin-Löf Type Theory:

◮ First article [Set07], ◮ Implementation in Agda [AAS16].

◮ Bashar’s talk on processes in Agda [IS16].

Anton Setzer Coinductive Reasoning 67/ 65

slide-68
SLIDE 68

Bibliography

Bibliography I

Andreas Abel, Stephan Adelsberger, and Anton Setzer. Interactive programming in Agda – objects and graphical user interfaces. To appear in Journal of Functional Programming. Preprint available at http://www.cs.swan.ac.uk/∼csetzer/articles/ooAgda.pdf, 2016. Andreas Abel, Brigitte Pientka, David Thibodeau, and Anton Setzer. Copatterns: Programming infinite structures by observations. In Roberto Giacobazzi and Radhia Cousot, editors, Proceedings of the 40th annual ACM SIGPLAN-SIGACT symposium on Principles of programming languages, POPL ’13, pages 27–38, New York, NY, USA,

  • 2013. ACM.

Anton Setzer Coinductive Reasoning 68/ 65

slide-69
SLIDE 69

Bibliography

Bibliography II

Bashar Igried and Anton Setzer. Programming with monadic CSP-style processes in dependent type theory. To appear in proceedings of TyDe 2016, Type-driven Development, preprint available from http://www.cs.swan.ac.uk/∼csetzer/articles/TyDe2016.pdf, 2016. Anton Setzer, Andreas Abel, Brigitte Pientka, and David Thibodeau. Unnesting of copatterns. In Gilles Dowek, editor, Rewriting and Typed Lambda Calculi. Proceedings RTA-TLCA 2014, volume 8560 of Lecture Notes in Computer Science, pages 31–45. Springer International Publishing, 2014.

Anton Setzer Coinductive Reasoning 69/ 65

slide-70
SLIDE 70

Bibliography

Bibliography III

Anton Setzer. Object-oriented programming in dependent type theory. In Henrik Nilsson, editor, Trends in Functional Programming Volume 7, pages 91 – 108, Bristol and Chicago, 2007. Intellect. Anton Setzer. Coalgebras as types determined by their elimination rules. In P. Dybjer, Sten Lindström, Erik Palmgren, and G. Sundholm, editors, Epistemology versus Ontology, volume 27 of Logic, Epistemology, and the Unity of Science, pages 351–369. Springer, Dordrecht, Heidelberg, New York, 2012. 10.1007/978-94-007-4435-6_16.

Anton Setzer Coinductive Reasoning 70/ 65

slide-71
SLIDE 71

Bibliography

Bibliography IV

Anton Setzer. How to reason coinductively informally. In Reinhard Kahle, Thomas Strahm, and Thomas Studer, editors, Advances in Proof Theory, pages 377–408. Springer, 2016.

Anton Setzer Coinductive Reasoning 71/ 65

slide-72
SLIDE 72

Bibliography

Switch over to Talk by Bashar

Anton Setzer Coinductive Reasoning 72/ 65