Star.agda 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. module Star where
  2. open import Prelude
  3. infixr 40 _•_ _++_
  4. infixl 30 _on_
  5. infixr 20 _==>_ _=[_]=>_
  6. data Star {X : Set} (R : Rel X) : Rel X where
  7. ε : {x : X} -> Star R x x
  8. _•_ : {x y z : X} -> R x y -> Star R y z -> Star R x z
  9. _++_ : {X : Set}{R : Rel X}{x y z : X} ->
  10. Star R x y -> Star R y z -> Star R x z
  11. ε ++ ys = ys
  12. (x • xs) ++ ys = x • (xs ++ ys)
  13. _==>_ : {X : Set} -> Rel X -> Rel X -> Set
  14. R ==> S = forall {a b} -> R a b -> S a b
  15. _on_ : {X Y : Set} -> (R : Rel X) -> (f : Y -> X) -> Rel Y
  16. R on f = \a b -> R (f a) (f b)
  17. _=[_]=>_ : {X Y : Set} (R : Rel X) (f : X -> Y) (S : Rel Y) -> Set
  18. R =[ f ]=> S = R ==> S on f
  19. return : {X : Set}{R : Rel X} -> R ==> Star R
  20. return x = x • ε
  21. module JoinMap where
  22. join : {X : Set}{R : Rel X} -> Star (Star R) ==> Star R
  23. join ε = ε
  24. join (xs • xss) = xs ++ join xss
  25. map : forall {X Y R S} -> (f : X -> Y) ->
  26. R =[ f ]=> S -> Star R =[ f ]=> Star S
  27. map f pm ε = ε
  28. map f pm (x • xs) = pm x • map f pm xs
  29. bind : forall {X Y R S} -> (f : X -> Y) ->
  30. R =[ f ]=> Star S -> Star R =[ f ]=> Star S
  31. bind f k m = join (map f k m)
  32. bind : forall {X Y R S} -> (f : X -> Y) ->
  33. R =[ f ]=> Star S -> Star R =[ f ]=> Star S
  34. bind f k ε = ε
  35. bind f k (x • xs) = k x ++ bind f k xs
  36. join : {X : Set}{R : Rel X} -> Star (Star R) ==> Star R
  37. join = bind id id
  38. map : forall {X Y R S} -> (f : X -> Y) ->
  39. R =[ f ]=> S -> Star R =[ f ]=> Star S
  40. map f k = bind f (return · k)
  41. -- Generic length
  42. length : {X : Set}{R : Rel X} -> Star R =[ ! ]=> Star One
  43. length = map ! !
  44. -- Reverse
  45. _op : {X : Set} -> Rel X -> Rel X
  46. (R op) a b = R b a
  47. reverse : {X : Set}{R : Rel X}{a b : X} -> Star R a b -> Star (R op) b a
  48. reverse {X}{R} xs = rev xs ε
  49. where
  50. rev : forall {a b c} ->
  51. Star R a b -> Star (R op) a c -> Star (R op) b c
  52. rev ε ys = ys
  53. rev (x • xs) ys = rev xs (x • ys)