Debian Codegraph @

doc/README/Data/List/Relation/Binary/Equality.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Documentation for pointwise equality over `List`s
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --allow-unsolved-metas #-}
8
9 module README.Data.List.Relation.Binary.Equality where
10
11 open import Data.Nat using (ℕ; _+_; _<_; s≤s; z≤n; _*_; _∸_; _≤_)
12 open import Data.Nat.Properties as ℕ
13 open import Data.List.Base
14
15 ------------------------------------------------------------------------
16 -- Pointwise equality
17
18 -- There are many different options for what it means for two
19 -- different lists of type `List A` to be "equal". Here we will
20 -- consider "pointwise" equality that requires the lists to be the
21 -- same length and every pair of elements to be "equal".
22
23 -- The most basic option is simply to use propositional equality
24 -- `_≡_` over lists:
25
26 open import Relation.Binary.PropositionalEquality
27 using (_≡_; sym; refl)
28
29 lem₁ : 1 ∷ 2 ∷ 3 ∷ [] ≡ 1 ∷ 2 ∷ 3 ∷ []
30 lem₁ = refl
31
32 -- However propositional equality is only suitable when we want to
33 -- use propositional equality to compare the individual elements.
34 -- Although a contrived example, consider trying to prove the
35 -- equality of two lists of the type `List (ℕ → ℕ)`:
36
37 lem₂ : (λ x → 2 * x + 2) ∷ [] ≡ (λ x → 2 * (x + 1)) ∷ []
38 lem₂ = {!!}
39
40 -- In such a case it is impossible to prove the two lists equal with
41 -- refl as the two functions are not propositionally equal. In the
42 -- absence of postulating function extensionality (see README.Axioms),
43 -- the most common definition of function equality is to say that two
44 -- functions are equal if their outputs are always propositionally
45 -- equal for any input. This notion of function equality `_≗_` is
46 -- found in:
47
48 open import Relation.Binary.PropositionalEquality using (_≗_)
49
50 -- We now want to use the `Pointwise` relation to say that the two
51 -- lists are equal if their elements are pointwise equal with resepct
52 -- to `_≗_`. However instead of using the pointwise module directly
53 -- to write:
54
55 open import Data.List.Relation.Binary.Pointwise.Base using (Pointwise)
56
57 lem₃ : Pointwise _≗_ ((λ x → x + 1) ∷ []) ((λ x → x + 2 ∸ 1) ∷ [])
58 lem₃ = {!!}
59
60 -- the library provides some nicer wrappers and infix notation in the
61 -- folder "Data.List.Relation.Binary.Equality".
62
63 -- Within this folder there are four different modules.
64
65 import Data.List.Relation.Binary.Equality.Setoid as SetoidEq
66 import Data.List.Relation.Binary.Equality.DecSetoid as DecSetoidEq
67 import Data.List.Relation.Binary.Equality.Propositional as PropEq
68 import Data.List.Relation.Binary.Equality.DecPropositional as DecPropEq
69
70 -- Which one should be used depends on whether the underlying equality
71 -- over "A" is:
72 -- i) propositional or setoid-based
73 -- ii) decidable.
74
75 -- Each of the modules except `PropEq` are designed to be opened with a
76 -- module parameter. This is to avoid having to specify the underlying
77 -- equality relation or the decidability proofs every time you use the
78 -- list equality.
79
80 -- In our example function equality is not decidable and not propositional
81 -- and so we want to use the `SetoidEq` module. This requires a proof that
82 -- the `_≗_` relation forms a setoid over functions of the type `ℕ → ℕ`.
83 -- This is found in:
84
85 open import Relation.Binary.PropositionalEquality using (_→-setoid_)
86
87 -- The `SetoidEq` module should therefore be opened as follows:
88
89 open SetoidEq (ℕ →-setoid ℕ)
90
91 -- All four equality modules provide an infix operator `_≋_` for the
92 -- new equality relation over lists. The type of `lem₃` can therefore
93 -- be rewritten as:
94
95 lem₄ : (λ x → x + 1) ∷ [] ≋ (λ x → x + 2 ∸ 1) ∷ []
96 lem₄ = 2x+2≗2[x+1] ∷ []
97 where
98 2x+2≗2[x+1] : (λ x → x + 1) ≗ (λ x → x + 2 ∸ 1)
99 2x+2≗2[x+1] x = sym (+-∸-assoc x (s≤s z≤n))
100
101 -- The modules also provide proofs that the `_≋_` relation is a
102 -- setoid in its own right and therefore is reflexive, symmetric,
103 -- transitive:
104
105 lem₅ : (λ x → 2 * x + 2) ∷ [] ≋ (λ x → 2 * x + 2) ∷ []
106 lem₅ = ≋-refl
107
108 -- If we could prove that `_≗_` forms a `DecSetoid` then we could use
109 -- the module `DecSetoidEq` instead. This exports everything from
110 -- `SetoidEq` as well as the additional proof `_≋?_` that the list
111 -- equality is decidable.
112
113 -- This pattern of four modules for each of the four different types
114 -- of equality is repeated throughout the library (e.g. see the
115 -- `Membership`). Note that in this case the modules `PropEq` and
116 -- `DecPropEq` are not very useful as if two lists are pointwise
117 -- propositionally equal they are necessarily propositionally equal
118 -- (and vice-versa). There are proofs of this fact exported by
119 -- `PropEq` and `DecPropEq`. Although, these two types of list equality
120 -- are not very useful in practice, they are included for completeness's
121 -- sake.