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/ecCallbyValue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ and try_reduce_fixdef
if not (st.st_ri.iota && is_fix_def st p) then
raise Bailout;

(match st.st_ri.delta_p p with
| `Force -> ()
| _ ->
if EcReduction.reduction_opaque st.st_ri st.st_env p then
raise Bailout);

let Args.{ resty = ty; stack = args; } = args in
let op = oget (oper st p) in
let fix = EcDecl.operator_as_fix op in
Expand Down Expand Up @@ -444,7 +450,7 @@ and reduce_user_delta st f1 p tys args =
match reduce_user_with_exn st f2 with
| f -> f
| exception NotReducible ->
let mode = st.st_ri.delta_p p in
let mode = EcReduction.opacity_mode st.st_ri p (st.st_ri.delta_p p) in
let nargs = List.length args.stack in
match mode with
| #Op.redmode as mode -> begin
Expand Down
27 changes: 25 additions & 2 deletions src/ecHiGoal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,21 @@ let process_change fp (tc : tcenv1) =
let fp = TTC.tc1_process_formula tc fp in
t_change fp tc

(* -------------------------------------------------------------------- *)
(* Resolve the [-delta ops] / [+delta ops] items of a hint clause and
record them as reduction-opacity overrides in the simplify context. *)
let apply_hint_opacity tc env (specs : (bool * pqsymbol list) list) simpl =
List.fold_left (fun simpl (opq, ops) ->
let ops =
List.map (fun ps ->
match EcEnv.Op.lookup_opt (unloc ps) env with
| None -> tc_lookup_error !!tc ~loc:ps.pl_loc `Operator (unloc ps)
| Some p -> fst p) ops
in
EcEnv.SimplifyContext.set_opacity
(List.map (fun p -> (p, opq)) ops) simpl)
simpl specs

(* -------------------------------------------------------------------- *)
let process_local_hint (hint : plocalhint) (tc : tcenv1) =
let env = FApi.tc1_env tc in
Expand Down Expand Up @@ -137,8 +152,13 @@ let process_local_hint (hint : plocalhint) (tc : tcenv1) =
in
(mode, List.rev ops))
in
hd |> Option.fold ~none:simpl ~some:(fun hd ->
EcEnv.SimplifyContext.set_default_hd (Some hd) simpl)
let simpl =
hd |> Option.fold ~none:simpl ~some:(fun hd ->
EcEnv.SimplifyContext.set_default_hd (Some hd) simpl)
in

(* reduction-opacity overrides ([-delta ops] / [+delta ops]) *)
apply_hint_opacity tc env h.ph_opacity simpl

| PLHClear base ->
EcEnv.SimplifyContext.clear ?base simpl
Expand Down Expand Up @@ -205,6 +225,9 @@ let process_simplify_info ri (tc : tcenv1) =
) simpl hint.ph_lemmas
in

(* Per-call reduction-opacity overrides ([-delta ops] / [+delta ops]). *)
let simpl = apply_hint_opacity tc env hint.ph_opacity simpl in

(* Database list consulted by this call: the unsigned selection if any
(else the proof-local default / active set), with the signed
activate / deactivate deltas applied in order. [None] when no [hint]
Expand Down
23 changes: 18 additions & 5 deletions src/ecLowGoal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,13 @@ let rec t_lazy_match ?(reduce = `Full) ?(texn = fun _ -> raise InvalidGoalShape)
let strategy =
match reduce with
| `None -> raise InvalidGoalShape
| `Full ->
{ EcReduction.full_red with
user_local = FApi.tc1_simplify_context tc }
| `Full -> EcReduction.full_red
| `NoDelta -> EcReduction.nodelta in
(* the proof-local simplify context (activated databases, local
rules, opacity overrides) applies to these head reductions too *)
let strategy =
{ strategy with
EcReduction.user_local = FApi.tc1_simplify_context tc } in
FApi.t_seq (FApi.t_or (t_hred_with_info strategy) texn) (t_lazy_match ~reduce tx) tc

(* -------------------------------------------------------------------- *)
Expand Down Expand Up @@ -2547,7 +2550,12 @@ let t_crush ?(delta = true) ?tsolve (tc : tcenv1) =
{ cs_undosubst = Sid.empty (*Sid.of_list (List.map fst (LDecl.tohyps (FApi.tc1_hyps tc)).h_local)*) ;
cs_sbeq = (* Sid.of_list (List.map fst (LDecl.tohyps (FApi.tc1_hyps tc)).h_local)*) Sid.empty;
} in
FApi.t_seq (entry state) (t_simplify_with_info EcReduction.nodelta) tc
(* the closing pass sees the proof-local simplify context (activated
databases, local rules, opacity overrides), as the entry pass does *)
let final =
{ EcReduction.nodelta with
EcReduction.user_local = FApi.tc1_simplify_context tc } in
FApi.t_seq (entry state) (t_simplify_with_info final) tc


(* -------------------------------------------------------------------- *)
Expand Down Expand Up @@ -2919,6 +2927,11 @@ let t_crush_fwd ?(delta = true) nb_intros (tc : tcenv1) =
| _ -> t_fail tc
in

(* the closing pass sees the proof-local simplify context (activated
databases, local rules, opacity overrides), as the entry pass does *)
let final =
{ EcReduction.nodelta with
EcReduction.user_local = FApi.tc1_simplify_context tc } in
FApi.t_seq
(aux0 nb_intros)
(t_simplify_with_info EcReduction.nodelta) tc
(t_simplify_with_info final) tc
2 changes: 2 additions & 0 deletions src/ecParser.mly
Original file line number Diff line number Diff line change
Expand Up @@ -2551,6 +2551,7 @@ simplify_hint_item:
| m=pmode x=lident { `Db (m = `Plus, unloc x) }
| m=pmode l=bracket(qoident+) { `Hd (m, l) }
| l=brace(qident+) { `Lemma l }
| m=pmode DELTA l=qoident+ { `Opacity (m = `Minus, l) }

(* The body of a [hint] clause: an unsigned base database selection
followed by items. A clause may not both select databases (unsigned
Expand All @@ -2568,6 +2569,7 @@ simplify_hint_body:
let m = match m with `Plus -> `Include | `Minus -> `Exclude in
{ h with ph_hd = Some (m, l) }
| `Lemma l -> { h with ph_lemmas = h.ph_lemmas @ l }
| `Opacity (b, l) -> { h with ph_opacity = h.ph_opacity @ [(b, l)] }
in
let h =
List.fold_left doit
Expand Down
12 changes: 7 additions & 5 deletions src/ecParsetree.ml
Original file line number Diff line number Diff line change
Expand Up @@ -575,14 +575,16 @@ type pmpred_args = (osymbol * pformula) list
[ph_lemmas] are lemmas added to the default DB for this call (lemma
sets are add-only -- the head filter restricts which rules apply). *)
type psimplify_hint = {
ph_select : symbol list;
ph_dbs : (bool * symbol) list;
ph_hd : ([`Include | `Exclude] * pqsymbol list) option;
ph_lemmas : pqsymbol list;
ph_select : symbol list;
ph_dbs : (bool * symbol) list;
ph_hd : ([`Include | `Exclude] * pqsymbol list) option;
ph_lemmas : pqsymbol list;
(* [-delta ops] / [+delta ops]: true = make reduction-opaque *)
ph_opacity : (bool * pqsymbol list) list;
}

let empty_simplify_hint = {
ph_select = []; ph_dbs = []; ph_hd = None; ph_lemmas = [];
ph_select = []; ph_dbs = []; ph_hd = None; ph_lemmas = []; ph_opacity = [];
}

(* -------------------------------------------------------------------- *)
Expand Down
26 changes: 24 additions & 2 deletions src/ecReduction.ml
Original file line number Diff line number Diff line change
Expand Up @@ -720,14 +720,33 @@ exception NotRed of not_reducible
let nohead = NotRed NoHead
let needsubterm = NotRed NeedSubTerm

(* -------------------------------------------------------------------- *)
(* Effective reduction-opacity of an operator: the proof-local override
([hint -delta op] / [hint +delta op]) wins over the operator's
[opaque] declaration. *)
let reduction_opaque (ri : reduction_info) (env : EcEnv.env) (p : EcPath.path) =
match EcEnv.SimplifyContext.opacity p ri.user_local with
| Some b -> b
| None -> (oget (EcEnv.Op.by_path_opt p env)).op_opaque.reduction

(* Adjust a delta mode by the proof-local opacity override: [Some true]
blocks the reduction (unless forced), [Some false] upgrades default
transparency-gated unfolding to a forced one. *)
let opacity_mode (ri : reduction_info) (p : EcPath.path) mode =
match mode, EcEnv.SimplifyContext.opacity p ri.user_local with
| (`Force | `No), _ | _, None -> mode
| _, Some true -> `No
| `IfTransparent, Some false -> `Force
| `IfApplied, Some false -> `IfApplied

(* -------------------------------------------------------------------- *)
let reduce_local ri hyps x =
if ri.delta_h x
then try LDecl.unfold x hyps with NotReducible -> raise nohead
else raise nohead

let reduce_op ri env nargs p tys =
match ri.delta_p p with
match opacity_mode ri p (ri.delta_p p) with
| `No ->
raise nohead

Expand Down Expand Up @@ -1096,7 +1115,10 @@ let reduce_head simplify ri env hyps f =

(* ι-reduction (match-fix) *)
| Fapp ({ f_node = Fop (p, tys); }, fargs)
when ri.iota && EcEnv.Op.is_fix_def env p ->
when ri.iota && EcEnv.Op.is_fix_def env p
&& (match ri.delta_p p with
| `Force -> true
| _ -> not (reduction_opaque ri env p)) ->

let op = oget (EcEnv.Op.by_path_opt p env) in
let fix = EcDecl.operator_as_fix op in
Expand Down
10 changes: 10 additions & 0 deletions src/ecReduction.mli
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ type reduction_info = {
and deltap = [EcEnv.Op.redmode | `No]
and rlogic_info = [`Full | `ProductCompat] option

(* Effective reduction-opacity of an operator under the proof-local
overrides carried by [user_local]. *)
val reduction_opaque : reduction_info -> EcEnv.env -> EcPath.path -> bool

(* Adjust a delta mode by the proof-local opacity override. *)
val opacity_mode :
reduction_info -> EcPath.path ->
[`Force | `IfTransparent | `IfApplied | `No] ->
[`Force | `IfTransparent | `IfApplied | `No]

val full_red : reduction_info
val full_compat : reduction_info
val no_red : reduction_info
Expand Down
12 changes: 12 additions & 0 deletions src/ecSimplifyContext.ml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ type simplify_context = {
ls_default_hd : head_filter option;
(* Per-database overlay of locally added rules (add-only). *)
ls_added : entry list Msym.t;
(* Reduction-opacity overrides ([hint -delta op] / [hint +delta op]). *)
ls_opacity : bool Mp.t;
}

(* -------------------------------------------------------------------- *)
Expand All @@ -38,6 +40,7 @@ let empty : simplify_context = {
ls_default_db = None;
ls_default_hd = None;
ls_added = Msym.empty;
ls_opacity = Mp.empty;
}

(* -------------------------------------------------------------------- *)
Expand All @@ -52,6 +55,15 @@ let default_db (ls : simplify_context) : symbol list option =
let default_hd (ls : simplify_context) : head_filter option =
ls.ls_default_hd

(* -------------------------------------------------------------------- *)
let set_opacity (specs : (path * bool) list) (ls : simplify_context) : simplify_context =
{ ls with ls_opacity =
List.fold_left (fun m (p, b) -> Mp.add p b m) ls.ls_opacity specs }

(* -------------------------------------------------------------------- *)
let opacity (p : path) (ls : simplify_context) : bool option =
Mp.find_opt p ls.ls_opacity

(* -------------------------------------------------------------------- *)
(* [None] denotes the default database. *)
let normbase (base : symbol option) : symbol =
Expand Down
7 changes: 7 additions & 0 deletions src/ecSimplifyContext.mli
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ val set_default_hd :
(head_mode * path list) option -> simplify_context -> simplify_context
val clear_default : simplify_context -> simplify_context

(* Per-proof / per-call reduction-opacity overrides ([hint -delta op] /
[hint +delta op]): [Some true] blocks delta/iota reduction of the
operator, [Some false] re-enables it, [None] defers to the
operator's [opaque] declaration. *)
val set_opacity : (path * bool) list -> simplify_context -> simplify_context
val opacity : path -> simplify_context -> bool option

val added : ?base:symbol -> simplify_context -> entry list

val add_rules : ?base:symbol -> entry list -> simplify_context -> simplify_context
Expand Down
17 changes: 17 additions & 0 deletions tests/hint-db-crush.ec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(* A rule of an activated database applies inside crush even when the
term it rewrites only appears after a delta step: the head reductions
of split/elim see the activated databases too. *)
require import AllCore.

op c : int.
op g : int -> bool.
op sw (x y : int) = (y, x).

axiom gE : g c = true.
hint simplify in db : gE.

lemma crush_db : g (sw c c).`2.
proof. hint +db. by move=> />. qed.

lemma crush_db_split (b : bool) : b => g (sw c c).`2 /\ b.
proof. hint +db. by move=> />. qed.
47 changes: 47 additions & 0 deletions tests/hint-delta-opacity.ec
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require import AllCore.

(* Proof-local, tactic-local and per-call reduction-opacity overrides:
[-delta op] blocks the delta-reduction of [op], [+delta op] allows it
even when [op] is declared [opaque]. An explicit unfolding
([rewrite /op]) still unfolds. *)

op f (x : int) = x + 1.
op [opaque] h (x : int) = x + 2.

(* [rewrite fE] fails when the goal no longer contains [f 0]: it
witnesses that [f] was not unfolded. *)
lemma fE : f 0 = 1 by done.

(* --- Sanity ---------------------------------------------------------- *)

lemma f_done : f 0 = 1. proof. done. qed.
lemma h_nodone : h 0 = 2. proof. fail done. by rewrite /h. qed.

(* --- [-delta f]: proof-local ----------------------------------------- *)

(* An explicit unfolding still unfolds. *)
lemma f_unfold : f 0 = 1.
proof. hint -delta f. fail done. rewrite /f. done. qed.

lemma f_local : f 0 = 1.
proof. hint -delta f. fail done. simplify. rewrite fE. done. qed.

(* ... it also applies to the reductions of crush. *)
lemma f_local_crush : f 0 = 1 /\ true.
proof. hint -delta f. fail by move=> />. move=> />. by rewrite fE. qed.

(* --- [-delta f]: tactic-local and per-call --------------------------- *)

lemma f_with : f 0 = 1.
proof. fail with hint -delta f (done). done. qed.

lemma f_call : f 0 = 1.
proof. simplify hint -delta f. rewrite fE. done. qed.

(* --- [+delta h] overrides the [opaque] declaration ------------------- *)

lemma h_local : h 0 = 2.
proof. hint +delta h. done. qed.

lemma h_with : h 0 = 2.
proof. by with hint +delta h (done). qed.
Loading