Debian Codegraph @

apps/tc/src/rocq_elpi_tc_register.ml

raw symbols SCIP
1 (* license: GNU Lesser General Public License Version 2.1 or later *)
2 (* ------------------------------------------------------------------------- *)
3
4 open Elpi_plugin
5 open Classes
6 open Rocq_elpi_arg_HOAS
7 open Names
8
9 type qualified_name = Rocq_elpi_utils.qualified_name
10
11 type loc_name_atts = (Loc.t * qualified_name * Attributes.vernac_flags)
12
13 (* Hack to convert a Coq GlobRef into an elpi string *)
14 let gref2elpi_term (gref: GlobRef.t) : Cmd.raw =
15 let gref_2_string gref = Pp.string_of_ppcmds (Printer.pr_global gref) in
16 Cmd.String (gref_2_string gref)
17
18
19 let mode2str = function
20 | None | Some [] -> ""
21 | Some (x::xs) ->
22 let pp_mode = Hints.string_of_mode in
23 List.fold_left (fun acc e -> acc ^ " " ^ pp_mode e) (pp_mode x) xs
24
25
26 (* Returns the elpi term representing the type class received in argument *)
27 let observer_class m (x : Typeclasses.typeclass) : Rocq_elpi_arg_HOAS.Cmd.raw list =
28 [Cmd.String "new_class"; gref2elpi_term x.cl_impl; Cmd.String (mode2str m)]
29
30 let observer_default_instance (x : Typeclasses.typeclass) : Rocq_elpi_arg_HOAS.Cmd.raw list =
31 [Cmd.String "default_instance";gref2elpi_term x.cl_impl]
32
33 let observer_coercion add (x : Typeclasses.typeclass) : Rocq_elpi_arg_HOAS.Cmd.raw list =
34 let name2str x = Cmd.String (Names.Name.print x |> Pp.string_of_ppcmds) in
35 let proj = x.cl_projs |> List.map (fun (x: Typeclasses.class_method) -> x.meth_name) in
36 let mode = if add then "add_coercions" else "remove_coercions" in
37 Cmd.String mode :: List.map name2str proj
38
39 (**
40 Returns the list of Cmd.raw arguments to be passed to the elpi program in charge
41 to compile instances. The arguments are [Inst, TC, Locality, Prio] where:
42 - Inst : is the elpi Term for the current instance
43 - TC : is the elpi Term for the type class implemented by Inst
44 - Locality : is the elpi String [Local|Global|Export] for the locality of Inst
45 - Prio : is the elpi Int N representing the priority of the instance. N is:
46 | -1 if the instance has no user-defined priority
47 | N if the instance has the user-defined priority N
48 *)
49 let observer_instance ({locality; instance; info; class_name} : instance) : Rocq_elpi_arg_HOAS.Cmd.raw list =
50 let locality2elpi_string loc =
51 let hint2string = function
52 | Hints.Local -> "Local"
53 | Export -> "Export"
54 | SuperGlobal -> "Global" in
55 Cmd.String (hint2string loc) in
56 let prio2elpi_int (prio: Typeclasses.hint_info) =
57 Cmd.Int (Option.default (-1) prio.hint_priority) in
58 [ Cmd.String "new_instance";
59 gref2elpi_term instance;
60 gref2elpi_term class_name;
61 locality2elpi_string locality;
62 prio2elpi_int info
63 ]
64
65 let class_runner f m cl =
66 let actions = [
67 observer_coercion false;
68 observer_class m;
69 observer_coercion true;
70 (* observer_default_instance *)
71 ] in
72 List.iter (fun obs -> f (obs cl)) actions
73
74 let observer_class =
75 Libobject.declare_object @@
76 Libobject.local_object "TC_HACK_OBSERVER_CLASSES"
77 ~cache:(fun (run,m,cl) -> class_runner run m cl)
78 ~discharge:(fun x -> Some x)
79
80 let observer_instance =
81 Libobject.declare_object @@
82 Libobject.local_object "TC_HACK_OBSERVER_INSTANCE"
83 ~cache:(fun (run,inst) -> run @@ observer_instance inst)
84 ~discharge:(fun (_,inst as x) -> if inst.locality = Local then None else Some x)
85
86 [%%if coq = "9.0" || coq = "9.1" || coq = "9.2"]
87 let observer_class_disp rp c = (observer_class (rp,None,c))
88 [%%else]
89 let observer_class_disp rp (omode,c) = (observer_class (rp,omode,c))
90 [%%endif]
91
92 let observer_evt ((loc, name, atts) : loc_name_atts) (x : Event.t) =
93 let open Rocq_elpi_vernacular in
94 let run_program e = Interp.run_program ~loc name ~syndata:None ~atts e in
95 match x with
96 | Event.NewClass cl -> Lib.add_leaf (observer_class_disp run_program cl)
97 | Event.NewInstance inst -> Lib.add_leaf (observer_instance (run_program,inst))
98
99 module StringMap = Map.Make(String)
100
101 type observers = observer StringMap.t
102
103 let observers : observers ref = Summary.ref StringMap.empty ~name:"tc_observers"
104
105 let build_observer_name (observer : qualified_name) =
106 String.concat "." observer
107
108 type action =
109 | Create of string * loc_name_atts
110 | Activate of qualified_name
111 | Deactivate of qualified_name
112
113 let action_manager x =
114 match x with
115 | Create (name, loc_name_atts) ->
116 let t1 = Sys.time () in
117 begin
118 try
119 let observer = Classes.register_observer ~name (observer_evt loc_name_atts) in
120 observers := StringMap.add name observer !observers;
121 Classes.activate_observer observer
122 with e when Rocq_elpi_utils.is_sync_anomaly e ->
123 Feedback.msg_warning Pp.(str (Printf.sprintf "%s already registered" name))
124 end;
125 let t2 = Sys.time () in
126 if Rocq_elpi_tc_time.get_time_tc_bench () then Feedback.msg_debug Pp.(str @@ Printf.sprintf "[TC] register.ml time is %.5f" (t1 -. t2))
127 | Activate observer ->
128 Classes.activate_observer (StringMap.find (build_observer_name observer) !observers)
129 | Deactivate observer ->
130 Classes.deactivate_observer (StringMap.find (build_observer_name observer) !observers)
131
132
133 (* Take an action and execute it with the action manager *)
134 let inTakeover =
135 let cache = action_manager
136 in Libobject.(declare_object
137 (superglobal_object_nodischarge "TC_HACK_OBSERVER" ~cache ~subst:None))
138
139 (* Adds a new observer in coq and activate it *)
140 let register_observer ((_, name, _) as lna : loc_name_atts) =
141 let obs_name = build_observer_name name in
142 Lib.add_leaf (inTakeover (Create (obs_name, lna)))
143
144 let activate_observer (observer : qualified_name) =
145 Lib.add_leaf (inTakeover (Activate observer))
146
147 let deactivate_observer (observer : qualified_name) =
148 Lib.add_leaf (inTakeover (Deactivate observer))