r/ProgrammingLanguages • u/Background_Class_558 • 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?
View all comments
Show parent comment
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.