diff --git a/src/ecEnv.ml b/src/ecEnv.ml index 745eaa14f..af52385a0 100644 --- a/src/ecEnv.ml +++ b/src/ecEnv.ml @@ -3954,6 +3954,7 @@ module Theory = struct let close ?(clears : path list = []) ?(pempty : clear_mode = `No) + ?(source : path option) (loca : is_local) (mode : thmode) (env : env) @@ -3977,7 +3978,7 @@ module Theory = struct items |> omap (fun items -> let ctheory = { cth_items = items - ; cth_source = None + ; cth_source = omap (fun ths_base -> { ths_base }) source ; cth_loca = loca ; cth_mode = mode } in diff --git a/src/ecEnv.mli b/src/ecEnv.mli index 0e6ac17b6..e3f1e726d 100644 --- a/src/ecEnv.mli +++ b/src/ecEnv.mli @@ -322,6 +322,7 @@ module Theory : sig val close : ?clears:(path list) -> ?pempty:[`Full | `Alias | `ClearOnly | `No] + -> ?source:path -> EcTypes.is_local -> EcTheory.thmode -> env -> compiled_theory option diff --git a/src/ecScope.ml b/src/ecScope.ml index f22823d68..c7d18a2c2 100644 --- a/src/ecScope.ml +++ b/src/ecScope.ml @@ -1942,12 +1942,13 @@ module Theory = struct in { scope with sc_clears = scope.sc_clears @ clears } (* -------------------------------------------------------------------- *) - let exit_r ?pempty (scope : scope) = + let exit_r ?pempty ?source (scope : scope) = match scope.sc_top with | None -> raise TopScope | Some sup -> let clears = scope.sc_clears in - let _, cth, _ = EcSection.exit_theory ?pempty ~clears scope.sc_env in + let _, cth, _ = + EcSection.exit_theory ?pempty ?source ~clears scope.sc_env in let loaded = scope.sc_loaded in let required = scope.sc_required in let sup = { @@ -1957,10 +1958,10 @@ module Theory = struct ((cth, required), scope.sc_name, sup) (* ------------------------------------------------------------------ *) - let exit ?import ?(pempty = `ClearOnly) ?(clears =[]) (scope : scope) = + let exit ?import ?(pempty = `ClearOnly) ?(clears =[]) ?source (scope : scope) = assert (scope.sc_pr_uc = None); - let cth = exit_r ~pempty (add_clears clears scope) in + let cth = exit_r ~pempty ?source (add_clears clears scope) in let ((cth, required), (name, _), scope) = cth in let scope = List.fold_right require_loaded required scope in let scope = ofold (fun cth scope -> bind ?import scope cth) scope cth in @@ -2184,8 +2185,8 @@ module Cloning = struct (* ------------------------------------------------------------------ *) let hooks ~(override_locality: is_local option) : scope R.ovrhooks = - let thexit sc ~import pempty = - snd (Theory.exit ~import ?clears:None ~pempty sc) in + let thexit sc ~import ?source pempty = + snd (Theory.exit ~import ?clears:None ?source ~pempty sc) in let add_item scope ~import item = let item = EcTheory.mkitem ~import item in { scope with sc_env = EcSection.add_item ~override_locality item scope.sc_env } in @@ -2256,10 +2257,17 @@ module Cloning = struct let cpath = EcEnv.root (env scope) in let npath = if incl then cpath else EcPath.pqname cpath name in + (* A clone that changes nothing is a pristine copy of its source: + it records it, and a theory override between two pristine copies + of the same theory is then replayed as a renaming. *) + let source = + if C.evc_is_empty ovrds && List.is_empty rnms && Sp.is_empty ntclr && not incl + then Some opath else None in + let (proofs, scope) = EcTheoryReplay.replay (hooks ~override_locality:thcl.pthc_local) ~abstract:opts.R.clo_abstract ~override_locality:thcl.pthc_local ~incl - ~clears:ntclr ~renames:rnms ~opath ~npath ovrds + ~clears:ntclr ~renames:rnms ~opath ~npath ?source ovrds scope (name, false, oth.cth_items, oth.cth_loca) in diff --git a/src/ecScope.mli b/src/ecScope.mli index fa9a2838f..d5b909496 100644 --- a/src/ecScope.mli +++ b/src/ecScope.mli @@ -179,6 +179,7 @@ module Theory : sig ?import:bool -> ?pempty:[`ClearOnly | `Full | `Alias | `No] -> ?clears:(pqsymbol option) list + -> ?source:EcPath.path -> scope -> symbol * scope (* [import scope name] find and import theory [name] in scope diff --git a/src/ecSection.ml b/src/ecSection.ml index 2460ce249..f0176c4ca 100644 --- a/src/ecSection.ml +++ b/src/ecSection.ml @@ -1513,12 +1513,12 @@ let enter_theory (name:symbol) (lc:is_local) (mode:thmode) scenv : scenv = sc_items = []; sc_deps = scenv.sc_deps; } -let exit_theory ?clears ?pempty scenv = +let exit_theory ?clears ?pempty ?source scenv = match scenv.sc_name with | Sc _ -> hierror "cannot close a theory containing pending sections" | Top -> hierror "no theory to close" | Th (name, lc, mode) -> - let cth = EcEnv.Theory.close ?clears ?pempty lc mode scenv.sc_env in + let cth = EcEnv.Theory.close ?clears ?pempty ?source lc mode scenv.sc_env in let scenv = { (oget scenv.sc_top) with sc_deps = scenv.sc_deps } in name, cth, scenv diff --git a/src/ecSection.mli b/src/ecSection.mli index e142f2299..963dbcdfe 100644 --- a/src/ecSection.mli +++ b/src/ecSection.mli @@ -24,6 +24,7 @@ val enter_theory : EcSymbols.symbol -> EcTypes.is_local -> thmode -> scenv -> sc val exit_theory : ?clears:EcPath.path list -> ?pempty:[ `ClearOnly | `Full | `Alias | `No ] -> + ?source:EcPath.path -> scenv -> EcSymbols.symbol * EcEnv.Theory.compiled_theory option * scenv val import : EcPath.path -> scenv -> scenv diff --git a/src/ecThCloning.ml b/src/ecThCloning.ml index 8a3de5f9f..09ffaab84 100644 --- a/src/ecThCloning.ml +++ b/src/ecThCloning.ml @@ -112,6 +112,18 @@ let evc_empty = evc_ths = Msym.empty; evc_target = None; } +let evc_is_empty (evc : evclone) = + Msym.is_empty evc.evc_types + && Msym.is_empty evc.evc_ops + && Msym.is_empty evc.evc_preds + && Msym.is_empty evc.evc_abbrevs + && Msym.is_empty evc.evc_modexprs + && Msym.is_empty evc.evc_modtypes + && List.is_empty evc.evc_lemmas.ev_global + && Msym.is_empty evc.evc_lemmas.ev_bynames + && Msym.is_empty evc.evc_ths + && is_none evc.evc_target + let rec evc_update (upt : evclone -> evclone) (nm : symbol list) (evc : evclone) = match nm with | [] -> upt evc diff --git a/src/ecThCloning.mli b/src/ecThCloning.mli index 465b2a2aa..6c8557d02 100644 --- a/src/ecThCloning.mli +++ b/src/ecThCloning.mli @@ -85,6 +85,7 @@ and evtags = ([`Include | `Exclude] * symbol) list and evinfo = ptactic_core option * clmode * bool (* explicit *) val evc_empty : evclone +val evc_is_empty : evclone -> bool (* -------------------------------------------------------------------- *) type axclone = { diff --git a/src/ecTheoryReplay.ml b/src/ecTheoryReplay.ml index 3fbd9b5dd..479ad9cc6 100644 --- a/src/ecTheoryReplay.ml +++ b/src/ecTheoryReplay.ml @@ -36,7 +36,7 @@ and 'a ovrhooks = { henv : 'a -> EcSection.scenv; hadd_item : 'a -> import:bool -> EcTheory.theory_item_r -> 'a; hthenter : 'a -> thmode -> symbol -> is_local -> 'a; - hthexit : 'a -> import:bool -> [`Full | `Alias | `ClearOnly | `No] -> 'a; + hthexit : 'a -> import:bool -> ?source:EcPath.path -> [`Full | `Alias | `ClearOnly | `No] -> 'a; herr : 'b . ?loc:EcLocation.t -> string -> 'b; } @@ -1414,7 +1414,27 @@ and replay1 (ove : _ ovrenv) (subst, ops, proofs, scope) (hidden, item) = | Some (p, true) -> Some (EcSubst.subst_path subst p) | _ -> None in + (* The override replaces a pristine copy of a theory by another + pristine copy of the same theory: the two are indiscernible, so + the items are not replayed and compared one by one, every path + of the source copy is redirected to the target instead. *) + let pristine = + match target, cth.cth_source with + | Some tp, Some { ths_base } when List.is_empty ove.ovre_rnms -> begin + let env = EcSection.env (ove.ovre_hooks.henv scope) in + match EcEnv.Theory.by_path_opt ~mode:`All tp env with + | Some { cth_source = Some { ths_base = tb }; cth_mode } -> + EcPath.p_equal ths_base tb && cth_mode = thmode + | _ -> false + end + | _ -> false in + let (subst, ops, proofs, subscope) = + if pristine then + let subst = + EcSubst.add_path subst ~src:(xpath ove ox) ~dst:(oget target) in + (subst, ops, proofs, scope) + else let new_local = odfl cth.cth_loca ove.ovre_local in let subscope = ove.ovre_hooks.hthenter scope thmode x new_local in let (subst, ops, proofs, subscope) = @@ -1449,7 +1469,7 @@ and replay1 (ove : _ ovrenv) (subst, ops, proofs, scope) (hidden, item) = (* -------------------------------------------------------------------- *) let replay (hooks : 'a ovrhooks) ~abstract ~override_locality ~incl ~clears ~renames - ~opath ~npath ovrds (scope : 'a) (name, hidden, items, base_local) + ~opath ~npath ?source ovrds (scope : 'a) (name, hidden, items, base_local) = let subst = EcSubst.add_path EcSubst.empty ~src:opath ~dst:npath in let ove = { @@ -1473,7 +1493,8 @@ let replay (hooks : 'a ovrhooks) List.fold_left (fun state item -> replay1 ove state (hidden, item)) (subst, Mp.empty, [], scope) items in - let scope = if incl then scope else hooks.hthexit scope ~import:true `No in + let scope = + if incl then scope else hooks.hthexit scope ~import:true ?source `No in (List.rev proofs, scope) with EcEnv.DuplicatedBinding x -> diff --git a/src/ecTheoryReplay.mli b/src/ecTheoryReplay.mli index cc791ec34..5c9922d7b 100644 --- a/src/ecTheoryReplay.mli +++ b/src/ecTheoryReplay.mli @@ -27,7 +27,7 @@ and 'a ovrhooks = { henv : 'a -> EcSection.scenv; hadd_item : 'a -> import:bool -> EcTheory.theory_item_r -> 'a; hthenter : 'a -> thmode -> symbol -> EcTypes.is_local -> 'a; - hthexit : 'a -> import:bool -> [`Full | `Alias | `ClearOnly | `No] -> 'a; + hthexit : 'a -> import:bool -> ?source:path -> [`Full | `Alias | `ClearOnly | `No] -> 'a; herr : 'b . ?loc:EcLocation.t -> string -> 'b; } @@ -40,6 +40,7 @@ val replay : 'a ovrhooks -> renames:(renaming list) -> opath:path -> npath:path + -> ?source:path -> evclone -> 'a -> symbol * bool * theory_item list * EcTypes.is_local -> axclone list * 'a diff --git a/tests/clone-alias-target.ec b/tests/clone-alias-target.ec index dd294fb4c..9a89801c6 100644 --- a/tests/clone-alias-target.ec +++ b/tests/clone-alias-target.ec @@ -13,6 +13,7 @@ end T. clone T as T1. expect " +(* clone T as T1 *) theory T1. (* import *) theory X. (* import *) type u. diff --git a/tests/clone-pristine-override.ec b/tests/clone-pristine-override.ec new file mode 100644 index 000000000..292031a24 --- /dev/null +++ b/tests/clone-pristine-override.ec @@ -0,0 +1,55 @@ +(* A theory override whose two sides are pristine clones of the same + theory is replayed as a renaming of the source copy; the result still + instantiates, and an override whose sides are not pristine takes the + item-by-item path. *) +require import AllCore. + +abstract theory Base. + op n : int. + op f (x : int) = x + n. + axiom n_pos : 0 < n. +end Base. + +abstract theory Lower. + clone import Base as B. + lemma f_gt (x : int) : x < f x by smt(n_pos). +end Lower. + +(* both B are pristine copies of Base *) +abstract theory Upper. + clone import Base as B. + clone import Lower as L with theory B <- B. + lemma f2_gt (x : int) : x < f (f x) by smt(f_gt). +end Upper. + +(* the pristine copy still instantiates *) +clone Upper as U with op B.n <- 3 proof B.n_pos by done. + +lemma t1 (x : int) : x < U.B.f (U.B.f x) by apply U.f2_gt. + +(* the target carries an override: not pristine, replayed item by item *) +abstract theory Upper2. + clone import Base as B with op n <= 2 proof n_pos by done. + clone import Lower as L with theory B <- B. + lemma f2_gt (x : int) : x < f (f x) by smt(f_gt). +end Upper2. + +clone Upper2 as U2. + +lemma t2 (x : int) : x < U2.B.f (U2.B.f x) by apply U2.f2_gt. + +(* the source copy carries an override: not pristine either *) +abstract theory Lower3. + clone import Base as B with op n <= 2 proof n_pos by done. + lemma f_gt (x : int) : x < f x by smt(n_pos). +end Lower3. + +abstract theory Upper3. + clone import Base as B with op n <= 2 proof n_pos by done. + clone import Lower3 as L with theory B <- B. + lemma f2_gt (x : int) : x < f (f x) by smt(f_gt). +end Upper3. + +clone Upper3 as U3. + +lemma t3 (x : int) : x < U3.B.f (U3.B.f x) by apply U3.f2_gt.