Skip to content

Commit a018fc8

Browse files
authored
Merge pull request #31 from yforster/reify-inductive-mapping
Adapt reification to take into account extract inductive directives
2 parents 95924d4 + 61b2281 commit a018fc8

File tree

1 file changed

+29
-3
lines changed

1 file changed

+29
-3
lines changed

lib/coq_verified_extraction_plugin/lib/verified_extraction.ml

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,28 @@ struct
534534
Feedback.msg_debug (Pp.str (Printf.sprintf "Ocaml get_boxed_ordinal ordinal: %u" (tag_int land 255)));
535535
tag_int land 255 *)
536536

537-
let reify env sigma ty v : Constr.t =
537+
let apply_reordering hd m cstrs =
538+
try
539+
let find_ind_ord (ind, (na, tags)) =
540+
if Kernames.eq_inductive_def ind hd then
541+
Some (Array.of_list (List.map (fun i -> cstrs.(i)) tags))
542+
else None
543+
in
544+
CList.find_map_exn find_ind_ord m
545+
with Not_found -> cstrs
546+
547+
let find_reverse_mapping hd m cstr =
548+
try
549+
let find_ind_ord (ind, (na, tags)) =
550+
if Kernames.eq_inductive_def ind hd then
551+
Some (CList.index0 Int.equal cstr tags)
552+
else None
553+
in
554+
CList.find_map_exn find_ind_ord m
555+
with Not_found -> cstr
556+
557+
558+
let reify env sigma m ty v : Constr.t =
538559
let open Declarations in
539560
let debug s = debug Pp.(fun () -> str ("reify: ") ++ s ()) in
540561
let rec aux ty v =
@@ -544,11 +565,13 @@ struct
544565
| IsInductive (hd, u, args) ->
545566
let open Inductive in
546567
let open Inductiveops in
568+
let qhd = match Metacoq_template_plugin.Ast_quoter.quote_global_reference (IndRef hd) with Kernames.IndRef i -> i | _ -> assert false in
547569
let spec = lookup_mind_specif env hd in
548570
let npars = inductive_params spec in
549571
let params, _indices = CList.chop npars args in
550572
let indfam = make_ind_family ((hd, u), params) in
551573
let cstrs = get_constructors env indfam in
574+
let cstrs = apply_reordering qhd m cstrs in
552575
if Obj.is_block v then
553576
let ord = Obj.tag v in
554577
let () = debug Pp.(fun () -> str (Printf.sprintf "Reifying constructor block of tag %i" ord)) in
@@ -557,6 +580,7 @@ struct
557580
with Not_found -> ill_formed env sigma ty
558581
in
559582
let cstr = cstrs.(coqidx) in
583+
let coqidx = find_reverse_mapping qhd m coqidx in
560584
let ctx = Vars.smash_rel_context cstr.cs_args in
561585
let vargs = List.init (List.length ctx) (Obj.field v) in
562586
let args' = List.map2 (fun decl v ->
@@ -571,6 +595,7 @@ struct
571595
try find_nth_constant ord cstrs
572596
with Not_found -> ill_formed env sigma ty
573597
in
598+
let coqidx = find_reverse_mapping qhd m coqidx in
574599
let () = debug Pp.(fun () -> str @@ Printf.sprintf "Reifying constant constructor: %i is %i in Coq" ord coqidx) in
575600
Term.applistc (Constr.mkConstructU ((hd, coqidx + 1), u)) params
576601
| IsPrimitive (c, u, _args) ->
@@ -584,8 +609,9 @@ struct
584609
in aux ty v
585610

586611
let reify opts env sigma tyinfo result =
587-
if opts.time then time opts (Pp.str "Reification") (reify env sigma tyinfo) result
588-
else reify env sigma tyinfo result
612+
let mapping = opts.malfunction_pipeline_config.reorder_constructors in
613+
if opts.time then time opts (Pp.str "Reification") (reify env sigma mapping tyinfo) result
614+
else reify env sigma mapping tyinfo result
589615

590616
end
591617

0 commit comments

Comments
 (0)