CoPatWith.agda 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. module CoPatWith where
  2. data Maybe (A : Set) : Set where
  3. nothing : Maybe A
  4. just : A -> Maybe A
  5. data _×_ (A B : Set) : Set where
  6. _,_ : A -> B -> A × B
  7. record CoList (A : Set) : Set where
  8. constructor inn
  9. field
  10. out : Maybe (A × CoList A)
  11. open CoList
  12. module With where
  13. map : {A B : Set}(f : A -> B)(l : CoList A) -> CoList B
  14. out (map f l) with out l
  15. out (map f l) | nothing = nothing
  16. out (map f l) | just (a , as) = just (f a , map f as)
  17. module NoWith where
  18. map : {A B : Set}(f : A -> B)(l : CoList A) -> CoList B
  19. out (map {A = A}{B = B} f l) = map' f l (out l)
  20. where outmap : (f : A -> B)(l : CoList A)(outl : Maybe (A × CoList A)) -> Maybe (B × CoList B)
  21. outmap f l nothing = nothing
  22. outmap f l (just (a , as)) = just (f a , map f as)
  23. module With2 where
  24. map : {A B : Set}(f : A -> B)(l : CoList A) -> CoList B
  25. out (map f (inn l)) with l
  26. out (map f (inn .nothing)) | nothing = nothing
  27. out (map f (inn .(just (a , as)))) | just (a , as) = just (f a , map f as)
  28. module NoWith2 where
  29. map : {A B : Set}(f : A -> B)(l : CoList A) -> CoList B
  30. out (map {A = A}{B = B} f l) = map' f l (out l)
  31. where outmap : (f : A -> B)(l : CoList A)(outl : Maybe (A × CoList A)) -> Maybe (B × CoList B)
  32. outmap f (inn .nothing) nothing = nothing
  33. outmap f (inn .(just (a , as))) (just (a , as)) = just (f a , map f as)