~ubuntu-branches/ubuntu/saucy/agda-stdlib/saucy

« back to all changes in this revision

Viewing changes to src/Record.agda

  • Committer: Package Import Robot
  • Author(s): Iain Lane
  • Date: 2011-11-29 17:00:35 UTC
  • mfrom: (2.1.4 sid)
  • Revision ID: package-import@ubuntu.com-20111129170035-be8cbok8ojft5tjl
Tags: 0.6~darcs20111129t1640-1
* [ef445ab] Imported Upstream version 0.6~darcs20111129t1640
  + Darcs snapshot required for Agda 2.3.0 compatibility
* [f801f83] Update BDs and deps to require Agda 2.3.0
* [c52be90] Use 3.0 (quilt) for bz2 orig

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
------------------------------------------------------------------------
 
2
-- The Agda standard library
 
3
--
 
4
-- Record types with manifest fields and "with", based on Randy
 
5
-- Pollack's "Dependently Typed Records in Type Theory"
 
6
------------------------------------------------------------------------
 
7
 
 
8
-- For an example of how this module can be used, see README.Record.
 
9
 
 
10
-- The module is parametrised by the type of labels, which should come
 
11
-- with decidable equality.
 
12
 
 
13
open import Relation.Binary
 
14
open import Relation.Binary.PropositionalEquality
 
15
 
 
16
module Record (Label : Set) (_≟_ : Decidable (_≡_ {A = Label})) where
 
17
 
 
18
open import Data.Bool
 
19
open import Data.Empty
 
20
open import Data.List
 
21
open import Data.Product hiding (proj₁; proj₂)
 
22
open import Data.Unit
 
23
open import Function hiding (type-signature)
 
24
open import Level
 
25
open import Relation.Nullary
 
26
open import Relation.Nullary.Decidable
 
27
 
 
28
------------------------------------------------------------------------
 
29
-- A Σ-type with a manifest field
 
30
 
 
31
-- A variant of Σ where the value of the second field is "manifest"
 
32
-- (given by the first).
 
33
 
 
34
infix 4 _,
 
35
 
 
36
record Manifest-Σ {a b} (A : Set a) {B : A → Set b}
 
37
                  (f : (x : A) → B x) : Set a where
 
38
  constructor _,
 
39
  field proj₁ : A
 
40
 
 
41
  proj₂ : B proj₁
 
42
  proj₂ = f proj₁
 
43
 
 
44
------------------------------------------------------------------------
 
45
-- Signatures and records
 
46
 
 
47
mutual
 
48
 
 
49
  infixl 5 _,_∶_ _,_≔_
 
50
 
 
51
  data Signature s : Set (suc s) where
 
52
    ∅     : Signature s
 
53
    _,_∶_ : (Sig : Signature s)
 
54
            (ℓ : Label)
 
55
            (A : Record Sig → Set s) →
 
56
            Signature s
 
57
    _,_≔_ : (Sig : Signature s)
 
58
            (ℓ : Label)
 
59
            {A : Record Sig → Set s}
 
60
            (a : (r : Record Sig) → A r) →
 
61
            Signature s
 
62
 
 
63
  -- Record is a record type to ensure that the signature can be
 
64
  -- inferred from a value of type Record Sig.
 
65
 
 
66
  record Record {s} (Sig : Signature s) : Set s where
 
67
    constructor rec
 
68
    field fun : Record-fun Sig
 
69
 
 
70
  Record-fun : ∀ {s} → Signature s → Set s
 
71
  Record-fun ∅             = Lift ⊤
 
72
  Record-fun (Sig , ℓ ∶ A) =          Σ (Record Sig) A
 
73
  Record-fun (Sig , ℓ ≔ a) = Manifest-Σ (Record Sig) a
 
74
 
 
75
------------------------------------------------------------------------
 
76
-- Variants of Signature and Record
 
77
 
 
78
-- It may be easier to define values of type Record′ (Sig⇒Sig′ Sig)
 
79
-- than of type Record Sig.
 
80
 
 
81
mutual
 
82
 
 
83
  data Signature′ s : Set (suc s) where
 
84
    ∅     : Signature′ s
 
85
    _,_∶_ : (Sig : Signature′ s)
 
86
            (ℓ : Label)
 
87
            (A : Record′ Sig → Set s) →
 
88
            Signature′ s
 
89
    _,_≔_ : (Sig : Signature′ s)
 
90
            (ℓ : Label)
 
91
            {A : Record′ Sig → Set s}
 
92
            (a : (r : Record′ Sig) → A r) →
 
93
            Signature′ s
 
94
 
 
95
  Record′ : ∀ {s} → Signature′ s → Set s
 
96
  Record′ ∅             = Lift ⊤
 
97
  Record′ (Sig , ℓ ∶ A) =          Σ (Record′ Sig) A
 
98
  Record′ (Sig , ℓ ≔ a) = Manifest-Σ (Record′ Sig) a
 
99
 
 
100
-- We can convert between the two kinds of signatures/records.
 
101
 
 
102
mutual
 
103
 
 
104
  Sig′⇒Sig : ∀ {s} → Signature′ s → Signature s
 
105
  Sig′⇒Sig ∅             = ∅
 
106
  Sig′⇒Sig (Sig , ℓ ∶ A) = Sig′⇒Sig Sig , ℓ ∶ (A ∘ Rec⇒Rec′ _)
 
107
  Sig′⇒Sig (Sig , ℓ ≔ a) = Sig′⇒Sig Sig , ℓ ≔ (a ∘ Rec⇒Rec′ _)
 
108
 
 
109
  Rec⇒Rec′ : ∀ {s} (Sig : Signature′ s) →
 
110
             Record (Sig′⇒Sig Sig) → Record′ Sig
 
111
  Rec⇒Rec′ ∅             (rec r) = r
 
112
  Rec⇒Rec′ (Sig , ℓ ∶ A) (rec r) = (Rec⇒Rec′ _ (Σ.proj₁ r) , Σ.proj₂ r)
 
113
  Rec⇒Rec′ (Sig , ℓ ≔ a) (rec r) = (Rec⇒Rec′ _ (Manifest-Σ.proj₁ r) ,)
 
114
 
 
115
mutual
 
116
 
 
117
  Sig⇒Sig′ : ∀ {s} → Signature s → Signature′ s
 
118
  Sig⇒Sig′ ∅             = ∅
 
119
  Sig⇒Sig′ (Sig , ℓ ∶ A) = Sig⇒Sig′ Sig , ℓ ∶ (A ∘ Rec′⇒Rec _)
 
120
  Sig⇒Sig′ (Sig , ℓ ≔ a) = Sig⇒Sig′ Sig , ℓ ≔ (a ∘ Rec′⇒Rec _)
 
121
 
 
122
  Rec′⇒Rec : ∀ {s} (Sig : Signature s) →
 
123
             Record′ (Sig⇒Sig′ Sig) → Record Sig
 
124
  Rec′⇒Rec ∅             r = rec r
 
125
  Rec′⇒Rec (Sig , ℓ ∶ A) r = rec (Rec′⇒Rec _ (Σ.proj₁ r) , Σ.proj₂ r)
 
126
  Rec′⇒Rec (Sig , ℓ ≔ a) r = rec (Rec′⇒Rec _ (Manifest-Σ.proj₁ r) ,)
 
127
 
 
128
------------------------------------------------------------------------
 
129
-- Labels
 
130
 
 
131
-- A signature's labels, starting with the last one.
 
132
 
 
133
labels : ∀ {s} → Signature s → List Label
 
134
labels ∅             = []
 
135
labels (Sig , ℓ ∶ A) = ℓ ∷ labels Sig
 
136
labels (Sig , ℓ ≔ a) = ℓ ∷ labels Sig
 
137
 
 
138
-- Inhabited if the label is part of the signature.
 
139
 
 
140
infix 4 _∈_
 
141
 
 
142
_∈_ : ∀ {s} → Label → Signature s → Set
 
143
ℓ ∈ Sig =
 
144
  foldr (λ ℓ′ → if ⌊ ℓ ≟ ℓ′ ⌋ then (λ _ → ⊤) else id) ⊥ (labels Sig)
 
145
 
 
146
------------------------------------------------------------------------
 
147
-- Projections
 
148
 
 
149
-- Signature restriction and projection. (Restriction means removal of
 
150
-- a given field and all subsequent fields.)
 
151
 
 
152
Restrict : ∀ {s} (Sig : Signature s) (ℓ : Label) → ℓ ∈ Sig →
 
153
           Signature s
 
154
Restrict ∅              ℓ ()
 
155
Restrict (Sig , ℓ′ ∶ A) ℓ ℓ∈ with ℓ ≟ ℓ′
 
156
... | yes _ = Sig
 
157
... | no  _ = Restrict Sig ℓ ℓ∈
 
158
Restrict (Sig , ℓ′ ≔ a) ℓ ℓ∈ with ℓ ≟ ℓ′
 
159
... | yes _ = Sig
 
160
... | no  _ = Restrict Sig ℓ ℓ∈
 
161
 
 
162
Restricted : ∀ {s} (Sig : Signature s) (ℓ : Label) → ℓ ∈ Sig → Set s
 
163
Restricted Sig ℓ ℓ∈ = Record (Restrict Sig ℓ ℓ∈)
 
164
 
 
165
Proj : ∀ {s} (Sig : Signature s) (ℓ : Label) {ℓ∈ : ℓ ∈ Sig} →
 
166
       Restricted Sig ℓ ℓ∈ → Set s
 
167
Proj ∅              ℓ {}
 
168
Proj (Sig , ℓ′ ∶ A) ℓ with ℓ ≟ ℓ′
 
169
... | yes _ = A
 
170
... | no  _ = Proj Sig ℓ
 
171
Proj (_,_≔_ Sig ℓ′ {A = A} a) ℓ with ℓ ≟ ℓ′
 
172
... | yes _ = A
 
173
... | no  _ = Proj Sig ℓ
 
174
 
 
175
-- Record restriction and projection.
 
176
 
 
177
infixl 5 _∣_
 
178
 
 
179
_∣_ : ∀ {s} {Sig : Signature s} → Record Sig →
 
180
      (ℓ : Label) {ℓ∈ : ℓ ∈ Sig} → Restricted Sig ℓ ℓ∈
 
181
_∣_ {Sig = ∅}            r       ℓ {}
 
182
_∣_ {Sig = Sig , ℓ′ ∶ A} (rec r) ℓ with ℓ ≟ ℓ′
 
183
... | yes _ = Σ.proj₁ r
 
184
... | no  _ = Σ.proj₁ r ∣ ℓ
 
185
_∣_ {Sig = Sig , ℓ′ ≔ a} (rec r) ℓ with ℓ ≟ ℓ′
 
186
... | yes _ = Manifest-Σ.proj₁ r
 
187
... | no  _ = Manifest-Σ.proj₁ r ∣ ℓ
 
188
 
 
189
infixl 5 _·_
 
190
 
 
191
_·_ : ∀ {s} {Sig : Signature s} (r : Record Sig)
 
192
      (ℓ : Label) {ℓ∈ : ℓ ∈ Sig} →
 
193
      Proj Sig ℓ {ℓ∈} (r ∣ ℓ)
 
194
_·_ {Sig = ∅}            r       ℓ {}
 
195
_·_ {Sig = Sig , ℓ′ ∶ A} (rec r) ℓ with ℓ ≟ ℓ′
 
196
... | yes _ = Σ.proj₂ r
 
197
... | no  _ = Σ.proj₁ r · ℓ
 
198
_·_ {Sig = Sig , ℓ′ ≔ a} (rec r) ℓ with ℓ ≟ ℓ′
 
199
... | yes _ = Manifest-Σ.proj₂ r
 
200
... | no  _ = Manifest-Σ.proj₁ r · ℓ
 
201
 
 
202
------------------------------------------------------------------------
 
203
-- With
 
204
 
 
205
-- Sig With ℓ ≔ a is the signature Sig, but with the ℓ field set to a.
 
206
 
 
207
mutual
 
208
 
 
209
  infixl 5 _With_≔_
 
210
 
 
211
  _With_≔_ : ∀ {s} (Sig : Signature s) (ℓ : Label) {ℓ∈ : ℓ ∈ Sig} →
 
212
             ((r : Restricted Sig ℓ ℓ∈) → Proj Sig ℓ r) → Signature s
 
213
  _With_≔_ ∅ ℓ {} a
 
214
  Sig , ℓ′ ∶ A With ℓ ≔ a with ℓ ≟ ℓ′
 
215
  ... | yes _ = Sig            , ℓ′ ≔ a
 
216
  ... | no  _ = Sig With ℓ ≔ a , ℓ′ ∶ A ∘ drop-With
 
217
  Sig , ℓ′ ≔ a′ With ℓ ≔ a with ℓ ≟ ℓ′
 
218
  ... | yes _ = Sig            , ℓ′ ≔ a
 
219
  ... | no  _ = Sig With ℓ ≔ a , ℓ′ ≔ a′ ∘ drop-With
 
220
 
 
221
  drop-With : ∀ {s} {Sig : Signature s} {ℓ : Label} {ℓ∈ : ℓ ∈ Sig}
 
222
              {a : (r : Restricted Sig ℓ ℓ∈) → Proj Sig ℓ r} →
 
223
              Record (Sig With ℓ ≔ a) → Record Sig
 
224
  drop-With {Sig = ∅} {ℓ∈ = ()}      r
 
225
  drop-With {Sig = Sig , ℓ′ ∶ A} {ℓ} (rec r) with ℓ ≟ ℓ′
 
226
  ... | yes _ = rec (Manifest-Σ.proj₁ r , Manifest-Σ.proj₂ r)
 
227
  ... | no  _ = rec (drop-With (Σ.proj₁ r) , Σ.proj₂ r)
 
228
  drop-With {Sig = Sig , ℓ′ ≔ a} {ℓ} (rec r) with ℓ ≟ ℓ′
 
229
  ... | yes _ = rec (Manifest-Σ.proj₁ r ,)
 
230
  ... | no  _ = rec (drop-With (Manifest-Σ.proj₁ r) ,)