top | item 17942112

The algebra and calculus of algebraic data types

170 points| alex_hirner | 7 years ago |codewords.recurse.com

48 comments

order
[+] vajrabum|7 years ago|reply
That's fun and a bit surprising, but maybe it shouldn't be. I'm reminded that Dana Scott with Christopher Strachey showed that by using lattices or complete partial orders with a bit of topology to model graphs of possible computations of a program you could, just as in analysis, define least upper and lower bounds and a notion of continuity to derive a construction of limit for a computation which is analogous to a limit in analysis. They called this model a domain. That bit of of math is the basis of denotational semantics of programming languages and is necessary because sets are largely sufficient as the basis for algebra and analysis but not for programs which have evils like partial functions, side effects and variable assignment. I believe that Christopher Strachey with Scott also introduced the formal notions of sum, product and recusive types. They also showed how definitions or models of recursive types and functions could be well founded through their construction of limits on domains. An early tech report on it can be found here:

https://www.cs.ox.ac.uk/files/3228/PRG06.pdf

and here's a more recent free book from David Schmidt on the topic:

http://people.cs.ksu.edu/~schmidt/text/DenSem-full-book.pdf

[+] evincarofautumn|7 years ago|reply
Another interesting area of program semantics that hasn’t seen much attention afaik is in the design of languages whose programs form some algebraic structure.

My area of research is concatenative programming, where concatenation of two programs denotes the composition of those programs, and the empty program is the identity function (on the “program state”, which is usually a stack). That means that the syntax and semantics of the language both form monoids, and there’s a homomorphism from the syntax onto the semantics.

Several years ago, I was trying to come up with languages with other more interesting algebraic structures, so you could apply theorems from those structures to programs. A particular challenge is a language whose structure forms a nontrivial ring¹, in particular a Euclidean ring—then you could find the greatest common divisor of two programs, and because a Euclidean ring is a unique factorisation domain, you could also factor a program into prime subprograms. I was fascinated by what that might mean and whether it could be useful.

Unfortunately, it was that “nontrivial” aspect that I could never get past. The language always ended up insufficiently powerful to express anything of interest, its algebraic structure was trivial (e.g. there’s only one program), or it ended up having a weaker structure (e.g. an idempotent semiring). Chris Pressey also worked on this concept a bunch around 2007, and produced some results like Cabra² and Burro³, but ran into similar dead ends like Potro⁴.

¹ https://en.wikipedia.org/wiki/Ring_(mathematics)

² http://catseye.tc/article/Languages.md#cabra

³ http://catseye.tc/article/Languages.md#burro

https://github.com/catseye/Chrysoberyl/blob/master/article/L...

[+] mikhailfranco|7 years ago|reply
Also take a look at the Boom Hierarchy [1], which came from the same era (Bird-Meertens formalism, etc.). It describes relationships between data structures constructed from 4 algebraic properties: unit, commutative, associative and idempotent. The resulting 16 possibilities include the familiar set, bag, list and tree, but also other less useful combinations, such as non-empty mobile.

[1] http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.49....

[+] rfw|7 years ago|reply
Nat = Nat + 1 does end up being meaningful: since ultimately the numbers that fall out (e.g. Bool = 2) end up describing the sizes of sets, an interpretation of Nat is some infinite cardinal (aleph-null), which is the size of the set of natural numbers.
[+] firethief|7 years ago|reply
That makes sense, but this case shows that apparently arbitrary algebraic simplifications aren't allowed because otherwise we could subtract Nat from each side and have trouble.
[+] arithma|7 years ago|reply
A cool aspect is how this algebra/calculus sheds light that maps(/dictionary/associative-arrays) have an equivalent cardinality to functions and that they are in many ways the same.
[+] notduncansmith|7 years ago|reply
In Clojure, maps can be called as functions (equivalent to `get`). This isomorphism is also reflected in K.
[+] benrbray|7 years ago|reply
I once tried to take the Taylor series idea further and define the "cosine" of a type as the Taylor expansion of cos(x), but didn't get anywhere with it.

Can you do anything else weird with ADTs?

[+] abecedarius|7 years ago|reply
Huh. Thinking about this example I'm confused, because the power series for exp(x) looks like the type for unordered lists of x: e.g. the term for lists of length three is x^3, and if you ignore order you get x^3/3!. The cosine is almost the same as the even terms of this series, but with a minus sign on every other term.

What confuses me (besides what to make of the minus sign) is that the type of sets of x ought to be 2^x -- that is, x -> Bool -- because it's isomorphic to the characteristic function of a set, and a function type is an exponential (https://bartoszmilewski.com/2015/03/13/function-types/). But 2^x differs from exp(x) by a constant factor. So I seem to be messing with surface-level analogies without real understanding.

[+] Sharlin|7 years ago|reply
One thing I’ve been wondering is how to interpret the function types `Void -> a` and `a -> Void`, where Void is the uninhabited type (unique up to isomorphism). The number of inhabitants of those types should be |a|^0=1 and 0^|a|=0, respectively, but what does that mean? In real world, function(s) of type a->Void certainly exist, such functions being those that diverge (loop forever or halt via some non-local means).
[+] hiker|7 years ago|reply
`Void` being the uninhabited type, in the light of the Curry-Howard isomorphism stands for a false proposition.

`a -> Void` get interpreted as "not a" or "from a follows contradiction" or equivalently "a is uninhabited". Combinatorially it's `0 ^ a` which for non-empty a is zero but is equal to 1 when a is empty (0^0=1). In other words there are no functions of type `a -> Void` for non-empty a and there's exactly one such function for uninhabited a (id :: Void -> Void).

`Void -> a` is interpreted "from falsehood, anything (follows)" https://en.wikipedia.org/wiki/Principle_of_explosion. Combinatorially a^0 = 1 for all a so there's exactly one such function. An easy way to define it is by induction on Void (which has no cases and you're done).

[+] contravariant|7 years ago|reply
In mathematics, assuming 'void' is the empty set, the function void -> a exists, but is kind of trivial because it never returns anything, however functions a -> void can't exist because they'd have to return an element of void, which don't exist, the one exception being the function void -> void. Denoting A -> B as B^A this translates to:

    a^0 = 1  
    0^a = 0  
    0^0 = 1
The fact that there's precisely 1 function void -> a is considered rather special in category theory, and means that 'void' is the so called 'initial object' for the category (which is automatically unique up to isomorphism).
[+] Quekid5|7 years ago|reply
AFAIUI:

"Void -> a" means that the function cannot be called, because there no way to conjure a Void value. In Haskell, you can do it by (ab)using 'undefined', but that's kind of cheating. However, if you're using Idris with totality checking, I don't think you'll actually get any code calling such a function compile.

The "a -> Void" means that the function can never return.

[+] jpfr|7 years ago|reply
Such functions can’t exist.

The “C” void is expressed by the unit singleton.

So it is |a|^1.

[+] tromp|7 years ago|reply
Very neat insights into data type differentiation! Minor nitpick: the article uses

    data Unit = Unit
but the Haskell standard Prelude already has the empty tuple type (), with single element (), functioning as the standard unit type.
[+] hyperpallium|7 years ago|reply
Does distributivity hold? a x (b + c) = a x b + a x c
[+] evincarofautumn|7 years ago|reply
Up to isomorphism, yes. That is, you can write a pair of functions that are mutual inverses that witness the fact that those two types are isomorphic:

    distl :: (a, Either b c) -> Either (a, b) (a, c)
    distl (a, b_c) = case b_c of
      Left b -> Left (a, b)
      Right c -> Right (a, c)

    factl :: Either (a, b) (a, c) -> (a, Either b c)
    factl ab_ac = case ab_ac of
      Left (a, b) -> (a, Left b)
      Right (a, c) -> (a, Right c)
With the “TypeOperators” feature enabled (and “UnicodeSyntax” for pretty arrows and such, because why not), you can write it more literally:

    type (×) a b = (a, b)
    infixl 7 ×

    type (+) a b = Either a b
    infixl 6 +

    distl ∷ a × (b + c) → a × b + a × c
    factl ∷ a × b + a × c → a × (b + c)
[+] danharaj|7 years ago|reply
Not literally, but there is a 1-1 correspondence between terms of the former and terms of the latter. Modulo technicalities in the presence of non-termination.
[+] yawaramin|7 years ago|reply
Logically, it would seem to. If you can have an apple and either a ball or a cat, that's logically the same as having either an apple and a ball, or an apple and a cat. In terms of types, they are the same.
[+] andrzejsz|7 years ago|reply
What happened to Code Words that are not new issues?