Filter.agda 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. {-
  2. Types Summer School 2007
  3. Bertinoro
  4. Aug 19 - 31, 2007
  5. Agda
  6. Ulf Norell
  7. -}
  8. module Filter where
  9. open import Nat
  10. data Bool : Set where
  11. false : Bool
  12. true : Bool
  13. infixr 40 _::_
  14. data List (A : Set) : Set where
  15. [] : List A
  16. _::_ : A -> List A -> List A
  17. filter : {A : Set} -> (A -> Bool) -> List A -> List A
  18. filter p [] = []
  19. filter p (x :: xs) with p x
  20. filter p (x :: xs) | true = x :: filter p xs
  21. filter p (x :: xs) | false = filter p xs
  22. infix 20 _⊆_
  23. data _⊆_ {A : Set} : List A -> List A -> Set where
  24. stop : [] ⊆ []
  25. drop : forall {xs y ys} -> xs ⊆ ys -> xs ⊆ y :: ys
  26. keep : forall {x xs ys} -> xs ⊆ ys -> x :: xs ⊆ x :: ys
  27. subset : {A : Set}(p : A -> Bool)(xs : List A) -> filter p xs ⊆ xs
  28. subset p [] = stop
  29. subset p (x :: xs) with p x
  30. ... | true = keep (subset p xs)
  31. ... | false = drop (subset p xs)
  32. {-
  33. subset p (x :: xs) with p x
  34. subset p (x :: xs) | true = keep (subset p xs)
  35. subset p (x :: xs) | false = drop (subset p xs)
  36. -}