Modal.agda 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. module Modal where
  2. open import Prelude
  3. open import Star
  4. data Progress (A : Set) : Set where
  5. cont : A -> Progress A
  6. stop : Progress A
  7. record Some {A : Set}(R : Rel A) : Set where
  8. field
  9. a : A
  10. b : A
  11. edge : R a b
  12. some : {A : Set}{R : Rel A}{a b : A} -> R a b -> Some R
  13. some x = record {a = _; b = _; edge = x}
  14. EdgePred : {A : Set} -> Rel A -> Set1
  15. EdgePred R = forall {a b} -> R a b -> Set
  16. data PStep {A : Set}{R : Rel A}(P : EdgePred R) :
  17. Rel (Progress (Some (Star R))) where
  18. step : {a b c : A}{x : R a b}{xs : Star R b c} ->
  19. PStep P (cont (some (x • xs))) (cont (some xs))
  20. done : {a b c : A}{x : R a b}{xs : Star R b c} ->
  21. P x -> PStep P (cont (some (x • xs))) stop
  22. Any : {A : Set}{R : Rel A}(P : EdgePred R) -> EdgePred (Star R)
  23. Any P xs = Star (PStep P) (cont (some xs)) stop
  24. mapAny : {A₁ A₂ : Set}{R₁ : Rel A₁}{R₂ : Rel A₂}
  25. {P₁ : EdgePred R₁}{P₂ : EdgePred R₂}{a b : A₁}{xs : Star R₁ a b}
  26. {i : A₁ -> A₂}{f : R₁ =[ i ]=> R₂} ->
  27. ({a b : A₁}{x : R₁ a b} -> P₁ x -> P₂ (f x)) ->
  28. Any P₁ xs -> Any (\{a b} -> P₂{a}{b}) (map i f xs)
  29. mapAny h (step • i) = step • mapAny h i
  30. mapAny h (done p • ε) = done (h p) • ε
  31. mapAny h (done p • (() • _))
  32. data Check {A : Set}{R : Rel A}(P : EdgePred R) :
  33. Rel (Some (Star R)) where
  34. check : {a b c : A}{x : R a b}{xs : Star R b c} ->
  35. P x -> Check P (some (x • xs)) (some xs)
  36. checkedEdge : {A : Set}{R : Rel A}{P : EdgePred R}{xs ys : Some (Star R)} ->
  37. Check P xs ys -> Some R
  38. checkedEdge (check {x = x} _) = some x
  39. uncheck : {X : Set}{R : Rel X}{P : EdgePred R}{xs ys : Some (Star R)}
  40. (chk : Check P xs ys) -> P (Some.edge (checkedEdge chk))
  41. uncheck (check p) = p
  42. All : {A : Set}{R : Rel A}(P : EdgePred R) -> EdgePred (Star R)
  43. All P {a}{b} xs = Star (Check P) (some xs) (some {a = b} ε)
  44. data Lookup {A : Set}{R : Rel A}(P Q : EdgePred R) : Set where
  45. result : {a b : A} -> (x : R a b) -> P x -> Q x -> Lookup P Q
  46. lookup : {A : Set}{R : Rel A}{P Q : EdgePred R}{a b : A}{xs : Star R a b} ->
  47. Any P xs -> All Q xs -> Lookup (\{a b} -> P{a}{b}) Q
  48. lookup (step • i) (check _ • xs) = lookup i xs
  49. lookup (done p • ε) (check q • _ ) = result _ p q
  50. lookup (done p • (() • _)) (check q • _ )