Debian Codegraph @

vernac/vernactypes.ml

raw symbols SCIP
1
2 (** [('a,'b,'x) runner] means that any function taking ['a] and
3 returning ['b] and some additional data can be interpreted as a
4 function on a state ['x].
5
6 The additional return data ['d] is useful when combining runners.
7 We don't need an additional input data as it can just go in the closure.
8 *)
9 type ('a,'b,'x) runner = { run : 'd. 'x -> ('a -> 'b * 'd) -> 'x * 'd }
10
11
12 module Prog = struct
13
14 type state = Declare.OblState.t
15 type stack = state NeList.t
16
17 type (_,_) t =
18 | Ignore : (unit, unit) t
19 | Modify : (state, state) t
20 | Read : (state, unit) t
21 | Push : (unit, unit) t
22 | Pop : (state, unit) t
23
24 let runner (type a b) (ty:(a,b) t) : (a,b,stack) runner =
25 { run = fun pm f ->
26 match ty with
27 | Ignore -> let (), v = f () in pm, v
28 | Modify ->
29 let st, pm = NeList.repr pm in
30 let st, v = f st in
31 NeList.of_repr (st,pm), v
32 | Read ->
33 let (), v = f (NeList.head pm) in
34 pm, v
35 | Push ->
36 let (), v = f () in
37 NeList.push Declare.OblState.empty (Some pm), v
38 | Pop ->
39 let st, pm = NeList.repr pm in
40 assert (not (CList.is_empty pm));
41 let (), v = f st in
42 NeList.of_list pm, v
43 }
44
45 end
46
47 module Proof = struct
48 module LStack = Vernacstate.LemmaStack
49
50 type state = Declare.Proof.t
51 type stack = LStack.t option
52
53 type (_,_) t =
54 | Ignore : (unit, unit) t
55 | Modify : (state, state) t
56 | Read : (state, unit) t
57 | ReadOpt : (state option, unit) t
58 | Reject : (unit, unit) t
59 | Close : (state, unit) t
60 | Open : (unit, state) t
61
62 let use = function
63 | None -> CErrors.user_err (Pp.str "Command not supported (No proof-editing in progress).")
64 | Some stack -> LStack.pop stack
65
66 let runner (type a b) (ty:(a,b) t) : (a,b,stack) runner =
67 { run = fun stack f ->
68 match ty with
69 | Ignore -> let (), v = f () in stack, v
70 | Modify ->
71 let p, rest = use stack in
72 let p, v = f p in
73 Some (LStack.push rest p), v
74 | Read ->
75 let p, _ = use stack in
76 let (), v = f p in
77 stack, v
78 | ReadOpt ->
79 let p = Option.map LStack.get_top stack in
80 let (), v = f p in
81 stack, v
82 | Reject ->
83 let () = if Option.has_some stack
84 then CErrors.user_err (Pp.str "Command not supported (Open proofs remain).")
85 in
86 let (), v = f () in
87 stack, v
88 | Close ->
89 let p, rest = use stack in
90 let (), v = f p in
91 rest, v
92 | Open ->
93 let p, v = f () in
94 Some (LStack.push stack p), v
95 }
96
97 end
98
99 module OpaqueAccess = struct
100
101 (* Modification of opaque tables (by Require registering foreign
102 tables and Qed/abstract/etc adding entries to the local table)
103 is currently not tracked by vernactypes.
104 *)
105 type _ t =
106 | Ignore : unit t
107 | Access : Global.indirect_accessor t
108
109 let access = Library.indirect_accessor[@@warning "-3"]
110
111 let runner (type a) (ty:a t) : (a,unit,unit) runner =
112 { run = fun () f ->
113 match ty with
114 | Ignore -> let (), v = f () in (), v
115 | Access -> let (), v = f access in (), v
116 }
117
118 end
119
120 (* lots of messing with tuples in there, can we do better? *)
121 let combine_runners (type a b x c d y) (r1:(a,b,x) runner) (r2:(c,d,y) runner)
122 : (a*c, b*d, x*y) runner
123 = { run = fun (x,y) f ->
124 match r1.run x @@ fun x ->
125 match r2.run y @@ fun y ->
126 match f (x,y)
127 with ((b, d), o) -> (d, (b, o))
128 with (y, (b, o)) -> (b, (y, o))
129 with (x, (y, o)) -> ((x, y), o) }
130
131 type ('prog,'proof,'opaque_access) state_gen = {
132 prog : 'prog;
133 proof : 'proof;
134 opaque_access : 'opaque_access;
135 }
136
137 let tuple { prog; proof; opaque_access } = (prog, proof), opaque_access
138 let untuple ((prog, proof), opaque_access) = { prog; proof; opaque_access }
139
140 type no_state = (unit, unit, unit) state_gen
141 let no_state = { prog = (); proof = (); opaque_access = (); }
142
143 let ignore_state = { prog = Prog.Ignore; proof = Proof.Ignore; opaque_access = OpaqueAccess.Ignore }
144
145 type 'r typed_vernac_gen =
146 TypedVernac : {
147 spec : (('inprog, 'outprog) Prog.t,
148 ('inproof, 'outproof) Proof.t,
149 'inaccess OpaqueAccess.t) state_gen;
150 run : ('inprog, 'inproof, 'inaccess) state_gen -> ('outprog, 'outproof, unit) state_gen * 'r;
151 } -> 'r typed_vernac_gen
152
153 let map_typed_vernac f (TypedVernac {spec; run}) =
154 TypedVernac {spec; run = (fun st -> Util.on_snd f (run st)) }
155
156 type typed_vernac = unit typed_vernac_gen
157
158 type full_state = (Prog.stack,Vernacstate.LemmaStack.t option,unit) state_gen
159
160 let run (TypedVernac { spec = { prog; proof; opaque_access }; run }) (st:full_state) : full_state * _ =
161 let ( * ) = combine_runners in
162 let runner = Prog.runner prog * Proof.runner proof * OpaqueAccess.runner opaque_access in
163 let st, v = runner.run (tuple st) @@ fun st ->
164 let st, v= run @@ untuple st in tuple st, v
165 in
166 untuple st, v
167
168 let typed_vernac_gen spec run = TypedVernac { spec; run }
169
170 let typed_vernac spec run = TypedVernac { spec; run = (fun st -> run st, () ) }
171
172 let vtdefault f = typed_vernac ignore_state
173 (fun (_:no_state) -> let () = f () in no_state)
174
175 let vtnoproof f = typed_vernac { ignore_state with proof = Reject }
176 (fun (_:no_state) -> let () = f () in no_state)
177
178 let vtcloseproof f = typed_vernac { ignore_state with prog = Modify; proof = Close }
179 (fun {prog; proof} -> let prog = f ~lemma:proof ~pm:prog in { no_state with prog })
180
181 let vtopenproof f = typed_vernac { ignore_state with proof = Open }
182 (fun (_:no_state) -> let proof = f () in { no_state with proof })
183
184 let vtmodifyproof f = typed_vernac { ignore_state with proof = Modify }
185 (fun {proof} -> let proof = f ~pstate:proof in { no_state with proof })
186
187 let vtreadproofopt f = typed_vernac { ignore_state with proof = ReadOpt }
188 (fun {proof} -> let () = f ~pstate:proof in no_state)
189
190 let vtreadproof f = typed_vernac { ignore_state with proof = Read }
191 (fun {proof} -> let () = f ~pstate:proof in no_state)
192
193 let vtreadprogram f = typed_vernac { ignore_state with prog = Read }
194 (fun {prog} -> let () = f ~pm:prog in no_state)
195
196 let vtmodifyprogram f = typed_vernac { ignore_state with prog = Modify }
197 (fun {prog} -> let prog = f ~pm:prog in { no_state with prog })
198
199 let vtdeclareprogram f = typed_vernac { ignore_state with prog = Read; proof = Open }
200 (fun {prog} -> let proof = f ~pm:prog in { no_state with proof })
201
202 let vtopenproofprogram f = typed_vernac { ignore_state with prog = Modify; proof = Open }
203 (fun {prog} -> let prog, proof = f ~pm:prog in { no_state with prog; proof; })
204
205 let vtopaqueaccess f = typed_vernac { ignore_state with opaque_access = Access }
206 (fun {opaque_access} -> let () = f ~opaque_access in no_state)