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
84 changes: 82 additions & 2 deletions doc/tactics/hint-simplify.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,87 @@ Global declarations add lemmas to a simplification database.

.. admonition:: Syntax

`hint simplify {lemma}+ .`
`hint simplify [{option}*]? {item} (, {item})* .`

Add the listed lemmas to the default simplification database.

.. admonition:: Syntax

`hint simplify in {db} : {lemma}+ .`
`hint simplify in {db} : [{option}*]? {item} (, {item})* .`

Add the listed lemmas to the named database `{db}`.

Each `{item}` is a lemma name, or a parenthesised list of lemma names,
optionally followed by a priority:

- `{lemma}`
- `{lemma} @ {n}`
- `( {lemma} (, {lemma})* ) @ {n}`

Rules are tried in increasing priority order, `0` being the default, so
`@ {n}` with a larger `{n}` places a rule after the rules declared without
a priority. The priority of an item applies to every rule it produces.

The optional bracketed `{option}` list applies to every item of the
command:

- `reduce` -- head-normalise the statement of the lemma, unfolding
transparent definitions, before compiling it into rules. This lets a
lemma stated through an abbreviation-like operator register a rule on
the operator the abbreviation unfolds to.
- `eqtrue` -- accepted for compatibility. A boolean statement that is not
an equation is always compiled as a rewrite to `true` (see below), so
the option currently has no additional effect.

These commands are theory-level declarations: once imported, the
corresponding rules are available to simplification.

------------------------------------------------------------------------
Rule shape
------------------------------------------------------------------------

A lemma is compiled into one or more rewrite rules by decomposing its
statement. The statement is first stripped of its leading universal
quantifiers, then split as follows:

- `{c} => {f}` -- a conditional rule: `{c}` is recorded as a side
condition of every rule produced from `{f}`. When a rule is applied,
each side condition is instantiated and must simplify to `true` for the
rule to fire.
- `{f1} /\ {f2}` -- a conjunction contributes the rules of both
conjuncts. Side conditions and binders introduced above the conjunction
apply to both sides.
- `forall {x}, {f}` -- a conjunct may carry its own binders, which are
added to those of the enclosing statement.
- `{lhs} = {rhs}` and `{lhs} <=> {rhs}` -- a rewrite rule from `{lhs}` to
`{rhs}`.
- any other boolean statement `{p}` -- a rewrite rule from `{p}` to
`true`.

Hence a lemma such as

.. code-block:: easycrypt

lemma gE : g 0 = 4 /\ g 1 = 6 /\ g 2 = 2.
hint simplify gE.

registers three rules, one per equation, and

.. code-block:: easycrypt

lemma hE (x : int) : (0 < x => h x = x + 1) /\ h 0 = 7.
hint simplify hE.

registers a rule for `h x` guarded by `0 < x` and an unconditional rule
for `h 0`.

The left-hand side of each rule must be a first-order pattern: an
application of an operator to patterns, a tuple, a projection, an
integer literal, or a variable. Its head must not be a variable, and
every bound variable of the rule must occur in it (a binder of the
statement that a conjunct does not mention is simply dropped for that
conjunct). Lemmas quantifying over memories or modules are rejected.

------------------------------------------------------------------------
The `hint` clause
------------------------------------------------------------------------
Expand Down Expand Up @@ -160,8 +228,13 @@ Example
lemma wrap2E x : box (wrap2 x) = box (x + 2).
proof. smt(). qed.

op g : int -> int.

axiom gE : g 0 = 4 /\ g 1 = 6 /\ g 2 = 2.

hint simplify wrap1E.
hint simplify in named : wrap2E.
hint simplify gE.

lemma demo_default (x : int) :
box (wrap1 x) = box (x + 1).
Expand Down Expand Up @@ -190,3 +263,10 @@ Example
simplify hint +[wrap1].
trivial.
qed.

lemma demo_conjunction :
g 0 + g 1 + g 2 = 12.
proof.
(*$*) simplify.
trivial.
qed.
18 changes: 10 additions & 8 deletions src/ecEnv.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1532,9 +1532,7 @@ module Reduction = struct
(* The default-database name is owned by [EcSimplifyContext]. *)
let dname : symbol = EcSimplifyContext.dname

let add_rule ((src, rule) : path * rule option) (db : mredinfo) =
match rule with None -> db | Some rule ->

let add_rule ((src, rule) : path * rule) (db : mredinfo) =
let p : topsym =
match rule.rl_ptn with
| Rule (`Op p, _) -> `Path (fst p)
Expand All @@ -1557,22 +1555,24 @@ module Reduction = struct

Some { ri_priomap; ri_list }) p db

let add_rules (rules : (path * rule option) list) (db : mredinfo) =
let add_rules (rules : (path * rule) list) (db : mredinfo) =
List.fold_left ((^~) add_rule) db rules

let updatedb ?(base : symbol option) (rules : (path * rule option) list) (db : mredinfo Msym.t) =
let updatedb ?(base : symbol option) (rules : (path * rule) list) (db : mredinfo Msym.t) =
let nbase = odfl dname base in
let base = Msym.find_def Mrd.empty nbase db in
Msym.add nbase (add_rules rules base) db

let add ?(import = true) ({ red_base; red_rules } : reduction_rule) (env : env) =
let rstrip = List.map (fun (x, _, y) -> (x, y)) red_rules in
let rstrip =
List.concat_map (fun (x, _, rules) ->
List.map (fun rule -> (x, rule)) rules) red_rules in

{ env with
env_redbase = updatedb ?base:red_base rstrip env.env_redbase;
env_item = mkitem ~import (Th_reduction { red_base; red_rules }) :: env.env_item; }

let add1 ?base (prule : path * rule_option * rule option) (env : env) =
let add1 ?base (prule : path * rule_option * rule list) (env : env) =
add { red_base = base; red_rules = [prule] } env

let get_entries ?base (p : topsym) (env : env) =
Expand Down Expand Up @@ -3592,7 +3592,9 @@ module Theory = struct
let bind_rd_th =
let for1 _path db = function
| Th_reduction { red_base; red_rules } ->
let rules = List.map (fun (x, _, y) -> (x, y)) red_rules in
let rules =
List.concat_map (fun (x, _, rules) ->
List.map (fun rule -> (x, rule)) rules) red_rules in
Some (Reduction.updatedb ?base:red_base rules db)
| _ -> None

Expand Down
2 changes: 1 addition & 1 deletion src/ecEnv.mli
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ module Reduction : sig
(* Name of the default database (the empty name). *)
val dname : symbol
val all : env -> (base * (topsym * rule list) list) list
val add1 : ?base:symbol -> path * rule_option * rule option -> env -> env
val add1 : ?base:symbol -> path * rule_option * rule list -> env -> env
val add : ?import:bool -> reduction_rule -> env -> env
val get : ?base:symbol -> topsym -> env -> rule list
val get_entries : ?base:symbol -> topsym -> env -> entry list
Expand Down
10 changes: 6 additions & 4 deletions src/ecHiGoal.ml
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ let process_local_hint (hint : plocalhint) (tc : tcenv1) =
let simpl =
List.fold_left (fun simpl lemma ->
let path = EcEnv.Ax.lookup_path (unloc lemma) env in
let rule = EcReduction.User.compile ~opts ~prio:0 env path in
EcEnv.SimplifyContext.add_rules [(path, rule)] simpl)
let rules = EcReduction.User.compile ~opts ~prio:0 env path in
EcEnv.SimplifyContext.add_rules
(List.map (fun rule -> (path, rule)) rules) simpl)
simpl h.ph_lemmas
in

Expand Down Expand Up @@ -198,8 +199,9 @@ let process_simplify_info ri (tc : tcenv1) =
let opts = EcTheory.{ ur_delta = false; ur_eqtrue = false; } in
List.fold_left (fun simpl lemma ->
let path = EcEnv.Ax.lookup_path (unloc lemma) env in
let rule = EcReduction.User.compile ~opts ~prio:0 env path in
EcEnv.SimplifyContext.add_rules [(path, rule)] simpl
let rules = EcReduction.User.compile ~opts ~prio:0 env path in
EcEnv.SimplifyContext.add_rules
(List.map (fun rule -> (path, rule)) rules) simpl
) simpl hint.ph_lemmas
in

Expand Down
Loading
Loading