Library Containers.SetDecide


This file implements a decision procedure for a certain class of propositions involving finite sets.
Require Import SetInterface SetFacts.
Require Decidable Setoid.
Generalizable All Variables.

Overview

This functor defines the tactic fsetdec, which will solve any valid goal of the form
    forall s1 ... sn,
    forall x1 ... xm,
    P1 -> ... -> Pk -> P
where P's are defined by the grammar:

P ::=
| Q
| Empty F
| Subset F F'
| Equal F F'

Q ::=
| E.eq X X'
| In X F
| Q /\ Q'
| Q \/ Q'
| Q -> Q'
| Q <-> Q'
| ~ Q
| True
| False

F ::=
| S
| empty
| singleton X
| add X F
| remove X F
| union F F'
| inter F F'
| diff F F'

X ::= x1 | ... | xm
S ::= s1 | ... | sn

The tactic will also work on some goals that vary slightly from the above form:
  • The variables and hypotheses may be mixed in any order and may have already been introduced into the context. Moreover, there may be additional, unrelated hypotheses mixed in (these will be ignored).
  • A conjunction of hypotheses will be handled as easily as separate hypotheses, i.e., P1 P2 P can be solved iff P1 P2 P can be solved.
  • fsetdec should solve any goal if the FSet-related hypotheses are contradictory.
  • fsetdec will first perform any necessary zeta and beta reductions and will invoke subst to eliminate any Coq equalities between finite sets or their elements.
  • If E.eq is convertible with Coq's equality, it will not matter which one is used in the hypotheses or conclusion.
  • The tactic can solve goals where the finite sets or set elements are expressed by Coq terms that are more complicated than variables. However, non-local definitions are not expanded, and Coq equalities between non-variable terms are not used. For example, this goal will be solved:
        forall (f : t -> t),
        forall (g : elt -> elt),
        forall (s1 s2 : t),
        forall (x1 x2 : elt),
        Equal s1 (f s2) ->
        E.eq x1 (g (g x2)) ->
        In x1 s1 ->
        In (g (g x2)) (f s2)
    
    This one will not be solved:
        forall (f : t -> t),
        forall (g : elt -> elt),
        forall (s1 s2 : t),
        forall (x1 x2 : elt),
        Equal s1 (f s2) ->
        E.eq x1 (g x2) ->
        In x1 s1 ->
        g x2 = g (g x2) ->
        In (g (g x2)) (f s2)
    

Facts and Tactics for Propositional Logic

These lemmas and tactics are in a module so that they do not affect the namespace if you import the enclosing module Decide.
Module FSetLogicalFacts.
  Export Decidable.
  Export Setoid.

Lemmas and Tactics About Decidable Propositions

Propositional Equivalences Involving Negation

These are all written with the unfolded form of negation, since I am not sure if setoid rewriting will always perform conversion.

Tactics for Negations


  Tactic Notation "fold" "any" "not" :=
    repeat (
      match goal with
        | H: context [?P False] |- _
          fold (¬ P) in H
        | |- context [?P False] ⇒
          fold (¬ P)
      end).

push not using db will pushes all negations to the leaves of propositions in the goal, using the lemmas in db to assist in checking the decidability of the propositions involved. If using db is omitted, then core will be used. Additional versions are provided to manipulate the hypotheses or the hypotheses and goal together.
XXX: This tactic and the similar subsequent ones should have been defined using autorewrite. However, dealing with multiples rewrite sites and side-conditions is done more cleverly with the following explicit analysis of goals.

  Ltac or_not_l_iff P Q tac :=
    (rewrite (or_not_l_iff_1 P Q) by tac) ||
      (rewrite (or_not_l_iff_2 P Q) by tac).

  Ltac or_not_r_iff P Q tac :=
    (rewrite (or_not_r_iff_1 P Q) by tac) ||
      (rewrite (or_not_r_iff_2 P Q) by tac).

  Ltac or_not_l_iff_in P Q H tac :=
    (rewrite (or_not_l_iff_1 P Q) in H by tac) ||
      (rewrite (or_not_l_iff_2 P Q) in H by tac).

  Ltac or_not_r_iff_in P Q H tac :=
    (rewrite (or_not_r_iff_1 P Q) in H by tac) ||
      (rewrite (or_not_r_iff_2 P Q) in H by tac).

  Tactic Notation "push" "not" "using" ident(db) :=
    let dec := solve_decidable using db in
      unfold not, iff;
        repeat (
          match goal with
            | |- context [True False] ⇒ rewrite not_true_iff
            | |- context [False False] ⇒ rewrite not_false_iff
            | |- context [(?P False) False] ⇒ rewrite (not_not_iff P) by dec
            | |- context [(?P False) (?Q False)] ⇒
              rewrite (contrapositive P Q) by dec
            | |- context [(?P False) ?Q] ⇒ or_not_l_iff P Q dec
            | |- context [?P (?Q False)] ⇒ or_not_r_iff P Q dec
            | |- context [(?P False) ?Q] ⇒ rewrite (imp_not_l P Q) by dec
            | |- context [?P ?Q False] ⇒ rewrite (not_or_iff P Q)
            | |- context [?P ?Q False] ⇒ rewrite (not_and_iff P Q)
            | |- context [(?P ?Q) False] ⇒ rewrite (not_imp_iff P Q) by dec
          end);
        fold any not.

  Tactic Notation "push" "not" :=
    push not using core.

  Tactic Notation
  "push" "not" "in" "*" "|-" "using" ident(db) :=
    let dec := solve_decidable using db in
      unfold not, iff in × |-;
        repeat (
          match goal with
            | H: context [True False] |- _rewrite not_true_iff in H
            | H: context [False False] |- _rewrite not_false_iff in H
            | H: context [(?P False) False] |- _
              rewrite (not_not_iff P) in H by dec
            | H: context [(?P False) (?Q False)] |- _
              rewrite (contrapositive P Q) in H by dec
            | H: context [(?P False) ?Q] |- _or_not_l_iff_in P Q H dec
            | H: context [?P (?Q False)] |- _or_not_r_iff_in P Q H dec
            | H: context [(?P False) ?Q] |- _
              rewrite (imp_not_l P Q) in H by dec
            | H: context [?P ?Q False] |- _rewrite (not_or_iff P Q) in H
            | H: context [?P ?Q False] |- _rewrite (not_and_iff P Q) in H
            | H: context [(?P ?Q) False] |- _
              rewrite (not_imp_iff P Q) in H by dec
          end);
        fold any not.

  Tactic Notation "push" "not" "in" "*" "|-" :=
    push not in × |- using core.

  Tactic Notation "push" "not" "in" "*" "using" ident(db) :=
    push not using db; push not in × |- using db.
  Tactic Notation "push" "not" "in" "*" :=
    push not in × using core.

A simple test case to see how this works.
  Lemma test_push : P Q R : Prop,
    decidable P
    decidable Q
    (¬ True)
    (¬ False)
    (¬ ¬ P)
    (¬ (P Q) ¬ R)
    ((P Q) ¬ R)
    (¬ (P Q) R)
    (R ¬ (P Q))
    (¬ R (P Q))
    (¬ P R)
    (¬ ((R P) (Q R)))
    (¬ (P R))
    (¬ (P R))
    True.
  Proof.
    intros. push not in ×.
    tauto.
  Qed.

pull not using db will pull as many negations as possible toward the top of the propositions in the goal, using the lemmas in db to assist in checking the decidability of the propositions involved. If using db is omitted, then core will be used. Additional versions are provided to manipulate the hypotheses or the hypotheses and goal together.

  Tactic Notation "pull" "not" "using" ident(db) :=
    let dec := solve_decidable using db in
      unfold not, iff;
      repeat (
        match goal with
        | |- context [True False] ⇒ rewrite not_true_iff
        | |- context [False False] ⇒ rewrite not_false_iff
        | |- context [(?P False) False] ⇒ rewrite (not_not_iff P) by dec
        | |- context [(?P False) (?Q False)] ⇒
          rewrite (contrapositive P Q) by dec
        | |- context [(?P False) ?Q] ⇒ or_not_l_iff P Q dec
        | |- context [?P (?Q False)] ⇒ or_not_r_iff P Q dec
        | |- context [(?P False) ?Q] ⇒ rewrite (imp_not_l P Q) by dec
        | |- context [(?P False) (?Q False)] ⇒
          rewrite <- (not_or_iff P Q)
        | |- context [?P ?Q False] ⇒ rewrite <- (not_and_iff P Q)
        | |- context [?P (?Q False)] ⇒ rewrite <- (not_imp_iff P Q) by dec
        | |- context [(?Q False) ?P] ⇒
          rewrite <- (not_imp_rev_iff P Q) by dec
        end);
      fold any not.

  Tactic Notation "pull" "not" :=
    pull not using core.

  Tactic Notation
    "pull" "not" "in" "*" "|-" "using" ident(db) :=
    let dec := solve_decidable using db in
      unfold not, iff in × |-;
        repeat (
        match goal with
        | H: context [True False] |- _rewrite not_true_iff in H
        | H: context [False False] |- _rewrite not_false_iff in H
        | H: context [(?P False) False] |- _
          rewrite (not_not_iff P) in H by dec
        | H: context [(?P False) (?Q False)] |- _
          rewrite (contrapositive P Q) in H by dec
        | H: context [(?P False) ?Q] |- _or_not_l_iff_in P Q H dec
        | H: context [?P (?Q False)] |- _or_not_r_iff_in P Q H dec
        | H: context [(?P False) ?Q] |- _
          rewrite (imp_not_l P Q) in H by dec
        | H: context [(?P False) (?Q False)] |- _
          rewrite <- (not_or_iff P Q) in H
        | H: context [?P ?Q False] |- _
          rewrite <- (not_and_iff P Q) in H
        | H: context [?P (?Q False)] |- _
          rewrite <- (not_imp_iff P Q) in H by dec
        | H: context [(?Q False) ?P] |- _
          rewrite <- (not_imp_rev_iff P Q) in H by dec
        end);
      fold any not.

  Tactic Notation "pull" "not" "in" "*" "|-" :=
    pull not in × |- using core.

  Tactic Notation "pull" "not" "in" "*" "using" ident(db) :=
    pull not using db; pull not in × |- using db.
  Tactic Notation "pull" "not" "in" "*" :=
    pull not in × using core.

A simple test case to see how this works.
  Lemma test_pull : P Q R : Prop,
    decidable P
    decidable Q
    (¬ True)
    (¬ False)
    (¬ ¬ P)
    (¬ (P Q) ¬ R)
    ((P Q) ¬ R)
    (¬ (P Q) R)
    (R ¬ (P Q))
    (¬ R (P Q))
    (¬ P R)
    (¬ (R P) ¬ (Q R))
    (¬ P ¬ R)
    (P ¬ R)
    (¬ R P)
    True.
  Proof.
    intros. pull not in ×. tauto.
  Qed.

End FSetLogicalFacts.
Import FSetLogicalFacts.

Auxiliary Tactics

Again, these lemmas and tactics are in a module so that they do not affect the namespace if you import the enclosing module Decide.

Generic Tactics

We begin by defining a few generic, useful tactics.
if t then t1 else t2 executes t and, if it does not fail, then t1 will be applied to all subgoals produced. If t fails, then t2 is executed.
  Tactic Notation
    "if" tactic(t)
    "then" tactic(t1)
    "else" tactic(t2) :=
    first [ t; first [ t1 | fail 2 ] | t2 ].

prop P holds by t succeeds (but does not modify the goal or context) if the proposition P can be proved by t in the current context. Otherwise, the tactic fails.
  Tactic Notation "prop" constr(P) "holds" "by" tactic(t) :=
    let H := fresh in
      assert P as H by t;
        clear H.

This tactic acts just like assert ... by ... but will fail if the context already contains the proposition.
  Tactic Notation "assert" "new" constr(e) "by" tactic(t) :=
    match goal with
      | H: e |- _fail 1
      | _assert e by t
    end.

subst++ is similar to subst except that
  • it never fails (as subst does on recursive
equations),
  • it substitutes locally defined variable for their
definitions,
  • it performs beta reductions everywhere, which may
arise after substituting a locally defined function for its definition.
  Tactic Notation "subst" "++" :=
    repeat (
      match goal with
        | x : _ |- _subst x
      end);
    cbv zeta beta in ×.

decompose records calls decompose record H on every relevant hypothesis H.
  Tactic Notation "decompose" "records" :=
    repeat (
      match goal with
        | H: FSetSpecs ?F |- _revert H
        | H: _ |- _progress (decompose record H); clear H
      end); intros.

Discarding Irrelevant Hypotheses

We will want to clear the context of any non-FSet-related hypotheses in order to increase the speed of the tactic. To do this, we will need to be able to decide which are relevant. We do this by making a simple inductive definition classifying the propositions of interest.
  Inductive FSet_elt_Prop : Prop Prop :=
  | eq_Prop : (S : Set) (x y : S),
    FSet_elt_Prop (x = y)
  | eq_elt_prop : `{OrderedType A} (x y : A),
    FSet_elt_Prop (x === y)
  | neq_elt_prop : `{OrderedType A} (x y : A),
    FSet_elt_Prop (x =/= y)
  | In_elt_prop : `{F : @FSet A HA} (x : A) (s : set A),
    FSet_elt_Prop (In x s)
  | True_elt_prop :
    FSet_elt_Prop True
  | False_elt_prop :
    FSet_elt_Prop False
  | conj_elt_prop : P Q,
    FSet_elt_Prop P
    FSet_elt_Prop Q
    FSet_elt_Prop (P Q)
  | disj_elt_prop : P Q,
    FSet_elt_Prop P
    FSet_elt_Prop Q
    FSet_elt_Prop (P Q)
  | impl_elt_prop : P Q,
    FSet_elt_Prop P
    FSet_elt_Prop Q
    FSet_elt_Prop (P Q)
  | not_elt_prop : P,
    FSet_elt_Prop P
    FSet_elt_Prop (¬ P).

  Inductive FSet_Prop : Prop Prop :=
  | elt_FSet_Prop : P,
    FSet_elt_Prop P
    FSet_Prop P
  | Empty_FSet_Prop : `{F : @FSet A HA} (s : set A),
    FSet_Prop (Empty s)
  | Subset_FSet_Prop : `{F : @FSet A HA} (s1 s2 : set A),
    FSet_Prop (Subset s1 s2)
  | Equal_FSet_Prop : `{F : @FSet A HA} (s1 s2 : set A),
    FSet_Prop (Equal s1 s2).

Here is the tactic that will throw away hypotheses that are not useful (for the intended scope of the fsetdec tactic).
  Hint Constructors FSet_elt_Prop FSet_Prop : FSet_Prop.
  Ltac discard_nonFSet :=
    repeat (
      match goal with
        | H : FSetSpecs ?F |- _revert H
        | H : ?P |- _
          if prop (FSet_Prop P) holds by
            (auto 100 with FSet_Prop)
            then fail
            else clear H
      end); intros.

Turning Set Operators into Propositional Connectives

The lemmas from FSetFacts will be used to break down set operations into propositional formulas built over the predicates In and E.eq applied only to variables. We are going to use them with autorewrite.
  Hint Rewrite
    @empty_iff @singleton_iff @add_iff @remove_iff
    @union_iff @inter_iff @diff_iff
    : set_simpl.

Decidability of FSet Propositions

In is decidable.
  Lemma dec_In :
     `{HF : @FSetSpecs A HA F} x s, decidable (In x s).
  Proof.
    red; intros; generalize (mem_iff s x); case (mem x s); intuition.
  Qed.

E.eq is decidable.
  Lemma dec_eq : `{HA : OrderedType A} (x y : A), decidable (x === y).
  Proof.
    red; intros; destruct (eq_dec x y); auto.
  Qed.

The hint database FSet_decidability will be given to the push_neg tactic from the module Negation.
  Hint Resolve @dec_In @dec_eq : FSet_decidability.

Normalizing Propositions About Equality

We have to deal with the fact that E.eq may be convertible with Coq's equality. Thus, we will find the following tactics useful to replace one form with the other everywhere.

  Ltac E_neq_to_not_E_eq :=
    repeat (
      match goal with
        | H: context [?x =/= ?y] |- _
          progress (change (x =/= y) with (¬ x === y) in H)
        | |- context [?x = ?y] ⇒
          progress (change (x =/= y) with (¬ x === y))
      end).

  Ltac Logic_eq_to_E_eq :=
    repeat (
      match goal with
        | H: context [?x = ?y] |- _
          progress (change (x = y) with (x === y) in H)
        | |- context [?x = ?y] ⇒
          progress (change (x = y) with (x === y))
      end).

  Ltac E_eq_to_Logic_eq :=
    repeat (
      match goal with
        | H: context [?x === ?y] |- _
          progress (change (x === y) with (@Logic.eq _ x y) in H)
        | |- context [?x === ?y] ⇒
          progress (change (x === y) with (@Logic.eq _ x y))
      end).

This tactic works like the built-in tactic subst, but at the level of set element equality (which may not be the convertible with Coq's equality).
  Ltac substFSet :=
    repeat (
      match goal with
        | H: ?x === ?y |- _rewrite H in *; clear H
      end).

Considering Decidability of Base Propositions

This tactic adds assertions about the decidability of E.eq and In to the context. This is necessary for the completeness of the fsetdec tactic. However, in order to minimize the cost of proof search, we should be careful to not add more than we need. Once negations have been pushed to the leaves of the propositions, we only need to worry about decidability for those base propositions that appear in a negated form.
  Ltac assert_decidability :=
  
We actually don't want these rules to fire if the syntactic context in the patterns below is trivially empty, but we'll just do some clean-up at the afterward.
    repeat (
      match goal with
        | H: context [?x =/= ?y] |- _
          assert new (x === y x =/= y) by (apply dec_eq)
        | H: context [¬ In ?x ?s] |- _
          assert new (In x s ¬ In x s) by (apply dec_In)
        | |- context [?x =/= ?y] ⇒
          assert new (x === y x =/= y) by (apply dec_eq)
        | |- context [¬ In ?x ?s] ⇒
          assert new (In x s ¬ In x s) by (apply dec_In)
      end);
  
Now we eliminate the useless facts we added (because they would likely be very harmful to performance).
    repeat (
      match goal with
        | _: ¬ ?P, H : ?P ¬ ?P |- _clear H
      end).

Handling Empty, Subset, and Equal

This tactic instantiates universally quantified hypotheses (which arise from the unfolding of Empty, Subset, and Equal) for each of the set element expressions that is involved in some membership or equality fact. Then it throws away those hypotheses, which should no longer be needed.
  Notation Eq := @Equivalence.equiv.
  Ltac inst_FSet_hypotheses :=
    repeat (
      match goal with
        | H : a : ?A, _,
          _ : context [ @In ?A _ _ ?x _ ] |- _
          let P := type of (H x) in
          assert new P by (exact (H x))
        | H : a : ?A, _
          |- context [ @In ?A _ _ ?x _ ] ⇒
          let P := type of (H x) in
          assert new P by (exact (H x))
        | H : a : ?A, _,
          _ : context [@Equivalence.equiv ?A _ _ ?x ?y ] |- _
          let P := type of (H x) in
          let P' := type of (H y) in
          assert new P by (exact (H x));
          assert new P' by (exact (H y))
        | H : a : ?A, _
          |- context [@Equivalence.equiv ?A _ _ ?x ?y ] ⇒
          let P := type of (H x) in
          let P' := type of (H y) in
          assert new P by (exact (H x));
          assert new P' by (exact (H y))
      end);
    repeat (
      match goal with
        | H : a : ?A, _ |- _
          match type of A with
            | Proprevert H
            | _clear H
          end
      end); intros.

The Core fsetdec Auxiliary Tactics

Here is the crux of the proof search. Recursion through intuition! (This will terminate if I correctly understand the behavior of intuition.)
  Ltac fsetdec_rec :=
    try (match goal with
           | H: ?x === ?x False |- _destruct H
         end);
    (reflexivity ||
      contradiction ||
        (progress substFSet; intuition fsetdec_rec)).

If we add unfold Empty, Subset, Equal in *; intros; to the beginning of this tactic, it will satisfy the same specification as the fsetdec tactic; however, it will be much slower than necessary without the pre-processing done by the wrapper tactic fsetdec.
  Ltac fsetdec_body :=
    inst_FSet_hypotheses;
    autorewrite with set_simpl in *;
    push not in × using FSet_decidability;
    substFSet;
    assert_decidability;
    auto using reflexivity;
    (intuition fsetdec_rec) ||
      fail 1 "because the goal is beyond the scope of this tactic".

End FSetDecideAuxiliary.
Import FSetDecideAuxiliary.

The fsetdec Tactic

Here is the top-level tactic (the only one intended for clients of this library). It's specification is given at the top of the file.
Ltac fsetdec :=
We first unfold any occurrences of iff.
  unfold iff in *;
We fold occurrences of not because it is better for intros to leave us with a goal of ¬ P than a goal of False.
  fold any not; intros;
Now we decompose conjunctions, which will allow the discard_nonFSet and assert_decidability tactics to do a much better job.
  decompose records;
  discard_nonFSet;
We unfold these defined propositions on finite sets. If our goal was one of them, then have one more item to introduce now.
  unfold Empty, Subset, Equal in *; intros;
We now want to get rid of all uses of = in favor of E.eq. However, the best way to eliminate a = is in the context is with subst, so we will try that first. In fact, we may as well convert uses of E.eq into = when possible before we do subst so that we can even more mileage out of it. Then we will convert all remaining uses of = back to E.eq when possible. We use change_to_E_t to ensure that we have a canonical name for set elements, so that Logic_eq_to_E_eq will work properly.
  E_eq_to_Logic_eq; subst++;
    E_neq_to_not_E_eq; Logic_eq_to_E_eq;
The next optimization is to swap a negated goal with a negated hypothesis when possible. Any swap will improve performance by eliminating the total number of negations, but we will get the maximum benefit if we swap the goal with a hypotheses mentioning the same set element, so we try that first. If we reach the fourth branch below, we attempt any swap. However, to maintain completeness of this tactic, we can only perform such a swap with a decidable proposition; hence, we first test whether the hypothesis is an FSet_elt_Prop, noting that any FSet_elt_Prop is decidable.
  pull not using FSet_decidability;
  unfold not in *;
  match goal with
    | H: (In ?x ?r) False |- (In ?x ?s) False
      contradict H; fsetdec_body
    | H: (In ?x ?r) False |- (?x === ?y) False
      contradict H; fsetdec_body
    | H: (In ?x ?r) False |- (?y === ?x) False
      contradict H; fsetdec_body
    | H: ?P False |- ?Q False
      if prop (FSet_elt_Prop P) holds by
        (auto 100 with FSet_Prop)
      then (contradict H; fsetdec_body)
      else fsetdec_body
    | |- _
      fsetdec_body
  end.