Trace.v 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. (* Copyright © 2019 Ariadne Devos
  2. SPDX-License-Identifier: GPL-2.0 or GPL-3.0 *)
  3. Require Import Coq.Setoids.Setoid.
  4. (* Traces can be finite (like lists) or infintie (like streams).
  5. Equality of each element is 'eq', not an arbitrary
  6. equivalence relationship. *)
  7. Section trace.
  8. (* Presumably an event, can be anything *)
  9. Variable A : Type.
  10. CoInductive trace :=
  11. | Done : trace
  12. | More : A -> trace -> trace.
  13. (* Witnesses of extensional equality *)
  14. CoInductive trace_eq_proof : trace -> trace -> Prop :=
  15. | EqDone : trace_eq_proof Done Done
  16. | EqMore : forall a t u, trace_eq_proof t u -> trace_eq_proof (More a t) (More a u).
  17. Axiom streameq_leibniz : forall t u, trace_eq_proof t u <-> t = u.
  18. (* Trick Coq in reducing seq.
  19. Adapted from: adam.chlipala.net/cpdt/html/Coinductive.html. *)
  20. Definition frob (t : trace) : trace :=
  21. match t with
  22. | Done => Done
  23. | More a t' => More a t'
  24. end.
  25. Lemma frob_idem (t : trace) : t = frob t.
  26. Proof.
  27. destruct t.
  28. all : reflexivity.
  29. Qed.
  30. Section Concatenation.
  31. CoFixpoint seq (u : trace) (v : trace) :=
  32. match u with
  33. | Done => v
  34. | More s u' => More s (seq u' v)
  35. end.
  36. (* Neutrality of nil *)
  37. Lemma seq_nil_l (t : trace) : seq Done t = t.
  38. Proof.
  39. set (t' := seq Done t).
  40. rewrite (frob_idem t).
  41. rewrite (frob_idem t').
  42. destruct t.
  43. all : (simpl; reflexivity).
  44. Qed.
  45. CoFixpoint seq_nil_r' (t : trace) : trace_eq_proof (seq t Done) t.
  46. Proof.
  47. refine (match t with
  48. | Done => _
  49. | More a t' => _
  50. end).
  51. - set (u := seq Done Done).
  52. rewrite (frob_idem u).
  53. simpl. constructor.
  54. - set (u := seq (More a t') Done).
  55. rewrite (frob_idem u).
  56. simpl.
  57. constructor.
  58. + apply seq_nil_r'.
  59. Qed.
  60. Definition seq_nil_r (t : trace) : seq t Done = t.
  61. Proof. rewrite <- streameq_leibniz. exact (seq_nil_r' t). Qed.
  62. (* Associativity of seq *)
  63. CoFixpoint seq_assoc' (t : trace) (u : trace) (v : trace) : trace_eq_proof (seq (seq t u) v) (seq t (seq u v)).
  64. Proof.
  65. refine (match t with
  66. | Done => _
  67. | More a t' => _
  68. end).
  69. + repeat rewrite seq_nil_l.
  70. rewrite streameq_leibniz.
  71. reflexivity.
  72. + set (x := seq (More a t') u).
  73. set (y := seq (More a t') (seq u v)).
  74. rewrite (frob_idem x).
  75. rewrite (frob_idem y).
  76. simpl.
  77. clear x y.
  78. set (x := seq (More a (seq t' u)) v).
  79. rewrite (frob_idem x).
  80. simpl.
  81. constructor.
  82. apply seq_assoc'.
  83. Qed.
  84. Lemma seq_assoc (t : trace) (u : trace) (v : trace) : seq (seq t u) v = seq t (seq u v).
  85. Proof. pose seq_assoc'. pose streameq_leibniz. firstorder. Qed.
  86. (* Therefore, seq is a monoid. *)
  87. (* induction lemma *)
  88. Theorem seq_more_lift : forall a t u, seq (More a t) u = More a (seq t u).
  89. Proof.
  90. intros.
  91. set (x := seq (More a t) u).
  92. rewrite (frob_idem x).
  93. cbn.
  94. reflexivity.
  95. Qed.
  96. End Concatenation.
  97. End trace.
  98. Section tmap.
  99. CoFixpoint tmap A B (f : A -> B) (t : trace A) : trace B :=
  100. match t with
  101. | Done _ => Done _
  102. | More _ a t' => More _ (f a) (tmap A B f t')
  103. end.
  104. Theorem tmap_empty A B f : tmap A B f (Done _) = Done _.
  105. Proof.
  106. rewrite (frob_idem _ (tmap _ _ f (Done _))).
  107. cbn.
  108. reflexivity.
  109. Qed.
  110. Theorem tmap_peel A B (f : A -> B) (a : A) (t' : trace A) : tmap _ _ f (More _ a t') = More _ (f a) (tmap _ _ f t').
  111. Proof.
  112. rewrite (frob_idem _ (tmap _ _ f (More _ _ _))).
  113. cbn.
  114. reflexivity.
  115. Qed.
  116. CoFixpoint tmap_compose' A B C (g : B -> C) (f : A -> B) (t : trace A) : trace_eq_proof _ (tmap _ _ g (tmap _ _ f t)) (tmap _ _ (Basics.compose g f) t).
  117. Proof.
  118. destruct t.
  119. + repeat rewrite tmap_empty.
  120. constructor.
  121. + repeat rewrite tmap_peel.
  122. constructor.
  123. apply tmap_compose'.
  124. Qed.
  125. Theorem tmap_compose A B C (g : B -> C) (f : A -> B) (t : trace A) : tmap _ _ g (tmap _ _ f t) = tmap _ _ (Basics.compose g f) t.
  126. Proof. rewrite <- streameq_leibniz. apply tmap_compose'. Qed.
  127. CoFixpoint tmap_merge' A B (f : A -> B) t u : trace_eq_proof _ (seq _ (tmap _ _ f t) (tmap _ _ f u)) (tmap _ _ f (seq _ t u)).
  128. Proof.
  129. destruct t.
  130. + rewrite tmap_empty.
  131. repeat rewrite seq_nil_l.
  132. rewrite streameq_leibniz.
  133. reflexivity.
  134. + rewrite seq_more_lift.
  135. repeat rewrite tmap_peel.
  136. rewrite seq_more_lift.
  137. constructor.
  138. apply tmap_merge'.
  139. Qed.
  140. Theorem tmap_merge A B (f : A -> B) t u : seq _ (tmap _ _ f t) (tmap _ _ f u) = tmap _ _ f (seq _ t u).
  141. Proof.
  142. rewrite <- streameq_leibniz.
  143. apply tmap_merge'.
  144. Qed.
  145. End tmap.