Lvc.Infra.Relations

Require Import Arith Coq.Lists.List Setoid Coq.Lists.SetoidList Util.
Require Export Infra.Option EqDec AutoIndTac Tactics.

Set Implicit Arguments.

Relations

The development in this file are based on the material of the Semantics lecture 2011 at Saarland University.

Section Relations.
  Variable X:Type.

  Definition rel : Type := XXProp.

Unary functions and predicates on relations

  Section Unary.
    Variable R : rel.

    Definition reflexive :=
       x, R x x.

    Definition symmetric :=
       x y, R x yR y x.

    Definition transitive :=
       x y z, R x yR y zR x z.

    Definition functional :=
       x y z, R x yR x zy=z.

    Definition reducible (x : X) :=
       y, R x y.

    Definition normal (x : X) :=
      ¬ reducible x.

    Definition total :=
       x, reducible x.

    Hint Unfold symmetric transitive functional reducible normal total.

The transitive reflexive closure of a relation


    Inductive star : rel :=
    | star_refl : x, star x x
    | S_star : y x z, R x ystar y zstar x z.

    CoInductive diverges : XProp :=
    | DivergesI x y : R x ydiverges ydiverges x.

    Lemma star_trans (x y z:X)
      : star x ystar y zstar x z.
      intros A. revert z. induction A; eauto using star.
    Qed.

    Lemma star_right (x y z:X)
      : star x yR y zstar x z.
    Proof.
      intros A B. induction A ; eauto using star.
    Qed.

The transitive reflexive closure of a relation with size index


    Inductive starn : natXXProp :=
    | starn_refl : a, starn 0 a a
    | S_starn : x y z n, R x ystarn n y zstarn (S n) x z.

    Lemma starn_one
      : x y, R x ystarn (S 0) x y.
    Proof.
      intros. eauto using starn.
    Qed.

    Lemma starn_trans n m :
       x y, starn n x y z, starn m y zstarn (n+m) x z.
    Proof.
      intros. induction H. simpl. assumption.
      econstructor; eauto.
    Qed.

    Lemma star0_is_eq a b
      : starn 0 a ba = b.
      intros. inversion H. reflexivity.
    Qed.

    Lemma starn_iff_star a b
      : star a b n, starn n a b.
      split. induction 1. 0; constructor.
      destruct IHstar as [n B]. (S n). apply (S_starn H B).
      intros [n B]. induction B. constructor. apply (S_star H IHB).
    Qed.

The transitive closure of a relation.

    Inductive plus : rel :=
    | plusO x y : R x yplus x y
    | plusS x y z : R x yplus y zplus x z.

    Lemma plus_trans x y z :
      plus x yplus y zplus x z.
    Proof.
      intros A B. induction A; eauto using plus.
    Qed.

    Lemma star_plus x y z:
      R x ystar y zplus x z.
    Proof.
      intros. general induction H0.
      constructor. assumption.
      econstructor 2; eauto.
    Qed.

    Lemma plus_right x y z :
      plus x yR y zplus x z.
    Proof.
      intros A B. apply plus_trans with (y:=y); eauto using plus.
    Qed.

    Lemma plus_star x y :
      plus x ystar x y.
    Proof.
      induction 1; econstructor; try eassumption. constructor.
    Qed.

    Lemma star_plus_plus : x y z, star x yplus y zplus x z.
    Proof.
      intros. induction H; eauto using star, plus.
    Qed.

    Lemma plus_destr x z
    : plus x z y, R x y star y z.
    Proof.
      intros. destruct H; y; eauto using star, plus, plus_star.
    Qed.

    Lemma star_destr : x z, star x z(x = z) plus x z.
    Proof.
      intros. destruct H. left; eauto. right; eauto using star_plus.
    Qed.

Termination and Normalization

Also know as must-termination and may-termination

    Inductive terminates : XProp :=
    | terminatesI x : ( y, R x yterminates y) → terminates x.

    Definition terminating : Prop :=
       x, terminates x.

    Definition normalizes´ (x:X) : Prop :=
       y, inhabited (star x y) normal y.

    Inductive normalizes : XProp :=
    | normalizesI1 x : normal xnormalizes x
    | normalizesI2 x y : R x ynormalizes ynormalizes x.

    Lemma normalizes_normalizes´_agree (x:X) :
      normalizes´ x normalizes x.
      split; intros.
      destruct H as [y [A B]] . destruct A.
      general induction H; eauto 20 using inhabited, normalizes.
      induction H.
       x. eauto using star.
      firstorder. x0. firstorder using star.
    Qed.

    Definition normalizing : Prop :=
       x, normalizes x.

    Lemma functional_normalizes_terminates x
      : functionalnormalizes xterminates x.
    Proof.
      intros F N. induction N as [x A|x y A B]; constructor.
      intros y B. exfalso. apply A. now y; eauto using inhabited.
      intros C. assert (y=) by (eapply F; eauto). subst. trivial.
    Qed.

  End Unary.

  Lemma star_starplus (R : rel) (x y : X)
    : star R x ystar (plus R) x y.
  Proof.
    intros. induction H; eauto using star, plus.
  Qed.

  Lemma star_idem_1 R x y
    : star (star R) x ystar R x y.
  Proof.
    intros. remember (star R). induction H; subst; eauto using star, star_trans.
  Qed.

  Lemma star_idem_2 R x y
    : star R x ystar (star R) x y.
  Proof.
    induction 1; eauto using star.
  Qed.

  Lemma div_plus´ : (R : rel) (x y : X), star R x ydiverges (plus R) ydiverges R x.
  Proof.
    intro R. cofix. intros. destruct H0.
    apply plus_destr in H0. destruct H0 as [x0´ [Step SStep]].
    destruct H; eapply DivergesI; eauto using star_trans, star_right.
  Qed.

  Lemma div_ext_star : (R : rel) (x y : X), star R x ydiverges (plus R) ydiverges (plus R) x.
  Proof.
    intros. induction H; eauto using diverges, star_plus.
  Qed.

  Lemma div_plus : (R : rel) (x : X), diverges (plus R) xdiverges R x.
  Proof.
    intros. eauto using (div_plus´ (star_refl R x)).
  Qed.

  Lemma div_ext_star_2 (R: rel) x y
  : functional Rdiverges R xstar R x ydiverges R y.
  Proof.
    intros. general induction H1; eauto.
    inv H2. eapply IHstar; eauto. erewrite H0; eauto.
  Qed.

  Lemma div_ext_plus (R: rel) x y
  : functional Rdiverges R xplus R x ydiverges R y.
  Proof.
    intros. eapply div_ext_star_2; eauto. eapply plus_star; eauto.
  Qed.

  Lemma normal_terminates (R: rel) s
  : normal R sterminates R s.
  Proof.
    intros. econstructor; intros. exfalso. firstorder.
  Qed.

Relational approximation

  Definition rle (R : rel) :=
     x y, R x y x y.

  Definition req (R : rel) :=
    (rle R × rle R)%type.

Reduction decidability

  Definition reddec R :=
     x, reducible R x normal R x.

End Relations.

Global Hint Unfold symmetric transitive functional reducible normal total.

complete induction and size induction


Require Import Omega.

Lemma complete_induction (p : natProp) (x : nat)
  : ( x, ( y, y<xp y) → p x) → p x.
Proof.
  intros A. apply A. induction x; intros y B.
  exfalso ; omega.
  apply A. intros z C. apply IHx. omega.
Qed.

Lemma gt_terminates
  : terminating gt.
Proof.
  intros x. apply complete_induction. clear x.
  intros x A. constructor. exact A.
Qed.

Lemma size_induction (X : Type) (f : Xnat) (p: XProp) (x : X)
  : ( x, ( y, f y < f xp y) → p x) → p x.
Proof.
  intros A. apply A.
  induction (f x); intros y B.
  exfalso; omega.
  apply A. intros z C. apply IHn. omega.
Qed.

Definition size_recursion (X : Type) (f : Xnat) (p: XType) (x : X)
  : ( x, ( y, f y < f xp y) → p x) → p x.
Proof.
  intros A. apply A.
  induction (f x); intros y B.
  exfalso; destruct (f y); inv B.
  apply A. intros z C. apply IHn. cbv in B,C. cbv.
  inv B. assumption. eapply le_S in C. eapply le_trans; eauto.
Defined.

Inductive fstNoneOrR {X Y:Type} (R:XYProp)
  : option Xoption YProp :=
| fstNone (x:option Y) : fstNoneOrR R None x
| bothR (x:X) (y:Y) : R x yfstNoneOrR R (Some x) (Some y)
.

Instance fstNoneOrR_Reflexive {A : Type} {R : relation A} {Rrefl: Reflexive R}
: Reflexive (fstNoneOrR R).
Proof.
  hnf; intros. destruct x; econstructor; eauto.
Qed.