| 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 |
{-# OPTIONS --cubical-compatible --safe #-} |
| 11 |
|
| 12 |
open import Data.Bool.Base using (true; false; if_then_else_) |
| 13 |
open import Data.Empty using (⊥) |
| 14 |
open import Data.List.Base using (List; []; _∷_; foldr) |
| 15 |
open import Data.Product.Base hiding (proj₁; proj₂) |
| 16 |
open import Data.Unit.Polymorphic using (⊤) |
| 17 |
open import Function.Base using (id; _∘_) |
| 18 |
open import Level using (suc; _⊔_) |
| 19 |
open import Relation.Binary.Definitions using (DecidableEquality) |
| 20 |
open import Relation.Nullary.Decidable using (does) |
| 21 |
|
| 22 |
-- The module is parametrised by the type of labels, which should come |
| 23 |
-- with decidable equality. |
| 24 |
|
| 25 |
module Data.Record {ℓ} (Label : Set ℓ) (_≟_ : DecidableEquality Label) where |
| 26 |
|
| 27 |
------------------------------------------------------------------------ |
| 28 |
-- A Σ-type with a manifest field |
| 29 |
|
| 30 |
-- A variant of Σ where the value of the second field is "manifest" |
| 31 |
-- (given by the first). |
| 32 |
|
| 33 |
infix 4 _, |
| 34 |
|
| 35 |
record Manifest-Σ {a b} (A : Set a) {B : A → Set b} |
| 36 |
(f : (x : A) → B x) : Set a where |
| 37 |
constructor _, |
| 38 |
field proj₁ : A |
| 39 |
|
| 40 |
proj₂ : B proj₁ |
| 41 |
proj₂ = f proj₁ |
| 42 |
|
| 43 |
------------------------------------------------------------------------ |
| 44 |
-- Signatures and records |
| 45 |
|
| 46 |
mutual |
| 47 |
|
| 48 |
infixl 5 _,_∶_ _,_≔_ |
| 49 |
|
| 50 |
data Signature s : Set (suc s ⊔ ℓ) where |
| 51 |
∅ : Signature s |
| 52 |
_,_∶_ : (Sig : Signature s) |
| 53 |
(ℓ : Label) |
| 54 |
(A : Record Sig → Set s) → |
| 55 |
Signature s |
| 56 |
_,_≔_ : (Sig : Signature s) |
| 57 |
(ℓ : Label) |
| 58 |
{A : Record Sig → Set s} |
| 59 |
(a : (r : Record Sig) → A r) → |
| 60 |
Signature s |
| 61 |
|
| 62 |
-- Record is a record type to ensure that the signature can be |
| 63 |
-- inferred from a value of type Record Sig. |
| 64 |
|
| 65 |
record Record {s} (Sig : Signature s) : Set s where |
| 66 |
eta-equality |
| 67 |
inductive |
| 68 |
constructor rec |
| 69 |
field fun : Record-fun Sig |
| 70 |
|
| 71 |
Record-fun : ∀ {s} → Signature s → Set s |
| 72 |
Record-fun ∅ = ⊤ |
| 73 |
Record-fun (Sig , ℓ ∶ A) = Σ (Record Sig) A |
| 74 |
Record-fun (Sig , ℓ ≔ a) = Manifest-Σ (Record Sig) a |
| 75 |
|
| 76 |
------------------------------------------------------------------------ |
| 77 |
-- Labels |
| 78 |
|
| 79 |
-- A signature's labels, starting with the last one. |
| 80 |
|
| 81 |
labels : ∀ {s} → Signature s → List Label |
| 82 |
labels ∅ = [] |
| 83 |
labels (Sig , ℓ ∶ A) = ℓ ∷ labels Sig |
| 84 |
labels (Sig , ℓ ≔ a) = ℓ ∷ labels Sig |
| 85 |
|
| 86 |
-- Inhabited if the label is part of the signature. |
| 87 |
|
| 88 |
infix 4 _∈_ |
| 89 |
|
| 90 |
_∈_ : ∀ {s} → Label → Signature s → Set |
| 91 |
ℓ ∈ Sig = |
| 92 |
foldr (λ ℓ′ → if does (ℓ ≟ ℓ′) then (λ _ → ⊤) else id) ⊥ (labels Sig) |
| 93 |
|
| 94 |
------------------------------------------------------------------------ |
| 95 |
-- Projections |
| 96 |
|
| 97 |
-- Signature restriction and projection. (Restriction means removal of |
| 98 |
-- a given field and all subsequent fields.) |
| 99 |
|
| 100 |
Restrict : ∀ {s} (Sig : Signature s) (ℓ : Label) → ℓ ∈ Sig → |
| 101 |
Signature s |
| 102 |
Restrict ∅ ℓ () |
| 103 |
Restrict (Sig , ℓ′ ∶ A) ℓ ℓ∈ with does (ℓ ≟ ℓ′) |
| 104 |
... | true = Sig |
| 105 |
... | false = Restrict Sig ℓ ℓ∈ |
| 106 |
Restrict (Sig , ℓ′ ≔ a) ℓ ℓ∈ with does (ℓ ≟ ℓ′) |
| 107 |
... | true = Sig |
| 108 |
... | false = Restrict Sig ℓ ℓ∈ |
| 109 |
|
| 110 |
Restricted : ∀ {s} (Sig : Signature s) (ℓ : Label) → ℓ ∈ Sig → Set s |
| 111 |
Restricted Sig ℓ ℓ∈ = Record (Restrict Sig ℓ ℓ∈) |
| 112 |
|
| 113 |
Proj : ∀ {s} (Sig : Signature s) (ℓ : Label) {ℓ∈ : ℓ ∈ Sig} → |
| 114 |
Restricted Sig ℓ ℓ∈ → Set s |
| 115 |
Proj ∅ ℓ {} |
| 116 |
Proj (Sig , ℓ′ ∶ A) ℓ {ℓ∈} with does (ℓ ≟ ℓ′) |
| 117 |
... | true = A |
| 118 |
... | false = Proj Sig ℓ {ℓ∈} |
| 119 |
Proj (_,_≔_ Sig ℓ′ {A = A} a) ℓ {ℓ∈} with does (ℓ ≟ ℓ′) |
| 120 |
... | true = A |
| 121 |
... | false = Proj Sig ℓ {ℓ∈} |
| 122 |
|
| 123 |
-- Record restriction and projection. |
| 124 |
|
| 125 |
infixl 5 _∣_ |
| 126 |
|
| 127 |
_∣_ : ∀ {s} {Sig : Signature s} → Record Sig → |
| 128 |
(ℓ : Label) {ℓ∈ : ℓ ∈ Sig} → Restricted Sig ℓ ℓ∈ |
| 129 |
_∣_ {Sig = ∅} r ℓ {} |
| 130 |
_∣_ {Sig = Sig , ℓ′ ∶ A} (rec r) ℓ {ℓ∈} with does (ℓ ≟ ℓ′) |
| 131 |
... | true = Σ.proj₁ r |
| 132 |
... | false = _∣_ (Σ.proj₁ r) ℓ {ℓ∈} |
| 133 |
_∣_ {Sig = Sig , ℓ′ ≔ a} (rec r) ℓ {ℓ∈} with does (ℓ ≟ ℓ′) |
| 134 |
... | true = Manifest-Σ.proj₁ r |
| 135 |
... | false = _∣_ (Manifest-Σ.proj₁ r) ℓ {ℓ∈} |
| 136 |
|
| 137 |
infixl 5 _·_ |
| 138 |
|
| 139 |
_·_ : ∀ {s} {Sig : Signature s} (r : Record Sig) |
| 140 |
(ℓ : Label) {ℓ∈ : ℓ ∈ Sig} → |
| 141 |
Proj Sig ℓ {ℓ∈} (r ∣ ℓ) |
| 142 |
_·_ {Sig = ∅} r ℓ {} |
| 143 |
_·_ {Sig = Sig , ℓ′ ∶ A} (rec r) ℓ {ℓ∈} with does (ℓ ≟ ℓ′) |
| 144 |
... | true = Σ.proj₂ r |
| 145 |
... | false = _·_ (Σ.proj₁ r) ℓ {ℓ∈} |
| 146 |
_·_ {Sig = Sig , ℓ′ ≔ a} (rec r) ℓ {ℓ∈} with does (ℓ ≟ ℓ′) |
| 147 |
... | true = Manifest-Σ.proj₂ r |
| 148 |
... | false = _·_ (Manifest-Σ.proj₁ r) ℓ {ℓ∈} |
| 149 |
|
| 150 |
------------------------------------------------------------------------ |
| 151 |
-- With |
| 152 |
|
| 153 |
-- Sig With ℓ ≔ a is the signature Sig, but with the ℓ field set to a. |
| 154 |
|
| 155 |
mutual |
| 156 |
|
| 157 |
infixl 5 _With_≔_ |
| 158 |
|
| 159 |
_With_≔_ : ∀ {s} (Sig : Signature s) (ℓ : Label) {ℓ∈ : ℓ ∈ Sig} → |
| 160 |
((r : Restricted Sig ℓ ℓ∈) → Proj Sig ℓ r) → Signature s |
| 161 |
_With_≔_ ∅ ℓ {} a |
| 162 |
_With_≔_ (Sig , ℓ′ ∶ A) ℓ {ℓ∈} a with does (ℓ ≟ ℓ′) |
| 163 |
... | true = Sig , ℓ′ ≔ a |
| 164 |
... | false = _With_≔_ Sig ℓ {ℓ∈} a , ℓ′ ∶ A ∘ drop-With |
| 165 |
_With_≔_ (Sig , ℓ′ ≔ a′) ℓ {ℓ∈} a with does (ℓ ≟ ℓ′) |
| 166 |
... | true = Sig , ℓ′ ≔ a |
| 167 |
... | false = _With_≔_ Sig ℓ {ℓ∈} a , ℓ′ ≔ a′ ∘ drop-With |
| 168 |
|
| 169 |
drop-With : ∀ {s} {Sig : Signature s} {ℓ : Label} {ℓ∈ : ℓ ∈ Sig} |
| 170 |
{a : (r : Restricted Sig ℓ ℓ∈) → Proj Sig ℓ r} → |
| 171 |
Record (_With_≔_ Sig ℓ {ℓ∈} a) → Record Sig |
| 172 |
drop-With {Sig = ∅} {ℓ∈ = ()} r |
| 173 |
drop-With {Sig = Sig , ℓ′ ∶ A} {ℓ} (rec r) with does (ℓ ≟ ℓ′) |
| 174 |
... | true = rec (Manifest-Σ.proj₁ r , Manifest-Σ.proj₂ r) |
| 175 |
... | false = rec (drop-With (Σ.proj₁ r) , Σ.proj₂ r) |
| 176 |
drop-With {Sig = Sig , ℓ′ ≔ a} {ℓ} (rec r) with does (ℓ ≟ ℓ′) |
| 177 |
... | true = rec (Manifest-Σ.proj₁ r ,) |
| 178 |
... | false = rec (drop-With (Manifest-Σ.proj₁ r) ,) |