r/ProgrammingLanguages • • Jul 27 '26

Any examples of a "sum unmerging" operator, the categorical dual of the record merging one?

For some types A, B, C... the type { x : A, y : B, z : C, ... } is the type of records with projections x, y, z... into the corresponding types and the type [ x : A, y : B, z : C ] is the type of sums with injections from the corresponding types A, B, C, ....

For arbitrary A, B, C, D we can define an operator // that merges two binary records:

_//_ : { x : A, y : B } → { z : C, w : D } → { x : A, y : B, z : C, w : D }

Naturally, we can also define the dual of this operator for two binary sum types:

_\\ : [ x : A, y : B, z : C, w : D ] → [ x : A, y : B ] ⊎ [ z : C, w : D ]

This is easy to generalize to an arbitrary number of injections/projections.

While there is plenty examples of an operator like _//_ existing in programming languages (// in Nix, & in Nickel, ⫽ in Dhall, Record.merge in PureScript) I can't really think of anything like the _\\ operator from above. I suppose its ergonomics are partly responsible for it, after all it is often easier to infer the arity of the records for _//_ from the arguments used than it is to infer the arity of the outputs for _\\ from context. But can you think of anything?

20 Upvotes

12 comments sorted by

View all comments

4

u/ExplodingStrawHat Jul 27 '26 edited Jul 27 '26

Are you sure that's the actual dual?  I assume the product of the smaller sum types in the codomain should itself be a sum/coproduct, right?

4

u/Background_Class_558 Jul 27 '26

Oh yeah sorry, brain glitched as i was typing it. Semantically, it should be a coproduct. Edited the post.

1

u/ExplodingStrawHat Jul 27 '26

Fair. I wonder if part of it would be a type inference issue? Otherwise I think it should be possible to implement in say, PureScript, given enough typeclsss magic.

2

u/Background_Class_558 Jul 27 '26

I have also realized that its syntax makes absolutely no sense because it should be unary. Edited that in as well but it doesn't look good visually.