std:Reflection/Row

lib/Reflection/Row.omg

lib/Reflection/Row.omg272 lines
1-- Typed operations on one record or union layer. The family keeps the
2-- original labels while allowing each field to use its interpretation.
3-- `la` is shared by original and rebuilt fields; evidence (`lp`) and fold
4-- results (`lx`) may live in different universes. A family polymorphic in
5-- every output universe would live in Typeω, which is not ordinary data.
6-- Written positions are optional: a computed type may have no source order.
7-- Labels determine the row; source order remains explicit metadata for users
8-- such as ABI derivation that must not silently substitute label order.
9let Field = { name : Text, position : Optional Natural }
10let Interpretation =
11      \{la lp : Level} (P : Type la -> Type lp) -> forall (A : Type la) -> P A -> Type la
12let original = \{la lp : Level} (P : Type la -> Type lp) (A : Type la) (_ : P A) -> A
13let Identity = \{la : Level} (A : Type la) -> A
14let identityMonad =
15      \{la : Level} ->
16        { pure = \{A : Type la} (value : A) -> value
17        , bind = \{A B : Type la} (value : A) (after : A -> B) -> after value
18        }
19
20let Build =
21      \{la lp : Level} (P : Type la -> Type lp) (R : Interpretation P -> Type la) ->
22        forall (F : Interpretation P) (M : Type la -> Type la)
23        -> std:Monad.Monad M
24        -> (forall (A : Type la) (description : P A)
25            -> Field
26            -> (forall (G : Interpretation P) -> R G -> G A description)
27            -> M (F A description))
28        -> M (R F)
29let Fields =
30      \{la lp lx : Level} (P : Type la -> Type lp) (R : Interpretation P -> Type la) ->
31        forall (X : Type lx)
32        -> (forall (A : Type la) (description : P A)
33            -> Field
34            -> (forall (G : Interpretation P) -> R G -> G A description)
35            -> X
36            -> X)
37        -> X
38        -> X
39let Cases =
40      \{la lp lx : Level} (P : Type la -> Type lp) (U : Interpretation P -> Type la) ->
41        forall (X : Type lx)
42        -> (forall (A : Type la) (description : P A)
43            -> Field
44            -> (forall (G : Interpretation P) -> G A description -> U G)
45            -> X
46            -> X)
47        -> X
48        -> X
49let Match =
50      \{la lp lx : Level} (P : Type la -> Type lp) (U : Interpretation P -> Type la) ->
51        forall (F : Interpretation P) (X : Type lx)
52        -> (forall (A : Type la) (description : P A)
53            -> Field
54            -> (forall (G : Interpretation P) -> G A description -> U G)
55            -> F A description
56            -> X)
57        -> U F
58        -> X
59let RecordCase =
60      \{la lp lx : Level} (P : Type la -> Type lp) ->
61        forall (R : Interpretation P -> Type la)
62        -> Build P R
63        -> Fields @la @lp @lx P R
64        -> P (R (original P))
65let UnionCase =
66      \{la lp lx : Level} (P : Type la -> Type lp) ->
67        forall (U : Interpretation P -> Type la)
68        -> Cases @la @lp @lx P U
69        -> Match @la @lp @la P U
70        -> P (U (original P))
71
72-- Two typed conversions. Their types do not assert that they are inverses,
73-- nor do the row operations above assert enumeration or reconstruction laws.
74-- Such laws require evidence about the supplied operations and children.
75let Conversion =
76      \{la : Level} (A : Type la) ->
77        exists (Target : Type la) * { to : A -> Target, from : Target -> A }
78let target = \{la : Level} (A : Type la) (conversion : Conversion A) -> conversion.1
79let unchanged =
80      \{la : Level} (A : Type la) ->
81        ((A, { to = \(value : A) -> value, from = \(value : A) -> value }) : Conversion A)
82let recordConversion
83    : forall {la lx : Level} -> RecordCase @la @(Level/suc la) @lx (Conversion @la)
84    = \{la} {lx} R build fields ->
85        (R target,
86        { to =
87            \(value : R (original Conversion)) ->
88              build
89                target
90                Identity
91                identityMonad
92                (\A description name project ->
93                   description.2.to (project (original Conversion) value))
94        , from =
95            \(value : R target) ->
96              build
97                (original Conversion)
98                Identity
99                identityMonad
100                (\A description name project -> description.2.from (project target value))
101        })
102let unionConversion
103    : forall {la lx : Level} -> UnionCase @la @(Level/suc la) @lx (Conversion @la)
104    = \{la} {lx} U cases match ->
105        (U target,
106        { to =
107            match
108              (original Conversion)
109              (U target)
110              (\A description name inject value -> inject target (description.2.to value))
111        , from =
112            match
113              target
114              (U (original Conversion))
115              (\A description name inject value ->
116                 inject (original Conversion) (description.2.from value))
117        })
118
119-- An indexed mapping preserves a shape while transforming its children.
120-- The same laws apply to products, sums and shapes without any children.
121-- They do not constrain names, written positions, or a separate enumeration.
122let IndexedMap =
123      \{li la : Level} (I : Type li) (R : (I -> Type la) -> Type la) ->
124        forall (F G : I -> Type la) -> (forall (i : I) -> F i -> G i) -> R F -> R G
125let MapLaws =
126      \{li la : Level} (I : Type li) (R : (I -> Type la) -> Type la) (map : IndexedMap I R) ->
127        { identity : forall (F : I -> Type la) (value : R F) -> map F F (\i x -> x) value === value
128        , composition :
129            forall (F G H : I -> Type la)
130                   (f : forall (i : I) -> F i -> G i)
131                   (g : forall (i : I) -> G i -> H i)
132                   (value : R F)
133            -> map G H g (map F G f value) === map F H (\i x -> g i (f i x)) value
134        , congruent :
135            forall (F G : I -> Type la) (f g : forall (i : I) -> F i -> G i)
136            -> (forall (i : I) (x : F i) -> f i x === g i x)
137            -> forall (value : R F)
138            -> map F G f value === map F G g value
139        }
140let Inverse =
141      \{la lb : Level} (A : Type la) (B : Type lb) (to : A -> B) (from : B -> A) ->
142        { left : forall (a : A) -> from (to a) === a
143        , right : forall (b : B) -> to (from b) === b
144        }
145let Iso =
146      \{la lb : Level} (A : Type la) (B : Type lb) ->
147        exists (to : A -> B) (from : B -> A) * Inverse A B to from
148
149-- Pointwise inverse maps lift through any shape satisfying these laws.
150-- The proof needs neither function extensionality nor a particular index
151-- representation. An empty-record map can preserve its input directly,
152-- so no empty-record eta or uniqueness axiom is required.
153let mapIso =
154      \{li la : Level}
155       (I : Type li)
156       (R : (I -> Type la) -> Type la)
157       (map : IndexedMap I R)
158       (laws : MapLaws I R map)
159       (A B : I -> Type la)
160       (children : forall (i : I) -> Iso (A i) (B i)) ->
161        let E = std:Equal
162        let to = \i -> (children i).1
163        let from = \i -> (children i).2.1
164        let forward = map A B to
165        let backward = map B A from
166        let left =
167              \(value : R A) ->
168                E.trans
169                  (laws.composition A B A to from value)
170                  (E.trans
171                     (laws.congruent
172                        A
173                        A
174                        (\i x -> from i (to i x))
175                        (\i x -> x)
176                        (\i x -> (children i).2.2.left x)
177                        value)
178                     (laws.identity A value))
179        let right =
180              \(value : R B) ->
181                E.trans
182                  (laws.composition B A B from to value)
183                  (E.trans
184                     (laws.congruent
185                        B
186                        B
187                        (\i x -> to i (from i x))
188                        (\i x -> x)
189                        (\i x -> (children i).2.2.right x)
190                        value)
191                     (laws.identity B value))
192        in  ((forward, (backward, { left, right })) : Iso (R A) (R B))
193-- A lawful focus on one component. These equalities concern values; names
194-- and ABI positions are independent metadata and are not certified here.
195let Lens =
196      \(A B : Type) ->
197        exists (get : A -> B) (set : B -> A -> A) *
198          { selected : forall (value : B) (original : A) -> get (set value original) === value
199          , unchanged : forall (original : A) -> set (get original) original === original
200          , overwrite :
201              forall (new old : B) (original : A) -> set new (set old original) === set new original
202          }
203let RecordEdit =
204      \(I : Type) (R : (I -> Type) -> Row Type) ->
205        exists (get : forall (F : I -> Type) (i : I) -> { ...R F } -> F i)
206               (set : forall (F : I -> Type) (i : I) -> F i -> { ...R F } -> { ...R F }) *
207          { selected :
208              forall (F : I -> Type) (i : I) (value : F i) (original : { ...R F })
209              -> get F i (set F i value original) === value
210          , unchanged :
211              forall (F : I -> Type) (i : I) (original : { ...R F })
212              -> set F i (get F i original) original === original
213          , overwrite :
214              forall (F : I -> Type) (i : I) (new old : F i) (original : { ...R F })
215              -> set F i new (set F i old original) === set F i new original
216          }
217let fieldLens =
218      \(I : Type) (R : (I -> Type) -> Row Type) (edit : RecordEdit I R) (F : I -> Type) (i : I) ->
219        ((edit.1 F i,
220         (edit.2.1 F i,
221         { selected = edit.2.2.selected F i
222         , unchanged = edit.2.2.unchanged F i
223         , overwrite = edit.2.2.overwrite F i
224         })) :
225           Lens { ...R F } (F i))
226
227-- A physical record field originates in a row's polymorphic operations.
228-- A lawful lens alone could also focus a computed value (for example a sum),
229-- so its three value laws do not by themselves establish this provenance.
230let FieldOrigin =
231      \(A B : Type) (lens : Lens A B) ->
232        forall (P : forall (A B : Type) -> Lens A B -> Type)
233        -> (forall (I : Type)
234                   (R : (I -> Type) -> Row Type)
235                   (edit : RecordEdit I R)
236                   (F : I -> Type)
237                   (i : I)
238            -> P { ...R F } (F i) (fieldLens I R edit F i))
239        -> P A B lens
240let RecordField = \(A B : Type) -> exists (lens : Lens A B) * FieldOrigin A B lens
241let recordField =
242      \(I : Type) (R : (I -> Type) -> Row Type) (edit : RecordEdit I R) (F : I -> Type) (i : I) ->
243        ((fieldLens I R edit F i, \P consume -> consume I R edit F i) :
244           RecordField { ...R F } (F i))
245
246in  { Lens
247    , RecordEdit
248    , RecordField
249    , recordField
250    , fieldLens
251    , Field
252    , Interpretation
253    , Identity
254    , identityMonad
255    , original
256    , Build
257    , Fields
258    , Cases
259    , Match
260    , RecordCase
261    , UnionCase
262    , Conversion
263    , target
264    , unchanged
265    , recordConversion
266    , unionConversion
267    , IndexedMap
268    , MapLaws
269    , Inverse
270    , Iso
271    , mapIso
272    }
Your result
Complete file: lib/Reflection/Row.omg · with imports