Coinduccion y Correcursion
Coinduccion y Correcursion
Coinduccion y Correcursion
Abstract
where
import STAL (display)
A Question about map and filter
These outcomes are different. This is because the test (>4) yields a
different result after all numbers are increased by 1.
When we make sure that the test used in the filter takes this change
into account, we get the same answers:
Prelude> filter (>4) (map (+1) [1..10])
[5,6,7,8,9,10,11]
Prelude> map (+1) (filter ((>4).(+1)) [1..10])
[5,6,7,8,9,10,11]
Show that for every finite list xs :: [a], every function f :: a -> b,
every total predicate p :: b -> Bool the following holds:
filter p (map f xs) = map f (filter (p.f ) xs).
Note: a predicate p :: b -> Bool is total if for every object x :: b,
the application p x gives either true or false. In particular, for no
x :: b does p x give rise to an error message.
Solution
1. p (f x) = True
2. p (f x) = False.
In case (1) we have:
map
filter p (map f (x:xs)) = filter p (f x) : (map f (x:xs))
filter
= (f x) : (filter p (map f (x:xs)))
ih
= (f x) : (map f (filter (p.f ) xs))
map
= map f (x : (filter (p.f ) xs))
filter
= map f (filter (p.f ) (x:xs)).
Can you also find a formula for the number of moves of the disk of
size k during the transfer of a tower with disks of sizes 1, . . . , n, and
1 ≤ k ≤ n? Again, you should prove by mathematical induction that
your formula is correct.
Solution
data Peg = A | B | C
type Tower = ([Int], [Int], [Int])
There are six possible single moves from one peg to another:
transfer :: Peg -> Peg -> Peg -> Int -> Tower
-> [Tower]
transfer _ _ _ 0 tower = [tower]
transfer p q r n tower = transfer p r q (n-1) tower
++
transfer r q p (n-1)
(move p q tower’)
where tower’ = last (transfer p r q (n-1) tower)
It is assumed that all proposition letters are from a list P0, P1, . . .. Then
¬(P1 ∨ ¬P2) is represented as Neg (Disj (P 1) (Neg (P 2))),
and shown on the screen as ~(P1 v ~P2), and so on.
We define the list of subformulas of a formula as follows:
Proof.
2
If one proves a property of formulas by induction on the structure of
the formula, then the fact is used that every formula can be mapped
to a natural number that indicates its constructive complexity: 0 for
the atomic formulas, the maximum of rank(Φ) and rank(Ψ) plus 1 for
a conjunction Φ ∧ Ψ, and so on.
Analysing Sequences by Difference Analysis
Haskell implementation:
ones = 1 : ones
Such definitions are called corecursive definitions.
ones2 = iterate id 1
theNats :: [Integer]
theNats = iterate succ 0
theOdds :: [Integer]
theOdds = iterate (\ n -> n+2) 1
zipWith
In a picture:
[xn+1, xn+2, . . .
*
[(z x0 y0), (z x1 y1), . . . , (z xn yn),
[yn+1, yn+2, . . .
primes’ :: [Integer]
primes’ = sieve’ [2..]
Proving Properties of Corecursive Programs
How does one prove things about corecursive programs? E.g., how does
one prove that sieve and sieve’ compute the same stream result for
every stream argument? Proof by induction does not work here, for
there is no base case.
Comparing the observational behaviour of infinite objects
To show that two infinite objects x and y are equal, we show that they
exhibit the same behaviour, i.e. we show that x ∼ y. Such a proof is
called a proof by coinduction.
The general pattern of a proof by coinduction of x ∼ y, where x, y :: a,
is as follows. Define a relation R on objects of some set A with a ⊂ A.
Next, show that R is a bisimulation, with xRy.
This shows:
head
map f (iterate f x) −→ (f x) (1)
tail
map f (iterate f x) −→ map f x : (iterate f (f x)) (2)
head
iterate f (f x) −→ (f x) (3)
tail
iterate f (f x) −→ iterate f (f (f x)) (4)
The two observations we can perform on streams are head and tail.
Now (f x)∆a(f x), so (f x)R(f x). Thus, the bisimilarity require-
ments hold for head observations.
Also,
(map f x : (iterate f (f x))) S (iterate f (f (f x))),
by definition of S, so
(map f x : (iterate f (f x))) R (iterate f (f (f x))),
by the definition of R. Hence, the bisimilarity requirements hold for tail
observations.
This shows that R is a bisimimulation that connects map f (iterate f x)
and iterate f (f x).
Hence
(map f (iterate f x)) ∼ (iterate f (f x)).