| 1 | (************************************************************************) |
| 2 | (* * The Rocq Prover / The Rocq Development Team *) |
| 3 | (* v * Copyright INRIA, CNRS and contributors *) |
| 4 | (* <O___,, * (see version control and CREDITS file for authors & dates) *) |
| 5 | (* \VV/ **************************************************************) |
| 6 | (* // * This file is distributed under the terms of the *) |
| 7 | (* * GNU Lesser General Public License Version 2.1 *) |
| 8 | (* * (see LICENSE file for the text of the license) *) |
| 9 | (************************************************************************) |
| 10 | |
| 11 | (** Generic arguments used by the extension mechanisms of several Rocq ASTs. *) |
| 12 | |
| 13 | (** Generic arguments must be registered according to their usage: |
| 14 | |
| 15 | (raw level printers are always useful for clearer [-time] output, for beautify, |
| 16 | and some other debug prints) |
| 17 | |
| 18 | - extensible constr syntax beyond notations (eg [ltac:()], [ltac2:()] and ltac2 [$x]). |
| 19 | Such genargs appear in glob_term GGenarg and constrexpr CGenarg. |
| 20 | They must be registered with [Genintern.register_intern0] |
| 21 | and [GlobEnv.register_constr_interp0]. |
| 22 | |
| 23 | The glob level may be kept through notations and other operations like Ltac definitions |
| 24 | (eg [Ltac foo := exact ltac2:(foo)]) in which case [Gensubst.register_subst0] |
| 25 | and a glob level printer are useful. |
| 26 | |
| 27 | Other useful registrations are |
| 28 | - [Genintern.register_intern_pat] and [Patternops.register_interp_pat] |
| 29 | to be used in tactic patterns. |
| 30 | - [Genintern.register_ntn_subst0] to be used in notations |
| 31 | (eg [Notation "foo" := ltac2:(foo)]). |
| 32 | |
| 33 | NB: only the base [ExtraArg] is allowed here. |
| 34 | |
| 35 | - tactic arguments to commands defined without depending on ltac_plugin |
| 36 | (VernacProof, HintsExtern, Hint Rewrite, etc). |
| 37 | |
| 38 | Must be registered with [Genintern.register_intern0] and |
| 39 | [Genintern.register_interp0]. |
| 40 | |
| 41 | The glob level can be kept (currently with Hint Extern and Hint |
| 42 | Rewrite) so [Gensubst.register_subst0] is also needed. |
| 43 | |
| 44 | Currently AFAICT this is just [Tacarg.wit_ltac]. |
| 45 | |
| 46 | NB: only the base [ExtraArg] is allowed here. |
| 47 | |
| 48 | - vernac arguments, used by vernac extend. Usually declared in mlg |
| 49 | using VERNAC ARGUMENT EXTEND then used in VERNAC EXTEND. |
| 50 | |
| 51 | With VERNAC ARGUMENT EXTEND the raw level printer is registered |
| 52 | by including PRINTED BY. |
| 53 | |
| 54 | Must be registered with [Procq.register_grammar] (handled by |
| 55 | VERNAC ARGUMENT EXTEND when declared that way) as vernac extend |
| 56 | only gets the genarg as argument so must get the grammar from |
| 57 | the registration. |
| 58 | |
| 59 | Unless combined with some other use, the glob and top levels will be empty |
| 60 | (as in [vernac_genarg_type]). |
| 61 | |
| 62 | - Ltac tactic_extend arguments. Usually declared in mlg using ARGUMENT EXTEND |
| 63 | then used in TACTIC EXTEND. |
| 64 | |
| 65 | Must be registered with [Genintern.register_intern0], |
| 66 | [Gensubst.register_subst0] and [Genintern.register_interp0]. |
| 67 | |
| 68 | Must be registered with [Procq.register_grammar] as tactic extend |
| 69 | only gets the genarg as argument so must get the grammar from |
| 70 | the registration. |
| 71 | |
| 72 | They must be associated with a [Geninterp.Val.tag] using [Geninterp.register_val0] |
| 73 | (which creates a fresh tag if passed [None]). |
| 74 | Note: although [Genintern.register_interp0] registers a producer |
| 75 | of arbitrary [Geninterp.Val.t], tactic_extend requires them to be of the tag |
| 76 | registered by [Geninterp.register_val0] to work properly. |
| 77 | |
| 78 | They should also have all printer levels registered with [Genprint.register_print0]. |
| 79 | |
| 80 | All registrations are handled by the arguments to ARGUMENT EXTEND when declared that way. |
| 81 | |
| 82 | All of them can also be used as vernac_extend arguments since |
| 83 | vernac_extend uses a subset of the registrations needed for tactic_extend. |
| 84 | |
| 85 | - some hack in Tacentries.ml_val_tactic_extend and its variant in |
| 86 | Tac2core_ltac1 for Ltac1.lambda. |
| 87 | *) |
| 88 | |
| 89 | (** The route of a generic argument, from parsing to evaluation. |
| 90 | In the following diagram, "object" can be ltac_expr, constr, tactic_value, etc. |
| 91 | |
| 92 | {% \begin{verbatim} %} |
| 93 | parsing in_raw out_raw |
| 94 | char stream ---> raw_object ---> raw_object generic_argument -------+ |
| 95 | encapsulation decaps| |
| 96 | | |
| 97 | V |
| 98 | raw_object |
| 99 | | |
| 100 | globalization | |
| 101 | V |
| 102 | glob_object |
| 103 | | |
| 104 | encaps | |
| 105 | in_glob | |
| 106 | V |
| 107 | glob_object generic_argument |
| 108 | | |
| 109 | out in out_glob | |
| 110 | object <--- object generic_argument <--- object <--- glob_object <---+ |
| 111 | | decaps encaps interp decaps |
| 112 | | |
| 113 | V |
| 114 | effective use |
| 115 | {% \end{verbatim} %} |
| 116 | |
| 117 | To distinguish between the uninterpreted, globalized and |
| 118 | interpreted worlds, we annotate the type [generic_argument] by a |
| 119 | phantom argument. |
| 120 | |
| 121 | *) |
| 122 | |
| 123 | (** {5 Generic types} *) |
| 124 | |
| 125 | module ArgT : |
| 126 | sig |
| 127 | type ('a, 'b, 'c) tag |
| 128 | val eq : ('a1, 'b1, 'c1) tag -> ('a2, 'b2, 'c2) tag -> ('a1 * 'b1 * 'c1, 'a2 * 'b2 * 'c2) CSig.eq option |
| 129 | val repr : ('a, 'b, 'c) tag -> string |
| 130 | type any = Any : ('a, 'b, 'c) tag -> any |
| 131 | val name : string -> any option |
| 132 | val dump : unit -> (int * string) list |
| 133 | end |
| 134 | |
| 135 | (** Generic types. The first parameter is the OCaml lowest level, the second one |
| 136 | is the globalized level, and third one the internalized level. *) |
| 137 | type (_, _, _) genarg_type = |
| 138 | | ExtraArg : ('a, 'b, 'c) ArgT.tag -> ('a, 'b, 'c) genarg_type |
| 139 | | ListArg : ('a, 'b, 'c) genarg_type -> ('a list, 'b list, 'c list) genarg_type |
| 140 | | OptArg : ('a, 'b, 'c) genarg_type -> ('a option, 'b option, 'c option) genarg_type |
| 141 | | PairArg : ('a1, 'b1, 'c1) genarg_type * ('a2, 'b2, 'c2) genarg_type -> |
| 142 | ('a1 * 'a2, 'b1 * 'b2, 'c1 * 'c2) genarg_type |
| 143 | |
| 144 | type 'a uniform_genarg_type = ('a, 'a, 'a) genarg_type |
| 145 | (** Alias for concision when the three types agree. *) |
| 146 | |
| 147 | type 'a vernac_genarg_type = ('a, Util.Empty.t, Util.Empty.t) genarg_type |
| 148 | (** Produced by VERNAC ARGUMENT EXTEND *) |
| 149 | |
| 150 | val make0 : string -> ('raw, 'glob, 'top) genarg_type |
| 151 | (** Create a new generic type of argument: force to associate |
| 152 | unique ML types at each of the three levels. *) |
| 153 | |
| 154 | val create_arg : string -> ('raw, 'glob, 'top) genarg_type |
| 155 | (** Alias for [make0]. *) |
| 156 | |
| 157 | (** {5 Specialized types} *) |
| 158 | |
| 159 | (** All of [rlevel], [glevel] and [tlevel] must be non convertible |
| 160 | to ensure the injectivity of the GADT type inference. *) |
| 161 | |
| 162 | type rlevel = [ `rlevel ] |
| 163 | type glevel = [ `glevel ] |
| 164 | type tlevel = [ `tlevel ] |
| 165 | |
| 166 | (** Generic types at a fixed level. The first parameter embeds the OCaml type |
| 167 | and the second one the level. *) |
| 168 | type (_, _) abstract_argument_type = |
| 169 | | Rawwit : ('a, 'b, 'c) genarg_type -> ('a, rlevel) abstract_argument_type |
| 170 | | Glbwit : ('a, 'b, 'c) genarg_type -> ('b, glevel) abstract_argument_type |
| 171 | | Topwit : ('a, 'b, 'c) genarg_type -> ('c, tlevel) abstract_argument_type |
| 172 | |
| 173 | type 'a raw_abstract_argument_type = ('a, rlevel) abstract_argument_type |
| 174 | (** Specialized type at raw level. *) |
| 175 | |
| 176 | type 'a glob_abstract_argument_type = ('a, glevel) abstract_argument_type |
| 177 | (** Specialized type at globalized level. *) |
| 178 | |
| 179 | type 'a typed_abstract_argument_type = ('a, tlevel) abstract_argument_type |
| 180 | (** Specialized type at internalized level. *) |
| 181 | |
| 182 | (** {6 Projections} *) |
| 183 | |
| 184 | val rawwit : ('a, 'b, 'c) genarg_type -> ('a, rlevel) abstract_argument_type |
| 185 | (** Projection on the raw type constructor. *) |
| 186 | |
| 187 | val glbwit : ('a, 'b, 'c) genarg_type -> ('b, glevel) abstract_argument_type |
| 188 | (** Projection on the globalized type constructor. *) |
| 189 | |
| 190 | val topwit : ('a, 'b, 'c) genarg_type -> ('c, tlevel) abstract_argument_type |
| 191 | (** Projection on the internalized type constructor. *) |
| 192 | |
| 193 | (** {5 Generic arguments} *) |
| 194 | |
| 195 | type 'l generic_argument = GenArg : ('a, 'l) abstract_argument_type * 'a -> 'l generic_argument |
| 196 | (** A inhabitant of ['level generic_argument] is a inhabitant of some type at |
| 197 | level ['level], together with the representation of this type. *) |
| 198 | |
| 199 | type raw_generic_argument = rlevel generic_argument |
| 200 | type glob_generic_argument = glevel generic_argument |
| 201 | type typed_generic_argument = tlevel generic_argument |
| 202 | |
| 203 | (** {6 Constructors} *) |
| 204 | |
| 205 | val in_gen : ('a, 'co) abstract_argument_type -> 'a -> 'co generic_argument |
| 206 | (** [in_gen t x] embeds an argument of type [t] into a generic argument. *) |
| 207 | |
| 208 | val out_gen : ('a, 'co) abstract_argument_type -> 'co generic_argument -> 'a |
| 209 | (** [out_gen t x] recovers an argument of type [t] from a generic argument. It |
| 210 | fails if [x] has not the right dynamic type. *) |
| 211 | |
| 212 | val has_type : 'co generic_argument -> ('a, 'co) abstract_argument_type -> bool |
| 213 | (** [has_type v t] tells whether [v] has type [t]. If true, it ensures that |
| 214 | [out_gen t v] will not raise a dynamic type exception. *) |
| 215 | |
| 216 | (** {6 Type reification} *) |
| 217 | |
| 218 | type argument_type = ArgumentType : ('a, 'b, 'c) genarg_type -> argument_type |
| 219 | |
| 220 | (** {6 Equalities} *) |
| 221 | |
| 222 | val argument_type_eq : argument_type -> argument_type -> bool |
| 223 | val genarg_type_eq : |
| 224 | ('a1, 'b1, 'c1) genarg_type -> |
| 225 | ('a2, 'b2, 'c2) genarg_type -> |
| 226 | ('a1 * 'b1 * 'c1, 'a2 * 'b2 * 'c2) CSig.eq option |
| 227 | val abstract_argument_type_eq : |
| 228 | ('a, 'l) abstract_argument_type -> ('b, 'l) abstract_argument_type -> |
| 229 | ('a, 'b) CSig.eq option |
| 230 | |
| 231 | val pr_argument_type : argument_type -> Pp.t |
| 232 | (** Print a human-readable representation for a given type. *) |
| 233 | |
| 234 | val genarg_tag : 'a generic_argument -> argument_type |
| 235 | |
| 236 | val unquote : ('a, 'co) abstract_argument_type -> argument_type |
| 237 | |
| 238 | (** {6 Registering genarg-manipulating functions} |
| 239 | |
| 240 | This is boilerplate code used here and there in the code of Rocq. *) |
| 241 | |
| 242 | val get_arg_tag : ('a, 'b, 'c) genarg_type -> ('a, 'b, 'c) ArgT.tag |
| 243 | (** Works only on base objects (ExtraArg), otherwise fails badly. *) |
| 244 | |
| 245 | module type GenObj = |
| 246 | sig |
| 247 | type ('raw, 'glb, 'top) obj |
| 248 | (** An object manipulating generic arguments. *) |
| 249 | |
| 250 | val name : string |
| 251 | (** A name for such kind of manipulation, e.g. [interp]. *) |
| 252 | |
| 253 | val default : ('raw, 'glb, 'top) ArgT.tag -> ('raw, 'glb, 'top) obj option |
| 254 | (** A generic object when there is no registered object for this type. *) |
| 255 | end |
| 256 | |
| 257 | module Register (M : GenObj) : |
| 258 | sig |
| 259 | (** Warning: although the following APIs use [genarg_type] the |
| 260 | values must always be [ExtraArg some_tag]. *) |
| 261 | |
| 262 | val register0 : ('raw, 'glb, 'top) genarg_type -> |
| 263 | ('raw, 'glb, 'top) M.obj -> unit |
| 264 | (** Register a ground type manipulation function. Must be [ExtraArg]. *) |
| 265 | |
| 266 | val obj : ('raw, 'glb, 'top) genarg_type -> ('raw, 'glb, 'top) M.obj |
| 267 | (** Recover a manipulation function at a given type. Must be [ExtraArg]. *) |
| 268 | |
| 269 | val mem : _ genarg_type -> bool |
| 270 | (** Is this type registered? (must be [ExtraArg]) *) |
| 271 | |
| 272 | val fold_keys : (ArgT.any -> 'acc -> 'acc) -> 'acc -> 'acc |
| 273 | (** Fold over the registered keys. *) |
| 274 | |
| 275 | end |
| 276 | |
| 277 | (** {5 Compatibility layer} |
| 278 | |
| 279 | The functions below are aliases for generic_type constructors. |
| 280 | |
| 281 | *) |
| 282 | |
| 283 | val wit_list : ('a, 'b, 'c) genarg_type -> ('a list, 'b list, 'c list) genarg_type |
| 284 | val wit_opt : ('a, 'b, 'c) genarg_type -> ('a option, 'b option, 'c option) genarg_type |
| 285 | val wit_pair : ('a1, 'b1, 'c1) genarg_type -> ('a2, 'b2, 'c2) genarg_type -> |
| 286 | ('a1 * 'a2, 'b1 * 'b2, 'c1 * 'c2) genarg_type |