123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159(******************************************************************************)(* This file is part of Waterproof-lib. *)(* *)(* Waterproof-lib is free software: you can redistribute it and/or modify *)(* it under the terms of the GNU General Public License as published by *)(* the Free Software Foundation, either version 3 of the License, or *)(* (at your option) any later version. *)(* *)(* Waterproof-lib is distributed in the hope that it will be useful, *)(* but WITHOUT ANY WARRANTY; without even the implied warranty of *)(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *)(* GNU General Public License for more details. *)(* *)(* You should have received a copy of the GNU General Public License *)(* along with Waterproof-lib. If not, see <https://www.gnu.org/licenses/>. *)(* *)(******************************************************************************)(**
In this module we keep two tables:
- [wp_unfold_map] a map from strings to global references, that is used to keep track of the
introduced notations, and the global reference they are associated with
- [wp_unfold_tbl] a table from global references to unfold actions, that can later be used
by the unfold framework. The unfold actions can be of three types:
- unfold the definition associated to the global reference
- apply a bi-implication
- rewrite an equality
*)openLtac2_plugin.Tac2entriesopenLtac2_plugin.Tac2expropenNamesmoduleStringMap=Map.Make(String)(** A type to represent the different unfold actions, a name for the action, and the data they need. *)typeunfold_action=|Unfoldofstring*GlobRef.t|Applyofstring*EConstr.constr|Rewriteofstring*EConstr.constr(** The map that associate notation strings to references *)letwp_unfold_map=Summary.ref~name:"wp_unfold_map"StringMap.empty(** The table that associates global references to unfold actions *)letwp_unfold_tbl:(GlobRef.t,unfold_action)Hashtbl.tref=Summary.ref~name:"wp_unfold_tbl"(Hashtbl.create60)(** The following constructions are necessary to ensure persistence of the tables.. *)letcache_unfold_mapmp=wp_unfold_map:=mpletdeclare_unfold_map=letopenLibobjectindeclare_object{(default_object"WP_UNFOLD_MAP")withcache_function=cache_unfold_map;load_function=(fun_->cache_unfold_map);classify_function=(fun_->Keep);}letcache_unfold_tbltbl=wp_unfold_tbl:=tblletdeclare_unfold_tbl=letopenLibobjectindeclare_object{(default_object"WP_UNFOLD_TBL")withcache_function=cache_unfold_tbl;load_function=(fun_->cache_unfold_tbl);classify_function=(fun_->Keep);}letadd_to_unfold_map(toks:stringlist)(id:GlobRef.t):unit=lets=String.concat" "toksinLib.add_leaf(declare_unfold_map(StringMap.addsid!wp_unfold_map))letadd_to_unfold_tbl(id:GlobRef.t)(ua:unfold_action):unit=letnew_table=Hashtbl.copy!wp_unfold_tblinHashtbl.addnew_tableidua;(* Adding a copy here, because otherwise we seem to get strange behavior when
one adds definitions later in a file: earlier in a file it will already use
to try the new definition and failing because it is not in the context yet.
TODO: check with an expert what is best practice here. *)Lib.add_leaf(declare_unfold_tblnew_table)letextract_def(s:string):GlobRef.toption=StringMap.find_opts!wp_unfold_map(**
Registers a new unfold notation in the notation table.
Arguments:
- [toks]: the list of string tokens that should follow "Expand" in the notation
- [id]: the qualid to associate the unfold action to
Returns:
- (the notation interpretation data created, a deprecated version of the notation data)
*)letregister_unfold(toks:stringlist)(id:Libnames.qualid):notation_interpretation_data*notation_interpretation_data=letglob_ref=Nametab.locateidinletfull_id=Libnames.qualid_of_path(Nametab.path_of_globalglob_ref)inletsexpr_seq=List.map(funs->SexprStr(CAst.makes))("Expand"::toks)inletget_qualid()=Libnames.qualid_of_string"Unfold.wp_expand"inletget_ref()=CAst.make@@CTacExt(Ltac2_plugin.Tac2quote.wit_reference,CAst.make(Ltac2_plugin.Tac2qexpr.QReferencefull_id))inletrhs=CTacApp(CAst.make(CTacRef(RelId(get_qualid()))),[get_ref()])inletsexpr_seq_old=List.map(funs->SexprStr(CAst.makes))(["Expand";"the";"definition";"of"]@toks)inletget_qualid_old()=Libnames.qualid_of_string"Unfold.wp_expand_deprecated"inletrhs_old=CTacApp(CAst.make(CTacRef(RelId(get_qualid_old()))),[get_ref()])in(register_notation[]sexpr_seqNone(CAst.makerhs),register_notation[]sexpr_seq_oldNone(CAst.makerhs_old))(** A type that represents the datastructure that can be added
to the unfold table. When it is added, it will be converted
to an unfold action.
*)typeunfold_entry=|Unfold_entryofstring|Apply_entryofstring*Constrexpr.constr_expr|Rewrite_entryofstring*Constrexpr.constr_expr(** Registers a new entry in the table that stores a list of
unfold actions associated to a reference.
In this step, the [Constrexpr.constr_expr] is interpreted
into an [EConstr.constr] to be stored in the unfold table.
Arguments:
- [id]: the global reference to associate the unfold action to
- [ue]: the unfold action to register
*)letregister_unfold_entry(id:GlobRef.t)(ue:unfold_entry):unit=letf(e:Constrexpr.constr_expr)=letenv=Global.env()inletsigma=Evd.from_envenvinlet(constr_e,_)=Constrintern.interp_constrenvsigmaeinconstr_einmatchuewith|Unfold_entrys->add_to_unfold_tblid(Unfold(s,id))|Apply_entry(s,e)->add_to_unfold_tblid(Apply(s,fe))|Rewrite_entry(s,e)->add_to_unfold_tblid(Rewrite(s,fe))letget_all_references():GlobRef.tlist=letlst=!wp_unfold_tbl|>Hashtbl.to_seq_keys|>List.of_seqin(* Remove duplicates *)lettbl=Hashtbl.create(List.lengthlst)inList.iter(funx->Hashtbl.replacetblx())lst;Hashtbl.fold(funkey_acc->key::acc)tbl[]letfind_unfold_actions_by_ref(r:GlobRef.t):unfold_actionlist=Hashtbl.find_all!wp_unfold_tblrletfind_unfold_actions_by_str(s:string):unfold_actionlist=matchextract_defswith|Somer->find_unfold_actions_by_refr|None->[]