~ubuntu-branches/ubuntu/raring/agda-stdlib/raring

« back to all changes in this revision

Viewing changes to src/Function/Inverse.agda

  • Committer: Bazaar Package Importer
  • Author(s): Iain Lane
  • Date: 2011-02-25 22:28:40 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110225222840-jt16gl302kca7h0g
Tags: 0.5-1~ubuntu1

* Upload to Ubuntu from Debian's VCS due to delays in unstable caused by
  the GHC 7 transition (LP: #725364)
* [ba20206] Make package architecture independent (Closes: #573807, #598708,
  #599958)
* [0fb0600] Standards-Version → 3.9.1, no changes required
* [d3f13b8] Update required Agda version to 2.2.8
* [cc1f5c8] Imported Upstream version 0.4
* [2c82171] Add watch file
* [9251e0b] Imported Upstream version 0.5
* [0518fa6] No longer need procps | hurd BD as we no longer have the ticker
* [daf2445] Don't use upstream's make install — handled ourselves by
  dh_install
* [1b86533] Update watchfile to point at new location
* [637f47d] Remove debian/gbp.conf as we are no longer building for exp
* [cc88671] Require Agda 2.2.10
* [e99dab5] Set maximum stack size to 1G to prevent overflows in the build
* [251cd1d] Run the test suite manually
* [a7db697] Set the variable in the emacs loading script properly

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
------------------------------------------------------------------------
 
2
-- Inverses
 
3
------------------------------------------------------------------------
 
4
 
 
5
{-# OPTIONS --universe-polymorphism #-}
 
6
 
 
7
module Function.Inverse where
 
8
 
 
9
open import Level
 
10
open import Function as Fun using (flip)
 
11
open import Function.Bijection hiding (id; _∘_)
 
12
open import Function.Equality as F
 
13
  using (_⟶_) renaming (_∘_ to _⟪∘⟫_)
 
14
open import Function.Equivalence as Eq using (Equivalent; ⇔-setoid)
 
15
open import Function.LeftInverse as Left hiding (id; _∘_)
 
16
open import Function.Surjection  as Surj hiding (id; _∘_)
 
17
open import Relation.Binary
 
18
import Relation.Binary.PropositionalEquality as P
 
19
 
 
20
-- Inverses.
 
21
 
 
22
record _InverseOf_ {f₁ f₂ t₁ t₂}
 
23
                   {From : Setoid f₁ f₂} {To : Setoid t₁ t₂}
 
24
                   (from : To ⟶ From) (to : From ⟶ To) :
 
25
                   Set (f₁ ⊔ f₂ ⊔ t₁ ⊔ t₂) where
 
26
  field
 
27
    left-inverse-of  : from LeftInverseOf  to
 
28
    right-inverse-of : from RightInverseOf to
 
29
 
 
30
-- The set of all inverses between two setoids.
 
31
 
 
32
record Inverse {f₁ f₂ t₁ t₂}
 
33
               (From : Setoid f₁ f₂) (To : Setoid t₁ t₂) :
 
34
               Set (f₁ ⊔ f₂ ⊔ t₁ ⊔ t₂) where
 
35
  field
 
36
    to         : From ⟶ To
 
37
    from       : To ⟶ From
 
38
    inverse-of : from InverseOf to
 
39
 
 
40
  open _InverseOf_ inverse-of public
 
41
 
 
42
  left-inverse : LeftInverse From To
 
43
  left-inverse = record
 
44
    { to              = to
 
45
    ; from            = from
 
46
    ; left-inverse-of = left-inverse-of
 
47
    }
 
48
 
 
49
  open LeftInverse left-inverse public
 
50
    using (injective; injection)
 
51
 
 
52
  bijection : Bijection From To
 
53
  bijection = record
 
54
    { to        = to
 
55
    ; bijective = record
 
56
      { injective  = injective
 
57
      ; surjective = record
 
58
        { from             = from
 
59
        ; right-inverse-of = right-inverse-of
 
60
        }
 
61
      }
 
62
    }
 
63
 
 
64
  open Bijection bijection public
 
65
    using (equivalent; surjective; surjection; right-inverse)
 
66
 
 
67
-- The set of all inverses between two sets.
 
68
 
 
69
infix 3 _⇿_
 
70
 
 
71
_⇿_ : ∀ {f t} → Set f → Set t → Set _
 
72
From ⇿ To = Inverse (P.setoid From) (P.setoid To)
 
73
 
 
74
-- If two setoids are in bijective correspondence, then there is an
 
75
-- inverse between them.
 
76
 
 
77
fromBijection :
 
78
  ∀ {f₁ f₂ t₁ t₂} {From : Setoid f₁ f₂} {To : Setoid t₁ t₂} →
 
79
  Bijection From To → Inverse From To
 
80
fromBijection b = record
 
81
  { to         = Bijection.to b
 
82
  ; from       = Bijection.from b
 
83
  ; inverse-of = record
 
84
    { left-inverse-of  = Bijection.left-inverse-of b
 
85
    ; right-inverse-of = Bijection.right-inverse-of b
 
86
    }
 
87
  }
 
88
 
 
89
------------------------------------------------------------------------
 
90
-- Map and zip
 
91
 
 
92
map : ∀ {f₁ f₂ t₁ t₂} {From : Setoid f₁ f₂} {To : Setoid t₁ t₂}
 
93
        {f₁′ f₂′ t₁′ t₂′}
 
94
        {From′ : Setoid f₁′ f₂′} {To′ : Setoid t₁′ t₂′} →
 
95
      (t : (From ⟶ To) → (From′ ⟶ To′)) →
 
96
      (f : (To ⟶ From) → (To′ ⟶ From′)) →
 
97
      (∀ {to from} → from InverseOf to → f from InverseOf t to) →
 
98
      Inverse From To → Inverse From′ To′
 
99
map t f pres eq = record
 
100
  { to         = t to
 
101
  ; from       = f from
 
102
  ; inverse-of = pres inverse-of
 
103
  } where open Inverse eq
 
104
 
 
105
zip : ∀ {f₁₁ f₂₁ t₁₁ t₂₁}
 
106
        {From₁ : Setoid f₁₁ f₂₁} {To₁ : Setoid t₁₁ t₂₁}
 
107
        {f₁₂ f₂₂ t₁₂ t₂₂}
 
108
        {From₂ : Setoid f₁₂ f₂₂} {To₂ : Setoid t₁₂ t₂₂}
 
109
        {f₁ f₂ t₁ t₂} {From : Setoid f₁ f₂} {To : Setoid t₁ t₂} →
 
110
      (t : (From₁ ⟶ To₁) → (From₂ ⟶ To₂) → (From ⟶ To)) →
 
111
      (f : (To₁ ⟶ From₁) → (To₂ ⟶ From₂) → (To ⟶ From)) →
 
112
      (∀ {to₁ from₁ to₂ from₂} →
 
113
         from₁ InverseOf to₁ → from₂ InverseOf to₂ →
 
114
         f from₁ from₂ InverseOf t to₁ to₂) →
 
115
      Inverse From₁ To₁ → Inverse From₂ To₂ → Inverse From To
 
116
zip t f pres eq₁ eq₂ = record
 
117
  { to         = t (to   eq₁) (to   eq₂)
 
118
  ; from       = f (from eq₁) (from eq₂)
 
119
  ; inverse-of = pres (inverse-of eq₁) (inverse-of eq₂)
 
120
  } where open Inverse
 
121
 
 
122
------------------------------------------------------------------------
 
123
-- Inverse is an equivalence relation
 
124
 
 
125
-- Identity and composition (reflexivity and transitivity).
 
126
 
 
127
id : ∀ {s₁ s₂} → Reflexive (Inverse {s₁} {s₂})
 
128
id {x = S} = record
 
129
  { to         = F.id
 
130
  ; from       = F.id
 
131
  ; inverse-of = record
 
132
    { left-inverse-of  = LeftInverse.left-inverse-of id′
 
133
    ; right-inverse-of = LeftInverse.left-inverse-of id′
 
134
    }
 
135
  } where id′ = Left.id {S = S}
 
136
 
 
137
infixr 9 _∘_
 
138
 
 
139
_∘_ : ∀ {f₁ f₂ m₁ m₂ t₁ t₂} →
 
140
      TransFlip (Inverse {f₁} {f₂} {m₁} {m₂})
 
141
                (Inverse {m₁} {m₂} {t₁} {t₂})
 
142
                (Inverse {f₁} {f₂} {t₁} {t₂})
 
143
f ∘ g = record
 
144
  { to         = to   f ⟪∘⟫ to   g
 
145
  ; from       = from g ⟪∘⟫ from f
 
146
  ; inverse-of = record
 
147
    { left-inverse-of  = LeftInverse.left-inverse-of (Left._∘_ (left-inverse  f) (left-inverse  g))
 
148
    ; right-inverse-of = LeftInverse.left-inverse-of (Left._∘_ (right-inverse g) (right-inverse f))
 
149
    }
 
150
  } where open Inverse
 
151
 
 
152
-- Symmetry.
 
153
 
 
154
private
 
155
 module Dummy where
 
156
 
 
157
  sym : ∀ {f₁ f₂ t₁ t₂} →
 
158
        Sym (Inverse {f₁} {f₂} {t₁} {t₂}) (Inverse {t₁} {t₂} {f₁} {f₂})
 
159
  sym inv = record
 
160
    { from       = to
 
161
    ; to         = from
 
162
    ; inverse-of = record
 
163
      { left-inverse-of  = right-inverse-of
 
164
      ; right-inverse-of = left-inverse-of
 
165
      }
 
166
    } where open Inverse inv
 
167
 
 
168
-- For fixed universe levels we can construct a setoid.
 
169
 
 
170
setoid : (s₁ s₂ : Level) → Setoid (suc (s₁ ⊔ s₂)) (s₁ ⊔ s₂)
 
171
setoid s₁ s₂ = record
 
172
  { Carrier       = Setoid s₁ s₂
 
173
  ; _≈_           = Inverse
 
174
  ; isEquivalence =
 
175
      record {refl = id; sym = Dummy.sym; trans = flip _∘_}
 
176
  }
 
177
 
 
178
------------------------------------------------------------------------
 
179
-- A generalisation which includes both _⇔_ and _⇿_
 
180
 
 
181
-- There are two kinds of "isomorphisms": equivalences and inverses.
 
182
 
 
183
data Kind : Set where
 
184
  equivalent inverse : Kind
 
185
 
 
186
Isomorphism : Kind → ∀ {ℓ₁ ℓ₂} → Set ℓ₁ → Set ℓ₂ → Set _
 
187
Isomorphism inverse    A B = Inverse    (P.setoid A) (P.setoid B)
 
188
Isomorphism equivalent A B = Equivalent (P.setoid A) (P.setoid B)
 
189
 
 
190
-- Inverses are stronger, equivalences weaker.
 
191
 
 
192
⇿⇒ : ∀ {k x y} {X : Set x} {Y : Set y} →
 
193
     Isomorphism inverse X Y → Isomorphism k X Y
 
194
⇿⇒ {inverse}    = Fun.id
 
195
⇿⇒ {equivalent} = Inverse.equivalent
 
196
 
 
197
⇒⇔ : ∀ {k x y} {X : Set x} {Y : Set y} →
 
198
     Isomorphism k X Y → Isomorphism equivalent X Y
 
199
⇒⇔ {inverse}    = Inverse.equivalent
 
200
⇒⇔ {equivalent} = Fun.id
 
201
 
 
202
-- Equational reasoning for isomorphisms.
 
203
 
 
204
module EquationalReasoning where
 
205
 
 
206
  private
 
207
 
 
208
    refl : ∀ {k ℓ} → Reflexive (Isomorphism k {ℓ})
 
209
    refl {inverse}    = id
 
210
    refl {equivalent} = Eq.id
 
211
 
 
212
    trans : ∀ {k ℓ₁ ℓ₂ ℓ₃} →
 
213
            Trans (Isomorphism k {ℓ₁} {ℓ₂})
 
214
                  (Isomorphism k {ℓ₂} {ℓ₃})
 
215
                  (Isomorphism k {ℓ₁} {ℓ₃})
 
216
    trans {inverse}    = flip _∘_
 
217
    trans {equivalent} = flip Eq._∘_
 
218
 
 
219
  sym : ∀ {k ℓ₁ ℓ₂} →
 
220
        Sym (Isomorphism k {ℓ₁} {ℓ₂})
 
221
            (Isomorphism k {ℓ₂} {ℓ₁})
 
222
  sym {inverse}    = Dummy.sym
 
223
  sym {equivalent} = Eq.sym
 
224
 
 
225
  infix  2 _∎
 
226
  infixr 2 _≈⟨_⟩_ _⇿⟨_⟩_
 
227
 
 
228
  _≈⟨_⟩_ : ∀ {k x y z} (X : Set x) {Y : Set y} {Z : Set z} →
 
229
           Isomorphism k X Y → Isomorphism k Y Z → Isomorphism k X Z
 
230
  _ ≈⟨ X≈Y ⟩ Y≈Z = trans X≈Y Y≈Z
 
231
 
 
232
  _⇿⟨_⟩_ : ∀ {k x y z} (X : Set x) {Y : Set y} {Z : Set z} →
 
233
           X ⇿ Y → Isomorphism k Y Z → Isomorphism k X Z
 
234
  X ⇿⟨ X⇿Y ⟩ Y⇔Z = X ≈⟨ ⇿⇒ X⇿Y ⟩ Y⇔Z
 
235
 
 
236
  _∎ : ∀ {k x} (X : Set x) → Isomorphism k X X
 
237
  X ∎ = refl
 
238
 
 
239
-- For fixed universe levels we can construct a setoid.
 
240
 
 
241
Isomorphism-setoid : Kind → (ℓ : Level) → Setoid _ _
 
242
Isomorphism-setoid k ℓ = record
 
243
  { Carrier       = Set ℓ
 
244
  ; _≈_           = Isomorphism k
 
245
  ; isEquivalence =
 
246
      record {refl = _ ∎; sym = sym; trans = _≈⟨_⟩_ _}
 
247
  } where open EquationalReasoning
 
248
 
 
249
-- Every unary relation induces an equivalence relation. (No claim is
 
250
-- made that this relation is unique.)
 
251
 
 
252
InducedEquivalence₁ : Kind → ∀ {a s} {A : Set a}
 
253
                      (S : A → Set s) → Setoid _ _
 
254
InducedEquivalence₁ k S = record
 
255
  { _≈_           = λ x y → Isomorphism k (S x) (S y)
 
256
  ; isEquivalence = record {refl = refl; sym = sym; trans = trans}
 
257
  } where open Setoid (Isomorphism-setoid _ _)
 
258
 
 
259
-- Every binary relation induces an equivalence relation. (No claim is
 
260
-- made that this relation is unique.)
 
261
 
 
262
InducedEquivalence₂ : Kind → ∀ {a b s} {A : Set a} {B : Set b}
 
263
                      (_S_ : A → B → Set s) → Setoid _ _
 
264
InducedEquivalence₂ k _S_ = record
 
265
  { _≈_           = λ x y → ∀ {z} → Isomorphism k (z S x) (z S y)
 
266
  ; isEquivalence = record
 
267
    { refl  = refl
 
268
    ; sym   = λ i≈j → sym i≈j
 
269
    ; trans = λ i≈j j≈k → trans i≈j j≈k
 
270
    }
 
271
  } where open Setoid (Isomorphism-setoid _ _)
 
272
 
 
273
open Dummy public