| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
[%%import "rocq_elpi_config.mlh"] |
| 6 |
|
| 7 |
module API = Elpi.API |
| 8 |
module State = API.State |
| 9 |
module Conv = API.Conversion |
| 10 |
module CConv = API.ContextualConversion |
| 11 |
module B = struct |
| 12 |
include API.BuiltInData |
| 13 |
include Elpi.Builtin |
| 14 |
end |
| 15 |
module Pred = API.BuiltInPredicate |
| 16 |
module U = API.Utils |
| 17 |
|
| 18 |
open Names |
| 19 |
|
| 20 |
open Rocq_elpi_utils |
| 21 |
open Rocq_elpi_HOAS |
| 22 |
|
| 23 |
let prop = { B.any with Conv.ty = Conv.TyName "prop" } |
| 24 |
|
| 25 |
let id = { B.string with |
| 26 |
API.Conversion.ty = Conv.TyName "id"; |
| 27 |
pp_doc = (fun fmt () -> |
| 28 |
Format.fprintf fmt "%% [id] is a name that matters, we piggy back on Elpi's strings.@\n"; |
| 29 |
Format.fprintf fmt "%% Note: [name] is a name that does not matter.@\n"; |
| 30 |
Format.fprintf fmt "typeabbrev id string.@\n@\n") |
| 31 |
} |
| 32 |
|
| 33 |
type scope = ExecutionSite | CurrentModule | Library |
| 34 |
|
| 35 |
let options : (options, API.Data.constraints) CConv.ctx_readback = |
| 36 |
fun ~depth hyps constraints state -> |
| 37 |
state, get_options ~depth (API.RawData.of_hyps hyps) state, constraints, [] |
| 38 |
|
| 39 |
let scope = let open Conv in let open API.AlgebraicData in declare { |
| 40 |
ty = TyName "scope"; |
| 41 |
doc = "Specify to which module the clause should be attached to"; |
| 42 |
pp = (fun fmt _ -> Format.fprintf fmt "<todo>"); |
| 43 |
constructors = [ |
| 44 |
K("execution-site","The module inside which the Elpi program is run",N, |
| 45 |
B ExecutionSite, |
| 46 |
M (fun ~ok ~ko -> function ExecutionSite -> ok | _ -> ko ())); |
| 47 |
K("current","The module being defined (see begin/end-module)",N, |
| 48 |
B CurrentModule, |
| 49 |
M (fun ~ok ~ko -> function CurrentModule -> ok | _ -> ko ())); |
| 50 |
K("library","The outermost module (carrying the file name)",N, |
| 51 |
B Library, |
| 52 |
M (fun ~ok ~ko -> function Library -> ok | _ -> ko ())) |
| 53 |
] |
| 54 |
} |> CConv.(!<) |
| 55 |
|
| 56 |
let grafting = let open Conv in let open API.AlgebraicData in declare { |
| 57 |
ty = TyName "grafting"; |
| 58 |
doc = "Specify if the clause has to be grafted before, grafted after or replace a named clause"; |
| 59 |
pp = (fun fmt _ -> Format.fprintf fmt "<todo>"); |
| 60 |
constructors = [ |
| 61 |
K("before","",A(id,N), |
| 62 |
B (fun x -> (`Before,x)), |
| 63 |
M (fun ~ok ~ko -> function (`Before,x) -> ok x | _ -> ko ())); |
| 64 |
K("after","",A(id,N), |
| 65 |
B (fun x -> (`After,x)), |
| 66 |
M (fun ~ok ~ko -> function (`After,x) -> ok x | _ -> ko ())); |
| 67 |
K("remove","",A(id,N), |
| 68 |
B (fun x -> (`Remove,x)), |
| 69 |
M (fun ~ok ~ko -> function (`Remove,x) -> ok x | _ -> ko ())); |
| 70 |
K("replace","",A(id,N), |
| 71 |
B (fun x -> (`Replace,x)), |
| 72 |
M (fun ~ok ~ko -> function (`Replace,x) -> ok x | _ -> ko ())); |
| 73 |
] |
| 74 |
} |> CConv.(!<) |
| 75 |
|
| 76 |
type clause = string option * ([ `After | `Before | `Remove | `Replace ] * string) option * API.Data.term |
| 77 |
|
| 78 |
let clause = let open Conv in let open API.AlgebraicData in declare { |
| 79 |
ty = TyName "clause"; |
| 80 |
doc = {|clauses |
| 81 |
|
| 82 |
A clause like |
| 83 |
:name "foo" :before "bar" foo X Y :- bar X Z, baz Z Y |
| 84 |
is represented as |
| 85 |
clause "foo" (before "bar") (pi x y z\ foo x y :- bar x z, baz z y) |
| 86 |
that is exactly what one would load in the context using =>. |
| 87 |
|
| 88 |
The name and the grafting specification can be left unspecified.|}; |
| 89 |
pp = (fun fmt _ -> Format.fprintf fmt "<todo>"); |
| 90 |
constructors = [ |
| 91 |
K("clause","",A(B.unspec id,A(B.unspec grafting,A(prop,N))), |
| 92 |
B (fun id graft c -> unspec2opt id, unspec2opt graft, c), |
| 93 |
M (fun ~ok ~ko (id,graft,c) -> ok (opt2unspec id) (opt2unspec graft) c)); |
| 94 |
] |
| 95 |
} |> CConv.(!<) |
| 96 |
|
| 97 |
let set_accumulate_to_db_synterp, get_accumulate_to_db_synterp = |
| 98 |
let f = ref (fun ~loc:_ _ -> assert false) in |
| 99 |
(fun x -> f := x), |
| 100 |
(fun () -> !f) |
| 101 |
|
| 102 |
let clauses_for_later_synterp : _ State.component = |
| 103 |
State.declare_component ~name:"rocq-elpi:clauses_for_later" ~descriptor:synterp_state |
| 104 |
~init:(fun () -> []) |
| 105 |
~start:(fun x -> x) |
| 106 |
~pp:(fun fmt l -> |
| 107 |
List.iter (fun (dbname, code,vars,scope) -> |
| 108 |
Format.fprintf fmt "db:%s code:%a scope:%a\n" |
| 109 |
(String.concat "." dbname) |
| 110 |
Elpi.API.Pp.Ast.program code Rocq_elpi_utils.pp_scope scope) l) () |
| 111 |
|
| 112 |
|
| 113 |
type located = |
| 114 |
| LocGref of Names.GlobRef.t |
| 115 |
| LocModule of Names.ModPath.t |
| 116 |
| LocModuleType of Names.ModPath.t |
| 117 |
| LocAbbreviation of Globnames.abbreviation |
| 118 |
|
| 119 |
let located = let open Conv in let open API.AlgebraicData in declare { |
| 120 |
ty = TyName "located"; |
| 121 |
doc = "Result of coq.locate-all"; |
| 122 |
pp = (fun fmt _ -> Format.fprintf fmt "<todo>"); |
| 123 |
constructors = [ |
| 124 |
K("loc-gref","",A(gref,N), |
| 125 |
B (fun x -> LocGref x), |
| 126 |
M (fun ~ok ~ko -> function LocGref x -> ok x | _ -> ko ())); |
| 127 |
K("loc-modpath","",A(modpath,N), |
| 128 |
B (fun x -> LocModule x), |
| 129 |
M (fun ~ok ~ko -> function LocModule x -> ok x | _ -> ko ())); |
| 130 |
K("loc-modtypath","",A(modtypath,N), |
| 131 |
B (fun x -> LocModuleType x), |
| 132 |
M (fun ~ok ~ko -> function LocModuleType x -> ok x | _ -> ko ())); |
| 133 |
K("loc-abbreviation","",A(abbreviation,N), |
| 134 |
B (fun x -> LocAbbreviation x), |
| 135 |
M (fun ~ok ~ko -> function LocAbbreviation x -> ok x | _ -> ko ())); |
| 136 |
] |
| 137 |
} |> CConv.(!<) |
| 138 |
|
| 139 |
|
| 140 |
let list = B.list |
| 141 |
let pair = B.pair |
| 142 |
let option = B.option |
| 143 |
|
| 144 |
let invocation_site_loc : API.Ast.Loc.t State.component = |
| 145 |
State.declare_component ~name:"rocq-elpi:invocation-site-loc" ~descriptor:interp_state |
| 146 |
~pp:(fun fmt x -> Format.fprintf fmt "%a" API.Ast.Loc.pp x) |
| 147 |
~init:(fun () -> API.Ast.Loc.initial "(should-not-happen)") |
| 148 |
~start:(fun x -> x) () |
| 149 |
let invocation_site_loc_synterp : API.Ast.Loc.t State.component = |
| 150 |
State.declare_component ~name:"rocq-elpi:invocation-site-loc" ~descriptor:synterp_state |
| 151 |
~pp:(fun fmt x -> Format.fprintf fmt "%a" API.Ast.Loc.pp x) |
| 152 |
~init:(fun () -> API.Ast.Loc.initial "(should-not-happen)") |
| 153 |
~start:(fun x -> x) () |
| 154 |
|
| 155 |
[%%if elpi >= (1, 20, 0)] |
| 156 |
let compat_graft x = x |
| 157 |
[%%else] |
| 158 |
let compat_graft = Option.map (function `Remove, _ -> nYI "clause removal" | ((`Replace | `Before | `After), _) as x -> x) |
| 159 |
[%%endif] |
| 160 |
|
| 161 |
type accumulation_item = qualified_name * API.Ast.program * Id.t list * Rocq_elpi_utils.clause_scope |
| 162 |
let accumulate_clauses ~clauses_for_later ~accumulate_to_db ~preprocess_clause ~scope ~dbname clauses ~depth ~options state = |
| 163 |
let invocation_loc = State.get invocation_site_loc_synterp state in |
| 164 |
let loc = invocation_loc in |
| 165 |
let dbname = Rocq_elpi_utils.string_split_on_char '.' dbname in |
| 166 |
let clauses scope = |
| 167 |
clauses |> CList.rev_map (fun (name,graft,clause) -> |
| 168 |
let vars, clause = preprocess_clause ~depth clause in |
| 169 |
let graft = compat_graft graft in |
| 170 |
let clause = U.clause_of_term ?name ?graft ~depth loc clause in |
| 171 |
(dbname,clause,vars,scope)) in |
| 172 |
let local = (options : options).local = Some true in |
| 173 |
let super_global = options.local = Some false in |
| 174 |
match scope with |
| 175 |
| B.Unspec | B.Given ExecutionSite -> |
| 176 |
let scope = if super_global then SuperGlobal else if local then Local else Regular in |
| 177 |
State.update clauses_for_later state (fun l -> |
| 178 |
clauses scope @ l), (), [] |
| 179 |
| B.Given Library -> |
| 180 |
if local then CErrors.user_err Pp.(str "coq.elpi.accumulate: library scope is incompatible with @local!"); |
| 181 |
State.update clauses_for_later state (fun l -> |
| 182 |
clauses Rocq_elpi_utils.Global @ l), (), [] |
| 183 |
| B.Given CurrentModule -> |
| 184 |
let scope = if local then Local else Regular in |
| 185 |
let f = accumulate_to_db in |
| 186 |
f ~loc:(to_coq_loc loc) (clauses scope); |
| 187 |
state, (), [] |
| 188 |
|
| 189 |
let locate_module, locate_module_type = |
| 190 |
let open API.BuiltIn in |
| 191 |
let open Pred in |
| 192 |
let open Notation in |
| 193 |
MLCode(Pred("coq.locate-module", |
| 194 |
In(id, "ModName", |
| 195 |
Out(modpath, "ModPath", |
| 196 |
Easy "locates a module. It's a fatal error if ModName cannot be located. *E*")), |
| 197 |
(fun s _ ~depth -> |
| 198 |
let qualid = Libnames.qualid_of_string s in |
| 199 |
let mp = |
| 200 |
try Nametab.locate_module qualid |
| 201 |
with Not_found -> |
| 202 |
err Pp.(str "Module not found: " ++ Libnames.pr_qualid qualid) in |
| 203 |
!:mp)), |
| 204 |
DocAbove), |
| 205 |
MLCode(Pred("coq.locate-module-type", |
| 206 |
In(id, "ModName", |
| 207 |
Out(modtypath, "ModPath", |
| 208 |
Easy "locates a module. It's a fatal error if ModName cannot be located. *E*")), |
| 209 |
(fun s _ ~depth -> |
| 210 |
let qualid = Libnames.qualid_of_string s in |
| 211 |
let mp = |
| 212 |
try Nametab.locate_modtype qualid |
| 213 |
with Not_found -> |
| 214 |
err Pp.(str "Module type not found: " ++ Libnames.pr_qualid qualid) in |
| 215 |
!:mp)), |
| 216 |
DocAbove) |
| 217 |
|
| 218 |
let modpath_to_path, modtypath_to_path, modpath_to_library, modtypath_to_library = |
| 219 |
let open API.BuiltIn in |
| 220 |
let open Pred in |
| 221 |
let open Notation in |
| 222 |
let open CConv in |
| 223 |
MLCode(Pred("coq.modpath->path", |
| 224 |
In(modpath, "MP", |
| 225 |
Out(B.list B.string, "FullPath", |
| 226 |
Read(unit_ctx, "extract the full kernel name, each component is a separate list item"))), |
| 227 |
(fun mp _ ~depth h c state -> !: (mp2path mp))), |
| 228 |
DocAbove), |
| 229 |
MLCode(Pred("coq.modtypath->path", |
| 230 |
In(modtypath, "MTP", |
| 231 |
Out(B.list B.string, "FullPath", |
| 232 |
Read(unit_ctx, "extract the full kernel name, each component is a separate list item"))), |
| 233 |
(fun mtyp _ ~depth h c state -> !: (mp2path mtyp))), |
| 234 |
DocAbove), |
| 235 |
MLCode(Pred("coq.modpath->library", |
| 236 |
In(modpath, "MP", |
| 237 |
Out(modpath, "LibraryPath", |
| 238 |
Read(unit_ctx, "extract the enclosing module which can be Required"))), |
| 239 |
(fun mp _ ~depth h c state -> !: ModPath.(MPfile (dp mp)))), |
| 240 |
DocAbove), |
| 241 |
MLCode(Pred("coq.modtypath->library", |
| 242 |
In(modtypath, "MTP", |
| 243 |
Out(modpath, "LibraryPath", |
| 244 |
Read(unit_ctx, "extract the enclosing module which can be Required"))), |
| 245 |
(fun mtyp _ ~depth h c state -> !: ModPath.(MPfile (dp mtyp)))), |
| 246 |
DocAbove) |
| 247 |
|
| 248 |
let current_path, current_section_path = |
| 249 |
let open API.BuiltIn in |
| 250 |
let open Pred in |
| 251 |
let open Notation in |
| 252 |
let open CConv in |
| 253 |
MLCode(Pred("coq.env.current-path", |
| 254 |
Out(list B.string, "Path", |
| 255 |
Read(unit_ctx, "lists the current module path")), |
| 256 |
(fun _ ~depth _ _ state -> !: (mp2path (Lib.current_mp ())))), |
| 257 |
DocAbove), |
| 258 |
MLCode(Pred("coq.env.current-section-path", |
| 259 |
Out(list B.string, "Path", |
| 260 |
Read(unit_ctx, "lists the current section path")), |
| 261 |
(fun _ ~depth _ _ state -> |
| 262 |
let base = Lib.current_dirpath false in |
| 263 |
let base_w_sections = Lib.current_dirpath true in |
| 264 |
let sections = Libnames.drop_dirpath_prefix base base_w_sections in |
| 265 |
!: (mp2path (Names.ModPath.MPfile sections)))), |
| 266 |
DocAbove) |
| 267 |
|
| 268 |
module SynterpAction = struct |
| 269 |
open Declaremods |
| 270 |
type action = |
| 271 |
| BeginModule of (string * ModPath.t option * (string * ModPath.t) list) * module_params_expr * module_expr module_signature |
| 272 |
| BeginModuleType of (string * (string * ModPath.t) list) * module_params_expr * module_expr list |
| 273 |
| BeginSection of string |
| 274 |
| EndModule of ModPath.t |
| 275 |
| EndModuleType of ModPath.t |
| 276 |
| EndSection |
| 277 |
| ApplyModule of (string * ModPath.t option * ModPath.t * ModPath.t list * Declaremods.inline) * module_params_expr * module_expr list * module_expr module_signature |
| 278 |
| ApplyModuleType of (string * ModPath.t * ModPath.t list * Declaremods.inline) * module_params_expr * module_expr list * module_expr list |
| 279 |
| IncludeModule of (ModPath.t * Declaremods.inline) * module_expr list |
| 280 |
| IncludeModuleType of (ModPath.t * Declaremods.inline) * module_expr list |
| 281 |
| ImportModule of ModPath.t |
| 282 |
| ExportModule of ModPath.t |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
type t = { action : action; resulting_state : Vernacstate.Synterp.t } |
| 292 |
let synterp_state_after { resulting_state } = resulting_state |
| 293 |
|
| 294 |
let pp_action = function |
| 295 |
| BeginModule ((id,_,_),_,_) -> |
| 296 |
Pp.(str "begin-module" ++ spc () ++ qstring id) |
| 297 |
| BeginModuleType ((id,_),_,_) -> |
| 298 |
Pp.(str "begin-module-type" ++ spc () ++ qstring id) |
| 299 |
| EndModule mp -> |
| 300 |
Pp.(str "end-module" ++ spc () ++ (str @@ ModPath.to_string mp)) |
| 301 |
| EndModuleType mp -> |
| 302 |
Pp.(str "end-module-type" ++ spc () ++ (str @@ ModPath.to_string mp)) |
| 303 |
| BeginSection id -> |
| 304 |
Pp.(str "begin-section" ++ spc () ++ qstring id) |
| 305 |
| EndSection -> |
| 306 |
Pp.(str "end-section" ++ spc ()) |
| 307 |
| ApplyModule ((id,_,_,_,_),_,_,_) -> |
| 308 |
Pp.(str "apply-module" ++ spc () ++ qstring id) |
| 309 |
| ApplyModuleType ((id,_,_,_),_,_,_) -> |
| 310 |
Pp.(str "apply-module-type" ++ spc () ++ qstring id) |
| 311 |
| IncludeModule ((mp,_),_) -> |
| 312 |
Pp.(str "include-module" ++ spc () ++ (str @@ ModPath.to_string mp)) |
| 313 |
| IncludeModuleType ((mp,_),_) -> |
| 314 |
Pp.(str "include-module-type" ++ spc () ++ (str @@ ModPath.to_string mp)) |
| 315 |
| ImportModule mp -> |
| 316 |
Pp.(str "import-module" ++ spc () ++ (str @@ ModPath.to_string mp)) |
| 317 |
| ExportModule mp -> |
| 318 |
Pp.(str "export-module" ++ spc () ++ (str @@ ModPath.to_string mp)) |
| 319 |
let pp { action } = pp_action action |
| 320 |
|
| 321 |
module Tree = struct |
| 322 |
module Group : sig |
| 323 |
type group |
| 324 |
|
| 325 |
val group : group API.Conversion.t |
| 326 |
|
| 327 |
val new_group : string -> group |
| 328 |
|
| 329 |
val group_name : group -> string |
| 330 |
|
| 331 |
val debug_pp_group : group -> Pp.t |
| 332 |
end = struct |
| 333 |
type group = {uid : int; name : string} |
| 334 |
|
| 335 |
let group : group API.Conversion.t = |
| 336 |
let open API.OpaqueData in |
| 337 |
declare { |
| 338 |
name = "group"; |
| 339 |
doc = "Synterp action group"; |
| 340 |
pp = (fun fmt {uid; name} -> Format.fprintf fmt "{uid=%i;name=%S}" uid name); |
| 341 |
compare = Stdlib.compare; |
| 342 |
hash = Hashtbl.hash; |
| 343 |
hconsed = false; |
| 344 |
constants = []; |
| 345 |
} |
| 346 |
|
| 347 |
let new_group : string -> group = |
| 348 |
let r = ref (-1) in fun name -> incr r; {uid = !r; name} |
| 349 |
|
| 350 |
let group_name : group -> string = fun g -> g.name |
| 351 |
|
| 352 |
let debug_pp_group : group -> Pp.t = fun {uid; name} -> |
| 353 |
Pp.(str name ++ str " (uid=" ++ int uid ++ str ")") |
| 354 |
end |
| 355 |
include Group |
| 356 |
|
| 357 |
type node = |
| 358 |
| Group of group * tree |
| 359 |
| Action of t |
| 360 |
|
| 361 |
and tree = node list |
| 362 |
|
| 363 |
let rec to_list : tree -> t list = fun t -> |
| 364 |
let fn n = |
| 365 |
match n with |
| 366 |
| Group (_, t) -> to_list t |
| 367 |
| Action a -> [a] |
| 368 |
in |
| 369 |
List.concat_map fn t |
| 370 |
|
| 371 |
let rec debug_pp : tree -> Pp.t = fun ns -> |
| 372 |
let fn acc n = |
| 373 |
let open Pp in |
| 374 |
match n with |
| 375 |
| Action a -> |
| 376 |
acc ++ |
| 377 |
str "Action: " ++ pp a ++ fnl () |
| 378 |
| Group (g, ns) -> |
| 379 |
acc ++ |
| 380 |
str "Entering group: " ++ debug_pp_group g ++ fnl () ++ |
| 381 |
debug_pp ns ++ |
| 382 |
str "Leaving group: " ++ debug_pp_group g ++ fnl () |
| 383 |
in |
| 384 |
List.fold_left fn Pp.(str "") ns |
| 385 |
end |
| 386 |
|
| 387 |
module WZipper = struct |
| 388 |
open Tree |
| 389 |
|
| 390 |
|
| 391 |
type rev_tree = node list |
| 392 |
|
| 393 |
type path = |
| 394 |
| P_top |
| 395 |
| P_group of group * rev_tree * path |
| 396 |
|
| 397 |
type zipper = rev_tree * path |
| 398 |
|
| 399 |
let empty = ([], P_top) |
| 400 |
|
| 401 |
let rec collect : zipper -> tree = fun (rev_items, path) -> |
| 402 |
match path with |
| 403 |
| P_top -> List.rev rev_items |
| 404 |
| P_group(group, items, path) -> |
| 405 |
let s = |
| 406 |
let items = List.rev rev_items in |
| 407 |
Group(group, items) |
| 408 |
in |
| 409 |
collect (s :: items, path) |
| 410 |
|
| 411 |
let to_list : zipper -> t list = fun z -> |
| 412 |
Tree.to_list (collect z) |
| 413 |
|
| 414 |
let add_action : zipper -> t -> zipper = fun (rev_items, path) a -> |
| 415 |
(Action a :: rev_items, path) |
| 416 |
|
| 417 |
let open_group : zipper -> string -> zipper * group = fun (rev_items, path) group -> |
| 418 |
let group = new_group group in |
| 419 |
(([], P_group(group,rev_items,path)), group) |
| 420 |
|
| 421 |
let close_group : zipper -> group -> (zipper, [`GroupMismatch of group|`NoGroup]) result = fun (group_items, path) group -> |
| 422 |
match path with |
| 423 |
| P_group (group', items, path) when group' = group -> |
| 424 |
Ok (Group (group, List.rev group_items) :: items, path) |
| 425 |
| P_group (group', _, _) -> Error (`GroupMismatch group') |
| 426 |
| P_top -> Error `NoGroup |
| 427 |
|
| 428 |
end |
| 429 |
|
| 430 |
module RZipper = struct |
| 431 |
open Tree |
| 432 |
|
| 433 |
type path = |
| 434 |
| P_top |
| 435 |
| P_group of group * tree * path |
| 436 |
|
| 437 |
type zipper = path * tree |
| 438 |
|
| 439 |
let empty : zipper = (P_top, []) |
| 440 |
|
| 441 |
let rec collect : zipper -> tree = fun (path, items) -> |
| 442 |
match path with |
| 443 |
| P_top -> items |
| 444 |
| P_group(group, group_items, path) -> |
| 445 |
let s = Group(group, items) in |
| 446 |
collect (path, s :: group_items) |
| 447 |
|
| 448 |
let of_w : WZipper.zipper -> zipper = fun wz -> |
| 449 |
(P_top, WZipper.collect wz) |
| 450 |
|
| 451 |
let open_group : zipper -> string -> (zipper * group, [`GroupMismatch of group | `LeadingAction of t | `Empty]) result = fun (path, items) group -> |
| 452 |
match items with |
| 453 |
| Group (group', group_items) :: items when group_name group' = group -> |
| 454 |
Ok ((P_group(group', items, path), group_items), group') |
| 455 |
| Group (group', _) :: _ -> Error (`GroupMismatch group') |
| 456 |
| Action a :: _ -> Error (`LeadingAction a) |
| 457 |
| [] -> Error `Empty |
| 458 |
|
| 459 |
let close_group : zipper -> group -> (zipper, [`GroupMismatch of group| `NoGroup]) result = fun (path, group_items) group -> |
| 460 |
match path with |
| 461 |
| P_group (group', items, path) when group_items = [] && group' = group -> |
| 462 |
Ok (path, items) |
| 463 |
| P_group (group', _, _) -> Error (`GroupMismatch group') |
| 464 |
| P_top -> Error `NoGroup |
| 465 |
|
| 466 |
let pop_action : zipper -> (t * zipper, [`LeadingGroup of group | `Empty]) result = fun (path, items) -> |
| 467 |
match items with |
| 468 |
| Action a :: items -> Ok (a, (path, items)) |
| 469 |
| Group (group, _) :: _ -> Error (`LeadingGroup group) |
| 470 |
| [] -> Error `Empty |
| 471 |
|
| 472 |
let pop_group : zipper -> string -> (t list * zipper, [`GroupMismatch of group | `LeadingAction of t | `Empty]) result = fun (path, items) group -> |
| 473 |
match items with |
| 474 |
| Group (group', group_items) :: items when group_name group' = group -> |
| 475 |
let actions = Tree.to_list group_items in |
| 476 |
Ok (actions, (path, items)) |
| 477 |
| Group (group', _) :: _ -> Error (`GroupMismatch group') |
| 478 |
| Action a :: _ -> Error (`LeadingAction a) |
| 479 |
| [] -> Error `Empty |
| 480 |
|
| 481 |
let is_empty : zipper -> [`Empty|`Group of group|`Action of t] = fun rz -> |
| 482 |
match pop_action rz with |
| 483 |
| Error `Empty -> `Empty |
| 484 |
| Error (`LeadingGroup g) -> (`Group g) |
| 485 |
| Ok (a, _) -> `Action a |
| 486 |
end |
| 487 |
|
| 488 |
let action = |
| 489 |
let open Conv in let open API.AlgebraicData in declare { |
| 490 |
ty = TyName "synterp-action"; |
| 491 |
doc = "Action executed during the parsing phase (aka synterp)"; |
| 492 |
pp = (fun fmt a -> Pp.pp_with fmt (pp_action a)); |
| 493 |
constructors = [ |
| 494 |
K("begin-module","",A(id,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function BeginModule ((x,_,_),_,_) -> ok x | _ -> ko ())); |
| 495 |
K("begin-module-type","",A(id,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function BeginModuleType ((x,_),_,_) -> ok x | _ -> ko ())); |
| 496 |
K("begin-section","",A(id,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function BeginSection x -> ok x | _ -> ko ())); |
| 497 |
K("end-module","",A(modpath,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function EndModule x -> ok x | _ -> ko ())); |
| 498 |
K("end-module-type","",A(modtypath,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function EndModuleType x -> ok x | _ -> ko ())); |
| 499 |
K("end-section","",N,B EndSection,M (fun ~ok ~ko -> function EndSection -> ok | _ -> ko ())); |
| 500 |
K("apply-module-functor","",A(id,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function ApplyModule ((x,_,_,_,_),_,_,_) -> ok x | _ -> ko ())); |
| 501 |
K("apply-module-type-functor","",A(id,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function ApplyModuleType ((x,_,_,_),_,_,_) -> ok x | _ -> ko ())); |
| 502 |
K("include-module","",A(modpath,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function IncludeModule ((x,_),_) -> ok x | _ -> ko ())); |
| 503 |
K("include-module-type","",A(modtypath,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function IncludeModuleType ((x,_),_) -> ok x | _ -> ko ())); |
| 504 |
K("import-module","",A(modpath,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function ImportModule x -> ok x | _ -> ko ())); |
| 505 |
K("export-module","",A(modpath,N),B(fun x -> nYI "readback action"),M (fun ~ok ~ko -> function ExportModule x -> ok x | _ -> ko ())); |
| 506 |
] |
| 507 |
} |> CConv.(!<) |
| 508 |
|
| 509 |
let log : WZipper.zipper State.component = |
| 510 |
State.declare_component ~name:"rocq-elpi:synterp-action-write" ~descriptor:synterp_state |
| 511 |
~pp:(fun fmt x -> Format.fprintf fmt "<todo>") |
| 512 |
~init:(fun () -> WZipper.empty) |
| 513 |
~start:(fun x -> x) () |
| 514 |
|
| 515 |
exception Error of Pp.t |
| 516 |
let synterp_interp_error x = raise (Error x) |
| 517 |
|
| 518 |
let get_parsing_actions_synterp = |
| 519 |
let open API.BuiltIn in |
| 520 |
let open Pred in |
| 521 |
let open Notation in |
| 522 |
let open CConv in |
| 523 |
|
| 524 |
[ |
| 525 |
|
| 526 |
MLData action; |
| 527 |
MLData Tree.group; |
| 528 |
|
| 529 |
MLCode (Pred("coq.synterp-actions", |
| 530 |
Out(list action,"A", |
| 531 |
Read(unit_ctx,"Get the list of actions performed during the parsing phase (aka synterp) up to now.")), |
| 532 |
(fun _ ~depth _ _ state -> !: (List.map (fun { action } -> action) @@ List.rev (WZipper.to_list (State.get log state))))), |
| 533 |
DocAbove); |
| 534 |
|
| 535 |
MLCode(Pred("coq.begin-synterp-group", |
| 536 |
In(id, "ID", |
| 537 |
Out(Tree.group, "Group", |
| 538 |
Full(unit_ctx,"Create and open a new synterp action group with the given name."))), |
| 539 |
(fun name _ ~depth _ _ state -> |
| 540 |
let (s, k) = State.update_return log state (fun wz -> WZipper.open_group wz name) in |
| 541 |
s, ((), Some k), [])), |
| 542 |
DocAbove); |
| 543 |
|
| 544 |
MLCode(Pred("coq.end-synterp-group", |
| 545 |
In(Tree.group, "Group", |
| 546 |
Full(unit_ctx,"End the synterp action group Group. Group must refer to the most recently openned group.")), |
| 547 |
(fun name ~depth _ _ state -> |
| 548 |
State.update log state (fun wz -> |
| 549 |
match WZipper.close_group wz name with |
| 550 |
| Ok wz -> wz |
| 551 |
| Error `NoGroup -> |
| 552 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to close a synterp action group (" ^ Tree.group_name name ^ ") but there is no group to close."))) |
| 553 |
| Error (`GroupMismatch name') -> |
| 554 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to close a synterp action group (" ^ Tree.group_name name ^ ") but the current group has name " ^ Tree.group_name name' ^ "."))) |
| 555 |
), (), [])), |
| 556 |
DocAbove); |
| 557 |
] |
| 558 |
|
| 559 |
let read : RZipper.zipper State.component = |
| 560 |
State.declare_component ~name:"rocq-elpi:synterp-action-read" ~descriptor:interp_state |
| 561 |
~pp:(fun fmt x -> Format.fprintf fmt "<todo>") |
| 562 |
~init:(fun () -> RZipper.empty) |
| 563 |
~start:(fun x -> x) () |
| 564 |
|
| 565 |
|
| 566 |
let push action state = |
| 567 |
State.update log state (fun l -> WZipper.add_action l { action; resulting_state = Vernacstate.Synterp.freeze () }) |
| 568 |
|
| 569 |
let common_err = "Interp actions must match synterp actions. For example if a module was imported during the synterp phase, then it must also be imported during the interp one." |
| 570 |
|
| 571 |
let pop case state = |
| 572 |
State.update_return read state (fun rz -> |
| 573 |
match RZipper.pop_action rz with |
| 574 |
| Ok (a, rz) -> Vernacstate.Synterp.unfreeze a.resulting_state; rz, a.action |
| 575 |
| Error (`LeadingGroup g) -> |
| 576 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command created an action group (" ^ Tree.group_name g ^ ") in the synterp phase but did not open the group in the interp phase."))) |
| 577 |
| Error `Empty -> |
| 578 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command did perform no (more) actions during the parsing phase (aka synterp), while during the execution phase (aka interp) it tried to perform a") ++ spc() ++ str case ++ spc() ++ str "action." ++ spc() ++ strbrk common_err))) |
| 579 |
|
| 580 |
type 'a replay = 'a -> State.t -> State.t * ModPath.t option |
| 581 |
|
| 582 |
let interp_close_section = Declaremods.Interp.close_section |
| 583 |
|
| 584 |
let replay1 action state = |
| 585 |
match action with |
| 586 |
| BeginModule((name,_,_),binders_ast,ty) -> |
| 587 |
if Global.sections_are_opened () then |
| 588 |
err Pp.(str"This code cannot be run within a section since it opens a module"); |
| 589 |
let id = Id.of_string name in |
| 590 |
let mp = Declaremods.Interp.start_module None id binders_ast ty in |
| 591 |
let loc = to_coq_loc @@ State.get invocation_site_loc state in |
| 592 |
Dumpglob.dump_moddef ~loc mp "mod"; |
| 593 |
(state, None) |
| 594 |
| BeginModuleType((name,_),binders_ast,ty) -> |
| 595 |
if Global.sections_are_opened () then |
| 596 |
err Pp.(str"This code cannot be run within a section since it opens a module"); |
| 597 |
let id = Id.of_string name in |
| 598 |
let mp = Declaremods.Interp.start_modtype id binders_ast ty in |
| 599 |
let loc = to_coq_loc @@ State.get invocation_site_loc state in |
| 600 |
Dumpglob.dump_moddef ~loc mp "modtype"; |
| 601 |
(state, None) |
| 602 |
| EndModule mp -> |
| 603 |
let mp1 = Declaremods.Interp.end_module () in |
| 604 |
assert(ModPath.equal mp mp1); |
| 605 |
(Rocq_elpi_HOAS.grab_global_env_drop_sigma state, Some mp) |
| 606 |
| EndModuleType mp -> |
| 607 |
let mp1 = Declaremods.Interp.end_modtype () in |
| 608 |
assert(ModPath.equal mp mp1); |
| 609 |
(Rocq_elpi_HOAS.grab_global_env_drop_sigma state, Some mp) |
| 610 |
| BeginSection name -> |
| 611 |
let id = Id.of_string name in |
| 612 |
let lid = CAst.make ~loc:(to_coq_loc @@ State.get invocation_site_loc state) id in |
| 613 |
Dumpglob.dump_definition lid true "sec"; |
| 614 |
Lib.Interp.open_section id; |
| 615 |
(state, None) |
| 616 |
| EndSection -> |
| 617 |
let loc = to_coq_loc @@ State.get invocation_site_loc state in |
| 618 |
Dumpglob.dump_reference ~loc |
| 619 |
(DirPath.to_string (Lib.current_dirpath true)) "<>" "sec"; |
| 620 |
interp_close_section (); |
| 621 |
(Rocq_elpi_HOAS.grab_global_env_drop_sigma state, None) |
| 622 |
| ImportModule mp -> |
| 623 |
Declaremods.Interp.import_module ~export:Lib.Import Libobject.unfiltered mp; |
| 624 |
(state, None) |
| 625 |
| ExportModule mp -> |
| 626 |
Declaremods.Interp.import_module ~export:Lib.Export Libobject.unfiltered mp; |
| 627 |
(state, None) |
| 628 |
| IncludeModule(_,me) -> |
| 629 |
Declaremods.Interp.declare_include me; |
| 630 |
(state, None) |
| 631 |
| IncludeModuleType (_,me) -> |
| 632 |
Declaremods.Interp.declare_include me; |
| 633 |
(state, None) |
| 634 |
| ApplyModule ((name,_,_,_,_),params,mexpr_ast,ty) -> |
| 635 |
if Global.sections_are_opened () then |
| 636 |
err Pp.(str"This elpi code cannot be run within a section since it defines a module"); |
| 637 |
let id = Id.of_string name in |
| 638 |
let mp = Declaremods.Interp.declare_module id params ty mexpr_ast in |
| 639 |
let loc = to_coq_loc @@ State.get invocation_site_loc state in |
| 640 |
Dumpglob.dump_moddef ~loc mp "mod"; |
| 641 |
(state, Some mp) |
| 642 |
| ApplyModuleType ((name,_,_,_),params,mexpr_ast1,mexpr_ast2) -> |
| 643 |
if Global.sections_are_opened () then |
| 644 |
err Pp.(str"This elpi code cannot be run within a section since it defines a module"); |
| 645 |
let id = Id.of_string name in |
| 646 |
let mp = Declaremods.Interp.declare_modtype id params mexpr_ast1 mexpr_ast2 in |
| 647 |
let loc = to_coq_loc @@ State.get invocation_site_loc state in |
| 648 |
Dumpglob.dump_moddef ~loc mp "modtype"; |
| 649 |
(state, Some mp) |
| 650 |
|
| 651 |
let replay_group state name = |
| 652 |
let rz = State.get read state in |
| 653 |
let (group, rz) = |
| 654 |
match RZipper.pop_group rz name with |
| 655 |
| Ok res -> res |
| 656 |
| Error `Empty -> |
| 657 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to replay an action group (" ^ name ^ ") in the interp phase but there are no remaining synterp actions or groups."))) |
| 658 |
| Error (`LeadingAction a) -> |
| 659 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to replay an action group (" ^ name ^ ") in the interp phase but the next synterp action (") ++ pp a ++ strbrk ("is an action and not a group."))) |
| 660 |
| Error (`GroupMismatch name') -> |
| 661 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to replay an action group (" ^ name ^ ") in the interp phase but the next action group is named " ^ Tree.group_name name' ^ "."))) |
| 662 |
in |
| 663 |
let state = State.set read state rz in |
| 664 |
List.fold_left (fun state {action; resulting_state} -> |
| 665 |
Vernacstate.Synterp.unfreeze resulting_state; |
| 666 |
fst (replay1 action state) |
| 667 |
) state group |
| 668 |
|
| 669 |
let wrong_synterp_action x a = |
| 670 |
synterp_interp_error Pp.(v 0 (str "At parsing time, the program did perform action:" ++ fnl () ++ |
| 671 |
h (pp_action a) ++ fnl () ++ |
| 672 |
str"while at execution time it tried to perform action:" ++ fnl () ++ |
| 673 |
str x ++ fnl () ++ |
| 674 |
h (strbrk common_err))) |
| 675 |
let check_inconsistent_synterp_action eq ppx ppy x y a = |
| 676 |
if not (eq x y) then |
| 677 |
synterp_interp_error Pp.(v 0 (str "The program did perform action:" ++ fnl () ++ |
| 678 |
h (pp_action a) ++ fnl () ++ |
| 679 |
str"at both parsing and execution time, but on different inputs:" ++ fnl () ++ |
| 680 |
v 0 (str "- " ++ ppx x ++ str " (synterp)" |
| 681 |
++ cut () ++ |
| 682 |
str "- " ++ ppy y ++ str " (interp)") ++ fnl () ++ |
| 683 |
h (strbrk common_err))) |
| 684 |
|
| 685 |
let check_inconsistent_synterp_action_string = |
| 686 |
check_inconsistent_synterp_action (=) Pp.str Pp.str |
| 687 |
let check_inconsistent_synterp_action_modpath = |
| 688 |
check_inconsistent_synterp_action ModPath.equal (fun x -> Pp.str @@ ModPath.to_string x) (fun x -> Pp.str @@ ModPath.to_string x) |
| 689 |
|
| 690 |
let eqU f x = function |
| 691 |
| B.Given y -> f x y |
| 692 |
| B.Unspec -> true |
| 693 |
|
| 694 |
let check_inconsistent_synterp_action_applym = |
| 695 |
let eq (n1,t1,f1,a1,i1) (n2,t2,f2,a2,i2) = |
| 696 |
n1 = n2 && |
| 697 |
eqU (Option.equal ModPath.equal) t1 t2 && |
| 698 |
eqU ModPath.equal f1 f2 && |
| 699 |
eqU (CList.equal ModPath.equal) a1 a2 && |
| 700 |
eqU (=) i1 i2 |
| 701 |
in |
| 702 |
let ppx (n,t,f,a,i) = Pp.str n in |
| 703 |
let ppy (n,t,f,a,i) = Pp.str n in |
| 704 |
check_inconsistent_synterp_action eq ppx ppy |
| 705 |
|
| 706 |
let check_inconsistent_synterp_action_applymt = |
| 707 |
let eq (n1,f1,a1,i1) (n2,f2,a2,i2) = |
| 708 |
n1 = n2 && |
| 709 |
eqU ModPath.equal f1 f2 && |
| 710 |
eqU (CList.equal ModPath.equal) a1 a2 && |
| 711 |
eqU (=) i1 i2 |
| 712 |
in |
| 713 |
let ppx (n,f,a,i) = Pp.str n in |
| 714 |
let ppy (n,f,a,i) = Pp.str n in |
| 715 |
check_inconsistent_synterp_action eq ppx ppy |
| 716 |
|
| 717 |
let check_inconsistent_synterp_action_m = |
| 718 |
let eq (n1,t1,a1) (n2,t2,a2) = |
| 719 |
n1 = n2 && |
| 720 |
eqU (Option.equal ModPath.equal) t1 t2 && |
| 721 |
eqU (CList.equal (fun (x1,y1) (x2,y2) -> x1 = x2 && ModPath.equal y1 y2)) a1 a2 |
| 722 |
in |
| 723 |
let ppx (n,t,a) = Pp.str n in |
| 724 |
let ppy (n,t,a) = Pp.str n in |
| 725 |
check_inconsistent_synterp_action eq ppx ppy |
| 726 |
|
| 727 |
let check_inconsistent_synterp_action_mt = |
| 728 |
let eq (n1,a1) (n2,a2) = |
| 729 |
n1 = n2 && |
| 730 |
eqU (CList.equal (fun (x1,y1) (x2,y2) -> x1 = x2 && ModPath.equal y1 y2)) a1 a2 |
| 731 |
in |
| 732 |
let ppx (n,a) = Pp.str n in |
| 733 |
let ppy (n,a) = Pp.str n in |
| 734 |
check_inconsistent_synterp_action eq ppx ppy |
| 735 |
|
| 736 |
let check_inconsistent_synterp_action_includem = |
| 737 |
let eq (n1,t1) (n2,t2) = |
| 738 |
ModPath.equal n1 n2 && |
| 739 |
eqU (=) t1 t2 |
| 740 |
in |
| 741 |
let ppx (n,a) = Pp.str @@ ModPath.to_string n in |
| 742 |
let ppy (n,a) = Pp.str @@ ModPath.to_string n in |
| 743 |
check_inconsistent_synterp_action eq ppx ppy |
| 744 |
|
| 745 |
let pop_BeginModule (id,_,_ as x) state = |
| 746 |
let case = Printf.sprintf "begin-module \"%s\"" id in |
| 747 |
let state, a = pop case state in |
| 748 |
match a with |
| 749 |
| BeginModule(x',_,_) -> check_inconsistent_synterp_action_m x' x a; replay1 a state |
| 750 |
| _ -> wrong_synterp_action case a |
| 751 |
let pop_BeginModuleType (id, _ as x') state = |
| 752 |
let case = Printf.sprintf "begin-module-type \"%s\"" id in |
| 753 |
let state, a = pop case state in |
| 754 |
match a with |
| 755 |
| BeginModuleType(x,_,_) -> check_inconsistent_synterp_action_mt x x' a; replay1 a state |
| 756 |
| _ -> wrong_synterp_action case a |
| 757 |
let pop_BeginSection x' state = |
| 758 |
let case = Printf.sprintf "begin-section \"%s\"" x' in |
| 759 |
let state, a = pop case state in |
| 760 |
match a with |
| 761 |
| BeginSection x -> check_inconsistent_synterp_action_string x x' a; replay1 a state |
| 762 |
| _ -> wrong_synterp_action case a |
| 763 |
let pop_EndModule () state = |
| 764 |
let case = "end-module" in |
| 765 |
let state, a = pop case state in |
| 766 |
match a with |
| 767 |
| EndModule _ -> replay1 a state |
| 768 |
| _ -> wrong_synterp_action case a |
| 769 |
let pop_EndModuleType () state = |
| 770 |
let case = "end-module-type" in |
| 771 |
let state, a = pop case state in |
| 772 |
match a with |
| 773 |
| EndModuleType _ -> replay1 a state |
| 774 |
| _ -> wrong_synterp_action case a |
| 775 |
let pop_EndSection () state = |
| 776 |
let case = "end-section" in |
| 777 |
let state, a = pop case state in |
| 778 |
match a with |
| 779 |
| EndSection -> replay1 a state |
| 780 |
| _ -> wrong_synterp_action case a |
| 781 |
let pop_ApplyModule a' state = |
| 782 |
let case = "apply-module" in |
| 783 |
let state, ac = pop case state in |
| 784 |
match ac with |
| 785 |
| ApplyModule (a,_,_,_) -> check_inconsistent_synterp_action_applym a a' ac; replay1 ac state |
| 786 |
| _ -> wrong_synterp_action case ac |
| 787 |
let pop_ApplyModuleType a' state = |
| 788 |
let case = "apply-module-type" in |
| 789 |
let state, ac = pop case state in |
| 790 |
match ac with |
| 791 |
| ApplyModuleType (a,_,_,_) -> check_inconsistent_synterp_action_applymt a a' ac; replay1 ac state |
| 792 |
| _ -> wrong_synterp_action case ac |
| 793 |
let pop_IncludeModule a' state = |
| 794 |
let case = "include-module" in |
| 795 |
let state, ac = pop case state in |
| 796 |
match ac with |
| 797 |
| IncludeModule (a,_) -> check_inconsistent_synterp_action_includem a a' ac; replay1 ac state |
| 798 |
| _ -> wrong_synterp_action case ac |
| 799 |
let pop_IncludeModuleType a' state = |
| 800 |
let case = "include-module-type" in |
| 801 |
let state, ac = pop case state in |
| 802 |
match ac with |
| 803 |
| IncludeModuleType (a,_) -> check_inconsistent_synterp_action_includem a a' ac; replay1 ac state |
| 804 |
| _ -> wrong_synterp_action case ac |
| 805 |
|
| 806 |
let pop_ImportModule a' state = |
| 807 |
let case = "import-module" in |
| 808 |
let state, ac = pop case state in |
| 809 |
match ac with |
| 810 |
| ImportModule a -> check_inconsistent_synterp_action_modpath a a' ac; replay1 ac state |
| 811 |
| _ -> wrong_synterp_action case ac |
| 812 |
let pop_ExportModule a' state = |
| 813 |
let case = "export-module" in |
| 814 |
let state, ac = pop case state in |
| 815 |
match ac with |
| 816 |
| ExportModule a -> check_inconsistent_synterp_action_modpath a a' ac; replay1 ac state |
| 817 |
| _ -> wrong_synterp_action case ac |
| 818 |
|
| 819 |
|
| 820 |
let builtins_interp = |
| 821 |
let open API.BuiltIn in |
| 822 |
let open Pred in |
| 823 |
let open Notation in |
| 824 |
let open CConv in |
| 825 |
|
| 826 |
[ |
| 827 |
|
| 828 |
LPDoc "-- Synterp ----------------------------------------------------------"; |
| 829 |
|
| 830 |
MLData action; |
| 831 |
MLData Tree.group; |
| 832 |
|
| 833 |
MLCode (Pred("coq.next-synterp-action", |
| 834 |
Out(action,"A", |
| 835 |
Read(unit_ctx,"Get the next action performed during parsing (aka synterp), that is also the next action to be performed during execution (aka interp). See also coq.replay-synterp-action")), |
| 836 |
(fun _ ~depth _ _ state -> !: ( |
| 837 |
match RZipper.pop_action (State.get read state) with |
| 838 |
| Ok ({ action }, _) -> action |
| 839 |
| _ -> raise No_clause |
| 840 |
))), |
| 841 |
DocAbove); |
| 842 |
|
| 843 |
MLCode(Pred("coq.replay-synterp-action-group", |
| 844 |
In(id, "ID", |
| 845 |
Full(unit_ctx,"Execute all actions of synterp action group ID. ID must be the name of the next group, it must not be opened already, and there must not be any actions before it.")), |
| 846 |
(fun name ~depth _ _ state -> |
| 847 |
let state = replay_group state name in |
| 848 |
state, (), [])), |
| 849 |
DocAbove); |
| 850 |
|
| 851 |
MLCode(Pred("coq.begin-synterp-group", |
| 852 |
In(id, "ID", |
| 853 |
Out(Tree.group, "Group", |
| 854 |
Full(unit_ctx,"Match a begin-synterp-group synterp operation. ID must be the name of the next synterp action group and there must not be any actions before it."))), |
| 855 |
(fun name _ ~depth _ _ state -> |
| 856 |
let (s, k) = |
| 857 |
State.update_return read state (fun rz -> |
| 858 |
match RZipper.open_group rz name with |
| 859 |
| Ok rz -> rz |
| 860 |
| Error `Empty -> |
| 861 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to match a begin-synterp-group operation on group " ^ name ^ " but there are no groups or actions left in the current state."))) |
| 862 |
| Error (`GroupMismatch name') -> |
| 863 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to match a begin-synterp-group operation on group " ^ name ^ " but the next group to match has name " ^ Tree.group_name name' ^ "."))) |
| 864 |
| Error (`LeadingAction a) -> |
| 865 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to match a begin-synterp-group operation on group " ^ name ^ " but the next item to match is an action (") ++ pp a ++ strbrk "), not a group.")) |
| 866 |
) |
| 867 |
in s, ((), Some k), [])), |
| 868 |
DocAbove); |
| 869 |
|
| 870 |
MLCode(Pred("coq.end-synterp-group", |
| 871 |
In(Tree.group, "Group", |
| 872 |
Full(unit_ctx,"Match a end-synterp-group synterp operation. Group must be the currently opened synterp action group and the group must not have any more synterp actions or groups left to replay.")), |
| 873 |
(fun name ~depth _ _ state -> |
| 874 |
State.update read state (fun rz -> |
| 875 |
match RZipper.close_group rz name with |
| 876 |
| Ok wz -> wz |
| 877 |
| Error `NoGroup -> |
| 878 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to match an end-synterp-group operation on group " ^ Tree.group_name name ^ " but there is no group to end."))) |
| 879 |
| Error (`GroupMismatch name') -> |
| 880 |
synterp_interp_error Pp.(hov 0 (strbrk ("The command tried to match an end-synterp-group operation on group " ^ Tree.group_name name ^ " but the next group to close has name " ^ Tree.group_name name' ^ "."))) |
| 881 |
), (), [])), |
| 882 |
DocAbove); |
| 883 |
|
| 884 |
] |
| 885 |
|
| 886 |
end |
| 887 |
|
| 888 |
let rec dirpath_of_modpath = function |
| 889 |
| ModPath.MPfile d -> DirPath.repr d |
| 890 |
| ModPath.MPdot(mp,l) -> Label.to_id l :: dirpath_of_modpath mp |
| 891 |
| _ -> assert false |
| 892 |
|
| 893 |
let module_ast_of_modpath x = |
| 894 |
let open Libnames in |
| 895 |
qualid_of_dirpath (DirPath.make (dirpath_of_modpath x)) |
| 896 |
|
| 897 |
let module_ast_of_modtypath x = |
| 898 |
let open Constrexpr in let open Libnames in |
| 899 |
CAst.make @@ CMident (qualid_of_dirpath (DirPath.make (dirpath_of_modpath x))), |
| 900 |
Declaremods.DefaultInline |
| 901 |
|
| 902 |
let attribute_decl a = let open API.AlgebraicData in Decl { |
| 903 |
ty = Conv.TyName "attribute"; |
| 904 |
doc = "Generic attribute"; |
| 905 |
pp = (fun fmt a -> Format.fprintf fmt "TODO"); |
| 906 |
constructors = [ |
| 907 |
K("attribute","",A(B.string,A(a,N)), |
| 908 |
B (fun s a -> s,a), |
| 909 |
M (fun ~ok ~ko -> function (s,a) -> ok s a)); |
| 910 |
] |
| 911 |
} |
| 912 |
let attribute_alloc = let open API.AlgebraicData in |
| 913 |
allocate_constructors (Param (fun a -> attribute_decl a)) |
| 914 |
|
| 915 |
let attribute a = let open API.AlgebraicData in declare_allocated attribute_alloc (attribute_decl a) |> CConv.(!<) |
| 916 |
|
| 917 |
type attribute_data = |
| 918 |
| AttributeString of string |
| 919 |
| AttributeLoc of API.Ast.Loc.t |
| 920 |
type attribute_value = |
| 921 |
| AttributeEmpty |
| 922 |
| AttributeList of (string * attribute_value) list |
| 923 |
| AttributeLeaf of attribute_data |
| 924 |
|
| 925 |
let attribute_value = let open API.AlgebraicData in let open CConv in declare { |
| 926 |
ty = Conv.TyName "attribute-value"; |
| 927 |
doc = "Generic attribute value"; |
| 928 |
pp = (fun fmt a -> Format.fprintf fmt "TODO"); |
| 929 |
constructors = [ |
| 930 |
K("leaf-str","",A(B.string,N), |
| 931 |
B (fun s -> |
| 932 |
if s = "" then AttributeEmpty |
| 933 |
else AttributeLeaf (AttributeString s)), |
| 934 |
M (fun ~ok ~ko -> function |
| 935 |
| AttributeEmpty -> ok "" |
| 936 |
| AttributeLeaf (AttributeString x) -> ok x |
| 937 |
| _ -> ko ())); |
| 938 |
K("leaf-loc","",A(B.loc,N), |
| 939 |
B (fun s -> |
| 940 |
AttributeLeaf (AttributeLoc s)), |
| 941 |
M (fun ~ok ~ko -> function |
| 942 |
| AttributeLeaf (AttributeLoc x) -> ok x |
| 943 |
| _ -> ko ())); |
| 944 |
K("node","",C((fun self -> !> (B.list (attribute (!< self)))),N), |
| 945 |
B (fun l -> AttributeList l), |
| 946 |
M (fun ~ok ~ko -> function AttributeList l -> ok l | _ -> ko ()) |
| 947 |
) |
| 948 |
] |
| 949 |
} |> CConv.(!<) |
| 950 |
|
| 951 |
let attribute = attribute attribute_value |
| 952 |
|
| 953 |
let synterp_close_section = Declaremods.Synterp.close_section |
| 954 |
|
| 955 |
let coq_synterp_builtins = |
| 956 |
let open API.BuiltIn in |
| 957 |
let open Pred in |
| 958 |
let open Notation in |
| 959 |
let open CConv in |
| 960 |
[ |
| 961 |
LPCode Rocq_elpi_builtins_arg_HOAS.code; |
| 962 |
LPDoc "Coq terms are not visible at synterp time, they are always holes"; |
| 963 |
LPCode "kind term type."; |
| 964 |
LPCode "kind sealed-goal type."; |
| 965 |
LPCode "kind gref type."; |
| 966 |
LPCode "kind abbreviation type."; |
| 967 |
LPDoc "-- Parsing time APIs ----------------------------------------------------"; |
| 968 |
MLData id; |
| 969 |
MLData modpath; |
| 970 |
MLData modtypath; |
| 971 |
locate_module; |
| 972 |
locate_module_type; |
| 973 |
MLData located; |
| 974 |
MLCode(Pred("coq.locate-all", |
| 975 |
In(id, "Name", |
| 976 |
Out(B.list located, "Located", |
| 977 |
Easy {|finds all possible meanings of a string. Does not fail.|})), |
| 978 |
(fun s _ ~depth -> |
| 979 |
let qualid = Libnames.qualid_of_string s in |
| 980 |
let l = ref [] in |
| 981 |
let add x = l := !l @ [x] in |
| 982 |
begin |
| 983 |
try add @@ LocModule (Nametab.locate_module qualid) |
| 984 |
with Not_found -> () |
| 985 |
end; |
| 986 |
begin |
| 987 |
try add @@ LocModuleType (Nametab.locate_modtype qualid) |
| 988 |
with Not_found -> () |
| 989 |
end; |
| 990 |
!: !l)), |
| 991 |
DocAbove); |
| 992 |
|
| 993 |
MLData module_inline_default; |
| 994 |
|
| 995 |
|
| 996 |
MLCode(Pred("coq.env.begin-module-functor", |
| 997 |
In(id, "The name of the functor", |
| 998 |
In(option modtypath, "Its module type", |
| 999 |
In(list (pair id modtypath), "Parameters of the functor", |
| 1000 |
Full(unit_ctx, "Starts a functor *E*")))), |
| 1001 |
(fun name mp binders ~depth _ _ state -> |
| 1002 |
if Lib.sections_are_opened () then |
| 1003 |
err Pp.(str"This code cannot be run within a section since it opens a module"); |
| 1004 |
let ty = |
| 1005 |
match mp with |
| 1006 |
| None -> Declaremods.Check [] |
| 1007 |
| Some mp -> Declaremods.(Enforce (module_ast_of_modtypath mp)) in |
| 1008 |
let id = Id.of_string name in |
| 1009 |
let binders_ast = |
| 1010 |
List.map (fun (id, mty) -> |
| 1011 |
[CAst.make (Id.of_string id)], (module_ast_of_modtypath mty)) |
| 1012 |
binders in |
| 1013 |
let _, x, y = Declaremods.Synterp.start_module None id binders_ast ty in |
| 1014 |
let state = SynterpAction.(push (BeginModule((name,mp,binders), x, y))) state in |
| 1015 |
|
| 1016 |
state, (), [])), |
| 1017 |
DocNext); |
| 1018 |
|
| 1019 |
LPCode {| |
| 1020 |
func coq.env.begin-module id, option modtypath -> . |
| 1021 |
coq.env.begin-module Name MP :- |
| 1022 |
coq.env.begin-module-functor Name MP []. |
| 1023 |
|}; |
| 1024 |
|
| 1025 |
|
| 1026 |
MLCode(Pred("coq.env.end-module", |
| 1027 |
Out(modpath, "ModPath", |
| 1028 |
Full(unit_ctx, "end the current module that becomes known as ModPath *E*")), |
| 1029 |
(fun _ ~depth _ _ state -> |
| 1030 |
let mp0 = Lib.current_mp () in |
| 1031 |
let mp = Declaremods.Synterp.end_module () in |
| 1032 |
let state = SynterpAction.(push (EndModule mp)) state in |
| 1033 |
assert(ModPath.equal mp0 mp); |
| 1034 |
state, !: mp, [])), |
| 1035 |
DocAbove); |
| 1036 |
|
| 1037 |
|
| 1038 |
MLCode(Pred("coq.env.begin-module-type-functor", |
| 1039 |
In(id, "The name of the functor", |
| 1040 |
In(list (pair id modtypath), "The parameters of the functor", |
| 1041 |
Full(unit_ctx,"Starts a module type functor *E*"))), |
| 1042 |
(fun name binders ~depth _ _ state -> |
| 1043 |
if Lib.sections_are_opened () then |
| 1044 |
err Pp.(str"This code cannot be run within a section since it opens a module"); |
| 1045 |
let id = Id.of_string name in |
| 1046 |
let binders_ast = |
| 1047 |
List.map (fun (id, mty) -> |
| 1048 |
[CAst.make (Id.of_string id)], (module_ast_of_modtypath mty)) |
| 1049 |
binders in |
| 1050 |
let _,y,z = Declaremods.Synterp.start_modtype id binders_ast [] in |
| 1051 |
let state = SynterpAction.(push (BeginModuleType((name,binders),y,z))) state in |
| 1052 |
|
| 1053 |
state, (), [])), |
| 1054 |
DocNext); |
| 1055 |
|
| 1056 |
LPCode {| |
| 1057 |
func coq.env.begin-module-type id ->. |
| 1058 |
coq.env.begin-module-type Name :- |
| 1059 |
coq.env.begin-module-type-functor Name []. |
| 1060 |
|}; |
| 1061 |
|
| 1062 |
|
| 1063 |
MLCode(Pred("coq.env.end-module-type", |
| 1064 |
Out(modtypath, "ModTyPath", |
| 1065 |
Full(unit_ctx, "end the current module type that becomes known as ModPath *E*")), |
| 1066 |
(fun _ ~depth _ _ state -> |
| 1067 |
let mp0 = Lib.current_mp () in |
| 1068 |
let _mp = Declaremods.Synterp.end_modtype () in |
| 1069 |
|
| 1070 |
let state = SynterpAction.(push (EndModuleType(mp0))) state in |
| 1071 |
state, !: mp0, [])), |
| 1072 |
DocAbove); |
| 1073 |
|
| 1074 |
MLCode(Pred("coq.env.apply-module-functor", |
| 1075 |
In(id, "The name of the new module", |
| 1076 |
In(option modtypath, "Its module type", |
| 1077 |
In(modpath, "The functor being applied", |
| 1078 |
In(list modpath, "Its arguments", |
| 1079 |
In(module_inline_default, "Arguments inlining", |
| 1080 |
Out(modpath, "The modpath of the new module", |
| 1081 |
Full(unit_ctx, "Applies a functor *E*"))))))), |
| 1082 |
(fun name mp f arguments inline _ ~depth _ _ state -> |
| 1083 |
let ty = |
| 1084 |
match mp with |
| 1085 |
| None -> Declaremods.Check [] |
| 1086 |
| Some mp -> Declaremods.(Enforce (module_ast_of_modtypath mp)) in |
| 1087 |
let id = Id.of_string name in |
| 1088 |
let fa = CAst.make (Constrexpr.CMident (module_ast_of_modpath f)) in |
| 1089 |
let mexpr_ast_args = List.map module_ast_of_modpath arguments in |
| 1090 |
let mexpr_ast = |
| 1091 |
List.fold_left (fun hd arg -> CAst.make (Constrexpr.CMapply(hd,arg))) fa mexpr_ast_args in |
| 1092 |
let mp1, a,b,c = Declaremods.Synterp.declare_module id [] ty [mexpr_ast,inline] in |
| 1093 |
let state = SynterpAction.(push (ApplyModule((name,mp,f,arguments,inline),a,b,c))) state in |
| 1094 |
state, !: mp1, [])), |
| 1095 |
DocNext); |
| 1096 |
|
| 1097 |
MLCode(Pred("coq.env.apply-module-type-functor", |
| 1098 |
In(id, "The name of the new module type", |
| 1099 |
In(modtypath, "The functor", |
| 1100 |
In(list modpath, "Its arguments", |
| 1101 |
In(module_inline_default, "Arguments inlining", |
| 1102 |
Out(modtypath, "The modtypath of the new module type", |
| 1103 |
Full(unit_ctx, "Applies a type functor *E*")))))), |
| 1104 |
(fun name f arguments inline _ ~depth _ _ state -> |
| 1105 |
let id = Id.of_string name in |
| 1106 |
let fa,_ = module_ast_of_modtypath f in |
| 1107 |
let mexpr_ast_args = List.map module_ast_of_modpath arguments in |
| 1108 |
let mexpr_ast = |
| 1109 |
List.fold_left (fun hd arg -> CAst.make (Constrexpr.CMapply(hd,arg))) fa mexpr_ast_args in |
| 1110 |
let mp, a,b,c = Declaremods.Synterp.declare_modtype id [] [] [mexpr_ast,inline] in |
| 1111 |
let state = SynterpAction.(push (ApplyModuleType((name,f,arguments,inline),a,c,b ))) state in |
| 1112 |
state, !: mp, [])), |
| 1113 |
DocNext); |
| 1114 |
|
| 1115 |
|
| 1116 |
MLCode(Pred("coq.env.include-module", |
| 1117 |
In(modpath, "ModPath", |
| 1118 |
In(module_inline_default, "Inline", |
| 1119 |
Full(unit_ctx, "is like the vernacular Include, Inline can be omitted *E*"))), |
| 1120 |
(fun mp inline ~depth _ _ state -> |
| 1121 |
let fpath = match mp with |
| 1122 |
| ModPath.MPdot(mp,l) -> |
| 1123 |
Libnames.make_path (ModPath.dp mp) (Label.to_id l) |
| 1124 |
| _ -> nYI "functors" in |
| 1125 |
let tname = Constrexpr.CMident (Libnames.qualid_of_path fpath) in |
| 1126 |
let i = CAst.make tname, inline in |
| 1127 |
let me = Declaremods.Synterp.declare_include [i] in |
| 1128 |
let state = SynterpAction.(push (IncludeModule ((mp,inline),me))) state in |
| 1129 |
state, (), [])), |
| 1130 |
DocAbove); |
| 1131 |
|
| 1132 |
|
| 1133 |
MLCode(Pred("coq.env.include-module-type", |
| 1134 |
In(modtypath, "ModTyPath", |
| 1135 |
In(module_inline_default, "Inline", |
| 1136 |
Full(unit_ctx, "is like the vernacular Include Type, Inline can be omitted *E*"))), |
| 1137 |
(fun mp inline ~depth _ _ state -> |
| 1138 |
let fpath = Nametab.path_of_modtype mp in |
| 1139 |
let tname = Constrexpr.CMident (Libnames.qualid_of_path fpath) in |
| 1140 |
let i = CAst.make tname, inline in |
| 1141 |
let me = Declaremods.Synterp.declare_include [i] in |
| 1142 |
let state = SynterpAction.(push (IncludeModuleType ((mp,inline),me))) state in |
| 1143 |
state, (), [])), |
| 1144 |
DocAbove); |
| 1145 |
|
| 1146 |
MLCode(Pred("coq.env.import-module", |
| 1147 |
In(modpath, "ModPath", |
| 1148 |
Full(unit_ctx, "is like the vernacular Import *E*")), |
| 1149 |
(fun mp ~depth _ _ state -> |
| 1150 |
Declaremods.Synterp.import_module ~export:Lib.Import Libobject.unfiltered mp; |
| 1151 |
let state = SynterpAction.(push (ImportModule mp)) state in |
| 1152 |
state, (), [])), |
| 1153 |
DocAbove); |
| 1154 |
|
| 1155 |
MLCode(Pred("coq.env.export-module", |
| 1156 |
In(modpath, "ModPath", |
| 1157 |
Full(unit_ctx, "is like the vernacular Export *E*")), |
| 1158 |
(fun mp ~depth _ _ state -> |
| 1159 |
Declaremods.Synterp.import_module ~export:Lib.Export Libobject.unfiltered mp; |
| 1160 |
let state = SynterpAction.(push (ExportModule mp)) state in |
| 1161 |
state, (), [])), |
| 1162 |
DocAbove); |
| 1163 |
|
| 1164 |
MLCode(Pred("coq.env.begin-section", |
| 1165 |
In(id, "Name", |
| 1166 |
Full(unit_ctx, "starts a section named Name *E*")), |
| 1167 |
(fun name ~depth _ _ state -> |
| 1168 |
let id = Id.of_string name in |
| 1169 |
Lib.Synterp.open_section id; |
| 1170 |
let state = SynterpAction.(push (BeginSection name)) state in |
| 1171 |
state, (), [])), |
| 1172 |
DocAbove); |
| 1173 |
|
| 1174 |
MLCode(Pred("coq.env.end-section", |
| 1175 |
Full(unit_ctx, "end the current section *E*"), |
| 1176 |
(fun ~depth _ _ state -> |
| 1177 |
synterp_close_section (); |
| 1178 |
let state = SynterpAction.(push EndSection) state in |
| 1179 |
state, (), [])), |
| 1180 |
DocAbove); |
| 1181 |
|
| 1182 |
modpath_to_path; modtypath_to_path; modpath_to_library; modtypath_to_library; |
| 1183 |
current_path; current_section_path; |
| 1184 |
|
| 1185 |
MLData clause; |
| 1186 |
MLData grafting; |
| 1187 |
MLData scope; |
| 1188 |
|
| 1189 |
LPCode {| |
| 1190 |
% see coq.elpi.accumulate-clauses |
| 1191 |
func coq.elpi.accumulate scope, id, clause ->. |
| 1192 |
coq.elpi.accumulate S N C :- coq.elpi.accumulate-clauses S N [C]. |
| 1193 |
|}; |
| 1194 |
|
| 1195 |
MLCode(Pred("coq.elpi.accumulate-clauses", |
| 1196 |
In(B.unspec scope, "Scope", |
| 1197 |
In(id, "DbName", |
| 1198 |
In(B.list clause, "Clauses", |
| 1199 |
Full (options , {| |
| 1200 |
Declare that, once the program is over, the given clauses has to be |
| 1201 |
added to the given db (see Elpi Db). |
| 1202 |
Clauses usually belong to Coq modules: the Scope argument lets one |
| 1203 |
select which module: |
| 1204 |
- execution site (default) is the module in which the pogram is |
| 1205 |
invoked |
| 1206 |
- current is the module currently being constructed (see |
| 1207 |
begin/end-module) |
| 1208 |
- library is the current file (the module that is named after the file) |
| 1209 |
The clauses are visible as soon as the enclosing module is Imported. |
| 1210 |
Clauses cannot be accumulated inside functors. |
| 1211 |
Supported attributes: |
| 1212 |
- @local! (default: false, discard at the end of section or module) |
| 1213 |
- @global! (default: false, always active, only if Scope is execution-site, discouraged)|} )))), |
| 1214 |
(fun scope dbname clauses ~depth options _ state -> |
| 1215 |
accumulate_clauses |
| 1216 |
~clauses_for_later:clauses_for_later_synterp |
| 1217 |
~accumulate_to_db:(get_accumulate_to_db_synterp()) |
| 1218 |
~preprocess_clause:(fun ~depth x -> [], x) |
| 1219 |
~scope ~dbname clauses ~depth ~options state)), |
| 1220 |
DocAbove); |
| 1221 |
|
| 1222 |
] @ SynterpAction.get_parsing_actions_synterp @ [ |
| 1223 |
MLData attribute_value; |
| 1224 |
MLData attribute; |
| 1225 |
] |
| 1226 |
|
| 1227 |
let synterp_api_doc = ". bla bla" |
| 1228 |
|