PureConstructions.v 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. (* SPDX-License-Identifier: GPL-2.0 or GPL-3.0
  2. Copyright © 2019 Ariadne Devos *)
  3. Require Import Coq.Program.Basics.
  4. Require Import S.Lucid.Lift.
  5. (* Define some pure ‘glue’ arrows, manipulating
  6. pre- and post-conditions and reordering data.
  7. They are not that useful on theirselves, but
  8. are great for composition. *)
  9. Section pure_constructions.
  10. (* The indexed pure identity arrow.
  11. The precondition P must imply the postcondition Q.
  12. When sequenced, this can be used to transform
  13. the pre- and post-conditions. *)
  14. Program Definition hoareA_sig_pure A (P : A -> Prop) (Q : A -> Prop) (sense : forall a : A, P a -> Q a) : PureSig A A
  15. := {| P := P;
  16. Q := Q;
  17. f := id |}.
  18. (* The indexed pure identity arrow,
  19. with equal pre- and post-conditions *)
  20. Program Definition identityA_sig_pure0 A P : PureSig A A := hoareA_sig_pure A P P _.
  21. (* Reverse an input and output pair and the condition *)
  22. Program Definition revA_sig_pure A B P : PureSig (A * B) (B * A)
  23. := {| P := P;
  24. Q := prod_curry (flip (prod_uncurry P));
  25. f := prod_curry (flip pair) |}.
  26. Definition assoc_pairs A B C : A * (B * C) -> (A * B) * C
  27. := fun p => match p with (a, (b, c)) => ((a, b), c) end.
  28. Definition assoc_pairs_reverse A B C : (A * B) * C -> A * (B * C)
  29. := fun p => match p with ((a, b), c) => (a, (b, c)) end.
  30. (* Change the association of nested products.
  31. This can be neccessary due to use of parA and seqA. *)
  32. Program Definition assocA_sig_pure A B C (P : (A * (B * C)) -> Prop) : PureSig (A * (B * C)) ((A * B) * C)
  33. := {| P := P;
  34. Q := compose P (assoc_pairs_reverse A B C);
  35. f := assoc_pairs A B C; |}.
  36. Program Definition assocA_sig_pure_reverse A B C (P : (A * B) * C -> Prop) : PureSig ((A * B) * C) (A * (B * C))
  37. := {| P := P;
  38. Q := compose P (assoc_pairs A B C);
  39. f := assoc_pairs_reverse A B C; |}.
  40. End pure_constructions.