(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*      Laurence Rideau, INRIA Sophia-Antipolis-M\u00e9diterran\u00e9e           *)
(*      Bernard Paul Serpette, INRIA Sophia-Antipolis-M\u00e9diterran\u00e9e     *)
(*      Xavier Leroy, INRIA Paris-Rocquencourt                         *)
(*                                                                     *)
(*  Copyright Institut National de Recherche en Informatique et en     *)
(*  Automatique.  All rights reserved.  This file is distributed       *)
(*  under the terms of the GNU General Public License as published by  *)
(*  the Free Software Foundation, either version 2 of the License, or  *)
(*  (at your option) any later version.  This file is also distributed *)
(*  under the terms of the INRIA Non-Commercial License Agreement.     *)
(*                                                                     *)
(* *********************************************************************)

(** Translation of parallel moves into sequences of individual moves *)

(** The ``parallel move'' problem, also known as ``parallel assignment'',
  is the following.  We are given a list of (source, destination) pairs
  of locations.  The goal is to find a sequence of elementary
  moves ([loc <- loc] assignments) such that, at the end of the sequence,
  location [dst] contains the value of location [src] at the beginning of
  the sequence, for each ([src], [dst]) pairs in the given problem.
  Moreover, other locations should keep their values, except one register
  of each type, which can be used as temporaries in the generated sequences.

  The parallel move problem is trivial if the sources and destinations do
  not overlap.  For instance,
<<
  (R1, R2) <- (R3, R4)     becomes    R1 <- R3; R2 <- R4
>>
  However, arbitrary overlap is allowed between sources and destinations.
  This requires some care in ordering the individual moves, as in
<<
  (R1, R2) <- (R3, R1)     becomes    R2 <- R1; R1 <- R3
>>
  Worse, cycles (permutations) can require the use of temporaries, as in
<<
  (R1, R2, R3) <- (R2, R3, R1)   becomes   T <- R1; R1 <- R2; R2 <- R3; R3 <- T;
>>
  An amazing fact is that for any parallel move problem, at most one temporary
  (or in our case one integer temporary and one float temporary) is needed.

  The development in this section was designed by Laurence Rideau and
  Bernard Serpette.  It is described in the paper
  ``Tilting at windmills with Coq: Formal verification of
  a compilation algorithm for parallel moves'',
  #<A HREF="http://hal.inria.fr/inria-00176007">#
  http://hal.inria.fr/inria-00176007
  #</A>#
*)

Require Import Relations.
Require Import Axioms.
Require Import Coqlib.
Require Import Recdef.

Section PARMOV.

(** * Registers, moves, and their semantics *)

(** The development is parameterized by the type of registers,
    equipped with a decidable equality. *)

Variable reg: Type.
Variable reg_eq : forall (r1 r2: reg), {r1=r2} + {r1<>r2}.

(** The [temp] function maps every register [r] to the register that
    should be used to save the value of [r] temporarily while performing
    a cyclic assignment involving [r].  In the simplest case, there is
    only one such temporary register [rtemp] and [temp] is the constant
    function [fun r => rtemp].  In more realistic cases, different temporary
    registers can be used to hold different values.  In the case of Compcert,
    we have two temporary registers, one for integer values and the other
    for floating-point values, and [temp] returns the appropriate temporary
    depending on the type of its argument. *)

Variable temp: reg -> reg.

Definition is_temp (r: reg): Prop := exists s, r = temp s.

(** A set of moves is a list of pairs of registers, of the form
    (source, destination). *)

Definition moves := (list (reg * reg))%type.  (* src -> dst *)

Definition srcs (m: moves) := List.map (@fst reg reg) m.
Definition dests (m: moves) := List.map (@snd reg reg) m.

(** ** Semantics of moves *)

(** The dynamic  semantics of moves is given in terms of environments.
    An environment is a mapping of registers to their current values. *)

Variable val: Type.
Definition env := reg -> val.

Lemma env_ext:
  forall (e1 e2: env),
  (forall r, e1 r = e2 r) -> e1 = e2.
Proof (@extensionality reg val).

(** The main operation over environments is update: it assigns
  a value [v] to a register [r] and preserves the values of other
  registers. *)

Definition update (r: reg) (v: val) (e: env) : env :=
  fun r' => if reg_eq r' r then v else e r'.

Lemma update_s:
  forall r v e, update r v e r = v.
Proof.
(* Goal: forall (r : reg) (v : val) (e : env) (r' : reg) (_ : not (@eq reg r' r)), @eq val (update r v e r') (e r') *)
  unfold update; intros. destruct (reg_eq r r). auto. congruence.
Qed.

Lemma update_o:
  forall r v e r', r' <> r -> update r v e r' =  e r'.
Proof.
(* Goal: forall (r : reg) (v : val) (e : env) (r' : reg) (_ : not (@eq reg r' r)), @eq val (update r v e r') (e r') *)
  unfold update; intros. destruct (reg_eq r' r). congruence. auto.
Qed.

Lemma update_ident:
  forall r e, update r (e r) e = e.
Proof.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  intros. apply env_ext; intro. unfold update. destruct (reg_eq r0 r); congruence.
Qed.

Lemma update_commut:
  forall r1 v1 r2 v2 e,
  r1 <> r2 ->
  update r1 v1 (update r2 v2 e) = update r2 v2 (update r1 v1 e).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. apply env_ext; intro; unfold update.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct (reg_eq r r1); destruct (reg_eq r r2); auto.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  congruence.
Qed.

Lemma update_twice:
  forall r v e,
  update r v (update r v e) = update r v e.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. apply env_ext; intro; unfold update.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct (reg_eq r0 r); auto.
Qed.

(** A list of moves [(src1, dst1), ..., (srcN, dstN)] can be given
  three different semantics, as environment transformers.
  The first semantics corresponds to a parallel assignment:
  [dst1, ..., dstN] are set to the initial values of [src1, ..., srcN]. *)

Fixpoint exec_par (m: moves) (e: env) {struct m}: env :=
  match m with
  | nil => e
  | (s, d) :: m' => update d (e s) (exec_par m' e)
  end.

(** The second semantics corresponds to a sequence of individual
  assignments: first, [dst1] is set to the initial value of [src1];
  then, [dst2] is set to the current value of [src2] after the first
  assignment; etc. *)

Fixpoint exec_seq (m: moves) (e: env) {struct m}: env :=
  match m with
  | nil => e
  | (s, d) :: m' => exec_seq m' (update d (e s) e)
  end.

(** The third semantics is also sequential, but executes the moves
  in reverse order, starting with [srcN, dstN] and finishing with
  [src1, dst1]. *)

Fixpoint exec_seq_rev (m: moves) (e: env) {struct m}: env :=
  match m with
  | nil => e
  | (s, d) :: m' =>
      let e' := exec_seq_rev m' e in
      update d (e' s) e'
  end.

(** * Specification of the non-deterministic algorithm *)

(** Rideau and Serpette's parallel move compilation algorithm is first
   specified as a non-deterministic set of rewriting rules operating
   over triples [(mu, sigma, tau)] of moves.  We define these rules
   as an inductive predicate [transition] relating triples of moves,
   and its reflexive transitive closure [transitions]. *)

Inductive state: Type :=
  State (mu: moves) (sigma: moves) (tau: moves) : state.

Definition no_read (mu: moves) (d: reg) : Prop :=
  ~In d (srcs mu).

Inductive transition: state -> state -> Prop :=
  | tr_nop: forall mu1 r mu2 sigma tau,
      transition (State (mu1 ++ (r, r) :: mu2) sigma tau)
                 (State (mu1 ++ mu2) sigma tau)
  | tr_start: forall mu1 s d mu2 tau,
      transition (State (mu1 ++ (s, d) :: mu2) nil tau)
                 (State (mu1 ++ mu2) ((s, d) :: nil) tau)
  | tr_push: forall mu1 d r mu2 s sigma tau,
      transition (State (mu1 ++ (d, r) :: mu2) ((s, d) :: sigma) tau)
                 (State (mu1 ++ mu2) ((d, r) :: (s, d) :: sigma) tau)
  | tr_loop: forall mu sigma s d tau,
      transition (State mu (sigma ++ (s, d) :: nil) tau)
                 (State mu (sigma ++ (temp s, d) :: nil) ((s, temp s) :: tau))
  | tr_pop: forall mu s0 d0 s1 d1 sigma tau,
      no_read mu d1 -> d1 <> s0 ->
      transition (State mu ((s1, d1) :: sigma ++ (s0, d0) :: nil) tau)
                 (State mu (sigma ++ (s0, d0) :: nil) ((s1, d1) :: tau))
  | tr_last: forall mu s d tau,
      no_read mu d ->
      transition (State mu ((s, d) :: nil) tau)
                 (State mu nil ((s, d) :: tau)).

Definition transitions: state -> state -> Prop :=
  clos_refl_trans state transition.

(** ** Well-formedness properties of states *)

(** A state [mu, sigma, tau] is well-formed if the following properties hold:

- [mu] concatenated with [sigma] is a ``mill'', that is, no destination
  appears twice (predicate [is_mill] below).
- No temporary register appears in [mu] (predicate [move_no_temp]).
- No temporary register appears in [sigma] except perhaps as the source
  of the last move in [sigma] (predicate [temp_last]).
- [sigma] is a ``path'', that is, the source of a move is the destination
  of the following move.
*)

Definition is_mill (m: moves) : Prop :=
  list_norepet (dests m).

Definition is_not_temp (r: reg) : Prop :=
  forall d, r <> temp d.

Definition move_no_temp (m: moves) : Prop :=
  forall s d, In (s, d) m -> is_not_temp s /\ is_not_temp d.

Definition temp_last (m: moves) : Prop :=
  match List.rev m with
  | nil => True
  | (s, d) :: m' => is_not_temp d /\ move_no_temp m'
  end.

Definition is_first_dest (m: moves) (d: reg) : Prop :=
  match m with
  | nil => True
  | (s0, d0) :: _ => d = d0
  end.

Inductive is_path: moves -> Prop :=
  | is_path_nil:
      is_path nil
  | is_path_cons: forall s d m,
      is_first_dest m s ->
      is_path m ->
      is_path ((s, d) :: m).

Inductive state_wf: state -> Prop :=
  | state_wf_intro:
      forall mu sigma tau,
      is_mill (mu ++ sigma) ->
      move_no_temp mu ->
      temp_last sigma ->
      is_path sigma ->
      state_wf (State mu sigma tau).

(** Some trivial properties of srcs and dests. *)

Lemma dests_append:
  forall m1 m2, dests (m1 ++ m2) = dests m1 ++ dests m2.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. unfold dests. apply map_app.
Qed.

Lemma dests_decomp:
  forall m1 s d m2, dests (m1 ++ (s, d) :: m2) = dests m1 ++ d :: dests m2.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. unfold dests. rewrite map_app. reflexivity.
Qed.

Lemma srcs_append:
  forall m1 m2, srcs (m1 ++ m2) = srcs m1 ++ srcs m2.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. unfold srcs. apply map_app.
Qed.

Lemma srcs_decomp:
  forall m1 s d m2, srcs (m1 ++ (s, d) :: m2) = srcs m1 ++ s :: srcs m2.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. unfold srcs. rewrite map_app. reflexivity.
Qed.

Lemma srcs_dests_combine:
  forall s d,
  List.length s = List.length d ->
  srcs (List.combine s d) = s /\
  dests (List.combine s d) = d.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  induction s; destruct d; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  tauto.
(* Goal: and (@eq (list reg) (@nil reg) (@cons reg a s)) (@eq (list reg) (@nil reg) (@nil reg)) *)
(* Goal: and (@eq (list reg) (@cons reg a (srcs (@combine reg reg s d))) (@cons reg a s)) (@eq (list reg) (@cons reg r (dests (@combine reg reg s d))) (@cons reg r d)) *)
  discriminate.
(* Goal: and (@eq (list reg) (@nil reg) (@cons reg a s)) (@eq (list reg) (@nil reg) (@nil reg)) *)
(* Goal: and (@eq (list reg) (@cons reg a (srcs (@combine reg reg s d))) (@cons reg a s)) (@eq (list reg) (@cons reg r (dests (@combine reg reg s d))) (@cons reg r d)) *)
  discriminate.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  elim (IHs d); intros. split; congruence. congruence.
Qed.

(** Some properties of [is_mill] and [dests_disjoint]. *)

Definition dests_disjoint (m1 m2: moves) : Prop :=
  list_disjoint (dests m1) (dests m2).

Lemma dests_disjoint_sym:
  forall m1 m2,
  dests_disjoint m1 m2 <-> dests_disjoint m2 m1.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold dests_disjoint; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  split; intros; apply list_disjoint_sym; auto.
Qed.

Lemma dests_disjoint_cons_left:
  forall m1 s d m2,
  dests_disjoint ((s, d) :: m1) m2 <->
  dests_disjoint m1 m2 /\ ~In d (dests m2).
Proof.
(* Goal: forall (m1 m2 : list (prod reg reg)) (m3 : moves), iff (dests_disjoint (@app (prod reg reg) m1 m2) m3) (and (dests_disjoint m1 m3) (dests_disjoint m2 m3)) *)
  unfold dests_disjoint, list_disjoint.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  simpl; intros; split; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  split. auto. firstorder.
(* Goal: no_adherence s *)
(* Goal: @eq val (update d (e1 s) e1 r) (weak_update d (e2 s) e2 r) *)
  destruct H. elim H0; intro.
(* Goal: not (@eq reg x y) *)
(* Goal: not (@eq reg x y) *)
  red; intro; subst. contradiction.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply H; auto.
Qed.

Lemma dests_disjoint_cons_right:
  forall m1 s d m2,
  dests_disjoint m1 ((s, d) :: m2) <->
  dests_disjoint m1 m2 /\ ~In d (dests m1).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. rewrite dests_disjoint_sym. rewrite dests_disjoint_cons_left.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite dests_disjoint_sym. tauto.
Qed.

Lemma dests_disjoint_append_left:
  forall m1 m2 m3,
  dests_disjoint (m1 ++ m2) m3 <->
  dests_disjoint m1 m3 /\ dests_disjoint m2 m3.
Proof.
(* Goal: forall (m1 m2 : list (prod reg reg)) (m3 : moves), iff (dests_disjoint (@app (prod reg reg) m1 m2) m3) (and (dests_disjoint m1 m3) (dests_disjoint m2 m3)) *)
  unfold dests_disjoint, list_disjoint.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros; split; intros. split; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply H; eauto. rewrite dests_append. apply in_or_app. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply H; eauto. rewrite dests_append. apply in_or_app. auto.
(* Goal: no_adherence s *)
(* Goal: @eq val (update d (e1 s) e1 r) (weak_update d (e2 s) e2 r) *)
  destruct H.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite dests_append in H0. elim (in_app_or _ _ _ H0); auto.
Qed.

Lemma dests_disjoint_append_right:
  forall m1 m2 m3,
  dests_disjoint m1 (m2 ++ m3) <->
  dests_disjoint m1 m2 /\ dests_disjoint m1 m3.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. rewrite dests_disjoint_sym. rewrite dests_disjoint_append_left.
(* Goal: iff (and (dests_disjoint m2 m1) (dests_disjoint m3 m1)) (and (dests_disjoint m1 m2) (dests_disjoint m1 m3)) *)
  intuition; rewrite dests_disjoint_sym; assumption.
Qed.

Lemma is_mill_cons:
  forall s d m,
  is_mill ((s, d) :: m) <->
  is_mill m /\ ~In d (dests m).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold is_mill, dests_disjoint; intros. simpl.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  split; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  inversion H; tauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  constructor; tauto.
Qed.

Lemma is_mill_append:
  forall m1 m2,
  is_mill (m1 ++ m2) <->
  is_mill m1 /\ is_mill m2 /\ dests_disjoint m1 m2.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold is_mill, dests_disjoint; intros. rewrite dests_append.
(* Goal: iff (@list_norepet reg (@app reg (dests m1) (dests m2))) (and (@list_norepet reg (dests m1)) (and (@list_norepet reg (dests m2)) (@list_disjoint reg (dests m1) (dests m2)))) *)
  apply list_norepet_app.
Qed.

(** Some properties of [move_no_temp]. *)

Lemma move_no_temp_append:
  forall m1 m2,
  move_no_temp m1 -> move_no_temp m2 -> move_no_temp (m1 ++ m2).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros; red; intros. elim (in_app_or _ _ _ H1); intro.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply H; auto. apply H0; auto.
Qed.

Lemma move_no_temp_rev:
  forall m, move_no_temp (List.rev m) -> move_no_temp m.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros; red; intros. apply H. rewrite <- List.In_rev. auto.
Qed.

(** Some properties of [temp_last]. *)

Lemma temp_last_change_last_source:
  forall s d s' sigma,
  temp_last (sigma ++ (s, d) :: nil) ->
  temp_last (sigma ++ (s', d) :: nil).
Proof.
(* Goal: forall (s d s' : reg) (sigma : list (prod reg reg)) (_ : temp_last (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg))))), temp_last (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s' d) (@nil (prod reg reg)))) *)
  intros until sigma. unfold temp_last.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  repeat rewrite rev_unit. auto.
Qed.

Lemma temp_last_push:
  forall s1 d1 s2 d2 sigma,
  temp_last ((s2, d2) :: sigma) ->
  is_not_temp s1 -> is_not_temp d1 ->
  temp_last ((s1, d1) :: (s2, d2) :: sigma).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold temp_last; intros. simpl. simpl in H.
(* Goal: match @app (prod reg reg) (@app (prod reg reg) (@rev (prod reg reg) sigma) (@cons (prod reg reg) (@pair reg reg s2 d2) (@nil (prod reg reg)))) (@cons (prod reg reg) (@pair reg reg s1 d1) (@nil (prod reg reg))) with | nil => True | cons (pair s d as p) m' => and (is_not_temp d) (move_no_temp m') end *)
  destruct (rev sigma); simpl in *.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intuition. red; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  elim H; intros. inversion H4. subst; tauto. tauto.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
  destruct p as [sN dN]. intuition.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  red; intros. elim (in_app_or _ _ _ H); intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply H3; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  elim H4; intros. inversion H5; subst; tauto. elim H5.
Qed.

Lemma temp_last_pop:
  forall s1 d1 sigma s2 d2,
  temp_last ((s1, d1) :: sigma ++ (s2, d2) :: nil) ->
  temp_last (sigma ++ (s2, d2) :: nil).
Proof.
(* Goal: forall (s1 d1 : reg) (sigma : list (prod reg reg)) (s2 d2 : reg) (_ : temp_last (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s2 d2) (@nil (prod reg reg)))))), temp_last (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s2 d2) (@nil (prod reg reg)))) *)
  intros until d2.
  change ((s1, d1) :: sigma ++ (s2, d2) :: nil)
    with ((((s1, d1) :: nil) ++ sigma) ++ ((s2, d2) :: nil)).
(* Goal: temp_last (@nil (prod reg reg)) *)
(* Goal: is_path (@nil (prod reg reg)) *)
  unfold temp_last. repeat rewrite rev_unit.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intuition. simpl in H1. red; intros. apply H1.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply in_or_app. auto.
Qed.

(** Some properties of [is_path]. *)

Lemma is_path_pop:
  forall s d m,
  is_path ((s, d) :: m) -> is_path m.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. inversion H; subst. auto.
Qed.

Lemma is_path_change_last_source:
  forall s s' d sigma,
  is_path (sigma ++ (s, d) :: nil) ->
  is_path (sigma ++ (s', d) :: nil).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  induction sigma; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  constructor. red; auto. constructor.
(* Goal: @incl reg (@cons reg (@fst reg reg a) (srcs (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (@cons reg s0 (@cons reg (@snd reg reg a) (dests (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))))) *)
  inversion H; subst; clear H.
(* Goal: @list_norepet reg (@cons reg r l) *)
  constructor.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct sigma as [ | [s1 d1] sigma']; simpl; simpl in H2; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
Qed.

Lemma path_sources_dests:
  forall s0 d0 sigma,
  is_path (sigma ++ (s0, d0) :: nil) ->
  List.incl (srcs (sigma ++ (s0, d0) :: nil))
            (s0 :: dests (sigma ++ (s0, d0) :: nil)).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  induction sigma; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red; simpl; tauto.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  inversion H; subst; clear H. simpl.
(* Goal: @incl reg (@cons reg s (srcs (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (@cons reg s0 (@cons reg d (dests (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))))) *)
  assert (In s (dests (sigma ++ (s0, d0) :: nil))).
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
    destruct sigma as [ | [s1 d1] sigma']; simpl; simpl in H2; intuition.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply incl_cons. simpl; tauto.
(* Goal: @incl reg (srcs (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (@cons reg s0 (@cons reg d (dests (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))))) *)
  apply incl_tran with (s0 :: dests (sigma ++ (s0, d0) :: nil)).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply IHsigma; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red; simpl; tauto.
Qed.

Lemma no_read_path:
  forall d1 sigma s0 d0,
  d1 <> s0 ->
  is_path (sigma ++ (s0, d0) :: nil) ->
  ~In d1 (dests (sigma ++ (s0, d0) :: nil)) ->
  no_read (sigma ++ (s0, d0) :: nil) d1.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros.
(* Goal: no_read (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) d1 *)
  generalize (path_sources_dests _ _ _ H0). intro.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intro. elim H1. elim (H2 _ H3); intro. congruence. auto.
Qed.

(** To benefit from some proof automation, we populate a rewrite database
  with some of the properties above. *)

Lemma notin_dests_cons:
  forall x s d m,
  ~In x (dests ((s, d) :: m)) <-> x <> d /\ ~In x (dests m).
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. simpl. intuition auto.
Qed.

Lemma notin_dests_append:
  forall d m1 m2,
  ~In d (dests (m1 ++ m2)) <-> ~In d (dests m1) /\ ~In d (dests m2).
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. rewrite dests_append. rewrite in_app. tauto.
Qed.

Hint Rewrite is_mill_cons is_mill_append
  dests_disjoint_cons_left dests_disjoint_cons_right
  dests_disjoint_append_left dests_disjoint_append_right
  notin_dests_cons notin_dests_append: pmov.

(** ** Transitions preserve well-formedness *)

Lemma transition_preserves_wf:
  forall st st',
  transition st st' -> state_wf st -> state_wf st'.
Proof.
  induction 1; intro WF; inversion WF as [mu0 sigma0 tau0 A B C D];
  subst;
(* Goal: not (@In reg d (dests (@app (prod reg reg) mu sigma))) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) (@app (prod reg reg) mu sigma) (@cons (prod reg reg) (@pair reg reg (temp s) d) (@nil (prod reg reg)))) (update (temp s) (e1 s) e1)) (exec_par (@app (prod reg reg) (@app (prod reg reg) mu sigma) (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) e1) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  autorewrite with pmov in A; constructor; autorewrite with pmov.

  (* Nop *)
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  tauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red; intros. apply B. apply list_in_insert; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto. auto.

  (* Start *)
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  tauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red; intros. apply B. apply list_in_insert; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red. simpl. split. elim (B s d). auto.
(* Goal: @In (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) mu1 (@cons (prod reg reg) (@pair reg reg s d) mu2)) *)
(* Goal: move_no_temp (@nil (prod reg reg)) *)
(* Goal: is_path (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg))) *)
(* Goal: and (and (is_mill mu1) (and (is_mill mu2) (dests_disjoint mu1 mu2))) (and (and (and (is_mill sigma) (not (@In reg d (dests sigma)))) (and (not (@eq reg r d)) (not (@In reg r (dests sigma))))) (and (and (and (dests_disjoint mu1 sigma) (not (@In reg d (dests mu1)))) (not (@In reg r (dests mu1)))) (and (and (dests_disjoint mu2 sigma) (not (@In reg d (dests mu2)))) (not (@In reg r (dests mu2)))))) *)
(* Goal: move_no_temp (@app (prod reg reg) mu1 mu2) *)
(* Goal: temp_last (@cons (prod reg reg) (@pair reg reg d r) (@cons (prod reg reg) (@pair reg reg s d) sigma)) *)
(* Goal: is_path (@cons (prod reg reg) (@pair reg reg d r) (@cons (prod reg reg) (@pair reg reg s d) sigma)) *)
(* Goal: and (is_mill mu) (and (and (is_mill sigma) (and (and (is_mill (@nil (prod reg reg))) (not (@In reg d (dests (@nil (prod reg reg)))))) (and (dests_disjoint sigma (@nil (prod reg reg))) (not (@In reg d (dests sigma)))))) (and (dests_disjoint mu sigma) (and (dests_disjoint mu (@nil (prod reg reg))) (not (@In reg d (dests mu)))))) *)
(* Goal: move_no_temp mu *)
(* Goal: temp_last (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg (temp s) d) (@nil (prod reg reg)))) *)
(* Goal: is_path (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg (temp s) d) (@nil (prod reg reg)))) *)
(* Goal: and (is_mill mu) (and (and (is_mill sigma) (and (and (is_mill (@nil (prod reg reg))) (not (@In reg d0 (dests (@nil (prod reg reg)))))) (and (dests_disjoint sigma (@nil (prod reg reg))) (not (@In reg d0 (dests sigma)))))) (and (dests_disjoint mu sigma) (and (dests_disjoint mu (@nil (prod reg reg))) (not (@In reg d0 (dests mu)))))) *)
(* Goal: move_no_temp mu *)
(* Goal: temp_last (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) *)
(* Goal: is_path (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) *)
(* Goal: and (is_mill mu) (and (is_mill (@nil (prod reg reg))) (dests_disjoint mu (@nil (prod reg reg)))) *)
(* Goal: move_no_temp mu *)
(* Goal: temp_last (@nil (prod reg reg)) *)
(* Goal: is_path (@nil (prod reg reg)) *)
  apply in_or_app. right. apply in_eq.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red; simpl; tauto.
(* Goal: @list_norepet reg (@cons reg r l) *)
  constructor. exact I. constructor.

  (* Push *)
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
  intuition.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red; intros. apply B. apply list_in_insert; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  elim (B d r). apply temp_last_push; auto.
(* Goal: @In (prod reg reg) (@pair reg reg s d) (@cons (prod reg reg) (@pair reg reg s d) mu2) *)
(* Goal: move_no_temp (@nil (prod reg reg)) *)
(* Goal: is_path (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg))) *)
(* Goal: and (and (is_mill mu1) (and (is_mill mu2) (dests_disjoint mu1 mu2))) (and (and (and (is_mill sigma) (not (@In reg d (dests sigma)))) (and (not (@eq reg r d)) (not (@In reg r (dests sigma))))) (and (and (and (dests_disjoint mu1 sigma) (not (@In reg d (dests mu1)))) (not (@In reg r (dests mu1)))) (and (and (dests_disjoint mu2 sigma) (not (@In reg d (dests mu2)))) (not (@In reg r (dests mu2)))))) *)
(* Goal: move_no_temp (@app (prod reg reg) mu1 mu2) *)
(* Goal: temp_last (@cons (prod reg reg) (@pair reg reg d r) (@cons (prod reg reg) (@pair reg reg s d) sigma)) *)
(* Goal: is_path (@cons (prod reg reg) (@pair reg reg d r) (@cons (prod reg reg) (@pair reg reg s d) sigma)) *)
(* Goal: and (is_mill mu) (and (and (is_mill sigma) (and (and (is_mill (@nil (prod reg reg))) (not (@In reg d (dests (@nil (prod reg reg)))))) (and (dests_disjoint sigma (@nil (prod reg reg))) (not (@In reg d (dests sigma)))))) (and (dests_disjoint mu sigma) (and (dests_disjoint mu (@nil (prod reg reg))) (not (@In reg d (dests mu)))))) *)
(* Goal: move_no_temp mu *)
(* Goal: temp_last (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg (temp s) d) (@nil (prod reg reg)))) *)
(* Goal: is_path (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg (temp s) d) (@nil (prod reg reg)))) *)
(* Goal: and (is_mill mu) (and (and (is_mill sigma) (and (and (is_mill (@nil (prod reg reg))) (not (@In reg d0 (dests (@nil (prod reg reg)))))) (and (dests_disjoint sigma (@nil (prod reg reg))) (not (@In reg d0 (dests sigma)))))) (and (dests_disjoint mu sigma) (and (dests_disjoint mu (@nil (prod reg reg))) (not (@In reg d0 (dests mu)))))) *)
(* Goal: move_no_temp mu *)
(* Goal: temp_last (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) *)
(* Goal: is_path (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) *)
(* Goal: and (is_mill mu) (and (is_mill (@nil (prod reg reg))) (dests_disjoint mu (@nil (prod reg reg)))) *)
(* Goal: move_no_temp mu *)
(* Goal: temp_last (@nil (prod reg reg)) *)
(* Goal: is_path (@nil (prod reg reg)) *)
  apply in_or_app; right; apply in_eq.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  constructor. simpl. auto. auto.

  (* Loop *)
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  tauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply temp_last_change_last_source; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply is_path_change_last_source; eauto.

  (* Pop *)
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
  intuition.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply temp_last_pop; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply is_path_pop; eauto.

  (* Last *)
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
  intuition.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold temp_last. simpl. auto.
(* Goal: @list_norepet reg (@cons reg r l) *)
  constructor.
Qed.

Lemma transitions_preserve_wf:
  forall st st', transitions st st' -> state_wf st -> state_wf st'.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  induction 1; intros; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply transition_preserves_wf; eauto.
Qed.

(** ** Transitions preserve semantics *)

(** We define the semantics of states as follows.
  For a triple [mu, sigma, tau], we perform the moves [tau] in
  reverse sequential order, then the moves [mu ++ sigma] in parallel. *)

Definition statemove (st: state) (e: env) :=
  match st with
  | State mu sigma tau => exec_par (mu ++ sigma) (exec_seq_rev tau e)
  end.

(** In preparation to showing that transitions preserve semantics,
  we prove various properties of the parallel and sequential interpretations
  of moves. *)

Lemma exec_par_outside:
  forall m e r, ~In r (dests m) -> exec_par m e r = e r.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  induction m; simpl; intros. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct a as [s d]. rewrite update_o. apply IHm. tauto.
(* Goal: env_match (exec_seq m (update d (e1 s) e1)) (weak_exec_seq m (weak_update d (e2 s) e2)) *)
  simpl in H. intuition.
Qed.

Lemma exec_par_lift:
  forall m1 s d m2 e,
  ~In d (dests m1) ->
  exec_par (m1 ++ (s, d) :: m2) e = exec_par ((s, d) :: m1 ++ m2) e.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  induction m1; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  destruct a as [s0 d0]. simpl in H. rewrite IHm1. simpl.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply update_commut. tauto. tauto.
Qed.

Lemma exec_par_ident:
  forall m1 r m2 e,
  is_mill (m1 ++ (r, r) :: m2) ->
  exec_par (m1 ++ (r, r) :: m2) e = exec_par (m1 ++ m2) e.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. autorewrite with pmov in H.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  rewrite exec_par_lift. simpl.
(* Goal: @eq env (update r (e r) (exec_par (@app (prod reg reg) m1 m2) e)) (exec_par (@app (prod reg reg) m1 m2) e) *)
(* Goal: not (@In reg r (dests m1)) *)
  replace (e r) with (exec_par (m1 ++ m2) e r). apply update_ident.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply exec_par_outside. autorewrite with pmov. tauto. tauto.
Qed.

Lemma exec_seq_app:
  forall m1 m2 e,
  exec_seq (m1 ++ m2) e = exec_seq m2 (exec_seq m1 e).
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  induction m1; simpl; intros. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct a as [s d]. rewrite IHm1. auto.
Qed.

Lemma exec_seq_rev_app:
  forall m1 m2 e,
  exec_seq_rev (m1 ++ m2) e = exec_seq_rev m1 (exec_seq_rev m2 e).
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  induction m1; simpl; intros. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct a as [s d]. rewrite IHm1. auto.
Qed.

Lemma exec_seq_exec_seq_rev:
  forall m e,
  exec_seq_rev m e = exec_seq (List.rev m) e.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  induction m; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct a as [s d]. rewrite exec_seq_app. simpl. rewrite IHm. auto.
Qed.

Lemma exec_seq_rev_exec_seq:
  forall m e,
  exec_seq m e = exec_seq_rev (List.rev m) e.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. generalize (exec_seq_exec_seq_rev (List.rev m) e).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite List.rev_involutive. auto.
Qed.

Lemma exec_par_update_no_read:
  forall s d m e,
  no_read m d ->
  ~In d (dests m) ->
  exec_par m (update d (e s) e) =
  update d (e s) (exec_par m e).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold no_read; induction m; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: @eq env (update d0 (update d (e s) e s0) (exec_par m (update d (e s) e))) (update d (e s) (update d0 (e s0) (exec_par m e))) *)
  destruct a as [s0 d0]; simpl in *. rewrite IHm.
(* Goal: @eq env (update d0 (update d (e s) e s0) (update d (e s) (exec_par m e))) (update d (e s) (update d0 (e s0) (exec_par m e))) *)
(* Goal: not (@In reg d (srcs m)) *)
(* Goal: not (@In reg d (dests m)) *)
  rewrite update_commut. f_equal. f_equal.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply update_o. tauto. tauto. tauto. tauto.
Qed.

Lemma exec_par_append_eq:
  forall m1 m2 m3 e2 e3,
  exec_par m2 e2 = exec_par m3 e3 ->
  (forall r, In r (srcs m1) -> e2 r = e3 r) ->
  exec_par (m1 ++ m2) e2 = exec_par (m1 ++ m3) e3.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  induction m1; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto. destruct a as [s d]. f_equal; eauto.
Qed.

Lemma exec_par_combine:
  forall e sl dl,
  List.length sl = List.length dl ->
  list_norepet dl ->
  let e' := exec_par (combine sl dl) e in
  List.map e' dl = List.map e sl /\
  (forall l, ~In l dl -> e' l = e l).
Proof.
(* Goal: and (@eq (list reg) (@nil reg) (@cons reg a s)) (@eq (list reg) (@nil reg) (@nil reg)) *)
(* Goal: and (@eq (list reg) (@cons reg a (srcs (@combine reg reg s d))) (@cons reg a s)) (@eq (list reg) (@cons reg r (dests (@combine reg reg s d))) (@cons reg r d)) *)
  induction sl; destruct dl; simpl; intros; try discriminate.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  split; auto.
(* Goal: and (@eq (list val) (@cons val (update r (e a) (exec_par (@combine reg reg sl dl) e) r) (@map reg val (update r (e a) (exec_par (@combine reg reg sl dl) e)) dl)) (@cons val (e a) (@map reg val e sl))) (forall (l : reg) (_ : not (or (@eq reg r l) (@In reg l dl))), @eq val (update r (e a) (exec_par (@combine reg reg sl dl) e) l) (e l)) *)
  inversion H0; subst; clear H0.
(* Goal: and (@eq (list val) (@cons val (update r (e a) (exec_par (@combine reg reg sl dl) e) r) (@map reg val (update r (e a) (exec_par (@combine reg reg sl dl) e)) dl)) (@cons val (e a) (@map reg val e sl))) (forall (l : reg) (_ : not (or (@eq reg r l) (@In reg l dl))), @eq val (update r (e a) (exec_par (@combine reg reg sl dl) e) l) (e l)) *)
  injection H; intro; clear H.
(* Goal: and (@eq (list val) (@cons val (update r (e a) (exec_par (@combine reg reg sl dl) e) r) (@map reg val (update r (e a) (exec_par (@combine reg reg sl dl) e)) dl)) (@cons val (e a) (@map reg val e sl))) (forall (l : reg) (_ : not (or (@eq reg r l) (@In reg l dl))), @eq val (update r (e a) (exec_par (@combine reg reg sl dl) e) l) (e l)) *)
  destruct (IHsl dl H0 H4) as [A B].
(* Goal: and (@eq (list val) (@cons val (update r (e a) (exec_par (@combine reg reg sl dl) e) r) (@map reg val (update r (e a) (exec_par (@combine reg reg sl dl) e)) dl)) (@cons val (e a) (@map reg val e sl))) (forall (l : reg) (_ : not (or (@eq reg r l) (@In reg l dl))), @eq val (update r (e a) (exec_par (@combine reg reg sl dl) e) l) (e l)) *)
  set (e' := exec_par (combine sl dl) e) in *.
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
  split.
(* Goal: @eq (list val) (@cons val (update r (e a) e' r) (@map reg val (update r (e a) e') dl)) (@cons val (e a) (@map reg val e sl)) *)
(* Goal: forall (l : reg) (_ : not (or (@eq reg r l) (@In reg l dl))), @eq val (update r (e a) e' l) (e l) *)
  decEq. apply update_s.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite <- A. apply list_map_exten; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite update_o. auto. congruence.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. rewrite update_o. apply B. tauto. intuition.
Qed.

(** [env_equiv] is an equivalence relation between environments
  capturing the fact that two environments assign the same values to
  non-temporary registers, but can disagree on the values of temporary
  registers. *)

Definition env_equiv (e1 e2: env) : Prop :=
  forall r, is_not_temp r -> e1 r = e2 r.

Lemma env_equiv_refl:
  forall e, env_equiv e e.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold env_equiv; auto.
Qed.

Lemma env_equiv_refl':
  forall e1 e2, e1 = e2 -> env_equiv e1 e2.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold env_equiv; intros. rewrite H. auto.
Qed.

Lemma env_equiv_sym:
  forall e1 e2, env_equiv e1 e2 -> env_equiv e2 e1.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold env_equiv; intros. symmetry; auto.
Qed.

Lemma env_equiv_trans:
  forall e1 e2 e3, env_equiv e1 e2 -> env_equiv e2 e3 -> env_equiv e1 e3.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold env_equiv; intros. transitivity (e2 r); auto.
Qed.

Lemma exec_par_env_equiv:
  forall m e1 e2,
  move_no_temp m ->
  env_equiv e1 e2 ->
  env_equiv (exec_par m e1) (exec_par m e2).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold move_no_temp; induction m; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: env_match (let (s, d) := a in exec_seq m (update d (e1 s) e1)) (let (s, d) := a in weak_exec_seq m (weak_update d (e2 s) e2)) *)
  destruct a as [s d].
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  red; intros. unfold update. destruct (reg_eq r d).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply H0. elim (H s d); tauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply IHm; auto.
Qed.

(** The proof that transitions preserve semantics (up to the values of
  temporary registers) follows. *)

Lemma transition_preserves_semantics:
  forall st st' e,
  transition st st' -> state_wf st ->
  env_equiv (statemove st' e) (statemove st e).
Proof.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  induction 1; intro WF; inversion WF as [mu0 sigma0 tau0 A B C D]; subst; simpl.

  (* nop *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  apply env_equiv_refl'. unfold statemove.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  repeat rewrite app_ass. simpl. symmetry. apply exec_par_ident.
(* Goal: is_mill (@app (prod reg reg) mu1 (@cons (prod reg reg) (@pair reg reg r r) (@app (prod reg reg) mu2 sigma))) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) (@app (prod reg reg) mu1 mu2) (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) (exec_par (@app (prod reg reg) (@app (prod reg reg) mu1 (@cons (prod reg reg) (@pair reg reg s d) mu2)) (@nil (prod reg reg))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) (@app (prod reg reg) mu1 mu2) (@cons (prod reg reg) (@pair reg reg d r) (@cons (prod reg reg) (@pair reg reg s d) sigma))) (exec_seq_rev tau e)) (exec_par (@app (prod reg reg) (@app (prod reg reg) mu1 (@cons (prod reg reg) (@pair reg reg d r) mu2)) (@cons (prod reg reg) (@pair reg reg s d) sigma)) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg (temp s) d) (@nil (prod reg reg))))) (update (temp s) (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  rewrite app_ass in A. exact A.

  (* start *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  apply env_equiv_refl'. unfold statemove.
(* Goal: @eq env (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (e1 s1) e1)) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) e1) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  autorewrite with pmov in A.
(* Goal: @eq env (update d (exec_seq_rev tau e s) (exec_par (@app (prod reg reg) mu1 (@app (prod reg reg) mu2 (@nil (prod reg reg)))) (exec_seq_rev tau e))) (exec_par (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) mu1 (@app (prod reg reg) mu2 (@nil (prod reg reg))))) (exec_seq_rev tau e)) *)
(* Goal: not (@In reg d (dests mu1)) *)
(* Goal: not (@In reg d (dests (@app (prod reg reg) mu1 mu2))) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) (@app (prod reg reg) mu1 mu2) (@cons (prod reg reg) (@pair reg reg d r) (@cons (prod reg reg) (@pair reg reg s d) sigma))) (exec_seq_rev tau e)) (exec_par (@app (prod reg reg) (@app (prod reg reg) mu1 (@cons (prod reg reg) (@pair reg reg d r) mu2)) (@cons (prod reg reg) (@pair reg reg s d) sigma)) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg (temp s) d) (@nil (prod reg reg))))) (update (temp s) (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  rewrite exec_par_lift. repeat rewrite app_ass. simpl. rewrite exec_par_lift. reflexivity.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  tauto. autorewrite with pmov. tauto.

  (* push *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  apply env_equiv_refl'. unfold statemove.
(* Goal: @eq env (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (e1 s1) e1)) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) e1) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  autorewrite with pmov in A.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  rewrite exec_par_lift. rewrite exec_par_lift. simpl.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  rewrite exec_par_lift. repeat rewrite app_ass. simpl. rewrite exec_par_lift.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  simpl. apply update_commut. intuition.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  tauto. autorewrite with pmov; tauto.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
  autorewrite with pmov; intuition.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
  autorewrite with pmov; intuition.

  (* loop *)
(* Goal: forall _ : env_equiv (statemove (State (@nil (prod reg reg)) (@nil (prod reg reg)) tau) e) (statemove (State mu (@nil (prod reg reg)) (@nil (prod reg reg))) e), env_equiv (exec_seq (@rev (prod reg reg) tau) e) (exec_par mu e) *)
  unfold statemove. simpl exec_seq_rev.
(* Goal: @eq env (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  set (e1 := exec_seq_rev tau e).
(* Goal: @eq env (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (e1 s1) e1)) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) e1) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  autorewrite with pmov in A.
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg (temp s) d) (@nil (prod reg reg))))) (update (temp s) (e1 s) e1)) (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg))))) e1) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  repeat rewrite <- app_ass.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  assert (~In d (dests (mu ++ sigma))). autorewrite with pmov. tauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  repeat rewrite exec_par_lift; auto. simpl.
(* Goal: env_equiv (update d (update (temp s) (e1 s) e1 (temp s)) (exec_par (@app (prod reg reg) (@app (prod reg reg) mu sigma) (@nil (prod reg reg))) (update (temp s) (e1 s) e1))) (update d (e1 s) (exec_par (@app (prod reg reg) (@app (prod reg reg) mu sigma) (@nil (prod reg reg))) e1)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  repeat rewrite <- app_nil_end.
(* Goal: env_equiv (update d (update (temp s) (e1 s) e1 (temp s)) (exec_par (@app (prod reg reg) mu sigma) (update (temp s) (e1 s) e1))) (update d (e1 s) (exec_par (@app (prod reg reg) mu sigma) e1)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  assert (move_no_temp (mu ++ sigma)).
(* Goal: move_no_temp (@app (prod reg reg) mu sigma) *)
(* Goal: env_equiv (update d (update (temp s) (e1 s) e1 (temp s)) (exec_par (@app (prod reg reg) mu sigma) (update (temp s) (e1 s) e1))) (update d (e1 s) (exec_par (@app (prod reg reg) mu sigma) e1)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
    red in C. rewrite rev_unit in C. destruct C.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    apply move_no_temp_append; auto. apply move_no_temp_rev; auto.
(* Goal: env_equiv (update d (update (temp s) (e1 s) e1 (temp s)) (exec_par (@app (prod reg reg) mu sigma) (update (temp s) (e1 s) e1))) (update d (e1 s) (exec_par (@app (prod reg reg) mu sigma) e1)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  set (e2 := update (temp s) (e1 s) e1).
(* Goal: env_equiv (update d (e2 (temp s)) (exec_par (@app (prod reg reg) mu sigma) e2)) (update d (e1 s) (exec_par (@app (prod reg reg) mu sigma) e1)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  set (e3 := exec_par (mu ++ sigma) e1).
(* Goal: env_equiv (update d (e2 (temp s)) (exec_par (@app (prod reg reg) mu sigma) e2)) (update d (e1 s) e3) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  set (e4 := exec_par (mu ++ sigma) e2).
(* Goal: env_equiv (update d (e2 (temp s)) e4) (update d (e1 s) e3) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  assert (env_equiv e2 e1).
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
    unfold e2; red; intros. apply update_o. apply H1.
(* Goal: env_equiv (update d (e2 (temp s)) e4) (update d (e1 s) e3) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (exec_seq_rev tau e s1) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) (exec_seq_rev tau e)) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  assert (env_equiv e4 e3).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    unfold e4, e3. apply exec_par_env_equiv; auto.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  red; intros. unfold update. destruct (reg_eq r d).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold e2. apply update_s. apply H2. auto.

  (* pop *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  apply env_equiv_refl'. unfold statemove. simpl exec_seq_rev.
(* Goal: @eq env (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  set (e1 := exec_seq_rev tau e).
(* Goal: @eq env (exec_par (@app (prod reg reg) mu (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg))))) (update d1 (e1 s1) e1)) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s1 d1) (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))))) e1) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  autorewrite with pmov in A.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  apply exec_par_append_eq. simpl.
(* Goal: @eq env (exec_par (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) (update d1 (e1 s1) e1)) (update d1 (e1 s1) (exec_par (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) e1)) *)
(* Goal: forall (r : reg) (_ : @In reg r (srcs mu)), @eq val (update d1 (e1 s1) e1 r) (e1 r) *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  apply exec_par_update_no_read.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply no_read_path; auto. eapply is_path_pop; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  autorewrite with pmov; tauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  autorewrite with pmov; tauto.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. apply update_o. red; intro; subst r. elim (H H1).

  (* last *)
(* Goal: env_equiv (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  apply env_equiv_refl'. unfold statemove. simpl exec_seq_rev.
(* Goal: @eq env (exec_par (@app (prod reg reg) mu (@nil (prod reg reg))) (update d (exec_seq_rev tau e s) (exec_seq_rev tau e))) (exec_par (@app (prod reg reg) mu (@cons (prod reg reg) (@pair reg reg s d) (@nil (prod reg reg)))) (exec_seq_rev tau e)) *)
  set (e1 := exec_seq_rev tau e).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply exec_par_append_eq. simpl. auto.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. apply update_o. red; intro; subst r. elim (H H0).
Qed.

Lemma transitions_preserve_semantics:
  forall st st' e,
  transitions st st' -> state_wf st ->
  env_equiv (statemove st' e) (statemove st e).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  induction 1; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply transition_preserves_semantics; eauto.
(* Goal: env_equiv (statemove x e) (statemove x e) *)
(* Goal: env_equiv (statemove z e) (statemove x e) *)
  apply env_equiv_refl.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply env_equiv_trans with (statemove y e); auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply IHclos_refl_trans2. eapply transitions_preserve_wf; eauto.
Qed.

Lemma state_wf_start:
  forall mu,
  move_no_temp mu ->
  is_mill mu ->
  state_wf (State mu nil nil).
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. constructor. rewrite <- app_nil_end. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red. simpl. auto.
(* Goal: @list_norepet reg (@cons reg r l) *)
  constructor.
Qed.

(** The main correctness result in this section is the following:
  if we can transition repeatedly from an initial state [mu, nil, nil]
  to a final state [nil, nil, tau], then the sequential execution
  of the moves [rev tau] is semantically equivalent to the parallel
  execution of the moves [mu]. *)

Theorem transitions_correctness:
  forall mu tau,
  move_no_temp mu ->
  is_mill mu ->
  transitions (State mu nil nil) (State nil nil tau) ->
  forall e, env_equiv (exec_seq (List.rev tau) e) (exec_par mu e).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros.
  generalize (transitions_preserve_semantics _ _ e H1
              (state_wf_start _ H H0)).
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  unfold statemove. simpl. rewrite <- app_nil_end.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite exec_seq_exec_seq_rev. auto.
Qed.

(** * Determinisation of the transition relation *)

(** We now define a deterministic variant [dtransition] of the
  transition relation [transition].  [dtransition] is deterministic
  in the sense that at most one transition applies to a given state. *)

Inductive dtransition: state -> state -> Prop :=
  | dtr_nop: forall r mu tau,
      dtransition (State ((r, r) :: mu) nil tau)
                  (State mu nil tau)
  | dtr_start: forall s d mu tau,
      s <> d ->
      dtransition (State ((s, d) :: mu) nil tau)
                  (State mu ((s, d) :: nil) tau)
  | dtr_push: forall mu1 d r mu2 s sigma tau,
      no_read mu1 d ->
      dtransition (State (mu1 ++ (d, r) :: mu2) ((s, d) :: sigma) tau)
                  (State (mu1 ++ mu2) ((d, r) :: (s, d) :: sigma) tau)
  | dtr_loop_pop: forall mu s r0 d  sigma tau,
      no_read mu r0 ->
      dtransition (State mu ((s, r0) :: sigma ++ (r0, d) :: nil) tau)
                  (State mu (sigma ++ (temp r0, d) :: nil) ((s, r0) :: (r0, temp r0) :: tau))
  | dtr_pop: forall mu s0 d0 s1 d1 sigma tau,
      no_read mu d1 -> d1 <> s0 ->
      dtransition (State mu ((s1, d1) :: sigma ++ (s0, d0) :: nil) tau)
                  (State mu (sigma ++ (s0, d0) :: nil) ((s1, d1) :: tau))
  | dtr_last: forall mu s d tau,
      no_read mu d ->
      dtransition (State mu ((s, d) :: nil) tau)
                  (State mu nil ((s, d) :: tau)).

Definition dtransitions: state -> state -> Prop :=
  clos_refl_trans state dtransition.

(** Every deterministic transition corresponds to a sequence of
  non-deterministic transitions. *)

Lemma transition_determ:
  forall st st',
  dtransition st st' ->
  state_wf st ->
  transitions st st'.
Proof.
  induction 1; intro; unfold transitions.
  apply rt_step. exact (tr_nop nil r mu nil tau).
  apply rt_step. exact (tr_start nil s d mu tau).
  apply rt_step. apply tr_push.
  eapply rt_trans.
    apply rt_step.
    change ((s, r0) :: sigma ++ (r0, d) :: nil)
      with (((s, r0) :: sigma) ++ (r0, d) :: nil).
    apply tr_loop.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    apply rt_step. simpl. apply tr_pop. auto.
    inv H0.  generalize H6.
    change ((s, r0) :: sigma ++ (r0, d) :: nil)
      with (((s, r0) :: sigma) ++ (r0, d) :: nil).
    unfold temp_last; rewrite List.rev_unit. intros [E F].
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    elim (F s r0). unfold is_not_temp. auto.
(* Goal: @In (prod reg reg) (@pair reg reg s d) (@rev (prod reg reg) m) *)
    rewrite <- List.In_rev. apply in_eq.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply rt_step. apply tr_pop. auto. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply rt_step. apply tr_last. auto.
Qed.

Lemma transitions_determ:
  forall st st',
  dtransitions st st' ->
  state_wf st ->
  transitions st st'.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold transitions; induction 1; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply transition_determ; eauto.
(* Goal: clos_refl_trans state transition x x *)
(* Goal: clos_refl_trans state transition x z *)
  apply rt_refl.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply rt_trans with y. auto.
(* Goal: clos_refl_trans state transition y z *)
    apply IHclos_refl_trans2.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    apply transitions_preserve_wf with x; auto. red; auto.
Qed.

(** The semantic correctness of deterministic transitions follows. *)

Theorem dtransitions_correctness:
  forall mu tau,
  move_no_temp mu ->
  is_mill mu ->
  dtransitions (State mu nil nil) (State nil nil tau) ->
  forall e, env_equiv (exec_seq (List.rev tau) e) (exec_par mu e).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply transitions_correctness; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply transitions_determ. auto. apply state_wf_start; auto.
Qed.

(** * The compilation function *)

(** We now define a function that takes a well-formed parallel move [mu]
  and returns a sequence of elementary moves [tau] that is semantically
  equivalent.  We start by defining a number of auxiliary functions. *)

Fixpoint split_move (m: moves) (r: reg) {struct m} : option (moves * reg * moves) :=
  match m with
  | nil => None
  | (s, d) :: tl =>
      if reg_eq s r
      then Some (nil, d, tl)
      else match split_move tl r with
           | None => None
           | Some (before, d', after) => Some ((s, d) :: before, d', after)
           end
  end.

Fixpoint is_last_source (r: reg) (m: moves) {struct m} : bool :=
  match m with
  | nil => false
  | (s, d) :: nil =>
      if reg_eq s r then true else false
  | (s, d) :: tl =>
      is_last_source r tl
  end.

Fixpoint replace_last_source (r: reg) (m: moves) {struct m} : moves :=
  match m with
  | nil => nil
  | (s, d) :: nil => (r, d) :: nil
  | s_d :: tl => s_d :: replace_last_source r tl
  end.

Definition final_state (st: state) : bool :=
  match st with
  | State nil nil _ => true
  | _ => false
  end.

Definition parmove_step (st: state) : state :=
  match st with
  | State nil nil _ => st
  | State ((s, d) :: tl) nil l =>
      if reg_eq s d
      then State tl nil l
      else State tl ((s, d) :: nil) l
  | State t ((s, d) :: b) l =>
      match split_move t d with
      | Some (t1, r, t2) =>
          State (t1 ++ t2) ((d, r) :: (s, d) :: b) l
      | None =>
          match b with
          | nil => State t nil ((s, d) :: l)
          | _ =>
              if is_last_source d b
              then State t (replace_last_source (temp d) b) ((s, d) :: (d, temp d) :: l)
              else State t b ((s, d) :: l)
          end
      end
  end.

(** Here are the main correctness properties of these functions. *)

Lemma split_move_charact:
  forall m r,
  match split_move m r with
  | Some (before, d, after) => m = before ++ (r, d) :: after /\ no_read before r
  | None => no_read m r
  end.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold no_read. induction m; simpl; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
- tauto.
(* Goal: env_match (let (s, d) := a in exec_seq m (update d (e1 s) e1)) (let (s, d) := a in weak_exec_seq m (weak_update d (e2 s) e2)) *)
- destruct a as [s d]. destruct (reg_eq s r).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  + subst s. auto.
  + specialize (IHm r). destruct (split_move m r) as [[[before d'] after] | ].
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
    * destruct IHm. subst m. simpl. intuition.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
    * simpl; intuition.
Qed.

Lemma is_last_source_charact:
  forall r s d m,
  if is_last_source r (m ++ (s, d) :: nil)
  then s = r
  else s <> r.
Proof.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  induction m; simpl.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  destruct (reg_eq s r); congruence.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct a as [s0 d0]. case_eq (m ++ (s, d) :: nil); intros.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  generalize (app_cons_not_nil m nil (s, d)). congruence.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite <- H. auto.
Qed.

Lemma replace_last_source_charact:
  forall s d s' m,
  replace_last_source s' (m ++ (s, d) :: nil) =
  m ++ (s', d) :: nil.
Proof.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  induction m; simpl.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct a as [s0 d0]. case_eq (m ++ (s, d) :: nil); intros.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  generalize (app_cons_not_nil m nil (s, d)). congruence.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  rewrite <- H. congruence.
Qed.

Lemma parmove_step_compatible:
  forall st,
  final_state st = false ->
  dtransition st (parmove_step st).
Proof.
  intros st NOTFINAL. destruct st as [mu sigma tau]. unfold parmove_step.
  case_eq mu; [intros MEQ | intros [ms md] mtl MEQ].
  case_eq sigma; [intros SEQ | intros [ss sd] stl SEQ].
(* Goal: and (@eq (list reg) (@nil reg) (@cons reg a s)) (@eq (list reg) (@nil reg) (@nil reg)) *)
(* Goal: and (@eq (list reg) (@cons reg a (srcs (@combine reg reg s d))) (@cons reg a s)) (@eq (list reg) (@cons reg r (dests (@combine reg reg s d))) (@cons reg r d)) *)
  subst mu sigma. simpl in NOTFINAL. discriminate.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  simpl.
  case_eq stl; [intros STLEQ | intros xx1 xx2 STLEQ].
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply dtr_last. red; simpl; auto.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  elim (@exists_last _ stl). 2:congruence. intros sigma1 [[ss1 sd1] SEQ2].
  rewrite <- STLEQ. clear STLEQ xx1 xx2.
  generalize (is_last_source_charact sd ss1 sd1 sigma1).
  rewrite SEQ2. destruct (is_last_source sd (sigma1 ++ (ss1, sd1) :: nil)).
(* Goal: no_read (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) d1 *)
  intro. subst ss1.
  rewrite replace_last_source_charact. apply dtr_loop_pop.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red; simpl; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intro. apply dtr_pop. red; simpl; auto. auto.

  case_eq sigma; [intros SEQ | intros [ss sd] stl SEQ].
  destruct (reg_eq ms md).
(* Goal: @eq val (update d (e1 s) e1 r) (weak_update d (e2 s) e2 r) *)
(* Goal: @eq val (update d (e1 s) e1 r) (weak_update d (e2 s) e2 r) *)
  subst. apply dtr_nop.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply dtr_start. auto.

  generalize (split_move_charact ((ms, md) :: mtl) sd).
  case (split_move ((ms, md) :: mtl) sd); [intros [[before r] after] | idtac].
  intros [MEQ2 NOREAD].
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite MEQ2. apply dtr_push. auto.
  intro NOREAD.
  case_eq stl; [intros STLEQ | intros xx1 xx2 STLEQ].
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply dtr_last. auto.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  elim (@exists_last _ stl). 2:congruence. intros sigma1 [[ss1 sd1] SEQ2].
  rewrite <- STLEQ. clear STLEQ xx1 xx2.
  generalize (is_last_source_charact sd ss1 sd1 sigma1).
  rewrite SEQ2. destruct (is_last_source sd (sigma1 ++ (ss1, sd1) :: nil)).
(* Goal: no_read (@app (prod reg reg) sigma (@cons (prod reg reg) (@pair reg reg s0 d0) (@nil (prod reg reg)))) d1 *)
  intro. subst ss1.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite replace_last_source_charact. apply dtr_loop_pop. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intro. apply dtr_pop. auto. auto.
Qed.

(** The compilation function is obtained by iterating the [parmov_step]
  function.  To show that this iteration always terminates, we introduce
  the following measure over states. *)

Open Scope nat_scope.

Definition measure (st: state) : nat :=
  match st with
  | State mu sigma tau => 2 * List.length mu + List.length sigma
  end.

Lemma measure_decreasing_1:
  forall st st',
  dtransition st st' -> measure st' < measure st.
Proof.
(* Goal: forall (st st' : state) (_ : dtransition st st'), lt (measure st') (measure st) *)
  induction 1; repeat (simpl; rewrite List.app_length); simpl; omega.
Qed.

Lemma measure_decreasing_2:
  forall st,
  final_state st = false ->
  measure (parmove_step st) < measure st.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. apply measure_decreasing_1. apply parmove_step_compatible; auto.
Qed.

(** Compilation function for parallel moves. *)

Function parmove_aux (st: state) {measure measure st} : moves :=
  if final_state st
  then match st with State _ _ tau => tau end
  else parmove_aux (parmove_step st).
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. apply measure_decreasing_2. auto.
Qed.

Lemma parmove_aux_transitions:
  forall st,
  dtransitions st (State nil nil (parmove_aux st)).
Proof.
  unfold dtransitions. intro st. functional induction (parmove_aux st).
(* Goal: clos_refl_trans state transition x x *)
(* Goal: clos_refl_trans state transition x z *)
  destruct _x; destruct _x0; simpl in e; discriminate || apply rt_refl.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply rt_trans. apply rt_step. apply parmove_step_compatible; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
Qed.

Definition parmove (mu: moves) : moves :=
  List.rev (parmove_aux (State mu nil nil)).

(** This is the main correctness theorem: the sequence of elementary
  moves [parmove mu] is semantically equivalent to the parallel move
  [mu] if the latter is well-formed. *)

Theorem parmove_correctness:
  forall mu,
  move_no_temp mu -> is_mill mu ->
  forall e,
  env_equiv (exec_seq (parmove mu) e) (exec_par mu e).
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. unfold parmove. apply dtransitions_correctness; auto.
(* Goal: dtransitions (State mu (@nil (prod reg reg)) (@nil (prod reg reg))) (State (@nil (prod reg reg)) (@nil (prod reg reg)) (parmove_aux (State mu (@nil (prod reg reg)) (@nil (prod reg reg))))) *)
  apply parmove_aux_transitions.
Qed.

(** Here is an alternate formulation of [parmove], where the
  parallel move problem is given as two separate lists of sources
  and destinations. *)

Definition parmove2 (sl dl: list reg) : moves :=
  parmove (List.combine sl dl).

Theorem parmove2_correctness:
  forall sl dl,
  List.length sl = List.length dl ->
  list_norepet dl ->
  (forall r, In r sl -> is_not_temp r) ->
  (forall r, In r dl -> is_not_temp r) ->
  forall e,
  let e' := exec_seq (parmove2 sl dl) e in
  List.map e' dl = List.map e sl /\
  forall r, ~In r dl -> is_not_temp r -> e' r = e r.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros.
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : not (@In reg r dl)) (_ : is_not_temp r), @eq val (e' r) (e r)) *)
  destruct (srcs_dests_combine sl dl H) as [A B].
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : not (@In reg r dl)) (_ : is_not_temp r), @eq val (e' r) (e r)) *)
  assert (env_equiv e' (exec_par (List.combine sl dl) e)).
(* Goal: env_equiv e' (exec_par (@combine reg reg sl dl) e) *)
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : not (@In reg r dl)) (_ : is_not_temp r), @eq val (e' r) (e r)) *)
    unfold e', parmove2. apply parmove_correctness.
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
    red; intros; split.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    apply H1. eapply List.in_combine_l; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    apply H2. eapply List.in_combine_r; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    red. rewrite B. auto.
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : not (@In reg r dl)) (_ : is_not_temp r), @eq val (e' r) (e r)) *)
  destruct (exec_par_combine e sl dl H H0) as [C D].
  set (e1 := exec_par (combine sl dl) e) in *.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  split. rewrite <- C. apply list_map_exten; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  symmetry. apply H3. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. transitivity (e1 r); auto.
Qed.

(** * Additional syntactic properties *)

(** We now show an additional property of the sequence of moves
  generated by [parmove mu]: any such move [s -> d] is either
  already present in [mu], or of the form [temp s -> d] or [s -> temp s]
  for some [s -> d] in [mu].  This syntactic property is useful
  to show that [parmove] preserves register classes, for instance. *)

Section PROPERTIES.

Variable initial_move: moves.

Inductive wf_move: reg -> reg -> Prop :=
  | wf_move_same: forall s d,
      In (s, d) initial_move -> wf_move s d
  | wf_move_temp_left: forall s d,
      wf_move s d -> wf_move (temp s) d
  | wf_move_temp_right: forall s d,
      wf_move s d -> wf_move s (temp s).

Definition wf_moves (m: moves) : Prop :=
  forall s d, In (s, d) m -> wf_move s d.

Lemma wf_moves_cons: forall s d m,
  wf_moves ((s, d) :: m) <-> wf_move s d /\ wf_moves m.
Proof.
(* Goal: iff (forall (s0 d0 : reg) (_ : or (@eq (prod reg reg) (@pair reg reg s d) (@pair reg reg s0 d0)) (@In (prod reg reg) (@pair reg reg s0 d0) m)), wf_move s0 d0) (and (wf_move s d) (forall (s d : reg) (_ : @In (prod reg reg) (@pair reg reg s d) m), wf_move s d)) *)
  unfold wf_moves; intros; simpl. firstorder.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  inversion H0; subst s0 d0. auto.
Qed.

Lemma wf_moves_append: forall m1 m2,
  wf_moves (m1 ++ m2) <-> wf_moves m1 /\ wf_moves m2.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold wf_moves; intros. split; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  split; intros; apply H; apply in_or_app; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct H. elim (in_app_or _ _ _ H0); intro; auto.
Qed.

Hint Rewrite wf_moves_cons wf_moves_append: pmov.

Inductive wf_state: state -> Prop :=
  | wf_state_intro: forall mu sigma tau,
      wf_moves mu -> wf_moves sigma -> wf_moves tau ->
      wf_state (State mu sigma tau).

Lemma dtransition_preserves_wf_state:
  forall st st',
  dtransition st st' -> wf_state st -> wf_state st'.
Proof.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after)))) (not (or (@eq reg s r) (@In reg r (srcs before)))) *)
  induction 1; intro WF; inv WF; constructor; autorewrite with pmov in *; intuition.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply wf_move_temp_left; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply wf_move_temp_right; eauto.
Qed.

Lemma dtransitions_preserve_wf_state:
  forall st st',
  dtransitions st st' -> wf_state st -> wf_state st'.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  induction 1; intros; eauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  eapply dtransition_preserves_wf_state; eauto.
Qed.

End PROPERTIES.

Lemma parmove_wf_moves:
  forall mu, wf_moves mu (parmove mu).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros.
(* Goal: wf_moves mu (parmove mu) *)
  assert (wf_state mu (State mu nil nil)).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    constructor. red; intros. apply wf_move_same. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    red; simpl; tauto. red; simpl; tauto.
  generalize (dtransitions_preserve_wf_state mu
              _ _
              (parmove_aux_transitions (State mu nil nil)) H).
(* Goal: forall _ : wf_state mu (State (@nil (prod reg reg)) (@nil (prod reg reg)) (parmove_aux (State mu (@nil (prod reg reg)) (@nil (prod reg reg))))), wf_moves mu (parmove mu) *)
  intro WFS. inv WFS.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold parmove. red; intros. apply H5.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite List.In_rev. auto.
Qed.

Lemma parmove2_wf_moves:
  forall sl dl, wf_moves (List.combine sl dl) (parmove2 sl dl).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. unfold parmove2. apply parmove_wf_moves.
Qed.

(** As a corollary, we show that all sources of [parmove mu]
    are sources of [mu] or temporaries,
    and likewise all destinations of [parmove mu] are destinations of [mu]
    or temporaries. *)

Remark wf_move_initial_reg_or_temp:
  forall mu s d,
  wf_move mu s d ->
  (In s (srcs mu) \/ is_temp s) /\ (In d (dests mu) \/ is_temp d).
Proof.
(* Goal: forall (l : list reg) (_ : pairwise_disjoint l), @list_norepet reg l *)
  induction 1.
(* Goal: and (or (@In reg s (srcs mu)) (is_temp s)) (or (@In reg d (dests mu)) (is_temp d)) *)
(* Goal: and (or (@In reg (temp s) (srcs mu)) (is_temp (temp s))) (or (@In reg d (dests mu)) (is_temp d)) *)
(* Goal: and (or (@In reg s (srcs mu)) (is_temp s)) (or (@In reg (temp s) (dests mu)) (is_temp (temp s))) *)
  split; left.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  change s with (fst (s, d)). unfold srcs. apply List.in_map; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  change d with (snd (s, d)). unfold dests. apply List.in_map; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  split. right. exists s; auto. tauto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  split. tauto. right. exists s; auto.
Qed.

Lemma parmove_initial_reg_or_temp:
  forall mu s d,
  In (s, d) (parmove mu) ->
  (In s (srcs mu) \/ is_temp s) /\ (In d (dests mu) \/ is_temp d).
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. apply wf_move_initial_reg_or_temp. apply parmove_wf_moves. auto.
Qed.

Remark in_srcs:
  forall mu s, In s (srcs mu) -> exists d, In (s, d) mu.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. destruct (list_in_map_inv (@fst reg reg) _ _ H) as [[s' d'] [A B]].
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  simpl in A. exists d'; congruence.
Qed.

Remark in_dests:
  forall mu d, In d (dests mu) -> exists s, In (s, d) mu.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. destruct (list_in_map_inv (@snd reg reg) _ _ H) as [[s' d'] [A B]].
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  simpl in A. exists s'; congruence.
Qed.

Lemma parmove_srcs_initial_reg_or_temp:
  forall mu s,
  In s (srcs (parmove mu)) -> In s (srcs mu) \/ is_temp s.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. destruct (in_srcs _ _ H) as [d A].
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct (parmove_initial_reg_or_temp _ _ _ A). auto.
Qed.

Lemma parmove_dests_initial_reg_or_temp:
  forall mu d,
  In d (dests (parmove mu)) -> In d (dests mu) \/ is_temp d.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. destruct (in_dests _ _ H) as [s A].
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct (parmove_initial_reg_or_temp _ _ _ A). auto.
Qed.

(** As a second corollary, we show that [parmov] preserves register
    classes, in the sense made precise below. *)

Section REGISTER_CLASSES.

Variable class: Type.
Variable regclass: reg -> class.
Hypothesis temp_preserves_class: forall r, regclass (temp r) = regclass r.

Definition is_class_compatible (mu: moves) : Prop :=
  forall s d, In (s, d) mu -> regclass s = regclass d.

Lemma parmove_preserves_register_classes:
  forall mu,
  is_class_compatible mu ->
  is_class_compatible (parmove mu).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros.
  assert (forall s d, wf_move mu s d -> regclass s = regclass d).
(* Goal: forall (l : list reg) (_ : pairwise_disjoint l), @list_norepet reg l *)
    induction 1.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    apply H; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    rewrite temp_preserves_class. auto.
(* Goal: @eq class (regclass s) (regclass (temp s)) *)
(* Goal: is_class_compatible (parmove mu) *)
    symmetry; apply temp_preserves_class.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red; intros. apply H0. apply parmove_wf_moves. auto.
Qed.

End REGISTER_CLASSES.

(** * Extension to partially overlapping registers *)

(** We now extend the previous results to the case where distinct
  registers can partially overlap, so that assigning to one register
  changes the value of the other.  We asuume given a disjointness relation
  [disjoint] between registers. *)

Variable disjoint: reg -> reg -> Prop.

Hypothesis disjoint_sym:
  forall r1 r2, disjoint r1 r2 -> disjoint r2 r1.

Hypothesis disjoint_not_equal:
  forall r1 r2, disjoint r1 r2 -> r1 <> r2.

(** Two registers partially overlap if they are different and not disjoint.
    For the Coq development, it is easier to define the complement:
    two registers do not partially overlap if they are identical or disjoint. *)

Definition no_overlap (r1 r2: reg) : Prop :=
  r1 = r2 \/ disjoint r1 r2.

Lemma no_overlap_sym:
  forall r1 r2, no_overlap r1 r2 -> no_overlap r2 r1.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. destruct H. left; auto. right; auto.
Qed.

(** We axiomatize the effect of assigning a value to a register over an
  execution environment.  The target register is set to the given value
  (property [weak_update_s]), and registers disjoint from the target
  keep their previous values (property [weak_update_d]).  The values of
  other registers are undefined after the assignment. *)

Variable weak_update: reg -> val -> env -> env.

Hypothesis weak_update_s:
  forall r v e, weak_update r v e r = v.

Hypothesis weak_update_d:
  forall r1 v e r2, disjoint r1 r2 -> weak_update r1 v e r2 = e r2.

Fixpoint weak_exec_seq (m: moves) (e: env) {struct m}: env :=
  match m with
  | nil => e
  | (s, d) :: m' => weak_exec_seq m' (weak_update d (e s) e)
  end.

Definition disjoint_list (r: reg) (l: list reg) : Prop :=
  forall r', In r' l -> disjoint r r'.

Inductive pairwise_disjoint: list reg -> Prop :=
  | pairwise_disjoint_nil:
      pairwise_disjoint nil
  | pairwise_disjoint_cons: forall r l,
      disjoint_list r l ->
      pairwise_disjoint l ->
      pairwise_disjoint (r :: l).

Definition disjoint_temps (r: reg) : Prop :=
  forall t, is_temp t -> disjoint r t.

Section OVERLAP.

(** We consider a parallel move problem [mu] that satisfies the following
  conditions, which are stronger, overlap-aware variants of the
  [move_no_temp mu] and [is_mill mu] conditions used previously. *)

Variable mu: moves.

(** Sources and destinations are disjoint from all temporary registers. *)

Hypothesis mu_no_temporaries_src:
  forall s, In s (srcs mu) -> disjoint_temps s.

Hypothesis mu_no_temporaries_dst:
  forall d, In d (dests mu) -> disjoint_temps d.

(** Destinations are pairwise disjoint. *)

Hypothesis mu_dest_pairwise_disjoint:
  pairwise_disjoint (dests mu).

(** Sources and destinations do not partially overlap. *)

Hypothesis mu_src_dst_no_overlap:
  forall s d, In s (srcs mu) -> In d (dests mu) -> no_overlap s d.

(** Distinct temporaries do not partially overlap. *)

Hypothesis temps_no_overlap:
  forall t1 t2, is_temp t1 -> is_temp t2 -> no_overlap t1 t2.

(** The following lemmas show that [mu] is a windmill and does not
  contain temporary registers. *)

Lemma disjoint_list_notin:
  forall r l, disjoint_list r l -> ~In r l.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. red; intro.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  assert (r <> r). apply disjoint_not_equal. apply H; auto.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  congruence.
Qed.

Lemma pairwise_disjoint_norepet:
  forall l, pairwise_disjoint l -> list_norepet l.
Proof.
(* Goal: forall (l : list reg) (_ : pairwise_disjoint l), @list_norepet reg l *)
  induction 1.
(* Goal: @list_norepet reg (@cons reg r l) *)
  constructor.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  constructor. apply disjoint_list_notin; auto. auto.
Qed.

Lemma disjoint_temps_not_temp:
  forall r, disjoint_temps r -> is_not_temp r.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  intros; red; intros. apply disjoint_not_equal. apply H. exists d; auto.
Qed.

Lemma mu_is_mill:
  is_mill mu.
Proof.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  red. apply pairwise_disjoint_norepet. auto.
Qed.

Lemma mu_move_no_temp:
  move_no_temp mu.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  red; intros.
(* Goal: and (is_not_temp s) (is_not_temp d) *)
  split; apply disjoint_temps_not_temp.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply mu_no_temporaries_src; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold srcs. change s with (fst (s,d)). apply List.in_map; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply mu_no_temporaries_dst; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  unfold dests. change d with (snd (s,d)). apply List.in_map; auto.
Qed.

(** We define the ``adherence'' of the problem [mu] as the set of
  registers that partially overlap with one of the registers
  possibly assigned by the parallel move: destinations and temporaries.
  Again, we define the complement of the ``adherence'' set, which is
  more convenient for Coq reasoning. *)

Definition no_adherence (r: reg) : Prop :=
  forall x, In x (dests mu) \/ is_temp x -> no_overlap x r.

(** As a consequence of the hypotheses on [mu], none of the destination
  registers, source registers, and temporaries belong to the adherence. *)

Lemma no_overlap_pairwise:
  forall r1 r2 m, pairwise_disjoint m -> In r1 m -> In r2 m -> no_overlap r1 r2.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  induction 1; intros.
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  elim H.
  simpl in *. destruct H1; destruct H2.
(* Goal: @eq reg r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
(* Goal: no_overlap r1 r2 *)
  left. congruence.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  subst. right. apply H. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  subst. right. apply disjoint_sym. apply H. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
Qed.

Lemma no_adherence_dst:
  forall d, In d (dests mu) -> no_adherence d.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros; red; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct H0. apply no_overlap_pairwise with (dests mu); auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  right. apply disjoint_sym. apply mu_no_temporaries_dst; auto.
Qed.

Lemma no_adherence_src:
  forall s, In s (srcs mu) -> no_adherence s.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros; red; intros.
(* Goal: no_overlap x t *)
  destruct H0.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply no_overlap_sym. apply mu_src_dst_no_overlap; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  right. apply disjoint_sym. apply mu_no_temporaries_src; auto.
Qed.

Lemma no_adherence_tmp:
  forall t, is_temp t -> no_adherence t.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros; red; intros.
(* Goal: no_overlap x t *)
  destruct H0.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  right. apply mu_no_temporaries_dst; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply temps_no_overlap; auto.
Qed.

(** The relation [env_match] holds between two environments [e1] and [e2]
    if they assign the same values to all registers not in the adherence set. *)

Definition env_match (e1 e2: env) : Prop :=
  forall r, no_adherence r -> e1 r = e2 r.

(** The following lemmas relate the effect of executing moves
  using normal, overlap-unaware update and weak, overlap-aware update. *)

Lemma weak_update_match:
  forall e1 e2 s d,
  (In s (srcs mu) \/ is_temp s) ->
  (In d (dests mu) \/ is_temp d) ->
  env_match e1 e2 ->
  env_match (update d (e1 s) e1)
            (weak_update d (e2 s) e2).
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. red; intros.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  assert (no_overlap d r). apply H2. auto.
(* Goal: @eq val (update d (e1 s) e1 r) (weak_update d (e2 s) e2 r) *)
  destruct H3.
(* Goal: @eq val (e1 r) (e2 r) *)
(* Goal: disjoint d r *)
(* Goal: not (@eq reg r d) *)
  subst. rewrite update_s. rewrite weak_update_s. apply H1.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  destruct H. apply no_adherence_src; auto. apply no_adherence_tmp; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  rewrite update_o. rewrite weak_update_d. apply H1. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto. apply not_eq_sym. apply disjoint_not_equal. auto.
Qed.

Lemma weak_exec_seq_match:
  forall m e1 e2,
  (forall s, In s (srcs m) -> In s (srcs mu) \/ is_temp s) ->
  (forall d, In d (dests m) -> In d (dests mu) \/ is_temp d) ->
  env_match e1 e2 ->
  env_match (exec_seq m e1) (weak_exec_seq m e2).
Proof.
(* Goal: and (@eq moves (@cons (prod reg reg) (@pair reg reg s d) (@app (prod reg reg) before (@cons (prod reg reg) (@pair reg reg r d') after))) (@app (prod reg reg) (@cons (prod reg reg) (@pair reg reg s d) before) (@cons (prod reg reg) (@pair reg reg r d') after))) (not (@In reg r (srcs (@cons (prod reg reg) (@pair reg reg s d) before)))) *)
  induction m; intros; simpl.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  auto.
(* Goal: env_match (exec_seq m (update d (e1 s) e1)) (weak_exec_seq m (weak_update d (e2 s) e2)) *)
  destruct a as [s d]. simpl in H. simpl in H0.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply IHm; auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply weak_update_match; auto.
Qed.

End OVERLAP.

(** These lemmas imply the following correctness theorem for the [parmove2]
  function, taking partial register overlap into account. *)

Theorem parmove2_correctness_with_overlap:
  forall sl dl,
  List.length sl = List.length dl ->
  (forall r, In r sl -> disjoint_temps r) ->
  (forall r, In r dl -> disjoint_temps r) ->
  pairwise_disjoint dl ->
  (forall s d, In s sl -> In d dl -> no_overlap s d) ->
  (forall t1 t2, is_temp t1 -> is_temp t2 -> no_overlap t1 t2) ->
  forall e,
  let e' := weak_exec_seq (parmove2 sl dl) e in
  List.map e' dl = List.map e sl /\
  forall r, disjoint_list r dl -> disjoint_temps r -> e' r = e r.
Proof.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros.
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
  assert (list_norepet dl).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    apply pairwise_disjoint_norepet; auto.
  assert (forall r : reg, In r sl -> is_not_temp r).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    intros. apply disjoint_temps_not_temp; auto.
  assert (forall r : reg, In r dl -> is_not_temp r).
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    intros. apply disjoint_temps_not_temp; auto.
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
  generalize (parmove2_correctness sl dl H H5 H6 H7 e).
(* Goal: forall _ : let e' := exec_seq (parmove2 sl dl) e in and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : not (@In reg r dl)) (_ : is_not_temp r), @eq val (e' r) (e r)), and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
  set (e1 := exec_seq (parmove2 sl dl) e). intros [A B].
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
  destruct (srcs_dests_combine sl dl H) as [C D].
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
  assert (env_match (combine sl dl) e1 e').
(* Goal: env_match (@combine reg reg sl dl) e1 e' *)
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
    unfold parmove2. unfold e1, e'.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    apply weak_exec_seq_match; try (rewrite C); try (rewrite D); auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    intros. rewrite <- C. apply parmove_srcs_initial_reg_or_temp. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    intros. rewrite <- D. apply parmove_dests_initial_reg_or_temp. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
    red; auto.
(* Goal: and (@eq (list val) (@map reg val e' dl) (@map reg val e sl)) (forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r)) *)
  split.
(* Goal: @eq (list val) (@map reg val e' dl) (@map reg val e sl) *)
(* Goal: forall (r : reg) (_ : disjoint_list r dl) (_ : disjoint_temps r), @eq val (e' r) (e r) *)
  rewrite <- A.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  apply list_map_exten; intros. apply H8.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply no_adherence_dst. rewrite D; auto. rewrite D; auto. rewrite D; auto.
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  intros. transitivity (e1 r).
(* Goal: forall (x : reg) (_ : or (@In reg x dl) (is_temp x)), no_overlap x r *)
(* Goal: @eq val (e1 r) (e r) *)
  symmetry. apply H8. red. rewrite D. intros. destruct H11.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  right. apply disjoint_sym. apply H9. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  right. apply disjoint_sym. apply H10. auto.
(* Goal: is_temp x *)
(* Goal: @eq val (e1 r) (e r) *)
  apply B. apply disjoint_list_notin; auto. apply disjoint_temps_not_temp; auto.
Qed.

End PARMOV.
