Debian Codegraph @

theories/Ltac2/Module.v

raw
1 (************************************************************************)
2 (* * The Coq Proof Assistant / The Coq 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 From Ltac2 Require Import Init Std.
12 From Ltac2 Require List.
13
14 (** The name of a module or module type. It may be a functor. It may also be currently open. *)
15 Ltac2 Type t.
16
17 Module Export Notations.
18 (** Use as e.g. [module:(Ltac2.Module.Notations)]. *)
19 Ltac2 Notation "module:(" x(module) ")" : 0 := x.
20 End Notations.
21 (* XXX add dynamic lookup (like Env.get)? *)
22
23 (** Test equality of module names. *)
24 Ltac2 @external equal : t -> t -> bool
25 := "rocq-runtime.plugins.ltac2" "module_equal".
26
27 (** Print a module name. *)
28 Ltac2 @external to_message : t -> message
29 := "rocq-runtime.plugins.ltac2" "module_to_message".
30
31 (** This returns [true] for both module types and module type functors. *)
32 Ltac2 @external is_modtype : t -> bool
33 := "rocq-runtime.plugins.ltac2" "module_is_modtype".
34
35 (** This returns [true] for both module functors and module type functors. *)
36 Ltac2 @external is_functor : t -> bool
37 := "rocq-runtime.plugins.ltac2" "module_is_functor".
38
39 (** This returns [true] for modules which are arguments of a currently open functor
40 ([false] for submodules of bound modules). *)
41 Ltac2 @external is_bound_module : t -> bool
42 := "rocq-runtime.plugins.ltac2" "module_is_bound_module".
43
44 (** This returns [true] for modules which are libraries (i.e. files). *)
45 Ltac2 @external is_library : t -> bool
46 := "rocq-runtime.plugins.ltac2" "module_is_library".
47
48 (** This returns [true] if the argument refers to a currently open
49 interactive module (or module type or functor). *)
50 Ltac2 @external is_open : t -> bool
51 := "rocq-runtime.plugins.ltac2" "module_is_open".
52
53 (** Return the parent module, ie [A] for [A.B].
54 Toplevel modules (libraries) and bound modules (functor arguments) return [None]. *)
55 Ltac2 @external parent_module : t -> t option
56 := "rocq-runtime.plugins.ltac2" "module_parent_module".
57
58 (** Throws on [VarRef]. *)
59 Ltac2 @external module_of_reference : Std.reference -> t
60 := "rocq-runtime.plugins.ltac2" "module_of_reference".
61
62 (** Returns a reference to the current innermost open interactive module. *)
63 Ltac2 @external current_module : unit -> t
64 := "rocq-runtime.plugins.ltac2" "current_module".
65
66 Ltac2 @external loaded_libraries : unit -> t list
67 := "rocq-runtime.plugins.ltac2" "module_loaded_libraries".
68
69 Module Field.
70 (** This API is written using a record of functions to implement
71 case analysis instead of exposing [t] as a sum type for forward compatibility:
72 a case analysis can be written as [{ handle_nothing with ...}]
73 and any new field kinds will be handled without error. *)
74
75 (** Type of module fields. *)
76 Ltac2 Type t.
77
78 (** Information about rewrite rules. Currently nothing is exposed about it. *)
79 Ltac2 Type rewrule.
80
81 (** Case analysis over a module field. *)
82 Ltac2 Type 'a handler := {
83 handle_submodule : Module.t -> 'a;
84 handle_reference : Std.reference -> 'a;
85 handle_rewrule : rewrule -> 'a;
86 }.
87
88 (** Default case analysis. *)
89 Ltac2 handle_nothing := {
90 handle_submodule := (fun _ => None);
91 handle_reference := (fun _ => None);
92 handle_rewrule := (fun _ => None);
93 }.
94
95 (** Execute a case analysis. *)
96 Ltac2 @external handle : t -> 'a handler -> 'a
97 := "rocq-runtime.plugins.ltac2" "module_field_handle".
98
99 (** Test whether a module field is a submodule (or sub-module type or sub-functor). *)
100 Ltac2 is_submodule (f:t) : Module.t option :=
101 handle f { handle_nothing with handle_submodule := (fun x => Some x) }.
102
103 (** Test whether a module field is a reference. *)
104 Ltac2 is_reference (f:t) : Std.reference option :=
105 handle f { handle_nothing with handle_reference := (fun x => Some x) }.
106
107 End Field.
108
109 (** Return the contents of the given module. [None] on closed functors and module types.
110 Inductives are represented only by the first inductive of each mutual block (and no constructors). *)
111 Ltac2 @external module_contents : t -> Field.t list option
112 := "rocq-runtime.plugins.ltac2" "module_contents".
113
114 (** Deeply fold over references in the given module
115 (e.g. if [mod] is [M] and contains submodule [M.N]
116 which contains reference [M.N.x], [M.N.x] will be passed to [f]). *)
117 Ltac2 rec deep_fold_child_references (f:'a -> Std.reference -> 'a) (acc:'a) (mod:t) : 'a :=
118 match module_contents mod with
119 | None => acc
120 | Some contents =>
121 List.fold_left
122 (fun acc field =>
123 Field.handle field {
124 Field.handle_submodule := (fun mod => deep_fold_child_references f acc mod);
125 Field.handle_reference := (fun r => f acc r);
126 Field.handle_rewrule := (fun _ => acc);
127 })
128 acc contents
129 end.