Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/ecEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/ecEnv.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions src/ecScope.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/ecScope.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/ecSection.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions src/ecSection.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/ecThCloning.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/ecThCloning.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
27 changes: 24 additions & 3 deletions src/ecTheoryReplay.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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) =
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 ->
Expand Down
3 changes: 2 additions & 1 deletion src/ecTheoryReplay.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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
1 change: 1 addition & 0 deletions tests/clone-alias-target.ec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ end T.
clone T as T1.

expect "
(* clone T as T1 *)
theory T1.
(* import *) theory X.
(* import *) type u.
Expand Down
55 changes: 55 additions & 0 deletions tests/clone-pristine-override.ec
Original file line number Diff line number Diff line change
@@ -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.
Loading