<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>Random Hacks: Haskell: What happens when you divide infinity by 2?</title>
    <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Technology and Other Fun Stuff</description>
    <item>
      <title>"Haskell: What happens when you divide infinity by 2?" by alex</title>
      <description>&lt;p&gt;Some professor wrote about something called transreal arithmetic.  From what I understant it incorporates infinity and 1/0 as part of the numbering system.&lt;/p&gt;


	&lt;p&gt;http://spiedl.aip.org/getabs/servlet/GetabsServlet?prog=normal&amp;#38;id=PSISDG006499000001649902000001&amp;#38;idtype=cvips&amp;#38;gifs=yes&lt;/p&gt;</description>
      <pubDate>Mon, 11 Jun 2007 15:43:47 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1db122c2-ec70-49bc-98e0-ce20c4805158</guid>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2#comment-467</link>
    </item>
    <item>
      <title>"Haskell: What happens when you divide infinity by 2?" by null</title>
      <description>&lt;p&gt;when you have a formal infinity, 1/infinity becomes an infinitesimal number, so you can develop non-standard analysis.&lt;/p&gt;</description>
      <pubDate>Sat, 10 Feb 2007 05:57:47 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:b00a3e39-2ad9-4420-aa4f-c4b506d0a130</guid>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2#comment-289</link>
    </item>
    <item>
      <title>"Haskell: What happens when you divide infinity by 2?" by muppt</title>
      <description>&lt;p&gt;you can also add a -Infinity, and it&amp;#8217;ll still be compatible with Peano axioms.&lt;/p&gt;</description>
      <pubDate>Sat, 10 Feb 2007 05:51:44 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5284ecbf-6262-4ece-9af8-1030801c2e69</guid>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2#comment-288</link>
    </item>
    <item>
      <title>"Haskell: What happens when you divide infinity by 2?" by Eric Kidd</title>
      <description>&lt;p&gt;A caution: I&amp;#8217;m still studying the paper by Fokkinga and Meijer. And as I work through various sections, I&amp;#8217;m increasingly convinced that my understanding of some of the ideas is still pretty dodgy.&lt;/p&gt;


	&lt;p&gt;So please don&amp;#8217;t take anything in the previous comment &lt;i&gt;too&lt;/i&gt; seriously unless somebody with a better grounding in category theory confirms it!&lt;/p&gt;</description>
      <pubDate>Thu, 08 Feb 2007 20:51:02 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:04047e55-a2fe-4e22-899c-088e60d1e0b3</guid>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2#comment-281</link>
    </item>
    <item>
      <title>"Haskell: What happens when you divide infinity by 2?" by Eric Kidd</title>
      <description>&lt;p&gt;Mikael Johansson: &lt;i&gt;I do, alas, not, however, know how this compares to the category &lt;span class="caps"&gt;CPO&lt;/span&gt; you’re asking about&amp;#8230;&lt;/i&gt;&lt;/p&gt;


	&lt;p&gt;Well, the Peano arithmetic is related to  the functor K&lt;sub&gt;1&lt;/sub&gt;&amp;#x2b;Id. This  gives us the F-algebra 1&amp;#x2b;&lt;i&gt;a&lt;/i&gt; &amp;rarr; &lt;i&gt;a&lt;/i&gt;, which is basically any type of the form:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;/div&gt;

	&lt;p&gt;In a strict language (like ML), this could only represent a finite data structure. After all, every value of type &lt;code&gt;T&lt;/code&gt; must ultimately end in an &lt;code&gt;A&lt;/code&gt;.&lt;/p&gt;


	&lt;p&gt;Traditionally, we would need to use streams to represent an infinite data structure. In our case, that would give us the final F-coalgebra &lt;i&gt;a&lt;/i&gt; &amp;rarr; 1&amp;#x2b;&lt;i&gt;a&lt;/i&gt;. In a hypothetical programming language, we might write it:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;codata T = A | B T -- Not Haskell!&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Basically, instead of having the ability to build a structure using &lt;code&gt;A&lt;/code&gt; or &lt;code&gt;B&lt;/code&gt;, we&amp;#8217;re given a data structure and allowed to pick off one &lt;code&gt;B&lt;/code&gt; at a time until we hit an &lt;code&gt;A&lt;/code&gt;. It could go on for ever.&lt;/p&gt;


	&lt;p&gt;Now in the category &lt;b&gt;Set&lt;/b&gt;, the F-algebra 1&amp;#x2b;&lt;i&gt;a&lt;/i&gt; &amp;rarr; &lt;i&gt;a&lt;/i&gt; and the F-coalgebra &lt;i&gt;a&lt;/i&gt; &amp;rarr; 1&amp;#x2b;&lt;i&gt;a&lt;/i&gt; are different beasts. This means that you can declare the Peano numbers without having to deal with infinity.&lt;/p&gt;


	&lt;p&gt;But Haskell is a lazy language. As the famous example shows, a Haskell list can be infinity long:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;/div&gt;

	&lt;p&gt;Now, according to &lt;a href="http://citeseer.ist.psu.edu/fokkinga91program.html" rel="nofollow"&gt; Fokkinga and Meijer&lt;/a&gt;, there&amp;#8217;s actually some pretty deep math lurking here.  Apparently, Haskell&amp;#8217;s lazy evaluation moves us from category &lt;b&gt;Set&lt;/b&gt; to category &lt;b&gt;&lt;span class="caps"&gt;CPO&lt;/span&gt;&lt;/b&gt;, where our initial F-algebras and our final F-coalgebras turn out to be the same!&lt;/p&gt;


	&lt;p&gt;Anyway, here&amp;#8217;s the bit in &lt;a href="http://citeseer.ist.psu.edu/danielsson06fast.html" rel="nofollow"&gt;Danielsson &lt;i&gt;et al.&lt;/i&gt;&lt;/a&gt; which inspired this entire post:&lt;/p&gt;


&lt;blockquote&gt;

	&lt;p&gt;To be concrete, let us assume that the natural number data type &lt;code&gt;Nat&lt;/code&gt; is defined in the usual way,&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;/div&gt;

Note that this type contains many properly partial values that do not correspond to any natural number, and also a total but infinite value.
&lt;/blockquote&gt;

	&lt;p&gt;The &amp;#8220;total but infinite value&amp;#8221; appears above as &lt;code&gt;infinity&lt;/code&gt;. One of the partial values turned up when &lt;a href="http://programming.reddit.com/user/patchwork/" rel="nofollow"&gt;patchwork&lt;/a&gt;  tried to &lt;a href="http://programming.reddit.com/info/12mm7/comments/c12ndo" rel="nofollow"&gt;compute&lt;/a&gt; &lt;code&gt;subtract infinity infinity&lt;/code&gt; over on reddit.&lt;/p&gt;</description>
      <pubDate>Sat, 03 Feb 2007 19:22:24 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:901b2b9e-7203-455e-94f2-a4e9da526613</guid>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2#comment-267</link>
    </item>
    <item>
      <title>"Haskell: What happens when you divide infinity by 2?" by Jim Apple</title>
      <description>&lt;p&gt;I posted my reply at http://japple.blogspot.com/2007/02/countable-ordinals-in-haskell.html&lt;/p&gt;


	&lt;p&gt;Summary: I think we should represent countable ordinals as the limits of increasing functions from the naturals to the countable ordinals.&lt;/p&gt;</description>
      <pubDate>Sat, 03 Feb 2007 15:35:04 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:43f45753-e4a1-4570-87bb-c8335f50adad</guid>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2#comment-266</link>
    </item>
    <item>
      <title>"Haskell: What happens when you divide infinity by 2?" by Mikael Johansson</title>
      <description>&lt;p&gt;First off, your implementation of infinity is probably among the saner possible with a Peano arithmetic. At least for the infinity Aleph-0.&lt;/p&gt;


	&lt;p&gt;I do, alas, not, however, know how this compares to the category &lt;span class="caps"&gt;CPO&lt;/span&gt; you&amp;#8217;re asking about; I would, however, suppose that it is a sane way to do it.&lt;/p&gt;


	&lt;p&gt;You could also have done infinity as&lt;/p&gt;


	&lt;p&gt;data CompleteNumbers = Number Numbertype | Infinity&lt;/p&gt;


	&lt;p&gt;or as&lt;/p&gt;


	&lt;p&gt;data CompleteNumbers = Number Numbertype | Infinity | NInfinity&lt;/p&gt;


	&lt;p&gt;with the corresponding extensions to Ord and Num implemented specificially. Especially the one-point compactification of R (or &amp;#8230; well &amp;#8230; machine-R) you&amp;#8217;ll find as&lt;/p&gt;


	&lt;p&gt;data CompactR = (Real a) =&gt; Finite a | Infinity&lt;/p&gt;


	&lt;p&gt;with the correspondingly defined instantiation of Num and Ord.&lt;/p&gt;</description>
      <pubDate>Sat, 03 Feb 2007 13:26:30 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:5856cba0-1d38-4982-a373-04ee80bffea8</guid>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2#comment-265</link>
    </item>
    <item>
      <title>"Haskell: What happens when you divide infinity by 2?" by Alan Manuel Gloria</title>
      <description>&lt;p&gt;I once thought up of a way of doing the Grand Hotel in Haskell, by using an infinite list:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;infinityHotel = iterate (+1) 1&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;So the Grand Hotel is full already.  Let&amp;#8217;s say number one&amp;#8217;s brother decides to check in:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;1:infinityHotel&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Suppose instead that an infinite number of zero&amp;#8217;s decides to check in:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;zeroes = repeat 0&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;Which we enter into the hotel as:&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;foldr1 (++) (zipWith (:) infinityhotel
                         (map (:[]) zeroes))&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;</description>
      <pubDate>Sat, 03 Feb 2007 09:53:20 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:1a601ca5-bb9f-477b-ace2-2a23144bda60</guid>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2#comment-264</link>
    </item>
    <item>
      <title>Haskell: What happens when you divide infinity by 2?</title>
      <description>&lt;p&gt;Sometime back in elementary school, I first asked teachers, &amp;#8220;What happens when you divide infinity by 2?&amp;#8221; Some teachers couldn&amp;#8217;t answer, and others told me, &amp;#8220;It&amp;#8217;s still infinity!&amp;#8221;&lt;/p&gt;

&lt;p&gt;More recently, a couple of friends were discussing a similar question at lunch: &amp;#8220;What happens when you add 1 to infinity?&amp;#8221;&lt;/p&gt;

&lt;p&gt;Of course I said, &amp;#8220;It&amp;#8217;s still infinity!&amp;#8221;, but I couldn&amp;#8217;t explain it much better than my school teachers (at least not without using the word &lt;a href="http://en.wikipedia.org/wiki/Denumerable"&gt;denumerable&lt;/a&gt;, which is a good way to ruin a lunch conversation).&lt;/p&gt;

&lt;p&gt;And then tonight, while reading a &lt;a title="Fast and Loose Reasoning is Morally Correct" href="http://citeseer.ist.psu.edu/danielsson06fast.html"&gt;paper about Haskell&lt;/a&gt;, I was hit by an evil idea: When in doubt, ask the Haskell interpreter!&lt;/p&gt;

&lt;h2&gt;Step 1: Counting&lt;/h2&gt;

&lt;p&gt;First, we need to teach Haskell about the natural numbers. (Why not use Haskell&amp;#8217;s built-in integers? Just humor the crazy programmer for a moment, OK?)&lt;/p&gt;

&lt;p&gt;A number is either zero, or the successor of another number. We can write that in Haskell as:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='keyword'&gt;data&lt;/span&gt; &lt;span class='conid'&gt;Nat&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Zero&lt;/span&gt; &lt;span class='keyglyph'&gt;|&lt;/span&gt; &lt;span class='conid'&gt;Succ&lt;/span&gt; &lt;span class='conid'&gt;Nat&lt;/span&gt;
  &lt;span class='keyword'&gt;deriving&lt;/span&gt; &lt;span class='layout'&gt;(&lt;/span&gt;&lt;span class='conid'&gt;Show&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='conid'&gt;Eq&lt;/span&gt;&lt;span class='layout'&gt;,&lt;/span&gt; &lt;span class='conid'&gt;Ord&lt;/span&gt;&lt;span class='layout'&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;Math geeks in the audience will recognize this as the &lt;a href="http://mathworld.wolfram.com/PeanosAxioms.html"&gt;Peano arithmetic&lt;/a&gt;. The &amp;#8220;deriving&amp;#8221; keyword tells Haskell to define &lt;code&gt;show&lt;/code&gt; and the comparison operators for us.&lt;/p&gt;

&lt;p&gt;Using this definition of Nat, we can now define some numbers:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_haskell "&gt;&lt;span class='varid'&gt;one&lt;/span&gt;   &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Succ&lt;/span&gt; &lt;span class='conid'&gt;Zero&lt;/span&gt;
&lt;span class='varid'&gt;two&lt;/span&gt;   &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Succ&lt;/span&gt; &lt;span class='varid'&gt;one&lt;/span&gt;
&lt;span class='varid'&gt;three&lt;/span&gt; &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Succ&lt;/span&gt; &lt;span class='varid'&gt;two&lt;/span&gt;
&lt;span class='varid'&gt;four&lt;/span&gt;  &lt;span class='keyglyph'&gt;=&lt;/span&gt; &lt;span class='conid'&gt;Succ&lt;/span&gt; &lt;span class='varid'&gt;three&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;These work the way you&amp;#8217;d expect:&lt;/p&gt;

&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;*Main&amp;gt; three
Succ (Succ (Succ Zero))
*Main&amp;gt; two &amp;lt; three
True&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

&lt;p&gt;OK, I threw in that last example just for fun.&lt;/p&gt;&lt;p&gt;&lt;a href="http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2"&gt;Read More&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Fri, 02 Feb 2007 22:00:00 +0000</pubDate>
      <guid isPermaLink="false">urn:uuid:49c4f02e-74eb-430a-9b49-bfd623f3f586</guid>
      <author>Eric Kidd</author>
      <link>http://www.randomhacks.net/articles/2007/02/02/divide-infinity-by-2</link>
      <category>Haskell</category>
      <category>Math</category>
      <category>Hacks</category>
      <trackback:ping>http://www.randomhacks.net/articles/trackback/259</trackback:ping>
    </item>
  </channel>
</rss>
