Arrow.v 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. (* Copyright © 2019 Ariadne Devos
  2. SPDX-License-Identifier: GPL-2.0 or GPL-3.0 *)
  3. Require Import Coq.Classes.Equivalence.
  4. Require Import Coq.Relations.Relations.
  5. (* Arrows are generalisations of functions.
  6. They can be composed like functions, but
  7. are looser. They may have certain side-effects,
  8. or be reversible.
  9. A few different arrows are used in S²:
  10. Gallina functions, Gallina functions over
  11. subset types abstracted over their predicate,
  12. stateful arrows, the pre- post- and frame-
  13. conditions of stateful arrows. *)
  14. (* Define concepts by formulating rewrite rules *)
  15. (* First, very general. Sequential and parallel composition,
  16. with possibly arbitrary limitations on data values and
  17. no lifting from Gallina functions. These restrictions
  18. may be useful for reification. The semantics of parallel
  19. composition are intentionally left mostly undefined. *)
  20. Record arrow_info := {
  21. (* D: the set of data types *)
  22. D : Type;
  23. (* D2: the product of two data types *)
  24. D2 : D -> D -> D;
  25. (* the arrow type, from an input to an output. *)
  26. Arr : D -> D -> Type;
  27. }.
  28. (* Operations common to all arrows.
  29. Note that data values cannot be reordered, cloned or deleted.
  30. This may be important performance-wise in some applications
  31. (e.g. array values, quantum computing, reversible computing).
  32. Also note that there is no lifting funtion from Gallina
  33. to Arr i. This is because one might to reason by reification,
  34. or extract or manipulate a program to let it run on bare
  35. software. If lifting is allowable, see `arrow_lift_ops`.
  36. Another difference from Haskell: the parallel composition
  37. operator, might, in fact, be parallel (including interleaving).
  38. Or it might just always be in a particular order. *)
  39. Record arrow_ops (i : arrow_info) := {
  40. (* the identity, do-nothing arrow *)
  41. idA : forall A, Arr i A A;
  42. (* reassociate data values (1) *)
  43. assocA_r : forall A B C, Arr i (D2 i (D2 i A B) C) (D2 i A (D2 i B C));
  44. (* reassociate data values (2) *)
  45. assocA_l : forall A B C, Arr i (D2 i A (D2 i B C)) (D2 i (D2 i A B) C);
  46. (* sequential composition *)
  47. seqA : forall A B C, Arr i A B -> Arr i B C -> Arr i A C;
  48. (* parallel composition,
  49. not necessarily executed in any particular order
  50. (possibly even interleaved) *)
  51. parA : forall A B C D, Arr i A B -> Arr i C D -> Arr i (D2 i A C) (D2 i B D);
  52. (* are two arrows considered equivalent? *)
  53. eqvA : forall A B, relation (Arr i A B);
  54. }.
  55. Arguments idA { _ } a { _ }.
  56. Arguments assocA_r { _ } a A B C.
  57. Arguments assocA_l { _ } a A B C.
  58. Arguments seqA { _ } a { _ _ _ }.
  59. Arguments parA { _ } a { _ _ _ _ }.
  60. Arguments eqvA { _ } a { _ _ }.
  61. Record arrow_lawful i o := {
  62. (* the equivalence relation is, in fact, an equivalence *)
  63. equivA : forall A B, Equivalence (eqvA (i := i) o (A := A) (B := B));
  64. (* the reassociations cancel each other *)
  65. seqA_assocA_rl : forall A B C, eqvA o (seqA o (assocA_r o A B C) (assocA_l o A B C)) (idA o);
  66. seqA_assocA_lr : forall A B C, eqvA o (seqA o (assocA_l o A B C) (assocA_r o A B C)) (idA o);
  67. (* TODO: monoid laws *)
  68. (* the identity arrow can be deleted from the left in case of sequential composition *)
  69. seqA_idA_l : forall A B (k : Arr i A B), eqvA o (seqA o (idA o) k) k;
  70. seqA_idA_r : forall A B (k : Arr i A B), eqvA o (seqA o k (idA o)) k;
  71. (* sequential composition is associative *)
  72. seqA_assoc : forall A B C D (k : Arr i A B) (l : Arr i B C) (m : Arr i C D),
  73. eqvA o (seqA o (seqA o k l) m) (seqA o k (seqA o l m));
  74. (* there are no rules for parallel composition in
  75. the general case *)
  76. }.
  77. Arguments equivA { _ _ } a _ _.
  78. Record arrow_basic := {
  79. basicA_info : arrow_info;
  80. basicA_ops : arrow_ops basicA_info;
  81. basicA_laws : arrow_lawful basicA_info basicA_ops
  82. }.
  83. Coercion basicA_info : arrow_basic >-> arrow_info.
  84. Coercion basicA_ops : arrow_basic >-> arrow_ops.
  85. Coercion basicA_laws : arrow_basic >-> arrow_lawful.
  86. (* Sequence three arrows *)
  87. Definition seqA3 (i : arrow_basic) A B C D (x : Arr i A B) (y : Arr i B C) (z : Arr i C D) : Arr i A D
  88. := seqA i x (seqA i y z).
  89. Arguments seqA3 _ { _ _ _ _ }.