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
8 changes: 7 additions & 1 deletion src/ecEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [])
Expand All @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/ecEnv.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/ecScope.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion src/ecSection.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
116 changes: 84 additions & 32 deletions src/ecThCloning.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand Down Expand Up @@ -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) =
Expand All @@ -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
Expand All @@ -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, _) ->
Expand All @@ -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

Expand Down Expand Up @@ -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) =
Expand Down
7 changes: 5 additions & 2 deletions src/ecThCloning.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 28 additions & 2 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 | `ClearOnly | `No] -> 'a;
hthexit : 'a -> import:bool -> [`Full | `Alias | `ClearOnly | `No] -> 'a;
herr : 'b . ?loc:EcLocation.t -> string -> 'b;
}

Expand Down Expand Up @@ -1407,16 +1407,42 @@ 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
let (subst, ops, proofs, subscope) =
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

Expand Down
2 changes: 1 addition & 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 | `ClearOnly | `No] -> 'a;
hthexit : 'a -> import:bool -> [`Full | `Alias | `ClearOnly | `No] -> 'a;
herr : 'b . ?loc:EcLocation.t -> string -> 'b;
}

Expand Down
Loading
Loading