Debian Codegraph @

src/Relation/Nullary/Universe.agda

raw
1 ------------------------------------------------------------------------
2 -- The Agda standard library
3 --
4 -- A universe of proposition functors, along with some properties
5 ------------------------------------------------------------------------
6
7 {-# OPTIONS --cubical-compatible --safe #-}
8
9 module Relation.Nullary.Universe where
10
11 open import Data.Sum.Base as Sum hiding (map)
12 open import Data.Sum.Relation.Binary.Pointwise using (_⊎ₛ_; inj₁; inj₂)
13 open import Data.Product.Base as Product hiding (map)
14 open import Data.Product.Relation.Binary.Pointwise.NonDependent using (_×ₛ_)
15 open import Data.Empty using (⊥)
16 open import Effect.Applicative using (RawApplicative)
17 open import Effect.Monad using (RawMonad)
18 open import Function.Base using (_∘_; id)
19 open import Function.Indexed.Relation.Binary.Equality using (≡-setoid)
20 open import Level using (Level; _⊔_; suc; Lift; lift; lower)
21 open import Relation.Nullary.Negation
22 using (¬_; contradiction; ¬¬-Monad; ¬¬-map; negated-stable)
23 open import Relation.Binary.Core using (Rel)
24 open import Relation.Binary.Bundles using (Setoid)
25 import Relation.Binary.Construct.Always as Always using (setoid)
26 open import Relation.Binary.PropositionalEquality.Core using (_≡_; refl)
27 import Relation.Binary.PropositionalEquality.Properties as PropEq
28 using (setoid)
29 import Relation.Binary.Indexed.Heterogeneous.Construct.Trivial
30 as Trivial using (indexedSetoid)
31
32 infix 5 ¬¬_
33 infixr 4 _⇒_
34 infixr 3 _∧_
35 infixr 2 _∨_
36 infix 1 ⟨_⟩_≈_
37
38 -- The universe.
39
40 data PropF p : Set (suc p) where
41 Id : PropF p
42 K : (P : Set p) → PropF p
43 _∨_ : (F₁ F₂ : PropF p) → PropF p
44 _∧_ : (F₁ F₂ : PropF p) → PropF p
45 _⇒_ : (P₁ : Set p) (F₂ : PropF p) → PropF p
46 ¬¬_ : (F : PropF p) → PropF p
47
48 -- Equalities for universe inhabitants.
49
50 mutual
51
52 setoid : ∀ {p} → PropF p → Set p → Setoid p p
53 setoid Id P = PropEq.setoid P
54 setoid (K P) _ = PropEq.setoid P
55 setoid (F₁ ∨ F₂) P = (setoid F₁ P) ⊎ₛ (setoid F₂ P)
56 setoid (F₁ ∧ F₂) P = (setoid F₁ P) ×ₛ (setoid F₂ P)
57 setoid (P₁ ⇒ F₂) P = ≡-setoid P₁
58 (Trivial.indexedSetoid (setoid F₂ P))
59 setoid (¬¬ F) P = Always.setoid (¬ ¬ ⟦ F ⟧ P) _
60
61 ⟦_⟧ : ∀ {p} → PropF p → (Set p → Set p)
62 ⟦ F ⟧ P = Setoid.Carrier (setoid F P)
63
64 ⟨_⟩_≈_ : ∀ {p} (F : PropF p) {P : Set p} → Rel (⟦ F ⟧ P) p
65 ⟨_⟩_≈_ F = Setoid._≈_ (setoid F _)
66
67 -- ⟦ F ⟧ is functorial.
68
69 map : ∀ {p} (F : PropF p) {P Q} → (P → Q) → ⟦ F ⟧ P → ⟦ F ⟧ Q
70 map Id f p = f p
71 map (K P) f p = p
72 map (F₁ ∨ F₂) f FP = Sum.map (map F₁ f) (map F₂ f) FP
73 map (F₁ ∧ F₂) f FP = Product.map (map F₁ f) (map F₂ f) FP
74 map (P₁ ⇒ F₂) f FP = map F₂ f ∘ FP
75 map (¬¬ F) f FP = ¬¬-map (map F f) FP
76
77 map-id : ∀ {p} (F : PropF p) {P} → ⟨ ⟦ F ⟧ P ⇒ F ⟩ map F id ≈ id
78 map-id Id x = refl
79 map-id (K P) x = refl
80 map-id (F₁ ∨ F₂) (inj₁ x) = inj₁ (map-id F₁ x)
81 map-id (F₁ ∨ F₂) (inj₂ y) = inj₂ (map-id F₂ y)
82 map-id (F₁ ∧ F₂) (x , y) = (map-id F₁ x , map-id F₂ y)
83 map-id (P₁ ⇒ F₂) f = λ x → map-id F₂ (f x)
84 map-id (¬¬ F) ¬¬x = _
85
86 map-∘ : ∀ {p} (F : PropF p) {P Q R} (f : Q → R) (g : P → Q) →
87 ⟨ ⟦ F ⟧ P ⇒ F ⟩ map F f ∘ map F g ≈ map F (f ∘ g)
88 map-∘ Id f g x = refl
89 map-∘ (K P) f g x = refl
90 map-∘ (F₁ ∨ F₂) f g (inj₁ x) = inj₁ (map-∘ F₁ f g x)
91 map-∘ (F₁ ∨ F₂) f g (inj₂ y) = inj₂ (map-∘ F₂ f g y)
92 map-∘ (F₁ ∧ F₂) f g x = (map-∘ F₁ f g (proj₁ x) ,
93 map-∘ F₂ f g (proj₂ x))
94 map-∘ (P₁ ⇒ F₂) f g h = λ x → map-∘ F₂ f g (h x)
95 map-∘ (¬¬ F) f g x = _
96
97 -- A variant of sequence can be implemented for ⟦ F ⟧.
98
99 sequence : ∀ {p AF} → RawApplicative AF →
100 (AF (Lift p ⊥) → ⊥) →
101 ({A B : Set p} → (A → AF B) → AF (A → B)) →
102 ∀ F {P} → ⟦ F ⟧ (AF P) → AF (⟦ F ⟧ P)
103 sequence {AF = AF} A extract-⊥ sequence-⇒ = helper
104 where
105 open RawApplicative A
106
107 helper : ∀ F {P} → ⟦ F ⟧ (AF P) → AF (⟦ F ⟧ P)
108 helper Id x = x
109 helper (K P) x = pure x
110 helper (F₁ ∨ F₂) (inj₁ x) = inj₁ <$> helper F₁ x
111 helper (F₁ ∨ F₂) (inj₂ y) = inj₂ <$> helper F₂ y
112 helper (F₁ ∧ F₂) (x , y) = _,_ <$> helper F₁ x ⊛ helper F₂ y
113 helper (P₁ ⇒ F₂) f = sequence-⇒ (helper F₂ ∘ f)
114 helper (¬¬ F) x =
115 pure (λ ¬FP → x (λ fp → extract-⊥ (lift ∘ ¬FP <$> helper F fp)))
116
117 -- Some lemmas about double negation.
118
119 private
120 open module M {a} = RawMonad (¬¬-Monad {a = a})
121
122 ¬¬-pull : ∀ {p} (F : PropF p) {P} →
123 ⟦ F ⟧ (¬ ¬ P) → ¬ ¬ ⟦ F ⟧ P
124 ¬¬-pull = sequence rawApplicative
125 (λ f → f lower)
126 (λ f g → g (λ x → contradiction (λ y → g (λ _ → y)) (f x)))
127
128 ¬¬-remove : ∀ {p} (F : PropF p) {P} →
129 ¬ ¬ ⟦ F ⟧ (¬ ¬ P) → ¬ ¬ ⟦ F ⟧ P
130 ¬¬-remove F = negated-stable ∘ ¬¬-pull (¬¬ F)