std:Reflection/Structure
lib/Reflection/Structure.omg
lib/Reflection/Structure.omg167 lines
1-- Interpret structural evidence with typed values, projections and builders.
2-- This adapter is an ordinary fold over Reflect. Its function case is explicit:
3-- each consumer decides whether a function has a meaningful interpretation.
4let Rows = std:Reflection/Row
5let Field = Rows.Field
6let Build =
7 \{p : Level} (P : Type -> Type p) (R : Type) ->
8 forall (M : Type -> Type)
9 -> std:Monad.Monad M
10 -> (forall (A : Type) -> Text -> Optional Natural -> P A -> M A)
11 -> M R
12let Fields =
13 \{p : Level} (P : Type -> Type p) (R : Type) ->
14 forall (X : Type)
15 -> (forall (A : Type) -> Text -> Optional Natural -> P A -> (R -> A) -> X -> X)
16 -> X
17 -> X
18let Cases =
19 \{p : Level} (P : Type -> Type p) (U : Type) ->
20 forall (X : Type)
21 -> (forall (A : Type) -> Text -> Optional Natural -> P A -> (A -> U) -> X -> X)
22 -> X
23 -> X
24let Match =
25 \{p : Level} (P : Type -> Type p) (U : Type) ->
26 forall (X : Type)
27 -> (forall (A : Type) -> Text -> Optional Natural -> P A -> A -> X)
28 -> U
29 -> X
30let Algebra =
31 \{p lp : Level} (P : Type -> Type p) (Leaf : Type lp) ->
32 { leaf : Leaf
33 , bool : P Bool
34 , natural : P Natural
35 , integer : P Integer
36 , double : P Double
37 , text : P Text
38 , word : forall (bits : Natural) -> P (Word bits)
39 , list : forall (A : Type) -> P A -> P (List A)
40 , optional : forall (A : Type) -> P A -> P (Optional A)
41 , pair : forall (A B : Type) -> P A -> P B -> P (A * B)
42 , function : forall (A B : Type) -> P A -> P B -> P (A -> B)
43 , record : forall (R : Type) -> Build P R -> Fields P R -> P R
44 , union : forall (U : Type) -> Cases P U -> Match P U -> P U
45 }
46let Ops =
47 \(I : Type) (R : (I -> Type) -> Row Type) ->
48 { indices : List I
49 , field : I -> Field
50 , ordinal : I -> Natural
51 , project : forall (F : I -> Type) (i : I) -> { ...R F } -> F i
52 , build :
53 forall (F : I -> Type) (M : Type -> Type)
54 -> std:Monad.Monad M
55 -> (forall (i : I) -> M (F i))
56 -> M { ...R F }
57 , inject : forall (F : I -> Type) (i : I) -> F i -> < ...R F >
58 , match :
59 forall (F : I -> Type) (X : Type) -> (forall (i : I) -> F i -> X) -> < ...R F > -> X
60 , editRecord : Rows.RecordEdit I R
61 , mapRecord :
62 exists (map : Rows.IndexedMap I (\F -> { ...R F })) *
63 Rows.MapLaws I (\F -> { ...R F }) map
64 , mapUnion :
65 exists (map : Rows.IndexedMap I (\F -> < ...R F >)) *
66 Rows.MapLaws I (\F -> < ...R F >) map
67 }
68let FamilyAlgebra =
69 \{l p lp : Level} (Families : Type l) (P : (Families -> Type) -> Type p) (Leaf : Type lp) ->
70 { leaf : Leaf
71 , bool : P (\_ -> Bool)
72 , natural : P (\_ -> Natural)
73 , integer : P (\_ -> Integer)
74 , double : P (\_ -> Double)
75 , text : P (\_ -> Text)
76 , word : forall (bits : Natural) -> P (\_ -> Word bits)
77 , list : forall (A : Families -> Type) -> P A -> P (\F -> List (A F))
78 , optional : forall (A : Families -> Type) -> P A -> P (\F -> Optional (A F))
79 , pair : forall (A B : Families -> Type) -> P A -> P B -> P (\F -> A F * B F)
80 , function : forall (A B : Families -> Type) -> P A -> P B -> P (\F -> A F -> B F)
81 , record :
82 forall (I : Type) (R : (I -> Type) -> Row Type) (S : I -> Families -> Type)
83 -> Ops I R
84 -> (forall (i : I) -> P (S i))
85 -> P (\F -> { ...R (\i -> S i F) })
86 , union :
87 forall (I : Type) (R : (I -> Type) -> Row Type) (S : I -> Families -> Type)
88 -> Ops I R
89 -> (forall (i : I) -> P (S i))
90 -> P (\F -> < ...R (\i -> S i F) >)
91 }
92let algebra
93 : forall {l p lp : Level}
94 {Families : Type l}
95 {Leaf : Type lp}
96 (F : Families)
97 (P : Type -> Type p)
98 -> Algebra P Leaf
99 -> FamilyAlgebra Families (\S -> P (S F)) Leaf
100 = \{l} {p} {lp} {Families} {Leaf} F P a ->
101 { leaf = a.leaf
102 , bool = a.bool
103 , natural = a.natural
104 , integer = a.integer
105 , double = a.double
106 , text = a.text
107 , word = a.word
108 , list = \A child -> a.list (A F) child
109 , optional = \A child -> a.optional (A F) child
110 , pair = \A B first second -> a.pair (A F) (B F) first second
111 , function = \A B argument result -> a.function (A F) (B F) argument result
112 , record =
113 \I R S ops children ->
114 let T = \(i : I) -> S i F
115 in a.record
116 { ...R T }
117 (\M monad get ->
118 ops.build
119 T
120 M
121 monad
122 (\i -> get (T i) (ops.field i).name (ops.field i).position (children i)))
123 (\X step start ->
124 std:List.foldLeft
125 (\rest i ->
126 step
127 (T i)
128 (ops.field i).name
129 (ops.field i).position
130 (children i)
131 (ops.project T i)
132 rest)
133 start
134 ops.indices)
135 , union =
136 \I R S ops children ->
137 let T = \(i : I) -> S i F
138 in a.union
139 < ...R T >
140 (\X step start ->
141 std:List.foldLeft
142 (\rest i ->
143 step
144 (T i)
145 (ops.field i).name
146 (ops.field i).position
147 (children i)
148 (ops.inject T i)
149 rest)
150 start
151 ops.indices)
152 (\X step ->
153 ops.match
154 T
155 X
156 (\i -> step (T i) (ops.field i).name (ops.field i).position (children i)))
157 }
158let Of = \(A : Type) -> Reflect {=} (\(_ : {}) -> A)
159let interpret =
160 \{p : Level}
161 {S : {} -> Type}
162 (description : Reflect @p {=} S)
163 (F : {})
164 (P : Type -> Type p)
165 (a : Algebra P {}) ->
166 description.fold (\S -> P (S F)) (algebra F P a)
167in { Of, Field, Build, Fields, Cases, Match, Algebra, Ops, FamilyAlgebra, algebra, interpret }