From 50371bdd22646823bd60df3da6b8aeec0259b3dc Mon Sep 17 00:00:00 2001 From: Pierre-Yves Strub Date: Wed, 14 Oct 2026 16:09:11 +0200 Subject: [PATCH] fix(clone): alias a theory cleared by a theory override A theory override `theory B <- T` maps each item of `B` to the item with the same name in `T`. When `T` was itself produced by a clone that overrode one of its sub-theories with `theory A <- U`, that sub-theory was cleared: the items of `B.A` have no target, and the clone fails with: unknown symbol: ...B.A.t unless it also gives `theory B.A <- U`. A clone now leaves a non-imported alias `theory A = U` in place of a theory that `theory A <- U` has cleared, and theory overrides follow these aliases, so that `theory B <- T` suffices. The alias is left only when no more specific override departs from the theory override. It also names `U` in qualified names: `T.A.u` is `U.u`. Tests in tests/clone-theory-override-cleared.ec, and new cases in tests/clone-theory-override-nested.ec and tests/clone-theory-override-specific.ec. --- src/ecEnv.ml | 8 +- src/ecEnv.mli | 2 +- src/ecScope.mli | 2 +- src/ecSection.mli | 2 +- src/ecThCloning.ml | 116 ++++++++++++++++------- src/ecThCloning.mli | 7 +- src/ecTheoryReplay.ml | 30 +++++- src/ecTheoryReplay.mli | 2 +- tests/clone-theory-override-cleared.ec | 118 ++++++++++++++++++++++++ tests/clone-theory-override-nested.ec | 8 ++ tests/clone-theory-override-specific.ec | 35 +++++++ 11 files changed, 289 insertions(+), 41 deletions(-) create mode 100644 tests/clone-theory-override-cleared.ec diff --git a/src/ecEnv.ml b/src/ecEnv.ml index ef91f6821a..745eaa14f2 100644 --- a/src/ecEnv.ml +++ b/src/ecEnv.ml @@ -3947,7 +3947,9 @@ module Theory = struct in (cleared, omap (fun item_r -> { item with ti_item = item_r; }) item_r) (* ------------------------------------------------------------------ *) - type clear_mode = [`Full | `ClearOnly | `No] + (* [`Alias] is [`Full], but also drops a theory whose items are all + * aliases. *) + type clear_mode = [`Full | `Alias | `ClearOnly | `No] let close ?(clears : path list = []) @@ -3963,8 +3965,12 @@ module Theory = struct else snd (filter_th clears (root env) Sp.empty items) in let items = + let is_alias (item : theory_item) = + match item.ti_item with Th_alias _ -> true | _ -> false in + match items, pempty with | None, (`No | `ClearOnly) -> Some [] + | Some items, `Alias when List.for_all is_alias items -> None | _, _ -> items in diff --git a/src/ecEnv.mli b/src/ecEnv.mli index 0d4829c774..0e6ac17b69 100644 --- a/src/ecEnv.mli +++ b/src/ecEnv.mli @@ -321,7 +321,7 @@ module Theory : sig val close : ?clears:(path list) - -> ?pempty:[`Full | `ClearOnly | `No] + -> ?pempty:[`Full | `Alias | `ClearOnly | `No] -> EcTypes.is_local -> EcTheory.thmode -> env -> compiled_theory option diff --git a/src/ecScope.mli b/src/ecScope.mli index b1bc588b44..fa9a2838ff 100644 --- a/src/ecScope.mli +++ b/src/ecScope.mli @@ -177,7 +177,7 @@ module Theory : sig * its name. Raises [TopScope] if [scope] has not super scope. *) val exit : ?import:bool - -> ?pempty:[`ClearOnly | `Full | `No] + -> ?pempty:[`ClearOnly | `Full | `Alias | `No] -> ?clears:(pqsymbol option) list -> scope -> symbol * scope diff --git a/src/ecSection.mli b/src/ecSection.mli index 6a7ef8c7fa..e142f22998 100644 --- a/src/ecSection.mli +++ b/src/ecSection.mli @@ -23,7 +23,7 @@ val enter_theory : EcSymbols.symbol -> EcTypes.is_local -> thmode -> scenv -> sc val exit_theory : ?clears:EcPath.path list -> - ?pempty:[ `ClearOnly | `Full | `No ] -> + ?pempty:[ `ClearOnly | `Full | `Alias | `No ] -> 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 20562eaab7..8a3de5f9fd 100644 --- a/src/ecThCloning.ml +++ b/src/ecThCloning.ml @@ -87,7 +87,7 @@ type evclone = { evc_modtypes : (mt_override located) Msym.t; evc_lemmas : evlemma; evc_ths : (evclone * bool) Msym.t; - evc_target : EcPath.path option; + evc_target : (EcPath.path * bool) option; } @@ -513,7 +513,6 @@ end = struct | Some (sp, _) -> sp in - let thd = let thd = EcPath.toqsymbol sp in (fst thd @ [snd thd]) in let xdth = nm @ [x] in let rename (kind, name) = @@ -529,12 +528,29 @@ end = struct let sub, clear = odfl (evc_empty, false) sub in if is_some sub.evc_target then clone_error oc.oc_env (CE_DupOverride (OVK_Theory, name)); - let sub = { sub with evc_target = Some sp } in + let sub = { sub with evc_target = Some (sp, false) } in Some (sub, clear || (mode <> `Alias))) x evc.evc_ths in { evc with evc_ths = ths }) nm evc in + (* The target of the sub-theory [x] of the target theory [p]. A + * sub-theory that was cleared by a theory override when [p] was + * cloned is replaced by an alias to the theory it was overridden + * with: follow it. *) + let subtarget (p : EcPath.path) (x : symbol) = + let q = EcPath.pqname p x in + if is_some (EcEnv.Theory.by_path_opt ~mode:`All q oc.oc_env) then q else + + let alias (item : theory_item) = + match item.ti_item with + | Th_alias (y, [q]) when y = x -> Some q + | _ -> None in + + EcEnv.Theory.by_path_opt ~mode:`All p oc.oc_env + |> obind (fun cth -> List.opick alias cth.cth_items) + |> odfl q in + (* The lemmas that already have a proof *) let proved : (qsymbol, unit) Hashtbl.t = let proved = Hashtbl.create 0 in @@ -546,40 +562,59 @@ end = struct proved in (* A more specific override takes precedence over the one implied - * by the theory override. *) - let overridden (prefix : symbol list) (dth : theory_item_r) = - let sub = odfl evc_empty (evc_get (xdth @ prefix) evc) in + * by the theory override. Unless it overrides a sub-theory with + * its target and leaves an alias, it departs from the theory + * override, which then does not leave an alias. *) + let departs = ref false in - match dth with - | Th_type (x, _) -> - Msym.mem x sub.evc_types - | Th_operator (x, { op_kind = OB_oper _ }) -> - Msym.mem x sub.evc_ops - | Th_operator (x, { op_kind = OB_pred _ }) -> - Msym.mem x sub.evc_preds - | Th_operator (x, { op_kind = OB_nott _ }) -> - Msym.mem x sub.evc_abbrevs - | Th_module me -> - Msym.mem me.tme_expr.me_name sub.evc_modexprs - | Th_modtype (x, _) -> - Msym.mem x sub.evc_modtypes - | Th_theory (x, _) -> - Msym.find_opt x sub.evc_ths - |> obind (fun (sub, _) -> sub.evc_target) - |> is_some - | Th_axiom (x, _) -> - Hashtbl.mem proved (xdth @ prefix, x) - | _ -> false in + let overridden (prefix : symbol list) (tgp : EcPath.path) (dth : theory_item_r) = + let sub = odfl evc_empty (evc_get (xdth @ prefix) evc) in - let rec doit_r (prefix, tgprefix) (proofs, evc) dth = + let thtarget (x : symbol) = + Msym.find_opt x sub.evc_ths |> obind (fun (sub, _) -> sub.evc_target) in + + let overridden = + match dth with + | Th_type (x, _) -> + Msym.mem x sub.evc_types + | Th_operator (x, { op_kind = OB_oper _ }) -> + Msym.mem x sub.evc_ops + | Th_operator (x, { op_kind = OB_pred _ }) -> + Msym.mem x sub.evc_preds + | Th_operator (x, { op_kind = OB_nott _ }) -> + Msym.mem x sub.evc_abbrevs + | Th_module me -> + Msym.mem me.tme_expr.me_name sub.evc_modexprs + | Th_modtype (x, _) -> + Msym.mem x sub.evc_modtypes + | Th_theory (x, _) -> + is_some (thtarget x) + | Th_axiom (x, _) -> + Hashtbl.mem proved (xdth @ prefix, x) + | _ -> false in + + let agrees = + match dth with + | Th_theory (x, _) -> begin + match thtarget x with + | Some (p, true) -> + EcPath.p_equal p (subtarget tgp (rename (`Theory, x))) + | _ -> false + end + | _ -> false in + + if overridden && not agrees then departs := true; + overridden in + + let rec doit_r (prefix, tgp) (proofs, evc) dth = let dtpath (x : symbol) = loced (xdth @ prefix, x) in let tgpath ?kind (x : symbol) = let x = Option.fold ~none:x ~some:(fun k -> rename (k, x)) kind in - EcPath.fromqsymbol (thd @ tgprefix, x) in + EcPath.pqname tgp x in - if overridden prefix dth then (proofs, evc) else + if overridden prefix tgp dth then (proofs, evc) else match dth with | Th_type (x, _) -> @@ -603,9 +638,9 @@ end = struct ax_ovrd oc (proofs, evc) (dtpath x) (axd, mode) | Th_theory (x, dth) when dth.cth_mode = `Concrete -> - let tgx = rename (`Theory, x) in + let tgp = subtarget tgp (rename (`Theory, x)) in List.fold_left - (doit (prefix @ [x], tgprefix @ [tgx])) + (doit (prefix @ [x], tgp)) (proofs, evc) dth.cth_items @@ -636,7 +671,24 @@ end = struct and doit prefix (proofs, evc) dth = doit_r prefix (proofs, evc) dth.ti_item - in List.fold_left (doit ([], [])) (proofs, evc) dth.cth_items + in + + let (proofs, evc) = + List.fold_left (doit ([], sp)) (proofs, evc) dth.cth_items in + + let evc = + if mode <> `Inline `Clear || !departs then evc else + evc_update + (fun evc -> + let ths = + Msym.change + (omap (fun (sub, clear) -> + ({ sub with evc_target = Some (sp, true) }, clear))) + x evc.evc_ths + in { evc with evc_ths = ths }) + nm evc + + in (proofs, evc) (* ------------------------------------------------------------------ *) let ovrd oc state name (ovrd : theory_override) = diff --git a/src/ecThCloning.mli b/src/ecThCloning.mli index 2d1b56fa3b..465b2a2aa2 100644 --- a/src/ecThCloning.mli +++ b/src/ecThCloning.mli @@ -69,9 +69,12 @@ type evclone = { evc_modtypes : (mt_override located) Msym.t; evc_lemmas : evlemma; evc_ths : (evclone * bool) Msym.t; - evc_target : EcPath.path option; - (* the target of the theory override of this (sub-)theory, if any *) + evc_target : (EcPath.path * bool) option; } +(* [evc_target] is the target [T] of the theory override of this + * (sub-)theory, if any, and whether, once cleared, the theory is to be + * replaced by an alias to [T]: when the override is [theory X <- T] + * and no more specific override departs from it. *) and evlemma = { ev_global : (ptactic_core option * evtags option) list; diff --git a/src/ecTheoryReplay.ml b/src/ecTheoryReplay.ml index d1f293ab3f..3fbd9b5dd1 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 | `ClearOnly | `No] -> 'a; + hthexit : 'a -> import:bool -> [`Full | `Alias | `ClearOnly | `No] -> 'a; herr : 'b . ?loc:EcLocation.t -> string -> 'b; } @@ -1407,6 +1407,13 @@ and replay1 (ove : _ ovrenv) (subst, ops, proofs, scope) (hidden, item) = else subovrds.evc_lemmas.ev_global; } in + (* The theory [T] this one is to be replaced with, once cleared, + * by [theory ox <- T] *) + let target = + match subovrds.evc_target with + | Some (p, true) -> Some (EcSubst.subst_path subst p) + | _ -> None in + let (subst, ops, proofs, subscope) = let new_local = odfl cth.cth_loca ove.ovre_local in let subscope = ove.ovre_hooks.hthenter scope thmode x new_local in @@ -1414,9 +1421,28 @@ and replay1 (ove : _ ovrenv) (subst, ops, proofs, scope) (hidden, item) = List.fold_left (fun state item -> replay1 subove state (hidden, item)) (subst, ops, proofs, subscope) cth.cth_items in - let scope = ove.ovre_hooks.hthexit ~import:(not hidden) subscope `Full in + let pempty = if is_some target then `Alias else `Full in + let scope = ove.ovre_hooks.hthexit ~import:(not hidden) subscope pempty in (subst, ops, proofs, scope) + in + + (* If the overridden theory has been cleared, leave an alias to + * its target, so that a later theory override whose target is + * the theory being built can find what [ox] has been replaced + * with. *) + let subscope = + match target with + | None -> + subscope + | Some target -> + let env = EcSection.env (ove.ovre_hooks.henv subscope) in + let xpath = EcPath.pqname (EcEnv.root env) x in + if is_some (EcEnv.Theory.by_path_opt ~mode:`All xpath env) then + subscope + else + ove.ovre_hooks.hadd_item subscope ~import:false (Th_alias (x, [target])) + in (subst, ops, proofs, subscope) end diff --git a/src/ecTheoryReplay.mli b/src/ecTheoryReplay.mli index a5de45eace..cc791ec344 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 | `ClearOnly | `No] -> 'a; + hthexit : 'a -> import:bool -> [`Full | `Alias | `ClearOnly | `No] -> 'a; herr : 'b . ?loc:EcLocation.t -> string -> 'b; } diff --git a/tests/clone-theory-override-cleared.ec b/tests/clone-theory-override-cleared.ec new file mode 100644 index 0000000000..408982c673 --- /dev/null +++ b/tests/clone-theory-override-cleared.ec @@ -0,0 +1,118 @@ +(* A theory override `theory B <- T` maps each item of `B` to the item + with the same name in `T`. When `T` was itself produced by a clone + that overrode one of its sub-theories with `theory A <- U`, that + sub-theory was cleared, and the items of `B.A` used to have no + target. The cloning now leaves an alias `theory A = U` in place of a + theory that has been cleared, and theory overrides follow it. *) + +require import AllCore. + +theory Arr. + type 'a t. + op get : 'a t -> int -> 'a. +end Arr. + +abstract theory Base. + theory A. + type 'a t. + op get : 'a t -> int -> 'a. + end A. + + op k : int. +end Base. + +abstract theory Leaf. + clone import Base as B. + + axiom leafax (x : int A.t) : A.get x k = A.get x k. + lemma leaflm (x : int A.t) : A.get x k = A.get x k by done. +end Leaf. + +(* -------------------------------------------------------------------- *) +abstract theory Cleared. + clone import Base as B with theory A <- Arr. + + expect " +theory B. + (* no import *) theory A = Arr. + + (* import *) op k : int. +end B. +" by print theory B. + + (* `B.A` names `Arr` *) + op z (x : int B.A.t) : int Arr.t = x. + + clone Leaf as L with theory B <- B. + + expect " +(* Cleared.L.leaflm (shorten name: L.leaflm) *) +lemma leaflm: forall (x : int Arr.t), Arr.get x k = Arr.get x k. +" by print lemma L.leaflm. +end Cleared. + +(* -------------------------------------------------------------------- *) +(* With `<=`, the sub-theory is kept, and the result is the same. *) +abstract theory Kept. + clone import Base as B with theory A <= Arr. + clone Leaf as L with theory B <- B proof *. + realize leafax by done. + + expect " +(* Kept.L.leaflm (shorten name: L.leaflm) *) +lemma leaflm: forall (x : int Arr.t), Arr.get x k = Arr.get x k. +" by print lemma L.leaflm. +end Kept. + +(* -------------------------------------------------------------------- *) +(* Threading theories through a chain of clones. *) +abstract theory PreludeT. + clone import Base as B. + + op f (x : int A.t) = A.get x k. + lemma f_def x : f x = A.get x k by done. +end PreludeT. + +abstract theory RowT. + clone import Base as B. + clone import PreludeT as PR with theory B <- B. + + op g (x : int A.t) = PR.f x + 1. + lemma g_def x : g x = A.get x k + 1 by rewrite /g PR.f_def. +end RowT. + +abstract theory TopT. + clone import Base as B. + clone import PreludeT as PR with theory B <- B. + clone import RowT as RV with theory B <- B, theory PR <- PR. + + lemma top x : RV.g x = A.get x k + 1 by apply RV.g_def. +end TopT. + +abstract theory Chain. + clone import Base as B with theory A <- Arr. + clone import PreludeT as PR with theory B <- B. + clone import RowT as RV with theory B <- B, theory PR <- PR. + clone import TopT as T with theory B <- B, theory PR <- PR, theory RV <- RV. + + expect " +(* Chain.T.top (shorten name: top) *) +lemma top: forall (x : int Arr.t), g x = Arr.get x k + 1. +" by print lemma T.top. +end Chain. + +(* Cloning a theory with aliases: they point into the new theory, not + into `Chain` (they would be printed `Chain.B` and `Chain.PR`). *) +clone Chain as C with op B.k <- 3. + +expect " +theory RV. + (* no import *) theory B = B. + + (* no import *) theory PR = PR. + + (* import *) op g (x : int Arr.t) : int = PR.f x + 1. + + (* import *) lemma g_def: forall (x : int Arr.t), g x = Arr.get x 3 + 1. +end RV. +" by print theory C.RV. diff --git a/tests/clone-theory-override-nested.ec b/tests/clone-theory-override-nested.ec index a3c8a27a0c..841b949e71 100644 --- a/tests/clone-theory-override-nested.ec +++ b/tests/clone-theory-override-nested.ec @@ -36,6 +36,10 @@ clone Leaf as L1 with theory B.A <- Arr. expect " theory L1. + (* import *) theory B. + (* no import *) theory A = Arr. + end B. + (* import *) theory A. (* import *) type u. end A. @@ -52,6 +56,10 @@ expect " op h : Arr2.u -> int Arr.t. " by print op L2.h. +(* The cleared theories are replaced with aliases to their targets. *) +op w1 : L2.A.u -> Arr2.u = fun x => x. +op w2 : int L2.B.A.t -> int Arr.t = fun x => x. + (* Overriding the same theory twice is an error. *) expect fail "the theory `B.A' is instantiate twice" clone Leaf as L3 with theory B.A <- Arr, theory B.A <- Arr. diff --git a/tests/clone-theory-override-specific.ec b/tests/clone-theory-override-specific.ec index 6a95d3c3e7..d6aea81a89 100644 --- a/tests/clone-theory-override-specific.ec +++ b/tests/clone-theory-override-specific.ec @@ -57,3 +57,38 @@ expect " lemma leaflm: forall (x : int Arr.t), Arr.get x 0 = Arr.get x 0. " by print lemma L3.leaflm. +(* The theory override leaves an alias to its target in place of the + theory it clears, unless a more specific override departs from it. *) +clone Base as B1 with theory A <- Arr. + +(* `B1.A` was cleared and replaced with an alias to `Arr`: agrees. *) +clone Leaf as L4 with theory B <- B1, theory B.A <- Arr. + +expect " +theory L4. + (* no import *) theory B = B1. + + (* import *) lemma leaflm: + forall (x : int Arr.t), Arr.get x B1.k = Arr.get x B1.k. +end L4. +" by print theory L4. + +(* `B0.A` is not `Arr2`: departs. *) +expect " +theory L1. + (* no import *) theory B. + (* no import *) theory A = Arr2. + end B. + + (* import *) lemma leaflm: + forall (x : int Arr2.t), Arr2.get x B0.k = Arr2.get x B0.k. +end L1. +" by print theory L1. + +(* `B0.k` is not `0`: departs. *) +expect " +theory L3. + (* import *) lemma leaflm: + forall (x : int Arr.t), Arr.get x 0 = Arr.get x 0. +end L3. +" by print theory L3.