-
Notifications
You must be signed in to change notification settings - Fork 40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[sertop] Add sername program for serialization of terms #207
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
98e98c2
[sertop] Add sername program
palmskog fa70044
[sername] Add basic printing of terms
palmskog db82bdc
[sername] Use detyping to get term without de Bruijn indices
palmskog 1873f9a
[sername] Add option `--require-lib` to sername for loading a custom …
palmskog 282fd3f
[sername] Add option for disabling printing of prettified constrs and…
palmskog 95c10e5
[sername] Workaround for issue with error on load for some files
palmskog 8546a0a
[doc] Improve sername documentation
palmskog e86b502
[serlib] Expose some key functions needed by sername to obtain serial…
palmskog f266c4c
[sername] Add `--body` option to allow serializing constant bodies (n…
palmskog 441f5cd
[doc] Add changes for 0.7.1
palmskog 40760cd
[build] Correct dune version.
ejgallego 000dac6
[travis] [build] Disable warnings for sexplib >= 0.13, fix Coq version
ejgallego f3323d5
[changes] Note about sername future.
ejgallego 13064aa
[sername] Add small test.
ejgallego File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
(lang dune 1.2) | ||
(lang dune 1.4) | ||
(using fmt 1.0) | ||
(name coq-serapi) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,211 @@ | ||
(************************************************************************) | ||
(* * The Coq Proof Assistant / The Coq Development Team *) | ||
(* v * INRIA, CNRS and contributors - Copyright 1999-2018 *) | ||
(* <O___,, * (see CREDITS file for the list of authors) *) | ||
(* \VV/ **************************************************************) | ||
(* // * This file is distributed under the terms of the *) | ||
(* * GNU Lesser General Public License Version 2.1 *) | ||
(* * (see LICENSE file for the text of the license) *) | ||
(************************************************************************) | ||
|
||
(************************************************************************) | ||
(* Coq serialization API/Plugin *) | ||
(* Written by: Karl Palmskog *) | ||
(************************************************************************) | ||
(* Status: Very Experimental *) | ||
(************************************************************************) | ||
|
||
let fatal_exn exn info = | ||
let loc = Loc.get_loc info in | ||
let msg = Pp.(pr_opt_no_spc Topfmt.pr_loc loc ++ fnl () | ||
++ CErrors.iprint (exn, info)) in | ||
Format.eprintf "Error: @[%a@]@\n%!" Pp.pp_with msg; | ||
exit 1 | ||
|
||
let create_document ~require_lib ~in_file ~async ~async_workers ~quick ~iload_path ~debug = | ||
|
||
let open Sertop_init in | ||
|
||
(* coq initialization *) | ||
coq_init | ||
{ fb_handler = (fun _ -> ()) (* XXXX *) | ||
; ml_load = None | ||
; debug | ||
}; | ||
|
||
(* document initialization *) | ||
|
||
let stm_options = process_stm_flags | ||
{ enable_async = async | ||
; deep_edits = false | ||
; async_workers = async_workers | ||
} in | ||
|
||
(* Disable due to https://github.com/ejgallego/coq-serapi/pull/94 *) | ||
let stm_options = | ||
{ stm_options with | ||
async_proofs_tac_error_resilience = `None | ||
; async_proofs_cmd_error_resilience = false | ||
} in | ||
|
||
let stm_options = | ||
if quick | ||
then { stm_options with async_proofs_mode = APonLazy } | ||
else stm_options | ||
in | ||
|
||
(* | ||
let require_libs = | ||
let prelude = ["Coq.Init.Prelude", None, Some false] in | ||
match require_lib with | ||
| Some l -> prelude @ [l, None, Some false] | ||
| None -> prelude | ||
in | ||
*) | ||
let require_libs = ["Coq.Init.Prelude", None, Some false] in | ||
|
||
let ndoc = { Stm.doc_type = Stm.VoDoc in_file | ||
; require_libs | ||
; iload_path | ||
; stm_options | ||
} in | ||
|
||
(* Workaround, see | ||
https://github.com/ejgallego/coq-serapi/pull/101 *) | ||
if quick || async <> None | ||
then Safe_typing.allow_delayed_constants := true; | ||
|
||
match require_lib with | ||
| None -> Stm.new_doc ndoc | ||
| Some l -> | ||
(* | ||
let sdoc = Stm.new_doc ndoc in | ||
let dir,from,exp = l,None,Some false in | ||
let mp = Libnames.qualid_of_string dir in | ||
let mfrom = Option.map Libnames.qualid_of_string from in | ||
Flags.silently (Vernacentries.vernac_require mfrom exp) [mp]; | ||
sdoc | ||
*) | ||
let doc,sid = Stm.new_doc ndoc in | ||
let sent = Printf.sprintf "Require %s." l in | ||
let in_strm = Stream.of_string sent in | ||
let in_pa = Pcoq.Parsable.make ~loc:(Loc.initial (InFile in_file)) in_strm in | ||
match Stm.parse_sentence ~doc ~entry:Pvernac.main_entry sid in_pa with | ||
| Some ast -> | ||
let doc, sid, tip = Stm.add ~doc ~ontop:sid false ast in | ||
if tip <> `NewTip then CErrors.user_err ?loc:ast.loc Pp.(str "fatal, got no `NewTip`"); | ||
doc, sid | ||
| None -> assert false | ||
|
||
exception End_of_input | ||
|
||
let input_doc ~in_chan ~process ~doc ~sid = | ||
try while true; do | ||
let line = input_line in_chan in | ||
if String.trim line <> "" then process ~doc ~sid line | ||
done | ||
with End_of_file -> () | ||
|
||
let context_of_st m = match m with | ||
| `Valid (Some { Vernacstate.proof = Some pstate; _ } ) -> | ||
Pfedit.get_current_context pstate | ||
| _ -> | ||
let env = Global.env () in Evd.from_env env, env | ||
|
||
let str_pp_obj env sigma fmt (obj : Serapi_protocol.coq_object) : unit = | ||
Format.fprintf fmt "%a" Pp.pp_with (Serapi_protocol.gen_pp_obj env sigma obj) | ||
|
||
let process_line ~pp ~str_pp ~de_bruijn ~body ~doc ~sid line = | ||
let open Serapi_protocol in | ||
let st = Stm.state_of_id ~doc sid in | ||
let sigma, env = context_of_st st in | ||
let info = QueryUtil.info_of_id env line in | ||
let def = if body then fst info else snd info in | ||
match def with | ||
| [CoqConstr def_term] -> | ||
let evd = Evd.from_env env in | ||
let edef_term = EConstr.of_constr def_term in | ||
let gdef_term = Detyping.detype Detyping.Now false Names.Id.Set.empty env evd edef_term in | ||
palmskog marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Format.pp_set_margin Format.std_formatter 100000; | ||
Format.printf "%s: %!" line; | ||
if str_pp then Format.fprintf Format.std_formatter "\"@[%a@]\" %!" (str_pp_obj env sigma) (CoqConstr def_term); | ||
if de_bruijn then Format.printf "@[%a@] %!" pp (Serlib.Ser_constr.sexp_of_constr def_term); | ||
Format.printf "@[%a@]@\n%!" pp (Serlib.Ser_glob_term.sexp_of_glob_constr gdef_term) | ||
| _ -> () | ||
|
||
let check_pending_proofs ~pstate = | ||
Option.iter (fun pstate -> | ||
let pfs = Proof_global.get_all_proof_names pstate in | ||
if not CList.(is_empty pfs) then | ||
let msg = let open Pp in | ||
seq [ str "There are pending proofs: " | ||
; pfs |> List.rev |> prlist_with_sep pr_comma Names.Id.print; str "."] in | ||
CErrors.user_err msg | ||
) pstate | ||
|
||
let close_document ~doc ~pstate = | ||
let _doc = Stm.join ~doc in | ||
check_pending_proofs ~pstate | ||
|
||
let sername_version = Ser_version.ser_git_version | ||
|
||
let sername_man = | ||
[ | ||
`S "DESCRIPTION"; | ||
`P "Experimental Coq name serializer."; | ||
`S "USAGE"; | ||
`P "To serialize names listed in `names.txt` in module `Funs.mod`:"; | ||
`Pre "sername -Q fs,Funs --require-lib=Funs.mod names.txt"; | ||
`P "See the documentation on the project's webpage for more information." | ||
] | ||
|
||
let sername_doc = "sername Coq tool" | ||
|
||
open Cmdliner | ||
|
||
let driver debug printer async async_workers quick coq_path ml_path load_path rload_path require_lib str_pp de_bruijn body in_file omit_loc omit_att exn_on_opaque = | ||
|
||
(* closures *) | ||
let pp = Sertop_ser.select_printer printer in | ||
let process = process_line ~pp ~str_pp ~de_bruijn ~body in | ||
|
||
(* initialization *) | ||
let options = Serlib.Serlib_init.{ omit_loc; omit_att; exn_on_opaque } in | ||
Serlib.Serlib_init.init ~options; | ||
|
||
let iload_path = Serapi_paths.coq_loadpath_default ~implicit:true ~coq_path @ ml_path @ load_path @ rload_path in | ||
let doc, sid = create_document ~require_lib ~in_file:"file.v" ~async ~async_workers ~quick ~iload_path ~debug in | ||
|
||
(* main loop *) | ||
let in_chan = open_in in_file in | ||
let () = input_doc ~in_chan ~process ~doc ~sid in (* XX *) | ||
let pstate = match Stm.state_of_id ~doc sid with | ||
| `Valid (Some { Vernacstate.proof; _ }) -> proof | ||
| _ -> None | ||
in | ||
let () = close_document ~doc ~pstate in | ||
() | ||
|
||
let main () = | ||
let input_file = | ||
let doc = "Input file." in | ||
Arg.(required & pos 0 (some string) None & info [] ~docv:("FILE") ~doc) | ||
in | ||
|
||
let sername_cmd = | ||
let open Sertop_arg in | ||
Term.(const driver | ||
$ debug $ printer $ async $ async_workers $ quick $ prelude | ||
$ ml_include_path $ load_path $ rload_path $ require_lib $ str_pp $ de_bruijn $ body $ input_file $ omit_loc $ omit_att $ exn_on_opaque | ||
), | ||
Term.info "sername" ~version:sername_version ~doc:sername_doc ~man:sername_man | ||
in | ||
|
||
try match Term.eval ~catch:false sername_cmd with | ||
| `Error _ -> exit 1 | ||
| _ -> exit 0 | ||
with exn -> | ||
let (e, info) = CErrors.push exn in | ||
fatal_exn e info | ||
|
||
let _ = main () |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are you not adding this to
require_libs
above?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You mention a problem in the commit, but with no further details given it is hard to say more.