Let’s ignore for the moment the fact that
Pbinds are ordered, i.e. let’s assume that they are records (
Dictionary in SC). What does ‘<>’ (i.e. Pchain) actually do to Pbinds in Computer Science terms?
My first thought was that it is partial function composition, but that’s not at all the case. (To see why not, simply consider the simple case where all field values are numbers; then all record fields have type from string (key) to number, so there can be no function composition going on here, because it clearly would not type check.)
So ather dusting off some computer science papers, I think the correct answer is that (at this level of abstraction, i.e. Pbind approximated as a record), what
<> does to record is what Bracha and Lindstrom call record override.
In their notation, which I’m simplifying by dropping a subscript
r1 <- r2
produces a result that has all the attributes of r1 and r2. If r1 and r2 have names in common, the result takes its values for the common names from r2
Pchain works in the opposite sense, i.e.
p1 <> p2 it is the values from
p1 that override those of
The “record override” terminology is a slightly unfortunate. The actually define a notion of record merging in the same paper (a few lines before “override”), but with the caveat that there must not be any key/name conflicts. Of course, the “override” is really just a merge in a more general sense, but for records you have potentially many ways how to solve conflicts. The “override” just unformely prefers keys from one record in case of conflicts.
The notion of record override doesn’t translate into anything useful for total functions, (you’d be just replacing the whole “destination” function with the “source”), but interestingly it’s non-trivial for partial functions (which records can also be though of as being), because you can override just what isn’t defined, e.g. take the constant
5 as a function; you can use it to override the undefined/nil values of the (partial) function
1/x (in the math sense, forget about how SC implements it with
inf) with the meaning that the function (in Bracha’s notation)
5 <- 1/x (or
1/x <overrides> 5 in a pseudo-SC notation) overrides
1/x everywhere except at
x=0. So clearly this is a different notion of “composition” than function composition.
Pchain is actually generic in SC, i.e. it composes data (i.e. non-event) streams roughly like function composition works. The “overriding business” happens entirely in
Pbind because it isn’t simply a record, but rather a function from records to records, or more precisely it is a partially applied function that stores a record and uses it to record-override its argument (the protoEvent).