Coinduction

The corecursive definitions below are accepted if the option --guardedness is active:

  1. {-# OPTIONS --guardedness #-}

(An alternative approach is to use Sized Types.)

Coinductive Records

It is possible to define the type of infinite lists (or streams) of elements of some type A as follows:

  1. record Stream (A : Set) : Set where
  2. coinductive
  3. field
  4. hd : A
  5. tl : Stream A

As opposed to inductive record types, we have to introduce the keyword coinductive before defining the fields that constitute the record.

It is interesting to note that it is not necessary to give an explicit constructor to the record type Stream.

Now we can use copatterns to create Streams, like one that repeats a given element a infinitely many times:

  1. repeat : {A : Set} (a : A) -> Stream A
  2. hd (repeat a) = a
  3. tl (repeat a) = repeat a

We can also define pointwise equality (a bisimulation and an equivalence) of a pair of Streams as a coinductive record:

  1. record __ {A} (xs : Stream A) (ys : Stream A) : Set where
  2. coinductive
  3. field
  4. hd-≡ : hd xs hd ys
  5. tl-≈ : tl xs tl ys

Using copatterns we can define a pair of functions on Streams such that one returns the elements in the even positions and the other the elements in the odd positions:

  1. even : {A} Stream A Stream A
  2. hd (even xs) = hd xs
  3. tl (even xs) = even (tl (tl xs))
  4. odd : {A} Stream A Stream A
  5. odd xs = even (tl xs)
  6. split : {A} Stream A Stream A × Stream A
  7. split xs = even xs , odd xs

as well as a function that merges a pair of Streams by interleaving their elements:

  1. merge : {A} Stream A × Stream A Stream A
  2. hd (merge (xs , ys)) = hd xs
  3. tl (merge (xs , ys)) = merge (ys , tl xs)

Finally, we can prove that merge is a left inverse for split:

  1. merge-split-id : {A} (xs : Stream A) merge (split xs) xs
  2. hd-≡ (merge-split-id _) = refl
  3. tl-≈ (merge-split-id xs) = merge-split-id (tl xs)

Coinductive Record Constructors

It is possible to give an explicit constructor to coinductive record types like Stream:

  1. record Stream' (A : Set) : Set where
  2. coinductive
  3. constructor cons
  4. field
  5. hd : A
  6. tl : Stream' A

However, this constructor cannot be pattern-matched:

  1. -- Get the third element of a stream
  2. third : ∀{A} Stream' A → A
  3. -- Not allowed:
  4. -- third (cons _ (cons _ (cons x _))) = x

Instead, you can use the record fields as projections:

  1. third str = str .tl .tl .hd

The constructor can be used as usual in the right-hand side of definitions:

  1. -- Prepend a list to a stream
  2. prepend : ∀{A} List A Stream' A → Stream' A
  3. prepend [] str = str
  4. prepend (a as) str = cons a (prepend as str)

However, it doesn’t count as ‘guarding’ for the productivity checker:

  1. -- Make a stream with one element repeated forever
  2. cycle : ∀{A} A Stream' A
  3. -- Does not termination-check:
  4. -- cycle a = cons a (cycle a)

Instead, you can use copattern matching:

  1. cycle a .hd = a
  2. cycle a .tl = cycle a

It is also possible to use copattern-matching lambdas:

  1. cycle' : ∀{A} → A → Stream' A
  2. cycle' a = λ where
  3. .hd → a
  4. .tl → cycle' a

For more information on these restrictions, see this pull request, and this commit.

Old Coinduction

Note

This is the old way of coinduction support in Agda. You are advised to use Coinductive Records instead.

To use coinduction it is recommended that you import the module Coinduction from the standard library. Coinductive types can then be defined by labelling coinductive occurrences using the delay operator :

  1. data Co : Set where
  2. zero : Co
  3. suc : Co Co

The type ∞ A can be seen as a suspended computation of type A. It comes with delay and force functions:

  1. _ : {a} {A : Set a} A A
  2. : {a} {A : Set a} A A

Values of coinductive types can be constructed using corecursion, which does not need to terminate, but has to be productive. As an approximation to productivity the termination checker requires that corecursive definitions are guarded by coinductive constructors. As an example the infinite “natural number” can be defined as follows:

  1. inf : Co
  2. inf = suc (♯ inf)

The check for guarded corecursion is integrated with the check for size-change termination, thus allowing interesting combinations of inductive and coinductive types. We can for instance define the type of stream processors, along with some functions:

  1. -- Infinite streams.
  2. data Stream (A : Set) : Set where
  3. __ : (x : A) (xs : (Stream A)) Stream A
  4. -- A stream processor SP A B consumes elements of A and produces
  5. -- elements of B. It can only consume a finite number of As before
  6. -- producing a B.
  7. data SP (A B : Set) : Set where
  8. get : (f : A SP A B) SP A B
  9. put : (b : B) (sp : (SP A B)) SP A B
  10. -- The function eat is defined by an outer corecursion into Stream B
  11. -- and an inner recursion on SP A B.
  12. eat : {A B} SP A B Stream A Stream B
  13. eat (get f) (a as) = eat (f a) (♭ as)
  14. eat (put b sp) as = b eat (♭ sp) as
  15. -- Composition of stream processors.
  16. __ : {A B C} SP B C SP A B SP A C
  17. get f put x sp = f x sp
  18. put x sp sp = put x (♯ (♭ sp sp₂))
  19. sp get f = get x sp f x)

It is also possible to define “coinductive families”. It is recommended not to use the delay constructor (♯_) in a constructor’s index expressions. The following definition of equality between coinductive “natural numbers” is discouraged:

  1. data _≈’_ : Co Co Set where
  2. zero : zero ≈’ zero
  3. suc : {m n} (m ≈’ n) suc (♯ m) ≈’ suc (♯ n)

The recommended definition is the following one:

  1. data __ : Co Co Set where
  2. zero : zero zero
  3. suc : {m n} (♭ m n) suc m suc n