简体   繁体   中英

Is list a monad and comonad?

The list monad is given here . Also see Spivak's paper here . So list is a monad. Is it a comonad? How would you prove that?

The list type constructor a ↦ μ L. 1 + a * L doesn't admit a comonad structure. Recall that if it were a comonad, we'd have (using the names from Haskell's Functor and Comonad typeclasses)

fmap :: ∀  a b. (a → b) → [a] → [b]
extract :: ∀  a. [a] → a
duplicate :: ∀  a. [a] → [[a]]

However, even without going into any required laws, extract cannot be implemented, since its input could be the empty list, giving no way to come up with an a .

The nonempty list type constructor a ↦ μ NE. a + a * NE a ↦ μ NE. a + a * NE does admit a comonad structure, with extract returning the first element, and duplicate mapping [x, y, ..., z] to [[x], [x, y], ..., [x, y, ..., z]] (note that each of them are non-empty by construction).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM