diff --git a/src/ecCallbyValue.ml b/src/ecCallbyValue.ml index 7697f06f4..7a63c92e8 100644 --- a/src/ecCallbyValue.ml +++ b/src/ecCallbyValue.ml @@ -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 @@ -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 diff --git a/src/ecHiGoal.ml b/src/ecHiGoal.ml index 568991a03..9e69dc4dd 100644 --- a/src/ecHiGoal.ml +++ b/src/ecHiGoal.ml @@ -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 @@ -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 @@ -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] diff --git a/src/ecLowGoal.ml b/src/ecLowGoal.ml index 521425e36..462060134 100644 --- a/src/ecLowGoal.ml +++ b/src/ecLowGoal.ml @@ -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 (* -------------------------------------------------------------------- *) @@ -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 (* -------------------------------------------------------------------- *) @@ -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 diff --git a/src/ecParser.mly b/src/ecParser.mly index 69770d4c1..048973365 100644 --- a/src/ecParser.mly +++ b/src/ecParser.mly @@ -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 @@ -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 diff --git a/src/ecParsetree.ml b/src/ecParsetree.ml index 0b8528399..fd7764564 100644 --- a/src/ecParsetree.ml +++ b/src/ecParsetree.ml @@ -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 = []; } (* -------------------------------------------------------------------- *) diff --git a/src/ecReduction.ml b/src/ecReduction.ml index d8d913a74..a14d2a5b1 100644 --- a/src/ecReduction.ml +++ b/src/ecReduction.ml @@ -720,6 +720,25 @@ 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 @@ -727,7 +746,7 @@ let reduce_local ri hyps x = 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 @@ -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 diff --git a/src/ecReduction.mli b/src/ecReduction.mli index 7211da010..f092babeb 100644 --- a/src/ecReduction.mli +++ b/src/ecReduction.mli @@ -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 diff --git a/src/ecSimplifyContext.ml b/src/ecSimplifyContext.ml index 39c90e1a0..dfbad5f34 100644 --- a/src/ecSimplifyContext.ml +++ b/src/ecSimplifyContext.ml @@ -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; } (* -------------------------------------------------------------------- *) @@ -38,6 +40,7 @@ let empty : simplify_context = { ls_default_db = None; ls_default_hd = None; ls_added = Msym.empty; + ls_opacity = Mp.empty; } (* -------------------------------------------------------------------- *) @@ -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 = diff --git a/src/ecSimplifyContext.mli b/src/ecSimplifyContext.mli index cf541a1ce..e05249c41 100644 --- a/src/ecSimplifyContext.mli +++ b/src/ecSimplifyContext.mli @@ -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 diff --git a/tests/hint-db-crush.ec b/tests/hint-db-crush.ec new file mode 100644 index 000000000..05b3458f8 --- /dev/null +++ b/tests/hint-db-crush.ec @@ -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. diff --git a/tests/hint-delta-opacity.ec b/tests/hint-delta-opacity.ec new file mode 100644 index 000000000..189c962c3 --- /dev/null +++ b/tests/hint-delta-opacity.ec @@ -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.