Path.agda 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. module Path where
  2. open import Basics hiding (_==_)
  3. open import Proc
  4. import Graph
  5. private open module G = Graph Nat
  6. data Node : Set where
  7. node : Nat -> Node
  8. stop : Node
  9. _==_ : Node -> Node -> Bool
  10. stop == stop = true
  11. node zero == node zero = true
  12. node (suc n) == node (suc m) = node n == node m
  13. _ == _ = false
  14. data U : Set where
  15. int : U
  16. ext : U
  17. data Name : Set where
  18. fwd-edge : Nat -> Nat -> Name
  19. bwd-edge : Nat -> Node -> Name
  20. start : Nat -> Name
  21. finish : Nat -> Name
  22. N : U -> Set
  23. N int = Name
  24. N ext = False
  25. data Msg : Set where
  26. forward : Node -> Node -> Msg
  27. backward : Node -> Msg
  28. T : U -> Set
  29. T int = Msg
  30. T ext = Node
  31. private
  32. module Impl where
  33. private module P = ProcDef U T N
  34. open P hiding (_!_)
  35. P = Proc int
  36. infixr 40 _!_
  37. _!_ : Msg -> P -> P
  38. m ! p = P._!_ (lift m) p
  39. fwd : Nat -> Nat -> Msg
  40. fwd from to = forward (node from) (node to)
  41. fwd-runner : Nat -> Nat -> P
  42. fwd-runner from to = > react
  43. where
  44. react : Msg -> P
  45. react (forward from' to') =
  46. if to' == node from
  47. then fwd from to ! def (bwd-edge from from')
  48. else def (fwd-edge from to)
  49. react (backward _) = o
  50. bwd-runner : Nat -> Node -> P
  51. bwd-runner from w = > react
  52. where
  53. react : Msg -> P
  54. react (backward n) =
  55. if n == w then o
  56. else if n == node from
  57. then backward w ! o
  58. else def (bwd-edge from w)
  59. react (forward _ _) = def (bwd-edge from w)
  60. pitcher : Nat -> P
  61. pitcher n = forward stop (node n) ! o
  62. batter : Nat -> P
  63. batter n = > react
  64. where
  65. react : Msg -> P
  66. react (forward from to) =
  67. if to == node n
  68. then backward from ! o
  69. else def (start n)
  70. react _ = def (start n)
  71. env : Env
  72. env int (fwd-edge from to) = fwd-runner from to
  73. env int (bwd-edge from w) = bwd-runner from w
  74. env int (start n) = batter n
  75. env int (finish n) = pitcher n
  76. env ext ()
  77. edges : Graph -> P
  78. edges [] = o
  79. edges (edge x y :: G) = def (fwd-edge x y) || edges G
  80. φ : Tran ext int
  81. φ = record { upV = up; downV = down }
  82. where
  83. down : Node -> Lift Msg
  84. down x = lift (backward x)
  85. up : Msg -> Lift Node
  86. up (forward _ _) = bot
  87. up (backward x) = lift x
  88. main : Graph -> Nat -> Nat -> Proc ext
  89. main G x y = φ /| def (finish y) || def (start x) || edges G
  90. open Impl public
  91. param : Param
  92. param = record
  93. { U = U
  94. ; T = T
  95. ; Name = N
  96. ; env = env
  97. }