Debian Codegraph @

vernac/opaques.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 (************************************************************************)
12 (** {6 Tables of opaque proof terms} *)
13
14 (** We now store opaque proof terms apart from the rest of the environment.
15 This way, we can quickly load a first half of a .vo file without these opaque
16 terms, and access them only when a specific command (e.g. Print or
17 Print Assumptions) needs it. *)
18
19 type 'a const_entry_body = 'a Entries.proof_output Future.computation
20
21 type opaque_result =
22 | OpaqueCertif of Safe_typing.opaque_certificate Future.computation
23 | OpaqueValue of Opaqueproof.opaque_proofterm
24
25 (** Current table of opaque terms *)
26
27 module Summary =
28 struct
29 type t = opaque_result Opaqueproof.HandleMap.t
30 let state : t ref = ref Opaqueproof.HandleMap.empty
31 let init () = state := Opaqueproof.HandleMap.empty
32 let freeze () = !state
33 let unfreeze s = state := s
34
35 let join ?(except=Future.UUIDSet.empty) () =
36 let iter i pf = match pf with
37 | OpaqueValue _ -> ()
38 | OpaqueCertif cert ->
39 if Future.UUIDSet.mem (Future.uuid cert) except then ()
40 else if Safe_typing.is_filled_opaque i (Global.safe_env ()) then
41 assert (Future.is_over cert)
42 else
43 (* Little belly dance to preserve the fix_exn wrapper around filling *)
44 Future.force @@ Future.chain cert (fun cert -> Global.fill_opaque cert)
45 in
46 Opaqueproof.HandleMap.iter iter !state
47
48 end
49
50 type opaque_disk = Opaqueproof.opaque_proofterm option array
51
52 let get_opaque_disk i t =
53 let i = Opaqueproof.repr_handle i in
54 let () = assert (0 <= i && i < Array.length t) in
55 t.(i)
56
57 let set_opaque_disk i (c, priv) t =
58 let i = Opaqueproof.repr_handle i in
59 let () = assert (0 <= i && i < Array.length t) in
60 let () = assert (Option.is_empty t.(i)) in
61 let _, c = Constr.hcons c in
62 t.(i) <- Some (c, priv)
63
64 let current_opaques = Summary.state
65
66 let declare_defined_opaque ?feedback_id i (body : Safe_typing.private_constants const_entry_body) =
67 (* Note that the environment in which the variable is checked it the one when
68 the thunk is evaluated, not the one where this function is called. It does
69 not matter because the former must be an extension of the latter or
70 otherwise the call to Safe_typing would throw an anomaly. *)
71 let proof = Future.chain body begin fun (body, eff) ->
72 let cert = Safe_typing.check_opaque (Global.safe_env ()) i (body, eff) in
73 let () = Option.iter (fun id -> Feedback.feedback ~id Feedback.Complete) feedback_id in
74 cert
75 end
76 in
77 (* If the proof is already computed we fill it eagerly *)
78 let () = match Future.peek_val proof with
79 | None -> ()
80 | Some cert -> Global.fill_opaque cert
81 in
82 let proof = OpaqueCertif proof in
83 let () = assert (not @@ Opaqueproof.HandleMap.mem i !current_opaques) in
84 current_opaques := Opaqueproof.HandleMap.add i proof !current_opaques
85
86 let declare_private_opaque opaque =
87 let (i, pf) = Safe_typing.repr_exported_opaque opaque in
88 (* Joining was already done at private declaration time *)
89 let proof = OpaqueValue pf in
90 let () = assert (not @@ Opaqueproof.HandleMap.mem i !current_opaques) in
91 current_opaques := Opaqueproof.HandleMap.add i proof !current_opaques
92
93 let get_current_opaque i =
94 try
95 let pf = Opaqueproof.HandleMap.find i !current_opaques in
96 match pf with
97 | OpaqueValue pf -> Some pf
98 | OpaqueCertif cert ->
99 let c, ctx = Safe_typing.repr_certificate (Future.force cert) in
100 let ctx = match ctx with
101 | Opaqueproof.PrivateMonomorphic _ -> Opaqueproof.PrivateMonomorphic ()
102 | Opaqueproof.PrivatePolymorphic _ as ctx -> ctx
103 in
104 Some (c, ctx)
105 with Not_found -> None
106
107 let get_current_constraints i =
108 try
109 let pf = Opaqueproof.HandleMap.find i !current_opaques in
110 match pf with
111 | OpaqueValue _ -> None
112 | OpaqueCertif cert ->
113 let _, ctx = Safe_typing.repr_certificate (Future.force cert) in
114 let ctx = match ctx with
115 | Opaqueproof.PrivateMonomorphic ctx -> ctx
116 | Opaqueproof.PrivatePolymorphic _ -> Univ.ContextSet.empty
117 in
118 Some ctx
119 with Not_found -> None
120
121 let dump ?(except=Future.UUIDSet.empty) () =
122 let n =
123 if Opaqueproof.HandleMap.is_empty !current_opaques then 0
124 else (Opaqueproof.repr_handle @@ fst @@ Opaqueproof.HandleMap.max_binding !current_opaques) + 1
125 in
126 let opaque_table = Array.make n None in
127 let fold h cu f2t_map = match cu with
128 | OpaqueValue p ->
129 let i = Opaqueproof.repr_handle h in
130 let () = opaque_table.(i) <- Some p in
131 f2t_map
132 | OpaqueCertif cert ->
133 let i = Opaqueproof.repr_handle h in
134 let f2t_map, proof =
135 let uid = Future.uuid cert in
136 let f2t_map = Future.UUIDMap.add uid h f2t_map in
137 let c = Future.peek_val cert in
138 let () = if Option.is_empty c && (not @@ Future.UUIDSet.mem uid except) then
139 CErrors.anomaly
140 Pp.(str"Proof object "++int i++str" is not checked nor to be checked")
141 in
142 f2t_map, c
143 in
144 let c = match proof with
145 | None -> None
146 | Some cert ->
147 let (c, priv) = Safe_typing.repr_certificate cert in
148 let priv = match priv with
149 | Opaqueproof.PrivateMonomorphic _ -> Opaqueproof.PrivateMonomorphic ()
150 | Opaqueproof.PrivatePolymorphic _ as p -> p
151 in
152 Some (c, priv)
153 in
154 let () = opaque_table.(i) <- c in
155 f2t_map
156 in
157 let f2t_map = Opaqueproof.HandleMap.fold fold !current_opaques Future.UUIDMap.empty in
158 (opaque_table, f2t_map)