Prelude.agda 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. module Prelude where
  2. infix 20 _≡_ _≤_ _∈_
  3. infixl 60 _,_ _++_ _+_ _◄_ _◄²_
  4. _∘_ : {A B : Set}{C : B -> Set}(f : (x : B) -> C x)(g : A -> B)(x : A) -> C (g x)
  5. (f ∘ g) x = f (g x)
  6. data _≡_ {A : Set}(x : A) : {B : Set} -> B -> Set where
  7. refl : x ≡ x
  8. cong : {A : Set}{B : A -> Set}(f : (z : A) -> B z){x y : A} ->
  9. x ≡ y -> f x ≡ f y
  10. cong f refl = refl
  11. subst : {A : Set}(P : A -> Set){x y : A} -> x ≡ y -> P y -> P x
  12. subst P refl px = px
  13. sym : {A : Set}{x y : A} -> x ≡ y -> y ≡ x
  14. sym refl = refl
  15. data Nat : Set where
  16. zero : Nat
  17. suc : Nat -> Nat
  18. _+_ : Nat -> Nat -> Nat
  19. zero + m = m
  20. suc n + m = suc (n + m)
  21. {-# BUILTIN NATURAL Nat #-}
  22. {-# BUILTIN NATPLUS _+_ #-}
  23. data _≤_ : Nat -> Nat -> Set where
  24. leqZ : {m : Nat} -> zero ≤ m
  25. leqS : {n m : Nat} -> n ≤ m -> suc n ≤ suc m
  26. refl-≤ : {n : Nat} -> n ≤ n
  27. refl-≤ {zero } = leqZ
  28. refl-≤ {suc n} = leqS refl-≤
  29. refl-≤' : {n m : Nat} -> n ≡ m -> n ≤ m
  30. refl-≤' refl = refl-≤
  31. trans-≤ : {x y z : Nat} -> x ≤ y -> y ≤ z -> x ≤ z
  32. trans-≤ leqZ yz = leqZ
  33. trans-≤ (leqS xy) (leqS yz) = leqS (trans-≤ xy yz)
  34. lem-≤suc : {x : Nat} -> x ≤ suc x
  35. lem-≤suc {zero } = leqZ
  36. lem-≤suc {suc x} = leqS lem-≤suc
  37. lem-≤+L : (x : Nat){y : Nat} -> y ≤ x + y
  38. lem-≤+L zero = refl-≤
  39. lem-≤+L (suc x) = trans-≤ (lem-≤+L x) lem-≤suc
  40. lem-≤+R : {x y : Nat} -> x ≤ x + y
  41. lem-≤+R {zero } = leqZ
  42. lem-≤+R {suc x} = leqS lem-≤+R
  43. data List (A : Set) : Set where
  44. ε : List A
  45. _,_ : List A -> A -> List A
  46. _++_ : {A : Set} -> List A -> List A -> List A
  47. xs ++ ε = xs
  48. xs ++ (ys , y) = (xs ++ ys) , y
  49. data All {A : Set}(P : A -> Set) : List A -> Set where
  50. ∅ : All P ε
  51. _◄_ : forall {xs x} -> All P xs -> P x -> All P (xs , x)
  52. {-
  53. data Some {A : Set}(P : A -> Set) : List A -> Set where
  54. hd : forall {x xs} -> P x -> Some P (xs , x)
  55. tl : forall {x xs} -> Some P xs -> Some P (xs , x)
  56. -}
  57. data _∈_ {A : Set}(x : A) : List A -> Set where
  58. hd : forall {xs} -> x ∈ xs , x
  59. tl : forall {y xs} -> x ∈ xs -> x ∈ xs , y
  60. _!_ : {A : Set}{P : A -> Set}{xs : List A} ->
  61. All P xs -> {x : A} -> x ∈ xs -> P x
  62. ∅ ! ()
  63. (xs ◄ x) ! hd = x
  64. (xs ◄ x) ! tl i = xs ! i
  65. tabulate : {A : Set}{P : A -> Set}{xs : List A} ->
  66. ({x : A} -> x ∈ xs -> P x) -> All P xs
  67. tabulate {xs = ε} f = ∅
  68. tabulate {xs = xs , x} f = tabulate (f ∘ tl) ◄ f hd
  69. data All² {I : Set}{A : I -> Set}(P : {i : I} -> A i -> Set) :
  70. {is : List I} -> All A is -> Set where
  71. ∅² : All² P ∅
  72. _◄²_ : forall {i is}{x : A i}{xs : All A is} ->
  73. All² P xs -> P x -> All² P (xs ◄ x)
  74. data _∈²_ {I : Set}{A : I -> Set}{i : I}(x : A i) :
  75. {is : List I} -> All A is -> Set where
  76. hd² : forall {is}{xs : All A is} -> x ∈² xs ◄ x
  77. tl² : forall {j is}{y : A j}{xs : All A is} ->
  78. x ∈² xs -> x ∈² xs ◄ y