Stream.agda 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. -- simulating streams by Nat -> A
  2. module Stream where
  3. data Bool : Set where
  4. true : Bool
  5. false : Bool
  6. if_then_else_ : forall {A} -> Bool -> A -> A -> A
  7. if true then t else e = t
  8. if false then t else e = e
  9. data Nat : Set where
  10. zero : Nat
  11. succ : Nat -> Nat
  12. Stream : Set -> Set
  13. Stream A = Nat -> A
  14. _::_ : forall {A} -> A -> Stream A -> Stream A
  15. _::_ a as zero = a
  16. _::_ a as (succ n) = as n
  17. map : forall {A B} -> (A -> B) -> Stream A -> Stream B
  18. map f as n = f (as n)
  19. head : forall {A} -> Stream A -> A
  20. head as = as zero
  21. tail : forall {A} -> Stream A -> Stream A
  22. tail as n = as (succ n)
  23. -- construct the stream a :: f a :: f (f a) :: ...
  24. iterate : forall {A} -> (A -> A) -> A -> Stream A
  25. iterate f a zero = a
  26. iterate f a (succ n) = iterate f (f a) n
  27. zipWith : forall {A B C} -> (A -> B -> C) -> Stream A -> Stream B -> Stream C
  28. zipWith f as bs n = f (as n) (bs n)
  29. -- merge with with
  30. merge : forall {A} -> (A -> A -> Bool) -> Stream A -> Stream A -> Stream A
  31. merge le as bs with le (head as) (head bs)
  32. merge le as bs | true = head as :: merge le (tail as) bs
  33. merge le as bs | false = head bs :: merge le as (tail bs)
  34. {-
  35. -- without with
  36. merge' : forall {A} -> (A -> A -> Bool) -> Stream A -> Stream A -> Stream A
  37. merge' le as bs = if le (head as) (head bs)
  38. then (head as :: merge' le (tail as) bs)
  39. else (head bs :: merge' le as (tail bs))
  40. -}
  41. -- BOTH VARIANTS OF MERGE ARE NOT STRUCTURALLY RECURSIVE