| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Names |
| 12 |
open ModPath |
| 13 |
open Term |
| 14 |
open Declarations |
| 15 |
open Namegen |
| 16 |
open Libobject |
| 17 |
open Goptions |
| 18 |
open Libnames |
| 19 |
open Globnames |
| 20 |
open CErrors |
| 21 |
open Util |
| 22 |
open Pp |
| 23 |
open Miniml |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
let blacklist_table = Summary.ref Id.Set.empty ~name:"ExtrBlacklist" |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
module GlobOrd = |
| 33 |
struct |
| 34 |
type t = global |
| 35 |
let compare g1 g2 = |
| 36 |
let c = GlobRef.UserOrd.compare g1.glob g2.glob in |
| 37 |
if Int.equal c 0 then InfvInst.compare g1.inst g2.inst else c |
| 38 |
end |
| 39 |
|
| 40 |
module Refmap' = CMap.Make(GlobOrd) |
| 41 |
module Refset' = Set.Make(GlobOrd) |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
let occur_kn_in_ref kn r = let open GlobRef in match r.glob with |
| 46 |
| IndRef (kn',_) |
| 47 |
| ConstructRef ((kn',_),_) -> MutInd.CanOrd.equal kn kn' |
| 48 |
| ConstRef _ | VarRef _ -> false |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
let repr_of_r r = let open GlobRef in match r.glob with |
| 53 |
| ConstRef kn -> Constant.user kn |
| 54 |
| IndRef (kn,_) |
| 55 |
| ConstructRef ((kn,_),_) -> MutInd.user kn |
| 56 |
| VarRef v -> Lib.make_kn v |
| 57 |
|
| 58 |
let modpath_of_r r = |
| 59 |
KerName.modpath (repr_of_r r) |
| 60 |
|
| 61 |
let label_of_r r = |
| 62 |
KerName.label (repr_of_r r) |
| 63 |
|
| 64 |
let rec base_mp = function |
| 65 |
| MPdot (mp,l) -> base_mp mp |
| 66 |
| mp -> mp |
| 67 |
|
| 68 |
let is_modfile = function |
| 69 |
| MPfile _ -> true |
| 70 |
| _ -> false |
| 71 |
|
| 72 |
let raw_string_of_modfile = function |
| 73 |
| MPfile f -> String.capitalize_ascii (Id.to_string (List.hd (DirPath.repr f))) |
| 74 |
| _ -> assert false |
| 75 |
|
| 76 |
let extraction_current_mp () = fst (Safe_typing.flatten_env (Global.safe_env ())) |
| 77 |
|
| 78 |
let is_toplevel mp = ModPath.equal mp (extraction_current_mp ()) |
| 79 |
|
| 80 |
let at_toplevel mp = |
| 81 |
is_modfile mp || is_toplevel mp |
| 82 |
|
| 83 |
let mp_length mp = |
| 84 |
let mp0 = extraction_current_mp () in |
| 85 |
let rec len = function |
| 86 |
| mp when ModPath.equal mp mp0 -> 1 |
| 87 |
| MPdot (mp,_) -> 1 + len mp |
| 88 |
| _ -> 1 |
| 89 |
in len mp |
| 90 |
|
| 91 |
let rec prefixes_mp mp = match mp with |
| 92 |
| MPdot (mp',_) -> ModPath.Set.add mp (prefixes_mp mp') |
| 93 |
| _ -> ModPath.Set.singleton mp |
| 94 |
|
| 95 |
let rec get_nth_label_mp n = function |
| 96 |
| MPdot (mp,l) -> if Int.equal n 1 then l else get_nth_label_mp (n-1) mp |
| 97 |
| _ -> failwith "get_nth_label: not enough MPdot" |
| 98 |
|
| 99 |
let common_prefix_from_list mp0 mpl = |
| 100 |
let prefixes = prefixes_mp mp0 in |
| 101 |
let rec f = function |
| 102 |
| [] -> None |
| 103 |
| mp :: l -> if ModPath.Set.mem mp prefixes then Some mp else f l |
| 104 |
in f mpl |
| 105 |
|
| 106 |
let rec parse_labels2 ll = function |
| 107 |
| MPdot (mp,l) -> parse_labels2 (l::ll) mp |
| 108 |
| mp -> mp,ll |
| 109 |
|
| 110 |
let labels_of_ref r = |
| 111 |
let mp,l = KerName.repr (repr_of_r r) in |
| 112 |
parse_labels2 [l] mp |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
module InfvMap = Map.Make(InfvInst) |
| 124 |
|
| 125 |
type table = { |
| 126 |
typedefs : (constant_body * ml_type) InfvMap.t Cmap_env.t; |
| 127 |
cst_types : (constant_body * ml_schema) InfvMap.t Cmap_env.t; |
| 128 |
inductives : (mutual_inductive_body * ml_ind) InfvMap.t Mindmap_env.t; |
| 129 |
inductive_kinds : inductive_kind InfvMap.t Mindmap_env.t; |
| 130 |
recursors : KerName.Set.t; |
| 131 |
|
| 132 |
projs : GlobRef.Set_env.t; |
| 133 |
|
| 134 |
info_axioms : Refset'.t; |
| 135 |
log_axioms : Refset'.t; |
| 136 |
symbols : Id.t list Refmap'.t; |
| 137 |
opaques: Refset'.t; |
| 138 |
modfile_ids : Id.Set.t; |
| 139 |
modfile_mps : string DirPath.Map.t; |
| 140 |
} |
| 141 |
|
| 142 |
type t = table ref |
| 143 |
|
| 144 |
let empty_table = { |
| 145 |
typedefs = Cmap_env.empty; |
| 146 |
cst_types = Cmap_env.empty; |
| 147 |
inductives = Mindmap_env.empty; |
| 148 |
inductive_kinds = Mindmap_env.empty; |
| 149 |
recursors = KerName.Set.empty; |
| 150 |
projs = GlobRef.Set_env.empty; |
| 151 |
info_axioms = Refset'.empty; |
| 152 |
log_axioms = Refset'.empty; |
| 153 |
symbols = Refmap'.empty; |
| 154 |
opaques = Refset'.empty; |
| 155 |
modfile_ids = Id.Set.empty; |
| 156 |
modfile_mps = DirPath.Map.empty; |
| 157 |
} |
| 158 |
|
| 159 |
let make_table () = ref { empty_table with modfile_ids = !blacklist_table } |
| 160 |
|
| 161 |
let add_typedef table kn inst cb t = |
| 162 |
let upd = function |
| 163 |
| None -> Some (InfvMap.singleton inst (cb, t)) |
| 164 |
| Some map -> Some (InfvMap.add inst (cb, t) map) |
| 165 |
in |
| 166 |
table := { !table with typedefs = Cmap_env.update kn upd !table.typedefs } |
| 167 |
|
| 168 |
let lookup_typedef table kn inst cb = |
| 169 |
try |
| 170 |
let (cb0, t) = InfvMap.find inst (Cmap_env.find kn !table.typedefs) in |
| 171 |
if cb0 == cb then Some t else None |
| 172 |
with Not_found -> None |
| 173 |
|
| 174 |
let add_cst_type table kn inst cb s = |
| 175 |
let upd = function |
| 176 |
| None -> Some (InfvMap.singleton inst (cb, s)) |
| 177 |
| Some map -> Some (InfvMap.add inst (cb, s) map) |
| 178 |
in |
| 179 |
table := { !table with cst_types = Cmap_env.update kn upd !table.cst_types } |
| 180 |
|
| 181 |
let lookup_cst_type table kn inst cb = |
| 182 |
try |
| 183 |
let (cb0, s) = InfvMap.find inst (Cmap_env.find kn !table.cst_types) in |
| 184 |
if cb0 == cb then Some s else None |
| 185 |
with Not_found -> None |
| 186 |
|
| 187 |
let add_ind table kn inst mib ml_ind = |
| 188 |
let upd = function |
| 189 |
| None -> Some (InfvMap.singleton inst (mib, ml_ind)) |
| 190 |
| Some map -> Some (InfvMap.add inst (mib, ml_ind) map) |
| 191 |
in |
| 192 |
table := { !table with inductives = Mindmap_env.update kn upd !table.inductives } |
| 193 |
|
| 194 |
let lookup_ind table kn inst mib = |
| 195 |
try |
| 196 |
let (mib0, ml_ind) = InfvMap.find inst (Mindmap_env.find kn !table.inductives) in |
| 197 |
if mib == mib0 then Some ml_ind |
| 198 |
else None |
| 199 |
with Not_found -> None |
| 200 |
|
| 201 |
let add_inductive_kind table kn inst k = |
| 202 |
let upd = function |
| 203 |
| None -> Some (InfvMap.singleton inst k) |
| 204 |
| Some map -> Some (InfvMap.add inst k map) |
| 205 |
in |
| 206 |
table := { !table with inductive_kinds = Mindmap_env.update kn upd !table.inductive_kinds } |
| 207 |
|
| 208 |
let is_coinductive table r = |
| 209 |
let kn = let open GlobRef in match r.glob with |
| 210 |
| ConstructRef ((kn,_),_) -> kn |
| 211 |
| IndRef (kn,_) -> kn |
| 212 |
| _ -> assert false |
| 213 |
in |
| 214 |
try InfvMap.find r.inst (Mindmap_env.find kn !table.inductive_kinds) == Coinductive |
| 215 |
with Not_found -> false |
| 216 |
|
| 217 |
let is_coinductive_type table = function |
| 218 |
| Tglob (r,_) -> is_coinductive table r |
| 219 |
| _ -> false |
| 220 |
|
| 221 |
let get_record_fields table r = |
| 222 |
let kn = let open GlobRef in match r.glob with |
| 223 |
| ConstructRef ((kn,_),_) -> kn |
| 224 |
| IndRef (kn,_) -> kn |
| 225 |
| _ -> assert false |
| 226 |
in |
| 227 |
try match InfvMap.find r.inst (Mindmap_env.find kn !table.inductive_kinds) with |
| 228 |
| Record f -> f |
| 229 |
| _ -> [] |
| 230 |
with Not_found -> [] |
| 231 |
|
| 232 |
let record_fields_of_type table = function |
| 233 |
| Tglob (r,_) -> get_record_fields table r |
| 234 |
| _ -> [] |
| 235 |
|
| 236 |
let add_recursors table env ind = |
| 237 |
let kn = MutInd.canonical ind in |
| 238 |
let mk_kn id = |
| 239 |
KerName.make (KerName.modpath kn) id |
| 240 |
in |
| 241 |
let mib = Environ.lookup_mind ind env in |
| 242 |
Array.iter |
| 243 |
(fun mip -> |
| 244 |
let id = mip.mind_typename in |
| 245 |
let kn_rec = mk_kn (Nameops.add_suffix id "_rec") |
| 246 |
and kn_rect = mk_kn (Nameops.add_suffix id "_rect") in |
| 247 |
table := { !table with recursors = KerName.Set.add kn_rec (KerName.Set.add kn_rect !table.recursors) }) |
| 248 |
mib.mind_packets |
| 249 |
|
| 250 |
let is_recursor table r = match r.glob with |
| 251 |
| GlobRef.ConstRef c -> KerName.Set.mem (Constant.canonical c) !table.recursors |
| 252 |
| _ -> false |
| 253 |
|
| 254 |
let add_projection table n kn ip = table := { !table with projs = GlobRef.Set_env.add (GlobRef.ConstRef kn) !table.projs } |
| 255 |
let is_projection table r = GlobRef.Set_env.mem r.glob !table.projs |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
let add_info_axiom table r = table := { !table with info_axioms = Refset'.add r !table.info_axioms } |
| 260 |
let remove_info_axiom table r = table := { !table with info_axioms = Refset'.remove r !table.info_axioms } |
| 261 |
let add_log_axiom table r = table := { !table with log_axioms = Refset'.add r !table.log_axioms } |
| 262 |
let add_symbol table r = table := { !table with symbols = Refmap'.update r (function Some l -> Some l | _ -> Some []) !table.symbols } |
| 263 |
let add_symbol_rule table r l = table := { !table with symbols = Refmap'.update r (function Some lst -> Some (l :: lst) | _ -> Some [l]) !table.symbols } |
| 264 |
|
| 265 |
let add_opaque table r = table := { !table with opaques = Refset'.add r !table.opaques } |
| 266 |
let remove_opaque table r = table := { !table with opaques = Refset'.remove r !table.opaques } |
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
let safe_basename_of_global_gen table r = |
| 284 |
let last_chance r (kn, pos) = |
| 285 |
try Nametab.basename_of_global r |
| 286 |
with Not_found -> |
| 287 |
let id = Id.to_string (MutInd.label kn) in |
| 288 |
Id.of_string (id ^ "_" ^ String.concat "_" (List.map string_of_int pos)) |
| 289 |
in |
| 290 |
let unsafe_lookup_ind table kn = snd (InfvMap.find r.inst (Mindmap_env.find kn !table.inductives)) in |
| 291 |
let open GlobRef in |
| 292 |
match r.glob with |
| 293 |
| ConstRef kn -> Constant.label kn |
| 294 |
| IndRef (kn,0) -> MutInd.label kn |
| 295 |
| IndRef (kn,i) -> |
| 296 |
let r = r.glob in |
| 297 |
begin match table with |
| 298 |
| None -> last_chance r (kn, [i]) |
| 299 |
| Some table -> |
| 300 |
try (unsafe_lookup_ind table kn).ind_packets.(i).ip_typename |
| 301 |
with Not_found -> last_chance r (kn, [i]) |
| 302 |
end |
| 303 |
| ConstructRef ((kn,i),j) -> |
| 304 |
let r = r.glob in |
| 305 |
begin match table with |
| 306 |
| None -> last_chance r (kn, [i; j]) |
| 307 |
| Some table -> |
| 308 |
try (unsafe_lookup_ind table kn).ind_packets.(i).ip_consnames.(j-1) |
| 309 |
with Not_found -> last_chance r (kn, [i]) |
| 310 |
end |
| 311 |
| VarRef v -> v |
| 312 |
|
| 313 |
let safe_basename_of_global table r = safe_basename_of_global_gen (Some table) r |
| 314 |
|
| 315 |
let string_of_global r = |
| 316 |
try string_of_qualid (Nametab.shortest_qualid_of_global Id.Set.empty r.glob) |
| 317 |
with Not_found -> Id.to_string (safe_basename_of_global_gen None r) |
| 318 |
|
| 319 |
let safe_pr_global r = str (string_of_global r) |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
let safe_pr_long_global r = |
| 324 |
try Printer.pr_global r.glob |
| 325 |
with Not_found -> match r.glob with |
| 326 |
| GlobRef.ConstRef kn -> |
| 327 |
let mp,l = KerName.repr (Constant.user kn) in |
| 328 |
str ((ModPath.to_string mp)^"."^(Id.to_string l)) |
| 329 |
| _ -> assert false |
| 330 |
|
| 331 |
let pr_long_mp mp = |
| 332 |
let sp = Nametab.path_of_module mp in |
| 333 |
Libnames.pr_path sp |
| 334 |
|
| 335 |
let pr_long_global ref = pr_path (Nametab.path_of_global ref) |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
let err ?loc s = user_err ?loc s |
| 340 |
|
| 341 |
let warn_extraction_axiom_to_realize = |
| 342 |
CWarnings.create ~name:"extraction-axiom-to-realize" ~category:CWarnings.CoreCategories.extraction |
| 343 |
(fun axioms -> |
| 344 |
let s = if Int.equal (List.length axioms) 1 then "axiom" else "axioms" in |
| 345 |
strbrk ("The following "^s^" must be realized in the extracted code:") |
| 346 |
++ hov 1 (spc () ++ prlist_with_sep spc safe_pr_global axioms) |
| 347 |
++ str "." ++ fnl ()) |
| 348 |
|
| 349 |
let warn_extraction_logical_axiom = |
| 350 |
CWarnings.create ~name:"extraction-logical-axiom" ~category:CWarnings.CoreCategories.extraction |
| 351 |
(fun axioms -> |
| 352 |
let s = |
| 353 |
if Int.equal (List.length axioms) 1 then "axiom was" else "axioms were" |
| 354 |
in |
| 355 |
(strbrk ("The following logical "^s^" encountered:") ++ |
| 356 |
hov 1 (spc () ++ prlist_with_sep spc safe_pr_global axioms ++ str ".\n") |
| 357 |
++ strbrk "Having invalid logical axiom in the environment when extracting" |
| 358 |
++ spc () ++ strbrk "may lead to incorrect or non-terminating ML terms." ++ |
| 359 |
fnl ())) |
| 360 |
|
| 361 |
let warn_extraction_symbols = |
| 362 |
let pp_symb_with_rules (symb, rules) = |
| 363 |
safe_pr_global symb ++ |
| 364 |
if List.is_empty rules then str " (no rules)" else |
| 365 |
str ":" ++ spc() ++ prlist_with_sep spc Id.print rules |
| 366 |
in |
| 367 |
CWarnings.create ~name:"extraction-symbols" ~category:CWarnings.CoreCategories.extraction |
| 368 |
(fun symbols -> |
| 369 |
strbrk ("The following symbols and rules were encountered:") ++ fnl () ++ |
| 370 |
prlist_with_sep fnl pp_symb_with_rules symbols ++ fnl () ++ |
| 371 |
strbrk "The symbols must be realized such that the rewrite rules apply," ++ spc () ++ |
| 372 |
strbrk "or extraction may lead to incorrect or non-terminating ML terms." ++ |
| 373 |
fnl ()) |
| 374 |
|
| 375 |
let warning_axioms table = |
| 376 |
let info_axioms = Refset'.elements !table.info_axioms in |
| 377 |
if not (List.is_empty info_axioms) then |
| 378 |
warn_extraction_axiom_to_realize info_axioms; |
| 379 |
let log_axioms = Refset'.elements !table.log_axioms in |
| 380 |
if not (List.is_empty log_axioms) then |
| 381 |
warn_extraction_logical_axiom log_axioms; |
| 382 |
let symbols = Refmap'.bindings !table.symbols in |
| 383 |
if not (List.is_empty symbols) then |
| 384 |
warn_extraction_symbols symbols |
| 385 |
|
| 386 |
let warn_extraction_opaque_accessed = |
| 387 |
CWarnings.create ~name:"extraction-opaque-accessed" ~category:CWarnings.CoreCategories.extraction |
| 388 |
(fun lst -> strbrk "The extraction is currently set to bypass opacity, " ++ |
| 389 |
strbrk "the following opaque constant bodies have been accessed :" ++ |
| 390 |
lst ++ str "." ++ fnl ()) |
| 391 |
|
| 392 |
let warn_extraction_opaque_as_axiom = |
| 393 |
CWarnings.create ~name:"extraction-opaque-as-axiom" ~category:CWarnings.CoreCategories.extraction |
| 394 |
(fun lst -> strbrk "The extraction now honors the opacity constraints by default, " ++ |
| 395 |
strbrk "the following opaque constants have been extracted as axioms :" ++ |
| 396 |
lst ++ str "." ++ fnl () ++ |
| 397 |
strbrk "If necessary, use \"Set Extraction AccessOpaque\" to change this." |
| 398 |
++ fnl ()) |
| 399 |
|
| 400 |
let warning_opaques table accessed = |
| 401 |
let opaques = Refset'.elements !table.opaques in |
| 402 |
if not (List.is_empty opaques) then |
| 403 |
let lst = hov 1 (spc () ++ prlist_with_sep spc safe_pr_global opaques) in |
| 404 |
if accessed then warn_extraction_opaque_accessed lst |
| 405 |
else warn_extraction_opaque_as_axiom lst |
| 406 |
|
| 407 |
let warning_ambiguous_name = |
| 408 |
CWarnings.create ~name:"extraction-ambiguous-name" ~category:CWarnings.CoreCategories.extraction |
| 409 |
~quickfix:(fun ~loc (_,mp,r) -> List.map (Quickfix.make ~loc) [pr_long_mp mp;pr_long_global r]) |
| 410 |
(fun (q,mp,r) -> |
| 411 |
strbrk "The name " ++ pr_qualid q ++ strbrk " is ambiguous, " ++ |
| 412 |
strbrk "do you mean module " ++ |
| 413 |
pr_long_mp mp ++ |
| 414 |
strbrk " or object " ++ |
| 415 |
pr_long_global r ++ str " ?" ++ fnl () ++ |
| 416 |
strbrk "First choice is assumed, for the second one please use " ++ |
| 417 |
strbrk "fully qualified name." ++ fnl ()) |
| 418 |
|
| 419 |
let error_axiom_scheme ?loc r i = |
| 420 |
err ?loc (str "The type scheme axiom " ++ spc () ++ |
| 421 |
safe_pr_global r ++ spc () ++ str "needs " ++ int i ++ |
| 422 |
str " type variable(s).") |
| 423 |
|
| 424 |
let check_inside_section () = |
| 425 |
if Lib.sections_are_opened () then |
| 426 |
err (str "You can't do that within a section." ++ fnl () ++ |
| 427 |
str "Close it and try again.") |
| 428 |
|
| 429 |
let warn_extraction_reserved_identifier = |
| 430 |
CWarnings.create ~name:"extraction-reserved-identifier" ~category:CWarnings.CoreCategories.extraction |
| 431 |
(fun s -> strbrk ("The identifier "^s^ |
| 432 |
" contains __ which is reserved for the extraction")) |
| 433 |
|
| 434 |
let warning_id s = warn_extraction_reserved_identifier s |
| 435 |
|
| 436 |
let error_constant ?loc r = |
| 437 |
err ?loc (safe_pr_global r ++ str " is not a constant.") |
| 438 |
|
| 439 |
let error_inductive ?loc r = |
| 440 |
err ?loc (safe_pr_global r ++ spc () ++ str "is not an inductive type.") |
| 441 |
|
| 442 |
let error_nb_cons () = |
| 443 |
err (str "Not the right number of constructors.") |
| 444 |
|
| 445 |
let error_module_clash mp1 mp2 = |
| 446 |
err (str "The Rocq modules " ++ pr_long_mp mp1 ++ str " and " ++ |
| 447 |
pr_long_mp mp2 ++ str " have the same ML name.\n" ++ |
| 448 |
str "This is not supported yet. Please do some renaming first.") |
| 449 |
|
| 450 |
let error_no_module_expr mp = |
| 451 |
err (str "The module " ++ pr_long_mp mp |
| 452 |
++ str " has no body, it probably comes from\n" |
| 453 |
++ str "some Declare Module outside any Module Type.\n" |
| 454 |
++ str "This situation is currently unsupported by the extraction.") |
| 455 |
|
| 456 |
let error_singleton_become_prop ind = |
| 457 |
|
| 458 |
let ind = { glob = IndRef ind; inst = InfvInst.empty } in |
| 459 |
err (str "The informative inductive type " ++ safe_pr_global ind ++ |
| 460 |
str " has a Prop instance" ++ str "." ++ fnl () ++ |
| 461 |
str "This happens when a sort-polymorphic singleton inductive type\n" ++ |
| 462 |
str "has logical parameters, such as (I,I) : (True * True) : Prop.\n" ++ |
| 463 |
str "Extraction cannot handle this situation yet.\n" ++ |
| 464 |
str "Instead, use a sort-monomorphic type such as (True /\\ True)\n" ++ |
| 465 |
str "or extract to Haskell.") |
| 466 |
|
| 467 |
let error_unknown_module ?loc m = |
| 468 |
err ?loc (str "Module" ++ spc () ++ pr_qualid m ++ spc () ++ str "not found.") |
| 469 |
|
| 470 |
let error_scheme () = |
| 471 |
err (str "No Scheme modular extraction available yet.") |
| 472 |
|
| 473 |
let error_not_visible r = |
| 474 |
err (safe_pr_global r ++ str " is not directly visible.\n" ++ |
| 475 |
str "For example, it may be inside an applied functor.\n" ++ |
| 476 |
str "Use Recursive Extraction to get the whole environment.") |
| 477 |
|
| 478 |
let error_MPfile_as_mod mp b = |
| 479 |
let s1 = if b then "asked" else "required" in |
| 480 |
let s2 = if b then "extract some objects of this module or\n" else "" in |
| 481 |
err (str ("Extraction of file "^(raw_string_of_modfile mp)^ |
| 482 |
".v as a module is "^s1^".\n"^ |
| 483 |
"Monolithic Extraction cannot deal with this situation.\n"^ |
| 484 |
"Please "^s2^"use (Recursive) Extraction Library instead.\n")) |
| 485 |
|
| 486 |
let argnames_of_global r = |
| 487 |
let env = Global.env () in |
| 488 |
let typ, _ = Typeops.type_of_global_in_context env r.glob in |
| 489 |
let rels,_ = |
| 490 |
decompose_prod (Reduction.whd_all env typ) in |
| 491 |
List.rev_map (fun x -> Context.binder_name (fst x)) rels |
| 492 |
|
| 493 |
let msg_of_implicit = function |
| 494 |
| Kimplicit (r,i) -> |
| 495 |
let name = match (List.nth (argnames_of_global r) (i-1)) with |
| 496 |
| Anonymous -> "" |
| 497 |
| Name id -> "(" ^ Id.to_string id ^ ") " |
| 498 |
in |
| 499 |
(String.ordinal i)^" argument "^name^"of "^(string_of_global r) |
| 500 |
| Ktype | Kprop -> "" |
| 501 |
|
| 502 |
let error_remaining_implicit k = |
| 503 |
let s = msg_of_implicit k in |
| 504 |
err (str ("An implicit occurs after extraction : "^s^".") ++ fnl () ++ |
| 505 |
str "Please check your Extraction Implicit declarations." ++ fnl() ++ |
| 506 |
str "You might also try Unset Extraction SafeImplicits to force" ++ |
| 507 |
fnl() ++ str "the extraction of unsafe code and review it manually.") |
| 508 |
|
| 509 |
let warn_extraction_remaining_implicit = |
| 510 |
CWarnings.create ~name:"extraction-remaining-implicit" ~category:CWarnings.CoreCategories.extraction |
| 511 |
(fun s -> strbrk ("At least an implicit occurs after extraction : "^s^".") ++ fnl () ++ |
| 512 |
strbrk "Extraction SafeImplicits is unset, extracting nonetheless," |
| 513 |
++ strbrk "but this code is potentially unsafe, please review it manually.") |
| 514 |
|
| 515 |
let warning_remaining_implicit k = |
| 516 |
let s = msg_of_implicit k in |
| 517 |
warn_extraction_remaining_implicit s |
| 518 |
|
| 519 |
let check_loaded_modfile mp = match base_mp mp with |
| 520 |
| MPfile dp -> |
| 521 |
if not (Library.library_is_loaded dp) then begin |
| 522 |
match base_mp (extraction_current_mp ()) with |
| 523 |
| MPfile dp' when not (DirPath.equal dp dp') -> |
| 524 |
err (str "Please load library " ++ DirPath.print dp ++ str " first.") |
| 525 |
| _ -> () |
| 526 |
end |
| 527 |
| _ -> () |
| 528 |
|
| 529 |
let info_file f = |
| 530 |
Flags.if_verbose Feedback.msg_info |
| 531 |
(str ("The file "^f^" has been created by extraction.")) |
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
let my_bool_option name value = |
| 540 |
let { Goptions.get } = |
| 541 |
declare_bool_option_and_ref |
| 542 |
~key:["Extraction"; name] |
| 543 |
~value |
| 544 |
() |
| 545 |
in |
| 546 |
get |
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
let warn_using_current_directory = |
| 551 |
CWarnings.(create ~name:"extraction-default-directory" ~category:CoreCategories.extraction) |
| 552 |
(fun s -> |
| 553 |
Pp.(strbrk |
| 554 |
"Setting extraction output directory by default to \"" ++ str s ++ strbrk "\". Use \"" ++ |
| 555 |
str "Set Extraction Output Directory" ++ |
| 556 |
strbrk "\" or command line option \"-output-directory\" to " ++ |
| 557 |
strbrk "set a different directory for extracted files to appear in.")) |
| 558 |
|
| 559 |
let output_directory_key = ["Extraction"; "Output"; "Directory"] |
| 560 |
|
| 561 |
let { Goptions.get = output_directory } = |
| 562 |
declare_stringopt_option_and_ref ~stage:Summary.Stage.Interp ~value:None |
| 563 |
~key:output_directory_key () |
| 564 |
|
| 565 |
let output_directory () = |
| 566 |
match output_directory (), !System.output_directory with |
| 567 |
| Some dir, _ | None, Some dir -> |
| 568 |
|
| 569 |
System.mkdir dir; |
| 570 |
dir |
| 571 |
| None, None -> |
| 572 |
let pwd = Sys.getcwd () in |
| 573 |
warn_using_current_directory pwd; |
| 574 |
|
| 575 |
set_string_option_value ~stage:Summary.Stage.Interp output_directory_key pwd; |
| 576 |
pwd |
| 577 |
|
| 578 |
|
| 579 |
|
| 580 |
let access_opaque = my_bool_option "AccessOpaque" true |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
let auto_inline = my_bool_option "AutoInline" false |
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
let type_expand = my_bool_option "TypeExpand" true |
| 589 |
|
| 590 |
|
| 591 |
|
| 592 |
let keep_singleton = my_bool_option "KeepSingleton" false |
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
type opt_flag = |
| 597 |
{ opt_kill_dum : bool; |
| 598 |
opt_fix_fun : bool; |
| 599 |
opt_case_iot : bool; |
| 600 |
opt_case_idr : bool; |
| 601 |
opt_case_idg : bool; |
| 602 |
opt_case_cst : bool; |
| 603 |
opt_case_fun : bool; |
| 604 |
opt_case_app : bool; |
| 605 |
opt_let_app : bool; |
| 606 |
opt_lin_let : bool; |
| 607 |
opt_lin_beta : bool } |
| 608 |
|
| 609 |
let kth_digit n k = not (Int.equal (n land (1 lsl k)) 0) |
| 610 |
|
| 611 |
let flag_of_int n = |
| 612 |
{ opt_kill_dum = kth_digit n 0; |
| 613 |
opt_fix_fun = kth_digit n 1; |
| 614 |
opt_case_iot = kth_digit n 2; |
| 615 |
opt_case_idr = kth_digit n 3; |
| 616 |
opt_case_idg = kth_digit n 4; |
| 617 |
opt_case_cst = kth_digit n 5; |
| 618 |
opt_case_fun = kth_digit n 6; |
| 619 |
opt_case_app = kth_digit n 7; |
| 620 |
opt_let_app = kth_digit n 8; |
| 621 |
opt_lin_let = kth_digit n 9; |
| 622 |
opt_lin_beta = kth_digit n 10 } |
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
let int_flag_init = 1 + 2 + 4 + 8 + 32 + 64 + 128 + 256 |
| 633 |
|
| 634 |
let int_flag_ref = ref int_flag_init |
| 635 |
let opt_flag_ref = ref (flag_of_int int_flag_init) |
| 636 |
|
| 637 |
let chg_flag n = int_flag_ref := n; opt_flag_ref := flag_of_int n |
| 638 |
|
| 639 |
let optims () = !opt_flag_ref |
| 640 |
|
| 641 |
let () = declare_option ~kind:BoolKind |
| 642 |
~no_summary:true |
| 643 |
{optstage = Summary.Stage.Interp; |
| 644 |
optdepr = None; |
| 645 |
optkey = ["Extraction"; "Optimize"]; |
| 646 |
optread = (fun () -> not (Int.equal !int_flag_ref 0)); |
| 647 |
optwrite = (fun b -> chg_flag (if b then int_flag_init else 0))} |
| 648 |
|
| 649 |
let () = declare_int_option |
| 650 |
{ optstage = Summary.Stage.Interp; |
| 651 |
optdepr = None; |
| 652 |
optkey = ["Extraction";"Flag"]; |
| 653 |
optread = (fun _ -> Some !int_flag_ref); |
| 654 |
optwrite = (function |
| 655 |
| None -> chg_flag 0 |
| 656 |
| Some i -> chg_flag (max i 0))} |
| 657 |
|
| 658 |
|
| 659 |
|
| 660 |
let { Goptions.get = conservative_types } = |
| 661 |
declare_bool_option_and_ref |
| 662 |
~key:["Extraction"; "Conservative"; "Types"] |
| 663 |
~value:false |
| 664 |
() |
| 665 |
|
| 666 |
|
| 667 |
let { Goptions.get = file_comment } = |
| 668 |
declare_string_option_and_ref |
| 669 |
~key:["Extraction"; "File"; "Comment"] |
| 670 |
~value:"" |
| 671 |
() |
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
type lang = Ocaml | Haskell | Scheme | JSON |
| 676 |
|
| 677 |
let lang_ref = Summary.ref Ocaml ~name:"ExtrLang" |
| 678 |
|
| 679 |
let lang () = !lang_ref |
| 680 |
|
| 681 |
let extr_lang : lang -> obj = |
| 682 |
declare_object @@ superglobal_object_nodischarge "Extraction Lang" |
| 683 |
~cache:(fun l -> lang_ref := l) |
| 684 |
~subst:None |
| 685 |
|
| 686 |
let extraction_language x = Lib.add_leaf (extr_lang x) |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
let empty_inline_table = (Refset'.empty,Refset'.empty) |
| 691 |
|
| 692 |
let inline_table = Summary.ref empty_inline_table ~name:"ExtrInline" |
| 693 |
|
| 694 |
let to_inline r = Refset'.mem r (fst !inline_table) |
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
let empty_foreign_set = Refset'.empty |
| 699 |
|
| 700 |
let foreign_set = Summary.ref empty_foreign_set ~name:"ExtrForeign" |
| 701 |
|
| 702 |
let to_foreign r = Refset'.mem r !foreign_set |
| 703 |
|
| 704 |
|
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
let empty_callback_map = Refmap'.empty |
| 710 |
|
| 711 |
let callback_map = Summary.ref empty_callback_map ~name:"ExtrCallback" |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
let to_keep r = Refset'.mem r (snd !inline_table) |
| 716 |
|
| 717 |
let add_inline_entries b l = |
| 718 |
let f b = if b then Refset'.add else Refset'.remove in |
| 719 |
let i,k = !inline_table in |
| 720 |
inline_table := |
| 721 |
(List.fold_right (f b) l i), |
| 722 |
(List.fold_right (f (not b)) l k) |
| 723 |
|
| 724 |
let add_foreign_entries l = |
| 725 |
foreign_set := List.fold_right (Refset'.add) l !foreign_set |
| 726 |
|
| 727 |
|
| 728 |
let add_callback_entry alias_opt qualid_ref = |
| 729 |
callback_map := Refmap'.add qualid_ref alias_opt !callback_map |
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
let subst_global s g = { glob = fst (subst_global s g.glob); inst = g.inst } |
| 734 |
|
| 735 |
let inline_extraction : bool * global list -> obj = |
| 736 |
declare_object @@ superglobal_object "Extraction Inline" |
| 737 |
~cache:(fun (b,l) -> add_inline_entries b l) |
| 738 |
~subst:(Some (fun (s,(b,l)) -> (b,(List.map (fun x -> subst_global s x) l)))) |
| 739 |
~discharge:(fun x -> Some x) |
| 740 |
|
| 741 |
let foreign_extraction : global list -> obj = |
| 742 |
declare_object @@ superglobal_object "Extraction Foreign" |
| 743 |
~cache:(fun l -> add_foreign_entries l) |
| 744 |
~subst:(Some (fun (s,l) -> (List.map (fun x -> subst_global s x) l))) |
| 745 |
~discharge:(fun x -> Some x) |
| 746 |
|
| 747 |
let callback_extraction : string option * global -> obj = |
| 748 |
declare_object @@ superglobal_object "Extraction Callback" |
| 749 |
~cache:(fun (alias, x) -> add_callback_entry alias x) |
| 750 |
~subst:(Some (fun (s,(alias, x)) -> (alias, subst_global s x))) |
| 751 |
~discharge:(fun x -> Some x) |
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
let mono_global_with_alias qid = |
| 758 |
let gr = Smartlocate.global_with_alias qid in |
| 759 |
let inst = Environ.universes_of_global (Global.env ()) gr in |
| 760 |
List.map (fun inst -> { glob = gr; inst }) (InfvInst.generate inst) |
| 761 |
|
| 762 |
let extraction_inline b l = |
| 763 |
let refs = List.map_append mono_global_with_alias l in |
| 764 |
List.iter |
| 765 |
(fun r -> match r.glob with |
| 766 |
| GlobRef.ConstRef _ -> () |
| 767 |
| _ -> error_constant r) refs; |
| 768 |
Lib.add_leaf (inline_extraction (b,refs)) |
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
let print_extraction_inline () = |
| 773 |
let (i,n)= !inline_table in |
| 774 |
let i'= Refset'.filter (function { glob = GlobRef.ConstRef _ } -> true | _ -> false) i in |
| 775 |
(str "Extraction Inline:" ++ fnl () ++ |
| 776 |
Refset'.fold |
| 777 |
(fun r p -> |
| 778 |
(p ++ str " " ++ safe_pr_long_global r ++ fnl ())) i' (mt ()) ++ |
| 779 |
str "Extraction NoInline:" ++ fnl () ++ |
| 780 |
Refset'.fold |
| 781 |
(fun r p -> |
| 782 |
(p ++ str " " ++ safe_pr_long_global r ++ fnl ())) n (mt ())) |
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
let reset_inline : unit -> obj = |
| 787 |
declare_object @@ superglobal_object_nodischarge "Reset Extraction Inline" |
| 788 |
~cache:(fun () -> inline_table := empty_inline_table) |
| 789 |
~subst:None |
| 790 |
|
| 791 |
let reset_foreign : unit -> obj = |
| 792 |
declare_object @@ superglobal_object_nodischarge "Reset Extraction Foreign" |
| 793 |
~cache:(fun () -> foreign_set := empty_foreign_set) |
| 794 |
~subst:None |
| 795 |
|
| 796 |
let reset_callback : unit -> obj = |
| 797 |
declare_object @@ superglobal_object_nodischarge "Reset Extraction Callback" |
| 798 |
~cache:(fun () -> callback_map := empty_callback_map) |
| 799 |
~subst:None |
| 800 |
|
| 801 |
let reset_extraction_inline () = Lib.add_leaf (reset_inline ()) |
| 802 |
|
| 803 |
let reset_extraction_foreign () = Lib.add_leaf (reset_foreign ()) |
| 804 |
|
| 805 |
let reset_extraction_callback () = Lib.add_leaf (reset_callback ()) |
| 806 |
|
| 807 |
|
| 808 |
|
| 809 |
let safe_implicit = my_bool_option "SafeImplicits" true |
| 810 |
|
| 811 |
let err_or_warn_remaining_implicit k = |
| 812 |
if safe_implicit () then |
| 813 |
error_remaining_implicit k |
| 814 |
else |
| 815 |
warning_remaining_implicit k |
| 816 |
|
| 817 |
type int_or_id = ArgInt of int | ArgId of Id.t |
| 818 |
|
| 819 |
let implicits_table = Summary.ref Refmap'.empty ~name:"ExtrImplicit" |
| 820 |
|
| 821 |
let implicits_of_global r = |
| 822 |
try Refmap'.find r !implicits_table with Not_found -> Int.Set.empty |
| 823 |
|
| 824 |
let add_implicits r l = |
| 825 |
let names = argnames_of_global r in |
| 826 |
let n = List.length names in |
| 827 |
let add_arg s = function |
| 828 |
| ArgInt i -> |
| 829 |
if 1 <= i && i <= n then Int.Set.add i s |
| 830 |
else err (int i ++ str " is not a valid argument number for " ++ |
| 831 |
safe_pr_global r) |
| 832 |
| ArgId id -> |
| 833 |
try |
| 834 |
let i = List.index Name.equal (Name id) names in |
| 835 |
Int.Set.add i s |
| 836 |
with Not_found -> |
| 837 |
err (str "No argument " ++ Id.print id ++ str " for " ++ |
| 838 |
safe_pr_global r) |
| 839 |
in |
| 840 |
let ints = List.fold_left add_arg Int.Set.empty l in |
| 841 |
implicits_table := Refmap'.add r ints !implicits_table |
| 842 |
|
| 843 |
|
| 844 |
|
| 845 |
let implicit_extraction : global * int_or_id list -> obj = |
| 846 |
declare_object @@ superglobal_object_nodischarge "Extraction Implicit" |
| 847 |
~cache:(fun (r,l) -> add_implicits r l) |
| 848 |
~subst:(Some (fun (s,(r,l)) -> (subst_global s r, l))) |
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
let extraction_implicit r l = |
| 853 |
check_inside_section (); |
| 854 |
let r = mono_global_with_alias r in |
| 855 |
List.iter (fun r -> Lib.add_leaf (implicit_extraction (r, l))) r |
| 856 |
|
| 857 |
let string_of_modfile table mp = |
| 858 |
try DirPath.Map.find mp !table.modfile_mps |
| 859 |
with Not_found -> |
| 860 |
let id = Id.of_string (raw_string_of_modfile (MPfile mp)) in |
| 861 |
let id' = next_ident_away id !table.modfile_ids in |
| 862 |
let s' = Id.to_string id' in |
| 863 |
table := { !table with |
| 864 |
modfile_ids = Id.Set.add id' !table.modfile_ids; |
| 865 |
modfile_mps = DirPath.Map.add mp s' !table.modfile_mps; |
| 866 |
}; |
| 867 |
s' |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
let file_of_modfile table dp = |
| 872 |
let s0 = Id.to_string (List.hd (DirPath.repr dp)) in |
| 873 |
String.mapi (fun i c -> if i = 0 then s0.[0] else c) (string_of_modfile table dp) |
| 874 |
|
| 875 |
let add_blacklist_entries l = |
| 876 |
blacklist_table := |
| 877 |
List.fold_right (fun s -> Id.Set.add (Id.of_string (String.capitalize_ascii s))) |
| 878 |
l !blacklist_table |
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
let blacklist_extraction : string list -> obj = |
| 883 |
declare_object @@ superglobal_object_nodischarge "Extraction Blacklist" |
| 884 |
~cache:add_blacklist_entries |
| 885 |
~subst:None |
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
let extraction_blacklist l = |
| 890 |
let l = List.rev l in |
| 891 |
Lib.add_leaf (blacklist_extraction l) |
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
let print_extraction_blacklist () = |
| 896 |
prlist_with_sep fnl Id.print (Id.Set.elements !blacklist_table) |
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
let reset_blacklist : unit -> obj = |
| 901 |
declare_object @@ superglobal_object_nodischarge "Reset Extraction Blacklist" |
| 902 |
~cache:(fun ()-> blacklist_table := Id.Set.empty) |
| 903 |
~subst:None |
| 904 |
|
| 905 |
let reset_extraction_blacklist () = Lib.add_leaf (reset_blacklist ()) |
| 906 |
|
| 907 |
|
| 908 |
|
| 909 |
|
| 910 |
let (use_type_scheme_nb_args, type_scheme_nb_args_hook) = Hook.make () |
| 911 |
|
| 912 |
let customs = Summary.ref Refmap'.empty ~name:"ExtrCustom" |
| 913 |
|
| 914 |
let add_custom r ids s = customs := Refmap'.add r (ids,s) !customs |
| 915 |
|
| 916 |
let is_custom r = Refmap'.mem r !customs |
| 917 |
|
| 918 |
let is_inline_custom r = (is_custom r) && (to_inline r) |
| 919 |
|
| 920 |
let is_foreign_custom r = (is_custom r) && (to_foreign r) |
| 921 |
|
| 922 |
let find_callback r = Refmap'.find r !callback_map |
| 923 |
|
| 924 |
let find_custom r = snd (Refmap'.find r !customs) |
| 925 |
|
| 926 |
let find_type_custom r = Refmap'.find r !customs |
| 927 |
|
| 928 |
let custom_matchs = Summary.ref Refmap'.empty ~name:"ExtrCustomMatchs" |
| 929 |
|
| 930 |
let add_custom_match r s = |
| 931 |
custom_matchs := Refmap'.add r s !custom_matchs |
| 932 |
|
| 933 |
let indref_of_match pv = |
| 934 |
if Array.is_empty pv then raise Not_found; |
| 935 |
let (_,pat,_) = pv.(0) in |
| 936 |
match pat with |
| 937 |
| Pusual { glob = GlobRef.ConstructRef (ip,_); inst } -> { glob = GlobRef.IndRef ip; inst } |
| 938 |
| Pcons ({ glob = GlobRef.ConstructRef (ip,_); inst }, _) -> { glob = GlobRef.IndRef ip; inst } |
| 939 |
| _ -> raise Not_found |
| 940 |
|
| 941 |
let is_custom_match pv = |
| 942 |
try Refmap'.mem (indref_of_match pv) !custom_matchs |
| 943 |
with Not_found -> false |
| 944 |
|
| 945 |
let find_custom_match pv = |
| 946 |
Refmap'.find (indref_of_match pv) !custom_matchs |
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
let print_constref_extractions ref_set val_lookup_f section_str = |
| 951 |
let i'= Refset'.filter (function { glob = GlobRef.ConstRef _ } -> true | _ -> false) ref_set in |
| 952 |
(str section_str ++ fnl () ++ |
| 953 |
Refset'.fold |
| 954 |
(fun r p -> |
| 955 |
(p ++ str " " ++ safe_pr_long_global r ++ str " => \"" ++ str (val_lookup_f r) ++ str "\"" ++ fnl ())) i' (mt ()) |
| 956 |
) |
| 957 |
|
| 958 |
let print_extraction_foreign () = |
| 959 |
print_constref_extractions !foreign_set (find_custom) "Extraction Foreign Constant:" |
| 960 |
|
| 961 |
let print_extraction_callback () = |
| 962 |
let keys = Refmap'.domain !callback_map in |
| 963 |
print_constref_extractions keys (fun r -> |
| 964 |
match find_callback r with |
| 965 |
| None -> "no custom alias" |
| 966 |
| Some s -> s) "Extraction Callbacks for Constants:" |
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
let in_customs : global * string list * string -> obj = |
| 971 |
declare_object @@ superglobal_object_nodischarge "ML extractions" |
| 972 |
~cache:(fun (r,ids,s) -> add_custom r ids s) |
| 973 |
~subst:(Some (fun (s,(r,ids,str)) -> (subst_global s r, ids, str))) |
| 974 |
|
| 975 |
let in_custom_matchs : global * string -> obj = |
| 976 |
declare_object @@ superglobal_object_nodischarge "ML extractions custom matches" |
| 977 |
~cache:(fun (r,s) -> add_custom_match r s) |
| 978 |
~subst:(Some (fun (subs,(r,s)) -> (subst_global subs r, s))) |
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
let extract_callback optstr x = |
| 983 |
if lang () != Ocaml then |
| 984 |
CErrors.user_err (Pp.str "Extract Callback is supported only for OCaml extraction."); |
| 985 |
|
| 986 |
let refs = mono_global_with_alias x in |
| 987 |
List.iter begin fun qualid_ref -> |
| 988 |
match qualid_ref.glob with |
| 989 |
|
| 990 |
| GlobRef.ConstRef _ -> Lib.add_leaf (callback_extraction (optstr, qualid_ref)) |
| 991 |
| _ -> error_constant ?loc:x.CAst.loc qualid_ref |
| 992 |
end refs |
| 993 |
|
| 994 |
let extract_constant_generic r ids s arity_handler (is_redef, redef_msg) extr_type = |
| 995 |
check_inside_section (); |
| 996 |
let g = mono_global_with_alias r in |
| 997 |
List.iter begin fun g -> match g.glob with |
| 998 |
| GlobRef.ConstRef kn -> |
| 999 |
let env = Global.env () in |
| 1000 |
|
| 1001 |
let typ, _ = Typeops.type_of_global_in_context env (GlobRef.ConstRef kn) in |
| 1002 |
let typ = Reduction.whd_all env typ in |
| 1003 |
if Reduction.is_arity env typ then arity_handler env typ g; |
| 1004 |
if is_redef g then |
| 1005 |
CErrors.user_err ((str "The term ") ++ safe_pr_long_global g ++ (str " is already defined as ") |
| 1006 |
++ (str redef_msg) ++ (str " custom constant.")); |
| 1007 |
Lib.add_leaf (extr_type g); |
| 1008 |
Lib.add_leaf (in_customs (g,ids,s)); |
| 1009 |
| _ -> error_constant ?loc:r.CAst.loc g |
| 1010 |
end g |
| 1011 |
|
| 1012 |
let extract_constant_inline inline r ids s = |
| 1013 |
let arity_handler env typ g = |
| 1014 |
let nargs = Hook.get use_type_scheme_nb_args env typ in |
| 1015 |
if not (Int.equal (List.length ids) nargs) then error_axiom_scheme ?loc:r.CAst.loc g nargs |
| 1016 |
in |
| 1017 |
extract_constant_generic r ids s (arity_handler) (is_foreign_custom, "foreign") (fun g -> inline_extraction (inline,[g])) |
| 1018 |
|
| 1019 |
|
| 1020 |
let extract_constant_foreign r s = |
| 1021 |
if lang () != Ocaml then |
| 1022 |
CErrors.user_err (Pp.str "Extract Foreign Constant is supported only for OCaml extraction."); |
| 1023 |
let arity_handler env typ g = |
| 1024 |
CErrors.user_err (Pp.str "Extract Foreign Constant is supported only for functions.") |
| 1025 |
in |
| 1026 |
extract_constant_generic r [] s (arity_handler) (is_inline_custom, "inline") (fun g -> foreign_extraction [g]) |
| 1027 |
|
| 1028 |
|
| 1029 |
let extract_inductive r s l optstr = |
| 1030 |
check_inside_section (); |
| 1031 |
let g = mono_global_with_alias r in |
| 1032 |
List.iter begin fun g -> |
| 1033 |
Dumpglob.add_glob ?loc:r.CAst.loc g.glob; |
| 1034 |
match g.glob with |
| 1035 |
| GlobRef.IndRef ((kn,i) as ip) -> |
| 1036 |
let mib = Global.lookup_mind kn in |
| 1037 |
let n = Array.length mib.mind_packets.(i).mind_consnames in |
| 1038 |
if not (Int.equal n (List.length l)) then error_nb_cons (); |
| 1039 |
Lib.add_leaf (inline_extraction (true,[g])); |
| 1040 |
Lib.add_leaf (in_customs (g,[],s)); |
| 1041 |
Option.iter (fun s -> Lib.add_leaf (in_custom_matchs (g,s))) |
| 1042 |
optstr; |
| 1043 |
List.iteri |
| 1044 |
(fun j s -> |
| 1045 |
let g = { glob = GlobRef.ConstructRef (ip,succ j); inst = g.inst } in |
| 1046 |
Lib.add_leaf (inline_extraction (true,[g])); |
| 1047 |
Lib.add_leaf (in_customs (g,[],s))) l |
| 1048 |
| _ -> error_inductive ?loc:r.CAst.loc g |
| 1049 |
end g |