std:Reflection/Interface
lib/Reflection/Interface.omg
lib/Reflection/Interface.omg297 lines
1-- Handler dictionaries are ordinary interpretations of generic reflection.
2-- A function node retains its full argument/result families; a record node
3-- replaces each function with the caller's wrapper and certifies every field.
4-- Target families are computed independently of validation evidence, so using
5-- a handler type does not normalize a proof transport.
6let Rows = std:Reflection/Row
7let Structure = std:Reflection/Structure
8let Candidate = std:Reflection/Candidate
9let Certificate = std:Certificate
10let Eq = std:Equal
11let L = std:List
12let Wrapper = Type -> Type -> Type
13let Function = \(A B : Type) -> A -> B
14let Id = \(A : Type) -> A
15let identity =
16 { pure = \{A : Type} (x : A) -> x
17 , bind = \{A B : Type} (x : A) (f : A -> B) -> f x
18 }
19let castIso =
20 \(A B : Type) (proof : A === B) ->
21 let same
22 : Rows.Iso A A
23 = (\(x : A) -> x, (\(x : A) -> x, { left = \x -> refl, right = \x -> refl }))
24 in Eq.subst (\T -> Rows.Iso A T) proof same
25
26-- Leaf describes only the caller's custom vocabulary. All structural cases,
27-- row transformations and validation are shared across vocabularies.
28let make =
29 \{l : Level}
30 (Families : Type l)
31 (Leaf : ((Families -> Type) -> Type) -> Type)
32 {Formers : Type}
33 (formers : Formers) ->
34 let Family = Families -> Type
35 let Arrow = \(A B : Family) (F : Families) -> A F -> B F
36 let Boundary =
37 \(S : Family) ->
38 { formers : Formers
39 , fold :
40 forall (P : Family -> Type)
41 -> Structure.FamilyAlgebra Families P (Leaf P)
42 -> P S
43 }
44 let Method =
45 \(S : Family) ->
46 exists (A B : Family) *
47 { arguments : Boundary A
48 , result : Boundary B
49 , same : Candidate.Candidate (S === Arrow A B)
50 }
51 let Handlers =
52 \(S : Family) (H : Families -> Wrapper -> Type) ->
53 { formers : Formers
54 , tabulateHandlers :
55 forall (F : Families) (M : Wrapper)
56 -> (forall (A B : Family)
57 -> Text
58 -> Optional Natural
59 -> Boundary A
60 -> Boundary B
61 -> M (A F) (B F))
62 -> H F M
63 , mapHandlers :
64 forall (F : Families) (M N : Wrapper)
65 -> (forall (A B : Type) -> Text -> Optional Natural -> M A B -> N A B)
66 -> H F M
67 -> H F N
68 , foldHandlers :
69 forall (F : Families) (M : Wrapper) (X : Type)
70 -> (forall (A B : Family)
71 -> Text
72 -> Optional Natural
73 -> Boundary A
74 -> Boundary B
75 -> M (A F) (B F)
76 -> X
77 -> X)
78 -> H F M
79 -> X
80 -> X
81 , ordinary : forall (F : Families) -> Rows.Iso (S F) (H F Function)
82 }
83 let Package = \(S : Family) -> exists (H : Families -> Wrapper -> Type) * Handlers S H
84 let Proposed =
85 \(S : Family) ->
86 exists (H : Families -> Wrapper -> Type) * Candidate.Candidate (Handlers S H)
87 -- Capture the proposal before accepting evidence. Both the obligation
88 -- and the resulting package then share the same derived dictionary.
89 let Valid =
90 \{S : Family} (proposed : Proposed S) ->
91 Certificate.Valid (Candidate.certify proposed.2)
92 let select =
93 \{S : Family} (proposed : Proposed S) {valid : Valid @S proposed} ->
94 ((proposed.1, Candidate.select proposed.2 @valid) : Package S)
95 let Plan =
96 \(S : Family) ->
97 { reflection : Boundary S
98 , method : Method S
99 , interface : Proposed S
100 }
101 let fromBoundary =
102 \{S : Family} (reflection : Boundary S) ->
103 ({ reflection
104 , method =
105 (\(_ : Families) -> Bool,
106 (\(_ : Families) -> Bool,
107 { arguments = { formers, fold = \P a -> a.bool }
108 , result = { formers, fold = \P a -> a.bool }
109 , same =
110 Candidate.invalid
111 (S === Arrow (\_ -> Bool) (\_ -> Bool))
112 "an explicit function is required"
113 }))
114 , interface =
115 (\(_ : Families) (_ : Wrapper) -> {},
116 Candidate.invalid
117 (Handlers S (\_ _ -> {}))
118 "a record of functions is required")
119 } :
120 Plan S)
121 let rowInterface =
122 \(I : Type)
123 (R : (I -> Type) -> Row Type)
124 (S : I -> Family)
125 (ops : Structure.Ops I R)
126 (methods : forall (i : I) -> Method (S i)) ->
127 let Root = \(F : Families) -> { ...R (\i -> S i F) }
128 let Args = \(i : I) -> (methods i).1
129 let Result = \(i : I) -> (methods i).2.1
130 let info = \(i : I) -> (methods i).2.2
131 let H = \(F : Families) (M : Wrapper) -> { ...R (\i -> M (Args i F) (Result i F)) }
132 let checked =
133 Candidate.gather
134 I
135 R
136 { indices = ops.indices
137 , field = ops.field
138 , project = ops.project
139 , build = ops.build
140 }
141 (\i -> S i === Arrow (Args i) (Result i))
142 (\i -> (info i).same)
143 in ((H,
144 Candidate.map
145 (\(proofs : forall (i : I) -> S i === Arrow (Args i) (Result i)) ->
146 { formers
147 , tabulateHandlers =
148 \F M step ->
149 ops.build
150 (\i -> M (Args i F) (Result i F))
151 Id
152 identity
153 (\i ->
154 step
155 (Args i)
156 (Result i)
157 (ops.field i).name
158 (ops.field i).position
159 (info i).arguments
160 (info i).result)
161 , mapHandlers =
162 \F M N step ->
163 ops.mapRecord.1
164 (\i -> M (Args i F) (Result i F))
165 (\i -> N (Args i F) (Result i F))
166 (\i value ->
167 step
168 (Args i F)
169 (Result i F)
170 (ops.field i).name
171 (ops.field i).position
172 value)
173 , foldHandlers =
174 \F M X step handlers start ->
175 L.fold
176 (\(i : I) rest ->
177 step
178 (Args i)
179 (Result i)
180 (ops.field i).name
181 (ops.field i).position
182 (info i).arguments
183 (info i).result
184 (ops.project (\j -> M (Args j F) (Result j F)) i handlers)
185 rest)
186 start
187 ops.indices
188 , ordinary =
189 \F ->
190 Rows.mapIso
191 I
192 (\T -> { ...R T })
193 ops.mapRecord.1
194 ops.mapRecord.2
195 (\i -> S i F)
196 (\i -> Args i F -> Result i F)
197 (\i ->
198 castIso
199 (S i F)
200 (Args i F -> Result i F)
201 (Eq.cong (\(T : Family) -> T F) (proofs i)))
202 })
203 checked) :
204 Proposed Root)
205 let algebra =
206 { bool = fromBoundary { formers, fold = \P a -> a.bool }
207 , natural = fromBoundary { formers, fold = \P a -> a.natural }
208 , integer = fromBoundary { formers, fold = \P a -> a.integer }
209 , double = fromBoundary { formers, fold = \P a -> a.double }
210 , text = fromBoundary { formers, fold = \P a -> a.text }
211 , word = \(bits : Natural) -> fromBoundary { formers, fold = \P a -> a.word bits }
212 , list =
213 \(A : Family) (child : Plan A) ->
214 fromBoundary { formers, fold = \P a -> a.list A (child.reflection.fold P a) }
215 , optional =
216 \(A : Family) (child : Plan A) ->
217 fromBoundary
218 { formers, fold = \P a -> a.optional A (child.reflection.fold P a) }
219 , pair =
220 \(A B : Family) (first : Plan A) (second : Plan B) ->
221 fromBoundary
222 { formers
223 , fold =
224 \P a ->
225 a.pair A B (first.reflection.fold P a) (second.reflection.fold P a)
226 }
227 , function =
228 \(A B : Family) (argument : Plan A) (result : Plan B) ->
229 let reflected
230 : Boundary (Arrow A B)
231 = { formers
232 , fold =
233 \P a ->
234 a.function
235 A
236 B
237 (argument.reflection.fold P a)
238 (result.reflection.fold P a)
239 }
240 in fromBoundary reflected
241 // { method =
242 ((A,
243 (B,
244 { arguments = argument.reflection
245 , result = result.reflection
246 , same = Candidate.known refl
247 })) :
248 Method (Arrow A B))
249 }
250 , record =
251 \(I : Type)
252 (R : (I -> Type) -> Row Type)
253 (S : I -> Family)
254 (ops : Structure.Ops I R)
255 (children : forall (i : I) -> Plan (S i)) ->
256 let reflected
257 : Boundary (\F -> { ...R (\i -> S i F) })
258 = { formers
259 , fold =
260 \P a -> a.record I R S ops (\i -> (children i).reflection.fold P a)
261 }
262 in fromBoundary reflected
263 // { interface = rowInterface I R S ops (\i -> (children i).method) }
264 , union =
265 \(I : Type)
266 (R : (I -> Type) -> Row Type)
267 (S : I -> Family)
268 (ops : Structure.Ops I R)
269 (children : forall (i : I) -> Plan (S i)) ->
270 fromBoundary
271 { formers
272 , fold = \P a -> a.union I R S ops (\i -> (children i).reflection.fold P a)
273 }
274 }
275 in { Boundary
276 , Method
277 , Handlers
278 , Package
279 , Proposed
280 , Valid
281 , select
282 , Plan
283 , fromBoundary
284 , algebra
285 }
286
287let Native = make {} (\(_ : ({} -> Type) -> Type) -> {}) {=}
288let inspectNative =
289 \{S : {} -> Type} (description : Reflect @1 {=} S) ->
290 description.fold Native.Plan (Native.algebra // { leaf = {=} })
291let NativeValid =
292 \{S : {} -> Type} (description : Reflect @1 {=} S) ->
293 Native.Valid @S (inspectNative @S description).interface
294let native =
295 \{S : {} -> Type} (description : Reflect @1 {=} S) ->
296 Native.select @S (inspectNative @S description).interface
297in { make, Native, NativeValid, native }