Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Disclaimer: A lot of this doesn't really work quite right when you account for ⊥, so I'm going to blatantly disregard that for the sake of simplicity. </p> <p>A few initial points:</p> <ul> <li><p>Note that "union" is probably not the best term for A+B here--that's specifically <a href="http://en.wikipedia.org/wiki/Disjoint_union" rel="noreferrer">a <em>disjoint</em> union</a> of the two types, because the two sides are distinguished even if their types are the same. For what it's worth, the more common term is simply "sum type".</p></li> <li><p>Singleton types are, effectively, all unit types. They behave identically under algebraic manipulations and, more importantly, the amount of information present is still preserved.</p></li> <li><p>You probably want a zero type as well. There isn't a standard one, but the most common name is <code>Void</code>. There are no values whose type is zero, just as there is one value whose type is one.</p></li> </ul> <p>There's still one major operation missing here but I'll get back to that in a moment.</p> <p>As you've probably noticed, Haskell tends to borrow concepts from Category Theory, and all of the above has a very straightforward interpretation as such:</p> <ul> <li><p>Given objects A and B in <strong>Hask</strong>, their product A×B is the unique (up to isomorphism) type that allows two projections <em>fst</em> : A×B → A and <em>snd</em> : A×B → B, where given any type C and functions <em>f</em> : C → A, <em>g</em> : C → B you can define the pairing <em>f &amp;&amp;&amp; g</em> : C → A×B such that <em>fst ∘ (f &amp;&amp;&amp; g)</em> = <em>f</em> and likewise for <em>g</em>. Parametricity guarantees the universal properties automatically and my less-than-subtle choice of names should give you the idea. The <code>(&amp;&amp;&amp;)</code> operator is defined in <code>Control.Arrow</code>, by the way.</p></li> <li><p>The dual of the above is the coproduct A+B with injections <em>inl</em> : A → A+B and <em>inr</em> : B → A+B, where given any type C and functions <em>f</em> : A → C, <em>g</em> : B → C, you can define the copairing <em>f ||| g</em> : A+B → C such that the obvious equivalences hold. Again, parametricity guarantees most of the tricky parts automatically. In this case, the standard injections are simply <code>Left</code> and <code>Right</code> and the copairing is the function <code>either</code>.</p></li> </ul> <p>Many of the properties of product and sum types can be derived from the above. Note that any singleton type is a terminal object of <strong>Hask</strong> and any empty type is an initial object.</p> <p>Returning to the aforementioned missing operation, in a <a href="http://en.wikipedia.org/wiki/Cartesian_closed_category" rel="noreferrer">cartesian closed category</a> you have <a href="http://en.wikipedia.org/wiki/Exponential_object" rel="noreferrer">exponential objects</a> that correspond to arrows of the category. Our arrows are functions, our objects are types with kind <code>*</code>, and the type <code>A -&gt; B</code> indeed behaves as B<sup>A</sup> in the context of algebraic manipulation of types. If it's not obvious why this should hold, consider the type <code>Bool -&gt; A</code>. With only two possible inputs, a function of that type is isomorphic to two values of type <code>A</code>, i.e. <code>(A, A)</code>. For <code>Maybe Bool -&gt; A</code> we have three possible inputs, and so on. Also, observe that if we rephrase the copairing definition above to use algebraic notation, we get the identity C<sup>A</sup> × C<sup>B</sup> = C<sup>A+B</sup>.</p> <p>As for <em>why</em> this all makes sense--and in particular why your use of the power series expansion is justified--note that much of the above refers to the "inhabitants" of a type (i.e., distinct values having that type) in order to demonstrate the algebraic behavior. To make that perspective explicit:</p> <ul> <li><p>The product type <code>(A, B)</code> represents a value each from <code>A</code> and <code>B</code>, taken independently. So for any fixed value <code>a :: A</code>, there is one value of type <code>(A, B)</code> for each inhabitant of <code>B</code>. This is of course the cartesian product, and the number of inhabitants of the product type is the product of the number of inhabitants of the factors.</p></li> <li><p>The sum type <code>Either A B</code> represents a value from either <code>A</code> or <code>B</code>, with the left and right branches distinguished. As mentioned earlier, this is a disjoint union, and the number of inhabitants of the sum type is the sum of the number of inhabitants of the summands.</p></li> <li><p>The exponential type <code>B -&gt; A</code> represents a mapping from values of type <code>B</code> to values of type <code>A</code>. For any fixed argument <code>b :: B</code>, any value of <code>A</code> can be assigned to it; a value of type <code>B -&gt; A</code> picks one such mapping for each input, which is equivalent to a product of as many copies of <code>A</code> as <code>B</code> has inhabitants, hence the exponentiation.</p></li> </ul> <p>While it's tempting at first to treat types as sets, that doesn't actually work very well in this context--we have disjoint union rather than the standard union of sets, there's no obvious interpretation of intersection or many other set operations, and we don't usually care about set membership (leaving that to the type checker). </p> <p>On the other hand, the constructions above spend a lot of time talking about <em>counting</em> inhabitants, and <em>enumerating</em> the possible values of a type is a useful concept here. That quickly leads us to <a href="http://en.wikipedia.org/wiki/Enumerative_combinatorics" rel="noreferrer">enumerative combinatorics</a>, and if you consult the linked Wikipedia article you'll find that one of the first things it does is define "pairs" and "unions" in exactly the same sense as product and sum types by way of <a href="http://en.wikipedia.org/wiki/Generating_function" rel="noreferrer">generating functions</a>, then does the same for "sequences" that are identical to Haskell's lists using exactly the same technique you did.</p> <hr> <p><strong>Edit:</strong> Oh, and here's a quick bonus that I think demonstrates the point strikingly. You mentioned in a comment that for a tree type <code>T = 1 + T^2</code> you can derive the identity <code>T^6 = 1</code>, which is clearly wrong. However, <code>T^7 = T</code> <em>does</em> hold, and a bijection between trees and seven-tuples of trees can be constructed directly, cf. <a href="http://www.math.lsa.umich.edu/~ablass/comb.html" rel="noreferrer">Andreas Blass's "Seven Trees in One"</a>.</p> <p><strong>Edit×2:</strong> On the subject of the "derivative of a type" construction mentioned in other answers, you might also enjoy <a href="https://personal.cis.strath.ac.uk/~conor/Dissect.pdf" rel="noreferrer">this paper from the same author</a> which builds on the idea further, including notions of division and other interesting whatnot.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload