Skip to content

Commit

Permalink
[fleche] Prepare for caching / sharing of .vo contents
Browse files Browse the repository at this point in the history
For now we just update the types to get ready for coq/coq#17393 , we
will add the cache in the next commit.

Based on work from #641
  • Loading branch information
ejgallego committed Jun 5, 2024
1 parent 4ce6614 commit 2e5ed56
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 21 deletions.
8 changes: 4 additions & 4 deletions coq/init.ml
Original file line number Diff line number Diff line change
Expand Up @@ -85,19 +85,19 @@ let coq_init opts =
(**************************************************************************)

(* Inits the context for a document *)
let doc_init ~root_state ~workspace ~uri () =
let doc_init ~intern ~root_state ~workspace ~uri () =
(* Lsp.Io.log_error "init" "starting"; *)
Vernacstate.unfreeze_full_state (State.to_coq root_state);

(* Set load paths from workspace info. *Important*, this has to happen before
we declare the library below as [Declaremods/Library] will infer the module
name by looking at the load path! *)
Workspace.apply ~uri workspace;
Workspace.apply ~intern ~uri workspace;

(* We return the state at this point! *)
Vernacstate.freeze_full_state () |> State.of_coq

let doc_init ~token:_ ~root_state ~workspace ~uri =
let doc_init ~token:_ ~intern ~root_state ~workspace ~uri =
(* Don't interrupt document creation. *)
let token = Limits.create_atomic () in
Protect.eval ~token ~f:(doc_init ~root_state ~workspace ~uri) ()
Protect.eval ~token ~f:(doc_init ~intern ~root_state ~workspace ~uri) ()
1 change: 1 addition & 0 deletions coq/init.mli
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ val coq_init : coq_opts -> State.t

val doc_init :
token:Limits.Token.t
-> intern:Library.Intern.t
-> root_state:State.t
-> workspace:Workspace.t
-> uri:Lang.LUri.File.t
Expand Down
13 changes: 6 additions & 7 deletions coq/interp.ml
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,20 @@
(* Written by: Emilio J. Gallego Arias *)
(************************************************************************)

let coq_interp ~st cmd =
let coq_interp ~intern ~st cmd =
let st = State.to_coq st in
let cmd = Ast.to_coq cmd in
let intern = Vernacinterp.fs_intern in
Vernacinterp.interp ~intern ~st cmd |> State.of_coq

let interp ~token ~st cmd = Protect.eval ~token cmd ~f:(coq_interp ~st)
let interp ~token ~intern ~st cmd =
Protect.eval ~token cmd ~f:(coq_interp ~intern ~st)

module Require = struct
(* We could improve this Coq upstream by making the API a bit more
orthogonal *)
let interp ~st _files
let interp ~intern ~st _files
{ Ast.Require.from; export; mods; loc = _; attrs; control } =
let () = Vernacstate.unfreeze_full_state (State.to_coq st) in
let intern = Vernacinterp.fs_intern in
let fn () = Vernacentries.vernac_require ~intern from export mods in
(* Check generic attributes *)
let fn () =
Expand All @@ -42,6 +41,6 @@ module Require = struct
let () = Utils.with_control ~fn ~control ~st in
Vernacstate.freeze_full_state () |> State.of_coq

let interp ~token ~st files cmd =
Protect.eval ~token ~f:(interp ~st files) cmd
let interp ~token ~intern ~st files cmd =
Protect.eval ~token ~f:(interp ~intern ~st files) cmd
end
7 changes: 6 additions & 1 deletion coq/interp.mli
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@
assumed not to interact with the file-system, etc... Note these commands
will be memoized. *)
val interp :
token:Limits.Token.t -> st:State.t -> Ast.t -> (State.t, Loc.t) Protect.E.t
token:Limits.Token.t
-> intern:Library.Intern.t
-> st:State.t
-> Ast.t
-> (State.t, Loc.t) Protect.E.t

(** Interpretation of "require". We wrap this function for two reasons:
Expand All @@ -28,6 +32,7 @@ val interp :
module Require : sig
val interp :
token:Limits.Token.t
-> intern:Library.Intern.t
-> st:State.t
-> Files.t
-> Ast.Require.t
Expand Down
7 changes: 3 additions & 4 deletions coq/workspace.ml
Original file line number Diff line number Diff line change
Expand Up @@ -262,11 +262,10 @@ let describe_guess = function
| Error msg -> (msg, "")

(* Require a set of libraries *)
let load_objs libs =
let load_objs ~intern libs =
let rq_file { Require.library; from; flags } =
let mp = Libnames.qualid_of_string library in
let mfrom = Option.map Libnames.qualid_of_string from in
let intern = Vernacinterp.fs_intern in
Flags_.silently
(Vernacentries.vernac_require ~intern mfrom flags)
[ (mp, Vernacexpr.ImportAll) ]
Expand Down Expand Up @@ -296,7 +295,7 @@ let dirpath_of_uri ~uri =
ldir

(* NOTE: Use exhaustive match below to avoid bugs by skipping fields *)
let apply ~uri
let apply ~intern ~uri
{ coqlib = _
; coqcorelib = _
; ocamlpath
Expand All @@ -315,7 +314,7 @@ let apply ~uri
findlib_init ~ml_include_path ~ocamlpath;
List.iter Loadpath.add_vo_path vo_load_path;
Declaremods.start_library (dirpath_of_uri ~uri);
load_objs require_libs
load_objs ~intern require_libs

(* This can raise, and will do in incorrect CoqProject files *)
let dirpath_of_string_exn coq_path = Libnames.dirpath_of_string coq_path
Expand Down
2 changes: 1 addition & 1 deletion coq/workspace.mli
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ val guess :
val default : debug:bool -> cmdline:CmdLine.t -> t

(** [apply libname w] will prepare Coq for a new file [libname] on workspace [w] *)
val apply : uri:Lang.LUri.File.t -> t -> unit
val apply : intern:Library.Intern.t -> uri:Lang.LUri.File.t -> t -> unit

(** *)
val dirpath_of_uri : uri:Lang.LUri.File.t -> Names.DirPath.t
5 changes: 4 additions & 1 deletion fleche/doc.ml
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,10 @@ end = struct
->
Io.Log.trace "recovery" "bullet";
Coq.State.admit_goal ~token ~st
|> Coq.Protect.E.bind ~f:(fun st -> Coq.Interp.interp ~token ~st v)
|> Coq.Protect.E.bind ~f:(fun st ->
(* We skip the cache here, but likely we don't want to do that. *)
let intern = Vernacinterp.fs_intern in
Coq.Interp.interp ~token ~intern ~st v)
| _ ->
(* Fallback to qed special lex case *)
lex_recovery_heuristic ~token last_tok contents nodes st
Expand Down
8 changes: 5 additions & 3 deletions fleche/memo.ml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module CS = Stats

let intern = Vernacinterp.fs_intern

module Stats = struct
type t =
{ stats : Stats.t
Expand Down Expand Up @@ -287,7 +289,7 @@ module VernacEval = struct

type output = Coq.State.t

let eval ~token (st, stm) = Coq.Interp.interp ~token ~st stm
let eval ~token (st, stm) = Coq.Interp.interp ~token ~intern ~st stm
end

module Interp = CEval (VernacEval)
Expand Down Expand Up @@ -316,7 +318,7 @@ module RequireEval = struct
type output = Coq.State.t

let eval ~token (st, files, stm) =
Coq.Interp.Require.interp ~token ~st files stm
Coq.Interp.Require.interp ~token ~intern ~st files stm
end

module Require = CEval (RequireEval)
Expand Down Expand Up @@ -347,7 +349,7 @@ module InitEval = struct
type output = Coq.State.t

let eval ~token (root_state, workspace, uri) =
Coq.Init.doc_init ~token ~root_state ~workspace ~uri
Coq.Init.doc_init ~token ~intern ~root_state ~workspace ~uri

let input_info (st, ws, file) =
Format.asprintf "st %d | ws %d | file %s" (Hashtbl.hash st)
Expand Down

0 comments on commit 2e5ed56

Please sign in to comment.