Debian Codegraph @

src/Data/Nat/InfinitelyOften.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Definition of and lemmas related to "true infinitely often"
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Data.Nat.InfinitelyOften where
10
11 open import Data.Nat.Base using (ℕ; _≤_; _⊔_; _+_; suc; zero; s≤s)
12 open import Data.Nat.Properties
13 open import Data.Product.Base as Prod hiding (map)
14 open import Data.Sum.Base using (inj₁; inj₂; _⊎_)
15 open import Effect.Monad using (RawMonad)
16 open import Function.Base using (_∘_; id)
17 open import Level using (Level; 0ℓ)
18 open import Relation.Binary.PropositionalEquality.Core using (_≢_)
19 open import Relation.Nullary.Negation using (¬¬-Monad; call/cc)
20 open import Relation.Nullary.Negation.Core using (¬_; contradiction)
21 open import Relation.Unary using (Pred; _∪_; _⊆_)
22 open import Relation.Nullary.Negation.Core using (contradiction)
23
24 open RawMonad (¬¬-Monad {a = 0ℓ})
25
26 private
27 variable
28 ℓ : Level
29
30 infixr 1 _∪-Fin_
31
32 -- Only true finitely often.
33
34 Fin : Pred ℕ ℓ → Set ℓ
35 Fin P = ∃ λ i → ∀ j → i ≤ j → ¬ P j
36
37 -- A non-constructive definition of "true infinitely often".
38
39 Inf : Pred ℕ ℓ → Set ℓ
40 Inf P = ¬ Fin P
41
42 -- Fin is preserved by binary sums.
43
44 _∪-Fin_ : ∀ {ℓp ℓq P Q} → Fin {ℓp} P → Fin {ℓq} Q → Fin (P ∪ Q)
45 _∪-Fin_ {P = P} {Q} (i , ¬p) (j , ¬q) = (i ⊔ j , helper)
46 where
47 open ≤-Reasoning
48
49 helper : ∀ k → i ⊔ j ≤ k → ¬ (P ∪ Q) k
50 helper k i⊔j≤k (inj₁ p) = ¬p k (begin
51 i ≤⟨ m≤m⊔n i j ⟩
52 i ⊔ j ≤⟨ i⊔j≤k ⟩
53 k ∎) p
54 helper k i⊔j≤k (inj₂ q) = ¬q k (begin
55 j ≤⟨ m≤m⊔n j i ⟩
56 j ⊔ i ≡⟨ ⊔-comm j i ⟩
57 i ⊔ j ≤⟨ i⊔j≤k ⟩
58 k ∎) q
59
60 -- Inf commutes with binary sums (in the double-negation monad).
61
62 commutes-with-∪ : ∀ {P Q} → Inf (P ∪ Q) → ¬ ¬ (Inf P ⊎ Inf Q)
63 commutes-with-∪ p∪q =
64 call/cc λ ¬[p⊎q] →
65 (λ ¬p ¬q → contradiction (¬p ∪-Fin ¬q) p∪q)
66 <$> ¬[p⊎q] ∘ inj₁ ⊛ ¬[p⊎q] ∘ inj₂
67
68 -- Inf is functorial.
69
70 map : ∀ {ℓp ℓq P Q} → P ⊆ Q → Inf {ℓp} P → Inf {ℓq} Q
71 map P⊆Q ¬fin = ¬fin ∘ Prod.map id (λ fin j i≤j → fin j i≤j ∘ P⊆Q)
72
73 -- Inf is upwards closed.
74
75 up : ∀ {P} n → Inf {ℓ} P → Inf (P ∘ _+_ n)
76 up zero = id
77 up {P = P} (suc n) = up n ∘ up₁
78 where
79 up₁ : Inf P → Inf (P ∘ suc)
80 up₁ ¬fin (i , fin) = ¬fin (suc i , helper)
81 where
82 helper : ∀ j → 1 + i ≤ j → ¬ P j
83 helper ._ (s≤s i≤j) = fin _ i≤j
84
85 -- A witness.
86
87 witness : ∀ {P} → Inf {ℓ} P → ¬ ¬ ∃ P
88 witness ¬fin ¬p = ¬fin (0 , λ i _ Pi → ¬p (i , Pi))
89
90 -- Two different witnesses.
91
92 twoDifferentWitnesses
93 : ∀ {P} → Inf P → ¬ ¬ ∃₂ λ m n → m ≢ n × P m × P n
94 twoDifferentWitnesses inf =
95 witness inf >>= λ w₁ →
96 witness (up (1 + proj₁ w₁) inf) >>= λ w₂ →
97 pure (_ , _ , m≢1+m+n (proj₁ w₁) , proj₂ w₁ , proj₂ w₂)