Debian Codegraph @

src/Relation/Nullary/Reflects.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- Properties of the `Reflects` construct
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Relation.Nullary.Reflects where
10
11 open import Agda.Builtin.Equality
12
13 open import Data.Bool.Base
14 open import Data.Sum.Base using (_⊎_; inj₁; inj₂)
15 open import Data.Product.Base using (_×_; _,_; proj₁; proj₂)
16 open import Data.Unit.Polymorphic.Base using (⊤)
17 open import Data.Empty.Polymorphic using (⊥)
18 open import Level using (Level)
19 open import Function.Base using (_$_; _∘_; const; id)
20 open import Relation.Nullary.Negation.Core
21 using (¬_; contradiction-irr; contradiction; _¬-⊎_)
22 open import Relation.Nullary.Recomputable as Recomputable using (Recomputable)
23
24 private
25 variable
26 a : Level
27 A B : Set a
28
29 ------------------------------------------------------------------------
30 -- `Reflects` idiom.
31
32 -- The truth value of A is reflected by a boolean value.
33 -- `Reflects A b` is equivalent to `if b then A else ¬ A`.
34
35 data Reflects (A : Set a) : Bool → Set a where
36 ofʸ : ( a : A) → Reflects A true
37 ofⁿ : (¬a : ¬ A) → Reflects A false
38
39 ------------------------------------------------------------------------
40 -- Constructors and destructors
41
42 -- These lemmas are intended to be used mostly when `b` is a value, so
43 -- that the `if` expressions have already been evaluated away.
44 -- In this case, `of` works like the relevant constructor (`ofⁿ` or
45 -- `ofʸ`), and `invert` strips off the constructor to just give either
46 -- the proof of `A` or the proof of `¬ A`.
47
48 of : ∀ {b} → if b then A else ¬ A → Reflects A b
49 of {b = false} ¬a = ofⁿ ¬a
50 of {b = true } a = ofʸ a
51
52 invert : ∀ {b} → Reflects A b → if b then A else ¬ A
53 invert (ofʸ a) = a
54 invert (ofⁿ ¬a) = ¬a
55
56 ------------------------------------------------------------------------
57 -- recompute
58
59 -- Given an irrelevant proof of a reflected type, a proof can
60 -- be recomputed and subsequently used in relevant contexts.
61
62 recompute : ∀ {b} → Reflects A b → Recomputable A
63 recompute (ofʸ a) _ = a
64 recompute (ofⁿ ¬a) a = contradiction-irr a ¬a
65
66 recompute-constant : ∀ {b} (r : Reflects A b) (p q : A) →
67 recompute r p ≡ recompute r q
68 recompute-constant = Recomputable.recompute-constant ∘ recompute
69
70 ------------------------------------------------------------------------
71 -- Interaction with true, false, negation, product, sums etc.
72
73 infixr 1 _⊎-reflects_
74 infixr 2 _×-reflects_ _→-reflects_
75
76 ⊥-reflects : Reflects (⊥ {a}) false
77 ⊥-reflects = of λ()
78
79 ⊤-reflects : Reflects (⊤ {a}) true
80 ⊤-reflects = of _
81
82 T-reflects : ∀ b → Reflects (T b) b
83 T-reflects true = of _
84 T-reflects false = of id
85
86 ¬-reflects : ∀ {b} → Reflects A b → Reflects (¬ A) (not b)
87 ¬-reflects (ofʸ a) = of (_$ a)
88 ¬-reflects (ofⁿ ¬a) = of ¬a
89
90 _×-reflects_ : ∀ {a b} → Reflects A a → Reflects B b →
91 Reflects (A × B) (a ∧ b)
92 ofʸ a ×-reflects ofʸ b = of (a , b)
93 ofʸ a ×-reflects ofⁿ ¬b = of (¬b ∘ proj₂)
94 ofⁿ ¬a ×-reflects _ = of (¬a ∘ proj₁)
95
96 _⊎-reflects_ : ∀ {a b} → Reflects A a → Reflects B b →
97 Reflects (A ⊎ B) (a ∨ b)
98 ofʸ a ⊎-reflects _ = of (inj₁ a)
99 ofⁿ ¬a ⊎-reflects ofʸ b = of (inj₂ b)
100 ofⁿ ¬a ⊎-reflects ofⁿ ¬b = of (¬a ¬-⊎ ¬b)
101
102 _→-reflects_ : ∀ {a b} → Reflects A a → Reflects B b →
103 Reflects (A → B) (not a ∨ b)
104 ofʸ a →-reflects ofʸ b = of (const b)
105 ofʸ a →-reflects ofⁿ ¬b = of (¬b ∘ (_$ a))
106 ofⁿ ¬a →-reflects _ = of (λ a → contradiction a ¬a)
107
108 ------------------------------------------------------------------------
109 -- Other lemmas
110
111 fromEquivalence : ∀ {b} → (T b → A) → (A → T b) → Reflects A b
112 fromEquivalence {b = true} sound complete = of (sound _)
113 fromEquivalence {b = false} sound complete = of complete
114
115 -- `Reflects` is deterministic.
116 det : ∀ {b b′} → Reflects A b → Reflects A b′ → b ≡ b′
117 det (ofʸ a) (ofʸ _) = refl
118 det (ofʸ a) (ofⁿ ¬a) = contradiction a ¬a
119 det (ofⁿ ¬a) (ofʸ a) = contradiction a ¬a
120 det (ofⁿ ¬a) (ofⁿ _) = refl
121
122 T-reflects-elim : ∀ {a b} → Reflects (T a) b → b ≡ a
123 T-reflects-elim {a} r = det r (T-reflects a)