Warshall.agda 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. module Warshall
  2. (X : Set)
  3. ((≤) : X -> X -> Prop)
  4. -- and axioms...
  5. where
  6. id : {A:Set} -> A -> A
  7. id x = x
  8. (∘) : {A B C:Set} -> (B -> C) -> (A -> B) -> A -> C
  9. f ∘ g = \x -> f (g x)
  10. -- Natural numbers --------------------------------------------------------
  11. data Nat : Set where
  12. zero : Nat
  13. suc : Nat -> Nat
  14. (+) : Nat -> Nat -> Nat
  15. zero + m = m
  16. suc n + m = suc (n + m)
  17. -- Finite sets ------------------------------------------------------------
  18. data Zero : Set where
  19. data Suc (A:Set) : Set where
  20. fzero_ : Suc A
  21. fsuc_ : A -> Suc A
  22. mutual
  23. data Fin (n:Nat) : Set where
  24. finI : Fin_ n -> Fin n
  25. Fin_ : Nat -> Set
  26. Fin_ zero = Zero
  27. Fin_ (suc n) = Suc (Fin n)
  28. fzero : {n:Nat} -> Fin (suc n)
  29. fzero = finI fzero_
  30. fsuc : {n:Nat} -> Fin n -> Fin (suc n)
  31. fsuc i = finI (fsuc_ i)
  32. finE : {n:Nat} -> Fin n -> Fin_ n
  33. finE (finI i) = i
  34. infixr 15 ::
  35. -- Vectors ----------------------------------------------------------------
  36. data Nil : Set where
  37. nil_ : Nil
  38. data Cons (Xs:Set) : Set where
  39. cons_ : X -> Xs -> Cons Xs
  40. mutual
  41. data Vec (n:Nat) : Set where
  42. vecI : Vec_ n -> Vec n
  43. Vec_ : Nat -> Set
  44. Vec_ zero = Nil
  45. Vec_ (suc n) = Cons (Vec n)
  46. nil : Vec zero
  47. nil = vecI nil_
  48. (::) : {n:Nat} -> X -> Vec n -> Vec (suc n)
  49. x :: xs = vecI (cons_ x xs)
  50. vecE : {n:Nat} -> Vec n -> Vec_ n
  51. vecE (vecI xs) = xs
  52. vec : (n:Nat) -> X -> Vec n
  53. vec zero _ = nil
  54. vec (suc n) x = x :: vec n x
  55. map : {n:Nat} -> (X -> X) -> Vec n -> Vec n
  56. map {zero} f (vecI nil_) = nil
  57. map {suc n} f (vecI (cons_ x xs)) = f x :: map f xs
  58. (!) : {n:Nat} -> Vec n -> Fin n -> X
  59. (!) {suc n} (vecI (cons_ x _ )) (finI fzero_) = x
  60. (!) {suc n} (vecI (cons_ _ xs)) (finI (fsuc_ i)) = xs ! i
  61. upd : {n:Nat} -> Fin n -> X -> Vec n -> Vec n
  62. upd {suc n} (finI fzero_) x (vecI (cons_ _ xs)) = x :: xs
  63. upd {suc n} (finI (fsuc_ i)) x (vecI (cons_ y xs)) = y :: upd i x xs
  64. tabulate : {n:Nat} -> (Fin n -> X) -> Vec n
  65. tabulate {zero} f = nil
  66. tabulate {suc n} f = f fzero :: tabulate (\x -> f (fsuc x))
  67. postulate
  68. (===) : {n:Nat} -> Vec n -> Vec n -> Prop
  69. module Proof
  70. (F : {n:Nat} -> Vec n -> Vec n)
  71. -- and axioms...
  72. where
  73. stepF : {n:Nat} -> Fin n -> Vec n -> Vec n
  74. stepF i xs = upd i (F xs ! i) xs
  75. unsafeF' : {n:Nat} -> Nat -> Vec (suc n) -> Vec (suc n)
  76. unsafeF' zero = id
  77. unsafeF' (suc m) = unsafeF' m ∘ stepF fzero
  78. unsafeF : {n:Nat} -> Vec n -> Vec n
  79. unsafeF {zero} = id
  80. unsafeF {suc n} = unsafeF' (suc n)
  81. thm : {n:Nat} -> (xs:Vec n) -> F xs === unsafeF xs
  82. thm = ?