std:Reflection/Table

lib/Reflection/Table.omg

lib/Reflection/Table.omg116 lines
1-- A flat record of indexed leaves. The same field index selects both inputs
2-- of zipFold. map preserves the labels while changing the interpretation of
3-- each leaf. A non-record root or non-leaf member cannot supply the evidence.
4let C = std:Reflection/Candidate
5let L = std:List
6let make =
7      \{l : Level} (Key : Type l) ->
8        let Families = { member : Key -> Type }
9        let Family = Families -> Type
10        let Walk =
11              \(S : Family) ->
12                { map :
13                    forall (A B : Families)
14                    -> (forall (k : Key) -> Text -> A.member k -> B.member k)
15                    -> Text
16                    -> S A
17                    -> S B
18                , zipFold :
19                    forall (A B : Families) (X : Type)
20                    -> (forall (k : Key) -> Text -> A.member k -> B.member k -> X -> X)
21                    -> Text
22                    -> S A
23                    -> S B
24                    -> X
25                    -> X
26                }
27        let Plan = \(S : Family) -> { leaf : C.Candidate (Walk S), table : C.Candidate (Walk S) }
28        let invalid =
29              \(S : Family) ->
30                ({ leaf = C.invalid (Walk S) "expected a declared member"
31                 , table = C.invalid (Walk S) "expected a record of members"
32                 } :
33                   Plan S)
34        let inspect =
35              \{D : Key -> Type} {H : Family} (shape : Reflect @1 { member = D } H) ->
36                shape.fold
37                  Plan
38                  { leaf =
39                      { member =
40                          \k ->
41                            let S = \(F : Families) -> F.member k
42                            in  invalid S
43                                // { leaf =
44                                       C.known
45                                         @(Walk S)
46                                         { map = \A B step label x -> step k label x
47                                         , zipFold = \A B X step label x y -> step k label x y
48                                         }
49                                   }
50                      }
51                  , bool = invalid (\_ -> Bool)
52                  , natural = invalid (\_ -> Natural)
53                  , integer = invalid (\_ -> Integer)
54                  , double = invalid (\_ -> Double)
55                  , text = invalid (\_ -> Text)
56                  , word = \n -> invalid (\_ -> Word n)
57                  , list = \A _ -> invalid (\F -> List (A F))
58                  , optional = \A _ -> invalid (\F -> Optional (A F))
59                  , pair = \A B _ _ -> invalid (\F -> A F * B F)
60                  , function = \A B _ _ -> invalid (\F -> A F -> B F)
61                  , union = \I row S ops children -> invalid (\F -> < ...row (\i -> S i F) >)
62                  , record =
63                      \I row S ops children ->
64                        let Root = \(F : Families) -> { ...row (\i -> S i F) }
65                        let members =
66                              C.gather
67                                I
68                                row
69                                { indices = ops.indices
70                                , field = ops.field
71                                , project = ops.project
72                                , build = ops.build
73                                }
74                                (\i -> Walk (S i))
75                                (\i -> (children i).leaf)
76                        in  invalid Root
77                            // { table =
78                                   C.map
79                                     (\(at : forall (i : I) -> Walk (S i)) ->
80                                        ({ map =
81                                             \A B step _ ->
82                                               ops.mapRecord.1
83                                                 (\i -> S i A)
84                                                 (\i -> S i B)
85                                                 (\i -> (at i).map A B step (ops.field i).name)
86                                         , zipFold =
87                                             \A B X step _ xs ys start ->
88                                               L.fold
89                                                 (\i rest ->
90                                                    (at i).zipFold
91                                                      A
92                                                      B
93                                                      X
94                                                      step
95                                                      (ops.field i).name
96                                                      (ops.project (\j -> S j A) i xs)
97                                                      (ops.project (\j -> S j B) i ys)
98                                                      rest)
99                                                 start
100                                                 ops.indices
101                                         } :
102                                           Walk Root))
103                                     members
104                               }
105                  }
106        let Valid =
107              \{D : Key -> Type} {H : Family} (shape : Reflect @1 { member = D } H) ->
108                std:Certificate.Valid (C.certify (inspect @D @H shape).table)
109        let select =
110              \{D : Key -> Type}
111               {H : Family}
112               (shape : Reflect @1 { member = D } H)
113               {valid : Valid @D @H shape} ->
114                C.select (inspect @D @H shape).table @valid
115        in  { Families, Walk, Valid, select }
116in  { make }
Your result
Complete file: lib/Reflection/Table.omg · with imports