(* This program is free software; you can redistribute it and/or      *)
(* modify it under the terms of the GNU Lesser General Public License *)
(* as published by the Free Software Foundation; either version 2.1   *)
(* of the License, or (at your option) any later version.             *)
(*                                                                    *)
(* This program is distributed in the hope that it will be useful,    *)
(* but WITHOUT ANY WARRANTY; without even the implied warranty of     *)
(* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the      *)
(* GNU General Public License for more details.                       *)
(*                                                                    *)
(* You should have received a copy of the GNU Lesser General Public   *)
(* License along with this program; if not, write to the Free         *)
(* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA *)
(* 02110-1301 USA                                                     *)


(* Contribution to the Coq Library   V6.3 (July 1999)                    *)

(****************************************************************************)
(*                 The Calculus of Inductive Constructions                  *)
(*                                                                          *)
(*                                Projet Coq                                *)
(*                                                                          *)
(*                     INRIA                        ENS-CNRS                *)
(*              Rocquencourt                        Lyon                    *)
(*                                                                          *)
(*                                Coq V5.10                                 *)
(*                              Nov 25th 1994                               *)
(*                                                                          *)
(****************************************************************************)
(*                               Simulation.v                               *)
(*                                                                          *)
(*                                Gerard Huet                               *)
(*                                                                          *)
(* Developed in V5.8  January 1993                                          *)
(* Ported    to V5.10 January 1995                                          *)
(****************************************************************************)

(* Reduction of a term by a set of redexes *)

Require Import Arith.
Require Import Terms.
Require Import Reduction.
Require Import Redexes.
Require Import Test.
Require Import Marks.
Require Import Substitution.
Require Import Residuals.

(* Commuting mark and subst *)

Lemma mark_lift_rec :
 forall (M : lambda) (n k : nat),
 lift_rec_r (mark M) k n = mark (lift_rec M k n).
Proof.
(* Goal: forall (N M : lambda) (n : nat), @eq redexes (subst_rec_r (mark M) (mark N) n) (mark (subst_rec M N n)) *)
simple induction M; simpl in |- *; intros.
(* Goal: @eq redexes (Var (relocate n k n0)) (Var (relocate n k n0)) *)
(* Goal: @eq redexes (Fun (lift_rec_r (mark l) (S k) n)) (Fun (mark (lift_rec l (S k) n))) *)
(* Goal: @eq redexes (Ap false (lift_rec_r (mark l) k n) (lift_rec_r (mark l0) k n)) (Ap false (mark (lift_rec l k n)) (mark (lift_rec l0 k n))) *)
elim (test k n); simpl in |- *; intros; trivial.
(* Goal: @eq lambda (Abs (subst_rec (unmark r) (unmark V) (S n))) (Abs (unmark (subst_rec_r r V (S n)))) *)
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; trivial.
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; elim H0; trivial.
Qed.

Lemma mark_lift :
 forall (M : lambda) (n : nat), lift_r n (mark M) = mark (lift n M).
Proof.
(* Goal: forall (M : lambda) (n : nat), @eq redexes (lift_r n (mark M)) (mark (lift n M)) *)
unfold lift in |- *; unfold lift_r in |- *; intros; apply mark_lift_rec.
Qed.

Lemma mark_subst_rec :
 forall (N M : lambda) (n : nat),
 subst_rec_r (mark M) (mark N) n = mark (subst_rec M N n).
Proof.
(* Goal: forall (N M : lambda) (n : nat), @eq redexes (subst_rec_r (mark M) (mark N) n) (mark (subst_rec M N n)) *)
simple induction M; simpl in |- *; intros.
(* Goal: @eq lambda (insert_Ref (unmark V) n n0) (unmark (insert_Var V n n0)) *)
(* Goal: @eq lambda (Abs (subst_rec (unmark r) (unmark V) (S n))) (Abs (unmark (subst_rec_r r V (S n)))) *)
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
unfold insert_Var, insert_Ref in |- *.
(* Goal: @eq redexes match compare n0 n with | inleft (left l as s) => Var (Init.Nat.pred n) | inleft (right e as s) => lift_r n0 (mark N) | inright g => Var n end (mark match compare n0 n with | inleft (left l as s) => Ref (Init.Nat.pred n) | inleft (right e as s) => lift n0 N | inright g => Ref n end) *)
(* Goal: @eq redexes (Fun (subst_rec_r (mark l) (mark N) (S n))) (Fun (mark (subst_rec l N (S n)))) *)
(* Goal: @eq redexes (Ap false (subst_rec_r (mark l) (mark N) n) (subst_rec_r (mark l0) (mark N) n)) (Ap false (mark (subst_rec l N n)) (mark (subst_rec l0 N n))) *)
elim (compare n0 n); intro H.
(* Goal: @eq redexes (if H then Var (Init.Nat.pred n) else lift_r n0 (mark N)) (mark (if H then Ref (Init.Nat.pred n) else lift n0 N)) *)
(* Goal: @eq redexes (Var n) (mark (Ref n)) *)
(* Goal: @eq redexes (Fun (subst_rec_r (mark l) (mark N) (S n))) (Fun (mark (subst_rec l N (S n)))) *)
(* Goal: @eq redexes (Ap false (subst_rec_r (mark l) (mark N) n) (subst_rec_r (mark l0) (mark N) n)) (Ap false (mark (subst_rec l N n)) (mark (subst_rec l0 N n))) *)
elim H; intro H'.
(* Goal: @eq redexes (Var n) (mark (Ref n)) *)
(* Goal: @eq redexes (Fun (subst_rec_r (mark l) (mark N) (S n))) (Fun (mark (subst_rec l N (S n)))) *)
(* Goal: @eq redexes (Ap false (subst_rec_r (mark l) (mark N) n) (subst_rec_r (mark l0) (mark N) n)) (Ap false (mark (subst_rec l N n)) (mark (subst_rec l0 N n))) *)
simpl in |- *; trivial.
(* Goal: @eq redexes (lift_r n0 (mark N)) (mark (lift n0 N)) *)
(* Goal: @eq redexes (Var n) (mark (Ref n)) *)
(* Goal: @eq redexes (Fun (subst_rec_r (mark l) (mark N) (S n))) (Fun (mark (subst_rec l N (S n)))) *)
(* Goal: @eq redexes (Ap false (subst_rec_r (mark l) (mark N) n) (subst_rec_r (mark l0) (mark N) n)) (Ap false (mark (subst_rec l N n)) (mark (subst_rec l0 N n))) *)
rewrite (mark_lift N n0); trivial.
(* Goal: @eq redexes (Var n) (mark (Ref n)) *)
(* Goal: @eq redexes (Fun (subst_rec_r (mark l) (mark N) (S n))) (Fun (mark (subst_rec l N (S n)))) *)
(* Goal: @eq redexes (Ap false (subst_rec_r (mark l) (mark N) n) (subst_rec_r (mark l0) (mark N) n)) (Ap false (mark (subst_rec l N n)) (mark (subst_rec l0 N n))) *)
simpl in |- *; trivial.
(* Goal: @eq lambda (Abs (subst_rec (unmark r) (unmark V) (S n))) (Abs (unmark (subst_rec_r r V (S n)))) *)
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; trivial.
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; elim H0; trivial.
Qed.

Lemma mark_subst :
 forall M N : lambda, subst_r (mark M) (mark N) = mark (subst M N).
Proof.
(* Goal: forall M N : lambda, @eq redexes (subst_r (mark M) (mark N)) (mark (subst M N)) *)
unfold subst in |- *; unfold subst_r in |- *; intros; apply mark_subst_rec.
Qed.

(* residuals simulates par_red1 *)

Lemma simulation :
 forall M M' : lambda,
 par_red1 M M' -> exists V : redexes, residuals (mark M) V (mark M').
Proof.
(* Goal: forall (M M' : lambda) (_ : par_red1 M M'), @ex redexes (fun V : redexes => residuals (mark M) V (mark M')) *)
simple induction 1; simpl in |- *; intros.
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
elim H1; intros V1 P1.
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
elim H3; intros V2 P2.
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (Fun (mark M0)) (mark N)) V (mark (subst N' M'0))) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Var n) V (Var n)) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Fun (mark M0)) V (Fun (mark M'0))) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
exists (Ap true (Fun V1) V2).
(* Goal: residuals (Ap false (Fun (mark M0)) (mark N)) (Ap true (Fun V1) V2) (mark (subst N' M'0)) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Var n) V (Var n)) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Fun (mark M0)) V (Fun (mark M'0))) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
elim mark_subst; auto.
(* Goal: @ex redexes (fun V : redexes => residuals (Var n) V (Var n)) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Fun (mark M0)) V (Fun (mark M'0))) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
exists (Var n); trivial.
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
elim H1; intros V1 P1.
(* Goal: @ex redexes (fun V : redexes => residuals (Fun (mark M0)) V (Fun (mark M'0))) *)
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
exists (Fun V1); auto.
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
elim H1; intros V1 P1.
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
elim H3; intros V2 P2.
(* Goal: @ex redexes (fun V : redexes => residuals (Ap false (mark M0) (mark N)) V (Ap false (mark M'0) (mark N'))) *)
exists (Ap false V1 V2); auto.
Qed.

(* Commuting unmark and subst *)

Lemma unmark_lift_rec :
 forall (U : redexes) (n k : nat),
 lift_rec (unmark U) k n = unmark (lift_rec_r U k n).
Proof.
(* Goal: forall (V U : redexes) (n : nat), @eq lambda (subst_rec (unmark U) (unmark V) n) (unmark (subst_rec_r U V n)) *)
simple induction U; simpl in |- *; intros.
(* Goal: @eq lambda (Ref (relocate n k n0)) (Ref (relocate n k n0)) *)
(* Goal: @eq lambda (Abs (lift_rec (unmark r) (S k) n)) (Abs (unmark (lift_rec_r r (S k) n))) *)
(* Goal: @eq lambda (App (lift_rec (unmark r) k n) (lift_rec (unmark r0) k n)) (App (unmark (lift_rec_r r k n)) (unmark (lift_rec_r r0 k n))) *)
elim (test k n); trivial.
(* Goal: @eq lambda (Abs (subst_rec (unmark r) (unmark V) (S n))) (Abs (unmark (subst_rec_r r V (S n)))) *)
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; trivial.
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; elim H0; trivial.
Qed.

Lemma unmark_lift :
 forall (U : redexes) (n : nat), lift n (unmark U) = unmark (lift_r n U).
Proof.
(* Goal: forall (U : redexes) (n : nat), @eq lambda (lift n (unmark U)) (unmark (lift_r n U)) *)
unfold lift in |- *; unfold lift_r in |- *; intros; apply unmark_lift_rec.
Qed.

Lemma unmark_subst_rec :
 forall (V U : redexes) (n : nat),
 subst_rec (unmark U) (unmark V) n = unmark (subst_rec_r U V n).
Proof.
(* Goal: forall (V U : redexes) (n : nat), @eq lambda (subst_rec (unmark U) (unmark V) n) (unmark (subst_rec_r U V n)) *)
simple induction U; simpl in |- *; intros.
(* Goal: @eq lambda (insert_Ref (unmark V) n n0) (unmark (insert_Var V n n0)) *)
(* Goal: @eq lambda (Abs (subst_rec (unmark r) (unmark V) (S n))) (Abs (unmark (subst_rec_r r V (S n)))) *)
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
unfold insert_Var, insert_Ref in |- *.
(* Goal: @eq redexes (Var n) (mark (Ref n)) *)
(* Goal: @eq redexes (Fun (subst_rec_r (mark l) (mark N) (S n))) (Fun (mark (subst_rec l N (S n)))) *)
(* Goal: @eq redexes (Ap false (subst_rec_r (mark l) (mark N) n) (subst_rec_r (mark l0) (mark N) n)) (Ap false (mark (subst_rec l N n)) (mark (subst_rec l0 N n))) *)
elim (compare n0 n); intro H; simpl in |- *; trivial.
(* Goal: @eq lambda (Abs (subst_rec (unmark r) (unmark V) (S n))) (Abs (unmark (subst_rec_r r V (S n)))) *)
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; trivial.
(* Goal: forall _ : @eq nat n0 n, @eq lambda (lift n0 (unmark V)) (unmark (lift_r n0 V)) *)
(* Goal: @eq lambda (Abs (subst_rec (unmark r) (unmark V) (S n))) (Abs (unmark (subst_rec_r r V (S n)))) *)
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
rewrite (unmark_lift V n0); trivial.
(* Goal: @eq lambda (Abs (subst_rec (unmark r) (unmark V) (S n))) (Abs (unmark (subst_rec_r r V (S n)))) *)
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; trivial.
(* Goal: @eq lambda (App (subst_rec (unmark r) (unmark V) n) (subst_rec (unmark r0) (unmark V) n)) (App (unmark (subst_rec_r r V n)) (unmark (subst_rec_r r0 V n))) *)
elim H; elim H0; trivial.
Qed.

Lemma unmark_subst :
 forall U V : redexes, subst (unmark U) (unmark V) = unmark (subst_r U V).
Proof.
(* Goal: forall U V : redexes, @eq lambda (subst (unmark U) (unmark V)) (unmark (subst_r U V)) *)
unfold subst in |- *; unfold subst_r in |- *; intros; apply unmark_subst_rec.
Qed.

Lemma completeness :
 forall U V W : redexes, residuals U V W -> par_red1 (unmark U) (unmark W).
Proof.
(* Goal: forall (U V W : redexes) (_ : residuals U V W), par_red1 (unmark U) (unmark W) *)
simple induction 1; simpl in |- *; auto.
(* Goal: forall (U1 V1 W1 : redexes) (_ : residuals U1 V1 W1) (_ : par_red1 (unmark U1) (unmark W1)) (U2 V2 W2 : redexes) (_ : residuals U2 V2 W2) (_ : par_red1 (unmark U2) (unmark W2)) (_ : bool), par_red1 (App (Abs (unmark U1)) (unmark U2)) (unmark (subst_r W2 W1)) *)
intros; elim unmark_subst; auto.
Qed.


(**************************************************)
(* Reduction of a lambda term by a set of redexes *)
(**************************************************)

Definition reduction (M : lambda) (U : redexes) (N : lambda) :=
  residuals (mark M) U (mark N).

Lemma reduction_function :
 forall (M N P : lambda) (U : redexes),
 reduction M U N -> reduction M U P -> N = P.
Proof.
(* Goal: forall (M N P : lambda) (U : redexes) (_ : reduction M U N) (_ : reduction M U P), @eq lambda N P *)
unfold reduction in |- *; intros; cut (comp (mark N) (mark P)).
intro; rewrite (inverse N); rewrite (inverse P); apply comp_unmark_eq;
 trivial.
(* Goal: comp (mark N) (mark P) *)
apply mutual_residuals_comp with U (mark M) (mark M); trivial.
Qed.