(* *********************************************************************)
(*                                                                     *)
(*              The Compcert verified compiler                         *)
(*                                                                     *)
(*          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.     *)
(*                                                                     *)
(* *********************************************************************)

(** Whole-program behaviors *)

Require Import Classical.
Require Import ClassicalEpsilon.
Require Import Coqlib.
Require Import Events.
Require Import Globalenvs.
Require Import Integers.
Require Import Smallstep.

Set Implicit Arguments.

(** * Behaviors for program executions *)

(** The four possible outcomes for the execution of a program:
- Termination, with a finite trace of observable events
  and an integer value that stands for the process exit code
  (the return value of the main function).
- Divergence with a finite trace of observable events.
  (At some point, the program runs forever without doing any I/O.)
- Reactive divergence with an infinite trace of observable events.
  (The program performs infinitely many I/O operations separated
   by finite amounts of internal computations.)
- Going wrong, with a finite trace of observable events
  performed before the program gets stuck.
*)

Inductive program_behavior: Type :=
  | Terminates: trace -> int -> program_behavior
  | Diverges: trace -> program_behavior
  | Reacts: traceinf -> program_behavior
  | Goes_wrong: trace -> program_behavior.

(** Operations and relations on behaviors *)

Definition not_wrong (beh: program_behavior) : Prop :=
  match beh with
  | Terminates _ _ => True
  | Diverges _ => True
  | Reacts _ => True
  | Goes_wrong _ => False
  end.

Definition behavior_app (t: trace) (beh: program_behavior): program_behavior :=
  match beh with
  | Terminates t1 r => Terminates (t ** t1) r
  | Diverges t1 => Diverges (t ** t1)
  | Reacts T => Reacts (t *** T)
  | Goes_wrong t1 => Goes_wrong (t ** t1)
  end.

Lemma behavior_app_assoc:
  forall t1 t2 beh,
  behavior_app (t1 ** t2) beh = behavior_app t1 (behavior_app t2 beh).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. destruct beh; simpl; f_equal; traceEq.
Qed.

Lemma behavior_app_E0:
  forall beh, behavior_app E0 beh = beh.
Proof.
(* Goal: forall beh : program_behavior, @eq program_behavior (behavior_app E0 beh) beh *)
  destruct beh; auto.
Qed.

Definition behavior_prefix (t: trace) (beh: program_behavior) : Prop :=
  exists beh', beh = behavior_app t beh'.

Definition behavior_improves (beh1 beh2: program_behavior) : Prop :=
  beh1 = beh2 \/ exists t, beh1 = Goes_wrong t /\ behavior_prefix t beh2.

Lemma behavior_improves_refl:
  forall beh, behavior_improves beh beh.
Proof.
(* Goal: forall beh : program_behavior, behavior_improves beh beh *)
  intros; red; auto.
Qed.

Lemma behavior_improves_trans:
  forall beh1 beh2 beh3,
  behavior_improves beh1 beh2 -> behavior_improves beh2 beh3 ->
  behavior_improves beh1 beh3.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. red. destruct H; destruct H0; subst; auto.
(* Goal: or (@eq program_behavior beh1 beh3) (@ex trace (fun t : trace => and (@eq program_behavior beh1 (Goes_wrong t)) (behavior_prefix t beh3))) *)
  destruct H as [t1 [EQ1 [beh2' EQ1']]].
(* Goal: or (@eq program_behavior beh1 beh3) (@ex trace (fun t : trace => and (@eq program_behavior beh1 (Goes_wrong t)) (behavior_prefix t beh3))) *)
  destruct H0 as [t2 [EQ2 [beh3' EQ2']]].
(* Goal: program_behaves L1 beh *)
  subst. destruct beh2'; simpl in EQ2; try discriminate. inv EQ2.
(* Goal: or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  right. exists t1; split; auto. exists (behavior_app t beh3'). apply behavior_app_assoc.
Qed.

Lemma behavior_improves_bot:
  forall beh, behavior_improves (Goes_wrong E0) beh.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. right. exists E0; split; auto. exists beh. rewrite behavior_app_E0; auto.
Qed.

Lemma behavior_improves_app:
  forall t beh1 beh2,
  behavior_improves beh1 beh2 ->
  behavior_improves (behavior_app t beh1) (behavior_app t beh2).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. red; destruct H. left; congruence.
(* Goal: program_behaves L1 beh *)
  destruct H as [t' [A [beh' B]]]. subst.
(* Goal: or (@eq program_behavior (behavior_app t (Goes_wrong t')) (behavior_app t (behavior_app t' beh'))) (@ex trace (fun t0 : trace => and (@eq program_behavior (behavior_app t (Goes_wrong t')) (Goes_wrong t0)) (behavior_prefix t0 (behavior_app t (behavior_app t' beh'))))) *)
  right; exists (t ** t'); split; auto. exists beh'. rewrite behavior_app_assoc; auto.
Qed.

(** Associating behaviors to programs. *)

Section PROGRAM_BEHAVIORS.

Variable L: semantics.

Inductive state_behaves (s: state L): program_behavior -> Prop :=
  | state_terminates: forall t s' r,
      Star L s t s' ->
      final_state L s' r ->
      state_behaves s (Terminates t r)
  | state_diverges: forall t s',
      Star L s t s' -> Forever_silent L s' ->
      state_behaves s (Diverges t)
  | state_reacts: forall T,
      Forever_reactive L s T ->
      state_behaves s (Reacts T)
  | state_goes_wrong: forall t s',
      Star L s t s' ->
      Nostep L s' ->
      (forall r, ~final_state L s' r) ->
      state_behaves s (Goes_wrong t).

Inductive program_behaves: program_behavior -> Prop :=
  | program_runs: forall s beh,
      initial_state L s -> state_behaves s beh ->
      program_behaves beh
  | program_goes_initially_wrong:
      (forall s, ~initial_state L s) ->
      program_behaves (Goes_wrong E0).

Lemma state_behaves_app:
  forall s1 t s2 beh,
  Star L s1 t s2 -> state_behaves s2 beh -> state_behaves s1 (behavior_app t beh).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. inv H0; simpl; econstructor; eauto; try (eapply star_trans; eauto).
(* Goal: @forever_reactive (genvtype L) (state L) (step L) (globalenv L) s1 (Eappinf t T) *)
  eapply star_forever_reactive; eauto.
Qed.

(** * Existence of behaviors *)

(** We now show that any program admits at least one behavior.
  The proof requires classical logic: the axiom of excluded middle
  and an axiom of description. *)

(** The most difficult part of the proof is to show the existence
  of an infinite trace in the case of reactive divergence. *)

Section TRACEINF_REACTS.

Variable s0: state L.

Hypothesis reacts:
  forall s1 t1, Star L s0 t1 s1 ->
  exists s2, exists t2, Star L s1 t2 s2 /\ t2 <> E0.

Lemma reacts':
  forall s1 t1, Star L s0 t1 s1 ->
  { s2 : state L & { t2 : trace | Star L s1 t2 s2 /\ t2 <> E0 } }.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros.
(* Goal: @sigT (state L) (fun s2 : state L => @sig trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
  destruct (constructive_indefinite_description _ (reacts H)) as [s2 A].
(* Goal: @sigT (state L) (fun s2 : state L => @sig trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
  destruct (constructive_indefinite_description _ A) as [t2 [B C]].
(* Goal: @sigT (state L) (fun s2 : state L => @sig trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
  exists s2; exists t2; auto.
Qed.

CoFixpoint build_traceinf' (s1: state L) (t1: trace) (ST: Star L s0 t1 s1) : traceinf' :=
  match reacts' ST with
  | existT s2 (exist t2 (conj A B)) =>
      Econsinf' t2
                (build_traceinf' (star_trans ST A (eq_refl _)))
                B
  end.

Lemma reacts_forever_reactive_rec:
  forall s1 t1 (ST: Star L s0 t1 s1),
  Forever_reactive L s1 (traceinf_of_traceinf' (build_traceinf' ST)).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  cofix COINDHYP; intros.
  rewrite (unroll_traceinf' (build_traceinf' ST)). simpl.
  destruct (reacts' ST) as [s2 [t2 [A B]]].
  rewrite traceinf_traceinf'_app.
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. eexact A. auto. apply COINDHYP.
Qed.

Lemma reacts_forever_reactive:
  exists T, Forever_reactive L s0 T.
Proof.
(* Goal: @ex traceinf (fun T : traceinf => @forever_reactive (genvtype L) (state L) (step L) (globalenv L) s0 T) *)
  exists (traceinf_of_traceinf' (build_traceinf' (star_refl (step L) (globalenv L) s0))).
(* Goal: @forever_reactive (genvtype L) (state L) (step L) (globalenv L) s0 (traceinf_of_traceinf' (@build_traceinf' s0 E0 (@star_refl (genvtype L) (state L) (step L) (globalenv L) s0))) *)
  apply reacts_forever_reactive_rec.
Qed.

End TRACEINF_REACTS.

Lemma diverges_forever_silent:
  forall s0,
  (forall s1 t1, Star L s0 t1 s1 -> exists s2, Step L s1 E0 s2) ->
  Forever_silent L s0.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  cofix COINDHYP; intros.
  destruct (H s0 E0) as [s1 ST]. constructor.
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. eexact ST. apply COINDHYP.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. eapply H. eapply star_left; eauto.
Qed.

Lemma state_behaves_exists:
  forall s, exists beh, state_behaves s beh.
Proof.
(* Goal: forall s : state L, @ex program_behavior (fun beh : program_behavior => state_behaves s beh) *)
  intros s0.
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (classic (forall s1 t1, Star L s0 t1 s1 -> exists s2, exists t2, Step L s1 t2 s2)).
(* 1 Divergence (silent or reactive) *)
  destruct (classic (exists s1, exists t1, Star L s0 t1 s1 /\
                       (forall s2 t2, Star L s1 t2 s2 ->
                        exists s3, Step L s2 E0 s3))).
(* 1.1 Silent divergence *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct H0 as [s1 [t1 [A B]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists (Diverges t1); econstructor; eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply diverges_forever_silent; auto.
(* 1.2 Reactive divergence *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (@reacts_forever_reactive s0) as [T FR].
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros.
(* Goal: @ex (state L) (fun s2 : state L => @ex trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  generalize (not_ex_all_not _ _ H0 s1). intro A; clear H0.
(* Goal: @ex (state L) (fun s2 : state L => @ex trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  generalize (not_ex_all_not _ _ A t1). intro B; clear A.
(* Goal: @ex (state L) (fun s2 : state L => @ex trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (not_and_or _ _ B). contradiction.
(* Goal: @ex (state L) (fun s2 : state L => @ex trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (not_all_ex_not _ _ H0) as [s2 C]; clear H0.
(* Goal: @ex (state L) (fun s2 : state L => @ex trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (not_all_ex_not _ _ C) as [t2 D]; clear C.
(* Goal: @ex (state L) (fun s2 : state L => @ex trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (imply_to_and _ _ D) as [E F]; clear D.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  destruct (H s2 (t1 ** t2)) as [s3 [t3 G]]. eapply star_trans; eauto.
(* Goal: @ex (state L) (fun s2 : state L => @ex trace (fun t2 : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t2 s2) (not (@eq trace t2 E0)))) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  exists s3; exists (t2 ** t3); split.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  eapply star_right; eauto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  red; intros. destruct (app_eq_nil t2 t3 H0). subst. elim F. exists s3; auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists (Reacts T); econstructor; eauto.
(* 2 Termination (normal or by going wrong) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (not_all_ex_not _ _ H) as [s1 A]; clear H.
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (not_all_ex_not _ _ A) as [t1 B]; clear A.
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (imply_to_and _ _ B) as [C D]; clear B.
(* Goal: @ex program_behavior (fun beh : program_behavior => state_behaves s0 beh) *)
  destruct (classic (exists r, final_state L s1 r)) as [[r FINAL] | NOTFINAL].
(* 2.1 Normal termination *)
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists (Terminates t1 r); econstructor; eauto.
(* 2.2 Going wrong *)
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  exists (Goes_wrong t1); econstructor; eauto. red. intros.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  generalize (not_ex_all_not _ _ D s'); intros.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  generalize (not_ex_all_not _ _ H t); intros.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  auto.
Qed.

Theorem program_behaves_exists:
  exists beh, program_behaves beh.
Proof.
(* Goal: @ex program_behavior (fun beh : program_behavior => program_behaves beh) *)
  destruct (classic (exists s, initial_state L s)) as [[s0 INIT] | NOTINIT].
(* 1. Initial state is defined. *)
(* Goal: @ex program_behavior (fun beh : program_behavior => program_behaves beh) *)
(* Goal: @ex program_behavior (fun beh : program_behavior => program_behaves beh) *)
  destruct (state_behaves_exists s0) as [beh SB].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists beh; econstructor; eauto.
(* 2. Initial state is undefined *)
(* Goal: @ex program_behavior (fun beh : program_behavior => program_behaves beh) *)
  exists (Goes_wrong E0). apply program_goes_initially_wrong.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. eapply not_ex_all_not; eauto.
Qed.

End PROGRAM_BEHAVIORS.

(** * Forward simulations and program behaviors *)

Section FORWARD_SIMULATIONS.

Context L1 L2 index order match_states (S: fsim_properties L1 L2 index order match_states).

Lemma forward_simulation_state_behaves:
  forall i s1 s2 beh1,
  match_states i s1 s2 -> state_behaves L1 s1 beh1 ->
  exists beh2, state_behaves L2 s2 beh2 /\ behavior_improves beh1 beh2.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. inv H0.
- (* termination *)
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit simulation_star; eauto. intros [i' [s2' [A B]]].
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (state_behaves L2 s2 beh2) (behavior_improves (Terminates t r) beh2)) *)
  exists (Terminates t r); split.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  econstructor; eauto. eapply fsim_match_final_states; eauto.
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply behavior_improves_refl.
- (* silent divergence *)
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit simulation_star; eauto. intros [i' [s2' [A B]]].
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (state_behaves L2 s2 beh2) (behavior_improves (Diverges t) beh2)) *)
  exists (Diverges t); split.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  econstructor; eauto. eapply simulation_forever_silent; eauto.
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply behavior_improves_refl.
- (* reactive divergence *)
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (state_behaves L2 s2 beh2) (behavior_improves (Reacts T) beh2)) *)
  exists (Reacts T); split.
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. eapply simulation_forever_reactive; eauto.
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply behavior_improves_refl.
- (* going wrong *)
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit simulation_star; eauto. intros [i' [s2' [A B]]].
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (state_behaves L2 s2 beh2) (behavior_improves (Goes_wrong t) beh2)) *)
  destruct (state_behaves_exists L2 s2') as [beh' SB].
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (state_behaves L2 s2 beh2) (behavior_improves (Goes_wrong t) beh2)) *)
  exists (behavior_app t beh'); split.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  eapply state_behaves_app; eauto.
(* Goal: behavior_improves (Goes_wrong t) (behavior_app t beh') *)
  replace (Goes_wrong t) with (behavior_app t (Goes_wrong E0)).
(* Goal: behavior_improves (behavior_app t (Goes_wrong E0)) (behavior_app t beh') *)
(* Goal: @eq program_behavior (behavior_app t (Goes_wrong E0)) (Goes_wrong t) *)
  apply behavior_improves_app. apply behavior_improves_bot.
(* Goal: @eq program_behavior (behavior_app t (Goes_wrong E0)) (Goes_wrong t) *)
  simpl. decEq. traceEq.
Qed.

End FORWARD_SIMULATIONS.

Theorem forward_simulation_behavior_improves:
  forall L1 L2, forward_simulation L1 L2 ->
  forall beh1, program_behaves L1 beh1 ->
  exists beh2, program_behaves L2 beh2 /\ behavior_improves beh1 beh2.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros L1 L2 FS. destruct FS as [init order match_states S]. intros. inv H.
- (* initial state defined *)
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit (fsim_match_initial_states S); eauto. intros [i [s' [INIT MATCH]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit forward_simulation_state_behaves; eauto. intros [beh2 [A B]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists beh2; split; auto. econstructor; eauto.
- (* initial state undefined *)
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (program_behaves L2 beh2) (behavior_improves (Goes_wrong E0) beh2)) *)
  destruct (classic (exists s', initial_state L2 s')).
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (program_behaves L2 beh2) (behavior_improves (Goes_wrong E0) beh2)) *)
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (program_behaves L2 beh2) (behavior_improves (Goes_wrong E0) beh2)) *)
  destruct H as [s' INIT].
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (program_behaves L2 beh2) (behavior_improves (Goes_wrong E0) beh2)) *)
(* Goal: @ex program_behavior (fun beh2 : program_behavior => and (program_behaves L2 beh2) (behavior_improves (Goes_wrong E0) beh2)) *)
  destruct (state_behaves_exists L2 s') as [beh' SB].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists beh'; split. econstructor; eauto. apply behavior_improves_bot.
(* Goal: @ex program_behavior (fun beh1 : program_behavior => and (program_behaves L1 beh1) (behavior_improves beh1 (Goes_wrong E0))) *)
  exists (Goes_wrong E0); split.
(* Goal: program_behaves L1 (Goes_wrong E0) *)
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply program_goes_initially_wrong.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. elim H; exists s; auto.
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply behavior_improves_refl.
Qed.

Corollary forward_simulation_same_safe_behavior:
  forall L1 L2, forward_simulation L1 L2 ->
  forall beh,
  program_behaves L1 beh -> not_wrong beh ->
  program_behaves L2 beh.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. exploit forward_simulation_behavior_improves; eauto.
(* Goal: forall _ : @ex program_behavior (fun beh1 : program_behavior => and (program_behaves L1 beh1) (behavior_improves beh1 beh)), program_behaves L1 beh *)
  intros [beh' [A B]]. destruct B.
(* Goal: program_behaves L1 beh *)
(* Goal: program_behaves L1 beh *)
  congruence.
(* Goal: program_behaves L1 beh *)
  destruct H2 as [t [C D]]. subst. contradiction.
Qed.

(** * Backward simulations and program behaviors *)

Section BACKWARD_SIMULATIONS.

Context L1 L2 index order match_states (S: bsim_properties L1 L2 index order match_states).

Definition safe_along_behavior (s: state L1) (b: program_behavior) : Prop :=
  forall t1 s' b2, Star L1 s t1 s' -> b = behavior_app t1 b2 ->
     (exists r, final_state L1 s' r)
  \/ (exists t2, exists s'', Step L1 s' t2 s'').

Remark safe_along_safe:
  forall s b, safe_along_behavior s b -> safe L1 s.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. eapply H; eauto. symmetry; apply behavior_app_E0.
Qed.

Remark star_safe_along:
  forall s b t1 s' b2,
  safe_along_behavior s b ->
  Star L1 s t1 s' -> b = behavior_app t1 b2 ->
  safe_along_behavior s' b2.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. eapply H. eapply star_trans; eauto.
(* Goal: program_behaves L1 beh *)
  subst. rewrite behavior_app_assoc. eauto.
Qed.

Remark not_safe_along_behavior:
  forall s b,
  ~ safe_along_behavior s b ->
  exists t, exists s',
     behavior_prefix t b
  /\ Star L1 s t s'
  /\ Nostep L1 s'
  /\ (forall r, ~(final_state L1 s' r)).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros.
(* Goal: @ex trace (fun t : trace => @ex (state L1) (fun s' : state L1 => and (behavior_prefix t b) (and (@star (genvtype L1) (state L1) (step L1) (globalenv L1) s t s') (and (@nostep (genvtype L1) (state L1) (step L1) (globalenv L1) s') (forall r : Int.int, not (final_state L1 s' r)))))) *)
  destruct (not_all_ex_not _ _ H) as [t1 A]; clear H.
(* Goal: @ex trace (fun t : trace => @ex (state L1) (fun s' : state L1 => and (behavior_prefix t b) (and (@star (genvtype L1) (state L1) (step L1) (globalenv L1) s t s') (and (@nostep (genvtype L1) (state L1) (step L1) (globalenv L1) s') (forall r : Int.int, not (final_state L1 s' r)))))) *)
  destruct (not_all_ex_not _ _ A) as [s' B]; clear A.
(* Goal: @ex trace (fun t : trace => @ex (state L1) (fun s' : state L1 => and (behavior_prefix t b) (and (@star (genvtype L1) (state L1) (step L1) (globalenv L1) s t s') (and (@nostep (genvtype L1) (state L1) (step L1) (globalenv L1) s') (forall r : Int.int, not (final_state L1 s' r)))))) *)
  destruct (not_all_ex_not _ _ B) as [b2 C]; clear B.
(* Goal: @ex trace (fun t : trace => @ex (state L1) (fun s' : state L1 => and (behavior_prefix t b) (and (@star (genvtype L1) (state L1) (step L1) (globalenv L1) s t s') (and (@nostep (genvtype L1) (state L1) (step L1) (globalenv L1) s') (forall r : Int.int, not (final_state L1 s' r)))))) *)
  destruct (imply_to_and _ _ C) as [D E]; clear C.
(* Goal: @ex trace (fun t : trace => @ex (state L1) (fun s' : state L1 => and (behavior_prefix t b) (and (@star (genvtype L1) (state L1) (step L1) (globalenv L1) s t s') (and (@nostep (genvtype L1) (state L1) (step L1) (globalenv L1) s') (forall r : Int.int, not (final_state L1 s' r)))))) *)
  destruct (imply_to_and _ _ E) as [F G]; clear E.
(* Goal: @ex trace (fun t : trace => @ex (state L1) (fun s' : state L1 => and (behavior_prefix t b) (and (@star (genvtype L1) (state L1) (step L1) (globalenv L1) s t s') (and (@nostep (genvtype L1) (state L1) (step L1) (globalenv L1) s') (forall r : Int.int, not (final_state L1 s' r)))))) *)
  destruct (not_or_and _ _ G) as [P Q]; clear G.
(* Goal: @ex trace (fun t : trace => @ex (state L1) (fun s' : state L1 => and (behavior_prefix t b) (and (@star (genvtype L1) (state L1) (step L1) (globalenv L1) s t s') (and (@nostep (genvtype L1) (state L1) (step L1) (globalenv L1) s') (forall r : Int.int, not (final_state L1 s' r)))))) *)
  exists t1; exists s'.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  split. exists b2; auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  split. auto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  split. red; intros; red; intros. elim Q. exists t; exists s'0; auto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. elim P. exists r; auto.
Qed.

Lemma backward_simulation_star:
  forall s2 t s2', Star L2 s2 t s2' ->
  forall i s1 b, match_states i s1 s2 -> safe_along_behavior s1 (behavior_app t b) ->
  exists i', exists s1', Star L1 s1 t s1' /\ match_states i' s1' s2'.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  induction 1; intros.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists i; exists s1; split; auto. apply star_refl.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit (bsim_simulation S); eauto. eapply safe_along_safe; eauto.
  intros [i' [s1' [A B]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  assert (Star L1 s0 t1 s1'). intuition. apply plus_star; auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit IHstar; eauto. eapply star_safe_along; eauto.
(* Goal: @eq program_behavior (behavior_app (Eapp t1 t) beh3') (behavior_app t1 (behavior_app t beh3')) *)
  subst t; apply behavior_app_assoc.
  intros [i'' [s2'' [C D]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists i''; exists s2''; split; auto. eapply star_trans; eauto.
Qed.

Lemma backward_simulation_forever_silent:
  forall i s1 s2,
  Forever_silent L2 s2 -> match_states i s1 s2 -> safe L1 s1 ->
  Forever_silent L1 s1.
Proof.
  assert (forall i s1 s2,
         Forever_silent L2 s2 -> match_states i s1 s2 -> safe L1 s1 ->
         forever_silent_N (step L1) order (globalenv L1) i s1).
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
    cofix COINDHYP; intros.
(* Goal: @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S3)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S3)))) *)
    inv H.  destruct (bsim_simulation S _ _ _ H2 _ H0 H1) as [i' [s2' [A B]]].
    destruct A as [C | [C D]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
    eapply forever_silent_N_plus; eauto. eapply COINDHYP; eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
      eapply star_safe; eauto. apply plus_star; auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
    eapply forever_silent_N_star; eauto. eapply COINDHYP; eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
      eapply star_safe; eauto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. eapply forever_silent_N_forever; eauto. eapply bsim_order_wf; eauto.
Qed.

Lemma backward_simulation_forever_reactive:
  forall i s1 s2 T,
  Forever_reactive L2 s2 T -> match_states i s1 s2 -> safe_along_behavior s1 (Reacts T) ->
  Forever_reactive L1 s1 T.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  cofix COINDHYP; intros. inv H.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  destruct (backward_simulation_star H2 (Reacts T0) H0) as [i' [s1' [A B]]]; eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  econstructor; eauto. eapply COINDHYP; eauto. eapply star_safe_along; eauto.
Qed.

Lemma backward_simulation_state_behaves:
  forall i s1 s2 beh2,
  match_states i s1 s2 -> state_behaves L2 s2 beh2 ->
  exists beh1, state_behaves L1 s1 beh1 /\ behavior_improves beh1 beh2.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. destruct (classic (safe_along_behavior s1 beh2)).
- (* 1. Safe along *)
  exists beh2; split; [idtac|apply behavior_improves_refl].
(* Goal: @forever_silent (genvtype L) (state L) (step L) (globalenv L) (@snd trace (state L) s) *)
  inv H0.
+ (* termination *)
  assert (Terminates t r = behavior_app t (Terminates E0 r)).
(* Goal: @eq program_behavior (behavior_app t (Goes_wrong E0)) (Goes_wrong t) *)
    simpl. rewrite E0_right; auto.
  rewrite H0 in H1.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit backward_simulation_star; eauto.
  intros [i' [s1' [A B]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit (bsim_match_final_states S); eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
    eapply safe_along_safe. eapply star_safe_along; eauto.
  intros [s1'' [C D]].
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. eapply star_trans; eauto. traceEq. auto.
+ (* silent divergence *)
  assert (Diverges t = behavior_app t (Diverges E0)).
(* Goal: @eq program_behavior (behavior_app t (Goes_wrong E0)) (Goes_wrong t) *)
    simpl. rewrite E0_right; auto.
  rewrite H0 in H1.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit backward_simulation_star; eauto.
  intros [i' [s1' [A B]]].
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. eauto. eapply backward_simulation_forever_silent; eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  eapply safe_along_safe. eapply star_safe_along; eauto.
+ (* reactive divergence *)
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. eapply backward_simulation_forever_reactive; eauto.
+ (* goes wrong *)
  assert (Goes_wrong t = behavior_app t (Goes_wrong E0)).
(* Goal: @eq program_behavior (behavior_app t (Goes_wrong E0)) (Goes_wrong t) *)
    simpl. rewrite E0_right; auto.
  rewrite H0 in H1.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit backward_simulation_star; eauto.
  intros [i' [s1' [A B]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit (bsim_progress S); eauto. eapply safe_along_safe. eapply star_safe_along; eauto.
  intros [[r FIN] | [t' [s2' STEP2]]].
  elim (H4 _ FIN).
  elim (H3 _ _ STEP2).

- (* 2. Not safe along *)
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit not_safe_along_behavior; eauto.
  intros [t [s1' [PREF [STEPS [NOSTEP NOFIN]]]]].
(* Goal: and (@forever_silent genv state step ge (state_of_tstate S')) (@eq traceinf T (Eappinf t (traceinf_of_tstate S'))) *)
  exists (Goes_wrong t); split.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  econstructor; eauto.
(* Goal: or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  right. exists t; auto.
Qed.

End BACKWARD_SIMULATIONS.

Theorem backward_simulation_behavior_improves:
  forall L1 L2, backward_simulation L1 L2 ->
  forall beh2, program_behaves L2 beh2 ->
  exists beh1, program_behaves L1 beh1 /\ behavior_improves beh1 beh2.
Proof.
(* Goal: @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S3)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S3)))) *)
  intros L1 L2 S beh2 H. destruct S as [index order match_states S]. inv H.
- (* L2's initial state is defined. *)
(* Goal: @ex program_behavior (fun beh1 : program_behavior => and (program_behaves L1 beh1) (behavior_improves beh1 beh2)) *)
  destruct (classic (exists s1, initial_state L1 s1)) as [[s1 INIT] | NOINIT].
+ (* L1's initial state is defined too. *)
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit (bsim_match_initial_states S); eauto. intros [i [s1' [INIT1' MATCH]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit backward_simulation_state_behaves; eauto. intros [beh1 [A B]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists beh1; split; auto. econstructor; eauto.
+ (* L1 has no initial state *)
(* Goal: @ex program_behavior (fun beh1 : program_behavior => and (program_behaves L1 beh1) (behavior_improves beh1 (Goes_wrong E0))) *)
  exists (Goes_wrong E0); split.
(* Goal: program_behaves L1 (Goes_wrong E0) *)
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply program_goes_initially_wrong.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. elim NOINIT; exists s0; auto.
(* Goal: behavior_improves (Goes_wrong E0) beh2 *)
  apply behavior_improves_bot.
- (* L2 has no initial state *)
(* Goal: @ex program_behavior (fun beh1 : program_behavior => and (program_behaves L1 beh1) (behavior_improves beh1 (Goes_wrong E0))) *)
  exists (Goes_wrong E0); split.
(* Goal: program_behaves L1 (Goes_wrong E0) *)
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply program_goes_initially_wrong.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit (bsim_initial_states_exist S); eauto. intros [s2 INIT2].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  elim (H0 s2); auto.
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply behavior_improves_refl.
Qed.

Corollary backward_simulation_same_safe_behavior:
  forall L1 L2, backward_simulation L1 L2 ->
  (forall beh, program_behaves L1 beh -> not_wrong beh) ->
  (forall beh, program_behaves L2 beh -> program_behaves L1 beh).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. exploit backward_simulation_behavior_improves; eauto.
(* Goal: forall _ : @ex program_behavior (fun beh1 : program_behavior => and (program_behaves L1 beh1) (behavior_improves beh1 beh)), program_behaves L1 beh *)
  intros [beh' [A B]]. destruct B.
(* Goal: program_behaves L1 beh *)
(* Goal: program_behaves L1 beh *)
  congruence.
(* Goal: program_behaves L1 beh *)
  destruct H2 as [t [C D]]. subst. elim (H0 (Goes_wrong t)). auto.
Qed.

(** * Program behaviors for the "atomic" construction *)

Section ATOMIC.

Variable L: semantics.
Hypothesis Lwb: well_behaved_traces L.

Remark atomic_finish: forall s t, output_trace t -> Star (atomic L) (t, s) t (E0, s).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  induction t; intros.
(* Goal: @star genv state step ge (state_of_tstate S) E0 (state_of_tstate S) *)
(* Goal: @eq traceinf (traceinf_of_tstate S) (Eappinf E0 (traceinf_of_tstate S)) *)
(* Goal: @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S3)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S3)))) *)
  apply star_refl.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  simpl in H; destruct H. eapply star_left; eauto.
(* Goal: @eq program_behavior (behavior_app t (Goes_wrong E0)) (Goes_wrong t) *)
  simpl. apply atomic_step_continue; auto. simpl; auto. auto.
Qed.

Lemma step_atomic_plus:
  forall s1 t s2, Step L s1 t s2 -> Plus (atomic L) (E0,s1) t (E0,s2).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros.  destruct t.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply plus_one. simpl; apply atomic_step_silent; auto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  exploit Lwb; eauto. simpl; intros.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  eapply plus_left. eapply atomic_step_start; eauto. eapply atomic_finish; eauto. auto.
Qed.

Lemma star_atomic_star:
  forall s1 t s2, Star L s1 t s2 -> Star (atomic L) (E0,s1) t (E0,s2).
Proof.
(* Goal: @star genv state step ge (state_of_tstate S) E0 (state_of_tstate S) *)
(* Goal: @eq traceinf (traceinf_of_tstate S) (Eappinf E0 (traceinf_of_tstate S)) *)
(* Goal: @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S3)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S3)))) *)
  induction 1. apply star_refl. eapply star_trans with (s2 := (E0,s2)).
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply plus_star. eapply step_atomic_plus; eauto. eauto. auto.
Qed.

Lemma atomic_forward_simulation: forward_simulation L (atomic L).
Proof.
(* Goal: forward_simulation L (atomic L) *)
  set (ms := fun (s: state L) (ts: state (atomic L)) => ts = (E0,s)).
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  apply forward_simulation_plus with ms; intros.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists (E0,s1); split. simpl; auto. red; auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  red in H. subst s2. simpl; auto.
(* Goal: and (@forever_silent genv state step ge (state_of_tstate S')) (@eq traceinf T (Eappinf t (traceinf_of_tstate S'))) *)
  red in H0. subst s2. exists (E0,s1'); split.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply step_atomic_plus; auto. red; auto.
Qed.

Lemma atomic_star_star_gen:
  forall ts1 t ts2, Star (atomic L) ts1 t ts2 ->
  exists t', Star L (snd ts1) t' (snd ts2) /\ fst ts1 ** t' = t ** fst ts2.
Proof.
(* Goal: forall (S1 S2 : tstate) (_ : tsteps S1 S2), @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S2)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S2)))) *)
  induction 1.
(* Goal: @eq traceinf (Eappinf t (Eappinf t' (traceinf_of_tstate S3))) (Eappinf (Eapp t t') (traceinf_of_tstate S3)) *)
  exists E0; split. apply star_refl. traceEq.
(* Goal: @ex trace (fun t' : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) (@snd trace (state L) s1) t' (@snd trace (state L) s3)) (@eq trace (Eapp (@fst trace (state L) s1) t') (Eapp t (@fst trace (state L) s3)))) *)
  destruct IHstar as [t' [A B]].
(* Goal: @ex trace (fun t' : trace => and (@star (genvtype L) (state L) (step L) (globalenv L) (@snd trace (state L) s1) t' (@snd trace (state L) s3)) (@eq trace (Eapp (@fst trace (state L) s1) t') (Eapp t (@fst trace (state L) s3)))) *)
  simpl in H; inv H; simpl in *.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists t'; split. eapply star_left; eauto. auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists (ev :: t0 ** t'); split. eapply star_left; eauto. rewrite B; auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists t'; split. auto. rewrite B; auto.
Qed.

Lemma atomic_star_star:
  forall s1 t s2, Star (atomic L) (E0,s1) t (E0,s2) -> Star L s1 t s2.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. exploit atomic_star_star_gen; eauto. intros [t' [A B]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  simpl in *. replace t with t'. auto. subst; traceEq.
Qed.

Lemma atomic_forever_silent_forever_silent:
  forall s, Forever_silent (atomic L) s -> Forever_silent L (snd s).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  cofix COINDHYP; intros. inv H. inv H0.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply forever_silent_intro with (snd (E0, s')). auto. apply COINDHYP; auto.
Qed.

Remark star_atomic_output_trace:
  forall s t t' s',
  Star (atomic L) (E0, s) t (t', s') -> output_trace t'.
Proof.
  assert (forall ts1 t ts2, Star (atomic L) ts1 t ts2 ->
          output_trace (fst ts1) -> output_trace (fst ts2)).
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  induction 1; intros. auto. inv H; simpl in *.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply IHstar. auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply IHstar. exploit Lwb; eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  destruct H2. apply IHstar. auto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. change t' with (fst (t',s')). eapply H; eauto. simpl; auto.
Qed.

Lemma atomic_forever_reactive_forever_reactive:
  forall s T, Forever_reactive (atomic L) (E0,s) T -> Forever_reactive L s T.
Proof.
  assert (forall t s T, Forever_reactive (atomic L) (t,s) T ->
          exists T', Forever_reactive (atomic L) (E0,s) T' /\ T = t *** T').
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  induction t; intros. exists T; auto.
(* Goal: @forever_silent (genvtype L) (state L) (step L) (globalenv L) (@snd trace (state L) s) *)
  inv H. inv H0. congruence. simpl in H; inv H.
(* Goal: @forever_reactive (genvtype L) (state L) (step L) (globalenv L) s1 (Eappinf t T) *)
  destruct (IHt s (t2***T0)) as [T' [A B]]. eapply star_forever_reactive; eauto.
(* Goal: @eq program_behavior (behavior_app t (Goes_wrong E0)) (Goes_wrong t) *)
  exists T'; split; auto. simpl. congruence.

(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  cofix COINDHYP; intros. inv H0. destruct s2 as [t2 s2].
  destruct (H _ _ _ H3) as [T' [A B]].
  assert (Star (atomic L) (E0, s) (t**t2) (E0, s2)).
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
    eapply star_trans. eauto. apply atomic_finish. eapply star_atomic_output_trace; eauto. auto.
  replace (t *** T0) with ((t ** t2) *** T'). apply forever_reactive_intro with s2.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply atomic_star_star; auto. destruct t; simpl in *; unfold E0 in *; congruence.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply COINDHYP. auto.
(* Goal: @eq traceinf (Eappinf t (Eappinf t' (traceinf_of_tstate S3))) (Eappinf (Eapp t t') (traceinf_of_tstate S3)) *)
  subst T0; traceEq.
Qed.

Theorem atomic_behaviors:
  forall beh, program_behaves L beh <-> program_behaves (atomic L) beh.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; split; intros.
- (* L -> atomic L *)
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit forward_simulation_behavior_improves. eapply atomic_forward_simulation. eauto.
(* Goal: forall _ : @ex program_behavior (fun beh2 : program_behavior => and (state_behaves L2 s' beh2) (behavior_improves beh1 beh2)), @ex program_behavior (fun beh2 : program_behavior => and (program_behaves L2 beh2) (behavior_improves beh1 beh2)) *)
  intros [beh2 [A B]]. red in B. destruct B as [EQ | [t [C D]]].
(* Goal: program_behaves L1 beh *)
(* Goal: program_behaves L1 beh *)
  congruence.
(* Goal: @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S3)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S3)))) *)
  subst beh. inv H. inv H1.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply program_runs with (E0,s). simpl; auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply state_goes_wrong with (E0,s'). apply star_atomic_star; auto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  red; intros; red; intros. inv H. eelim H3; eauto. eelim H3; eauto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. simpl in H. destruct H. eelim H4; eauto.
(* Goal: program_behaves L1 (Goes_wrong E0) *)
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply program_goes_initially_wrong.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. simpl in H; destruct H. eelim H1; eauto.
- (* atomic L -> L *)
(* Goal: @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S3)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S3)))) *)
  inv H.
+ (* initial state defined *)
  destruct s as [t s]. simpl in H0. destruct H0; subst t.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply program_runs with s; auto.
  inv H1.
* (* termination *)
  destruct s' as [t' s']. simpl in H2; destruct H2; subst t'.
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. eapply atomic_star_star; eauto. auto.
* (* silent divergence *)
  destruct s' as [t' s'].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  assert (t' = E0). inv H2. inv H1; auto. subst t'.
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. eapply atomic_star_star; eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  change s' with (snd (E0,s')). apply atomic_forever_silent_forever_silent. auto.
* (* reactive divergence *)
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  econstructor. apply atomic_forever_reactive_forever_reactive. auto.
* (* going wrong *)
  destruct s' as [t' s'].
  assert (t' = E0).
(* Goal: @eq program_behavior (behavior_app t (Goes_wrong E0)) (Goes_wrong t) *)
    destruct t'; auto. eelim H2. simpl. apply atomic_step_continue.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
    eapply star_atomic_output_trace; eauto.
(* Goal: state_behaves L2 s2 (Reacts T) *)
(* Goal: behavior_improves (Reacts T) (Reacts T) *)
  subst t'. econstructor. apply atomic_star_star; eauto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  red; intros; red; intros. destruct t0.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  elim (H2 E0 (E0,s'0)). constructor; auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  elim (H2 (e::nil) (t0,s'0)). constructor; auto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. elim (H3 r). simpl; auto.
+ (* initial state undefined *)
(* Goal: program_behaves L1 (Goes_wrong E0) *)
(* Goal: behavior_improves (Goes_wrong E0) (Goes_wrong E0) *)
  apply program_goes_initially_wrong.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros; red; intros. elim (H0 (E0,s)); simpl; auto.
Qed.

End ATOMIC.

(** * Additional results about infinite reduction sequences *)

(** We now show that any infinite sequence of reductions is either of
  the "reactive" kind or of the "silent" kind (after a finite number
  of non-silent transitions).  The proof necessitates the axiom of
  excluded middle.  This result is used below to relate
  the coinductive big-step semantics for divergence with the
  small-step notions of divergence. *)

Unset Implicit Arguments.

Section INF_SEQ_DECOMP.

Variable genv: Type.
Variable state: Type.
Variable step: genv -> state -> trace -> state -> Prop.

Variable ge: genv.

Inductive tstate: Type :=
  ST: forall (s: state) (T: traceinf), forever step ge s T -> tstate.

Definition state_of_tstate (S: tstate): state :=
  match S with ST s T F => s end.
Definition traceinf_of_tstate (S: tstate) : traceinf :=
  match S with ST s T F => T end.

Inductive tstep: trace -> tstate -> tstate -> Prop :=
  | tstep_intro: forall s1 t T s2 S F,
      tstep t (ST s1 (t *** T) (@forever_intro genv state step ge s1 t s2 T S F))
              (ST s2 T F).

Inductive tsteps: tstate -> tstate -> Prop :=
  | tsteps_refl: forall S, tsteps S S
  | tsteps_left: forall t S1 S2 S3, tstep t S1 S2 -> tsteps S2 S3 -> tsteps S1 S3.

Remark tsteps_trans:
  forall S1 S2, tsteps S1 S2 -> forall S3, tsteps S2 S3 -> tsteps S1 S3.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  induction 1; intros. auto. econstructor; eauto.
Qed.

Let treactive (S: tstate) : Prop :=
  forall S1,
  tsteps S S1 ->
  exists S2, exists S3, exists t, tsteps S1 S2 /\ tstep t S2 S3 /\ t <> E0.

Let tsilent (S: tstate) : Prop :=
  forall S1 t S2, tsteps S S1 -> tstep t S1 S2 -> t = E0.

Lemma treactive_or_tsilent:
  forall S, treactive S \/ (exists S', tsteps S S' /\ tsilent S').
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. destruct (classic (exists S', tsteps S S' /\ tsilent S')).
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  auto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  left. red; intros.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  generalize (not_ex_all_not _ _ H S1). intros.
(* Goal: @ex tstate (fun S2 : tstate => @ex tstate (fun S3 : tstate => @ex trace (fun t : trace => and (tsteps S1 S2) (and (tstep t S2 S3) (not (@eq trace t E0)))))) *)
(* Goal: @ex tstate (fun S2 : tstate => @ex tstate (fun S3 : tstate => @ex trace (fun t : trace => and (tsteps S1 S2) (and (tstep t S2 S3) (not (@eq trace t E0)))))) *)
  destruct (not_and_or _ _ H1). contradiction.
(* Goal: @ex tstate (fun S2 : tstate => @ex tstate (fun S3 : tstate => @ex trace (fun t : trace => and (tsteps S1 S2) (and (tstep t S2 S3) (not (@eq trace t E0)))))) *)
  unfold tsilent in H2.
(* Goal: @ex tstate (fun S2 : tstate => @ex tstate (fun S3 : tstate => @ex trace (fun t : trace => and (tsteps S1 S2) (and (tstep t S2 S3) (not (@eq trace t E0)))))) *)
  generalize (not_all_ex_not _ _ H2). intros [S2 A].
(* Goal: @ex tstate (fun S2 : tstate => @ex tstate (fun S3 : tstate => @ex trace (fun t : trace => and (tsteps S1 S2) (and (tstep t S2 S3) (not (@eq trace t E0)))))) *)
  generalize (not_all_ex_not _ _ A). intros [t B].
(* Goal: @ex tstate (fun S2 : tstate => @ex tstate (fun S3 : tstate => @ex trace (fun t : trace => and (tsteps S1 S2) (and (tstep t S2 S3) (not (@eq trace t E0)))))) *)
  generalize (not_all_ex_not _ _ B). intros [S3 C].
(* Goal: @ex tstate (fun S2 : tstate => @ex tstate (fun S3 : tstate => @ex trace (fun t : trace => and (tsteps S1 S2) (and (tstep t S2 S3) (not (@eq trace t E0)))))) *)
  generalize (imply_to_and _ _ C). intros [D F].
(* Goal: @ex tstate (fun S2 : tstate => @ex tstate (fun S3 : tstate => @ex trace (fun t : trace => and (tsteps S1 S2) (and (tstep t S2 S3) (not (@eq trace t E0)))))) *)
  generalize (imply_to_and _ _ F). intros [G J].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists S2; exists S3; exists t. auto.
Qed.

Lemma tsteps_star:
  forall S1 S2, tsteps S1 S2 ->
  exists t, star step ge (state_of_tstate S1) t (state_of_tstate S2)
         /\ traceinf_of_tstate S1 = t *** traceinf_of_tstate S2.
Proof.
(* Goal: forall (S1 S2 : tstate) (_ : tsteps S1 S2), @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S2)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S2)))) *)
  induction 1.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exists E0; split. apply star_refl. auto.
(* Goal: @ex trace (fun t : trace => and (@star genv state step ge (state_of_tstate S1) t (state_of_tstate S3)) (@eq traceinf (traceinf_of_tstate S1) (Eappinf t (traceinf_of_tstate S3)))) *)
  inv H. destruct IHtsteps as [t' [A B]].
(* Goal: and (@forever_silent genv state step ge (state_of_tstate S')) (@eq traceinf T (Eappinf t (traceinf_of_tstate S'))) *)
  exists (t ** t'); split.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  simpl; eapply star_left; eauto.
(* Goal: @eq traceinf (Eappinf t (Eappinf t' (traceinf_of_tstate S3))) (Eappinf (Eapp t t') (traceinf_of_tstate S3)) *)
  simpl in *. subst T. traceEq.
Qed.

Lemma tsilent_forever_silent:
  forall S,
  tsilent S -> forever_silent step ge (state_of_tstate S).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  cofix COINDHYP; intro S. case S. intros until f. simpl. case f. intros.
  assert (tstep t (ST s1 (t *** T0) (forever_intro s1 t s0 f0))
                  (ST s2 T0 f0)).
    constructor.
  assert (t = E0).
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
    red in H. eapply H; eauto. apply tsteps_refl.
  apply forever_silent_intro with (state_of_tstate (ST s2 T0 f0)).
  rewrite <- H1. assumption.
  apply COINDHYP.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  red; intros. eapply H. eapply tsteps_left; eauto. eauto.
Qed.

Lemma treactive_forever_reactive:
  forall S,
  treactive S -> forever_reactive step ge (state_of_tstate S) (traceinf_of_tstate S).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  cofix COINDHYP; intros.
  destruct (H S) as [S1 [S2 [t [A [B C]]]]]. apply tsteps_refl.
  destruct (tsteps_star _ _ A) as [t' [P Q]].
  inv B. simpl in *. rewrite Q. rewrite <- Eappinf_assoc.
  apply forever_reactive_intro with s2.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  eapply star_right; eauto.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  red; intros. destruct (Eapp_E0_inv _ _ H0). contradiction.
  change (forever_reactive step ge (state_of_tstate (ST s2 T F)) (traceinf_of_tstate (ST s2 T F))).
  apply COINDHYP.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  red; intros. apply H.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  eapply tsteps_trans. eauto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  eapply tsteps_left. constructor. eauto.
Qed.

Theorem forever_silent_or_reactive:
  forall s T,
  forever step ge s T ->
  forever_reactive step ge s T \/
  exists t, exists s', exists T',
  star step ge s t s' /\ forever_silent step ge s' /\ T = t *** T'.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros.
(* Goal: or (@forever_reactive genv state step ge s T) (@ex trace (fun t : trace => @ex state (fun s' : state => @ex traceinf (fun T' : traceinf => and (@star genv state step ge s t s') (and (@forever_silent genv state step ge s') (@eq traceinf T (Eappinf t T'))))))) *)
  destruct (treactive_or_tsilent (ST s T H)).
(* Goal: or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
(* Goal: or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  left.
(* Goal: @forever_reactive genv state step ge s T *)
(* Goal: or (@forever_reactive genv state step ge s T) (@ex trace (fun t : trace => @ex state (fun s' : state => @ex traceinf (fun T' : traceinf => and (@star genv state step ge s t s') (and (@forever_silent genv state step ge s') (@eq traceinf T (Eappinf t T'))))))) *)
  change (forever_reactive step ge (state_of_tstate (ST s T H)) (traceinf_of_tstate (ST s T H))).
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  apply treactive_forever_reactive. auto.
(* Goal: or (@forever_reactive genv state step ge s T) (@ex trace (fun t : trace => @ex state (fun s' : state => @ex traceinf (fun T' : traceinf => and (@star genv state step ge s t s') (and (@forever_silent genv state step ge s') (@eq traceinf T (Eappinf t T'))))))) *)
  destruct H0 as [S' [A B]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit tsteps_star; eauto. intros [t [C D]]. simpl in *.
(* Goal: or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  right. exists t; exists (state_of_tstate S'); exists (traceinf_of_tstate S').
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  split. auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  split. apply tsilent_forever_silent. auto.
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  auto.
Qed.

End INF_SEQ_DECOMP.

Set Implicit Arguments.

(** * Big-step semantics and program behaviors *)

Section BIGSTEP_BEHAVIORS.

Variable B: bigstep_semantics.
Variable L: semantics.
Hypothesis sound: bigstep_sound B L.

Lemma behavior_bigstep_terminates:
  forall t r,
  bigstep_terminates B t r -> program_behaves L (Terminates t r).
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. exploit (bigstep_terminates_sound sound); eauto.
(* Goal: forall _ : @ex (state L) (fun s1 : state L => @ex (state L) (fun s2 : state L => and (initial_state L s1) (and (@star (genvtype L) (state L) (step L) (globalenv L) s1 t s2) (final_state L s2 r)))), program_behaves L (Terminates t r) *)
  intros [s1 [s2 [P [Q R]]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  econstructor; eauto. econstructor; eauto.
Qed.

Lemma behavior_bigstep_diverges:
  forall T,
  bigstep_diverges B T ->
  program_behaves L (Reacts T)
  \/ exists t, program_behaves L (Diverges t) /\ traceinf_prefix t T.
Proof.
(* Goal: forall (T : traceinf) (_ : bigstep_diverges B T), or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  intros. exploit (bigstep_diverges_sound sound); eauto. intros [s1 [P Q]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  exploit forever_silent_or_reactive; eauto. intros [X | [t [s' [T' [X [Y Z]]]]]].
(* Goal: @eq traceinf T (Eappinf t (traceinf_of_tstate S')) *)
  left. econstructor; eauto. constructor; auto.
(* Goal: or (program_behaves L (Reacts T)) (@ex trace (fun t : trace => and (program_behaves L (Diverges t)) (traceinf_prefix t T))) *)
  right. exists t; split. econstructor; eauto. econstructor; eauto. exists T'; auto.
Qed.

End BIGSTEP_BEHAVIORS.
