Debian Codegraph @

doc/README/Design/Decidability.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Examples of decision procedures and how to use them
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module README.Design.Decidability where
10
11 open import Data.Bool
12 open import Data.List.Base using (List; []; _∷_)
13 open import Data.List.Properties using (∷-injective)
14 open import Data.Nat
15 open import Data.Nat.Properties using (suc-injective)
16 open import Data.Product.Base using (uncurry)
17 open import Data.Unit
18 open import Function.Base using (id; _∘_)
19 open import Relation.Binary.PropositionalEquality
20 open import Relation.Nary
21
22 ------------------------------------------------------------------------
23 -- Reflects
24
25 open import Relation.Nullary.Reflects
26
27 infix 4 _≟₀_ _≟₁_ _≟₂_
28
29 -- A proof of `Reflects P b` shows that a proposition `P` has the truth value of
30 -- the boolean `b`. A proof of `Reflects P true` amounts to a proof of `P`, and
31 -- a proof of `Reflects P false` amounts to a refutation of `P`.
32
33 ex₀ : (n : ℕ) → Reflects (n ≡ n) true
34 ex₀ n = ofʸ refl
35
36 ex₁ : (n : ℕ) → Reflects (zero ≡ suc n) false
37 ex₁ n = ofⁿ λ ()
38
39 ex₂ : (b : Bool) → Reflects (T b) b
40 ex₂ false = ofⁿ id
41 ex₂ true = ofʸ tt
42
43 ------------------------------------------------------------------------
44 -- Dec
45
46 open import Relation.Nullary.Decidable
47
48 -- A proof of `Dec P` is a proof of `Reflects P b` for some `b`.
49 -- `Dec P` is declared as a record, with fields:
50 -- does : Bool
51 -- proof : Reflects P does
52
53 ex₃ : (b : Bool) → Dec (T b)
54 does (ex₃ b) = b
55 proof (ex₃ b) = ex₂ b
56
57 -- We also have pattern synonyms `yes` and `no`, allowing both fields to be
58 -- given at once.
59
60 ex₄ : (n : ℕ) → Dec (zero ≡ suc n)
61 ex₄ n = no λ ()
62
63 -- It is possible, but not ideal, to define recursive decision procedures using
64 -- only the `yes` and `no` patterns. The following procedure decides whether two
65 -- given natural numbers are equal.
66
67 _≟₀_ : (m n : ℕ) → Dec (m ≡ n)
68 zero ≟₀ zero = yes refl
69 zero ≟₀ suc n = no λ ()
70 suc m ≟₀ zero = no λ ()
71 suc m ≟₀ suc n with m ≟₀ n
72 ... | yes p = yes (cong suc p)
73 ... | no ¬p = no (¬p ∘ suc-injective)
74
75 -- In this case, we can see that `does (suc m ≟ suc n)` should be equal to
76 -- `does (m ≟ n)`, because a `yes` from `m ≟ n` gives rise to a `yes` from the
77 -- result, and similarly for `no`. However, in the above definition, this
78 -- equality does not hold definitionally, because we always do a case split
79 -- before returning a result. To avoid this, we can return the `does` part
80 -- separately, before any pattern matching.
81
82 _≟₁_ : (m n : ℕ) → Dec (m ≡ n)
83 zero ≟₁ zero = yes refl
84 zero ≟₁ suc n = no λ ()
85 suc m ≟₁ zero = no λ ()
86 does (suc m ≟₁ suc n) = does (m ≟₁ n)
87 proof (suc m ≟₁ suc n) with m ≟₁ n
88 ... | yes p = ofʸ (cong suc p)
89 ... | no ¬p = ofⁿ (¬p ∘ suc-injective)
90
91 -- We now get definitional equalities such as the following.
92
93 _ : (m n : ℕ) → does (5 + m ≟₁ 3 + n) ≡ does (2 + m ≟₁ n)
94 _ = λ m n → refl
95
96 -- Even better, from a maintainability point of view, is to use `map` or `map′`,
97 -- both of which capture the pattern of the `does` field remaining the same, but
98 -- the `proof` field being updated.
99
100 _≟₂_ : (m n : ℕ) → Dec (m ≡ n)
101 zero ≟₂ zero = yes refl
102 zero ≟₂ suc n = no λ ()
103 suc m ≟₂ zero = no λ ()
104 suc m ≟₂ suc n = map′ (cong suc) suc-injective (m ≟₂ n)
105
106 _ : (m n : ℕ) → does (5 + m ≟₂ 3 + n) ≡ does (2 + m ≟₂ n)
107 _ = λ m n → refl
108
109 -- `map′` can be used in conjunction with combinators such as `_⊎-dec_` and
110 -- `_×-dec_` to build complex (simply typed) decision procedures.
111
112 module ListDecEq₀ {a} {A : Set a} (_≟ᴬ_ : (x y : A) → Dec (x ≡ y)) where
113
114 _≟ᴸᴬ_ : (xs ys : List A) → Dec (xs ≡ ys)
115 [] ≟ᴸᴬ [] = yes refl
116 [] ≟ᴸᴬ (y ∷ ys) = no λ ()
117 (x ∷ xs) ≟ᴸᴬ [] = no λ ()
118 (x ∷ xs) ≟ᴸᴬ (y ∷ ys) =
119 map′ (uncurry (cong₂ _∷_)) ∷-injective (x ≟ᴬ y ×-dec xs ≟ᴸᴬ ys)
120
121 -- The final case says that `x ∷ xs ≡ y ∷ ys` exactly when `x ≡ y` *and*
122 -- `xs ≡ ys`. The proofs are updated by the first two arguments to `map′`.
123
124 -- In the case of ≡-equality tests, the pattern
125 -- `map′ (congₙ c) c-injective (x₀ ≟ y₀ ×-dec ... ×-dec xₙ₋₁ ≟ yₙ₋₁)`
126 -- is captured by `≟-mapₙ n c c-injective (x₀ ≟ y₀) ... (xₙ₋₁ ≟ yₙ₋₁)`.
127
128 module ListDecEq₁ {a} {A : Set a} (_≟ᴬ_ : (x y : A) → Dec (x ≡ y)) where
129
130 _≟ᴸᴬ_ : (xs ys : List A) → Dec (xs ≡ ys)
131 [] ≟ᴸᴬ [] = yes refl
132 [] ≟ᴸᴬ (y ∷ ys) = no λ ()
133 (x ∷ xs) ≟ᴸᴬ [] = no λ ()
134 (x ∷ xs) ≟ᴸᴬ (y ∷ ys) = ≟-mapₙ 2 _∷_ ∷-injective (x ≟ᴬ y) (xs ≟ᴸᴬ ys)