package coq-lsp
Language Server Protocol native server for Coq
Install
Dune Dependency
Authors
Maintainers
Sources
coq-lsp-0.2.3.8.19.tbz
sha256=dd5d0993261d3742e77ccac8344307d97b507b265d8743ae0ce33d0b3fcfd98a
sha512=76727400b27900fdd659af7f03c5f2cd979f50ea0c76ad6f5b5de56a53b9db06dba1e1c786fd3e8ab695e42d94c53d58415c0c5b5eef8192f9863eaf7dcca693
doc/src/coq-lsp.petanque/agent.ml.html
Source file agent.ml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336
(************************************************************************) (* Flèche => RL agent: petanque *) (* Copyright 2019 MINES ParisTech -- Dual License LGPL 2.1 / GPL3+ *) (* Copyright 2019-2024 Inria -- Dual License LGPL 2.1 / GPL3+ *) (* Written by: Emilio J. Gallego Arias & coq-lsp contributors *) (************************************************************************) module State = struct type t = Coq.State.t let hash = Coq.State.hash let name = "state" module Inspect = struct type t = | Physical (** Flèche-based "almost physical" state eq *) | Goals (** Full goal equality; must faster than calling goals as it won't unelaborate them. Note that this may not fully capture proof state equality (it is possible to have similar goals but different evar_maps, but should be enough for all practical users. *) end let equal ?(kind = Inspect.Physical) = match kind with | Physical -> Coq.State.equal | Goals -> fun st1 st2 -> let st1, st2 = (Coq.State.lemmas ~st:st1, Coq.State.lemmas ~st:st2) in Option.equal Coq.Goals.Equality.equal_goals st1 st2 module Proof = struct type t = Coq.State.Proof.t let equal ?(kind = Inspect.Physical) = match kind with | Physical -> Coq.State.Proof.equal | Goals -> Coq.Goals.Equality.equal_goals let hash = Coq.State.Proof.hash end let lemmas st = Coq.State.lemmas ~st end (** Petanque errors *) module Error = struct type t = | Interrupted | Parsing of string | Coq of string | Anomaly of string | System of string | Theorem_not_found of string | No_state_at_point let to_string = function | Interrupted -> Format.asprintf "Interrupted" | Parsing msg -> Format.asprintf "Parsing: %s" msg | Coq msg -> Format.asprintf "Coq: %s" msg | Anomaly msg -> Format.asprintf "Anomaly: %s" msg | System msg -> Format.asprintf "System: %s" msg | Theorem_not_found msg -> Format.asprintf "Theorem_not_found: %s" msg | No_state_at_point -> Format.asprintf "No state at point" (* JSON-RPC server reserved codes *) let to_code = function | Interrupted -> -32001 | Parsing _ -> -32002 | Coq _ -> -32003 | Anomaly _ -> -32004 | System _ -> -32005 | Theorem_not_found _ -> -32006 | No_state_at_point -> -32007 let coq e = Coq e let system e = System e let make e ~feedback = Request.Error.make (to_code e) e ~feedback let make_request e = make e ~feedback:[] end module R = struct type 'a t = ('a, Error.t) Request.R.t end module Run_opts = struct type t = { memo : bool [@default true] ; hash : bool [@default true] } end module Run_result = struct type 'a t = { st : 'a ; hash : int option [@default None] ; proof_finished : bool ; feedback : (int * string) list } end let find_thm ~(doc : Fleche.Doc.t) ~thm = let { Fleche.Doc.toc; _ } = doc in let feedback = [] in match CString.Map.find_opt thm toc with | None -> let msg = Format.asprintf "@[[find_thm] Theorem not found!@]" in Error Error.(make (Theorem_not_found msg) ~feedback) | Some node -> ( (* If the node has an error we cannot assume the state is valid *) match List.filter Lang.Diagnostic.is_error node.diags with | [] -> Ok node | err :: _ -> let msg = Format.asprintf "@[[find_thm] Theorem found but failed with Coq error:@\n @[%a@]!@]" Pp.pp_with err.message in Error Error.(make (Theorem_not_found msg) ~feedback)) let execute_precommands ~token ~memo ~pre_commands ~(node : Fleche.Doc.Node.t) = match (pre_commands, node.prev, node.ast) with | Some pre_commands, Some prev, Some ast -> let st = prev.state in let open Coq.Protect.E.O in let* st = Fleche.Doc.run ~token ~memo ?loc:None ~st pre_commands in (* We re-interpret the lemma statement *) Fleche.Memo.Interp.eval ~token (st, ast.v) | _, _, _ -> Coq.Protect.E.ok node.state (* XXX Fix better by making protect errors and request errors share the loc type, so we can execute with Coq locations *) let clean_fb fbs = List.map (fun (lvl, { Coq.Message.Payload.msg; _ }) -> (lvl, { Coq.Message.Payload.range = None; quickFix = None; msg })) fbs let protect_to_result (r : _ Coq.Protect.E.t) : (_, _) Result.t = match r with | { r = Interrupted; feedback } -> let feedback = clean_fb feedback in Error Error.(make Interrupted ~feedback) | { r = Completed (Error (User { msg; _ })); feedback } -> let feedback = clean_fb feedback in Error Error.(make (Coq (Pp.string_of_ppcmds msg)) ~feedback) | { r = Completed (Error (Anomaly { msg; _ })); feedback } -> let feedback = clean_fb feedback in Error Error.(make (Anomaly (Pp.string_of_ppcmds msg)) ~feedback) | { r = Completed (Ok r); feedback } -> Ok (r feedback) let proof_finished { Coq.Goals.goals; stack; shelf; given_up; _ } = let check_stack stack = CList.(for_all (fun (l, r) -> is_empty l && is_empty r)) stack in List.for_all CList.is_empty [ goals; shelf; given_up ] && check_stack stack (* At some point we want to return both hashes *) module Hash_kind = struct type t = | Full | Proof [@@warning "-37"] let hash ~kind st = match kind with | Full -> Some (State.hash st) | Proof -> Option.map State.Proof.hash (State.lemmas st) end let hash_mode = Hash_kind.Proof (* XXX: duplicated with Request.R.of_execution, refactoring planned (not trivial) *) let fb_print_string (lvl, { Coq.Message.Payload.msg; _ }) = (lvl, Pp.string_of_ppcmds msg) let analyze_after_run ~hash st feedback = let proof_finished = let goals = Fleche.Info.Goals.get_goals_unit ~st in match goals with | None -> true | Some goals when proof_finished goals -> true | _ -> false in let hash = if hash then Hash_kind.hash ~kind:hash_mode st else None in let feedback = List.map fb_print_string feedback in Run_result.{ st; hash; proof_finished; feedback } (* Would be nice to keep this in sync with the type annotations. *) let default_opts = function | None -> { Run_opts.memo = true; hash = true } | Some opts -> opts let get_root_state ?opts ~doc () = let opts = default_opts opts in let hash = opts.hash in let state = doc.Fleche.Doc.root in Ok (analyze_after_run ~hash state []) let get_state_at_pos ?opts ~doc ~point () = match Fleche.Info.(LC.node ~doc ~point Exact) with | Some { Fleche.Doc.Node.state; _ } -> let opts = default_opts opts in let hash = opts.hash in Ok (analyze_after_run ~hash state []) | None -> Error (Error.make_request No_state_at_point) let start ~token ~doc ?opts ?pre_commands ~thm () = let open Coq.Compat.Result.O in let* node = find_thm ~doc ~thm in (* Usually single shot, so we don't memoize *) let opts = default_opts opts in let memo, hash = (opts.memo, opts.hash) in let execution = let open Coq.Protect.E.O in let+ st = execute_precommands ~token ~memo ~pre_commands ~node in (* Note this runs on the resulting state, anyways it is purely functional *) analyze_after_run ~hash st in protect_to_result execution let run ~token ?opts ~st ~tac () : (_ Run_result.t, Error.t) Request.R.t = let opts = default_opts opts in (* Improve with thm? *) let memo, hash = (opts.memo, opts.hash) in let execution = let open Coq.Protect.E.O in let+ st = Fleche.Doc.run ~token ~memo ?loc:None ~st tac in (* Note this runs on the resulting state, anyways it is purely functional *) analyze_after_run ~hash st in protect_to_result execution let goals ~token ~st = let f goals = let f = Coq.Goals.Reified_goal.map ~f:Pp.string_of_ppcmds in let g = Pp.string_of_ppcmds in (* XXX: Fixme, take feedback into account *) fun _feedback -> Option.map (Coq.Goals.map ~f ~g) goals in Coq.Protect.E.map ~f (Fleche.Info.Goals.goals ~token ~st) |> protect_to_result module Premise = struct module Info = struct type t = { kind : string (* type of object *) ; range : Lang.Range.t option (* a range *) ; offset : int * int (* a offset in the file *) ; raw_text : (string, string) Result.t (* raw text of the premise *) } end type t = { full_name : string (* should be a Coq DirPath, but let's go step by step *) ; file : string (* file (in FS format) where the premise is found *) ; info : (Info.t, string) Result.t (* Info about the object, if available *) } end (* We need some caching here otherwise it is very expensive to re-parse the glob files all the time. XXX move this caching to Flèche. *) module Memo = struct module H = Hashtbl.Make (CString) let table_glob = H.create 1000 let open_file glob = match H.find_opt table_glob glob with | Some g -> g | None -> let g = Coq.Glob.open_file glob in H.add table_glob glob g; g let table_source = H.create 1000 let input_source file = match H.find_opt table_source file with | Some res -> res | None -> if Sys.file_exists file then ( let res = Ok Coq.Compat.Ocaml_414.In_channel.(with_open_text file input_all) in H.add table_source file res; res) else let res = Error "source file is not available" in H.add table_source file res; res end let info_of ~glob ~name = let open Coq.Compat.Result.O in let* g = Memo.open_file glob in Ok (Option.map (fun { Coq.Glob.Info.kind; offset } -> (kind, offset)) (Coq.Glob.get_info g name)) let raw_of ~file ~offset = let bp, ep = offset in let open Coq.Compat.Result.O in let* c = Memo.input_source file in if String.length c < ep then Error "offset out of bounds" else Ok (String.sub c bp (ep - bp + 1)) let to_premise (p : Coq.Library_file.Entry.t) : Premise.t = let { Coq.Library_file.Entry.name; typ = _; file } = p in let file = Filename.(remove_extension file ^ ".v") in let glob = Filename.(remove_extension file ^ ".glob") in let info = match info_of ~glob ~name with | Ok None -> Error "not in glob table" | Error err -> Error err | Ok (Some (kind, offset)) -> let range = None in let raw_text = raw_of ~file ~offset in Ok { Premise.Info.kind; range; offset; raw_text } in { Premise.full_name = name; file; info } let premises ~token ~st = (let open Coq.Protect.E.O in let* all_libs = Coq.Library_file.loaded ~token ~st in let+ all_premises = Coq.Library_file.toc ~token ~st all_libs in (* XXX: Fixme, take feedback into account *) fun _feedback -> List.map to_premise all_premises) |> protect_to_result (* See PROTOCOL.md for details on versioning *) let version = 1
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>