std:Reflection/Candidate
lib/Reflection/Candidate.omg
lib/Reflection/Candidate.omg96 lines
1-- A computed shape may require evidence before it can produce an adapter.
2-- Have is inhabited; proving Need === Have supplies that evidence. Rejected
3-- shapes require an empty-union inhabitant, never a Boolean-to-proof cast.
4let Eq = std:Equal
5let C = std:Certificate
6let L = std:List
7let Candidate =
8 \{l : Level} (A : Type l) ->
9 exists (Need Have : Type) * { value : Have, build : Need -> A, problems : List Text }
10let need = \{l : Level} {A : Type l} (c : Candidate A) -> c.1
11let have = \{l : Level} {A : Type l} (c : Candidate A) -> c.2.1
12let supplied = \{l : Level} {A : Type l} (c : Candidate A) -> c.2.2.value
13let use = \{l : Level} {A : Type l} (c : Candidate A) -> c.2.2.build
14let known =
15 \{l : Level} {A : Type l} (value : A) ->
16 (({}, ({}, { value = {=}, build = \_ -> value, problems = [] })) : Candidate A)
17let invalid =
18 \{l : Level} (A : Type l) (why : Text) ->
19 ((<>, ({}, { value = {=}, build = \no -> absurd (\_ -> A) no, problems = [ why ] })) :
20 Candidate A)
21let map =
22 \{la lb : Level} {A : Type la} {B : Type lb} (f : A -> B) (c : Candidate A) ->
23 ((c.1,
24 (c.2.1,
25 { value = supplied c, build = \proof -> f (use c proof), problems = c.2.2.problems })) :
26 Candidate B)
27let both =
28 \{la lb : Level} {A : Type la} {B : Type lb} (a : Candidate A) (b : Candidate B) ->
29 ((a.1 * b.1,
30 (a.2.1 * b.2.1,
31 { value = (supplied a, supplied b)
32 , build = \proof -> (use a proof.1, use b proof.2)
33 , problems = a.2.2.problems # b.2.2.problems
34 })) :
35 Candidate (A * B))
36let certify =
37 \{l : Level} {A : Type l} (c : Candidate A) ->
38 C.make
39 c.1
40 c.2.1
41 (\equal -> c.2.2.build (Eq.subst (\(T : Type) -> T) (Eq.sym equal) c.2.2.value))
42let select =
43 \{l : Level} {A : Type l} (c : Candidate A) {valid : C.Valid (certify c)} ->
44 C.prove (certify c) @valid
45let Ops =
46 \(I : Type) (R : (I -> Type) -> Row Type) ->
47 { indices : List I
48 , field : I -> { name : Text, position : Optional Natural }
49 , project : forall (F : I -> Type) (i : I) -> { ...R F } -> F i
50 , build :
51 forall (F : I -> Type) (M : Type -> Type)
52 -> std:Monad.Monad M
53 -> (forall (i : I) -> M (F i))
54 -> M { ...R F }
55 }
56let Id = \(A : Type) -> A
57let identity =
58 { pure = \{A : Type} (value : A) -> value
59 , bind = \{A B : Type} (value : A) (after : A -> B) -> after value
60 }
61let gather =
62 \{l : Level}
63 (I : Type)
64 (R : (I -> Type) -> Row Type)
65 (ops : Ops I R)
66 (A : I -> Type l)
67 (children : forall (i : I) -> Candidate (A i)) ->
68 let Need = { ...R (\i -> (children i).1) }
69 let Have = { ...R (\i -> (children i).2.1) }
70 in ((Need,
71 (Have,
72 { value = ops.build (\i -> (children i).2.1) Id identity (\i -> supplied (children i))
73 , build = \proof i -> use (children i) (ops.project (\j -> (children j).1) i proof)
74 , problems =
75 L.concatMap
76 (\(i : I) ->
77 L.map
78 (\message -> (ops.field i).name ++ ": " ++ message)
79 (children i).2.2.problems)
80 ops.indices
81 })) :
82 Candidate (forall (i : I) -> A i))
83in { Candidate
84 , need
85 , have
86 , supplied
87 , use
88 , known
89 , invalid
90 , map
91 , both
92 , certify
93 , select
94 , Ops
95 , gather
96 }