Simplification

At the heart of Imandra is a powerful symbolic simplifier and partial evaluator. The simplifier is integrated with the inductive waterfall (e.g., [@@auto]), and is the main way in which previously proved lemmas are used during proofs, through the automatic application of rules. The simplifier can also be used as a pre-processing step before unrolling, via the [@@simp] attribute.

As the name suggests, simplification is a process that attempts to transform a formula into a "simpler" form, bringing the salient features of a formula or conjecture to the surface. Simplification can also prove goals by reducing them to true, and refute them by reducing them to false.

Notably, because the symbolic evaluation semantics of the simplifier operate on a compact digraph representation of formulas and function definitions, simplification can be thought as having memoized semantics for free.

We can see an example of this by using the following naive recursive version of the fibonacci function:

In [1]:
let rec fib n =
  if n <= 1 then
    1
  else
    fib (n-1) + fib (n-2)
Out[1]:
val fib : int -> int = <fun>
termination proof

Termination proof

call `fib (n - 1)` from `fib n`
originalfib n
subfib (n - 1)
original ordinalOrdinal.Int (_cnt n)
sub ordinalOrdinal.Int (_cnt (n - 1))
path[not (n <= 1)]
proof
detailed proof
ground_instances1
definitions0
inductions0
search_time
0.011s
details
Expand
smt_stats
num checks3
arith-make-feasible5
arith-max-columns11
arith-conflicts1
rlimit count2332
mk clause5
datatype occurs check2
mk bool var18
arith-lower3
decisions2
arith-propagations3
propagations2
arith-bound-propagations-cheap3
arith-max-rows4
conflicts2
datatype accessor ax2
num allocs957572936
final checks1
added eqs6
del clause5
arith eq adapter2
arith-upper6
memory13.850000
max memory24.340000
Expand
  • start[0.011s]
      let (_x_0 : int) = if n >= 0 then n else 0 in
      let (_x_1 : int) = n - 1 in
      let (_x_2 : int) = if _x_1 >= 0 then _x_1 else 0 in
      let (_x_3 : bool) = _x_1 <= 1 in
      not (n <= 1) && _x_0 >= 0 && _x_2 >= 0
      ==> _x_3 && _x_3 || Ordinal.( << ) (Ordinal.Int _x_2) (Ordinal.Int _x_0)
  • simplify
    into
    (n <= 1 || n <= 2)
    || Ordinal.( << ) (Ordinal.Int (if n >= 1 then -1 + n else 0))
       (Ordinal.Int (if n >= 0 then n else 0))
    expansions
    []
    rewrite_steps
      forward_chaining
      • unroll
        expr
        (|Ordinal.<<_119| (|Ordinal.Int_108| (ite (>= n_2664 1) (+ (- 1) n_2664) 0))
                          (|Ord…
        expansions
        • Unsat

        call `fib (n - 2)` from `fib n`
        originalfib n
        subfib (n - 2)
        original ordinalOrdinal.Int (_cnt n)
        sub ordinalOrdinal.Int (_cnt (n - 2))
        path[not (n <= 1)]
        proof
        detailed proof
        ground_instances1
        definitions0
        inductions0
        search_time
        0.011s
        details
        Expand
        smt_stats
        num checks3
        arith-make-feasible5
        arith-max-columns11
        arith-conflicts1
        rlimit count1167
        mk clause5
        datatype occurs check2
        mk bool var18
        arith-lower3
        decisions2
        arith-propagations3
        propagations2
        arith-bound-propagations-cheap3
        arith-max-rows4
        conflicts2
        datatype accessor ax2
        num allocs934854928
        final checks1
        added eqs6
        del clause5
        arith eq adapter2
        arith-upper6
        memory11.300000
        max memory24.340000
        Expand
        • start[0.011s]
            let (_x_0 : int) = if n >= 0 then n else 0 in
            let (_x_1 : int) = n - 2 in
            let (_x_2 : int) = if _x_1 >= 0 then _x_1 else 0 in
            let (_x_3 : bool) = _x_1 <= 1 in
            not (n <= 1) && _x_0 >= 0 && _x_2 >= 0
            ==> _x_3 && _x_3 || Ordinal.( << ) (Ordinal.Int _x_2) (Ordinal.Int _x_0)
        • simplify
          into
          (n <= 1 || n <= 3)
          || Ordinal.( << ) (Ordinal.Int (if n >= 2 then -2 + n else 0))
             (Ordinal.Int (if n >= 0 then n else 0))
          expansions
          []
          rewrite_steps
            forward_chaining
            • unroll
              expr
              (|Ordinal.<<_119| (|Ordinal.Int_108| (ite (>= n_2664 2) (+ (- 2) n_2664) 0))
                                (|Ord…
              expansions
              • Unsat

              If we try to use Imandra's simplification to search for a solution for fib 200, Imandra comes back to us with a solution immediately:

              In [2]:
              #check_models false;;
              instance (fun x -> x = fib 200) [@@simp];;
              #check_models true;;
              
              Out[2]:
              - : int -> bool = <fun>
              module CX : sig val x : int end
              
              Instance (after 0 steps, 0.052s):
               let (x : int) = 453973694165307953197296969697410619233826
              
              Instance
              proof attempt
              ground_instances0
              definitions201
              inductions0
              search_time
              0.052s
              details
              Expand
              smt_stats
              eliminated vars1
              rlimit count7905
              mk bool var1
              num allocs1055641070
              memory14.720000
              max memory24.340000
              Expand
              • start[0.052s] :var_0: = fib 200
              • simplify

                into
                x = 453973694165307953197296969697410619233826
                expansions
                [fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib, fib,
                 fib, fib, fib, fib, fib, fib]
                rewrite_steps
                  forward_chaining
                  • Sat (Some let (x : int) = 453973694165307953197296969697410619233826 )

                  If, however, we tried to use normal OCaml evaluation to compute fib 200, OCaml would take over 10 minutes in order to come back with a response (the #check_models false command here is used to tell Imandra not to check the instance Imandra computed using OCaml evaluation, as that requires the expensive computation of fib 200 via standard OCaml evaluation, which is not memoized).

                  Now that we know what simplification does, let's learn about how to influence it using rewrite and forward chaining rules.

                  Rewrite Rules

                  A rewrite rule is a theorem of the form

                  h_1 && ... && h_k ==> lhs = rhs

                  which Imandra can use in further proofs to replace terms that match with lhs (the "left hand side") with the appropriate instantiation of rhs (the "right hand side"), provided that the instantiations of the hypotheses can be established. Observe that rewrite rules are both conditional (requiring in general the establishment of hypotheses) and directed (replacing lhs with rhs). The lhs is also called the "pattern" of the rule.

                  An enabled rewrite rule causes Imandra to look for matches of the lhs, replacing the matched term with the (suitably instantiated) rhs, provided that Imandra can establish ("relieve") the (suitably instantiated) hypotheses of the rule.

                  For example, consider the lemma rev_len below. This lemma expresses the fact that the length of a list x is equal to the length of List.rev x, i.e., if we reverse a list, we end up with a list of the same length. This rule is unconditional: it has no hypotheses. Thus, it will always be able to fire on terms that match its left-hand side. Notice that Imandra uses a previously defined rewrite rule in order to prove this lemma! The lemma rev_len would make an excellent rewrite rule, so we use the [@@rw] annotation to install it:

                  In [3]:
                  lemma rev_len x =
                    List.length (List.rev x) = List.length x
                  [@@auto] [@@rw]
                  
                  Out[3]:
                  val rev_len : 'a list -> bool = <fun>
                  Goal:
                  
                  List.length (List.rev x) = List.length x.
                  
                  1 nontautological subgoal.
                  
                  Subgoal 1:
                  
                  |---------------------------------------------------------------------------
                   List.length (List.rev x) = List.length x
                  
                  
                  Must try induction.
                  
                  The recursive terms in the conjecture suggest 2 inductions.
                  Subsumption and merging reduces this to 1.
                  
                  We shall induct according to a scheme derived from List.rev.
                  
                  Induction scheme:
                  
                   (x = [] ==> φ x) && (not (x = []) && φ (List.tl x) ==> φ x).
                  
                  2 nontautological subgoals.
                  
                  Subgoal 1.2:
                  
                   H0. x = []
                  |---------------------------------------------------------------------------
                   List.length (List.rev x) = List.length x
                  
                  But simplification reduces this to true, using the definitions of List.length
                  and List.rev.
                  
                  Subgoal 1.1:
                  
                   H0. not (x = [])
                   H1. List.length (List.rev (List.tl x)) = List.length (List.tl x)
                  |---------------------------------------------------------------------------
                   List.length (List.rev x) = List.length x
                  
                  This simplifies, using the definitions of List.length and List.rev to:
                  
                  Subgoal 1.1':
                  
                   H0. x <> []
                   H1. List.length (List.rev (List.tl x)) = List.length (List.tl x)
                  |---------------------------------------------------------------------------
                   List.length (List.append (List.rev (List.tl x)) [List.hd x]) =
                   1 + List.length (List.tl x)
                  
                  
                  We can eliminate destructors by the following substitution:
                   x -> x1 :: x2
                  
                  This produces the modified subgoal:
                  
                  Subgoal 1.1'':
                  
                   H0. List.length (List.rev x2) = List.length x2
                  |---------------------------------------------------------------------------
                   List.length (List.append (List.rev x2) [x1]) = 1 + List.length x2
                  
                  
                  Cross-fertilizing with:
                  
                   List.length (List.rev x2) = List.length x2
                  
                  This produces the modified subgoal:
                  
                  Subgoal 1.1''':
                  
                  |---------------------------------------------------------------------------
                   List.length (List.append (List.rev x2) [x1]) = 1 + List.length (List.rev x2)
                  
                  
                  Candidates for generalization:
                  
                   List.rev x2
                  
                  This produces the modified subgoal:
                  
                  Subgoal 1.1'''':
                  
                  |---------------------------------------------------------------------------
                   List.length (List.append gen_1 [x1]) = 1 + List.length gen_1
                  
                  
                  Must try induction.
                  
                  The recursive terms in the conjecture suggest 2 inductions.
                  Subsumption and merging reduces this to 1.
                  
                  We shall induct according to a scheme derived from List.append.
                  
                  Induction scheme:
                  
                   (gen_1 = [] ==> φ gen_1 x1)
                   && (not (gen_1 = []) && φ (List.tl gen_1) x1 ==> φ gen_1 x1).
                  
                  2 nontautological subgoals.
                  
                  Subgoal 1.1''''.2:
                  
                   H0. gen_1 = []
                  |---------------------------------------------------------------------------
                   List.length (List.append gen_1 [x1]) = 1 + List.length gen_1
                  
                  But simplification reduces this to true, using the definitions of List.append
                  and List.length.
                  
                  Subgoal 1.1''''.1:
                  
                   H0. not (gen_1 = [])
                   H1. List.length (List.append (List.tl gen_1) [x1]) =
                       1 + List.length (List.tl gen_1)
                  |---------------------------------------------------------------------------
                   List.length (List.append gen_1 [x1]) = 1 + List.length gen_1
                  
                  But simplification reduces this to true, using the definitions of List.append
                  and List.length.
                  
                   Rules:
                      (:def List.append)
                      (:def List.length)
                      (:def List.rev)
                      (:fc List.len_nonnegative)
                      (:induct List.append)
                      (:induct List.rev)
                  
                  
                  Proved
                  proof
                  ground_instances0
                  definitions11
                  inductions2
                  search_time
                  0.476s
                  Expand
                  • start[0.476s, "Goal"] List.length (List.rev :var_0:) = List.length :var_0:
                  • subproof

                    List.length (List.rev x) = List.length x
                    • start[0.476s, "1"] List.length (List.rev x) = List.length x
                    • induction on (functional )
                      :scheme (x = [] ==> φ x) && (not (x = []) && φ (List.tl x) ==> φ x)
                    • Split (let (_x_0 : bool) = not (x = []) in
                             let (_x_1 : bool) = List.length (List.rev x) = List.length x in
                             let (_x_2 : sko_ty_0 list) = List.tl x in
                             (_x_0 || _x_1)
                             && (not (_x_0 && List.length (List.rev _x_2) = List.length _x_2)
                                 || _x_1)
                             :cases [not (x = []) || List.length (List.rev x) = List.length x;
                                     let (_x_0 : sko_ty_0 list) = List.tl x in
                                     (x = []
                                      || not (List.length (List.rev _x_0) = List.length _x_0))
                                     || List.length (List.rev x) = List.length x])
                      • subproof
                        let (_x_0 : sko_ty_0 list) = List.tl x in (x = [] || not (List.length (List.rev _x_0) = List.length _x_0)) || List.length (List.rev x) = List.length x
                        • start[0.419s, "1.1"]
                            let (_x_0 : sko_ty_0 list) = List.tl x in
                            (x = [] || not (List.length (List.rev _x_0) = List.length _x_0))
                            || List.length (List.rev x) = List.length x
                        • simplify
                          into
                          let (_x_0 : sko_ty_0 list) = List.tl x in
                          let (_x_1 : sko_ty_0 list) = List.rev _x_0 in
                          let (_x_2 : int) = List.length _x_0 in
                          (List.length (List.append _x_1 [List.hd x]) = 1 + _x_2 || x = [])
                          || not (List.length _x_1 = _x_2)
                          expansions
                          [List.length, List.rev]
                          rewrite_steps
                            forward_chaining
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                            • List.len_nonnegative
                          • Elim_destructor (:cstor :: :replace x1 :: x2 :context [])
                          • Generalize (let (_x_0 : sko_ty_0 list) = List.rev x2 in
                                        List.length (List.append _x_0 [x1]) = 1 + List.length _x_0
                                        :as (gen_1 : sko_ty_0 list))
                          • induction on (functional )
                            :scheme (gen_1 = [] ==> φ gen_1 x1)
                                    && (not (gen_1 = []) && φ (List.tl gen_1) x1 ==> φ gen_1 x1)
                          • Split (let (_x_0 : bool) = not (gen_1 = []) in
                                   let (_x_1 : sko_ty_0 list) = [x1] in
                                   let (_x_2 : bool)
                                       = List.length (List.append gen_1 _x_1) = 1 + List.length gen_1
                                   in
                                   let (_x_3 : sko_ty_0 list) = List.tl gen_1 in
                                   (_x_0 || _x_2)
                                   && (not
                                       (_x_0
                                        && List.length (List.append _x_3 _x_1) = 1 + List.length _x_3)
                                       || _x_2)
                                   :cases [not (gen_1 = [])
                                           || List.length (List.append gen_1 [x1]) =
                                              1 + List.length gen_1;
                                           let (_x_0 : sko_ty_0 list) = List.tl gen_1 in
                                           let (_x_1 : sko_ty_0 list) = [x1] in
                                           (gen_1 = []
                                            || not
                                               (List.length (List.append _x_0 _x_1) =
                                                1 + List.length _x_0))
                                           || List.length (List.append gen_1 _x_1) =
                                              1 + List.length gen_1])
                            • Subproof
                            • Subproof
                        • subproof
                          not (x = []) || List.length (List.rev x) = List.length x
                          • start[0.419s, "1.2"] not (x = []) || List.length (List.rev x) = List.length x
                          • simplify
                            into
                            true
                            expansions
                            [List.length, List.length, List.rev]
                            rewrite_steps
                              forward_chaining
                              • List.len_nonnegative
                              • List.len_nonnegative

                      With this rule installed and enabled, if Imandra's simplifier encounters a term of the form List.length (List.rev <term>), it will replace it with the simpler form List.length <term>.

                      Both the hypotheses and rhs can be omitted, in which case Imandra will default them to true. That is, h_1 && ... && h_k ==> lhs is equivalent to h_1 && h_k ==> lhs = true and lhs = rhs is equal to true ==> lhs = rhs.

                      Imandra's rewriting is:

                      • conditional: rewrite rules may contain conditions (hypotheses), and eligible rules are only applied when their hypotheses are established. Once the pattern of a rule has been matched, Imandra uses backward-chaining ("backchaining") to relieve the rule's hypotheses, recursively attempting to simplify them to true modulo the current simplification context. This is important to keep in mind when developing your theories: rewrite rules will not fire unless Imandra can simplify their instantiated hypotheses to true.

                      • oriented: given a rule whose conclusion is of the form lhs = rhs, rewriting happens by replacing the (instantiated) lhs with the (instantiated) rhs.

                      When adding a new rewrite rule, users should take care to orient the equality so that rhs is simpler or more canonical than lhs. If it's not clear what it means for an rhs to be "better" than the lhs, e.g., in the case of the proof for the associativity of append x @ (y @ z) = (x @ y) @ z, a canonical form should typically be chosen (e.g., associating to the left in this case) and kept in mind for further rules.

                      By default, the lhs must contain all the top-level variables of the theorem (i.e. the arguments to the lambda term representing the goal). There is an exception to this rule: if the lhs does not contain all the variables of the theorem but the rule hypotheses have subterms containing the remaining free variables, these terms can be annotated with [@trigger rw], signaling Imandra that the annotated terms should be used to complete the matching.

                      It's helpful to see an example where the use of [@trigger rw] is necessary. Let's first define a subset function on lists:

                      In [4]:
                      let rec subset x y =
                        match x with
                        | [] -> true
                        | x :: xs ->
                          if List.mem x y then subset xs y
                          else false
                      
                      Out[4]:
                      val subset : 'a list -> 'a list -> bool = <fun>
                      
                      termination proof

                      Termination proof

                      call `subset (List.tl x) y` from `subset x y`
                      originalsubset x y
                      subsubset (List.tl x) y
                      original ordinalOrdinal.Int (_cnt x)
                      sub ordinalOrdinal.Int (_cnt (List.tl x))
                      path[List.mem (List.hd x) y && not (x = [])]
                      proof
                      detailed proof
                      ground_instances4
                      definitions0
                      inductions0
                      search_time
                      0.014s
                      details
                      Expand
                      smt_stats
                      num checks9
                      arith-make-feasible11
                      arith-max-columns13
                      arith-conflicts1
                      rlimit count2583
                      mk clause5
                      datatype occurs check41
                      mk bool var68
                      arith-lower4
                      datatype splits7
                      decisions24
                      propagations6
                      arith-max-rows5
                      conflicts9
                      datatype accessor ax6
                      datatype constructor ax15
                      num allocs2330909460
                      final checks10
                      added eqs51
                      del clause1
                      arith eq adapter3
                      arith-upper6
                      memory30.670000
                      max memory42.160000
                      Expand
                      • start[0.014s]
                          let (_x_0 : int) = count.list (const 0) x in
                          let (_x_1 : ty_0 list) = List.tl x in
                          let (_x_2 : int) = count.list (const 0) _x_1 in
                          List.mem (List.hd x) y && not (x = []) && _x_0 >= 0 && _x_2 >= 0
                          ==> not (List.mem (List.hd _x_1) y && not (_x_1 = []))
                              || Ordinal.( << ) (Ordinal.Int _x_2) (Ordinal.Int _x_0)
                      • simplify
                        into
                        let (_x_0 : ty_0 list) = List.tl x in
                        let (_x_1 : int) = count.list (const 0) _x_0 in
                        let (_x_2 : int) = count.list (const 0) x in
                        (not (List.mem (List.hd _x_0) y && not (_x_0 = []))
                         || Ordinal.( << ) (Ordinal.Int _x_1) (Ordinal.Int _x_2))
                        || not (((List.mem (List.hd x) y && not (x = [])) && _x_2 >= 0) && _x_1 >= 0)
                        expansions
                        []
                        rewrite_steps
                          forward_chaining
                          • unroll
                            expr
                            (|List.mem_2970| (|get.::.0_2963| x_2975) y_2976)
                            expansions
                            • unroll
                              expr
                              (|`count.list (const 0)[0]`_2988| x_2975)
                              expansions
                              • unroll
                                expr
                                (|`count.list (const 0)[0]`_2988| (|get.::.1_2964| x_2975))
                                expansions
                                • unroll
                                  expr
                                  (|Ordinal.<<_119| (|Ordinal.Int_108|
                                                      (|`count.list (const 0)[0]`_2988| (|get.::.…
                                  expansions
                                  • Unsat

                                  Let's now suppose that we want to verify the transitivity of subset:

                                  In [5]:
                                  #max_induct 1;;
                                  verify (fun x y z -> subset x y && subset y z ==> subset x z) [@@auto]
                                  #max_induct 3;;
                                  
                                  Out[5]:
                                  - : 'a list -> 'a list -> 'a list -> bool = <fun>
                                  Goal:
                                  
                                  subset x y && subset y z ==> subset x z.
                                  
                                  1 nontautological subgoal.
                                  
                                  Subgoal 1:
                                  
                                   H0. subset x y
                                   H1. subset y z
                                  |---------------------------------------------------------------------------
                                   subset x z
                                  
                                  
                                  Must try induction.
                                  
                                  The recursive terms in the conjecture suggest 3 inductions.
                                  Subsumption and merging reduces this to 2.
                                  
                                  However, scheme scoring gives us a clear winner.
                                  We shall induct according to a scheme derived from subset.
                                  
                                  Induction scheme:
                                  
                                   (not (List.mem (List.hd x) z && not (x = [])) ==> φ x y z)
                                   && (not (x = []) && List.mem (List.hd x) z && φ (List.tl x) y z
                                       ==> φ x y z).
                                  
                                  2 nontautological subgoals.
                                  
                                  Subgoal 1.2:
                                  
                                   H0. subset x y
                                   H1. subset y z
                                  |---------------------------------------------------------------------------
                                   C0. List.mem (List.hd x) z && not (x = [])
                                   C1. subset x z
                                  
                                  This simplifies to the following 2 subgoals:
                                  
                                  Subgoal 1.2.2:
                                  
                                   H0. subset x y
                                   H1. subset y z
                                  |---------------------------------------------------------------------------
                                   C0. List.mem (List.hd x) z
                                   C1. subset x z
                                  
                                  This simplifies, using the definition of subset to:
                                  
                                  Subgoal 1.2.2':
                                  
                                   H0. x <> []
                                   H1. subset x y
                                   H2. subset y z
                                  |---------------------------------------------------------------------------
                                   List.mem (List.hd x) z
                                  
                                  
                                  We can eliminate destructors by the following substitution:
                                   x -> x1 :: x2
                                  
                                  This produces the modified subgoal:
                                  
                                  Subgoal 1.2.2'':
                                  
                                   H0. subset (x1 :: x2) y
                                   H1. subset y z
                                  |---------------------------------------------------------------------------
                                   List.mem x1 z
                                  
                                  This simplifies, using the definition of subset to:
                                  
                                  Subgoal 1.2.2''':
                                  
                                   H0. List.mem x1 y
                                   H1. subset x2 y
                                   H2. subset y z
                                  |---------------------------------------------------------------------------
                                   List.mem x1 z
                                  
                                  
                                  Must try induction.
                                  
                                   Aborting proof attempt for _verify_target.
                                  
                                   Rules:
                                      (:def subset)
                                      (:induct subset)
                                  
                                  Checkpoints:
                                  
                                   H0. List.mem x1 y
                                   H1. subset x2 y
                                   H2. subset y z
                                  |---------------------------------------------------------------------------
                                   List.mem x1 z
                                  
                                  Error[/server]: Maximum induction depth reached (1). You can set this with #max_induct.
                                  

                                  It looks like Imandra needs an additional lemma in order to prove this. By inspecting the checkpoint, it looks like all we need is a rule relating subset and List.mem. Let's attempt to prove it:

                                  In [6]:
                                  lemma mem_subset x y z =
                                    List.mem x y && subset y z ==> List.mem x z
                                  [@@auto] [@@rewrite]
                                  
                                  Out[6]:
                                  val mem_subset : 'a -> 'a list -> 'a list -> bool = <fun>
                                  Goal:
                                  
                                  List.mem x y && subset y z ==> List.mem x z.
                                  
                                  1 nontautological subgoal.
                                  
                                  Subgoal 1:
                                  
                                   H0. List.mem x y
                                   H1. subset y z
                                  |---------------------------------------------------------------------------
                                   List.mem x z
                                  
                                  
                                  Must try induction.
                                  
                                  The recursive terms in the conjecture suggest 3 inductions.
                                  Subsumption and merging reduces this to 2.
                                  
                                  Only 1 of those schemes are unflawed.
                                  We shall induct according to a scheme derived from subset.
                                  
                                  Induction scheme:
                                  
                                   (not (List.mem (List.hd y) z && not (y = [])) ==> φ x y z)
                                   && (not (y = []) && List.mem (List.hd y) z && φ x (List.tl y) z
                                       ==> φ x y z).
                                  
                                  2 nontautological subgoals.
                                  
                                  Subgoal 1.2:
                                  
                                   H0. List.mem x y
                                   H1. subset y z
                                  |---------------------------------------------------------------------------
                                   C0. List.mem (List.hd y) z && not (y = [])
                                   C1. List.mem x z
                                  
                                  This simplifies to the following 2 subgoals:
                                  
                                  Subgoal 1.2.2:
                                  
                                   H0. List.mem x y
                                   H1. subset y z
                                  |---------------------------------------------------------------------------
                                   C0. List.mem (List.hd y) z
                                   C1. List.mem x z
                                  
                                  But simplification reduces this to true, using the definitions of List.mem
                                  and subset.
                                  
                                  Subgoal 1.2.1:
                                  
                                   H0. y = []
                                   H1. List.mem x y
                                   H2. subset y z
                                   H3. List.mem (List.hd y) z
                                  |---------------------------------------------------------------------------
                                   List.mem x z
                                  
                                  But simplification reduces this to true, using the definitions of List.mem
                                  and subset.
                                  
                                  Subgoal 1.1:
                                  
                                   H0. List.mem x y
                                   H1. subset y z
                                   H2. not (y = [])
                                   H3. List.mem (List.hd y) z
                                   H4. List.mem x (List.tl y) && subset (List.tl y) z ==> List.mem x z
                                  |---------------------------------------------------------------------------
                                   List.mem x z
                                  
                                  But simplification reduces this to true, using the definitions of List.mem
                                  and subset.
                                  
                                   Rules:
                                      (:def List.mem)
                                      (:def subset)
                                      (:induct subset)
                                  
                                  File "jupyter cell 6", line 1, characters 0-91Error: in rewrite rule,
                                  variable (y : 'a list) does not occur in left-hand side of the rule
                                  or in `[@trigger rw]` terms
                                  See https://docs.imandra.ai/imandra-docs/notebooks/verification-simplification/#Rewrite-Rules
                                  
                                  Proved
                                  proof
                                  ground_instances0
                                  definitions6
                                  inductions1
                                  search_time
                                  0.366s
                                  Expand
                                  • start[0.366s, "Goal"]
                                      List.mem :var_0: :var_1: && subset :var_1: :var_2:
                                      ==> List.mem :var_0: :var_2:
                                  • subproof

                                    (not (List.mem x y) || not (subset y z)) || List.mem x z
                                    • start[0.366s, "1"] (not (List.mem x y) || not (subset y z)) || List.mem x z
                                    • induction on (functional )
                                      :scheme (not (List.mem (List.hd y) z && not (y = [])) ==> φ x y z)
                                              && (not (y = []) && List.mem (List.hd y) z && φ x (List.tl y) z
                                                  ==> φ x y z)
                                    • Split (let (_x_0 : bool) = List.mem (List.hd y) z in
                                             let (_x_1 : bool) = not (y = []) in
                                             let (_x_2 : bool) = not (List.mem x y) in
                                             let (_x_3 : bool) = not (subset y z) in
                                             let (_x_4 : bool) = List.mem x z in
                                             let (_x_5 : sko_ty_0 list) = List.tl y in
                                             (((_x_0 && _x_1 || _x_2) || _x_3) || _x_4)
                                             && (((_x_2 || _x_3) || _x_4)
                                                 || not
                                                    ((_x_1 && _x_0)
                                                     && ((_x_4 || not (List.mem x _x_5)) || not (subset _x_5 z))))
                                             :cases [((not (List.mem x y) || not (subset y z))
                                                      || List.mem (List.hd y) z && not (y = []))
                                                     || List.mem x z;
                                                     let (_x_0 : sko_ty_0 list) = List.tl y in
                                                     let (_x_1 : bool) = List.mem x z in
                                                     ((((not (List.mem x y) || not (subset y z)) || y = [])
                                                       || not (List.mem (List.hd y) z))
                                                      || not (List.mem x _x_0 && subset _x_0 z ==> _x_1))
                                                     || _x_1])
                                      • subproof
                                        let (_x_0 : sko_ty_0 list) = List.tl y in let (_x_1 : bool) = List.mem x z in ((((not (List.mem x y) || not (subset y z)) || y = []) || not (List.mem (List.hd y) z)) || not (List.mem x _x_0 && subset _x_0 z ==> _x_1)) || _x_1
                                        • start[0.265s, "1.1"]
                                            let (_x_0 : sko_ty_0 list) = List.tl y in
                                            let (_x_1 : bool) = List.mem x z in
                                            ((((not (List.mem x y) || not (subset y z)) || y = [])
                                              || not (List.mem (List.hd y) z))
                                             || not (List.mem x _x_0 && subset _x_0 z ==> _x_1))
                                            || _x_1
                                        • simplify
                                          into
                                          true
                                          expansions
                                          [List.mem, subset]
                                          rewrite_steps
                                            forward_chaining
                                          • subproof
                                            ((not (List.mem x y) || not (subset y z)) || List.mem (List.hd y) z && not (y = [])) || List.mem x z
                                            • start[0.265s, "1.2"]
                                                ((not (List.mem x y) || not (subset y z))
                                                 || List.mem (List.hd y) z && not (y = []))
                                                || List.mem x z
                                            • simplify
                                              into
                                              let (_x_0 : bool) = List.mem (List.hd y) z in
                                              let (_x_1 : bool) = not (List.mem x y) in
                                              let (_x_2 : bool) = not (subset y z) in
                                              let (_x_3 : bool) = List.mem x z in
                                              (((_x_0 || _x_1) || _x_2) || _x_3)
                                              && ((((not (y = []) || _x_1) || _x_2) || _x_3) || not _x_0)
                                              expansions
                                              []
                                              rewrite_steps
                                                forward_chaining
                                                  • Subproof
                                                  • Subproof

                                          While Imandra was successful in proving this lemma, it raised an error while trying to turn this lemma into a rewrite rule. As Imandra tells us, this is because the free variable y does not appear in the lhs term List.mem x z. If we, however, annotate the subset y z term with the appropriate [@trigger rw] attribute, Imandra can then successfully turn this term into a valid rewrite rule:

                                          In [7]:
                                          lemma mem_subset x y z =
                                            List.mem x y && (subset y z [@trigger rw]) ==> List.mem x z
                                          [@@auto] [@@rewrite]
                                          
                                          Out[7]:
                                          val mem_subset : 'a -> 'a list -> 'a list -> bool = <fun>
                                          Goal:
                                          
                                          List.mem x y && subset y z ==> List.mem x z.
                                          
                                          1 nontautological subgoal.
                                          
                                          Subgoal 1:
                                          
                                           H0. List.mem x y
                                           H1. subset y z
                                          |---------------------------------------------------------------------------
                                           List.mem x z
                                          
                                          
                                          Must try induction.
                                          
                                          The recursive terms in the conjecture suggest 3 inductions.
                                          Subsumption and merging reduces this to 2.
                                          
                                          Only 1 of those schemes are unflawed.
                                          We shall induct according to a scheme derived from subset.
                                          
                                          Induction scheme:
                                          
                                           (not (List.mem (List.hd y) z && not (y = [])) ==> φ x y z)
                                           && (not (y = []) && List.mem (List.hd y) z && φ x (List.tl y) z
                                               ==> φ x y z).
                                          
                                          2 nontautological subgoals.
                                          
                                          Subgoal 1.2:
                                          
                                           H0. List.mem x y
                                           H1. subset y z
                                          |---------------------------------------------------------------------------
                                           C0. List.mem (List.hd y) z && not (y = [])
                                           C1. List.mem x z
                                          
                                          This simplifies to the following 2 subgoals:
                                          
                                          Subgoal 1.2.2:
                                          
                                           H0. List.mem x y
                                           H1. subset y z
                                          |---------------------------------------------------------------------------
                                           C0. List.mem (List.hd y) z
                                           C1. List.mem x z
                                          
                                          But simplification reduces this to true, using the definitions of List.mem
                                          and subset.
                                          
                                          Subgoal 1.2.1:
                                          
                                           H0. y = []
                                           H1. List.mem x y
                                           H2. subset y z
                                           H3. List.mem (List.hd y) z
                                          |---------------------------------------------------------------------------
                                           List.mem x z
                                          
                                          But simplification reduces this to true, using the definitions of List.mem
                                          and subset.
                                          
                                          Subgoal 1.1:
                                          
                                           H0. List.mem x y
                                           H1. subset y z
                                           H2. not (y = [])
                                           H3. List.mem (List.hd y) z
                                           H4. List.mem x (List.tl y) && subset (List.tl y) z ==> List.mem x z
                                          |---------------------------------------------------------------------------
                                           List.mem x z
                                          
                                          But simplification reduces this to true, using the definitions of List.mem
                                          and subset.
                                          
                                           Rules:
                                              (:def List.mem)
                                              (:def subset)
                                              (:induct subset)
                                          
                                          
                                          Proved
                                          proof
                                          ground_instances0
                                          definitions7
                                          inductions1
                                          search_time
                                          0.601s
                                          Expand
                                          • start[0.601s, "Goal"]
                                              List.mem :var_0: :var_1: && subset :var_1: :var_2:
                                              ==> List.mem :var_0: :var_2:
                                          • subproof

                                            (not (List.mem x y) || not (subset y z)) || List.mem x z
                                            • start[0.600s, "1"] (not (List.mem x y) || not (subset y z)) || List.mem x z
                                            • induction on (functional )
                                              :scheme (not (List.mem (List.hd y) z && not (y = [])) ==> φ x y z)
                                                      && (not (y = []) && List.mem (List.hd y) z && φ x (List.tl y) z
                                                          ==> φ x y z)
                                            • Split (let (_x_0 : bool) = List.mem (List.hd y) z in
                                                     let (_x_1 : bool) = not (y = []) in
                                                     let (_x_2 : bool) = not (List.mem x y) in
                                                     let (_x_3 : bool) = not (subset y z) in
                                                     let (_x_4 : bool) = List.mem x z in
                                                     let (_x_5 : sko_ty_0 list) = List.tl y in
                                                     (((_x_0 && _x_1 || _x_2) || _x_3) || _x_4)
                                                     && (((_x_2 || _x_3) || _x_4)
                                                         || not
                                                            ((_x_1 && _x_0)
                                                             && ((_x_4 || not (List.mem x _x_5)) || not (subset _x_5 z))))
                                                     :cases [((not (List.mem x y) || not (subset y z))
                                                              || List.mem (List.hd y) z && not (y = []))
                                                             || List.mem x z;
                                                             let (_x_0 : sko_ty_0 list) = List.tl y in
                                                             let (_x_1 : bool) = List.mem x z in
                                                             ((((not (List.mem x y) || not (subset y z)) || y = [])
                                                               || not (List.mem (List.hd y) z))
                                                              || not (List.mem x _x_0 && subset _x_0 z ==> _x_1))
                                                             || _x_1])
                                              • subproof
                                                let (_x_0 : sko_ty_0 list) = List.tl y in let (_x_1 : bool) = List.mem x z in ((((not (List.mem x y) || not (subset y z)) || y = []) || not (List.mem (List.hd y) z)) || not (List.mem x _x_0 && subset _x_0 z ==> _x_1)) || _x_1
                                                • start[0.475s, "1.1"]
                                                    let (_x_0 : sko_ty_0 list) = List.tl y in
                                                    let (_x_1 : bool) = List.mem x z in
                                                    ((((not (List.mem x y) || not (subset y z)) || y = [])
                                                      || not (List.mem (List.hd y) z))
                                                     || not (List.mem x _x_0 && subset _x_0 z ==> _x_1))
                                                    || _x_1
                                                • simplify
                                                  into
                                                  true
                                                  expansions
                                                  [subset, List.mem, subset]
                                                  rewrite_steps
                                                    forward_chaining
                                                  • subproof
                                                    ((not (List.mem x y) || not (subset y z)) || List.mem (List.hd y) z && not (y = [])) || List.mem x z
                                                    • start[0.475s, "1.2"]
                                                        ((not (List.mem x y) || not (subset y z))
                                                         || List.mem (List.hd y) z && not (y = []))
                                                        || List.mem x z
                                                    • simplify
                                                      into
                                                      let (_x_0 : bool) = List.mem (List.hd y) z in
                                                      let (_x_1 : bool) = not (List.mem x y) in
                                                      let (_x_2 : bool) = not (subset y z) in
                                                      let (_x_3 : bool) = List.mem x z in
                                                      (((_x_0 || _x_1) || _x_2) || _x_3)
                                                      && ((((not (y = []) || _x_1) || _x_2) || _x_3) || not _x_0)
                                                      expansions
                                                      []
                                                      rewrite_steps
                                                        forward_chaining
                                                          • Subproof
                                                          • Subproof

                                                  And finally, let's verify that the rewrite rule can indeed match as we expect:

                                                  In [8]:
                                                  verify (fun x y z -> subset x y && subset y z ==> subset x z) [@@auto]
                                                  
                                                  Out[8]:
                                                  - : 'a list -> 'a list -> 'a list -> bool = <fun>
                                                  Goal:
                                                  
                                                  subset x y && subset y z ==> subset x z.
                                                  
                                                  1 nontautological subgoal.
                                                  
                                                  Subgoal 1:
                                                  
                                                   H0. subset x y
                                                   H1. subset y z
                                                  |---------------------------------------------------------------------------
                                                   subset x z
                                                  
                                                  
                                                  Must try induction.
                                                  
                                                  The recursive terms in the conjecture suggest 3 inductions.
                                                  Subsumption and merging reduces this to 2.
                                                  
                                                  However, scheme scoring gives us a clear winner.
                                                  We shall induct according to a scheme derived from subset.
                                                  
                                                  Induction scheme:
                                                  
                                                   (not (List.mem (List.hd x) z && not (x = [])) ==> φ x y z)
                                                   && (not (x = []) && List.mem (List.hd x) z && φ (List.tl x) y z
                                                       ==> φ x y z).
                                                  
                                                  2 nontautological subgoals.
                                                  
                                                  Subgoal 1.2:
                                                  
                                                   H0. subset x y
                                                   H1. subset y z
                                                  |---------------------------------------------------------------------------
                                                   C0. List.mem (List.hd x) z && not (x = [])
                                                   C1. subset x z
                                                  
                                                  This simplifies to the following 2 subgoals:
                                                  
                                                  Subgoal 1.2.2:
                                                  
                                                   H0. subset x y
                                                   H1. subset y z
                                                  |---------------------------------------------------------------------------
                                                   C0. List.mem (List.hd x) z
                                                   C1. subset x z
                                                  
                                                  But simplification reduces this to true, using the definitions of List.mem
                                                  and subset, and the rewrite rule mem_subset.
                                                  
                                                  Subgoal 1.2.1:
                                                  
                                                   H0. x = []
                                                   H1. subset x y
                                                   H2. subset y z
                                                   H3. List.mem (List.hd x) z
                                                  |---------------------------------------------------------------------------
                                                   subset x z
                                                  
                                                  But simplification reduces this to true, using the definition of subset.
                                                  
                                                  Subgoal 1.1:
                                                  
                                                   H0. subset x y
                                                   H1. subset y z
                                                   H2. not (x = [])
                                                   H3. List.mem (List.hd x) z
                                                   H4. subset y z && subset (List.tl x) y ==> subset (List.tl x) z
                                                  |---------------------------------------------------------------------------
                                                   subset x z
                                                  
                                                  But simplification reduces this to true, using the definitions of List.mem
                                                  and subset, and the rewrite rule mem_subset.
                                                  
                                                   Rules:
                                                      (:def List.mem)
                                                      (:def subset)
                                                      (:rw mem_subset)
                                                      (:induct subset)
                                                  
                                                  
                                                  Proved
                                                  proof
                                                  ground_instances0
                                                  definitions8
                                                  inductions1
                                                  search_time
                                                  0.354s
                                                  Expand
                                                  • start[0.354s, "Goal"]
                                                      subset :var_0: :var_1: && subset :var_1: :var_2: ==> subset :var_0: :var_2:
                                                  • subproof

                                                    (not (subset x y) || not (subset y z)) || subset x z
                                                    • start[0.354s, "1"] (not (subset x y) || not (subset y z)) || subset x z
                                                    • induction on (functional )
                                                      :scheme (not (List.mem (List.hd x) z && not (x = [])) ==> φ x y z)
                                                              && (not (x = []) && List.mem (List.hd x) z && φ (List.tl x) y z
                                                                  ==> φ x y z)
                                                    • Split (let (_x_0 : bool) = List.mem (List.hd x) z in
                                                             let (_x_1 : bool) = not (x = []) in
                                                             let (_x_2 : bool) = not (subset x y) in
                                                             let (_x_3 : bool) = not (subset y z) in
                                                             let (_x_4 : bool) = subset x z in
                                                             let (_x_5 : sko_ty_0 list) = List.tl x in
                                                             (((_x_0 && _x_1 || _x_2) || _x_3) || _x_4)
                                                             && (((_x_2 || _x_3) || _x_4)
                                                                 || not
                                                                    ((_x_1 && _x_0)
                                                                     && ((_x_3 || not (subset _x_5 y)) || subset _x_5 z)))
                                                             :cases [((not (subset x y) || not (subset y z))
                                                                      || List.mem (List.hd x) z && not (x = []))
                                                                     || subset x z;
                                                                     let (_x_0 : bool) = subset y z in
                                                                     let (_x_1 : sko_ty_0 list) = List.tl x in
                                                                     ((((not (subset x y) || not _x_0) || x = [])
                                                                       || not (List.mem (List.hd x) z))
                                                                      || not (_x_0 && subset _x_1 y ==> subset _x_1 z))
                                                                     || subset x z])
                                                      • subproof
                                                        let (_x_0 : bool) = subset y z in let (_x_1 : sko_ty_0 list) = List.tl x in ((((not (subset x y) || not _x_0) || x = []) || not (List.mem (List.hd x) z)) || not (_x_0 && subset _x_1 y ==> subset _x_1 z)) || subset x z
                                                        • start[0.260s, "1.1"]
                                                            let (_x_0 : bool) = subset y z in
                                                            let (_x_1 : sko_ty_0 list) = List.tl x in
                                                            ((((not (subset x y) || not _x_0) || x = [])
                                                              || not (List.mem (List.hd x) z))
                                                             || not (_x_0 && subset _x_1 y ==> subset _x_1 z))
                                                            || subset x z
                                                        • simplify
                                                          into
                                                          true
                                                          expansions
                                                          [subset, subset, List.mem, List.mem, List.mem]
                                                          rewrite_steps
                                                          • mem_subset
                                                          • mem_subset
                                                          • mem_subset
                                                          • mem_subset
                                                          forward_chaining
                                                        • subproof
                                                          ((not (subset x y) || not (subset y z)) || List.mem (List.hd x) z && not (x = [])) || subset x z
                                                          • start[0.260s, "1.2"]
                                                              ((not (subset x y) || not (subset y z))
                                                               || List.mem (List.hd x) z && not (x = []))
                                                              || subset x z
                                                          • simplify
                                                            into
                                                            let (_x_0 : bool) = List.mem (List.hd x) z in
                                                            let (_x_1 : bool) = not (subset x y) in
                                                            let (_x_2 : bool) = not (subset y z) in
                                                            let (_x_3 : bool) = subset x z in
                                                            (((_x_0 || _x_1) || _x_2) || _x_3)
                                                            && ((((not (x = []) || _x_1) || _x_2) || _x_3) || not _x_0)
                                                            expansions
                                                            []
                                                            rewrite_steps
                                                              forward_chaining
                                                                • Subproof
                                                                • Subproof

                                                        Permutative Restriction

                                                        The @@permutative annotation applies only to rewrite rules and is used to restrict the rule so that it will only apply if the instantiated rhs is lexicographically smaller than the matched lhs. This restriction can be particularly useful in order to break out of infinite rewrite loops while trying to "canonicalize" a form, for example distributing the "simplest" terms to the left.

                                                        Let's say we want to prove the commutativity of Peano_nat.plus and install it as a rewrite rule:

                                                        In [9]:
                                                        lemma comm_plus x y =
                                                          Peano_nat.(plus x y = plus y x)
                                                         [@@auto] [@@rw] [@@permutative]
                                                        
                                                        Out[9]:
                                                        val comm_plus : Peano_nat.t -> Peano_nat.t -> bool = <fun>
                                                        Goal:
                                                        
                                                        Peano_nat.( = ) (Peano_nat.plus x y) (Peano_nat.plus y x).
                                                        
                                                        1 nontautological subgoal.
                                                        
                                                        Subgoal 1:
                                                        
                                                        |---------------------------------------------------------------------------
                                                         Peano_nat.plus x y = Peano_nat.plus y x
                                                        
                                                        
                                                        Must try induction.
                                                        
                                                        The recursive terms in the conjecture suggest 2 inductions.
                                                        
                                                        As we have backtracked, we break ties by purity.
                                                        We shall induct according to a scheme derived from Peano_nat.plus.
                                                        
                                                        Induction scheme:
                                                        
                                                         (x = Peano_nat.Z ==> φ x y)
                                                         && (not (x = Peano_nat.Z) && φ (Destruct(Peano_nat.S, 0, x)) y ==> φ x y).
                                                        
                                                        2 nontautological subgoals.
                                                        
                                                        Subgoal 1.2:
                                                        
                                                         H0. x = Peano_nat.Z
                                                        |---------------------------------------------------------------------------
                                                         Peano_nat.plus x y = Peano_nat.plus y x
                                                        
                                                        This simplifies, using the definition of Peano_nat.plus to:
                                                        
                                                        Subgoal 1.2':
                                                        
                                                        |---------------------------------------------------------------------------
                                                         y = Peano_nat.plus y Peano_nat.Z
                                                        
                                                        
                                                        Must try induction.
                                                        
                                                        We shall induct according to a scheme derived from Peano_nat.plus.
                                                        
                                                        Induction scheme:
                                                        
                                                         (y = Peano_nat.Z ==> φ y)
                                                         && (not (y = Peano_nat.Z) && φ (Destruct(Peano_nat.S, 0, y)) ==> φ y).
                                                        
                                                        2 nontautological subgoals.
                                                        
                                                        Subgoal 1.2'.2:
                                                        
                                                         H0. y = Peano_nat.Z
                                                        |---------------------------------------------------------------------------
                                                         y = Peano_nat.plus y Peano_nat.Z
                                                        
                                                        But simplification reduces this to true, using the definition of
                                                        Peano_nat.plus.
                                                        
                                                        Subgoal 1.2'.1:
                                                        
                                                         H0. not (y = Peano_nat.Z)
                                                         H1. Destruct(Peano_nat.S, 0, y) =
                                                             Peano_nat.plus (Destruct(Peano_nat.S, 0, y)) Peano_nat.Z
                                                        |---------------------------------------------------------------------------
                                                         y = Peano_nat.plus y Peano_nat.Z
                                                        
                                                        But simplification reduces this to true, using the definition of
                                                        Peano_nat.plus.
                                                        
                                                        Subgoal 1.1:
                                                        
                                                         H0. not (x = Peano_nat.Z)
                                                         H1. Peano_nat.plus (Destruct(Peano_nat.S, 0, x)) y =
                                                             Peano_nat.plus y (Destruct(Peano_nat.S, 0, x))
                                                        |---------------------------------------------------------------------------
                                                         Peano_nat.plus x y = Peano_nat.plus y x
                                                        
                                                        This simplifies, using the definition of Peano_nat.plus to:
                                                        
                                                        Subgoal 1.1':
                                                        
                                                         H0. Peano_nat.plus (Destruct(Peano_nat.S, 0, x)) y =
                                                             Peano_nat.plus y (Destruct(Peano_nat.S, 0, x))
                                                        |---------------------------------------------------------------------------
                                                         C0. x = Peano_nat.Z
                                                         C1. Peano_nat.S (Peano_nat.plus (Destruct(Peano_nat.S, 0, x)) y) =
                                                             Peano_nat.plus y x
                                                        
                                                        
                                                        We can eliminate destructors by the following
                                                        substitution:
                                                         x -> Peano_nat.S x1
                                                        
                                                        This produces the modified subgoal:
                                                        
                                                        Subgoal 1.1'':
                                                        
                                                         H0. Peano_nat.plus x1 y = Peano_nat.plus y x1
                                                        |---------------------------------------------------------------------------
                                                         Peano_nat.S (Peano_nat.plus x1 y) = Peano_nat.plus y (Peano_nat.S x1)
                                                        
                                                        
                                                        Cross-fertilizing with:
                                                        
                                                         Peano_nat.plus x1 y = Peano_nat.plus y x1
                                                        
                                                        This produces the modified subgoal:
                                                        
                                                        Subgoal 1.1''':
                                                        
                                                        |---------------------------------------------------------------------------
                                                         Peano_nat.S (Peano_nat.plus y x1) = Peano_nat.plus y (Peano_nat.S x1)
                                                        
                                                        
                                                        Must try induction.
                                                        
                                                        The recursive terms in the conjecture suggest 2 inductions.
                                                        Subsumption and merging reduces this to 1.
                                                        
                                                        We shall induct according to a scheme derived from Peano_nat.plus.
                                                        
                                                        Induction scheme:
                                                        
                                                         (y = Peano_nat.Z ==> φ x1 y)
                                                         && (not (y = Peano_nat.Z) && φ x1 (Destruct(Peano_nat.S, 0, y)) ==> φ x1 y).
                                                        
                                                        2 nontautological subgoals.
                                                        
                                                        Subgoal 1.1'''.2:
                                                        
                                                         H0. y = Peano_nat.Z
                                                        |---------------------------------------------------------------------------
                                                         Peano_nat.S (Peano_nat.plus y x1) = Peano_nat.plus y (Peano_nat.S x1)
                                                        
                                                        But simplification reduces this to true, using the definition of
                                                        Peano_nat.plus.
                                                        
                                                        Subgoal 1.1'''.1:
                                                        
                                                         H0. not (y = Peano_nat.Z)
                                                         H1. Peano_nat.S (Peano_nat.plus (Destruct(Peano_nat.S, 0, y)) x1) =
                                                             Peano_nat.plus (Destruct(Peano_nat.S, 0, y)) (Peano_nat.S x1)
                                                        |---------------------------------------------------------------------------
                                                         Peano_nat.S (Peano_nat.plus y x1) = Peano_nat.plus y (Peano_nat.S x1)
                                                        
                                                        But simplification reduces this to true, using the definition of
                                                        Peano_nat.plus.
                                                        
                                                         Rules:
                                                            (:def Peano_nat.plus)
                                                            (:induct Peano_nat.plus)
                                                        
                                                        
                                                        Proved
                                                        proof
                                                        ground_instances0
                                                        definitions8
                                                        inductions3
                                                        search_time
                                                        0.441s
                                                        Expand
                                                        • start[0.441s, "Goal"]
                                                            Peano_nat.plus :var_0: :var_1: = Peano_nat.plus :var_1: :var_0:
                                                        • subproof

                                                          Peano_nat.plus x y = Peano_nat.plus y x
                                                          • start[0.441s, "1"] Peano_nat.plus x y = Peano_nat.plus y x
                                                          • induction on (functional )
                                                            :scheme (x = Peano_nat.Z ==> φ x y)
                                                                    && (not (x = Peano_nat.Z) && φ (Destruct(Peano_nat.S, 0, x)) y
                                                                        ==> φ x y)
                                                          • Split (let (_x_0 : bool) = not (x = Peano_nat.Z) in
                                                                   let (_x_1 : bool) = Peano_nat.plus x y = Peano_nat.plus y x in
                                                                   let (_x_2 : Peano_nat.t) = Destruct(Peano_nat.S, 0, x) in
                                                                   (_x_0 || _x_1)
                                                                   && (not (_x_0 && Peano_nat.plus _x_2 y = Peano_nat.plus y _x_2)
                                                                       || _x_1)
                                                                   :cases [not (x = Peano_nat.Z)
                                                                           || Peano_nat.plus x y = Peano_nat.plus y x;
                                                                           let (_x_0 : Peano_nat.t) = Destruct(Peano_nat.S, 0, x) in
                                                                           (x = Peano_nat.Z
                                                                            || not (Peano_nat.plus _x_0 y = Peano_nat.plus y _x_0))
                                                                           || Peano_nat.plus x y = Peano_nat.plus y x])
                                                            • subproof
                                                              let (_x_0 : Peano_nat.t) = Destruct(Peano_nat.S, 0, x) in (x = Peano_nat.Z || not (Peano_nat.plus _x_0 y = Peano_nat.plus y _x_0)) || Peano_nat.plus x y = Peano_nat.plus y x
                                                              • start[0.392s, "1.1"]
                                                                  let (_x_0 : Peano_nat.t) = Destruct(Peano_nat.S, 0, x) in
                                                                  (x = Peano_nat.Z || not (Peano_nat.plus _x_0 y = Peano_nat.plus y _x_0))
                                                                  || Peano_nat.plus x y = Peano_nat.plus y x
                                                              • simplify
                                                                into
                                                                let (_x_0 : Peano_nat.t) = Destruct(Peano_nat.S, 0, x) in
                                                                let (_x_1 : Peano_nat.t) = Peano_nat.plus _x_0 y in
                                                                (x = Peano_nat.Z || not (_x_1 = Peano_nat.plus y _x_0))
                                                                || Peano_nat.S _x_1 = Peano_nat.plus y x
                                                                expansions
                                                                Peano_nat.plus
                                                                rewrite_steps
                                                                  forward_chaining
                                                                  • Elim_destructor (:cstor Peano_nat.S :replace Peano_nat.S x1 :context [])
                                                                  • induction on (functional )
                                                                    :scheme (y = Peano_nat.Z ==> φ x1 y)
                                                                            && (not (y = Peano_nat.Z) && φ x1 (Destruct(Peano_nat.S, 0, y))
                                                                                ==> φ x1 y)
                                                                  • Split (let (_x_0 : bool) = not (y = Peano_nat.Z) in
                                                                           let (_x_1 : Peano_nat.t) = Peano_nat.S x1 in
                                                                           let (_x_2 : bool)
                                                                               = Peano_nat.S (Peano_nat.plus y x1) = Peano_nat.plus y _x_1
                                                                           in
                                                                           let (_x_3 : Peano_nat.t) = Destruct(Peano_nat.S, 0, y) in
                                                                           (_x_0 || _x_2)
                                                                           && (not
                                                                               (_x_0
                                                                                && Peano_nat.S (Peano_nat.plus _x_3 x1) =
                                                                                   Peano_nat.plus _x_3 _x_1)
                                                                               || _x_2)
                                                                           :cases [not (y = Peano_nat.Z)
                                                                                   || Peano_nat.S (Peano_nat.plus y x1) =
                                                                                      Peano_nat.plus y (Peano_nat.S x1);
                                                                                   let (_x_0 : Peano_nat.t) = Destruct(Peano_nat.S, 0, y) in
                                                                                   let (_x_1 : Peano_nat.t) = Peano_nat.S x1 in
                                                                                   (y = Peano_nat.Z
                                                                                    || not
                                                                                       (Peano_nat.S (Peano_nat.plus _x_0 x1) =
                                                                                        Peano_nat.plus _x_0 _x_1))
                                                                                   || Peano_nat.S (Peano_nat.plus y x1) = Peano_nat.plus y _x_1])
                                                                    • Subproof
                                                                    • Subproof
                                                                • subproof
                                                                  not (x = Peano_nat.Z) || Peano_nat.plus x y = Peano_nat.plus y x
                                                                  • start[0.392s, "1.2"]
                                                                      not (x = Peano_nat.Z) || Peano_nat.plus x y = Peano_nat.plus y x
                                                                  • simplify
                                                                    into
                                                                    y = Peano_nat.plus y Peano_nat.Z
                                                                    expansions
                                                                    Peano_nat.plus
                                                                    rewrite_steps
                                                                      forward_chaining
                                                                      • induction on (functional )
                                                                        :scheme (y = Peano_nat.Z ==> φ y)
                                                                                && (not (y = Peano_nat.Z) && φ (Destruct(Peano_nat.S, 0, y))
                                                                                    ==> φ y)
                                                                      • Split (let (_x_0 : bool) = not (y = Peano_nat.Z) in
                                                                               let (_x_1 : bool) = y = Peano_nat.plus y Peano_nat.Z in
                                                                               let (_x_2 : Peano_nat.t) = Destruct(Peano_nat.S, 0, y) in
                                                                               (_x_0 || _x_1)
                                                                               && (not (_x_0 && _x_2 = Peano_nat.plus _x_2 Peano_nat.Z) || _x_1)
                                                                               :cases [not (y = Peano_nat.Z) || y = Peano_nat.plus y Peano_nat.Z;
                                                                                       let (_x_0 : Peano_nat.t) = Destruct(Peano_nat.S, 0, y) in
                                                                                       (y = Peano_nat.Z
                                                                                        || not (_x_0 = Peano_nat.plus _x_0 Peano_nat.Z))
                                                                                       || y = Peano_nat.plus y Peano_nat.Z])
                                                                        • Subproof
                                                                        • Subproof

                                                                Had we not restricted comm_plus as a permutative rule, the simplifier would have entered a rewrite loop every time it encountered a term matching plus <x> <y>, while with the permutative restriction in place, this has the effect of directing all the "simplest" terms to the left of plus, which will help with making further rewrite rules applicable and with simplification in general.

                                                                Forward-chaining Rules

                                                                Forward chaining is the second type of rule that Imandra allows us to register and participate automatically in proofs.

                                                                A forward chaining rule is a theorem containing a collection of trigger terms which must include all free variables of the theorem. If Imandra can appropriately match the triggers with terms in the goal, then an instantiation of the rule is added to the context. The context of a goal is not displayed in the goal itself (i.e., when the goal is printed), but is rather used in the background to aid the simplifier in closing branches, relieving hypotheses of rewrite rules during backchaining, and so on.

                                                                For example, let us prove the following theorem and install it as a forward-chaining rule:

                                                                In [10]:
                                                                lemma len_nonnegative x =
                                                                  List.length x [@trigger] >= 0
                                                                [@@simp] [@@fc]
                                                                
                                                                Out[10]:
                                                                val len_nonnegative : 'a list -> bool = <fun>
                                                                
                                                                Proved
                                                                proof
                                                                ground_instances0
                                                                definitions0
                                                                inductions0
                                                                search_time
                                                                0.056s
                                                                details
                                                                Expand
                                                                smt_stats
                                                                rlimit count470
                                                                mk bool var1
                                                                memory47.690000
                                                                max memory68.950000
                                                                num allocs17541209720.000000
                                                                Expand
                                                                • start[0.056s] List.length :var_0: >= 0
                                                                • simplify

                                                                  into
                                                                  true
                                                                  expansions
                                                                  []
                                                                  rewrite_steps
                                                                    forward_chaining
                                                                    List.len_nonnegative
                                                                  • Unsat

                                                                  Now when Imandra encounters a term of the form List.length <term>, the formula List.length <term> >= 0 will be added to the context of the goal under focus. In other words, a forward chaining rule allows Imandra to extend the database of background logical facts it knows about a goal. These facts are made available to the simplifier, and can thus be used to enhance simplification by closing branches and relieving hypotheses of conditional rewrite rules.

                                                                  A forward chaining rule can contain multiple disjoint triggers. In this case, if either of the triggers matches, the forward chaining rule fires. For example, the following forward chaining version of the rev_len rewrite rule we added above will fire if either List.length x or List.length (List.rev x) matches.

                                                                  In [11]:
                                                                  lemma rev_len_fc x =
                                                                     List.length x [@trigger] = List.length (List.rev x) [@trigger]
                                                                  [@@auto] [@@fc]
                                                                  
                                                                  Out[11]:
                                                                  val rev_len_fc : 'a list -> bool = <fun>
                                                                  Goal:
                                                                  
                                                                  List.length x = List.length (List.rev x).
                                                                  
                                                                  1 nontautological subgoal.
                                                                  
                                                                  Subgoal 1:
                                                                  
                                                                  |---------------------------------------------------------------------------
                                                                   List.length x = List.length (List.rev x)
                                                                  
                                                                  But simplification reduces this to true, using the rewrite rule rev_len.
                                                                  
                                                                   Rules:
                                                                      (:rw rev_len)
                                                                      (:fc List.len_nonnegative)
                                                                      (:fc len_nonnegative)
                                                                  
                                                                  
                                                                  Proved
                                                                  proof
                                                                  ground_instances0
                                                                  definitions0
                                                                  inductions0
                                                                  search_time
                                                                  0.025s
                                                                  Expand
                                                                  • start[0.025s, "Goal"] List.length :var_0: = List.length (List.rev :var_0:)
                                                                  • subproof

                                                                    List.length x = List.length (List.rev x)
                                                                    • start[0.025s, "1"] List.length x = List.length (List.rev x)
                                                                    • simplify
                                                                      into
                                                                      true
                                                                      expansions
                                                                      []
                                                                      rewrite_steps
                                                                      rev_len
                                                                      forward_chaining
                                                                      • List.len_nonnegative
                                                                      • len_nonnegative
                                                                      • List.len_nonnegative
                                                                      • len_nonnegative

                                                                  Additionally, a forward chaining rule can contain multiple conjoined triggers, forming a trigger cluster. In this case, all the triggers must match in order for the forward chaining rule to apply.

                                                                  To create a trigger cluster multiple terms must be annotated with [@trigger <x>i], where <x> is a numeric identifier common to all the triggers in the cluster. For example:

                                                                  In [12]:
                                                                  theorem subset_trans l1 l2 l3 =
                                                                    (subset l1 l2 [@trigger 0i]) && (subset l2 l3 [@trigger 0i])
                                                                    ==>
                                                                    subset l1 l3
                                                                   [@@auto] [@@forward_chaining]
                                                                  
                                                                  Out[12]:
                                                                  val subset_trans : 'a list -> 'a list -> 'a list -> bool = <fun>
                                                                  Goal:
                                                                  
                                                                  subset l1 l2 && subset l2 l3 ==> subset l1 l3.
                                                                  
                                                                  1 nontautological subgoal.
                                                                  
                                                                  Subgoal 1:
                                                                  
                                                                   H0. subset l1 l2
                                                                   H1. subset l2 l3
                                                                  |---------------------------------------------------------------------------
                                                                   subset l1 l3
                                                                  
                                                                  
                                                                  Must try induction.
                                                                  
                                                                  The recursive terms in the conjecture suggest 3 inductions.
                                                                  Subsumption and merging reduces this to 2.
                                                                  
                                                                  However, scheme scoring gives us a clear winner.
                                                                  We shall induct according to a scheme derived from subset.
                                                                  
                                                                  Induction scheme:
                                                                  
                                                                   (not (List.mem (List.hd l1) l3 && not (l1 = [])) ==> φ l1 l2 l3)
                                                                   && (not (l1 = []) && List.mem (List.hd l1) l3 && φ (List.tl l1) l2 l3
                                                                       ==> φ l1 l2 l3).
                                                                  
                                                                  2 nontautological subgoals.
                                                                  
                                                                  Subgoal 1.2:
                                                                  
                                                                   H0. subset l1 l2
                                                                   H1. subset l2 l3
                                                                  |---------------------------------------------------------------------------
                                                                   C0. List.mem (List.hd l1) l3 && not (l1 = [])
                                                                   C1. subset l1 l3
                                                                  
                                                                  This simplifies to the following 2 subgoals:
                                                                  
                                                                  Subgoal 1.2.2:
                                                                  
                                                                   H0. subset l1 l2
                                                                   H1. subset l2 l3
                                                                  |---------------------------------------------------------------------------
                                                                   C0. List.mem (List.hd l1) l3
                                                                   C1. subset l1 l3
                                                                  
                                                                  But simplification reduces this to true, using the definitions of List.mem
                                                                  and subset, and the rewrite rule mem_subset.
                                                                  
                                                                  Subgoal 1.2.1:
                                                                  
                                                                   H0. l1 = []
                                                                   H1. subset l1 l2
                                                                   H2. subset l2 l3
                                                                   H3. List.mem (List.hd l1) l3
                                                                  |---------------------------------------------------------------------------
                                                                   subset l1 l3
                                                                  
                                                                  But simplification reduces this to true, using the definition of subset.
                                                                  
                                                                  Subgoal 1.1:
                                                                  
                                                                   H0. subset l1 l2
                                                                   H1. subset l2 l3
                                                                   H2. not (l1 = [])
                                                                   H3. List.mem (List.hd l1) l3
                                                                   H4. subset l2 l3 && subset (List.tl l1) l2 ==> subset (List.tl l1) l3
                                                                  |---------------------------------------------------------------------------
                                                                   subset l1 l3
                                                                  
                                                                  But simplification reduces this to true, using the definitions of List.mem
                                                                  and subset, and the rewrite rule mem_subset.
                                                                  
                                                                   Rules:
                                                                      (:def List.mem)
                                                                      (:def subset)
                                                                      (:rw mem_subset)
                                                                      (:induct subset)
                                                                  
                                                                  
                                                                  Proved
                                                                  proof
                                                                  ground_instances0
                                                                  definitions8
                                                                  inductions1
                                                                  search_time
                                                                  0.314s
                                                                  Expand
                                                                  • start[0.314s, "Goal"]
                                                                      subset :var_0: :var_1: && subset :var_1: :var_2: ==> subset :var_0: :var_2:
                                                                  • subproof

                                                                    (not (subset l1 l2) || not (subset l2 l3)) || subset l1 l3
                                                                    • start[0.313s, "1"] (not (subset l1 l2) || not (subset l2 l3)) || subset l1 l3
                                                                    • induction on (functional )
                                                                      :scheme (not (List.mem (List.hd l1) l3 && not (l1 = [])) ==> φ l1 l2 l3)
                                                                              && (not (l1 = [])
                                                                                  && List.mem (List.hd l1) l3 && φ (List.tl l1) l2 l3
                                                                                  ==> φ l1 l2 l3)
                                                                    • Split (let (_x_0 : bool) = List.mem (List.hd l1) l3 in
                                                                             let (_x_1 : bool) = not (l1 = []) in
                                                                             let (_x_2 : bool) = not (subset l1 l2) in
                                                                             let (_x_3 : bool) = not (subset l2 l3) in
                                                                             let (_x_4 : bool) = subset l1 l3 in
                                                                             let (_x_5 : sko_ty_0 list) = List.tl l1 in
                                                                             (((_x_0 && _x_1 || _x_2) || _x_3) || _x_4)
                                                                             && (((_x_2 || _x_3) || _x_4)
                                                                                 || not
                                                                                    ((_x_1 && _x_0)
                                                                                     && ((_x_3 || not (subset _x_5 l2)) || subset _x_5 l3)))
                                                                             :cases [((not (subset l1 l2) || not (subset l2 l3))
                                                                                      || List.mem (List.hd l1) l3 && not (l1 = []))
                                                                                     || subset l1 l3;
                                                                                     let (_x_0 : bool) = subset l2 l3 in
                                                                                     let (_x_1 : sko_ty_0 list) = List.tl l1 in
                                                                                     ((((not (subset l1 l2) || not _x_0) || l1 = [])
                                                                                       || not (List.mem (List.hd l1) l3))
                                                                                      || not (_x_0 && subset _x_1 l2 ==> subset _x_1 l3))
                                                                                     || subset l1 l3])
                                                                      • subproof
                                                                        let (_x_0 : bool) = subset l2 l3 in let (_x_1 : sko_ty_0 list) = List.tl l1 in ((((not (subset l1 l2) || not _x_0) || l1 = []) || not (List.mem (List.hd l1) l3)) || not (_x_0 && subset _x_1 l2 ==> subset _x_1 l3)) || subset l1 l3
                                                                        • start[0.219s, "1.1"]
                                                                            let (_x_0 : bool) = subset l2 l3 in
                                                                            let (_x_1 : sko_ty_0 list) = List.tl l1 in
                                                                            ((((not (subset l1 l2) || not _x_0) || l1 = [])
                                                                              || not (List.mem (List.hd l1) l3))
                                                                             || not (_x_0 && subset _x_1 l2 ==> subset _x_1 l3))
                                                                            || subset l1 l3
                                                                        • simplify
                                                                          into
                                                                          true
                                                                          expansions
                                                                          [subset, subset, List.mem, List.mem, List.mem]
                                                                          rewrite_steps
                                                                          • mem_subset
                                                                          • mem_subset
                                                                          • mem_subset
                                                                          • mem_subset
                                                                          forward_chaining
                                                                        • subproof
                                                                          ((not (subset l1 l2) || not (subset l2 l3)) || List.mem (List.hd l1) l3 && not (l1 = [])) || subset l1 l3
                                                                          • start[0.219s, "1.2"]
                                                                              ((not (subset l1 l2) || not (subset l2 l3))
                                                                               || List.mem (List.hd l1) l3 && not (l1 = []))
                                                                              || subset l1 l3
                                                                          • simplify
                                                                            into
                                                                            let (_x_0 : bool) = List.mem (List.hd l1) l3 in
                                                                            let (_x_1 : bool) = not (subset l1 l2) in
                                                                            let (_x_2 : bool) = not (subset l2 l3) in
                                                                            let (_x_3 : bool) = subset l1 l3 in
                                                                            (((_x_0 || _x_1) || _x_2) || _x_3)
                                                                            && ((((not (l1 = []) || _x_1) || _x_2) || _x_3) || not _x_0)
                                                                            expansions
                                                                            []
                                                                            rewrite_steps
                                                                              forward_chaining
                                                                                • Subproof
                                                                                • Subproof

                                                                        This forward chaining rule will match only if a goal contains terms that match both subset l1 l2 and subset l2 l3.

                                                                        It should be noted that Imandra supports automatic trigger selection, meaning it's often not necessary to annotate the trigger terms manually. Imandra can typically infer for us both simple triggers and trigger clusters. In fact for both the single trigger examples above, we could have omitted the trigger annotations altogether, and Imandra would have found some for us automatically.