coqproofdeterministicformal-verificationpartial-ordering

How to prove decidability of a relation swaping its parameters?


I have a situation where I defined an inductive datatype t and a partial order le over it (c.f. le_refl, le_trans, and le_antisym). The order has this particularity in the le_C case, that the order of the arguments are swapped in the inductive hypothesis.

Because of that, I did not succeed to prove that this ordering relation is deterministic (c.f. le_dec). The problematic subgoal is the following.

1 subgoal
t1 : t
IHt1 : forall t2 : t, {le t1 t2} + {~ le t1 t2}
t2 : t
______________________________________(1/1)
{le (C t1) (C t2)} + {~ le (C t1) (C t2)}

The induction hypothesis refers to le t1 t2 while I need le t2 t1.

When I think about that, it make sense, this binary function is neither primitively recursive on the first nor on the second parameter, but rather on the pair of both argument. I have the impression that I should somehow do an induction on both arguments at the same time, but fail to see how to do that.

I did manage to define a boolean function leb and to use it to prove le_dec, but I am wondering, from a learn point of view, how to do the proof directly with induction.

Questions

  1. How can I directly prove le_dec based on the definition of le (i.e. without first defining an equivalent boolean function)?

Minimal executable example

Main definitions

Inductive t : Set :=
  | A : t
  | B : t -> t
  | C : t -> t
  .

Inductive le : t -> t -> Prop :=
  | le_A :
      le A A

  | le_B : forall x y,
      le x y -> le (B x) (B y)

  | le_C : forall x y,
      le y x -> le (C x) (C y)

  | le_trans : forall t1 t2 t3,
      le t1 t2 -> le t2 t3 -> le t1 t3
  .

Helper lemmas

Require Import Coq.Program.Equality.

Lemma le_canonical_form_A_left (t1 : t) :
  le A t1 -> t1 = A.
Proof.
  intros LE. dependent induction LE; auto.
Qed.

Lemma le_canonical_form_B_left (t1 t2 : t) :
  le (B t1) t2 -> exists t3, t2 = B t3.
Proof.
  intros LE. dependent induction LE.
  - eauto.
  - destruct IHLE1 with t1 as [t4 ?]; clear IHLE1; trivial; subst.
    destruct IHLE2 with t4 as [t4' ?]; clear IHLE2; trivial; subst. eauto.
Qed.

Lemma le_canonical_form_C_left (t1 t2 : t) :
  le (C t1) t2 -> exists t3, t2 = C t3.
Proof.
  intros LE. dependent induction LE.
  - eauto.
  - destruct IHLE1 with t1 as [t4 ?]; clear IHLE1; trivial; subst.
    destruct IHLE2 with t4 as [t4' ?]; clear IHLE2; trivial; subst. eauto.
Qed.

Lemma le_inversion_B (t1 t2 : t) :
  le (B t1) (B t2) -> le t1 t2.
Proof.
  intros LE.
  dependent induction LE.
  - assumption.
  - apply le_canonical_form_B_left in LE1 as [t3 ?]; subst. eauto using le_trans.
Qed.

Lemma le_inversion_C (t1 t2 : t) :
  le (C t1) (C t2) -> le t2 t1.
Proof.
  intros LE.
  dependent induction LE.
  - assumption.
  - apply le_canonical_form_C_left in LE1 as [t3 ?]; subst. eauto using le_trans.
Qed.

Lemma le_inversion (t1 t2 : t) :
  le t1 t2 ->
  t1 = A /\ t2 = A \/
  (exists t1' t2', t1 = B t1' /\ t2 = B t2') \/
  (exists t1' t2', t1 = C t1' /\ t2 = C t2').
Proof.
  intros LE.
  destruct t1.
  - apply le_canonical_form_A_left in LE; subst. auto.
  - apply le_canonical_form_B_left in LE as [? ?]; subst. eauto 6.
  - apply le_canonical_form_C_left in LE as [? ?]; subst. eauto 6.
Qed.

Proofs of partial order

Lemma le_refl (x : t) :
  le x x.
Proof.
  induction x; eauto using le.
Qed.    

Lemma le_antisym (t1 t2 : t) :
  le t1 t2 -> le t2 t1 -> t1 = t2.
Proof.
  induction 1; intros LE.
  - auto.
  - apply le_inversion_B in LE. f_equal; auto.
  - apply le_inversion_C in LE. f_equal; auto using eq_sym.
  - rewrite IHle1; eauto using le_trans.
Qed.

Equivalent boolean function

Fixpoint height (x : t) : nat :=
  match x with
  | A => 1
  | B x' => 1 + height x'
  | C x' => 1 + height x'
  end.

Definition height_pair (p : t * t) : nat :=
  let (t1, t2) := p in height t1 + height t2.

Require Import Recdef.
Require Import Omega.

Function leb (p : t * t) { measure height_pair p } : bool :=
  match p with
  | (A, A) => true
  | (B x', B y') => leb (x', y')
  | (C x', C y') => leb (y', x')
  | _ => false
  end.
  - intros. subst. simpl. omega.
  - intros. subst. simpl. omega.
Defined.

Ltac inv H := inversion H; clear H; subst.

Lemma le_to_leb (t1 t2 : t) :
  le t1 t2 -> leb (t1, t2) = true.
Proof.
  remember (t1, t2) as p eqn:Heqn.
  revert Heqn.
  revert t1 t2.
  functional induction (leb p); intros t1 t2 Heqn LE; inv Heqn.
  - trivial.
  - apply IHb with x' y'; trivial.
    now apply le_inversion_B in LE.
  - apply IHb with y' x'; trivial.
    now apply le_inversion_C in LE.
  - exfalso. apply le_inversion in LE.
    intuition; subst.
    + easy.
    + destruct H0.
      destruct H.
      now (intuition; subst).
    + destruct H0.
      destruct H.
      now (intuition; subst).
Qed.

Lemma leb_to_le (t1 t2 : t) :
  leb (t1, t2) = true -> le t1 t2.
Proof.
  remember (t1, t2) as p eqn:Heqn.
  revert Heqn.
  revert t1 t2.
  functional induction (leb p); intros t1 t2 Heqn LEB; inv Heqn.
  - eauto using le.
  - eauto using le.
  - eauto using le.
  - discriminate LEB.
Qed.

Corollary le_iff_leb (t1 t2 : t) :
  le t1 t2 <-> leb (t1, t2) = true.
Proof.
  split.
  - apply le_to_leb.
  - apply leb_to_le.
Qed.

What I actually want to prove

Lemma le_dec (t1 t2 : t) :
  { le t1 t2 } + { ~le t1 t2 }.
Proof.
  revert t2.
  induction t1; intros t2.
  - destruct t2.
    + eauto using le.
    + right. intro contra. dependent induction contra.
      apply le_canonical_form_A_left in contra1; subst. eauto.
    + right. intro contra. dependent induction contra.
      apply le_canonical_form_A_left in contra1; subst. eauto.
  - destruct t2.
    + right. intro contra. clear IHt1. dependent induction contra.
      apply le_canonical_form_B_left in contra1 as [? ?]; subst. eauto.
    + destruct IHt1 with t2.
      * eauto using le.
      * right. intro contra. apply le_inversion_B in contra. contradiction.
    + right; intro contra. clear IHt1. dependent induction contra.
      apply le_canonical_form_B_left in contra1 as [? ?]; subst. eauto.
  - destruct t2.
    + right. intro contra. clear IHt1. dependent induction contra.
      apply le_canonical_form_C_left in contra1 as [? ?]; subst. eauto.
    + right. intro contra. clear IHt1. dependent induction contra.
      apply le_canonical_form_C_left in contra1 as [? ?]; subst. eauto.
    + destruct IHt1 with t2.
      * admit. (* Wrong assumption *)
      * admit. (* Wrong assumption *)
Restart.
  destruct (leb (t1, t2)) eqn:Heqn.
  - apply leb_to_le in Heqn. auto.
  - right. intro contra. apply le_to_leb in contra.
    rewrite Heqn in contra. discriminate.
Qed.

Solution based on Arthur's answer

Ltac destruct_exs_conjs :=
  repeat match goal with
  | H : exists _, _ |- _ => destruct H
  | H : _ /\ _ |- _ => destruct H
  end; subst.

Lemma le_dec_aux (t1 t2 : t) (n : nat) :
  height t1 + height t2 <= n ->
  {le t1 t2} + {~le t1 t2}.
Proof.
  revert t1 t2.
  induction n as [| n IH]; intros t1 t2 H.
  - destruct t1; simpl in H; omega.
  - destruct t1, t2.
    + eauto using le.
    + clear. right. intro contra. dependent induction contra.
      apply le_canonical_form_A_left in contra1; subst. eauto.
    + clear. right. intro contra. dependent induction contra.
      apply le_canonical_form_A_left in contra1; subst. eauto.
    + clear. right. intro contra. dependent induction contra.
      apply le_canonical_form_B_left in contra1; destruct_exs_conjs. eauto.
    + simpl in H.
      destruct (IH t1 t2); try omega.
      * eauto using le.
      * right. intro contra. apply le_inversion_B in contra. contradiction.
    + clear. right. intro contra. dependent induction contra.
      apply le_canonical_form_B_left in contra1; destruct_exs_conjs. eauto.
    + clear. right. intro contra. dependent induction contra.
      apply le_canonical_form_C_left in contra1; destruct_exs_conjs. eauto.
    + clear. right. intro contra. dependent induction contra.
      apply le_canonical_form_C_left in contra1; destruct_exs_conjs. eauto.
    + simpl in H.
      destruct (IH t2 t1); try omega.
      * eauto using le.
      * right. intro contra. apply le_inversion_C in contra. contradiction.
Qed.

Lemma le_dec' (t1 t2 : t) :
  { le t1 t2 } + { ~le t1 t2 }.
Proof.
  destruct (le_dec_aux t1 t2 (height t1 + height t2)); auto.
Qed.

Solution

  • Similarly to what you used for defining the leb function, you need to prove le_dec by induction on the height of the elements:

    Lemma le_dec_aux t1 t2 n : height t1 + height t2 <= n -> {le t1 t2} + {~le t1 t2}.
    Proof.
    revert t1 t2.
    induction n as [|n IH].
    (* ... *)
    

    That being said, I think it is perfectly fine to prove decidability using a boolean function. The Mathematical Components library uses this pattern extensively, using a specialized reflect predicate for connecting general propositions to boolean computations, instead of the sumbool type {A} + {B}.