| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
module API = Elpi.API |
| 6 |
module E = API.RawData |
| 7 |
module CD = API.RawOpaqueData |
| 8 |
|
| 9 |
open Rocq_elpi_utils |
| 10 |
open Rocq_elpi_HOAS |
| 11 |
open Names |
| 12 |
open Ltac_plugin.Tacinterp |
| 13 |
|
| 14 |
type phase = Interp | Synterp | Both |
| 15 |
type proof = |
| 16 |
| Begin of string option |
| 17 |
| End |
| 18 |
|
| 19 |
let push_name x = function |
| 20 |
| Names.Name.Name id -> |
| 21 |
let decl = Context.Named.Declaration.LocalAssum (Context.make_annot id Sorts.Relevant, Constr.mkProp) in |
| 22 |
{ x with Genintern.genv = push_named decl x.Genintern.genv } |
| 23 |
| _ -> x |
| 24 |
|
| 25 |
let push_gdecl (name,_,_,_,_) x = push_name x name |
| 26 |
|
| 27 |
let push_glob_ctx glob_ctx x = |
| 28 |
List.fold_right push_gdecl glob_ctx x |
| 29 |
|
| 30 |
let push_inductive_in_intern_env intern_env name params arity user_impls = |
| 31 |
let env = Global.env () in |
| 32 |
let sigma = Evd.from_env env in |
| 33 |
let sigma, ty = Pretyping.understand_tcc env sigma ~expected_type:Pretyping.IsType (Rocq_elpi_utils.mk_gforall arity params) in |
| 34 |
ty, Constrintern.compute_internalization_env env sigma ~impls:intern_env |
| 35 |
Constrintern.Inductive [name] [ty] [user_impls] |
| 36 |
|
| 37 |
let intern_tactic_constr = Ltac_plugin.Tacintern.intern_constr |
| 38 |
|
| 39 |
let intern_global_constr { Ltac_plugin.Tacintern.genv = env } ~intern_env t = |
| 40 |
let sigma = Evd.from_env env in |
| 41 |
Constrintern.intern_gen Pretyping.WithoutTypeConstraint env sigma ~impls:intern_env ~pattern_mode:false ~ltacvars:Constrintern.empty_ltac_sign t |
| 42 |
|
| 43 |
let intern_global_constr_ty { Ltac_plugin.Tacintern.genv = env } ~intern_env ?(expty=Pretyping.IsType) t = |
| 44 |
let sigma = Evd.from_env env in |
| 45 |
Constrintern.intern_gen expty env sigma ~impls:intern_env ~pattern_mode:false ~ltacvars:Constrintern.empty_ltac_sign t |
| 46 |
|
| 47 |
let intern_global_context { Ltac_plugin.Tacintern.genv = env } ~intern_env ctx = |
| 48 |
Constrintern.intern_context env ~bound_univs:UnivNames.empty_binders intern_env ctx |
| 49 |
|
| 50 |
let intern_one h = |
| 51 |
let open Constrexpr in |
| 52 |
match h with |
| 53 |
| CLocalAssum(nl,_,Default bk,_) -> nl |> List.map (fun n -> n.CAst.v,None,bk,None,mkGHole) |
| 54 |
| CLocalAssum(nl,_,Generalized(bk,_),_) -> nl |> List.map (fun n -> n.CAst.v,None,bk,None,mkGHole) |
| 55 |
| CLocalDef (n,_,_,None) -> [n.CAst.v,None,Glob_term.Explicit,None,mkGHole] |
| 56 |
| CLocalDef (n,_,_,Some _) -> [n.CAst.v,None,Glob_term.Explicit,Some mkGHole,mkGHole] |
| 57 |
| CLocalPattern _ -> nYI "irrefutable pattern in synterp" |
| 58 |
let drop_relevance (a,_,c,d,e) = (a,c,d,e) |
| 59 |
|
| 60 |
let intern_global_context_synterp (ctx : Constrexpr.local_binder_expr list) : Glob_term.glob_decl list = |
| 61 |
CList.concat_map intern_one ctx |> List.rev |
| 62 |
|
| 63 |
module Cmd = struct |
| 64 |
|
| 65 |
type raw_term = Constrexpr.constr_expr |
| 66 |
type glob_term = Genintern.glob_constr_and_expr |
| 67 |
type top_term = |
| 68 |
Ltac_plugin.Tacinterp.interp_sign * Genintern.glob_constr_and_expr |
| 69 |
|
| 70 |
type raw_record_decl = Vernacentries.Preprocessed_Mind_decl.record |
| 71 |
type glob_record_decl = Genintern.glob_sign * raw_record_decl |
| 72 |
type top_record_decl = interp_sign * glob_record_decl |
| 73 |
|
| 74 |
type raw_indt_decl = Vernacentries.Preprocessed_Mind_decl.inductive |
| 75 |
type glob_indt_decl = Genintern.glob_sign * raw_indt_decl |
| 76 |
type top_indt_decl = interp_sign * glob_indt_decl |
| 77 |
|
| 78 |
type univpoly = Mono | Poly | CumulPoly |
| 79 |
|
| 80 |
[%%if coq = "9.0"] |
| 81 |
type raw_record_decl_elpi = { |
| 82 |
name : qualified_name; |
| 83 |
parameters : Constrexpr.local_binder_expr list; |
| 84 |
sort : Constrexpr.sort_expr option; |
| 85 |
constructor : Names.Id.t option; |
| 86 |
fields : (Vernacexpr.local_decl_expr * Vernacexpr.record_field_attr) list; |
| 87 |
univpoly : univpoly; |
| 88 |
} |
| 89 |
[%%else] |
| 90 |
type raw_record_decl_elpi = { |
| 91 |
name : qualified_name; |
| 92 |
parameters : Constrexpr.local_binder_expr list; |
| 93 |
sort : Constrexpr.sort_expr option; |
| 94 |
constructor : Names.Id.t option; |
| 95 |
fields : (Vernacexpr.local_decl_expr * Record.Data.projection_flags * Vernacexpr.notation_declaration list) list; |
| 96 |
univpoly : univpoly; |
| 97 |
} |
| 98 |
[%%endif] |
| 99 |
type glob_record_decl_elpi = { |
| 100 |
name : string list * Names.Id.t; |
| 101 |
constructorname : Names.Id.t option; |
| 102 |
params : Glob_term.glob_decl list; |
| 103 |
arity : Glob_term.glob_constr; |
| 104 |
fields : (Glob_term.glob_constr * Rocq_elpi_HOAS.record_field_spec) list; |
| 105 |
univpoly : univpoly; |
| 106 |
} |
| 107 |
|
| 108 |
let pr_raw_record_decl _ _ _ = Pp.str "TODO: pr_raw_record_decl" |
| 109 |
let pr_glob_record_decl _ _ _ = Pp.str "TODO: pr_glob_record_decl" |
| 110 |
let pr_top_record_decl _ _ _ = Pp.str "TODO: pr_top_record_decl" |
| 111 |
|
| 112 |
type raw_indt_decl_elpi = { |
| 113 |
finiteness : Declarations.recursivity_kind; |
| 114 |
name : qualified_name; |
| 115 |
parameters : Constrexpr.local_binder_expr list; |
| 116 |
non_uniform_parameters : Constrexpr.local_binder_expr list option; |
| 117 |
arity : Constrexpr.constr_expr option; |
| 118 |
constructors : (Names.lident * Constrexpr.constr_expr) list; |
| 119 |
univpoly : univpoly; |
| 120 |
} |
| 121 |
type glob_indt_decl_elpi = { |
| 122 |
finiteness : Declarations.recursivity_kind; |
| 123 |
name : string list * Names.Id.t; |
| 124 |
arity : Glob_term.glob_constr; |
| 125 |
params : Glob_term.glob_decl list; |
| 126 |
nuparams : Glob_term.glob_decl list; |
| 127 |
nuparams_given : bool; |
| 128 |
constructors : (Names.Id.t * Glob_term.glob_constr) list; |
| 129 |
univpoly : univpoly; |
| 130 |
} |
| 131 |
|
| 132 |
let pr_raw_indt_decl _ _ _ = Pp.str "TODO: pr_raw_indt_decl" |
| 133 |
let pr_glob_indt_decl _ _ _ = Pp.str "TODO: pr_glob_indt_decl" |
| 134 |
let pr_top_indt_decl _ _ _ = Pp.str "TODO: pr_top_indt_decl" |
| 135 |
|
| 136 |
[%%if coq = "9.0" || coq = "9.1"] |
| 137 |
type raw_red_expr = Genredexpr.raw_red_expr |
| 138 |
[%%else] |
| 139 |
type raw_red_expr = Redexpr.raw_red_expr |
| 140 |
[%%endif] |
| 141 |
|
| 142 |
type raw_constant_decl = { |
| 143 |
name : qualified_name; |
| 144 |
atts : Attributes.vernac_flags; |
| 145 |
udecl : Constrexpr.universe_decl_expr option; |
| 146 |
typ : Constrexpr.local_binder_expr list * Constrexpr.constr_expr option; |
| 147 |
body : Constrexpr.constr_expr option; |
| 148 |
red : raw_red_expr option; |
| 149 |
} |
| 150 |
[%%if coq = "9.0" || coq = "9.1"] |
| 151 |
let empty_univ_csts = Univ.Constraints.empty |
| 152 |
let dest_udecl ({ UState.univdecl_instance ; univdecl_extensible_instance; univdecl_extensible_constraints; univdecl_constraints } : UState.universe_decl) = |
| 153 |
univdecl_extensible_instance, univdecl_extensible_constraints, univdecl_constraints, univdecl_instance |
| 154 |
let interp_univ_decl_opt = Constrintern.interp_univ_decl_opt |
| 155 |
[%%else] |
| 156 |
let empty_univ_csts = Univ.UnivConstraints.empty |
| 157 |
let dest_udecl ({ UState.univdecl_instance ; univdecl_extensible_instance; univdecl_extensible_constraints; univdecl_univ_constraints } : UState.universe_decl) = |
| 158 |
univdecl_extensible_instance, univdecl_extensible_constraints, univdecl_univ_constraints, univdecl_instance |
| 159 |
let interp_univ_decl_opt = Constrintern.interp_univ_decl_opt |
| 160 |
[%%endif] |
| 161 |
type glob_constant_decl_elpi = { |
| 162 |
name : string list * Names.Id.t; |
| 163 |
udecl : universe_decl_option; |
| 164 |
params : Glob_term.glob_decl list; |
| 165 |
typ : Glob_term.glob_constr; |
| 166 |
body : Glob_term.glob_constr option; |
| 167 |
} |
| 168 |
type glob_constant_decl = Genintern.glob_sign * raw_constant_decl |
| 169 |
type top_constant_decl = interp_sign * glob_constant_decl |
| 170 |
|
| 171 |
let pr_raw_constant_decl _ _ _ = Pp.str "TODO: pr_raw_constant_decl" |
| 172 |
let pr_glob_constant_decl _ _ _ = Pp.str "TODO: pr_glob_constant_decl" |
| 173 |
let pr_top_constant_decl _ _ _ = Pp.str "TODO: pr_top_constant_decl" |
| 174 |
|
| 175 |
|
| 176 |
type raw_context_decl = Constrexpr.local_binder_expr list |
| 177 |
type glob_context_decl = Genintern.glob_sign * raw_context_decl |
| 178 |
type top_context_decl = interp_sign * glob_context_decl |
| 179 |
|
| 180 |
let pr_raw_context_decl _ _ _ = Pp.str "TODO: pr_raw_context_decl" |
| 181 |
let pr_glob_context_decl _ _ _ = Pp.str "TODO: pr_glob_context_decl" |
| 182 |
let pr_top_context_decl _ _ _ = Pp.str "TODO: pr_top_context_decl" |
| 183 |
|
| 184 |
type ('a,'b,'c,'d,'e) t = |
| 185 |
| Int : int -> ('a,'b,'c,'d,'e) t |
| 186 |
| String : string -> ('a,'b,'c,'d,'e) t |
| 187 |
| Term : 'a -> ('a,'b,'c,'d,'e) t |
| 188 |
| RecordDecl : 'b -> ('a,'b,'c,'d,'e) t |
| 189 |
| IndtDecl : 'c -> ('a,'b,'c,'d,'e) t |
| 190 |
| ConstantDecl : 'd -> ('a,'b,'c,'d,'e) t |
| 191 |
| Context : 'e -> ('a,'b,'c,'d,'e) t |
| 192 |
|
| 193 |
type raw = (raw_term, raw_record_decl, raw_indt_decl, raw_constant_decl, raw_context_decl) t |
| 194 |
type glob = (glob_term, glob_record_decl, glob_indt_decl, glob_constant_decl, glob_context_decl) t |
| 195 |
type top = (top_term, top_record_decl, top_indt_decl, top_constant_decl, top_context_decl) t |
| 196 |
|
| 197 |
let pr_arg f g h i j x = match x with |
| 198 |
| Int n -> Pp.int n |
| 199 |
| String s -> Pp.qstring s |
| 200 |
| Term s -> f s |
| 201 |
| RecordDecl s -> g s |
| 202 |
| IndtDecl s -> h s |
| 203 |
| ConstantDecl s -> i s |
| 204 |
| Context c -> j c |
| 205 |
|
| 206 |
let pp_raw env sigma : raw -> Pp.t = |
| 207 |
pr_arg |
| 208 |
(pr_constr_expr env sigma) |
| 209 |
(pr_raw_record_decl env sigma) |
| 210 |
(pr_raw_indt_decl env sigma) |
| 211 |
(pr_raw_constant_decl env sigma) |
| 212 |
(pr_raw_context_decl env sigma) |
| 213 |
|
| 214 |
let pr_glob_constr_and_expr env sigma = function |
| 215 |
| (_, Some c) -> |
| 216 |
pr_constr_expr env sigma c |
| 217 |
| (c, None) -> |
| 218 |
Printer.pr_glob_constr_env env sigma c |
| 219 |
|
| 220 |
let pp_glob env sigma : glob -> Pp.t = |
| 221 |
pr_arg |
| 222 |
(pr_glob_constr_and_expr env sigma) |
| 223 |
(pr_glob_record_decl env sigma) |
| 224 |
(pr_glob_indt_decl env sigma) |
| 225 |
(pr_glob_constant_decl env sigma) |
| 226 |
(pr_glob_context_decl env sigma) |
| 227 |
|
| 228 |
let pp_top env sigma : top -> Pp.t = |
| 229 |
pr_arg |
| 230 |
(fun (_,x) -> pr_glob_constr_and_expr env sigma x) |
| 231 |
(pr_top_record_decl env sigma) |
| 232 |
(pr_top_indt_decl env sigma) |
| 233 |
(pr_top_constant_decl env sigma) |
| 234 |
(pr_top_context_decl env sigma) |
| 235 |
|
| 236 |
let sep_last_qualid = function |
| 237 |
| [] -> "_", [] |
| 238 |
| l -> CList.sep_last l |
| 239 |
|
| 240 |
let univpoly_of ~poly ~cumulative = |
| 241 |
match poly, cumulative with |
| 242 |
| true, true -> CumulPoly |
| 243 |
| true, false -> Poly |
| 244 |
| false, _ -> Mono |
| 245 |
|
| 246 |
[%%if coq = "9.0" || coq = "9.1"] |
| 247 |
let univpoly_of_flags flags = |
| 248 |
univpoly_of ~poly:flags.ComInductive.poly ~cumulative:flags.ComInductive.cumulative |
| 249 |
[%%else] |
| 250 |
let univpoly_of_flags flags = |
| 251 |
univpoly_of ~poly:(PolyFlags.univ_poly flags.ComInductive.poly) ~cumulative:(PolyFlags.cumulative flags.poly) |
| 252 |
[%%endif] |
| 253 |
|
| 254 |
let of_coq_inductive_definition id = |
| 255 |
let open Vernacentries.Preprocessed_Mind_decl in |
| 256 |
let { flags; udecl; typing_flags; private_ind; uniform; inductives } = id in |
| 257 |
if List.length inductives != 1 then nYI "mutual inductives"; |
| 258 |
let inductive = List.hd inductives in |
| 259 |
let (((name),(parameters,non_uniform_parameters),arity,constructors),notations) = inductive in |
| 260 |
if notations != [] then CErrors.user_err Pp.(str "notations not supported"); |
| 261 |
let name = [Names.Id.to_string name.CAst.v] in |
| 262 |
let constructors = |
| 263 |
List.map (function (Vernacexpr.(_,NoCoercion,NoInstance),c) -> c |
| 264 |
| _ -> CErrors.user_err Pp.(str "coercion and instance flags not supported")) |
| 265 |
constructors in |
| 266 |
let { ComInductive.template; finite } = flags in |
| 267 |
if template <> None then nYI "raw template polymorphic inductives"; |
| 268 |
if udecl <> None then nYI "raw universe polymorphic inductives with universe declaration"; |
| 269 |
{ |
| 270 |
finiteness = finite; |
| 271 |
name; |
| 272 |
parameters; |
| 273 |
non_uniform_parameters; |
| 274 |
arity; |
| 275 |
constructors; |
| 276 |
univpoly = univpoly_of_flags flags |
| 277 |
} |
| 278 |
|
| 279 |
let of_coq_record_definition id = |
| 280 |
let open Vernacentries.Preprocessed_Mind_decl in |
| 281 |
let { flags; udecl; primitive_proj; kind; records; } : record = id in |
| 282 |
if List.length records != 1 then nYI "mutual inductives"; |
| 283 |
let open Record.Ast in |
| 284 |
let { name; is_coercion; binders : Constrexpr.local_binder_expr list; cfs; idbuild; sort; default_inhabitant_id : Names.Id.t option; } = List.hd records in |
| 285 |
if is_coercion = Vernacexpr.AddCoercion then CErrors.user_err Pp.(str "coercion flag not supported"); |
| 286 |
let name = [Names.Id.to_string name.CAst.v] in |
| 287 |
let sort = sort |> Option.map (fun sort -> |
| 288 |
match sort.CAst.v with |
| 289 |
| Constrexpr.CSort s -> s |
| 290 |
| _ -> CErrors.user_err ?loc:sort.CAst.loc Pp.(str "only explicits sorts are supported")) in |
| 291 |
let { ComInductive.template; finite } = flags in |
| 292 |
if template <> None then nYI "raw template polymorphic inductives"; |
| 293 |
if udecl <> None then nYI "raw universe polymorphic inductives with universe declaration"; |
| 294 |
{ |
| 295 |
name; |
| 296 |
parameters = binders; |
| 297 |
sort; |
| 298 |
constructor = Some idbuild.v; |
| 299 |
fields = cfs; |
| 300 |
univpoly = univpoly_of_flags flags |
| 301 |
} |
| 302 |
|
| 303 |
let intern_record_decl glob_sign (it : raw_record_decl) = glob_sign, it |
| 304 |
|
| 305 |
let mkCLocalAssum x y z = Constrexpr.CLocalAssum(x,None,y,z) |
| 306 |
let dest_entry (_,_,_,_,x) = x |
| 307 |
|
| 308 |
let expr_Type_sort = Constrexpr_ops.expr_Type_sort |
| 309 |
|
| 310 |
[%%if coq = "9.0"] |
| 311 |
let raw_record_decl_to_glob_synterp ({ name; sort; parameters; constructor; fields; univpoly } : raw_record_decl_elpi) : glob_record_decl_elpi = |
| 312 |
let name, space = sep_last_qualid name in |
| 313 |
let params = intern_global_context_synterp parameters in |
| 314 |
let params = List.rev params in |
| 315 |
let arity = mkGHole in |
| 316 |
let fields = |
| 317 |
List.fold_left (fun acc -> function |
| 318 |
| Vernacexpr.AssumExpr ({ CAst.v = name } as fn,bl,x), { Vernacexpr.rf_coercion = inst; rf_priority = pr; rf_notation = nots; rf_canonical = canon } -> |
| 319 |
if nots <> [] then Rocq_elpi_utils.nYI "notation in record fields"; |
| 320 |
if pr <> None then Rocq_elpi_utils.nYI "priority in record fields"; |
| 321 |
let atts = { Rocq_elpi_HOAS.is_canonical = canon; is_coercion = if inst = Vernacexpr.AddCoercion then Reversible else Off; name } in |
| 322 |
let x = if bl = [] then x else Constrexpr_ops.mkCProdN bl x in |
| 323 |
let entry = intern_global_context_synterp [mkCLocalAssum [fn] (Constrexpr.Default Glob_term.Explicit) x] in |
| 324 |
let x = match entry with |
| 325 |
| [x] -> dest_entry x |
| 326 |
| _ -> assert false in |
| 327 |
(x, atts) :: acc |
| 328 |
| Vernacexpr.DefExpr _, _ -> Rocq_elpi_utils.nYI "DefExpr") |
| 329 |
[] fields in |
| 330 |
{ name = (space, Names.Id.of_string name); arity; params; constructorname = constructor; fields = List.rev fields; univpoly } |
| 331 |
|
| 332 |
|
| 333 |
let raw_record_decl_to_glob glob_sign ({ name; sort; parameters; constructor; fields; univpoly } : raw_record_decl_elpi) : glob_record_decl_elpi = |
| 334 |
let name, space = sep_last_qualid name in |
| 335 |
let sort = match sort with |
| 336 |
| Some x -> Constrexpr.CSort x |
| 337 |
| None -> Constrexpr.(CSort expr_Type_sort) in |
| 338 |
let intern_env, params = intern_global_context glob_sign ~intern_env:Constrintern.empty_internalization_env parameters in |
| 339 |
let glob_sign_params = push_glob_ctx params glob_sign in |
| 340 |
let params = List.rev params in |
| 341 |
let arity = intern_global_constr_ty ~intern_env glob_sign_params @@ CAst.make sort in |
| 342 |
let _, _, fields = |
| 343 |
List.fold_left (fun (gs,intern_env,acc) -> function |
| 344 |
| Vernacexpr.AssumExpr ({ CAst.v = name } as fn,bl,x), { Vernacexpr.rf_coercion = inst; rf_priority = pr; rf_notation = nots; rf_canonical = canon } -> |
| 345 |
if nots <> [] then Rocq_elpi_utils.nYI "notation in record fields"; |
| 346 |
if pr <> None then Rocq_elpi_utils.nYI "priority in record fields"; |
| 347 |
let atts = { Rocq_elpi_HOAS.is_canonical = canon; is_coercion = if inst = Vernacexpr.AddCoercion then Reversible else Off; name } in |
| 348 |
let x = if bl = [] then x else Constrexpr_ops.mkCProdN bl x in |
| 349 |
let intern_env, entry = intern_global_context ~intern_env gs [mkCLocalAssum [fn] (Constrexpr.Default Glob_term.Explicit) x] in |
| 350 |
let x = match entry with |
| 351 |
| [x] -> dest_entry x |
| 352 |
| _ -> assert false in |
| 353 |
let gs = push_glob_ctx entry gs in |
| 354 |
gs, intern_env, (x, atts) :: acc |
| 355 |
| Vernacexpr.DefExpr _, _ -> Rocq_elpi_utils.nYI "DefExpr") |
| 356 |
(glob_sign_params,intern_env,[]) fields in |
| 357 |
{ name = (space, Names.Id.of_string name); arity; params; constructorname = constructor; fields = List.rev fields; univpoly } |
| 358 |
[%%else] |
| 359 |
let raw_record_decl_to_glob_synterp ({ name; sort; parameters; constructor; fields; univpoly } : raw_record_decl_elpi) : glob_record_decl_elpi = |
| 360 |
let name, space = sep_last_qualid name in |
| 361 |
let params = intern_global_context_synterp parameters in |
| 362 |
let params = List.rev params in |
| 363 |
let arity = mkGHole in |
| 364 |
let fields = |
| 365 |
List.fold_left (fun acc -> function |
| 366 |
| Vernacexpr.AssumExpr ({ CAst.v = name } as fn,bl,x), { Record.Data.pf_coercion = coe; pf_instance = tcinst; pf_canonical = canon }, nots -> |
| 367 |
if nots <> [] then Rocq_elpi_utils.nYI "notation in record fields"; |
| 368 |
let () = match tcinst with |
| 369 |
| Some { inst_priority = Some _ } -> Rocq_elpi_utils.nYI "priority in record fields" |
| 370 |
| _ -> () |
| 371 |
in |
| 372 |
let atts = { Rocq_elpi_HOAS.is_canonical = canon; is_coercion = if Option.has_some coe then Reversible else Off; name } in |
| 373 |
let x = if bl = [] then x else Constrexpr_ops.mkCProdN bl x in |
| 374 |
let entry = intern_global_context_synterp [mkCLocalAssum [fn] (Constrexpr.Default Glob_term.Explicit) x] in |
| 375 |
let x = match entry with |
| 376 |
| [x] -> dest_entry x |
| 377 |
| _ -> assert false in |
| 378 |
(x, atts) :: acc |
| 379 |
| Vernacexpr.DefExpr _, _, _ -> Rocq_elpi_utils.nYI "DefExpr") |
| 380 |
[] fields in |
| 381 |
{ name = (space, Names.Id.of_string name); arity; params; constructorname = constructor; fields = List.rev fields; univpoly } |
| 382 |
|
| 383 |
let raw_record_decl_to_glob glob_sign ({ name; sort; parameters; constructor; fields; univpoly } : raw_record_decl_elpi) : glob_record_decl_elpi = |
| 384 |
let name, space = sep_last_qualid name in |
| 385 |
let sort = match sort with |
| 386 |
| Some x -> Constrexpr.CSort x |
| 387 |
| None -> Constrexpr.(CSort expr_Type_sort) in |
| 388 |
let intern_env, params = intern_global_context glob_sign ~intern_env:Constrintern.empty_internalization_env parameters in |
| 389 |
let glob_sign_params = push_glob_ctx params glob_sign in |
| 390 |
let params = List.rev params in |
| 391 |
let arity = intern_global_constr_ty ~intern_env glob_sign_params @@ CAst.make sort in |
| 392 |
let _, _, fields = |
| 393 |
List.fold_left (fun (gs,intern_env,acc) -> function |
| 394 |
| Vernacexpr.AssumExpr ({ CAst.v = name } as fn,bl,x), { Record.Data.pf_coercion = coe; pf_instance = tcinst; pf_canonical = canon }, nots -> |
| 395 |
if nots <> [] then Rocq_elpi_utils.nYI "notation in record fields"; |
| 396 |
let () = match tcinst with |
| 397 |
| Some { inst_priority = Some _ } -> Rocq_elpi_utils.nYI "priority in record fields" |
| 398 |
| _ -> () |
| 399 |
in |
| 400 |
let atts = { Rocq_elpi_HOAS.is_canonical = canon; is_coercion = if Option.has_some coe then Reversible else Off; name } in |
| 401 |
let x = if bl = [] then x else Constrexpr_ops.mkCProdN bl x in |
| 402 |
let intern_env, entry = intern_global_context ~intern_env gs [mkCLocalAssum [fn] (Constrexpr.Default Glob_term.Explicit) x] in |
| 403 |
let x = match entry with |
| 404 |
| [x] -> dest_entry x |
| 405 |
| _ -> assert false in |
| 406 |
let gs = push_glob_ctx entry gs in |
| 407 |
gs, intern_env, (x, atts) :: acc |
| 408 |
| Vernacexpr.DefExpr _, _, _ -> Rocq_elpi_utils.nYI "DefExpr") |
| 409 |
(glob_sign_params,intern_env,[]) fields in |
| 410 |
{ name = (space, Names.Id.of_string name); arity; params; constructorname = constructor; fields = List.rev fields; univpoly } |
| 411 |
[%%endif] |
| 412 |
|
| 413 |
let raw_indt_decl_to_glob_synterp ({ finiteness; name; parameters; non_uniform_parameters; arity; constructors; univpoly } : raw_indt_decl_elpi) : glob_indt_decl_elpi = |
| 414 |
let name, space = sep_last_qualid name in |
| 415 |
let name = Names.Id.of_string name in |
| 416 |
let params = intern_global_context_synterp parameters in |
| 417 |
let nuparams_given, nuparams = |
| 418 |
match non_uniform_parameters with |
| 419 |
| Some l -> true, l |
| 420 |
| None -> false, [] in |
| 421 |
let nuparams = intern_global_context_synterp nuparams in |
| 422 |
let params = List.rev params in |
| 423 |
let nuparams = List.rev nuparams in |
| 424 |
let arity = mkGHole in |
| 425 |
let constructors = List.map (fun (id,ty) -> id.CAst.v, mkGHole) constructors in |
| 426 |
{ finiteness; name = (space, name); arity; params; nuparams; nuparams_given; constructors; univpoly } |
| 427 |
|
| 428 |
let raw_indt_decl_to_glob glob_sign ({ finiteness; name; parameters; non_uniform_parameters; arity; constructors; univpoly } : raw_indt_decl_elpi) : glob_indt_decl_elpi = |
| 429 |
let name, space = sep_last_qualid name in |
| 430 |
let name = Names.Id.of_string name in |
| 431 |
let indexes = match arity with |
| 432 |
| Some x -> x |
| 433 |
| None -> CAst.make Constrexpr.(CSort expr_Type_sort) in |
| 434 |
let intern_env, params = intern_global_context glob_sign ~intern_env:Constrintern.empty_internalization_env parameters in |
| 435 |
let nuparams_given, nuparams = |
| 436 |
match non_uniform_parameters with |
| 437 |
| Some l -> true, l |
| 438 |
| None -> false, [] in |
| 439 |
let intern_env, nuparams = intern_global_context glob_sign ~intern_env nuparams in |
| 440 |
let params = List.rev params in |
| 441 |
let nuparams = List.rev nuparams in |
| 442 |
let allparams = params @ nuparams in |
| 443 |
let user_impls : Impargs.manual_implicits = |
| 444 |
if nuparams_given then List.map Rocq_elpi_utils.manual_implicit_of_gdecl nuparams |
| 445 |
else List.map Rocq_elpi_utils.manual_implicit_of_gdecl allparams in |
| 446 |
let glob_sign_params = push_glob_ctx allparams glob_sign in |
| 447 |
let arity = intern_global_constr_ty ~intern_env glob_sign_params indexes in |
| 448 |
let glob_sign_params_self = push_name glob_sign_params (Names.Name name) in |
| 449 |
let indty, intern_env = push_inductive_in_intern_env intern_env name allparams arity user_impls in |
| 450 |
let constructors = |
| 451 |
List.map (fun (id,ty) -> id.CAst.v, |
| 452 |
intern_global_constr_ty ~expty:(Pretyping.OfType indty) glob_sign_params_self ~intern_env ty) constructors in |
| 453 |
{ finiteness; name = (space, name); arity; params; nuparams; nuparams_given; constructors; univpoly } |
| 454 |
let intern_indt_decl glob_sign (it : raw_indt_decl) = glob_sign, it |
| 455 |
|
| 456 |
let expr_hole = CAst.make @@ Constrexpr.CHole(None) |
| 457 |
|
| 458 |
let raw_context_decl_to_glob_synterp fields = |
| 459 |
let fields = intern_global_context_synterp fields in |
| 460 |
List.rev fields |
| 461 |
|
| 462 |
let raw_context_decl_to_glob glob_sign fields = |
| 463 |
let _intern_env, fields = intern_global_context ~intern_env:Constrintern.empty_internalization_env glob_sign fields in |
| 464 |
List.rev fields |
| 465 |
let intern_context_decl glob_sign (it : raw_context_decl) = glob_sign, it |
| 466 |
|
| 467 |
let raw_decl_name_to_glob name = |
| 468 |
let name, space = sep_last_qualid name in |
| 469 |
(space, Names.Id.of_string name) |
| 470 |
|
| 471 |
let interp_red_expr = Redexpr.interp_redexp_no_ltac |
| 472 |
|
| 473 |
[%%if coq = "9.0" || coq = "9.1"] |
| 474 |
let interp_definition ~program_mode poly = ComDefinition.interp_definition ~program_mode |
| 475 |
let interp_assumption ~program_mode poly = ComAssumption.interp_assumption ~program_mode |
| 476 |
[%%else] |
| 477 |
let interp_definition ~program_mode poly = |
| 478 |
ComDefinition.interp_definition ~program_mode ~poly:(PolyFlags.of_univ_poly poly) |
| 479 |
let interp_assumption ~program_mode poly = |
| 480 |
ComAssumption.interp_assumption ~program_mode ~poly:(PolyFlags.of_univ_poly poly) |
| 481 |
[%%endif] |
| 482 |
|
| 483 |
let raw_constant_decl_to_constr ~depth coq_ctx state { name; typ = (bl,typ); body; red; udecl; atts } = |
| 484 |
let env = coq_ctx.env in |
| 485 |
let poly = |
| 486 |
let open Attributes in |
| 487 |
parse polymorphic atts in |
| 488 |
let state, udecl = |
| 489 |
match udecl, poly with |
| 490 |
| None, false -> state, NotUniversePolymorphic |
| 491 |
| Some _, false -> nYI "only universe polymorphic definitions can take universe binders" |
| 492 |
| None, true -> state, NonCumulative (([],true),(empty_univ_csts,true)) |
| 493 |
| Some udecl, true -> |
| 494 |
let sigma, udecl = interp_univ_decl_opt (Rocq_elpi_HOAS.get_global_env state) (Some udecl) in |
| 495 |
let univ_decl_extensible_instance, univ_decl_extensible_constraints, univ_decl_univ_constraints, univ_decl_instance = dest_udecl udecl in |
| 496 |
let ustate = Evd.ustate sigma in |
| 497 |
let state = merge_universe_context state ustate in |
| 498 |
state, NonCumulative ((univ_decl_instance,univ_decl_extensible_instance),(univ_decl_univ_constraints,univ_decl_extensible_constraints)) in |
| 499 |
let sigma = get_sigma state in |
| 500 |
match body, typ with |
| 501 |
| Some body, _ -> |
| 502 |
let sigma, red = option_map_acc (interp_red_expr env) sigma red in |
| 503 |
let sigma, (body, typ), impargs = |
| 504 |
interp_definition ~program_mode:false poly |
| 505 |
env sigma Constrintern.empty_internalization_env bl red body typ |
| 506 |
in |
| 507 |
let state, gls0 = set_current_sigma ~depth state sigma in |
| 508 |
let typ = option_default (fun () -> Retyping.get_type_of env sigma body) typ in |
| 509 |
state, udecl, typ, Some body, gls0 |
| 510 |
| None, Some typ -> |
| 511 |
assert(red = None); |
| 512 |
let sigma, typ, impargs = |
| 513 |
interp_assumption ~program_mode:false poly |
| 514 |
env sigma Constrintern.empty_internalization_env bl typ in |
| 515 |
let state, gls0 = set_current_sigma ~depth state sigma in |
| 516 |
state, udecl, typ, None, gls0 |
| 517 |
| _ -> assert false |
| 518 |
|
| 519 |
|
| 520 |
let raw_constant_decl_to_glob_synterp ({ name; atts; udecl; typ = (params,typ); body } : raw_constant_decl) state = |
| 521 |
let params = intern_global_context_synterp params in |
| 522 |
let params = List.rev params in |
| 523 |
let typ = mkGHole in |
| 524 |
let body = Option.map (fun _ -> mkGHole) body in |
| 525 |
let poly = |
| 526 |
let open Attributes in |
| 527 |
parse polymorphic atts in |
| 528 |
let udecl = |
| 529 |
if poly then NonCumulative (([],true),(empty_univ_csts,true)) |
| 530 |
else NotUniversePolymorphic in |
| 531 |
state, { name = raw_decl_name_to_glob name; params; typ; udecl; body } |
| 532 |
|
| 533 |
let raw_constant_decl_to_glob glob_sign ({ name; atts; udecl; typ = (params,typ); body } : raw_constant_decl) state = |
| 534 |
let intern_env, params = intern_global_context glob_sign ~intern_env:Constrintern.empty_internalization_env params in |
| 535 |
let glob_sign_params = push_glob_ctx params glob_sign in |
| 536 |
let params = List.rev params in |
| 537 |
let typ = Option.default expr_hole typ in |
| 538 |
let typ = intern_global_constr_ty ~intern_env glob_sign_params typ in |
| 539 |
let body = Option.map (intern_global_constr ~intern_env glob_sign_params) body in |
| 540 |
let poly = |
| 541 |
let open Attributes in |
| 542 |
parse polymorphic atts in |
| 543 |
let state, udecl = |
| 544 |
match udecl, poly with |
| 545 |
| None, false -> state, NotUniversePolymorphic |
| 546 |
| Some _, false -> nYI "only universe polymorphic definitions can take universe binders" |
| 547 |
| None, true -> state, NonCumulative (([],true),(empty_univ_csts,true)) |
| 548 |
| Some udecl, true -> |
| 549 |
let sigma, udecl = interp_univ_decl_opt (Rocq_elpi_HOAS.get_global_env state) (Some udecl) in |
| 550 |
let univ_decl_extensible_instance, univ_decl_extensible_constraints, univ_decl_univ_constraints, univ_decl_instance = dest_udecl udecl in |
| 551 |
let ustate = Evd.ustate sigma in |
| 552 |
let state = merge_universe_context state ustate in |
| 553 |
state, NonCumulative ((univ_decl_instance,univ_decl_extensible_instance),(univ_decl_univ_constraints,univ_decl_extensible_constraints)) in |
| 554 |
state, { name = raw_decl_name_to_glob name; params; typ; udecl; body } |
| 555 |
let intern_constant_decl glob_sign (it : raw_constant_decl) = glob_sign, it |
| 556 |
|
| 557 |
let glob glob_sign : raw -> glob = function |
| 558 |
| Int _ as x -> x |
| 559 |
| String _ as x -> x |
| 560 |
| Term t -> Term (intern_tactic_constr glob_sign t) |
| 561 |
| RecordDecl t -> RecordDecl (intern_record_decl glob_sign t) |
| 562 |
| IndtDecl t -> IndtDecl (intern_indt_decl glob_sign t) |
| 563 |
| ConstantDecl t -> ConstantDecl (intern_constant_decl glob_sign t) |
| 564 |
| Context c -> Context (intern_context_decl glob_sign c) |
| 565 |
|
| 566 |
let subst _mod_subst _x = |
| 567 |
CErrors.anomaly Pp.(str "command arguments should not be substituted") |
| 568 |
|
| 569 |
let interp ist env evd : glob -> top = function |
| 570 |
| Int _ as x -> x |
| 571 |
| String _ as x -> x |
| 572 |
| Term t -> Term(ist,t) |
| 573 |
| RecordDecl t -> (RecordDecl(ist,t)) |
| 574 |
| IndtDecl t -> (IndtDecl(ist,t)) |
| 575 |
| ConstantDecl t -> (ConstantDecl(ist,t)) |
| 576 |
| Context c -> (Context(ist,c)) |
| 577 |
|
| 578 |
end |
| 579 |
|
| 580 |
module Tac = struct |
| 581 |
|
| 582 |
type raw_term = Constrexpr.constr_expr |
| 583 |
type glob_term = Genintern.glob_constr_and_expr |
| 584 |
type top_term = interp_sign * Genintern.glob_constr_and_expr |
| 585 |
|
| 586 |
type raw_ltac_term = Constrexpr.constr_expr |
| 587 |
type glob_ltac_term = Glob_term.glob_constr |
| 588 |
type top_ltac_term = interp_sign * Names.Id.t |
| 589 |
|
| 590 |
type raw_ltac_tactic = Ltac_plugin.Tacexpr.raw_tactic_expr |
| 591 |
type glob_ltac_tactic = Ltac_plugin.Tacexpr.glob_tactic_expr |
| 592 |
[%%if coq = "9.0" || coq = "9.1" || coq = "9.2"] |
| 593 |
type top_ltac_tactic = Ltac_plugin.Tacinterp.value |
| 594 |
[%%else] |
| 595 |
type top_ltac_tactic = Ltac_plugin.Tacarg.tacvalue |
| 596 |
[%%endif] |
| 597 |
|
| 598 |
type ltac_ty = Int | String | Term | OpenTerm | List of ltac_ty |
| 599 |
|
| 600 |
type ('a,'f,'t) t = |
| 601 |
| Int : int -> ('a,'f,'t) t |
| 602 |
| String : string -> ('a,'f,'t) t |
| 603 |
| Term : 'a -> ('a,'f,'t) t |
| 604 |
| OpenTerm : 'a -> ('a,'f,'t) t |
| 605 |
| LTac : ltac_ty * 'f -> ('a,'f,'t) t |
| 606 |
| LTacTactic : 't -> ('a,'f,'t) t |
| 607 |
|
| 608 |
type raw = (raw_term, raw_ltac_term, raw_ltac_tactic) t |
| 609 |
type glob = (glob_term, glob_ltac_term, glob_ltac_tactic) t |
| 610 |
type top = (top_term, top_ltac_term, top_ltac_tactic) t |
| 611 |
|
| 612 |
let pr_raw_ltac_arg _ _ _ = Pp.str "TODO: pr_raw_ltac_arg" |
| 613 |
let pr_glob_ltac_arg _ _ _ = Pp.str "TODO: pr_glob_ltac_arg" |
| 614 |
let pr_top_ltac_arg _ _ _ = Pp.str "TODO: pr_top_ltac_arg" |
| 615 |
|
| 616 |
let pr_raw_ltac_tactic _ _ _ = Pp.str "TODO: pr_raw_ltac_tactic" |
| 617 |
let pr_glob_ltac_tactic _ _ _ = Pp.str "TODO: pr_glob_ltac_tactic" |
| 618 |
let pr_top_ltac_tactic _ _ _ = Pp.str "TODO: pr_top_ltac_tactic" |
| 619 |
|
| 620 |
let pr_arg f k t x = match x with |
| 621 |
| Int n -> Pp.int n |
| 622 |
| String s -> Pp.qstring s |
| 623 |
| Term s -> f s |
| 624 |
| OpenTerm s -> f s |
| 625 |
| LTac(_, s) -> k s |
| 626 |
| LTacTactic s -> t s |
| 627 |
|
| 628 |
let pr_glob_constr_and_expr env sigma = function |
| 629 |
| (_, Some c) -> |
| 630 |
pr_constr_expr env sigma c |
| 631 |
| (c, None) -> |
| 632 |
Printer.pr_glob_constr_env env sigma c |
| 633 |
|
| 634 |
let _pr_glob_constr = Printer.pr_glob_constr_env |
| 635 |
|
| 636 |
let pp_raw env sigma : raw -> Pp.t = |
| 637 |
pr_arg |
| 638 |
(pr_constr_expr env sigma) |
| 639 |
(pr_raw_ltac_arg env sigma) |
| 640 |
(pr_raw_ltac_tactic env sigma) |
| 641 |
|
| 642 |
let pp_glob env sigma = |
| 643 |
pr_arg |
| 644 |
(pr_glob_constr_and_expr env sigma) |
| 645 |
(pr_glob_ltac_arg env sigma) |
| 646 |
(pr_glob_ltac_tactic env sigma) |
| 647 |
|
| 648 |
let pp_top env sigma : top -> Pp.t = |
| 649 |
pr_arg |
| 650 |
((fun (_,x) -> pr_glob_constr_and_expr env sigma x)) |
| 651 |
(pr_top_ltac_arg env sigma) |
| 652 |
(pr_top_ltac_tactic env sigma) |
| 653 |
|
| 654 |
let glob glob_sign : raw -> _ * glob = function |
| 655 |
| Int _ as x -> glob_sign, x |
| 656 |
| String _ as x -> glob_sign, x |
| 657 |
| Term t -> glob_sign, Term (intern_tactic_constr glob_sign t) |
| 658 |
| OpenTerm t -> glob_sign, OpenTerm (intern_tactic_constr glob_sign t) |
| 659 |
| LTac(ty,t) -> glob_sign, LTac (ty,fst @@ intern_tactic_constr glob_sign t) |
| 660 |
| LTacTactic t -> glob_sign, LTacTactic (Ltac_plugin.Tacintern.intern_pure_tactic glob_sign t) |
| 661 |
|
| 662 |
let subst mod_subst = function |
| 663 |
| Int _ as x -> x |
| 664 |
| String _ as x -> x |
| 665 |
| Term t -> |
| 666 |
Term (Ltac_plugin.Tacsubst.subst_glob_constr_and_expr mod_subst t) |
| 667 |
| OpenTerm t -> |
| 668 |
OpenTerm (Ltac_plugin.Tacsubst.subst_glob_constr_and_expr mod_subst t) |
| 669 |
| LTac(ty,t) -> |
| 670 |
LTac(ty,(Detyping.subst_glob_constr (Global.env()) mod_subst t)) |
| 671 |
| LTacTactic t -> |
| 672 |
LTacTactic (Ltac_plugin.Tacsubst.subst_tactic mod_subst t) |
| 673 |
|
| 674 |
[%%if coq = "9.0" || coq = "9.1" || coq = "9.2"] |
| 675 |
let ltac1closure = Ltac_plugin.Tacinterp.Value.of_closure |
| 676 |
[%%else] |
| 677 |
let ltac1closure = Ltac_plugin.Tacinterp.Value.closure |
| 678 |
[%%endif] |
| 679 |
|
| 680 |
let interp ist = function |
| 681 |
| Int _ as x -> x |
| 682 |
| String _ as x -> x |
| 683 |
| Term t -> Term(ist,t) |
| 684 |
| OpenTerm t -> OpenTerm(ist,t) |
| 685 |
| LTac(ty,v) -> |
| 686 |
let id = |
| 687 |
match DAst.get v with |
| 688 |
| Glob_term.GVar id -> id |
| 689 |
| _ -> assert false in |
| 690 |
LTac(ty,(ist,id)) |
| 691 |
| LTacTactic t -> LTacTactic (ltac1closure ist t) |
| 692 |
|
| 693 |
[%%if coq = "9.0" || coq = "9.1" || coq = "9.2"] |
| 694 |
let register_interp0 wit f = Geninterp.register_interp0 wit (fun ist x -> |
| 695 |
Ftactic.bind (f ist x) @@ fun v -> |
| 696 |
Ftactic.return (Geninterp.Val.inject (Geninterp.val_tag (Topwit wit)) v)) |
| 697 |
let freturn = Ftactic.return |
| 698 |
[%%else] |
| 699 |
let register_interp0 wit f = Ltac_plugin.Tacinterp.Register.register_interp0 wit f |
| 700 |
let freturn = Ltac_plugin.Ftactic.return |
| 701 |
[%%endif] |
| 702 |
|
| 703 |
let add_genarg tag pr_raw pr_glob pr_top glob subst interp = |
| 704 |
let wit = Genarg.make0 tag in |
| 705 |
let tag = Geninterp.Val.create tag in |
| 706 |
let () = Genintern.register_intern0 wit glob in |
| 707 |
let () = Gensubst.register_subst0 wit subst in |
| 708 |
let () = register_interp0 wit interp in |
| 709 |
let () = Geninterp.register_val0 wit (Some (Geninterp.Val.Base tag)) in |
| 710 |
Ltac_plugin.Pptactic.declare_extra_genarg_pprule wit pr_raw pr_glob pr_top; |
| 711 |
wit |
| 712 |
;; |
| 713 |
|
| 714 |
let wit = add_genarg "elpi_ftactic_arg" |
| 715 |
(fun env sigma _ _ _ -> pp_raw env sigma) |
| 716 |
(fun env sigma _ _ _ -> pp_glob env sigma) |
| 717 |
(fun env sigma _ _ _ -> pp_top env sigma) |
| 718 |
glob |
| 719 |
subst |
| 720 |
(fun ist x -> freturn (interp ist x)) |
| 721 |
|
| 722 |
end |
| 723 |
|
| 724 |
let mk_indt_decl state univpoly r = |
| 725 |
match univpoly with |
| 726 |
| Cmd.Mono -> state, E.mkApp ideclc r [] |
| 727 |
| Cmd.Poly -> |
| 728 |
let state, up, gls = universe_decl.API.Conversion.embed ~depth:0 state (NonCumul(([],true),(Cmd.empty_univ_csts,true))) in |
| 729 |
assert(gls=[]); |
| 730 |
state, E.mkApp uideclc r [up] |
| 731 |
| Cmd.CumulPoly -> |
| 732 |
let state, up, gls = universe_decl.API.Conversion.embed ~depth:0 state (Cumul(([],true),(Cmd.empty_univ_csts,true))) in |
| 733 |
assert(gls=[]); |
| 734 |
state, E.mkApp uideclc r [up] |
| 735 |
|
| 736 |
let rec garityparams2lp_synterp ~depth params k state = |
| 737 |
match params with |
| 738 |
| [] -> k state |
| 739 |
| (name,_,imp,ob,src) :: params -> |
| 740 |
if ob <> None then Rocq_elpi_utils.nYI "defined parameters in a record/inductive declaration"; |
| 741 |
let src = E.mkDiscard in |
| 742 |
let state, tgt = garityparams2lp_synterp ~depth params k state in |
| 743 |
let state, imp = in_elpi_imp ~depth state imp in |
| 744 |
let name = EConstr.annotR name in |
| 745 |
state, in_elpi_arity_parameter name ~imp src tgt |
| 746 |
let rec gindparams2lp_synterp ~depth params k state = |
| 747 |
match params with |
| 748 |
| [] -> k state |
| 749 |
| (name,_,imp,ob,src) :: params -> |
| 750 |
if ob <> None then Rocq_elpi_utils.nYI "defined parameters in a record/inductive declaration"; |
| 751 |
let src = E.mkDiscard in |
| 752 |
let state, tgt = gindparams2lp_synterp ~depth params k state in |
| 753 |
let state, imp = in_elpi_imp ~depth state imp in |
| 754 |
let name = EConstr.annotR name in |
| 755 |
state, in_elpi_inductive_parameter name ~imp src tgt |
| 756 |
|
| 757 |
|
| 758 |
let rec gfields2lp_synterp ~depth fields state = |
| 759 |
match fields with |
| 760 |
| [] -> state, in_elpi_indtdecl_endrecord () |
| 761 |
| (f,({ name; is_coercion; is_canonical } as att)) :: fields -> |
| 762 |
let f = E.mkDiscard in |
| 763 |
let state, fields = gfields2lp_synterp ~depth fields state in |
| 764 |
in_elpi_indtdecl_field ~depth state att f fields |
| 765 |
|
| 766 |
let grecord2lp_synterp ~depth ~name ~constructorname arity fields state = |
| 767 |
let space, record_name = name in |
| 768 |
let qrecord_name = Id.of_string_soft @@ String.concat "." (space @ [Id.to_string record_name]) in |
| 769 |
let arity = E.mkDiscard in |
| 770 |
let state, fields = gfields2lp_synterp ~depth fields state in |
| 771 |
let constructor = match constructorname with |
| 772 |
| None -> Name.Name (Id.of_string ("Build_" ^ Id.to_string record_name)) |
| 773 |
| Some x -> Name.Name x in |
| 774 |
state, in_elpi_indtdecl_record (Name.Name qrecord_name) arity constructor fields |
| 775 |
|
| 776 |
let grecord2lp_synterp ~depth state { Cmd.name; arity; params; constructorname; fields; univpoly } = |
| 777 |
let state, r = gindparams2lp_synterp ~depth params (grecord2lp_synterp ~depth ~name ~constructorname arity fields) state in |
| 778 |
mk_indt_decl state univpoly r |
| 779 |
|
| 780 |
let grecord2lp ~loc ~base ~depth state { Cmd.name; arity; params; constructorname; fields; univpoly } = |
| 781 |
let open Rocq_elpi_glob_quotation in |
| 782 |
let state, r = gindparams2lp ~loc ~base params ~k:(grecord2lp ~loc ~base ~name ~constructorname arity fields) ~depth state in |
| 783 |
mk_indt_decl state univpoly r |
| 784 |
|
| 785 |
let contract_params env sigma name params nuparams_given t = |
| 786 |
if nuparams_given then t else |
| 787 |
let open Glob_term in |
| 788 |
let loc = Option.map Rocq_elpi_utils.of_coq_loc t.CAst.loc in |
| 789 |
let rec contract params args = |
| 790 |
match params, args with |
| 791 |
| [], rest -> rest |
| 792 |
| _ :: _, [] -> |
| 793 |
Rocq_elpi_utils.err ?loc Pp.(str "Inductive type "++ Names.Id.print name ++ |
| 794 |
str" is not applied to enough parameters. Missing: " ++ |
| 795 |
prlist_with_sep spc Names.Name.print (List.map (fun (x,_,_,_) -> x) params)) |
| 796 |
| (Name.Anonymous,_,_,_) :: ps , _ :: rest -> contract ps rest |
| 797 |
| (Name.Name pname,_,_,_) :: ps , arg :: rest -> |
| 798 |
begin match DAst.get arg with |
| 799 |
| GVar v when Names.Id.equal pname v -> contract ps rest |
| 800 |
| GHole _ -> contract ps rest |
| 801 |
| _ -> Rocq_elpi_utils.err ?loc Pp.(str "Inductive type "++ Names.Id.print name ++ |
| 802 |
str" is not applied to parameter " ++ Names.Id.print pname ++ |
| 803 |
str" but rather " ++ Printer.pr_glob_constr_env env sigma arg) |
| 804 |
end |
| 805 |
in |
| 806 |
let rec aux x = |
| 807 |
match DAst.get x with |
| 808 |
| GApp(hd,args) -> |
| 809 |
begin match DAst.get hd with |
| 810 |
| GVar id when Names.Id.equal id name -> |
| 811 |
DAst.make @@ GApp(hd,contract params args) |
| 812 |
| _ -> Glob_ops.map_glob_constr aux x |
| 813 |
end |
| 814 |
| _ -> Glob_ops.map_glob_constr aux x in |
| 815 |
aux t |
| 816 |
|
| 817 |
let nogls f ~depth state = |
| 818 |
let state, x = f ~depth state in |
| 819 |
state, x, () |
| 820 |
|
| 821 |
let drop_unit f ~depth state = |
| 822 |
let state, x, () = f ~depth state in |
| 823 |
state, x |
| 824 |
|
| 825 |
let ginductive2lp_synterp ~depth state { Cmd.finiteness; name; arity; params; nuparams; nuparams_given; constructors; univpoly } = |
| 826 |
let space, indt_name = name in |
| 827 |
let do_constructor ~depth state (name, ty) = |
| 828 |
let state, ty = garityparams2lp_synterp nuparams (fun state -> state, in_elpi_arity E.mkDiscard) ~depth state in |
| 829 |
state, in_elpi_indtdecl_constructor (Name.Name name) ty |
| 830 |
in |
| 831 |
let do_inductive_synterp ~depth state = |
| 832 |
let qindt_name = Id.of_string_soft @@ String.concat "." (space @ [Id.to_string indt_name]) in |
| 833 |
let state, arity = garityparams2lp_synterp nuparams (fun state -> state, in_elpi_arity E.mkDiscard) ~depth state in |
| 834 |
let state, constructors = Rocq_elpi_utils.list_map_acc (do_constructor ~depth ) state constructors in |
| 835 |
state, in_elpi_indtdecl_inductive state finiteness (Name.Name qindt_name) arity constructors |
| 836 |
in |
| 837 |
let state, r = gindparams2lp_synterp params (do_inductive_synterp ~depth) ~depth state in |
| 838 |
mk_indt_decl state univpoly r |
| 839 |
|
| 840 |
let ginductive2lp ~loc ~depth ~base state { Cmd.finiteness; name; arity; params; nuparams; nuparams_given; constructors; univpoly } = |
| 841 |
let open Rocq_elpi_glob_quotation in |
| 842 |
let space, indt_name = name in |
| 843 |
let contract state x = |
| 844 |
let params = List.map drop_relevance params in |
| 845 |
contract_params (get_global_env state) (get_sigma state) indt_name params nuparams_given x in |
| 846 |
let do_constructor ~depth state (name, ty) = |
| 847 |
let state, ty = garityparams2lp ~loc ~base nuparams ~k:(garity2lp ~loc ~base (contract state ty)) ~depth state in |
| 848 |
state, in_elpi_indtdecl_constructor (Name.Name name) ty |
| 849 |
in |
| 850 |
let do_inductive ~depth state = |
| 851 |
let short_name = EConstr.annotR @@ Name.Name indt_name in |
| 852 |
let qindt_name = Id.of_string_soft @@ String.concat "." (space @ [Id.to_string indt_name]) in |
| 853 |
let state, term_arity = gterm2lp ~loc ~depth ~base (Rocq_elpi_utils.mk_gforall arity nuparams) state in |
| 854 |
let state, arity = garityparams2lp ~loc ~base nuparams ~k:(garity2lp ~loc ~base arity) ~depth state in |
| 855 |
under_ctx short_name term_arity None ~k:(fun ~depth state -> |
| 856 |
let state, constructors = Rocq_elpi_utils.list_map_acc (do_constructor ~depth ) state constructors in |
| 857 |
state, in_elpi_indtdecl_inductive state finiteness (Name.Name qindt_name) arity constructors, ()) |
| 858 |
~depth state |
| 859 |
in |
| 860 |
let state, r = gindparams2lp ~loc ~base params ~k:(drop_unit do_inductive) ~depth state in |
| 861 |
mk_indt_decl state univpoly r |
| 862 |
|
| 863 |
let in_option = Elpi.(Builtin.option API.BuiltInData.any).API.Conversion.embed |
| 864 |
|
| 865 |
let decl_name2lp name = |
| 866 |
let space, constant_name = name in |
| 867 |
let qconstant_name = |
| 868 |
Id.of_string_soft @@ String.concat "." (space @ [Id.to_string constant_name]) in |
| 869 |
in_elpi_id (Name.Name qconstant_name) |
| 870 |
|
| 871 |
let cdeclc = E.Constants.declare_global_symbol "const-decl" |
| 872 |
let ucdeclc = E.Constants.declare_global_symbol "upoly-const-decl" |
| 873 |
|
| 874 |
let gdecl2lp_synterp ~depth state { Cmd.name; params; typ : _; body; udecl } = |
| 875 |
let state, typ = garityparams2lp_synterp ~depth params (fun state -> state, in_elpi_arity E.mkDiscard) state in |
| 876 |
let body = Option.map (fun _ -> E.mkDiscard) body in |
| 877 |
let name = decl_name2lp name in |
| 878 |
let state, body, gls = in_option ~depth state body in |
| 879 |
match udecl with |
| 880 |
| NotUniversePolymorphic -> state, E.mkApp cdeclc name [body;typ], gls |
| 881 |
| Cumulative _ -> assert false |
| 882 |
| NonCumulative ud -> |
| 883 |
let state, ud, gls1 = universe_decl.API.Conversion.embed ~depth state (NonCumul ud) in |
| 884 |
state, E.mkApp ucdeclc name [body;typ;ud], gls @ gls1 |
| 885 |
|
| 886 |
let cdecl2lp ~loc ~depth ~base state { Cmd.name; params; typ; body; udecl } = |
| 887 |
let open Rocq_elpi_glob_quotation in |
| 888 |
let state, typ = garityparams2lp ~loc ~base params ~k:(garity2lp ~loc ~base typ) ~depth state in |
| 889 |
let state, body = option_map_acc (fun state bo -> gterm2lp ~loc ~depth ~base (Rocq_elpi_utils.mk_gfun bo params) state) state body in |
| 890 |
let name = decl_name2lp name in |
| 891 |
let state, body, gls = in_option ~depth state body in |
| 892 |
match udecl with |
| 893 |
| NotUniversePolymorphic -> state, E.mkApp cdeclc name [body;typ], gls |
| 894 |
| Cumulative _ -> assert false |
| 895 |
| NonCumulative ud -> |
| 896 |
let state, ud, gls1 = universe_decl.API.Conversion.embed ~depth state (NonCumul ud) in |
| 897 |
state, E.mkApp ucdeclc name [body;typ;ud], gls @ gls1 |
| 898 |
|
| 899 |
let ctxitemc = E.Constants.declare_global_symbol "context-item" |
| 900 |
let ctxendc = E.Constants.declare_global_symbol "context-end" |
| 901 |
|
| 902 |
let rec do_context_glob_synterp fields ~depth state = |
| 903 |
match fields with |
| 904 |
| [] -> state, E.mkGlobal ctxendc |
| 905 |
| (name,_,bk,bo,ty) :: fields -> |
| 906 |
let ty = E.mkDiscard in |
| 907 |
let bo = Option.map (fun _ -> E.mkDiscard) bo in |
| 908 |
let state, fields = do_context_glob_synterp fields ~depth state in |
| 909 |
let state, bo, _ = in_option ~depth state bo in |
| 910 |
let state, imp = in_elpi_imp ~depth state bk in |
| 911 |
state, E.mkApp ctxitemc (in_elpi_id name) [imp;ty;bo;E.mkLam fields] |
| 912 |
|
| 913 |
let rec do_context_glob ~loc fields ~depth ~base state = |
| 914 |
match fields with |
| 915 |
| [] -> state, E.mkGlobal ctxendc |
| 916 |
| (name,_,bk,bo,ty) :: fields -> |
| 917 |
let open Rocq_elpi_glob_quotation in |
| 918 |
let state, ty = gterm2lp ~loc ~depth ~base ty state in |
| 919 |
let state, bo = option_map_acc (fun state bo -> gterm2lp ~loc ~depth ~base bo state) state bo in |
| 920 |
let state, fields, () = Rocq_elpi_glob_quotation.under_ctx (EConstr.annotR name) ty bo ~k:(nogls (do_context_glob ~loc ~base fields)) ~depth state in |
| 921 |
let state, bo, _ = in_option ~depth state bo in |
| 922 |
let state, imp = in_elpi_imp ~depth state bk in |
| 923 |
state, E.mkApp ctxitemc (in_elpi_id name) [imp;ty;bo;E.mkLam fields] |
| 924 |
let rec do_context_constr coq_ctx csts fields ~depth state = |
| 925 |
let map s x = constr2lp coq_ctx csts ~depth s (EConstr.of_constr x) in |
| 926 |
match fields with |
| 927 |
| [] -> state, E.mkGlobal ctxendc, [] |
| 928 |
| (id,bo,ty,bk) :: fields -> |
| 929 |
let name = Name id in |
| 930 |
let state, ty, gl0 = map state ty in |
| 931 |
let state, bo, gl1 = match bo with |
| 932 |
| None -> state, None, [] |
| 933 |
| Some bo -> let state, bo, gl = map state bo in state, Some bo, gl in |
| 934 |
|
| 935 |
let state, fields, gl2 = Rocq_elpi_glob_quotation.under_ctx (EConstr.annotR name) ty bo ~k:(do_context_constr coq_ctx csts fields) ~depth state in |
| 936 |
let state, bo, gl3 = in_option ~depth state bo in |
| 937 |
let state, imp = in_elpi_imp ~depth state bk in |
| 938 |
state, E.mkApp ctxitemc (in_elpi_id name) [imp;ty;bo;E.mkLam fields], gl0 @ gl1 @ gl2 @ gl3 |
| 939 |
|
| 940 |
|
| 941 |
let strc = E.Constants.declare_global_symbol "str" |
| 942 |
let trmc = E.Constants.declare_global_symbol "trm" |
| 943 |
let open_trmc = E.Constants.declare_global_symbol "open-trm" |
| 944 |
let tacc = E.Constants.declare_global_symbol "tac" |
| 945 |
let intc = E.Constants.declare_global_symbol "int" |
| 946 |
let ctxc = E.Constants.declare_global_symbol "ctx-decl" |
| 947 |
|
| 948 |
let my_cast_to_string v = |
| 949 |
let open Ltac_plugin in |
| 950 |
try Taccoerce.Value.cast (Genarg.topwit Stdarg.wit_string) v |
| 951 |
with CErrors.UserError _ -> try |
| 952 |
Taccoerce.Value.cast (Genarg.topwit Stdarg.wit_ident) v |> Names.Id.to_string |
| 953 |
with CErrors.UserError _ -> |
| 954 |
raise (Taccoerce.CannotCoerceTo "a string") |
| 955 |
let to_list v = |
| 956 |
let open Ltac_plugin in |
| 957 |
match Taccoerce.Value.to_list v with |
| 958 |
| None -> raise (Taccoerce.CannotCoerceTo "a list") |
| 959 |
| Some l -> l |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
let best_effort_recover_arity ~depth state glob_sign typ bl = |
| 965 |
let _, grouped_bl = intern_global_context glob_sign ~intern_env:Constrintern.empty_internalization_env bl in |
| 966 |
let rec aux ~depth state typ gbl = |
| 967 |
match gbl with |
| 968 |
| (name,_,ik,_,_) :: gbl -> |
| 969 |
begin match Rocq_elpi_HOAS.is_prod ~depth typ with |
| 970 |
| None -> state, in_elpi_arity typ |
| 971 |
| Some(ty,bo) -> |
| 972 |
let state, imp = in_elpi_imp ~depth state ik in |
| 973 |
let state, bo = aux ~depth:(depth+1) state bo gbl in |
| 974 |
let state, name = mk_coq_annot state name in |
| 975 |
state, in_elpi_arity_parameter name ~imp ty bo |
| 976 |
end |
| 977 |
| _ -> state, in_elpi_arity typ |
| 978 |
in |
| 979 |
aux ~depth state typ (List.rev grouped_bl) |
| 980 |
|
| 981 |
let in_elpi_string_arg ~depth state x = |
| 982 |
state, E.mkApp strc (CD.of_string x) [], [] |
| 983 |
|
| 984 |
let in_elpi_int_arg ~depth state x = |
| 985 |
state, E.mkApp intc (CD.of_int x) [], [] |
| 986 |
|
| 987 |
module NIdSet = struct |
| 988 |
include Id.Map |
| 989 |
let counter = ref 0 |
| 990 |
let add x s = |
| 991 |
if not (mem x s) then begin |
| 992 |
incr counter; |
| 993 |
add x !counter s |
| 994 |
end else |
| 995 |
s |
| 996 |
|
| 997 |
let fold f s acc = |
| 998 |
Id.Map.bindings s |> List.sort (fun (_,n) (_,m) -> m - n) |> |
| 999 |
List.fold_left (fun acc (x,_) -> f x acc) acc |
| 1000 |
|
| 1001 |
end |
| 1002 |
let free_glob_vars known_vars = |
| 1003 |
let open Glob_term in |
| 1004 |
let rec vars bound vs c = match DAst.get c with |
| 1005 |
| GVar id' -> if Id.Set.mem id' bound then vs else NIdSet.add id' vs |
| 1006 |
| _ -> Glob_ops.fold_glob_constr_with_binders Id.Set.add vars bound vs c in |
| 1007 |
fun rt -> |
| 1008 |
let vs = vars known_vars NIdSet.empty rt in |
| 1009 |
vs |
| 1010 |
let close_glob coq_ctx term = |
| 1011 |
let open Glob_term in |
| 1012 |
let fv_set = free_glob_vars coq_ctx.names term in |
| 1013 |
(NIdSet.cardinal fv_set ,NIdSet.fold (fun id t -> |
| 1014 |
DAst.(make (GLambda(Name.Name(id),None,Explicit,mkGHole,t)))) fv_set term) |
| 1015 |
|
| 1016 |
let in_elpi_term_arg ~loc ~base ~depth state coq_ctx hyps sigma ist glob_or_expr = |
| 1017 |
let closure = Ltac_plugin.Tacinterp.interp_glob_closure ist coq_ctx.env sigma glob_or_expr in |
| 1018 |
let g = Rocq_elpi_utils.detype_closed_glob coq_ctx.env sigma closure in |
| 1019 |
let state = Rocq_elpi_glob_quotation.set_coq_ctx_hyps state (coq_ctx,hyps) in |
| 1020 |
let state, t = Rocq_elpi_glob_quotation.gterm2lp ~loc ~depth ~base g state in |
| 1021 |
state, E.mkApp trmc t [], [] |
| 1022 |
|
| 1023 |
let in_elpi_open_term_arg ~loc ~base ~depth state coq_ctx hyps sigma ist glob_or_expr = |
| 1024 |
let closure = Ltac_plugin.Tacinterp.interp_glob_closure ist coq_ctx.env sigma glob_or_expr in |
| 1025 |
let g = Rocq_elpi_utils.detype_closed_glob coq_ctx.env sigma closure in |
| 1026 |
let (n, g) = close_glob coq_ctx g in |
| 1027 |
let state = Rocq_elpi_glob_quotation.set_coq_ctx_hyps state (coq_ctx,hyps) in |
| 1028 |
let state, t = Rocq_elpi_glob_quotation.gterm2lp ~loc ~depth ~base g state in |
| 1029 |
state, E.mkApp open_trmc (CD.of_int n) [t], [] |
| 1030 |
|
| 1031 |
let in_elpi_tac_econstr ~base:() ~depth ?calldepth coq_ctx hyps sigma state x = |
| 1032 |
let state, gls0 = set_current_sigma ~depth state sigma in |
| 1033 |
let state, t, gls1 = Rocq_elpi_HOAS.constr2lp ~depth ?calldepth coq_ctx E.no_constraints state x in |
| 1034 |
state, [E.mkApp trmc t []], gls0 @ gls1 |
| 1035 |
|
| 1036 |
let in_elpi_elab_term_arg ~depth ?calldepth state coq_ctx hyps sigma ist glob_or_expr = |
| 1037 |
let sigma, t = Ltac_plugin.Tacinterp.interp_open_constr_with_classes ist coq_ctx.env sigma glob_or_expr in |
| 1038 |
let state, gls0 = set_current_sigma ~depth state sigma in |
| 1039 |
let state, t, gls1 = constr2lp_closed ~depth ?calldepth coq_ctx E.no_constraints state t in |
| 1040 |
state, E.mkApp trmc t [], gls0 @ gls1 |
| 1041 |
|
| 1042 |
let singleton (state,x,gls) = state,[x],gls |
| 1043 |
let rec in_elpi_ltac_arg ~loc ~depth ~base ?calldepth coq_ctx hyps sigma state ty ist v = |
| 1044 |
let open Ltac_plugin in |
| 1045 |
let open Tac in |
| 1046 |
let self ty state = in_elpi_ltac_arg ~loc ~depth ~base ?calldepth coq_ctx hyps sigma state ty ist in |
| 1047 |
let self_list ty state l = |
| 1048 |
try |
| 1049 |
let state, l, gl = API.Utils.map_acc (self ty) state l in |
| 1050 |
state, List.flatten l, gl |
| 1051 |
with Taccoerce.CannotCoerceTo s -> |
| 1052 |
raise (Taccoerce.CannotCoerceTo (s ^ " list")) in |
| 1053 |
match (ty : ltac_ty) with |
| 1054 |
| List (List _) -> |
| 1055 |
Rocq_elpi_utils.err Pp.(str"ltac_<arg>_list_list is not implemented") |
| 1056 |
| List ty -> |
| 1057 |
let l = to_list v in |
| 1058 |
self_list ty state l |
| 1059 |
| Int -> |
| 1060 |
let n = Taccoerce.coerce_to_int v in |
| 1061 |
singleton @@ in_elpi_int_arg ~depth state n |
| 1062 |
| String -> |
| 1063 |
let s = my_cast_to_string v in |
| 1064 |
singleton @@ in_elpi_string_arg ~depth state s |
| 1065 |
| Term -> begin try |
| 1066 |
let t = Taccoerce.Value.cast (Genarg.topwit Stdarg.wit_open_constr) v in |
| 1067 |
let state, t, gls = constr2lp ~depth ?calldepth coq_ctx E.no_constraints state t in |
| 1068 |
state, [E.mkApp trmc t []], gls |
| 1069 |
with CErrors.UserError _ -> try |
| 1070 |
let closure = Taccoerce.coerce_to_uconstr v in |
| 1071 |
let g = Rocq_elpi_utils.detype_closed_glob coq_ctx.env sigma closure in |
| 1072 |
let state = Rocq_elpi_glob_quotation.set_coq_ctx_hyps state (coq_ctx,hyps) in |
| 1073 |
let state, t = Rocq_elpi_glob_quotation.gterm2lp ~loc ~depth ~base g state in |
| 1074 |
state, [E.mkApp trmc t []], [] |
| 1075 |
with Taccoerce.CannotCoerceTo _ -> try |
| 1076 |
let id = Taccoerce.coerce_to_hyp coq_ctx.env sigma v in |
| 1077 |
let state, t, gls = Rocq_elpi_HOAS.constr2lp ~depth ?calldepth coq_ctx E.no_constraints state (EConstr.mkVar id) in |
| 1078 |
state, [E.mkApp trmc t []], gls |
| 1079 |
with Taccoerce.CannotCoerceTo _ -> |
| 1080 |
raise (Taccoerce.CannotCoerceTo "a term") |
| 1081 |
end |
| 1082 |
| OpenTerm -> |
| 1083 |
let closure = Taccoerce.coerce_to_uconstr v in |
| 1084 |
let g = Rocq_elpi_utils.detype_closed_glob coq_ctx.env sigma closure in |
| 1085 |
let n, g = close_glob coq_ctx g in |
| 1086 |
let state = Rocq_elpi_glob_quotation.set_coq_ctx_hyps state (coq_ctx,hyps) in |
| 1087 |
let state, t = Rocq_elpi_glob_quotation.gterm2lp ~loc ~depth ~base g state in |
| 1088 |
state, [E.mkApp open_trmc (CD.of_int n) [t]], [] |
| 1089 |
|
| 1090 |
let { CD.cin = of_ltac_tactic; isc = is_ltac_tactic; cout = to_ltac_tactic }, tac = CD.declare { |
| 1091 |
CD.name = "ltac1-tactic"; |
| 1092 |
doc = "LTac1 tactic expression"; |
| 1093 |
pp = (fun fmt _ -> Format.fprintf fmt "«ltac1-tactic»"); |
| 1094 |
compare = (fun a b -> 0); |
| 1095 |
hash = (fun x -> Hashtbl.hash x); |
| 1096 |
hconsed = false; |
| 1097 |
constants = []; |
| 1098 |
} |
| 1099 |
|
| 1100 |
let in_elpi_ltac_tactic ~depth ?calldepth coq_ctx hyps sigma state t = |
| 1101 |
state, [E.mkApp tacc (of_ltac_tactic t) []], [] |
| 1102 |
|
| 1103 |
let in_elpi_tac ~loc ~base ~depth ?calldepth coq_ctx hyps sigma state x = |
| 1104 |
let open Tac in |
| 1105 |
match x with |
| 1106 |
| LTacTactic t -> in_elpi_ltac_tactic ~depth ?calldepth coq_ctx hyps sigma state t |
| 1107 |
| LTac(ty,(ist,id)) -> |
| 1108 |
let v = try Id.Map.find id ist.lfun with Not_found -> assert false in |
| 1109 |
begin try |
| 1110 |
in_elpi_ltac_arg ~loc ~depth ~base ?calldepth coq_ctx hyps sigma state ty ist v |
| 1111 |
with Ltac_plugin.Taccoerce.CannotCoerceTo s -> |
| 1112 |
let env = Some (coq_ctx.env,sigma) in |
| 1113 |
Ltac_plugin.Taccoerce.error_ltac_variable id env v s end |
| 1114 |
| Int x -> singleton @@ in_elpi_int_arg ~depth state x |
| 1115 |
| String x -> singleton @@ in_elpi_string_arg ~depth state x |
| 1116 |
| Term (ist,glob_or_expr) -> singleton @@ in_elpi_term_arg ~loc ~depth ~base state coq_ctx hyps sigma ist glob_or_expr |
| 1117 |
| OpenTerm (ist,glob_or_expr) -> singleton @@ in_elpi_open_term_arg ~loc ~depth ~base state coq_ctx hyps sigma ist glob_or_expr |
| 1118 |
|
| 1119 |
let handle_template_polymorphism = function |
| 1120 |
| None -> Some false |
| 1121 |
| Some false -> Some false |
| 1122 |
| Some true -> err Pp.(str "#[universes(template)] is not supported") |
| 1123 |
|
| 1124 |
let handle_template_polymorphism flags = |
| 1125 |
{ flags with ComInductive.template = handle_template_polymorphism flags.ComInductive.template } |
| 1126 |
|
| 1127 |
let in_elpi_cmd_synterp ~depth ?calldepth state (x : Cmd.raw) = |
| 1128 |
let open Cmd in |
| 1129 |
match x with |
| 1130 |
| RecordDecl raw_rdecl -> |
| 1131 |
let raw_rdecl = of_coq_record_definition raw_rdecl in |
| 1132 |
let glob_rdecl = raw_record_decl_to_glob_synterp raw_rdecl in |
| 1133 |
let state, t = grecord2lp_synterp ~depth state glob_rdecl in |
| 1134 |
state, t, [] |
| 1135 |
| IndtDecl raw_indt -> |
| 1136 |
let raw_indt = of_coq_inductive_definition raw_indt in |
| 1137 |
let glob_indt = raw_indt_decl_to_glob_synterp raw_indt in |
| 1138 |
let state, t = ginductive2lp_synterp ~depth state glob_indt in |
| 1139 |
state, t, [] |
| 1140 |
| ConstantDecl raw_cdecl -> |
| 1141 |
let state, glob_cdecl = raw_constant_decl_to_glob_synterp raw_cdecl state in |
| 1142 |
gdecl2lp_synterp ~depth state glob_cdecl |
| 1143 |
| Context raw_ctx -> |
| 1144 |
let glob_ctx = raw_context_decl_to_glob_synterp raw_ctx in |
| 1145 |
let state, t = do_context_glob_synterp glob_ctx ~depth state in |
| 1146 |
state, E.mkApp ctxc t [], [] |
| 1147 |
| Int x -> in_elpi_int_arg ~depth state x |
| 1148 |
| String x -> in_elpi_string_arg ~depth state x |
| 1149 |
| Term raw_term -> |
| 1150 |
state, E.mkApp trmc E.mkDiscard [], [] |
| 1151 |
|
| 1152 |
let dest_rdecl (raw_rdecl : Cmd.raw_record_decl) = |
| 1153 |
let open Vernacentries.Preprocessed_Mind_decl in |
| 1154 |
let { flags; udecl; primitive_proj; kind; records } = raw_rdecl in |
| 1155 |
flags, udecl, primitive_proj, kind, records |
| 1156 |
let interp_structure ~flags udecl kind ~primitive_proj x = |
| 1157 |
Record.interp_structure ~flags udecl kind ~primitive_proj x |
| 1158 |
|
| 1159 |
let dest_idecl raw_indt = |
| 1160 |
let open Vernacentries.Preprocessed_Mind_decl in |
| 1161 |
let { flags; udecl; typing_flags; uniform; private_ind; inductives } = raw_indt in |
| 1162 |
flags, udecl, typing_flags, uniform, private_ind, inductives |
| 1163 |
let interp_mutual_inductive ~flags ~env ~uniform ~private_ind ?typing_flags ~udecl x = |
| 1164 |
ComInductive.interp_mutual_inductive ~env ~flags ~uniform ~private_ind ?typing_flags udecl x |
| 1165 |
|
| 1166 |
let in_elpi_cmd ~loc ~depth ~base ?calldepth coq_ctx state ~raw (x : Cmd.top) = |
| 1167 |
let open Cmd in |
| 1168 |
let hyps = [] in |
| 1169 |
match x with |
| 1170 |
| RecordDecl (_ist,(glob_sign,raw_rdecl)) when raw -> |
| 1171 |
let raw_rdecl = of_coq_record_definition raw_rdecl in |
| 1172 |
let glob_rdecl = raw_record_decl_to_glob glob_sign raw_rdecl in |
| 1173 |
let state = Rocq_elpi_glob_quotation.set_coq_ctx_hyps state (coq_ctx,hyps) in |
| 1174 |
let state, t = grecord2lp ~loc ~base ~depth state glob_rdecl in |
| 1175 |
state, t, [] |
| 1176 |
| RecordDecl (_ist,(glob_sign,raw_rdecl)) -> |
| 1177 |
let flags, udecl, primitive_proj, kind, records = dest_rdecl raw_rdecl in |
| 1178 |
let flags = handle_template_polymorphism flags in |
| 1179 |
|
| 1180 |
let kind = if kind = Vernacexpr.Class true then Vernacexpr.Class false else kind in |
| 1181 |
let e = interp_structure ~flags udecl kind ~primitive_proj records in |
| 1182 |
record_entry2lp ~depth coq_ctx E.no_constraints state ~loose_udecl:(udecl = None) e |
| 1183 |
| IndtDecl (_ist,(glob_sign,raw_indt)) when raw -> |
| 1184 |
let raw_indt = of_coq_inductive_definition raw_indt in |
| 1185 |
let glob_indt = raw_indt_decl_to_glob glob_sign raw_indt in |
| 1186 |
let state = Rocq_elpi_glob_quotation.set_coq_ctx_hyps state (coq_ctx,hyps) in |
| 1187 |
let state, t = ginductive2lp ~loc ~depth ~base state glob_indt in |
| 1188 |
state, t, [] |
| 1189 |
| IndtDecl (_ist,(glob_sign,raw_indt)) -> |
| 1190 |
let flags, udecl, typing_flags, uniform, private_ind, inductives = dest_idecl raw_indt in |
| 1191 |
let flags = handle_template_polymorphism flags in |
| 1192 |
let e = |
| 1193 |
match inductives with |
| 1194 |
| [mind_w_not] -> |
| 1195 |
interp_mutual_inductive ~flags ~env:coq_ctx.env ~uniform ~private_ind ?typing_flags ~udecl [mind_w_not] |
| 1196 |
| _ -> nYI "(HOAS) mutual inductives" |
| 1197 |
in |
| 1198 |
inductive_entry2lp ~depth coq_ctx E.no_constraints state ~loose_udecl:(udecl = None) e |
| 1199 |
| ConstantDecl (_ist,(glob_sign,raw_cdecl)) when raw -> |
| 1200 |
let state, glob_cdecl = raw_constant_decl_to_glob glob_sign raw_cdecl state in |
| 1201 |
let state = Rocq_elpi_glob_quotation.set_coq_ctx_hyps state (coq_ctx,hyps) in |
| 1202 |
cdecl2lp ~loc ~depth ~base state glob_cdecl |
| 1203 |
| ConstantDecl (_ist,(glob_sign,({ name; typ = (bl,_) } as raw_cdecl))) -> |
| 1204 |
let state, udecl, typ, body, gls0 = |
| 1205 |
raw_constant_decl_to_constr ~depth coq_ctx state raw_cdecl in |
| 1206 |
let state, typ, gls1 = constr2lp_closed ~depth ?calldepth coq_ctx E.no_constraints state typ in |
| 1207 |
let state, body, gls2 = |
| 1208 |
option_map_acc2 (constr2lp_closed ~depth ?calldepth coq_ctx E.no_constraints) state body in |
| 1209 |
let state, typ = best_effort_recover_arity ~depth state glob_sign typ bl in |
| 1210 |
let state, body, _ = in_option ~depth state body in |
| 1211 |
let c = decl_name2lp (raw_decl_name_to_glob name) in |
| 1212 |
begin match udecl with |
| 1213 |
| NotUniversePolymorphic -> state, E.mkApp cdeclc c [body;typ], gls0 @ gls1 @ gls2 |
| 1214 |
| Cumulative _ -> assert false |
| 1215 |
| NonCumulative udecl -> |
| 1216 |
let state, ud, gls3 = universe_decl.API.Conversion.embed ~depth state (NonCumul udecl) in |
| 1217 |
state, E.mkApp ucdeclc c [body;typ;ud], gls0 @ gls1 @ gls2 @ gls3 |
| 1218 |
end |
| 1219 |
| Context (_ist,(glob_sign,raw_ctx)) when raw -> |
| 1220 |
let glob_ctx = raw_context_decl_to_glob glob_sign raw_ctx in |
| 1221 |
let state = Rocq_elpi_glob_quotation.set_coq_ctx_hyps state (coq_ctx,hyps) in |
| 1222 |
let state, t = do_context_glob ~loc glob_ctx ~depth ~base state in |
| 1223 |
state, E.mkApp ctxc t [], [] |
| 1224 |
| Context (_ist,(glob_sign,raw_ctx)) -> |
| 1225 |
let sigma, ctx = ComAssumption.interp_context coq_ctx.env (get_sigma state) raw_ctx in |
| 1226 |
let state, gls0 = set_current_sigma ~depth state sigma in |
| 1227 |
let state, t, gls1 = do_context_constr (upcast coq_ctx) E.no_constraints ctx ~depth state in |
| 1228 |
state, E.mkApp ctxc t [], gls0 @ gls1 |
| 1229 |
| Int x -> in_elpi_int_arg ~depth state x |
| 1230 |
| String x -> in_elpi_string_arg ~depth state x |
| 1231 |
| Term (ist,glob_or_expr) when raw -> |
| 1232 |
let sigma = get_sigma state in |
| 1233 |
in_elpi_term_arg ~loc ~depth ~base state coq_ctx hyps sigma ist glob_or_expr |
| 1234 |
| Term (ist,glob_or_expr) -> |
| 1235 |
let sigma = get_sigma state in |
| 1236 |
in_elpi_elab_term_arg ~depth ?calldepth state coq_ctx hyps sigma ist glob_or_expr |
| 1237 |
|
| 1238 |
type coq_arg = Cint of int | Cstr of string | Ctrm of EConstr.t | CLtac1 of Tac.top_ltac_tactic |
| 1239 |
|
| 1240 |
let in_coq_arg ~depth proof_context constraints state t = |
| 1241 |
match E.look ~depth t with |
| 1242 |
| E.App(c,i,[]) when c == intc -> |
| 1243 |
begin match E.look ~depth i with |
| 1244 |
| E.CData c when CD.is_int c -> state, Cint (CD.to_int c), [] |
| 1245 |
| _ -> raise API.Conversion.(TypeErr (TyName"argument",depth,t)) |
| 1246 |
end |
| 1247 |
| E.App(c,s,[]) when c == strc -> |
| 1248 |
begin match E.look ~depth s with |
| 1249 |
| E.CData c when CD.is_string c -> state, Cstr (CD.to_string c), [] |
| 1250 |
| _ -> raise API.Conversion.(TypeErr (TyName"argument",depth,t)) |
| 1251 |
end |
| 1252 |
| E.App(c,t,[]) when c == trmc -> |
| 1253 |
let state, t, gls = lp2constr ~depth proof_context constraints state t in |
| 1254 |
state, Ctrm t, gls |
| 1255 |
| E.App(c,t,[]) when c == trmc -> |
| 1256 |
let state, t, gls = lp2constr ~depth proof_context constraints state t in |
| 1257 |
state, Ctrm t, gls |
| 1258 |
| E.App(c,t,[]) when c == tacc -> |
| 1259 |
begin match E.look ~depth t with |
| 1260 |
| E.CData c when is_ltac_tactic c -> state, CLtac1 (to_ltac_tactic c), [] |
| 1261 |
| _ -> raise API.Conversion.(TypeErr (TyName"argument",depth,t)) |
| 1262 |
end |
| 1263 |
| _ -> raise API.Conversion.(TypeErr (TyName"argument",depth,t)) |
| 1264 |
|
| 1265 |
[%%if coq = "9.0" || coq = "9.1" || coq = "9.2"] |
| 1266 |
let ltac1ofvalue x = x |
| 1267 |
[%%else] |
| 1268 |
let ltac1ofvalue = Ltac_plugin.Taccoerce.Value.of_tacvalue |
| 1269 |
[%%endif] |
| 1270 |
|
| 1271 |
let ltac1_of_arg = let open Ltac_plugin in function |
| 1272 |
| Ctrm t -> Tacinterp.Value.of_constr t |
| 1273 |
| Cstr s -> Geninterp.(Val.inject (val_tag (Genarg.topwit Stdarg.wit_string))) s |
| 1274 |
| Cint i -> Tacinterp.Value.of_int i |
| 1275 |
| CLtac1 x -> ltac1ofvalue x |