List.agda 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. module List where
  2. data Bool : Set where
  3. true : Bool
  4. false : Bool
  5. if_then_else_ : {A : Set} -> Bool -> A -> A -> A
  6. if true then x else y = x
  7. if false then x else y = y
  8. boolElim : (P : Bool -> Set) -> P true -> P false -> (b : Bool) -> P b
  9. boolElim P t f true = t
  10. boolElim P t f false = f
  11. data False : Set where
  12. data True : Set where
  13. tt : True
  14. data Or (A B : Set) : Set where
  15. inl : (a : A) -> Or A B
  16. inr : (b : B) -> Or A B
  17. orElim : {A B : Set}
  18. -> (C : Or A B -> Set)
  19. -> (cl : (a : A) -> C(inl a))
  20. -> (cr : (b : B) -> C(inr b))
  21. -> (ab : Or A B)
  22. -> C ab
  23. orElim {A} {B} C cl cr (inl a) = cl a
  24. orElim {A} {B} C cl cr (inr b) = cr b
  25. data Rel(A : Set) : Set1 where
  26. rel : (A -> A -> Set) -> Rel A
  27. _is_than_ : {A : Set} -> A -> Rel A -> A -> Set
  28. x is rel f than y = f x y
  29. data Acc {A : Set} (less : Rel A) (x : A) : Set where
  30. acc : ((y : A) -> y is less than x -> Acc less y) -> Acc less x
  31. data WO {A : Set} (less : Rel A) : Set where
  32. wo : ((x : A) -> Acc less x) -> WO less
  33. data Nat : Set where
  34. Z : Nat
  35. S : Nat -> Nat
  36. eqNat : Nat -> Nat -> Set
  37. eqNat Z Z = True
  38. eqNat (S m) (S n) = eqNat m n
  39. eqNat _ _ = False
  40. substEqNat : (P : Nat -> Set) -> (a b : Nat) -> (e : eqNat a b) -> (P a) -> P b
  41. substEqNat P Z Z e pa = pa
  42. substEqNat P (S x) (S x') e pa = substEqNat (\n -> P(S n)) x x' e pa
  43. ltNat : Nat -> Nat -> Set
  44. ltNat Z Z = False
  45. ltNat Z (S n) = True
  46. ltNat (S m) (S n) = ltNat m n
  47. ltNat (S m) Z = False
  48. ltZ-elim : (n : Nat) -> ltNat n Z -> {whatever : Set} -> whatever
  49. ltZ-elim Z ()
  50. ltZ-elim (S _) ()
  51. transNat : (x y z : Nat) -> ltNat x y -> ltNat y z -> ltNat x z
  52. transNat x y Z x<y y<z = ltZ-elim y y<z
  53. transNat x Z (S z) x<Z Z<Sz = ltZ-elim x x<Z
  54. transNat Z (S y) (S z) tt y<z = tt
  55. transNat (S x) (S y) (S z) x<y y<z = transNat x y z x<y y<z
  56. ltNat-S-Lemma : (x : Nat) -> ltNat x (S x)
  57. ltNat-S-Lemma Z = tt
  58. ltNat-S-Lemma (S x) = ltNat-S-Lemma x
  59. ltNat-S-Lemma2 : (y z : Nat) -> ltNat y (S z) -> Or (eqNat z y) (ltNat y z)
  60. ltNat-S-Lemma2 Z Z h = inl tt
  61. ltNat-S-Lemma2 Z (S x) h = inr tt
  62. ltNat-S-Lemma2 (S x) (S x') h = ltNat-S-Lemma2 x x' h
  63. ltNatRel : Rel Nat
  64. ltNatRel = rel ltNat
  65. less = ltNatRel
  66. acc-Lemma1 : (x y : Nat) -> Acc less x -> eqNat x y -> Acc less y
  67. acc-Lemma1 x y a e = substEqNat (Acc less ) x y e a
  68. acc-Lemma2 : (x y : Nat) -> Acc less x -> ltNat y x -> Acc less y
  69. acc-Lemma2 x y (acc h) l = h y l
  70. -- postulate woltNat' : (n : Nat) -> Acc less n
  71. woltNat' : (n : Nat) -> Acc less n
  72. woltNat' Z = acc (\y y<Z -> ltZ-elim y y<Z)
  73. woltNat' (S x) = acc (\y y<Sx -> orElim (\w -> Acc less y) (\e -> substEqNat (Acc less ) x y e (woltNat' x ) ) (acc-Lemma2 x y (woltNat' x)) (ltNat-S-Lemma2 y x y<Sx ))
  74. woltNat : WO ltNatRel
  75. woltNat = wo woltNat'
  76. postulate le : Nat -> Nat -> Bool
  77. postulate gt : Nat -> Nat -> Bool
  78. data List (A : Set) : Set where
  79. nil : List A
  80. cons : (x : A) -> (xs : List A) -> List A
  81. _++_ : {A : Set} -> List A -> List A -> List A
  82. nil ++ ys = ys
  83. (cons x xs) ++ ys = cons x (xs ++ ys)
  84. length : {A : Set} -> List A -> Nat
  85. length nil = Z
  86. length (cons x xs) = S (length xs)
  87. filter : {A : Set} -> (A -> Bool) -> List A -> List A
  88. filter p nil = nil
  89. filter p (cons x xs) = if p x then cons x rest else rest
  90. where rest = filter p xs
  91. filterLemma
  92. : {A : Set} -> (p : A -> Bool) -> (xs : List A)
  93. -> length (filter p xs) is less than S (length xs)
  94. filterLemma p nil = tt
  95. filterLemma p (cons x xs) =
  96. boolElim (\px -> length (if px then cons x (filter p xs)
  97. else (filter p xs))
  98. is less than S (S (length xs)))
  99. (filterLemma p xs)
  100. (transNat (length (filter p xs)) (S (length xs)) (S (S (length xs)))
  101. (filterLemma p xs) (ltNat-S-Lemma (length xs )))
  102. (p x)
  103. qs : List Nat -> List Nat
  104. qs nil = nil
  105. qs (cons x xs) = qs (filter (le x) xs)
  106. ++ cons x nil
  107. ++ qs (filter (gt x) xs)
  108. data Measure : Set1 where
  109. μ : {M : Set} -> {rel : Rel M} -> (ord : WO rel) -> Measure
  110. down1-measure : Measure
  111. down1-measure = μ woltNat
  112. qs-2-1-hint = \(x : Nat) -> \(xs : List Nat) -> filterLemma (le x) xs
  113. qs-2-2-hint = \(x : Nat) -> \(xs : List Nat) -> filterLemma (gt x) xs