I was watching this lecture from Bartosz Milewski and he was explaining coproduct and sum types.
On the lecture, He went from one to the other.
Is the coproduct the same as the sum type?
Basically, yes. Coproducts are in principle more general, but that needn't necessarily concern you as far as Haskell is concerned.
An example of a category where the coproduct is not a sum type† is the category of vector spaces with linear mappings as the arrows. In this category, disjoint-sum types don't make a lot of sense because they would give you two different zero-vector elements.
Instead, it turns out that product types (which in linear algebra are called direct sums, but implementation-wise they're tuples, not alternatives) are a coproduct on this category:
type LFun v w = v -> w
initial :: VectorSpace w => LFun () w
initial () = zeroV
(+++) :: VectorSpace w => LFun u w -> LFun v w -> LFun (u,v) w
(f+++g) (u,v) = f u ^+^ g v
(The standard product on this category is then the tensor product. Though it's possible to ignore this and use ordinary tuples as the product type, i.e. the actual coproducts. I think this has to do with the fact that any Hilbert space is isomorphic to its dual space. In my See Derek Elkins' comment.constrained-categories
/linearmap-category
library, the products are tuples, whereas Mike Izbicki has not done this in the topically similar SubHask
library.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With