package coq-lsp
Language Server Protocol native server for Coq
Install
Dune Dependency
Authors
Maintainers
Sources
coq-lsp-0.2.0.8.19.tbz
sha256=01ffedbd55ae00526fe1dda890e3c15a883bfda8388c8d292121ee722db46360
sha512=b5d92146c27b5c432a3f92c9ae8ff69c4dc62709f394743ca2d43ab93ae3af64b08bb17ea5923c0fbe0a333de127de574c548ec3c8df81dbe3e11f485abc3216
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
(************************************************************************) (* 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 *) (************************************************************************) let pet_debug = false 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 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 (* JSON-RPC server reserved codes *) let to_code = function | Interrupted -> -32001 | Parsing _ -> -32002 | Coq _ -> -32003 | Anomaly _ -> -32004 | System _ -> -32005 | Theorem_not_found _ -> -32006 let coq e = Coq e let system e = System e end module R = struct type 'a t = ('a, Error.t) Result.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 } end let find_thm ~(doc : Fleche.Doc.t) ~thm = let { Fleche.Doc.toc; _ } = doc in match CString.Map.find_opt thm toc with | None -> let msg = Format.asprintf "@[[find_thm] Theorem not found!@]" in Error (Error.Theorem_not_found msg) | Some node -> if pet_debug then Format.eprintf "@[[find_thm] Theorem found!@\n@]%!"; (* let point = (range.start.line, range.start.character) in *) Ok node let parse ~loc tac st = let str = Gramlib.Stream.of_string tac in let str = Coq.Parsing.Parsable.make ?loc str in Coq.Parsing.parse ~st str (* Adaptor, should be supported in memo directly *) let eval_no_memo ~token (st, cmd) = Coq.Interp.interp ~token ~intern:() ~st cmd let parse_and_execute_in ~token ~loc ~memo tac st = (* To improve in memo *) let eval = if memo then Fleche.Memo.Interp.eval else eval_no_memo in let open Coq.Protect.E.O in let* ast = parse ~token ~loc tac st in match ast with | Some ast -> eval ~token (st, ast) | None -> Coq.Protect.E.ok st 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 = parse_and_execute_in ~token ~memo ~loc:None pre_commands st in (* We re-interpret the lemma statement *) Fleche.Memo.Interp.eval ~token (st, ast.v) | _, _, _ -> Coq.Protect.E.ok node.state let protect_to_result (r : _ Coq.Protect.E.t) : (_, _) Result.t = match r with | { r = Interrupted; feedback = _ } -> Error Error.Interrupted | { r = Completed (Error (User (_loc, msg))); feedback = _ } -> Error (Error.Coq (Pp.string_of_ppcmds msg)) | { r = Completed (Error (Anomaly (_loc, msg))); feedback = _ } -> Error (Error.Anomaly (Pp.string_of_ppcmds msg)) | { r = Completed (Ok r); feedback = _ } -> Ok r let proof_finished { Coq.Goals.goals; stack; shelf; given_up; _ } = List.for_all CList.is_empty [ goals; shelf; given_up ] && CList.is_empty 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 let analyze_after_run ~hash st = 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 Run_result.{ st; hash; proof_finished } (* 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 (* XXX: EJGA, we should not need the [Coq.State.in_stateM] here and in run *) 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 f () = let opts = default_opts opts in let memo, hash = (opts.memo, opts.hash) in let open Coq.Protect.E.O in let+ st = execute_precommands ~token ~memo ~pre_commands ~node in analyze_after_run ~hash st in let st = node.state in Coq.State.in_stateM ~token ~st ~f () |> protect_to_result let run ~token ?opts ~st ~tac () : (_ Run_result.t, Error.t) Result.t = let opts = default_opts opts in (* Improve with thm? *) let loc = None in let memo, hash = (opts.memo, opts.hash) in let f st = let open Coq.Protect.E.O in let+ st = parse_and_execute_in ~token ~memo ~loc tac st in analyze_after_run ~hash st in Coq.State.in_stateM ~token ~st ~f st |> protect_to_result 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 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 List.map to_premise all_premises) |> protect_to_result
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>