Example.agda 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. module Example where
  2. loop : Set
  3. loop = loop
  4. _∞_ : Set -> Set -> Set
  5. x ∞ y = x ∞ y
  6. data Nat : Set where
  7. zero : Nat
  8. succ : Nat -> Nat
  9. id : Nat -> Nat
  10. id zero = zero
  11. id (succ n) = succ (id n)
  12. bad : Nat -> Nat
  13. bad n = bad n
  14. _+_ : Nat -> Nat -> Nat
  15. zero + n = n
  16. (succ m) + n = succ (m + n)
  17. bad2 : Nat -> Nat
  18. bad2 (succ x) = bad2 x + bad2 (succ x)
  19. bad2 x = bad2 x
  20. data Bool : Set where
  21. true : Bool
  22. false : Bool
  23. _&&_ : Bool -> Bool -> Bool
  24. true && a = a
  25. false && a = false
  26. mutual
  27. even : Nat -> Bool
  28. even zero = true
  29. even (succ n) = odd n
  30. odd : Nat -> Bool
  31. odd zero = false
  32. odd (succ n) = even n
  33. data Ty : {_ : Nat} -> Set where
  34. Base : forall {n} -> Ty {succ n}
  35. Arr : forall {n} -> Ty {n} -> Ty {n} -> Ty {succ n}
  36. eqty : forall {n} -> Ty {n} -> Ty {n} -> Bool
  37. eqty Base Base = true
  38. eqty (Arr a b) (Arr a' b') = (eqty a a') && (eqty b b')
  39. eqty _ _ = false
  40. subty : forall {n} -> Ty {n} -> Ty {n} -> Bool
  41. subty Base Base = true
  42. subty (Arr a b) (Arr a' b') = (subty a' a) && (subty b b')
  43. subty _ _ = false
  44. -- the following is enough for making it termination check
  45. subty' : forall {n} -> Ty {n} -> Ty {n} -> Bool
  46. subty' Base Base = true
  47. subty' {succ n} (Arr a b) (Arr a' b')
  48. = (subty' a' a) && (subty' b b')
  49. subty' _ _ = false
  50. subty'' : forall {n} -> Ty {n} -> Ty {n} -> Bool
  51. subty'' Base Base = true
  52. subty'' {succ n} (Arr {.n} a b) (Arr .{n} a'' b'')
  53. = (subty'' {n} a'' a) && (subty'' {n} b b'')
  54. subty'' _ _ = false
  55. data _×_ (A B : Set) : Set where
  56. _,_ : A -> B -> A × B
  57. add : Nat × Nat -> Nat
  58. add (zero , m) = m
  59. add (succ n , m) = succ (add (n , m))
  60. eq : Nat × Nat -> Bool
  61. eq (zero , zero ) = true
  62. eq (succ n , succ m) = eq (n , m)
  63. eq _ = false
  64. -- the following should not termination check
  65. mutual
  66. f : Nat -> Nat -> Nat
  67. f zero y = zero
  68. f (succ x) zero = zero
  69. f (succ x) (succ y) = (g x (succ y)) + (f (succ (succ x)) y)
  70. g : Nat -> Nat -> Nat
  71. g zero y = zero
  72. g (succ x) zero = zero
  73. g (succ x) (succ y) = (f (succ x) (succ y)) + (g x (succ (succ y)))
  74. mutual
  75. badf : Nat × Nat -> Nat
  76. badf (zero , y) = zero
  77. badf (succ x , zero) = zero
  78. badf (succ x , succ y) = badg (x , succ y) + badf (succ (succ x) , y)
  79. badg : Nat × Nat -> Nat
  80. badg (zero , y) = zero
  81. badg (succ x , zero) = zero
  82. badg (succ x , succ y) = badf (succ x , succ y) + badg (x , succ (succ y))
  83. -- these are ok, however
  84. mutual
  85. f' : Nat -> Nat -> Nat
  86. f' zero y = zero
  87. f' (succ x) zero = zero
  88. f' (succ x) (succ y) = (g' x (succ y)) + (f' (succ (succ x)) y)
  89. g' : Nat -> Nat -> Nat
  90. g' zero y = zero
  91. g' (succ x) zero = zero
  92. g' (succ x) (succ y) = (f' (succ x) (succ y)) + (g' x (succ y))
  93. -- these are ok, however
  94. bla : Nat
  95. bla = succ (succ zero)
  96. mutual
  97. f'' : Nat -> Nat -> Nat
  98. f'' zero y = zero
  99. f'' (succ x) zero = zero
  100. f'' (succ x) (succ y) = (g'' x (succ y)) + (f'' bla y)
  101. g'' : Nat -> Nat -> Nat
  102. g'' zero y = zero
  103. g'' (succ x) zero = zero
  104. g'' (succ x) (succ y) = (f'' (succ x) (succ y)) + (g'' x (succ y))
  105. -- Ackermann
  106. ack : Nat -> Nat -> Nat
  107. ack zero y = succ y
  108. ack (succ x) zero = ack x (succ zero)
  109. ack (succ x) (succ y) = ack x (ack (succ x) y)
  110. ack' : Nat × Nat -> Nat
  111. ack' (zero , y) = succ y
  112. ack' (succ x , zero) = ack' (x , succ zero)
  113. ack' (succ x , succ y) = ack' (x , ack' (succ x , y))
  114. -- Maximum of 3 numbers
  115. max3 : Nat -> Nat -> Nat -> Nat
  116. max3 zero zero z = z
  117. max3 zero y zero = y
  118. max3 x zero zero = x
  119. max3 (succ x) (succ y) zero = succ (max3 x y zero)
  120. max3 (succ x) zero (succ z) = succ (max3 x zero z)
  121. max3 zero (succ y) (succ z) = succ (max3 zero y z)
  122. max3 (succ x) (succ y) (succ z) = succ (max3 x y z)
  123. -- addition of Ordinals
  124. data Ord : Set where
  125. ozero : Ord
  126. olim : (Nat -> Ord) -> Ord
  127. addord : Ord -> Ord -> Ord
  128. addord x ozero = x
  129. addord x (olim f) = olim (\ n -> addord x (f n))
  130. -- Higher-order example which should not pass the termination checker.
  131. -- (Not the current one, anyway.)
  132. foo : Ord -> (Nat -> Ord) -> Ord
  133. foo ozero g = ozero
  134. foo (olim f) g = olim (\n -> foo (g n) f)
  135. -- Examples checking that a function can be used with several
  136. -- different numbers of arguments on the right-hand side.
  137. const : {a b : Set1} -> a -> b -> a
  138. const x _ = x
  139. ok : Nat -> Nat -> Set
  140. ok zero y = Nat
  141. ok (succ x) y = const Nat (const (ok x y) (ok x))
  142. notOK : Set -> Set
  143. notOK x = const (notOK Ord) notOK
  144. -- An example which should fail (37 is an arbitrary number):
  145. data ⊤ : Set where
  146. tt : ⊤
  147. mutual
  148. foo37 : ⊤ -> ⊤
  149. foo37 x = bar37 x
  150. bar37 : ⊤ -> ⊤
  151. bar37 tt = foo37 tt
  152. -- Some examples involving with.
  153. -- Not OK:
  154. withNo : Nat -> Nat
  155. withNo n with n
  156. withNo n | m = withNo m
  157. -- OK:
  158. withYes : Nat -> Nat
  159. withYes n with n
  160. withYes n | zero = zero
  161. withYes n | succ m = withYes m
  162. -- Some rather convoluted examples.
  163. -- OK:
  164. number : Nat
  165. number = zero
  166. where
  167. data Foo12 : Nat -> Set where
  168. foo12 : Foo12 number
  169. -- Should the occurrence of number' in the type signature of foo12
  170. -- really be highlighted here?
  171. number' : Nat
  172. number' with zero
  173. number' | x = g12 foo12
  174. where
  175. data Foo12 : Nat -> Set where
  176. foo12 : Foo12 number'
  177. abstract
  178. g12 : {i : Nat} -> Foo12 i -> Nat
  179. g12 foo12 = zero
  180. -- Tests highlighting (but does not type check yet):
  181. -- number'' : Nat
  182. -- number'' with zero
  183. -- number'' | x = g12 (foo12 x)
  184. -- where
  185. -- data Foo12 : Nat -> Set where
  186. -- foo12 : (n : Nat) -> Foo12 (number'' | n)
  187. -- abstract
  188. -- g12 : {i : Nat} -> Foo12 i -> Nat
  189. -- g12 (foo12 n) = n
  190. data List (A : Set) : Set where
  191. [] : List A
  192. _::_ : A -> List A -> List A
  193. infixr 50 _::_
  194. -- butlast function
  195. good1 : {A : Set} -> List A -> A
  196. good1 (a :: []) = a
  197. good1 (a :: b :: bs) = good1 (b :: bs)
  198. infixl 10 _⊕_
  199. postulate
  200. _⊕_ : {A : Set} -> A -> A -> A -- non-deterministic choice
  201. -- a funny formulation of insert
  202. -- insert (a :: l) inserts a into l
  203. insert : {A : Set} -> List A -> List A
  204. insert [] = []
  205. insert (a :: []) = a :: []
  206. insert (a :: b :: bs) = a :: b :: bs ⊕ -- case a <= b
  207. b :: insert (a :: bs) -- case a > b
  208. -- list flattening
  209. flat : {A : Set} -> List (List A) -> List A
  210. flat [] = []
  211. flat ([] :: ll) = flat ll
  212. flat ((x :: l) :: ll) = x :: flat (l :: ll)
  213. -- leaf-labelled trees
  214. data Tree (A : Set) : Set where
  215. leaf : A -> Tree A
  216. node : Tree A -> Tree A -> Tree A
  217. -- flattening (does not termination check)
  218. tflat : {A : Set} -> Tree A -> List A
  219. tflat (leaf a) = a :: []
  220. tflat (node (leaf a) r) = a :: tflat r
  221. tflat (node (node l1 l2) r) = tflat (node l1 (node l2 r))
  222. -- Maximum of 3 numbers
  223. -- mixing tupling and swapping: does not work with structured orders
  224. max3' : Nat × Nat -> Nat -> Nat
  225. max3' (zero , zero) z = z
  226. max3' (zero , y) zero = y
  227. max3' (x , zero) zero = x
  228. max3' (succ x , succ y) zero = succ (max3' (x , y) zero)
  229. max3' (succ x , zero) (succ z) = succ (max3' (x , z) zero)
  230. max3' (zero , succ y) (succ z) = succ (max3' (y , z) zero)
  231. max3' (succ x , succ y) (succ z) = succ (max3' (z , x) y)