| 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 | open Names |
| 12 | open Mod_subst |
| 13 | |
| 14 | (** [Libobject] declares persistent objects, given with methods: |
| 15 | |
| 16 | * a caching function specifying how to add the object in the current |
| 17 | scope; called when the object is added and after the end of the containing |
| 18 | sections. |
| 19 | If the object wishes to register its visibility in the Nametab, |
| 20 | it should do so for all possible suffixes. |
| 21 | |
| 22 | * a loading function, specifying what to do when the module |
| 23 | containing the object is loaded; called at Require |
| 24 | and after the end of the containing modules. |
| 25 | If the object wishes to register its visibility in the Nametab, |
| 26 | it should do so for all suffixes no shorter than the "int" argument |
| 27 | |
| 28 | * an opening function, specifying what to do when the module |
| 29 | containing the object is opened; called when the containing modules |
| 30 | are Imported. |
| 31 | Objects which should only have an effect when the nearest containing module |
| 32 | is imported (and not when the modules containing the nearest module are imported) |
| 33 | must check that the "int" argument is [1]. |
| 34 | If the object wishes to register its visibility in the Nametab, |
| 35 | it should do so for the suffix of the length the "int" argument |
| 36 | |
| 37 | * a classification function, specifying what to do with the object, |
| 38 | when the current module (containing the object) is ended; |
| 39 | The possibilities are: |
| 40 | Dispose - the object is dropped at the end of the module. |
| 41 | Substitute - the object is kept at the end of the module. |
| 42 | When the module is cloned (Include, module aliases) |
| 43 | or when it's a module type which is getting instantiated |
| 44 | (eg if module type [T] is used for a functor argument [X : T] |
| 45 | or [Declare Module X : T]), |
| 46 | the substitution function is called on the object to update the module name. |
| 47 | Keep - the object is kept at the end of the module. |
| 48 | When the module is cloned the object is not cloned with it. |
| 49 | This means that Keep objects in a module type or functor are dropped. |
| 50 | Escape - like Keep, but also escapes module types, functors and opaque modules. |
| 51 | Monomorphic universes and universe constraints behave like this. |
| 52 | Anticipate - this is for objects that have to be explicitly managed |
| 53 | by the [end_module] function (currently only Require). |
| 54 | |
| 55 | * a substitution function, performing the substitution; |
| 56 | this function should be declared for substitutive objects |
| 57 | only (see above). NB: the substitution might be delayed |
| 58 | instead of happening at module creation, so this function |
| 59 | should _not_ depend on the current environment |
| 60 | |
| 61 | * a discharge function, that is called at section closing time to |
| 62 | collect the data necessary to rebuild the discharged form of the |
| 63 | non volatile objects. If it returns [None] the object is dropped. |
| 64 | It is called in the state inside the section at its end, before it is reset. |
| 65 | Notably the global environment contains the section data and the non-discharged globals. |
| 66 | |
| 67 | * a rebuild function, that is applied after section closing to |
| 68 | rebuild the non volatile content of a section from the data |
| 69 | collected by the discharge function |
| 70 | It is called in the state after the end of the section with any previous objects already present. |
| 71 | Notably the global environment contains the discharged globals. |
| 72 | |
| 73 | Any type defined as a persistent object must be pure (e.g. no references) and |
| 74 | marshallable by the OCaml Marshal module (e.g. no closures). |
| 75 | |
| 76 | *) |
| 77 | |
| 78 | type substitutivity = Dispose | Substitute | Keep | Escape | Anticipate |
| 79 | |
| 80 | (** Both names are passed to objects: a "semantic" [kernel_name], which |
| 81 | can be substituted and a "syntactic" [full_path] which can be printed |
| 82 | *) |
| 83 | |
| 84 | type object_name = Libnames.full_path * KerName.t |
| 85 | |
| 86 | type open_filter |
| 87 | |
| 88 | type ('a,'b,'discharged) object_declaration = { |
| 89 | object_name : string; |
| 90 | object_stage : Summary.Stage.t; |
| 91 | cache_function : 'b -> unit; |
| 92 | load_function : int -> 'b -> unit; |
| 93 | open_function : open_filter -> int -> 'b -> unit; |
| 94 | classify_function : 'a -> substitutivity; |
| 95 | subst_function : substitution * 'a -> 'a; |
| 96 | discharge_function : 'a -> 'discharged option; |
| 97 | rebuild_function : 'discharged -> 'a; |
| 98 | } |
| 99 | |
| 100 | val unfiltered : open_filter |
| 101 | |
| 102 | val make_filter : finite:bool -> string CAst.t list -> open_filter |
| 103 | (** Anomaly when the list is empty. *) |
| 104 | |
| 105 | type category |
| 106 | |
| 107 | val create_category : string -> category |
| 108 | (** Anomaly if called more than once for a given string. *) |
| 109 | |
| 110 | val in_filter : cat:category option -> open_filter -> bool |
| 111 | (** On [cat:None], returns whether the filter allows opening |
| 112 | uncategorized objects. |
| 113 | |
| 114 | On [cat:(Some category)], returns whether the filter allows |
| 115 | opening objects in the given [category]. *) |
| 116 | |
| 117 | val filtered_open : ?cat:category -> ('i -> 'a -> unit) -> |
| 118 | open_filter -> 'i -> 'a -> unit |
| 119 | (** Combinator for making objects with simple category-based open |
| 120 | behaviour. When [cat:None], can be opened by Unfiltered, but also |
| 121 | by Filtered with a negative set. *) |
| 122 | |
| 123 | val simple_open : ?cat:category -> ('a -> unit) -> |
| 124 | open_filter -> int -> 'a -> unit |
| 125 | (** Like [filtered_open], and also requires the int to be 1 to |
| 126 | actually open. *) |
| 127 | |
| 128 | val filter_eq : open_filter -> open_filter -> bool |
| 129 | |
| 130 | val filter_and : open_filter -> open_filter -> open_filter option |
| 131 | (** Returns [None] when the intersection is empty. *) |
| 132 | |
| 133 | val filter_or : open_filter -> open_filter -> open_filter |
| 134 | |
| 135 | (** The default object has empty methods. |
| 136 | Object creators are advised to use the construction |
| 137 | [{(default_object "MY_OBJECT") with |
| 138 | cache_function = ... |
| 139 | }] |
| 140 | and specify only these functions which are not empty/meaningless |
| 141 | |
| 142 | The classify_function must be specified. |
| 143 | *) |
| 144 | |
| 145 | val default_object : ?stage:Summary.Stage.t -> string -> ('a,'b,'a) object_declaration |
| 146 | |
| 147 | (** the identity substitution function *) |
| 148 | val ident_subst_function : substitution * 'a -> 'a |
| 149 | |
| 150 | (** {6 ... } *) |
| 151 | (** Given an object declaration, the function [declare_object_full] |
| 152 | will hand back two functions, the "injection" and "projection" |
| 153 | functions for dynamically typed library-objects. *) |
| 154 | |
| 155 | module Dyn : Dyn.S |
| 156 | |
| 157 | type obj = Dyn.t |
| 158 | |
| 159 | module ExportObj : sig |
| 160 | type t = { mpl : (open_filter * Names.ModPath.t) list } [@@unboxed] |
| 161 | end |
| 162 | |
| 163 | type ('subs, 'alg, 'keep, 'escape) object_view = |
| 164 | | ModuleObject of Id.t * 'subs |
| 165 | | ModuleTypeObject of Id.t * 'subs |
| 166 | | IncludeObject of 'alg |
| 167 | | KeepObject of Id.t * 'keep |
| 168 | | EscapeObject of Id.t * 'escape |
| 169 | | ExportObject of ExportObj.t |
| 170 | | AtomicObject of obj |
| 171 | |
| 172 | (* there are some extra invariants we could try to enforce by typing: |
| 173 | - substitutive_objects do not contain KeepObject and EscapeObject |
| 174 | - KeepObject only contains itself and atoms |
| 175 | - EscapeObject only contains itself and atoms *) |
| 176 | type t = (substitutive_objects, algebraic_objects, keep_objects, escape_objects) object_view |
| 177 | |
| 178 | and algebraic_objects = |
| 179 | | Objs of t list |
| 180 | | Ref of Names.ModPath.t * Mod_subst.substitution |
| 181 | |
| 182 | and substitutive_objects = Names.MBId.t list * algebraic_objects |
| 183 | |
| 184 | and keep_objects = { keep_objects : t list } |
| 185 | |
| 186 | and escape_objects = { escape_objects : t list } |
| 187 | |
| 188 | (** Object declaration and names: if you need the current prefix |
| 189 | (typically to interact with the nametab), you need to have it |
| 190 | passed to you. |
| 191 | |
| 192 | - [declare_named_object_gen] passes the raw prefix which you can |
| 193 | manipulate as you wish. |
| 194 | |
| 195 | - [declare_named_object_full] and [declare_named_object] provide |
| 196 | the convenience of packaging it with the provided [Id.t] into a |
| 197 | [object_name]. |
| 198 | |
| 199 | - [declare_object] and [declare_object_full] ignore the prefix for you. *) |
| 200 | |
| 201 | val declare_object : |
| 202 | ('a, 'a, _) object_declaration -> ('a -> obj) |
| 203 | |
| 204 | val declare_object_full : |
| 205 | ('a, 'a, _) object_declaration -> 'a Dyn.tag |
| 206 | |
| 207 | val declare_named_object_full : |
| 208 | ('a, object_name * 'a, _) object_declaration -> (Id.t * 'a) Dyn.tag |
| 209 | |
| 210 | val declare_named_object : |
| 211 | ('a, object_name * 'a, _) object_declaration -> (Id.t -> 'a -> obj) |
| 212 | |
| 213 | (** Object prefix morally contains the "prefix" naming of an object to |
| 214 | be stored by [library], where [obj_path] is the "absolute" path and |
| 215 | [obj_mp] is the current "module" prefix. |
| 216 | |
| 217 | Thus, for an object living inside [Module A. Section B.] the |
| 218 | prefix would be: |
| 219 | |
| 220 | [ { obj_path = "A.B"; obj_mp = "A"; } ] |
| 221 | |
| 222 | Note that [obj_path] is a "path" that is to say, |
| 223 | as opposed to [obj_mp] which is a single module name. |
| 224 | |
| 225 | *) |
| 226 | type object_prefix = { |
| 227 | obj_path : Libnames.full_path; |
| 228 | obj_mp : ModPath.t; |
| 229 | } |
| 230 | |
| 231 | val eq_object_prefix : object_prefix -> object_prefix -> bool |
| 232 | |
| 233 | val declare_named_object_gen : |
| 234 | ('a, object_prefix * 'a, _) object_declaration -> ('a -> obj) |
| 235 | |
| 236 | val cache_object : object_prefix * obj -> unit |
| 237 | val load_object : int -> object_prefix * obj -> unit |
| 238 | val open_object : open_filter -> int -> object_prefix * obj -> unit |
| 239 | val subst_object : substitution * obj -> obj |
| 240 | val classify_object : obj -> substitutivity |
| 241 | val object_name : obj -> string |
| 242 | val object_stage : obj -> Summary.Stage.t |
| 243 | |
| 244 | type discharged_obj |
| 245 | |
| 246 | val discharge_object : obj -> discharged_obj option |
| 247 | val rebuild_object : discharged_obj -> obj |
| 248 | |
| 249 | type locality = Local | Export | SuperGlobal |
| 250 | |
| 251 | (** Object with semi-static scoping: the scoping depends on the given |
| 252 | [locality] not the rest of the object. |
| 253 | |
| 254 | It is up to the caller of [add_leaf] to produce sensible errors if |
| 255 | a value which cannot be discharged is given with non Local |
| 256 | locality. |
| 257 | |
| 258 | If [subst] is [None] non [Local] values are [Keep], otherwise [Substitute]. |
| 259 | |
| 260 | [Export] values are only imported with shallow imports (depth = 1). |
| 261 | |
| 262 | [cat] only matters when importing, ie only for [Export] values. |
| 263 | *) |
| 264 | val object_with_locality : ?stage:Summary.Stage.t -> ?cat:category -> string -> |
| 265 | cache:('a -> unit) -> |
| 266 | subst:(Mod_subst.substitution * 'a -> 'a) option -> |
| 267 | discharge:('a -> 'a) -> |
| 268 | (locality * 'a, locality * 'a, locality * 'a) object_declaration |
| 269 | |
| 270 | (** Higher-level API for objects with fixed scope. |
| 271 | |
| 272 | - Local means that the object cannot be imported from outside. |
| 273 | - Global means that it can be imported (by importing the module that contains the |
| 274 | object). |
| 275 | - Superglobal means that the object survives to closing a module, and is imported |
| 276 | when the file which contains it is Required (even without Import). |
| 277 | - With the nodischarge variants, the object does not survive section closing. |
| 278 | With the normal variants, it does. |
| 279 | |
| 280 | We recommend to avoid declaring superglobal objects and using the nodischarge |
| 281 | variants. |
| 282 | *) |
| 283 | |
| 284 | val local_object : ?stage:Summary.Stage.t -> string -> |
| 285 | cache:('a -> unit) -> |
| 286 | discharge:('a -> 'a option) -> |
| 287 | ('a,'a,'a) object_declaration |
| 288 | |
| 289 | val local_object_nodischarge : ?stage:Summary.Stage.t -> string -> |
| 290 | cache:('a -> unit) -> |
| 291 | ('a,'a,'a) object_declaration |
| 292 | |
| 293 | val global_object : ?cat:category -> ?stage:Summary.Stage.t -> string -> |
| 294 | cache:('a -> unit) -> |
| 295 | subst:(Mod_subst.substitution * 'a -> 'a) option -> |
| 296 | discharge:('a -> 'a option) -> |
| 297 | ('a,'a,'a) object_declaration |
| 298 | |
| 299 | val global_object_nodischarge : ?cat:category -> ?stage:Summary.Stage.t -> string -> |
| 300 | cache:('a -> unit) -> |
| 301 | subst:(Mod_subst.substitution * 'a -> 'a) option -> |
| 302 | ('a,'a,'a) object_declaration |
| 303 | |
| 304 | val superglobal_object : ?stage:Summary.Stage.t -> string -> |
| 305 | cache:('a -> unit) -> |
| 306 | subst:(Mod_subst.substitution * 'a -> 'a) option -> |
| 307 | discharge:('a -> 'a option) -> |
| 308 | ('a,'a,'a) object_declaration |
| 309 | |
| 310 | val superglobal_object_nodischarge : ?stage:Summary.Stage.t -> string -> |
| 311 | cache:('a -> unit) -> |
| 312 | subst:(Mod_subst.substitution * 'a -> 'a) option -> |
| 313 | ('a,'a,'a) object_declaration |
| 314 | |
| 315 | (** {6 Debug} *) |
| 316 | |
| 317 | val dump : unit -> (int * string) list |