Debian Codegraph @

vernac/future.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 let not_ready_msg = ref (fun name ->
12 Pp.strbrk("The value you are asking for ("^name^") is not ready yet. "^
13 "Please wait or pass "^
14 "the \"-async-proofs off\" option to Rocqide to disable "^
15 "asynchronous script processing and don't pass \"-vio\" to "^
16 "rocq compile."))
17 let not_here_msg = ref (fun name ->
18 Pp.strbrk("The value you are asking for ("^name^") is not available "^
19 "in this process. If you really need this, pass "^
20 "the \"-async-proofs off\" option to Rocqide to disable "^
21 "asynchronous script processing and don't pass \"-vio\" to "^
22 "rocq compile."))
23
24 exception NotReady of string
25 exception NotHere of string
26
27 let _ = CErrors.register_handler (function
28 | NotReady name -> Some (!not_ready_msg name)
29 | NotHere name -> Some (!not_here_msg name)
30 | _ -> None)
31
32 type fix_exn = Stateid.exn_info option
33
34 let eval_fix_exn f (e, info) = match f with
35 | None -> (e, info)
36 | Some { Stateid.id; valid } ->
37 match Stateid.get info with
38 | Some _ -> (e, info)
39 | None ->
40 let loc = Loc.get_loc info in
41 let msg = CErrors.iprint (e, info) in
42 let qf = Result.value ~default:[] (Quickfix.from_exception e) in
43 let () = Feedback.(feedback ~id (Message (Error, loc, qf, msg))) in
44 (e, Stateid.add info ~valid id)
45
46 module UUID = struct
47 type t = int
48 let invalid = 0
49 let fresh =
50 let count = ref invalid in
51 fun () -> incr count; !count
52
53 let compare = compare
54 let equal = (==)
55 end
56
57 module UUIDMap = Map.Make(UUID)
58 module UUIDSet = Set.Make(UUID)
59
60 type 'a assignment = [ `Val of 'a | `Exn of Exninfo.iexn | `Comp of (unit -> 'a)]
61
62 (* Val is not necessarily a final state, so the
63 computation restarts from the state stocked into Val *)
64 and 'a comp =
65 | Delegated of (Mutex.t * Condition.t) option
66 | Closure of (unit -> 'a)
67 | Val of 'a
68 | Exn of Exninfo.iexn (* Invariant: this exception is always "fixed" as in fix_exn *)
69
70 and 'a computation =
71 | Ongoing of string * (UUID.t * fix_exn * 'a comp ref) CEphemeron.key
72
73 let unnamed = "unnamed"
74
75 let create ?(name=unnamed) ?(uuid=UUID.fresh ()) ~fix_exn x =
76 Ongoing (name, CEphemeron.create (uuid, fix_exn, ref x))
77 let get x =
78 match x with
79 | Ongoing (name, x) ->
80 try let uuid, fix, c = CEphemeron.get x in name, uuid, fix, c
81 with CEphemeron.InvalidKey ->
82 name, UUID.invalid, None, ref (Exn (NotHere name, Exninfo.null))
83
84 type 'a value = [ `Val of 'a | `Exn of Exninfo.iexn ]
85
86 let is_over kx = let _, _, _, x = get kx in match !x with
87 | Val _ | Exn _ -> true
88 | Closure _ | Delegated _ -> false
89
90 let is_exn kx = let _, _, _, x = get kx in match !x with
91 | Exn _ -> true
92 | Val _ | Closure _ | Delegated _ -> false
93
94 let peek_val kx = let _, _, _, x = get kx in match !x with
95 | Val v -> Some v
96 | Exn _ | Closure _ | Delegated _ -> None
97
98 let uuid kx = let _, id, _, _ = get kx in id
99
100 let from_val v = create ~fix_exn:None (Val v)
101
102 let create_delegate ?(blocking=true) ~name fix_exn =
103 let sync =
104 if blocking then Some (Mutex.create (), Condition.create ())
105 else None
106 in
107 let ck = create ~name ~fix_exn (Delegated sync) in
108 let assignment = fun v ->
109 let _, _, fix_exn, c = get ck in
110 let sync = match !c with Delegated s -> s | _ -> assert false in
111 begin match v with
112 | `Val v -> c := Val v
113 | `Exn e -> c := Exn (eval_fix_exn fix_exn e)
114 | `Comp f -> c := Closure f end;
115 let iter (lock, cond) = CThread.with_lock lock ~scope:(fun () -> Condition.broadcast cond) in
116 Option.iter iter sync
117 in
118 ck, assignment
119
120 (* TODO: get rid of try/catch to be stackless *)
121 let rec compute ck : 'a value =
122 let name, _, fix_exn, c = get ck in
123 match !c with
124 | Val x -> `Val x
125 | Exn (e, info) -> `Exn (e, info)
126 | Delegated None -> raise (NotReady name)
127 | Delegated (Some (lock, cond)) ->
128 CThread.with_lock lock ~scope:(fun () -> Condition.wait cond lock); compute ck
129 | Closure f ->
130 try
131 let data = f () in
132 c := Val data; `Val data
133 with e ->
134 let e = Exninfo.capture e in
135 let e = eval_fix_exn fix_exn e in
136 match e with
137 | (NotReady _, _) -> `Exn e
138 | _ -> c := Exn e; `Exn e
139
140 let force x = match compute x with
141 | `Val v -> v
142 | `Exn e -> Exninfo.iraise e
143
144 let chain ck f =
145 let name, uuid, fix_exn, c = get ck in
146 create ~uuid ~name ~fix_exn (match !c with
147 | Closure _ | Delegated _ -> Closure (fun () -> f (force ck))
148 | Exn _ as x -> x
149 | Val v -> Val (f v))
150
151 let create ~fix_exn f = create ~fix_exn (Closure f)
152
153 let replace kx y =
154 let _, _, _, x = get kx in
155 match !x with
156 | Exn _ -> x := Closure (fun () -> force y)
157 | _ -> CErrors.anomaly
158 (Pp.str "A computation can be replaced only if is_exn holds.")
159
160 let chain x f =
161 let y = chain x f in
162 if is_over x then ignore(force y);
163 y
164
165 let print f kx =
166 let open Pp in
167 let name, uid, _, x = get kx in
168 let uid =
169 if UUID.equal uid UUID.invalid then str "[#:" ++ str name ++ str "]"
170 else str "[" ++ int uid ++ str":" ++ str name ++ str "]"
171 in
172 match !x with
173 | Delegated _ -> str "Delegated" ++ uid
174 | Closure _ -> str "Closure" ++ uid
175 | Val x -> str "PureVal" ++ uid ++ spc () ++ hov 0 (f x)
176 | Exn (e, _) -> str "Exn" ++ uid ++ spc () ++ hov 0 (str (Printexc.to_string e))