| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
open Miniml |
| 12 |
open Constr |
| 13 |
open Declarations |
| 14 |
open Mod_declarations |
| 15 |
open Names |
| 16 |
open ModPath |
| 17 |
open Libnames |
| 18 |
open Pp |
| 19 |
open CErrors |
| 20 |
open Util |
| 21 |
open Table |
| 22 |
open Extraction |
| 23 |
open Modutil |
| 24 |
open Common |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
let toplevel_env () = |
| 31 |
let mp, struc = Safe_typing.flatten_env (Global.safe_env ()) in |
| 32 |
mp, List.rev struc |
| 33 |
|
| 34 |
let environment_until dir_opt = |
| 35 |
let rec parse = function |
| 36 |
| [] when Option.is_empty dir_opt -> [toplevel_env ()] |
| 37 |
| [] -> [] |
| 38 |
| d :: l -> |
| 39 |
let meb = |
| 40 |
Modops.destr_nofunctor (MPfile d) (mod_type @@ Global.lookup_module (MPfile d)) |
| 41 |
in |
| 42 |
match dir_opt with |
| 43 |
| Some d' when DirPath.equal d d' -> [MPfile d, meb] |
| 44 |
| _ -> (MPfile d, meb) :: (parse l) |
| 45 |
in parse (Library.loaded_libraries ()) |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
module type VISIT = sig |
| 52 |
type t |
| 53 |
|
| 54 |
val make : unit -> t |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
val add_mp_all : t -> ModPath.t -> unit |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
val add_ref : t -> global -> unit |
| 63 |
val add_kn : t -> KerName.t -> InfvInst.t -> unit |
| 64 |
val add_decl_deps : t -> ml_decl -> unit |
| 65 |
val add_spec_deps : t -> ml_spec -> unit |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
val needed_ind : t -> MutInd.t -> InfvInst.t -> bool |
| 70 |
val needed_cst : t -> Constant.t -> InfvInst.t -> bool |
| 71 |
val needed_mp : t -> ModPath.t -> bool |
| 72 |
val needed_mp_all : t -> ModPath.t -> bool |
| 73 |
end |
| 74 |
|
| 75 |
module Visit : VISIT = struct |
| 76 |
module KNOrd = |
| 77 |
struct |
| 78 |
type t = KerName.t * InfvInst.t |
| 79 |
let compare (kn1, i1) (kn2, i2) = |
| 80 |
let c = KerName.compare kn1 kn2 in |
| 81 |
if Int.equal c 0 then InfvInst.compare i1 i2 else c |
| 82 |
end |
| 83 |
module KNset = Set.Make(KNOrd) |
| 84 |
|
| 85 |
type t = |
| 86 |
{ mutable kn : KNset.t; |
| 87 |
mutable mp : ModPath.Set.t; |
| 88 |
mutable mp_all : ModPath.Set.t } |
| 89 |
|
| 90 |
let make () = { |
| 91 |
kn = KNset.empty; |
| 92 |
mp = ModPath.Set.empty; |
| 93 |
mp_all = ModPath.Set.empty; |
| 94 |
} |
| 95 |
|
| 96 |
let needed_ind v i inst = KNset.mem (MutInd.user i, inst) v.kn |
| 97 |
let needed_cst v c inst = KNset.mem (Constant.user c, inst) v.kn |
| 98 |
let needed_mp v mp = ModPath.Set.mem mp v.mp || ModPath.Set.mem mp v.mp_all |
| 99 |
let needed_mp_all v mp = ModPath.Set.mem mp v.mp_all |
| 100 |
let add_mp v mp = |
| 101 |
check_loaded_modfile mp; v.mp <- ModPath.Set.union (prefixes_mp mp) v.mp |
| 102 |
let add_mp_all v mp = |
| 103 |
check_loaded_modfile mp; |
| 104 |
v.mp <- ModPath.Set.union (prefixes_mp mp) v.mp; |
| 105 |
v.mp_all <- ModPath.Set.add mp v.mp_all |
| 106 |
let add_kn v kn inst = v.kn <- KNset.add (kn, inst) v.kn; add_mp v (KerName.modpath kn) |
| 107 |
let add_ref v r = let open GlobRef in match r.glob with |
| 108 |
| ConstRef c -> add_kn v (Constant.user c) r.inst |
| 109 |
| IndRef (ind,_) | ConstructRef ((ind,_),_) -> add_kn v (MutInd.user ind) r.inst |
| 110 |
| VarRef _ -> assert false |
| 111 |
let add_decl_deps v decl = |
| 112 |
decl_iter_references (fun kn -> add_ref v kn) (fun r -> add_ref v r) (fun r -> add_ref v r) decl |
| 113 |
let add_spec_deps v spec = |
| 114 |
spec_iter_references (fun r -> add_ref v r) (fun r -> add_ref v r) (fun r -> add_ref v r) spec |
| 115 |
end |
| 116 |
|
| 117 |
let get_mono_inst_univs = function |
| 118 |
| Monomorphic -> [InfvInst.empty] |
| 119 |
| Polymorphic uctx -> InfvInst.generate uctx |
| 120 |
|
| 121 |
let get_mono_inst = function |
| 122 |
| SFBconst cb -> get_mono_inst_univs cb.const_universes |
| 123 |
| SFBmind mib -> get_mono_inst_univs mib.mind_universes |
| 124 |
| SFBrules _ -> [InfvInst.empty] |
| 125 |
| SFBmodule _ | SFBmodtype _ -> assert false |
| 126 |
|
| 127 |
let add_field_label venv mp = function |
| 128 |
| (lab, (SFBconst _|SFBmind _ | SFBrules _ as f)) -> |
| 129 |
let insts = get_mono_inst f in |
| 130 |
List.iter (fun inst -> Visit.add_kn venv (KerName.make mp lab) inst) insts |
| 131 |
| (lab, (SFBmodule _|SFBmodtype _)) -> Visit.add_mp_all venv (MPdot (mp,lab)) |
| 132 |
|
| 133 |
let rec add_labels venv mp = function |
| 134 |
| MoreFunctor (_,_,m) -> add_labels venv mp m |
| 135 |
| NoFunctor sign -> List.iter (fun f -> add_field_label venv mp f) sign |
| 136 |
|
| 137 |
exception Impossible |
| 138 |
|
| 139 |
let check_arity env cb = |
| 140 |
let t = cb.const_type in |
| 141 |
if Reduction.is_arity env t then raise Impossible |
| 142 |
|
| 143 |
let get_body lbody = |
| 144 |
EConstr.of_constr lbody |
| 145 |
|
| 146 |
let check_fix env sg cb i = |
| 147 |
match cb.const_body with |
| 148 |
| Def lbody -> |
| 149 |
(match EConstr.kind sg (get_body lbody) with |
| 150 |
| Fix ((_,j),recd) when Int.equal i j -> check_arity env cb; (true,recd) |
| 151 |
| CoFix (j,recd) when Int.equal i j -> check_arity env cb; (false,recd) |
| 152 |
| _ -> raise Impossible) |
| 153 |
| Undef _ | OpaqueDef _ | Primitive _ | Symbol _ -> raise Impossible |
| 154 |
|
| 155 |
let prec_declaration_equal sg (na1, ca1, ta1) (na2, ca2, ta2) = |
| 156 |
Array.equal (Context.eq_annot Name.equal (EConstr.ERelevance.equal sg)) na1 na2 && |
| 157 |
Array.equal (EConstr.eq_constr sg) ca1 ca2 && |
| 158 |
Array.equal (EConstr.eq_constr sg) ta1 ta2 |
| 159 |
|
| 160 |
let factor_fix env sg l cb msb = |
| 161 |
let _,recd as check = check_fix env sg cb 0 in |
| 162 |
let n = Array.length (let fi,_,_ = recd in fi) in |
| 163 |
if Int.equal n 1 then [|l|], recd, msb |
| 164 |
else begin |
| 165 |
if List.length msb < n-1 then raise Impossible; |
| 166 |
let msb', msb'' = List.chop (n-1) msb in |
| 167 |
let labels = Array.make n l in |
| 168 |
List.iteri |
| 169 |
(fun j -> |
| 170 |
function |
| 171 |
| (l,SFBconst cb') -> |
| 172 |
let check' = check_fix env sg cb' (j+1) in |
| 173 |
if not ((fst check : bool) == (fst check') && |
| 174 |
prec_declaration_equal sg (snd check) (snd check')) |
| 175 |
then raise Impossible; |
| 176 |
labels.(j+1) <- l; |
| 177 |
| _ -> raise Impossible) msb'; |
| 178 |
labels, recd, msb'' |
| 179 |
end |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
let vm_state = |
| 187 |
|
| 188 |
let vm_handler _ _ _ () = (), None in |
| 189 |
((), { Mod_typing.vm_handler }) |
| 190 |
|
| 191 |
let expand_mexpr env mp me = |
| 192 |
let inl = Declaremods.default_inline_level() in |
| 193 |
|
| 194 |
|
| 195 |
let mp0 = ModPath.dummy in |
| 196 |
let state = ((Environ.universes env, Univ.UnivConstraints.empty), Reductionops.inferred_universes) in |
| 197 |
let mb, (_, cst), _ = Mod_typing.translate_module state vm_state env mp0 inl (MExpr ([], me, None)) in |
| 198 |
let sign = mod_type mb in |
| 199 |
let reso = mod_delta mb in |
| 200 |
Modops.subst_modtype_signature_and_resolver mp0 mp sign reso |
| 201 |
|
| 202 |
let expand_modtype env mp me = |
| 203 |
let inl = Declaremods.default_inline_level () in |
| 204 |
let state = ((Environ.universes env, Univ.UnivConstraints.empty), Reductionops.inferred_universes) in |
| 205 |
let mtb, _cst, _ = Mod_typing.translate_modtype state vm_state env mp inl ([],me) in |
| 206 |
mtb |
| 207 |
|
| 208 |
let no_delta = Mod_subst.empty_delta_resolver |
| 209 |
|
| 210 |
let flatten_modtype env mp me_alg struc_opt = |
| 211 |
match struc_opt with |
| 212 |
| Some me -> me, no_delta mp |
| 213 |
| None -> |
| 214 |
let mtb = expand_modtype env mp me_alg in |
| 215 |
mod_type mtb, mod_delta mtb |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
let env_for_mtb_with_def env mp me reso idl = |
| 221 |
let struc = Modops.destr_nofunctor mp me in |
| 222 |
let l = List.hd idl in |
| 223 |
let spot = function (l',SFBconst _) -> Id.equal l l' | _ -> false in |
| 224 |
let before = fst (List.split_when spot struc) in |
| 225 |
Modops.add_structure mp before reso env |
| 226 |
|
| 227 |
let make_cst resolver mp l = |
| 228 |
Mod_subst.constant_of_delta_kn resolver (KerName.make mp l) |
| 229 |
|
| 230 |
let make_mind resolver mp l = |
| 231 |
Mod_subst.mind_of_delta_kn resolver (KerName.make mp l) |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
let rec extract_structure_spec table venv env mp reso = function |
| 243 |
| [] -> [] |
| 244 |
| (l, SFBconst cb) :: msig -> |
| 245 |
let insts = get_mono_inst_univs cb.const_universes in |
| 246 |
let c = make_cst reso mp l in |
| 247 |
let map inst = extract_constant_spec table env c inst cb in |
| 248 |
let consts = List.map map insts in |
| 249 |
let specs = extract_structure_spec table venv env mp reso msig in |
| 250 |
let fold s specs = |
| 251 |
if logical_spec s then specs |
| 252 |
else |
| 253 |
let () = Visit.add_spec_deps venv s in |
| 254 |
(l, Spec s) :: specs |
| 255 |
in |
| 256 |
List.fold_right fold consts specs |
| 257 |
| (l, SFBmind mib) :: msig -> |
| 258 |
let insts = get_mono_inst_univs mib.mind_universes in |
| 259 |
let mind = make_mind reso mp l in |
| 260 |
let map inst = Sind (extract_inductive table env mind inst) in |
| 261 |
let minds = List.map map insts in |
| 262 |
let specs = extract_structure_spec table venv env mp reso msig in |
| 263 |
let fold s specs = |
| 264 |
if logical_spec s then specs |
| 265 |
else |
| 266 |
let () = Visit.add_spec_deps venv s in |
| 267 |
(l, Spec s) :: specs |
| 268 |
in |
| 269 |
List.fold_right fold minds specs |
| 270 |
| (l, SFBrules _) :: msig -> |
| 271 |
let specs = extract_structure_spec table venv env mp reso msig in |
| 272 |
specs |
| 273 |
| (l,SFBmodule mb) :: msig -> |
| 274 |
let specs = extract_structure_spec table venv env mp reso msig in |
| 275 |
let mp = MPdot (mp, l) in |
| 276 |
let spec = extract_mbody_spec table venv env mp mb in |
| 277 |
(l,Smodule spec) :: specs |
| 278 |
| (l,SFBmodtype mtb) :: msig -> |
| 279 |
let specs = extract_structure_spec table venv env mp reso msig in |
| 280 |
let mp = MPdot (mp, l) in |
| 281 |
let spec = extract_mbody_spec table venv env mp mtb in |
| 282 |
(l,Smodtype spec) :: specs |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
and extract_mexpr_spec table venv env mp1 (me_struct_o,me_alg) = match me_alg with |
| 291 |
| MEident mp -> |
| 292 |
let () = Visit.add_mp_all venv mp in |
| 293 |
MTident mp |
| 294 |
| MEwith(me',WithDef(idl,(c,ctx)))-> |
| 295 |
let () = match ctx with |
| 296 |
| None -> () |
| 297 |
| Some auctx -> |
| 298 |
|
| 299 |
if Array.is_empty (UVars.AbstractContext.names auctx).quals then () |
| 300 |
else user_err Pp.(str "Extraction of \"with Definition\" clauses not supported for sort polymorphic definitions.") |
| 301 |
in |
| 302 |
let me_struct,delta = flatten_modtype env mp1 me' me_struct_o in |
| 303 |
let env' = env_for_mtb_with_def env mp1 me_struct delta idl in |
| 304 |
let mt = extract_mexpr_spec table venv env mp1 (None,me') in |
| 305 |
let sg = Evd.from_env env in |
| 306 |
(match extract_with_type table env' sg (EConstr.of_constr c) with |
| 307 |
|
| 308 |
| None -> mt |
| 309 |
| Some (vl,typ) -> |
| 310 |
let () = type_iter_references (fun r -> Visit.add_ref venv r) typ in |
| 311 |
MTwith (mt, ML_With_type (InfvInst.empty, idl, vl, typ))) |
| 312 |
| MEwith(me',WithMod(idl,mp))-> |
| 313 |
let () = Visit.add_mp_all venv mp in |
| 314 |
MTwith (extract_mexpr_spec table venv env mp1 (None, me'), ML_With_module(idl, mp)) |
| 315 |
| MEapply _ -> |
| 316 |
|
| 317 |
let me_struct,delta = flatten_modtype env mp1 me_alg me_struct_o in |
| 318 |
extract_msignature_spec table venv env mp1 delta me_struct |
| 319 |
|
| 320 |
and extract_mexpression_spec table venv env mp1 (me_struct,me_alg) = match me_alg with |
| 321 |
| MEMoreFunctor me_alg' -> |
| 322 |
let mbid, mtb, me_struct' = match me_struct with |
| 323 |
| MoreFunctor (mbid, mtb, me') -> (mbid, mtb, me') |
| 324 |
| _ -> assert false |
| 325 |
in |
| 326 |
let mp = MPbound mbid in |
| 327 |
let env' = Environ.Internal.overwrite_module_parameter mbid mtb env in |
| 328 |
MTfunsig (mbid, extract_mbody_spec table venv env mp mtb, |
| 329 |
extract_mexpression_spec table venv env' mp1 (me_struct', me_alg')) |
| 330 |
| MENoFunctor m -> extract_mexpr_spec table venv env mp1 (Some me_struct, m) |
| 331 |
|
| 332 |
and extract_msignature_spec table venv env mp1 reso = function |
| 333 |
| NoFunctor struc -> |
| 334 |
let env' = Environ.Internal.overwrite_structure mp1 struc reso env in |
| 335 |
MTsig (mp1, extract_structure_spec table venv env' mp1 reso struc) |
| 336 |
| MoreFunctor (mbid, mtb, me) -> |
| 337 |
let mp = MPbound mbid in |
| 338 |
let env' = Environ.Internal.overwrite_module_parameter mbid mtb env in |
| 339 |
MTfunsig (mbid, extract_mbody_spec table venv env mp mtb, |
| 340 |
extract_msignature_spec table venv env' mp1 reso me) |
| 341 |
|
| 342 |
and extract_mbody_spec : 'a. State.t -> _ -> _ -> _ -> 'a generic_module_body -> _ = |
| 343 |
fun table venv env mp mb -> match mod_type_alg mb with |
| 344 |
| Some ty -> extract_mexpression_spec table venv env mp (mod_type mb, ty) |
| 345 |
| None -> extract_msignature_spec table venv env mp (mod_delta mb) (mod_type mb) |
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
let rec extract_structure table access venv env mp reso ~all = function |
| 355 |
| [] -> [] |
| 356 |
| (l, SFBconst cb) :: struc -> |
| 357 |
let sg = Evd.from_env env in |
| 358 |
let fix, struc = match factor_fix env sg l cb struc with |
| 359 |
| (vl, recd, struc) -> Some (vl, recd), struc |
| 360 |
| exception Impossible -> None, struc |
| 361 |
in |
| 362 |
let ms = extract_structure table access venv env mp reso ~all struc in |
| 363 |
let insts = get_mono_inst_univs cb.const_universes in |
| 364 |
let c = make_cst reso mp l in |
| 365 |
let map inst = match fix with |
| 366 |
| None -> |
| 367 |
let b = Visit.needed_cst venv c inst in |
| 368 |
if all || b then |
| 369 |
let d = extract_constant table access env c inst cb in |
| 370 |
if (not b) && (logical_decl d) then None |
| 371 |
else |
| 372 |
let () = Visit.add_decl_deps venv d in |
| 373 |
Some (l, SEdecl d) |
| 374 |
else None |
| 375 |
| Some (vl, recd) -> |
| 376 |
let vc = Array.map (make_cst reso mp) vl in |
| 377 |
let b = Array.exists (fun vf -> Visit.needed_cst venv vf inst) vc in |
| 378 |
if all || b then |
| 379 |
let d = extract_fixpoint table env sg vc inst recd in |
| 380 |
if (not b) && (logical_decl d) then None |
| 381 |
else |
| 382 |
let () = Visit.add_decl_deps venv d in |
| 383 |
Some (l, SEdecl d) |
| 384 |
else None |
| 385 |
in |
| 386 |
let consts = List.map_filter map insts in |
| 387 |
consts @ ms |
| 388 |
| (l, SFBmind mib) :: struc -> |
| 389 |
let ms = extract_structure table access venv env mp reso ~all struc in |
| 390 |
let insts = get_mono_inst_univs mib.mind_universes in |
| 391 |
let mind = make_mind reso mp l in |
| 392 |
let map inst = |
| 393 |
let b = Visit.needed_ind venv mind inst in |
| 394 |
if all || b then |
| 395 |
let d = Dind (extract_inductive table env mind inst) in |
| 396 |
if (not b) && (logical_decl d) then None |
| 397 |
else |
| 398 |
let () = Visit.add_decl_deps venv d in |
| 399 |
Some (l, SEdecl d) |
| 400 |
else None |
| 401 |
in |
| 402 |
let inds = List.map_filter map insts in |
| 403 |
inds @ ms |
| 404 |
| (l, SFBrules rrb) :: struc -> |
| 405 |
let inst = InfvInst.empty in |
| 406 |
let b = List.exists (fun (cst, _) -> Visit.needed_cst venv cst inst) rrb.rewrules_rules in |
| 407 |
let ms = extract_structure table access venv env mp reso ~all struc in |
| 408 |
if all || b then begin |
| 409 |
List.iter (fun (cst, _) -> Table.add_symbol_rule (State.get_table table) { glob = ConstRef cst; inst } l) rrb.rewrules_rules; |
| 410 |
ms |
| 411 |
end else ms |
| 412 |
| (l,SFBmodule mb) :: struc -> |
| 413 |
let ms = extract_structure table access venv env mp reso ~all struc in |
| 414 |
let mp = MPdot (mp,l) in |
| 415 |
let all' = all || Visit.needed_mp_all venv mp in |
| 416 |
if all' || Visit.needed_mp venv mp then |
| 417 |
(l, SEmodule (extract_module table access venv env mp ~all:all' mb)) :: ms |
| 418 |
else ms |
| 419 |
| (l,SFBmodtype mtb) :: struc -> |
| 420 |
let ms = extract_structure table access venv env mp reso ~all struc in |
| 421 |
let mp = MPdot (mp,l) in |
| 422 |
if all || Visit.needed_mp venv mp then |
| 423 |
(l, SEmodtype (extract_mbody_spec table venv env mp mtb)) :: ms |
| 424 |
else ms |
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
and extract_mexpr table access venv env mp = function |
| 429 |
| MEwith _ -> assert false |
| 430 |
| me when lang () != Ocaml -> |
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
let sign, delta = expand_mexpr env mp me in |
| 435 |
extract_msignature table access venv env mp delta ~all:true sign |
| 436 |
| MEident mp -> |
| 437 |
if is_modfile mp && not (State.get_modular table) then error_MPfile_as_mod mp false; |
| 438 |
Visit.add_mp_all venv mp; Miniml.MEident mp |
| 439 |
| MEapply (me, arg) -> |
| 440 |
Miniml.MEapply (extract_mexpr table access venv env mp me, |
| 441 |
extract_mexpr table access venv env mp (MEident arg)) |
| 442 |
|
| 443 |
and extract_mexpression table access venv env mp mty = function |
| 444 |
| MENoFunctor me -> extract_mexpr table access venv env mp me |
| 445 |
| MEMoreFunctor me -> |
| 446 |
let (mbid, mtb, mty) = match mty with |
| 447 |
| MoreFunctor (mbid, mtb, mty) -> (mbid, mtb, mty) |
| 448 |
| NoFunctor _ -> assert false |
| 449 |
in |
| 450 |
let mp1 = MPbound mbid in |
| 451 |
let env' = Environ.Internal.overwrite_module_parameter mbid mtb env in |
| 452 |
Miniml.MEfunctor |
| 453 |
(mbid, |
| 454 |
extract_mbody_spec table venv env mp1 mtb, |
| 455 |
extract_mexpression table access venv env' mp mty me) |
| 456 |
|
| 457 |
and extract_msignature table access venv env mp reso ~all = function |
| 458 |
| NoFunctor struc -> |
| 459 |
let env' = Environ.Internal.overwrite_structure mp struc reso env in |
| 460 |
Miniml.MEstruct (mp, extract_structure table access venv env' mp reso ~all struc) |
| 461 |
| MoreFunctor (mbid, mtb, me) -> |
| 462 |
let mp1 = MPbound mbid in |
| 463 |
let env' = Environ.Internal.overwrite_module_parameter mbid mtb env in |
| 464 |
Miniml.MEfunctor |
| 465 |
(mbid, |
| 466 |
extract_mbody_spec table venv env mp1 mtb, |
| 467 |
extract_msignature table access venv env' mp reso ~all me) |
| 468 |
|
| 469 |
and extract_module table access venv env mp ~all mb = |
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
let impl = match Mod_declarations.mod_expr mb with |
| 477 |
| Abstract -> error_no_module_expr mp |
| 478 |
| Algebraic me -> extract_mexpression table access venv env mp (mod_type mb) me |
| 479 |
| Struct (reso, sign) -> |
| 480 |
|
| 481 |
|
| 482 |
let () = add_labels venv mp (mod_type mb) in |
| 483 |
let sign = Modops.annotate_struct_body sign (mod_type mb) in |
| 484 |
extract_msignature table access venv env mp reso ~all:false sign |
| 485 |
| FullStruct -> extract_msignature table access venv env mp (mod_delta mb) ~all (mod_type mb) |
| 486 |
in |
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
let typ = match Mod_declarations.mod_expr mb with |
| 491 |
| FullStruct -> |
| 492 |
assert (Option.is_empty @@ mod_type_alg mb); |
| 493 |
mtyp_of_mexpr impl |
| 494 |
| _ -> extract_mbody_spec table venv env mp mb |
| 495 |
in |
| 496 |
{ ml_mod_expr = impl; |
| 497 |
ml_mod_type = typ } |
| 498 |
|
| 499 |
let mono_environment table ~opaque_access refs mpl = |
| 500 |
let venv = Visit.make () in |
| 501 |
let () = List.iter (fun r -> Visit.add_ref venv r) refs in |
| 502 |
let () = List.iter (fun mp -> Visit.add_mp_all venv mp) mpl in |
| 503 |
let env = Global.env () in |
| 504 |
let l = List.rev (environment_until None) in |
| 505 |
List.rev_map |
| 506 |
(fun (mp,struc) -> |
| 507 |
mp, extract_structure table opaque_access venv env mp (no_delta mp) ~all:(Visit.needed_mp_all venv mp) struc) |
| 508 |
l |
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
let descr () = match lang () with |
| 515 |
| Ocaml -> Ocaml.ocaml_descr |
| 516 |
| Haskell -> Haskell.haskell_descr |
| 517 |
| Scheme -> Scheme.scheme_descr |
| 518 |
| JSON -> Json.json_descr |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
let default_id = Id.of_string "Main" |
| 524 |
|
| 525 |
let mono_filename f = |
| 526 |
let d = descr () in |
| 527 |
match f with |
| 528 |
| None -> None, None, default_id |
| 529 |
| Some f -> |
| 530 |
let f = |
| 531 |
if Filename.check_suffix f d.file_suffix then |
| 532 |
Filename.chop_suffix f d.file_suffix |
| 533 |
else f |
| 534 |
in |
| 535 |
let id = |
| 536 |
if lang () != Haskell then default_id |
| 537 |
else |
| 538 |
try Id.of_string (Filename.basename f) |
| 539 |
with UserError _ -> |
| 540 |
user_err Pp.(str "Extraction: provided filename is not a valid identifier") |
| 541 |
in |
| 542 |
let f = |
| 543 |
if Filename.is_relative f then |
| 544 |
Filename.concat (output_directory ()) f |
| 545 |
else f |
| 546 |
in |
| 547 |
Some (f^d.file_suffix), Option.map ((^) f) d.sig_suffix, id |
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
let module_filename table mp = |
| 552 |
let f = file_of_modfile (State.get_table table) mp in |
| 553 |
let id = Id.of_string f in |
| 554 |
let f = Filename.concat (output_directory ()) f in |
| 555 |
let d = descr () in |
| 556 |
let fimpl_base = d.file_naming table mp ^ d.file_suffix in |
| 557 |
let fimpl = Filename.concat (output_directory ()) fimpl_base in |
| 558 |
Some fimpl, Option.map ((^) f) d.sig_suffix, id |
| 559 |
|
| 560 |
|
| 561 |
|
| 562 |
let print_one_decl table struc mp decl = |
| 563 |
let d = descr () in |
| 564 |
let () = State.reset table in |
| 565 |
let table = State.set_phase table Pre in |
| 566 |
ignore (d.pp_struct table struc); |
| 567 |
let table = State.set_phase table Impl in |
| 568 |
let ans = State.with_visibility table mp [] begin fun table -> |
| 569 |
d.pp_decl table decl |
| 570 |
end in |
| 571 |
v 0 ans |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
|
| 578 |
let formatter buf dry file = |
| 579 |
let ft = |
| 580 |
if dry then Format.make_formatter (fun _ _ _ -> ()) (fun _ -> ()) |
| 581 |
else |
| 582 |
match file with |
| 583 |
| Some f -> Topfmt.with_output_to f |
| 584 |
| None -> Format.formatter_of_buffer buf |
| 585 |
in |
| 586 |
|
| 587 |
|
| 588 |
Format.pp_set_max_boxes ft max_int; |
| 589 |
|
| 590 |
(match Topfmt.get_margin () with |
| 591 |
| None -> () |
| 592 |
| Some i -> |
| 593 |
Format.pp_set_margin ft i; |
| 594 |
Format.pp_set_max_indent ft (i-10)); |
| 595 |
|
| 596 |
ft |
| 597 |
|
| 598 |
let get_comment () = |
| 599 |
let s = file_comment () in |
| 600 |
if String.is_empty s then None |
| 601 |
else |
| 602 |
let split_comment = Str.split (Str.regexp "[ \t\n]+") s in |
| 603 |
Some (prlist_with_sep spc str split_comment) |
| 604 |
|
| 605 |
let print_structure_to_file table (fn,si,mo) dry struc = |
| 606 |
let buf = Buffer.create 1000 in |
| 607 |
let d = descr () in |
| 608 |
let () = State.reset table in |
| 609 |
let unsafe_needs = { |
| 610 |
mldummy = struct_ast_search Mlutil.isMLdummy struc; |
| 611 |
tdummy = struct_type_search Mlutil.isTdummy struc; |
| 612 |
tunknown = struct_type_search ((==) Tunknown) struc; |
| 613 |
magic = |
| 614 |
if lang () != Haskell then false |
| 615 |
else struct_ast_search (function MLmagic _ -> true | _ -> false) struc } |
| 616 |
in |
| 617 |
|
| 618 |
let table = State.set_phase table Pre in |
| 619 |
ignore (d.pp_struct table struc); |
| 620 |
let opened = opened_libraries table in |
| 621 |
|
| 622 |
let cout = if dry then None else Option.map open_out fn in |
| 623 |
let ft = formatter buf dry cout in |
| 624 |
let comment = get_comment () in |
| 625 |
begin try |
| 626 |
|
| 627 |
let table = State.set_phase table Impl in |
| 628 |
pp_with ft (d.preamble table mo comment opened unsafe_needs); |
| 629 |
pp_with ft (d.pp_struct table struc); |
| 630 |
Format.pp_print_flush ft (); |
| 631 |
Option.iter close_out cout; |
| 632 |
with reraise -> |
| 633 |
Format.pp_print_flush ft (); |
| 634 |
Option.iter close_out cout; raise reraise |
| 635 |
end; |
| 636 |
if not dry then Option.iter info_file fn; |
| 637 |
|
| 638 |
Option.iter |
| 639 |
(fun si -> |
| 640 |
let cout = open_out si in |
| 641 |
let ft = formatter buf false (Some cout) in |
| 642 |
begin try |
| 643 |
let table = State.set_phase table Intf in |
| 644 |
pp_with ft (d.sig_preamble table mo comment opened unsafe_needs); |
| 645 |
pp_with ft (d.pp_sig table (signature_of_structure struc)); |
| 646 |
Format.pp_print_flush ft (); |
| 647 |
close_out cout; |
| 648 |
with reraise -> |
| 649 |
Format.pp_print_flush ft (); |
| 650 |
close_out cout; raise reraise |
| 651 |
end; |
| 652 |
info_file si) |
| 653 |
(if dry then None else si); |
| 654 |
|
| 655 |
if not (Int.equal (Buffer.length buf) 0) then begin |
| 656 |
Feedback.msg_notice (str (Buffer.contents buf)); |
| 657 |
end |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
let init ?(inner=false) ~modular ~library () = |
| 665 |
if not inner then check_inside_section (); |
| 666 |
let keywords = (descr ()).keywords in |
| 667 |
let state = State.make ~modular ~library ~keywords () in |
| 668 |
if modular && lang () == Scheme then error_scheme (); |
| 669 |
state |
| 670 |
|
| 671 |
let warns table = |
| 672 |
let table = State.get_table table in |
| 673 |
warning_opaques table (access_opaque ()); |
| 674 |
warning_axioms table |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
let rec locate_ref = function |
| 680 |
| [] -> [],[] |
| 681 |
| qid::l -> |
| 682 |
let mpo = try Some (Nametab.locate_module qid) with Not_found -> None |
| 683 |
and ro = |
| 684 |
try |
| 685 |
let gr = Smartlocate.global_with_alias qid in |
| 686 |
let inst = Environ.universes_of_global (Global.env ()) gr in |
| 687 |
Some (List.map (fun inst -> { glob = gr; inst }) (InfvInst.generate inst)) |
| 688 |
with Nametab.GlobalizationError _ | UserError _ -> None |
| 689 |
in |
| 690 |
match mpo, ro with |
| 691 |
| None, None -> Nametab.error_global_not_found ~info:Exninfo.null qid |
| 692 |
| None, Some r -> |
| 693 |
let refs, mps = locate_ref l in r @ refs,mps |
| 694 |
| Some mp, None -> let refs,mps = locate_ref l in refs,mp::mps |
| 695 |
| Some mp, Some r -> |
| 696 |
let () = warning_ambiguous_name ?loc:qid.CAst.loc (qid, mp, (List.hd r).glob) in |
| 697 |
let refs,mps = locate_ref l in refs,mp::mps |
| 698 |
|
| 699 |
|
| 700 |
|
| 701 |
|
| 702 |
|
| 703 |
|
| 704 |
let full_extr opaque_access f (refs,mps) = |
| 705 |
let table = init ~modular:false ~library:false () in |
| 706 |
List.iter (fun mp -> if is_modfile mp then error_MPfile_as_mod mp true) mps; |
| 707 |
let struc = optimize_struct table (refs,mps) (mono_environment table ~opaque_access refs mps) in |
| 708 |
let () = warns table in |
| 709 |
print_structure_to_file table (mono_filename f) false struc |
| 710 |
|
| 711 |
let full_extraction ~opaque_access f lr = |
| 712 |
full_extr opaque_access f (locate_ref lr) |
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
let separate_extraction ~opaque_access lr = |
| 718 |
let table = init ~modular:true ~library:false () in |
| 719 |
let refs,mps = locate_ref lr in |
| 720 |
let struc = optimize_struct table (refs,mps) (mono_environment table ~opaque_access refs mps) in |
| 721 |
let () = List.iter (function |
| 722 |
| MPfile _, _ -> () |
| 723 |
| (MPdot _ | MPbound _), _ -> |
| 724 |
user_err (str "Separate Extraction from inside a module is not supported.")) |
| 725 |
struc |
| 726 |
in |
| 727 |
let () = warns table in |
| 728 |
let print = function |
| 729 |
| (MPfile dir, sel) as e -> |
| 730 |
print_structure_to_file table (module_filename table dir) false [e] |
| 731 |
| (MPdot _ | MPbound _), _ -> assert false |
| 732 |
in |
| 733 |
let () = List.iter print struc in |
| 734 |
() |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
let simple_extraction ~opaque_access r = |
| 740 |
match locate_ref [r] with |
| 741 |
| ([], [mp]) as p -> full_extr opaque_access None p |
| 742 |
| [r],[] -> |
| 743 |
let table = init ~modular:false ~library:false () in |
| 744 |
let struc = optimize_struct table ([r],[]) (mono_environment table ~opaque_access [r] []) in |
| 745 |
let d = get_decl_in_structure r struc in |
| 746 |
let () = warns table in |
| 747 |
let flag = |
| 748 |
if is_custom r then str "(** User defined extraction *)" ++ fnl() |
| 749 |
else mt () |
| 750 |
in |
| 751 |
let ans = flag ++ print_one_decl table struc (modpath_of_r r) d in |
| 752 |
Feedback.msg_notice ans |
| 753 |
| _ -> assert false |
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
let extraction_library ~opaque_access is_rec CAst.{loc;v=m} = |
| 760 |
let table = init ~modular:true ~library:true () in |
| 761 |
let dir_m = |
| 762 |
|
| 763 |
let q = qualid_of_ident m in |
| 764 |
try Nametab.full_name_module q with Not_found -> error_unknown_module ?loc q |
| 765 |
in |
| 766 |
let dir_m = dirpath_of_path dir_m in |
| 767 |
let venv = Visit.make () in |
| 768 |
let () = Visit.add_mp_all venv (MPfile dir_m) in |
| 769 |
let env = Global.env () in |
| 770 |
let l = List.rev (environment_until (Some dir_m)) in |
| 771 |
let select l (mp,struc) = |
| 772 |
if Visit.needed_mp venv mp |
| 773 |
then (mp, extract_structure table opaque_access venv env mp (no_delta mp) ~all:true struc) :: l |
| 774 |
else l |
| 775 |
in |
| 776 |
let struc = List.fold_left select [] l in |
| 777 |
let struc = optimize_struct table ([],[]) struc in |
| 778 |
let () = warns table in |
| 779 |
let print = function |
| 780 |
| (MPfile dir, sel) as e -> |
| 781 |
let dry = not is_rec && not (DirPath.equal dir dir_m) in |
| 782 |
print_structure_to_file table (module_filename table dir) dry [e] |
| 783 |
| _ -> assert false |
| 784 |
in |
| 785 |
let () = List.iter print struc in |
| 786 |
() |
| 787 |
|
| 788 |
|
| 789 |
|
| 790 |
|
| 791 |
let compile f = |
| 792 |
try |
| 793 |
let args = [ "ocamlc" |
| 794 |
; "-package";"zarith" |
| 795 |
; "-I"; Filename.dirname f |
| 796 |
; "-c"; f^"i" |
| 797 |
; f |
| 798 |
] in |
| 799 |
let res = CUnix.sys_command (Boot.Env.ocamlfind ()) args in |
| 800 |
match res with |
| 801 |
| Unix.WEXITED 0 -> () |
| 802 |
| Unix.WEXITED n | Unix.WSIGNALED n | Unix.WSTOPPED n -> |
| 803 |
CErrors.user_err |
| 804 |
Pp.(str "Compilation of file " ++ str f ++ |
| 805 |
str " failed with exit code " ++ int n) |
| 806 |
with Unix.Unix_error (e,_,_) -> |
| 807 |
CErrors.user_err |
| 808 |
Pp.(str "Compilation of file " ++ str f ++ |
| 809 |
str " failed with error " ++ str (Unix.error_message e)) |
| 810 |
|
| 811 |
let remove f = |
| 812 |
if Sys.file_exists f then Sys.remove f |
| 813 |
|
| 814 |
let extract_and_compile ~opaque_access l = |
| 815 |
if lang () != Ocaml then |
| 816 |
CErrors.user_err (Pp.str "This command only works with OCaml extraction"); |
| 817 |
let f = Filename.temp_file "testextraction" ".ml" in |
| 818 |
let () = full_extraction ~opaque_access (Some f) l in |
| 819 |
let () = compile f in |
| 820 |
let () = remove f; remove (f^"i") in |
| 821 |
let base = Filename.chop_suffix f ".ml" in |
| 822 |
let () = remove (base^".cmo"); remove (base^".cmi") in |
| 823 |
Feedback.msg_notice (str "Extracted code successfully compiled") |
| 824 |
|
| 825 |
|
| 826 |
let show_extraction ~pstate = |
| 827 |
let table = init ~inner:true ~modular:false ~library:false () in |
| 828 |
let prf = Declare.Proof.get pstate in |
| 829 |
let sigma, env = Declare.Proof.get_current_context pstate in |
| 830 |
let trms = Proof.partial_proof prf in |
| 831 |
let extr_term t = |
| 832 |
|
| 833 |
let ast, ty = extract_constr table env sigma t in |
| 834 |
let mp = Lib.current_mp () in |
| 835 |
let l = Declare.Proof.get_name pstate in |
| 836 |
let fake_ref = { glob = GlobRef.ConstRef (Constant.make2 mp l); inst = InfvInst.empty } in |
| 837 |
let decl = Dterm (fake_ref, ast, ty) in |
| 838 |
print_one_decl table [] mp decl |
| 839 |
in |
| 840 |
Feedback.msg_notice (Pp.prlist_with_sep Pp.fnl extr_term trms) |