<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Mathematics and Computation &#187; Programming</title>
	<atom:link href="http://math.andrej.com/category/programming/feed/?category_name=programming" rel="self" type="application/rss+xml" />
	<link>http://math.andrej.com</link>
	<description>Mathematics for computers</description>
	<lastBuildDate>Sun, 29 Jan 2012 16:52:56 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2-beta2-18055</generator>
		<item>
		<title>A puzzle about typing</title>
		<link>http://math.andrej.com/2012/01/20/a-puzzle-about-typing/</link>
		<comments>http://math.andrej.com/2012/01/20/a-puzzle-about-typing/#comments</comments>
		<pubDate>Fri, 20 Jan 2012 13:42:44 +0000</pubDate>
		<dc:creator>Andrej Bauer</dc:creator>
				<category><![CDATA[Computation]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://math.andrej.com/?p=1109</guid>
		<description><![CDATA[<p>While making a comment on Stackoverflow I noticed something: suppose we have a term in the $\lambda$-calculus in which no abstracted variable is used more than once. For example, $\lambda a b c . (a b) (\lambda d. d c)$ is such a term, but $\lambda f . f (\lambda x . x x)$ is not because [...]]]></description>
			<content:encoded><![CDATA[<p>While making a comment on <a href="http://cstheory.stackexchange.com/questions/9690/functions-that-typed-lambda-calculus-cannot-compute">Stackoverflow</a> I noticed something: suppose we have a term in the $\lambda$-calculus in which no abstracted variable is used more than once. For example, $\lambda a b c . (a b) (\lambda d. d c)$ is such a term, but $\lambda f . f (\lambda x . x x)$ is not because $x$ is used twice. If I am not mistaken, all such terms can be typed. For example:</p>
<pre class="brush: plain; gutter: false; title: ;">
# fun a b c -&gt; (a b) (fun d -&gt; d c) ;;
- : ('a -&gt; (('b -&gt; 'c) -&gt; 'c) -&gt; 'd) -&gt; 'a -&gt; 'b -&gt; 'd = &lt;fun&gt;

# fun a b c d e e' f g h i j k l m n o o' o'' o''' p q r r' s t u u' v w x y z -&gt;
    q u i c k b r o w n f o' x j u' m p s o'' v e r' t h e' l a z y d o''' g;;
  - : 'a -&gt; 'b -&gt; 'c -&gt; 'd -&gt; 'e -&gt; 'f -&gt; 'g -&gt; 'h -&gt; 'i -&gt; 'j -&gt;
    'k -&gt; 'l -&gt; 'm -&gt; 'n -&gt; 'o -&gt; 'p -&gt; 'q -&gt; 'r -&gt; 's -&gt; 't -&gt;
    ('u -&gt; 'j -&gt; 'c -&gt; 'l -&gt; 'b -&gt; 'v -&gt; 'p -&gt; 'w -&gt; 'o -&gt; 'g -&gt;
     'q -&gt; 'x -&gt; 'k -&gt; 'y -&gt; 'n -&gt; 't -&gt; 'z -&gt; 'r -&gt; 'a1 -&gt; 'e -&gt;
     'b1 -&gt; 'c1 -&gt; 'i -&gt; 'f -&gt; 'm -&gt; 'a -&gt; 'd1 -&gt; 'e1 -&gt; 'd -&gt; 's
     -&gt; 'h -&gt; 'f1) -&gt; 'v -&gt; 'b1 -&gt; 'z -&gt; 'c1 -&gt; 'u -&gt; 'y -&gt; 'a1
     -&gt; 'w -&gt; 'x -&gt; 'e1 -&gt; 'd1 -&gt; 'f1 = &lt;fun&gt;
</pre>
<p>What is the easiest way to see that this really is the case?</p>
<p>A related question is this (I am sure people have thought about it): how big can a type of a typeable $\lambda$-term be? For example, the Ackermann function can be typed as follows, although the type prevents it from doing the right thing in a typed setting:</p>
<pre class="brush: plain; gutter: false; title: ;">
# let one = fun f x -&gt; f x ;;
val one : ('a -&gt; 'b) -&gt; 'a -&gt; 'b =
# let suc = fun n f x -&gt; n f (f x) ;;
val suc : (('a -&gt; 'b) -&gt; 'b -&gt; 'c) -&gt; ('a -&gt; 'b) -&gt; 'a -&gt; 'c =
# let ack = fun m -&gt; m (fun f n -&gt; n f (f one)) suc ;;
val ack :
  ((((('a -&gt; 'b) -&gt; 'a -&gt; 'b) -&gt; 'c) -&gt;
   (((('a -&gt; 'b) -&gt; 'a -&gt; 'b) -&gt; 'c) -&gt; 'c -&gt; 'd) -&gt; 'd) -&gt;
   ((('e -&gt; 'f) -&gt; 'f -&gt; 'g) -&gt; ('e -&gt; 'f) -&gt; 'e -&gt; 'g) -&gt; 'h) -&gt; 'h = &lt;fun&gt;
</pre>
<p>That&#8217;s one mean type there! Can it be &#8220;explained&#8221;? Hmm, why <em>does</em> <code>ack</code> compute the Ackermann function in the untyped $\lambda$-calculus?</p>
]]></content:encoded>
			<wfw:commentRss>http://math.andrej.com/2012/01/20/a-puzzle-about-typing/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Running a classical proof with choice in Agda</title>
		<link>http://math.andrej.com/2011/05/10/running-a-classical-proof-with-choice-in-agda/</link>
		<comments>http://math.andrej.com/2011/05/10/running-a-classical-proof-with-choice-in-agda/#comments</comments>
		<pubDate>Tue, 10 May 2011 21:17:36 +0000</pubDate>
		<dc:creator>Martin Escardo</dc:creator>
				<category><![CDATA[Computation]]></category>
		<category><![CDATA[Constructive math]]></category>
		<category><![CDATA[Logic]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorial]]></category>

		<guid isPermaLink="false">http://math.andrej.com/?p=928</guid>
		<description><![CDATA[<p>As a preparation for my part of a joint tutorial Programs from proofs at MFPS 27 at the end of this month with Ulrich Berger, Monika Seisenberger, and Paulo Oliva, I&#8217;ve developed in Agda some things we&#8217;ve been doing together.</p>
<p>Using</p>

 Berardi-Bezem-Coquand functional, or alternatively,
Berger-Oliva modified bar recursion, or alternatively,
Escardo-Oliva countable product of selection functions,

<p>for giving a [...]]]></description>
			<content:encoded><![CDATA[<p>As a preparation for my part of a joint tutorial <em>Programs from proofs</em> at <a href="http://129.81.170.14/~mfps/MFPS27/MFPS_XXVII.html">MFPS 27</a> at the end of this month with <a href="http://www.cs.swan.ac.uk/~csulrich/">Ulrich Berger</a>, <a href="http://www.cs.swan.ac.uk/~csmona/">Monika Seisenberger</a>, and <a href="http://www.eecs.qmul.ac.uk/~pbo/">Paulo Oliva</a>, I&#8217;ve developed in <a href="http://wiki.portal.chalmers.se/agda/pmwiki.php">Agda</a> some things we&#8217;ve been doing together.</p>
<p>Using</p>
<ul>
<li> <a href="http://projecteuclid.org/DPubS?service=UI&amp;version=1.0&amp;verb=Display&amp;handle=euclid.jsl/1183745524">Berardi-Bezem-Coquand functional</a><a>, or alternatively,</a></li>
<li><a href="http://journals.cambridge.org/action/displayAbstract?fromPage=online&amp;aid=439279&amp;fulltextType=RA&amp;fileId=S0960129506005093">Berger-Oliva modified bar recursion</a>, or alternatively,</li>
<li><a href="http://journals.cambridge.org/action/displayAbstract?fromPage=online&amp;aid=7423096&amp;fulltextType=RA&amp;fileId=S0960129509990351">Escardo-Oliva countable product of selection functions</a>,</li>
</ul>
<p>for giving a proof term for classical countable choice, we prove the classical infinite pigeonhole principle in Agda: every infinite boolean sequence has a constant infinite subsequence, where the existential quantification is classical (double negated).</p>
<p>As a corollary, we get the finite pigeonhole principle, using Friedman&#8217;s trick to make the existential quantifiers intuitionistic.</p>
<p>This we can run, and it runs fast enough. The point is to illustrate in Agda how we can get witnesses from classical proofs that use countable choice. The finite pigeonhole principle has a simple constructive proof, of course, and hence this is really for illustration only.</p>
<p>The main Agda files are</p>
<ul>
<li><a href="http://www.cs.bham.ac.uk/~mhe/pigeon/html/InfinitePigeon.html">InfinitePigeon</a></li>
<li><a href="http://www.cs.bham.ac.uk/~mhe/pigeon/html/FinitePigeon.html">FinitePigeon</a></li>
<li><a href="http://www.cs.bham.ac.uk/~mhe/pigeon/html/Examples.html">Examples</a></li>
</ul>
<p>These are Agda files converted to html so that you can navigate them by clicking at words to go to their definitions.  A <a href="http://www.cs.bham.ac.uk/~mhe/pigeon/Pigeon.zip">zip file</a> with all Agda files is available. Not much more information is available <a href="http://www.cs.bham.ac.uk/~mhe/pigeon/">here</a>.</p>
<p>The three little modules that implement the Berardi-Bezem-Coquand, Berger-Oliva and Escardo-Oliva functionals disable the termination checker, but no other module does. The type of these functionals in Agda is the <a href="http://portal.acm.org/citation.cfm?id=1876437">J-shift principle</a>, which generalizes the double-negation shift.</p>
]]></content:encoded>
			<wfw:commentRss>http://math.andrej.com/2011/05/10/running-a-classical-proof-with-choice-in-agda/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Bob Harper has a blog</title>
		<link>http://math.andrej.com/2011/03/18/bob-harper-has-a-blog/</link>
		<comments>http://math.andrej.com/2011/03/18/bob-harper-has-a-blog/#comments</comments>
		<pubDate>Fri, 18 Mar 2011 16:32:53 +0000</pubDate>
		<dc:creator>Andrej Bauer</dc:creator>
				<category><![CDATA[News]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://math.andrej.com/?p=908</guid>
		<description><![CDATA[<p>Bob Harper of CMU, has recently started a blog, called Existential Type, about programming languages. He is a leading expert in Programming Languages. I remember being deeply inspired the first time I heard him talk. I was an incoming graduate student at CMU and he presented what the programming languages people at CMU did. His posts [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.cs.cmu.edu/~rwh/">Bob Harper</a> of CMU, has recently started a blog, called <a href="http://existentialtype.wordpress.com/">Existential Type</a>, about programming languages. He is a leading expert in Programming Languages. I remember being deeply inspired the first time I heard him talk. I was an incoming graduate student at CMU and he presented what the programming languages people at CMU did. His posts are fun to read, unreserved and very educational. Highly recommended!</p>
]]></content:encoded>
			<wfw:commentRss>http://math.andrej.com/2011/03/18/bob-harper-has-a-blog/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delimited continuations in eff</title>
		<link>http://math.andrej.com/2010/09/30/delimited-continuations-in-eff/</link>
		<comments>http://math.andrej.com/2010/09/30/delimited-continuations-in-eff/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 13:42:45 +0000</pubDate>
		<dc:creator>Andrej Bauer</dc:creator>
				<category><![CDATA[Eff]]></category>

		<guid isPermaLink="false">http://math.andrej.com/?p=728</guid>
		<description><![CDATA[<p>Let&#8217;s keep the blog rolling! Here are delimited continuations in eff, and a bunch of questions I do not know the answers to.</p>
<p></p>
<p>I am not going to explain what continuations and delimited continuations are, I will just show how to get them in eff. I should also warn you that delimited continuations confuse me deeply. The [...]]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s keep the blog rolling! Here are delimited continuations in eff, and a bunch of questions I do not know the answers to.</p>
<p><span id="more-728"></span></p>
<p>I am not going to explain what <a href="http://en.wikipedia.org/wiki/Continuation">continuations</a> and <a href="http://en.wikipedia.org/wiki/Delimited_continuation">delimited continuations</a> are, I will just show how to get them in eff. I should also warn you that delimited continuations confuse me deeply. The definition of <code>reset</code> and <code>shift</code> is simple:</p>
<pre class="brush: plain; gutter: false; title: ;">
effect reset:
  operation shift f: f (lambda x: yield x)
</pre>
<p>Reset is done with &#8220;<code>with reset:</code>&#8220;. Shift takes a function as a parameter and passes to it the continuation (delimited by the correspoding <code>with</code>). Here is a basic example:</p>
<pre class="brush: plain; gutter: false; highlight: [4]; title: ;">
&gt;&gt;&gt; with reset:
...     shift (lambda k: k(k(k(7)))) * 2 + 1
...
63
</pre>
<p>Let&#8217;s see if we understand this. When shift hapens its continuation is &#8220;multiply by 2 and add 1&#8243;. So <code>k</code> will be the function $x \mapsto 2 x + 1$. Thus $k(k(k(7)))$ is $2 \cdot (2 \cdot (2 \cdot 7 +1) + 1) + 1 = 63$, if I got my arithmetic right.</p>
<p>They say that continuations are the GOTO statement of functional programming:</p>
<pre class="brush: plain; gutter: false; highlight: [3]; title: ;">
&gt;&gt;&gt; (with reset: (shift (lambda k: 10)) * 3 + 1) + 2
...
12
</pre>
<p>This time when shift happens the continuation is &#8220;multiply by 3 and add 1&#8243;, so $k$ is the map $x \mapsto 3 x + 1$. But shift ignores $k$ and just passes $10$ straight to reset (that&#8217;s the GOTO), so the answer is $10 + 2 = 12$.</p>
<p>Let us compute the type of shift. The continuation is a function of type $A \to B$. The argument of shift is a function which accepts the continuation so it has type $(A \to B) \to C$. But this only makese sense if the result of <code>shift</code> is the same as the result of the continuation, therefore $C = B$. The type of <code>shift</code> is<br />
$$B^{B^A} \times B^A \to B.$$<br />
As an algebraic operation <code>shift</code> takes a parameter of type $B^{B^A}$, has arity $A$, and the carrier is $B$. This is a bit odd since the type of the parameter depends on the carrier and the arity. As is well known, continuations are <em>not</em> algebraic operations. But it seems like eff is telling us that <em>delimited</em> continuations are algebraic, if we look at them the right way. Indeed, consider the signature $\Sigma$ whose only operation is $\mathtt{shift}$ of type $(B^{B^A}, A)$. A $\Sigma$-algebra is a set $C$ with an operation $\mathtt{shift}_C : B^{B^A} \times C^A \to C$. Would we expect $\mathtt{shift}_C$ to satisfy any equations? By staring at the above definition of <code>shift</code>, we see that we should require, for all $k : A \to C$, $f : B^A \to B$ and $j : C \to B$,<br />
$$j \circ \mathtt{shift}_C(f, k) = f( j \circ k).$$<br />
Strictly speaking, this is not an equation in the sense of universal algebra because we used composition as well as $\mathtt{shift}_C$, whereas classical universal algebra only allows equations involving $\mathtt{shift}_C$. Nevertheless, we are still awfully close to being algebraic. Perhaps we are staring at a monad for delimited continuations? I need to think about this, or be told that someone already has. Also, what are some examples of $\Sigma$-algebras $(C, \mathtt{shift}_C)$, other than delimited continuations?</p>
<p>Since in eff we can have multiple instances of an effect, we get delimited continuations with multiple prompts, i.e., multiple resets. For example, try wrapping your head around the following piece of code:</p>
<pre class="brush: plain; gutter: false; highlight: [9,10,11,12,13,14,15,16,17,18]; title: ;">
&gt;&gt;&gt; with io:
...     with reset as promptA:
...         print_string &quot;Batman&quot;
...         with reset as promptB:
...             promptB.shift (lambda k: k (k (promptA.shift (lambda l: l (k (l (k ())))))))
...             print_string &quot;Robin&quot;
...          print_string &quot;Cat woman&quot;
...
Batman
Robin
Robin
Robin
Cat woman
Robin
Robin
Robin
Cat woman
()
</pre>
<p>I have no idea what this sort of thing is good for, perhaps someone can suggest a useful program with multiple resets.</p>
]]></content:encoded>
			<wfw:commentRss>http://math.andrej.com/2010/09/30/delimited-continuations-in-eff/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>How eff handles built-in effects</title>
		<link>http://math.andrej.com/2010/09/28/how-eff-handles-built-in-effects/</link>
		<comments>http://math.andrej.com/2010/09/28/how-eff-handles-built-in-effects/#comments</comments>
		<pubDate>Tue, 28 Sep 2010 21:03:41 +0000</pubDate>
		<dc:creator>Matija Pretnar</dc:creator>
				<category><![CDATA[Eff]]></category>

		<guid isPermaLink="false">http://math.andrej.com/?p=706</guid>
		<description><![CDATA[<p>From some of the responses we have been getting it looks like people think that the io effect in eff is like unsafePerformIO in Haskell, namely that it causes an effect but pretends to be pure. This is not the case. Let me explain how eff handles built-in effects.</p>
<p></p>
What does the io handler do?
<p>The io handler [...]]]></description>
			<content:encoded><![CDATA[<p>From some of the responses we have been getting it looks like people think that the <code>io</code> effect in eff is like <code>unsafePerformIO</code> in Haskell, namely that it causes an effect but pretends to be pure. This is <em>not</em> the case. Let me explain how eff handles built-in effects.</p>
<p><span id="more-706"></span></p>
<h3>What does the <code>io</code> handler do?</h3>
<p>The <code>io</code> handler does not print anything. It handles operations <code>print_string</code>, <code>read_string</code>, and <code>read_string</code> by triggering further operations, for example <code>print_string</code> triggers <code>PRINT_STRING</code> (which is not a valid operation name in eff, so programmers can&#8217;t do the same thing). Have a look at <code>builtin.ml</code> in the <a href="http://hg.andrej.com/eff/">repository</a>.</p>
<p>There are only a handful of such built-in operations. Currently they are <code>RAISE</code>, <code>PRINT_STRING</code>, <code>PRINT_VALUE</code> and <code>READ_STRING</code>, see the file <code>toplevel.ml</code> (at the moment the interpreter is a bit fishy about handling native references, but we promise to fix that soon). We try to keep their number small, as these are the gates to actual real-world effects.</p>
<p>Now, <code>PRINT_STRING</code> and the others are treated just as any other operation. Since no user-defined handler can possibly handle them (they have invalid eff names), they float up to the toplevel. The piece of theory which guarantees this is the equation which says that when $h$ does not handle the operation $f$ then $$h (f (p, k)) = f (p, h \circ k).$$</p>
<h3>Who does all the printing then?</h3>
<p>So what the toplevel sees is an element of the free algebra over the built-in operations. In order to do something sensible with this free algebra, eff wraps the computation in a special built-in handler that handles the built-in operations. This is the handler that triggers the actual hardware effects and yields back their results to continuations. It is the only non-pure part of eff.</p>
<p>The programmer cannot escape the toplevel handler, and neither can he somehow invoke it secretly to get something like <code>unsafePerformIO</code>. It is always wrapped around  your computation, on the outside.</p>
<p>The only reason we could think of for having <code>unsafePerformIO</code> is to give the programmer a hack with which he can shoot himself in the foot. So we decided that all I/O should be handled in a tightly controlled way, just like other effects. But since we also understand that programmers need a hack that allows them to easily print debugging info (until there is a debugger), we provided the <code>check</code> statement for that purpose.</p>
<h3>Can other operations float unhandled all the way to the toplevel?</h3>
<p>Yes, this is possible when an operation escapes its handler. The easiest way to do this is to send an operation outside of its scope in a closure. For example, consider an operation <code>kaboom</code>:</p>
<pre class="brush: plain; gutter: false; highlight: [8]; title: ;">
&gt;&gt;&gt; effect bomb:
...     operation kaboom(): BigExplosion
... with bomb:
...     a = 5
...     kaboom()
...     a + 4
...
BigExplosion
</pre>
<p>So far no surprises, the operation was handled. But now:</p>
<pre class="brush: plain; gutter: false; highlight: [5]; title: ;">
&gt;&gt;&gt; f = (with bomb:
...          lambda (): kaboom ())
... f ()
...
Runtime error: Uncaught operation kaboom () (global). (L3, 1-3)
</pre>
<p>Because the operation <code>kaboom</code> was triggered in line 3, which is outside of the scope of its handler, it propagated to the toplevel. What is eff supposed to do with it? We decided it is best to report a runtime error. Once we have a type system, eff should be able to report at compile time that an operation might end up being unhandled.</p>
<h3>But I want my programs to be pure!</h3>
<p>You are out of luck then. If a program is <em>absolutely</em> pure, you need not run it since it won&#8217;t cause an observable effect. Even in Haskell the program is required to be in the <code>IO</code> monad. After all, the only way for a program to communicate with the outside world is through a computational effect.</p>
<p>It is a good idea to concentrate all real-world computational effects just in one place, without cheating, and treat them in the same way as user-defined effects, as far as that is feasible. It should then be possible to devise a type system which can guarantee that certain effects do not happen, just like in Haskell. The algebraic semantics of eff is such a type system, but at a semantic level. What we need now is a useful implementation of it.</p>
]]></content:encoded>
			<wfw:commentRss>http://math.andrej.com/2010/09/28/how-eff-handles-built-in-effects/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

