Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is coproduct the same as sum types?

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?

like image 374
Felipe Gusmao Avatar asked Apr 23 '17 15:04

Felipe Gusmao


1 Answers

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 constrained-categories/linearmap-category library, the products are tuples, whereas Mike Izbicki has not done this in the topically similar SubHask library.)See Derek Elkins' comment.


I understand “sum type” in the data sense, i.e. as an algebraic data type with structure of a tagged union.
like image 172
leftaroundabout Avatar answered Oct 30 '22 03:10

leftaroundabout