Trace semantics

Now that we have seen a little bit of theory about orders and sequences, let talk about semantics.
In the most general case, one can see a program as a transition system. Then, to express the semantics, one can describe all the traces a program can have : it is called trace semantics. These semantics are in the most general you can find since they do not depend on the programming language : you can parameterise them by the transitions semantics.
In addition, you can abstract at different level these semantics, according to which properties you want to prove. This will give us a nice hierarchy of trace semantics, which we will abstract according to our needs.
As stated in the order theory chapter, by abstracting we mean "providing a concretisation function" for a certain Lattice on which we operate.
TODO : Prove theorems.
TODO : Define corretly length of infinite traces, and define galois relations between finite & infinite traces.

From Stdlib Require Import List.
From Stdlib Require Import Streams.
Import ListNotations.
From Stdlib Require Import Equalities.
From Stdlib Require Import Ensembles.
From AISAV Require Import Utils.
From AISAV Require Import Order.
From AISAV Require Import Sequence.

Transition system

First, we define what is a TransitionSystem.
We require it to have boolean equality in order to support junction.

Module Type TransitionSystem
  (Ty : Typ) (Import State : HasEqb Ty).
A transition system have states. In our case, it will be control point * memory state, but let keep it general.
  Definition state := Ty.t.

Then, it has of course transitions, that is, a function to pass from one state to another. Here, we force it to be deterministic.
  Parameter transition : state → state → Prop.

And finally, it also has a set of initial and final states.
  Parameter initial : state → Prop.
  Parameter final : state → Prop.
End TransitionSystem.

Traces

We can define traces for a given transition system. We will see two kind of traces.
  • Finite traces, the simplest one. They provide everything we need to reason on "value" semantics, that is, which values may output an execution of a program. However, they are not powerfull enough to prove things about termination, and more generally, about liveness properties, as we will see later.
  • Potentially infinite traces, lifting up this limitation.
  • It would remain one more general semantics, on set of set of traces instead of traces as we do here. This would allow us to reason about properties such as determinism. We won't cover it here, but it's not very different from what we see here.
Thus, we will see various different traces semantics. We will also create a hierarchy between these trace semantics, resumed in the following diagram : TODO
But first, let define what a trace should provide.
Module Type TraceDefinitions
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State).
A trace may be a generic type, for instance a list, a streams, or even a cumulation of both.
  Parameter trace : Type.
However, you should provide some basic operations on these traces. For instance, their length and how to build a trace from a state and another trace.
  Parameter length : trace → nat.
  Parameter cons : state → trace → trace.

The empty trace is often usefull
  Parameter epsilon : trace.
Traces of size 1 are the states basically. We call them Sigma.
  Parameter Sigma : trace → Prop.

More generally, you can always convert a set of states in set of traces of size 1.
  Parameter to_traces : (state → Prop) → (trace → Prop).
In the same ways, we can see transitions as traces of length 2
  Parameter trace_transitions : trace → Prop.

  Parameter concat : trace → trace → trace.
A very useful operation is junction. Junction concatenate two traces iff their end and being resp. match, removing the first element of the second trace.
  Parameter junction : trace → trace → trace.
End TraceDefinitions.

We can extend these definitions to sets of traces.
Module Type TraceSetSig
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
  (Import TraceDef : TraceDefinitions Ty State Transitions).
  Parameter concat_set: (trace → Prop) → (trace → Prop) → (trace → Prop).
  Parameter junction_set: (trace → Prop) → (trace → Prop) → (trace → Prop).
  Parameter cpow : (trace → Prop) → nat → (trace → Prop).
  Parameter jpow : (trace → Prop) → nat → (trace → Prop).

  Parameter Valid : trace → Prop.

End TraceSetSig.

Note that you should include it after definitions of Trace, and before any other definitions, if you want to include it in your module.
Module TraceSet
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
  (Import TraceDef : TraceDefinitions Ty State Transitions)
    <: TraceSetSig Ty State Transitions TraceDef.
We can extend these definitions to sets of traces.
  Definition concat_set := set_from_op concat.
  Definition junction_set := set_from_op junction.
  Definition cpow := generic_pow concat_set (fun x ⇒ x = epsilon).
  Definition jpow := generic_pow junction_set Sigma.
  Notation " t1 '^j' t2" := (junction_set t1 t2)
    (at level 40, left associativity).

We say that a trace sigma is valid if it respect the transition relation. That is, forall i, transition sigma_i sigma(i+1). TODO Put it in a module dedicated to semantics.
  Inductive ValidT : trace → Prop :=
  | ValidEmpty : ValidT epsilon
  | ValidSucc (x y : state) (t : trace) :
    transition x y → ValidT (cons x (cons y t)).

  Definition Valid := ValidT.

End TraceSet.

Module Type TraceSig
  (Ty : Typ) (State : HasEqb Ty)
  (Transitions : TransitionSystem Ty State) :=
     TraceDefinitions Ty State Transitions
  <+ TraceSetSig Ty State Transitions.

In addition, we want to work on semantics. Let define a module type listing all the requirement to consider something to be a semantic.
Module Type SemanticSig.
First of all, a semantic should output something. Most of the time, it will be a set of states or traces.
  Parameter t : Type.
Then, in order to apply the theorems we previously saw, we need to provide a partial order structure on this output type.
In the idea, a set of output "lower" than another as its semantics included in it (by the way, inclusion is a valid order, but you may not always want to use it, as we will see for MaximalTraces).
For concrete semantics, we will almost always require it to be a complete lattice, since it can help quite a lot with some proofs. But for generality sake, let restrains ourself to partial orders.
  Parameter r : Relation t.
  Axiom t_pre_order : PreOrder r.
  Axiom t_partial_order :
    @PartialOrder t eq _ r t_pre_order.

Then, the semantic of a program is just an output element. Pay attention to give it sense !
  Parameter semantics : t.

And finally, we require the ability to express this semantics in a least fixpoint form. This will allow us to use theorems from order theory.
  Parameter F : t → t.

LFP semantics and this semantics form are the same.
  Axiom lfp_semantics :
    ∀ lfp,
      @LFP' _ _ _ _ t_partial_order F lfp
        ↔ semantics = lfp.

End SemanticSig.

This give us a very general trace semantic formulation.
Module Type TraceSemanticSig
  (Ty : Typ) (State : HasEqb Ty)
  (Transitions : TransitionSystem Ty State) :=
     TraceSig Ty State Transitions
  <+ SemanticSig.

Prefix Trace Semantics

Now, we can define a first kind of semantics : PrefixTrace Semantics.
This semantics is based on traces represented by lists. Since we will reuse it, let define a module for it.
Module ListTrace
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State).
  Definition trace : Type := list state.
  Definition length : trace → nat := @List.length state .

  Definition cons := @List.cons state.

  Definition epsilon : trace := nil.
  Definition Sigma (t : trace) : Prop := length t = 1.
  Definition to_traces (S : state → Prop) : trace → Prop :=
    fun t ⇒ match t with [x] ⇒ S x | _ ⇒ False end.
  Definition trace_transitions : trace → Prop :=
    fun t ⇒ match t with [x; y] ⇒ transition x y | _ ⇒ False end.

For convenience, let call transitions in trace form tau.
  Notation tau := trace_transitions.

  Definition concat (t1 t2 : trace) : trace := t1 ++ t2.
  Definition junction (t1 t2 : trace) : trace :=
    match t2 with
    | [] ⇒ []
    | x::t2s ⇒
      match last_error_list t1 with
        | SomeSeq y ⇒ if State.eqb x y then concat t1 t2s
                    else []
        | _ ⇒ []
      end
    end.
End ListTrace.

Module FiniteTraceOp
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State).
  Include ListTrace Ty State Transitions.
  Include TraceSet Ty State Transitions.

  Definition begin_initial (t : trace) : Prop :=
    match t with
    | [] ⇒ False
    | x::_ ⇒ Transitions.initial x
    end.

  Definition end_final (t : trace) : Prop :=
    match last_error_list t with
    | NoneSeq | NonfiniteSeq ⇒ False
    | SomeSeq x ⇒ Transitions.final x
    end.

End FiniteTraceOp.

Now, we can define prefix trace semantics.
Module PrefixTrace
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
    <: TraceSemanticSig Ty State Transitions.
  Include FiniteTraceOp Ty State Transitions.

Basically, we define this semantics as the valid finite traces begining in an initial state.
  Definition t := trace → Prop.
  Definition r : Relation t := Included trace.
  Instance t_pre_order : PreOrder r.
  Admitted.
  Instance t_partial_order : PartialOrder eq r.
  Admitted.
  Definition semantics : t :=
    fun (t : trace) ⇒ Valid t ∧ begin_initial t.

As we saw, we want to express this semantics in a least fixpoint form. This would allow us to use theorems from order theory.
  Definition F (T : t) : t :=
    union (to_traces Transitions.initial) (T ^j tau).

LFP semantics and "intuitive" semantics are the same.
  Theorem lfp_semantics :
    ∀ lfp, LFP' F lfp ↔ semantics = lfp.

We can define prefix of a trace.
  Definition is_prefix (x : trace) (y : trace) : Prop :=
    ∃ u : trace, concat x u = y.
And the closure of a set of traces by prefix.
  Definition prefix_closure (T : trace → Prop) : trace → Prop :=
    fun u ⇒ ∃ t : trace, T t ∧ is_prefix u t.
Then we can show that our semantic is closed by prefix.
This closeness is one of the main limitation of this semantics : It means that one can't prove liveness properties such as termination with it. For this, we will need maximal trace semantics. In addition, as we saw, the use of set of traces instead of set of set of traces is also limiting : we can't prove that our executions are deterministic for instance.

Forward Reachability Semantics

This first semantic is nice, but a bit complex : it operates on traces. Quite often, one only want to know if a program will reach some state, for instance if it will output some values. For this kind of problem, trace semantics is too complex for our needs : we only need states. Thus, let abstract our prefix trace semantics in a state semantics : Forward Reachability Semantics.
Module ForwardReachability
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
    <: TraceSemanticSig Ty State Transitions.
We need to construct traces in exactly the same way as for prefix trace semantics. We need them to define the intuitive semantics, but they won't be needed anymore in the fixpoint formulation.
We define forward reachability semantics as the states reachable from initials states of the transition system, in respect with transitions of the system.
  Definition t := state → Prop.
  Definition r : Relation t := Included state.
  Instance t_pre_order : PreOrder r.
  Admitted.
  Instance t_partial_order : PartialOrder eq r.
  Admitted.
  Definition semantics : t :=
    fun (s : state) ⇒ ∃ t,
      Valid t ∧ begin_initial t ∧ last_error_list t = SomeSeq s.

Once again, we can express semantics in a least fixpoint form. But to do so, we first need to define a "post" function. That is, a function which, given a set of states, return the following states.
  Definition post (S : t) : t :=
    fun s' : state ⇒
      ∃ s : state, S s ∧ transition s s'.

  Definition F (T : t) : t :=
    union Transitions.initial (post T).

  Theorem lfp_semantics :
    ∀ lfp, LFP' F lfp ↔ semantics = lfp.

End ForwardReachability.

Link between prefix traces and forward reachability

I said that forward reachability was just an abstraction of prefix traces. Let prove it : we will show that there is a galois embedding from prefix traces to forward reachability.
We have to use a module, because we have to instantiate the functors we used to define our semantics, in order to do the proof.
Note that even if we didn't proceed this way, it would have given us a way to prove the lfp_semantics theorem for ForwardReachability.
Module GalloisEmbeddingPrefixForward
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State).
  Module FR := ForwardReachability Ty State Transitions.
  Module PT := PrefixTrace Ty State Transitions.
We import both module because we want to use gamma connection notations easily.
TODO it gives a warning, put notation in a separate module.
  Import FR.
  Import PT.
We abstract simply by taking the last state of the traces.
  Definition alpha := fun T ⇒
      (fun s : state ⇒
        ∃ t, T t ∧ last_error_list t = SomeSeq s).
And we concretize by taking all the traces ending in forward reachability semantics.
  Definition gamma := fun S ⇒
      (fun t : trace ⇒
        match last_error_list t with
        | SomeSeq s ⇒ S s
        | _ ⇒ False end).
  Theorem link_prefix_forward :
    alpha a<<->c gamma.
End GalloisEmbeddingPrefixForward.

Suffix trace semantics

These two semantics are nice, but as their name says, they are "forward". What if want to go backward, for instance to get sufficient precondition, or to debug ? This is where SuffixTrace semantics (and then BackwardCoReachability semantics) come into the game.
Let first define SuffixTrace semantic, in the same way that we already did for PrefixTrace semantic.
Module SuffixTrace
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
    <: TraceSemanticSig Ty State Transitions.
  Include FiniteTraceOp Ty State Transitions.

Basically, we define this semantics as the valid finite traces ending in a final state.
  Definition t := trace → Prop.
  Definition r : Relation t := Included trace.
  Instance t_pre_order : PreOrder r.
  Admitted.
  Instance t_partial_order : PartialOrder eq r.
  Admitted.

  Definition semantics : t :=
    fun (t : trace) ⇒ Valid t ∧ end_final t.

As we saw, we want to express this semantics in a least fixpoint form. This will allow us to use theorems from order theory.
  Definition F (T : t) : t :=
    union (to_traces Transitions.final) (tau ^j T).

LFP semantics and this semantics form are the same.
  Theorem lfp_semantics :
    ∀ lfp, LFP' F lfp ↔ semantics = lfp.

We can define suffix of a trace.
  Definition is_suffix (x : trace) (y : trace) : Prop :=
    ∃ u : trace, concat u x = y.
And the closure of a set of traces by suffix.
  Definition suffix_closure (T : trace → Prop) : trace → Prop :=
    fun u ⇒ ∃ t : trace, T t ∧ is_suffix u t.
Then we can show that our semantic is closed by suffix.

Backward State Co-Reachability Semantics

Then, as we did for prefix trace semantic, we can abstract traces into states, in order to obtain a simpler semantic : Backward (State) Co Reachability Semantics.
Module BackwardCoReachability
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
    <: TraceSemanticSig Ty State Transitions.
  Include FiniteTraceOp Ty State Transitions.

We define backward co reachability semantics as the states reachable from final states of the transition system, in respect with the reverse transitions of the system.
  Definition t := state → Prop.
  Definition r : Relation t := Included state.
  Instance t_pre_order : PreOrder r.
  Admitted.
  Instance t_partial_order : PartialOrder eq r.
  Admitted.
  Definition semantics : t :=
    fun (s : state) ⇒ ∃ t,
      Valid t ∧ end_final t ∧ List.hd_error t = Some s.

Here, instead of a "post" function, we need a "pre" function. That is, a function which, given a set of states, return the preceding states. If we see post as a function of the transitions, pre can be seen as post of the reverse transition relation. Hence, compared to the post definition, we just swap s and s'.
  Definition pre (S : t) : t :=
    fun s : state ⇒
      ∃ s' : state, S s ∧ transition s s'.

  Definition F (T : t) : t :=
    union Transitions.final (pre T).

  Theorem lfp_semantics :
    ∀ lfp, LFP' F lfp ↔ semantics = lfp.
End BackwardCoReachability.

Link between suffix traces and backward co reachability

As for prefix traces and forward reachability, there is a gallois embedding between suffix traces and backward co reachability.
Module GalloisEmbeddingSuffixBackward
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State).
  Module BCR := BackwardCoReachability Ty State Transitions.
  Module ST := SuffixTrace Ty State Transitions.
  Import BCR.
  Import ST.
We abstract simply by taking the fist state of the traces.
  Definition alpha := fun T ⇒
      (fun s : state ⇒
        ∃ t, T t ∧ match t with [] ⇒ False | x::_ ⇒ x = s end).
And we concretize by taking all the traces beginning in backward co reachability semantics.
  Definition gamma := fun S ⇒
      (fun t : trace ⇒
        match t with
        | s::_ ⇒ S s
        | _ ⇒ False end).
  Theorem link_suffix_backward :
    alpha a<<->c gamma.
End GalloisEmbeddingSuffixBackward.

Partial finite trace semantics

We have seen four nice semantics. They present, regarding to galois connection, the following structure :
However, prefix and suffix semantics are not linked together by any galois connection (they are complementary).
Let thus define a semantic to unfiy them : partial finite trace semantic.
This semantic basically consist to forget the fact that trace may end or begin, hence the name *partial* traces.
Thus, it will effectively includes all prefix and suffix traces.
Module PartialFiniteTrace
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
    <: TraceSemanticSig Ty State Transitions.
  Include FiniteTraceOp Ty State Transitions.

Basically, we define this semantics as the valid finite traces.
  Definition t := trace → Prop.
  Parameter r : Relation t.
  Instance t_pre_order : PreOrder r.
  Admitted.
  Instance t_partial_order : PartialOrder eq r.
  Admitted.
  Definition semantics : t :=
    fun (t : trace) ⇒ Valid t.

As we saw, we want to express this semantics in a least fixpoint form. This will allow us to use theorems from order theory.
  Definition F (T : t) : t :=
    union Sigma (T ^j tau).

LFP semantics and this semantics form are the same.
  Theorem lfp_semantics :
    ∀ lfp, LFP' F lfp ↔ semantics = lfp.
End PartialFiniteTrace.

Link between partial finite traces and prefix traces

We introduced PartialFiniteTrace to have a "super class" of semantics, more precise than PrefixTraces and SuffixTraces. Let prove this affirmation.
Module GalloisEmbeddingPartialPrefix
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State).
  Module PT := PrefixTrace Ty State Transitions.
  Module PFT := PartialFiniteTrace Ty State Transitions.
  Import PT.
  Import PFT.
For abstracting into prefix traces, we keep only traces beginning in an initial state.
  Definition alpha := fun T ⇒
      (fun t ⇒
        T t ∧ match t with x::_ ⇒ initial x | _ ⇒ False end).
For concretizing into partial traces, we add all traces not beginning in an initial state.
  Definition gamma := fun T ⇒
    union T
      (fun t : trace ⇒
        match t with
        | x::_ ⇒ ¬ initial x
        | _ ⇒ True end).
  Theorem link_partial_prefix :
    alpha a↔c gamma.
End GalloisEmbeddingPartialPrefix.

Link between partial finite traces and suffix traces

And of course, we can do the same between PartialFiniteTraces and SuffixTraces.
Module GalloisEmbeddingPartialSuffix
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State).
  Module ST := SuffixTrace Ty State Transitions.
  Module PFT := PartialFiniteTrace Ty State Transitions.
  Import ST.
  Import PFT.
For abstracting into suffix traces, this time we keep only traces ending in final state.
  Definition alpha := fun T ⇒
      (fun t ⇒
        T t ∧ match last_error_list t with SomeSeq s ⇒ final s | _ ⇒ False end).
For concretizing into partial traces, we add all traces not ending in a final state.
  Definition gamma := fun T ⇒
      union T (fun t : trace ⇒
        match last_error_list t with
        | SomeSeq s ⇒ ¬ final s
        | _ ⇒ True end).
  Theorem link_partial_suffix :
    alpha a↔c gamma.
End GalloisEmbeddingPartialSuffix.

Partial Trace Semantics

We can generalize these finite traces as finite/infinite traces. We will see that one can see finite semantics as abstraction of finite/infinite one.
TODO : for now, we can't define lenght correctly because we would need ordinals. It's not very important for our purpose here. But I prefer to wait to define ordinals before digging into this.
Module PartialTrace
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
    <: TraceSig Ty State Transitions.
In this semantic, we will need to use sequences of states (that is, finite or infinite sequences, which we represent here as list resp. streams here).
  Definition trace : Type := Sequence state.
Hence, we need to change a bit functions operating on traces. TODO : use ordinals
  Definition length (t : trace) : nat :=
    match t with
    | Finite t ⇒ List.length t
    | Infinite t ⇒ 0
    end.

  Definition cons := @ConsSeq state.

  Definition epsilon : trace := Finite nil.
  Definition Sigma (t : trace) : Prop := length t = 1.
  Definition to_traces (S : state → Prop) : trace → Prop :=
    fun t ⇒ match t with Finite [x] ⇒ S x | _ ⇒ False end.
  Definition trace_transitions : trace → Prop :=
    fun t ⇒ match t with Finite [x; y] ⇒ transition x y | _ ⇒ False end.

  Definition concat (t1 t2 : trace) : trace :=
    match t1, t2 with
    | Finite l1, Finite l2 ⇒ Finite (l1 ++ l2)
    | Finite l, Infinite s ⇒ Infinite (stream_concat l s)
    | Infinite s, _ ⇒ Infinite s
    end.
  Definition junction (t1 t2 : trace) : trace :=
    match t2 with
    | Finite [] ⇒ Finite []
    | Finite (x::t2s) ⇒
      match last_error t1 with
        | NoneSeq ⇒ Finite []
        | SomeSeq y ⇒ if State.eqb x y then concat t1 (Finite t2s)
                    else Finite []
        | NonfiniteSeq ⇒ t1
      end
    | Infinite (Cons x t2s) ⇒
      match last_error t1 with
        | NoneSeq ⇒ Finite []
        | SomeSeq y ⇒ if State.eqb x y then concat t1 (Infinite t2s)
                    else Finite []
        | NonfiniteSeq ⇒ t1
      end
    end.

For this semantics, inclusion no longer suffices as a partial order. Indeed, one can see this semantics as the union of finite and infinite traces. For finite traces, everything works as for finite partial traces, and the inclusion order works well. For infinite traces however, one can see the semantics as a greatest fixpoint on inclusion. To go back to least fixpoint, one has to use the corresponding co order. The nice thing is that these two orders operate on two conceptually different sets. Thus we can combine them into one order on finite/infinite trace, which will give us back our least fixpoint semantics.
  Parameter r : Relation (trace → Prop).
  Instance set_traces_pre_order : PreOrder r.
  Admitted.
  Instance set_traces_partial_order : PartialOrder eq r.
  Admitted.

  Include TraceSet Ty State Transitions.
End PartialTrace.

Maximal Trace Semantics

To conclude with, remark that partial traces lose precision from the "real" semantic of a program in one simple way : they do not include informations about whether or not they end in final states, or begin in initial states.
Let add these informations, which will conclude our travel in the world of semantics with this very general semantic : maximal trace semantics.
Note that one of the only way to get more precise is to consider a semantic about set of set of traces, in order to deal with properties such as determinism of executions.
Module MaximalTrace
  (Ty : Typ) (State : HasEqb Ty)
  (Import Transitions : TransitionSystem Ty State)
    <: TraceSig Ty State Transitions.
  Definition trace : Type := Sequence state.
  Definition length (t : trace) : nat :=
    match t with
    | Finite t ⇒ List.length t
    | Infinite t ⇒ 0
    end.

  Definition cons := @ConsSeq state.

  Definition epsilon : trace := Finite nil.
  Definition Sigma (t : trace) : Prop := length t = 1.
  Definition to_traces (S : state → Prop) : trace → Prop :=
    fun t ⇒ match t with Finite [x] ⇒ S x | _ ⇒ False end.
  Definition trace_transitions : trace → Prop :=
    fun t ⇒ match t with Finite [x; y] ⇒ transition x y | _ ⇒ False end.

  Definition concat (t1 t2 : trace) : trace :=
    match t1, t2 with
    | Finite l1, Finite l2 ⇒ Finite (l1 ++ l2)
    | Finite l, Infinite s ⇒ Infinite (stream_concat l s)
    | Infinite s, _ ⇒ Infinite s
    end.
  Definition junction (t1 t2 : trace) : trace :=
    match t2 with
    | Finite [] ⇒ Finite []
    | Finite (x::t2s) ⇒
      match last_error t1 with
        | NoneSeq ⇒ Finite []
        | SomeSeq y ⇒ if State.eqb x y then concat t1 (Finite t2s)
                    else Finite []
        | NonfiniteSeq ⇒ t1
      end
    | Infinite (Cons x t2s) ⇒
      match last_error t1 with
        | NoneSeq ⇒ Finite []
        | SomeSeq y ⇒ if State.eqb x y then concat t1 (Infinite t2s)
                    else Finite []
        | NonfiniteSeq ⇒ t1
      end
    end.

  Parameter r : Relation (trace → Prop).
  Instance set_traces_pre_order : PreOrder r.
  Admitted.
  Instance set_traces_partial_order : PartialOrder eq r.
  Admitted.

  Include TraceSet Ty State Transitions.
End MaximalTrace.