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?
5
u/Spotted_Metal Jul 27 '26
The typical way of processing sum types (besides pattern matching) is with something like the
caseexpression in Haskell ormatchin Rust. So we could imagine a version that handles splitting sum types like this (pseudocode):...though of the top of my head, I can't think of a language that has this feature. The main ergonomic problem I see is that we either need to specify the type annotations for each branch, or we need to use type inference which can cause its own issues (what if the code in the first branch is updated in a way that allows it to also handle
z, but we specifically wantzto be handled in the second branch?)It is worth nothing that in languages where sum types are nominally-typed (in other words, where
Type1 : [a : A]is not considered a subtype ofType2 : [a : A, b : B]), it might not even be possible to give the right type annotations to make this work. However, in those languages we could make use of nested sum types instead (pseudocode):...which is probably more ergonomic and allows us to side-step the need for a
_\\operator (though I could see it being useful for languages with structural sum types).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?
3
u/evincarofautumn Jul 27 '26
If you have the user spell out the fields, this looks like a relational projection, or an associative law
Applied to a single field, it’s a partial projection, like an uncons operation where you get either a match or a narrower sum
This also reminds me of something Lawvere does:
To pattern-match on a product of sums x:(f:1 + t:1) × y:2, it explicitly uses a distributive law
@xto unpack the cases forxand carry along the remaining fields: f:(x:1 × y:2) + t:(x:1 × y:2). Then this is composed with a variant/cocone[ … ]with an arm for how to handle each remaining component of the product. You could do a similar thing with sums.And I think inference would actually be okay for type-correct programs, but it wouldn’t naturally produce very helpful messages on a type error because you have a lot of nondeterminism in the choice of how you could split a variant. It’d probably work out a little better if you didn’t implicitly reorder anything.
8
u/Brohomology Jul 27 '26
Both of these operators are in fact isomorphisms, so you could choose the direction from which it is most easy to infer the arguments.