<?xml version="1.0" encoding="ISO-8859-15"?>
<rss version="2.0"><channel>
<title>monadic composition instead of monad transformers</title>
<link>http://sange.fi/~atehwa/cgi-bin/piki.cgi/</link>
<description>Recent changes in monadic composition instead of monad transformers</description>
<item><title>monadic composition instead of monad transformers</title>
<link>http://sange.fi/~atehwa/cgi-bin/piki.cgi/monadic%20composition%20instead%20of%20monad%20transformers</link>
<guid>http://sange.fi/~atehwa/cgi-bin/piki.cgi/#1336382909</guid>
<description>&lt;p&gt;As we probably all know, monads are handy programming constructs 
that let us express different "execution environments" and operations 
therein in a uniform way. Every monad gives the programmer some kind of 
&lt;del&gt;an&lt;/del&gt; environment where it is possible perform arbitrary 
computations in the ordinary way, while the monad controls the workings 
of the environment and usually provides some additional services 
specific to that environment. For instance, the ''state monad'' 
provides an environment with two additional services, storing a value 
for later reference and querying the stored value. The monad takes care 
of threading this stored value through the whole computation, including 
recursive calls, etc. Any kind of effect that could be achieved through 
"side effects" (violations of functional purity) can be modelled as 
services in a monad that is specifically designed to be able to provide 
those services. 

&lt;p&gt;[...]

&lt;p&gt;Monadic parsers are a prime &lt;del&gt;example.&lt;/del&gt; &lt;ins&gt;example of 
monadic composition.&lt;/ins&gt; A parser is a function that takes an "input 
stream" (a string, for instance), and returns a value (the result of 
parsing the beginning of the input stream) and an updated input stream, 
with the already parsed content removed. {{{ parseInt "300 square 
meters" ==&gt; (300, " square meters") }}} The input stream can be seen as 
a state which is inspected by various parsing operations (for finding 
out the result of parsing) and updated (by dropping elements from the 
beginning). It must be threaded (with updates and all) through the 
whole parsing computation, so it is useful to use the state monad to 
handle the input stream. 

&lt;p&gt;But, it also happens that parsers are functions that can ''fail'' 
under invalid input. For instance, the result of parseInt "you suck" 
should probably not be any trash integer but rather a value denoting 
failure. So a parser is also &lt;del&gt;and&lt;/del&gt; &lt;ins&gt;an&lt;/ins&gt; instance of 
the so called ''exception monad'', a monad whose sole service is to 
give a possibility to abort the computation and return nothing. (The 
exception monad can also provide some service to perform "alternative 
computations", trying out another computation in case the first one 
fails.) {{{ parseInt "300 square meters" ==&gt; Just (300, " square 
meters") parseInt "you suck" ==&gt; Nothing }}} (People who know the 
related literature will yell at me for introducing the exception monad, 
instead of the indeterminism monad, here. Let it be just for the sake 
of demonstration, right?) 

&lt;p&gt;[...]

&lt;p&gt;The downside, of course, is that you can't use the services provided 
by the existing monads but you'll have to write wrapper services for 
&lt;del&gt;those&lt;/del&gt; &lt;ins&gt;the services, in&lt;/ins&gt; addition to the 
definitions of bind (&gt;&gt;=) and return. 

&lt;p&gt;[...]

&lt;p&gt;[kategoria: ohjelmointi] &lt;ins&gt;[kategoria: keskeneräiset]&lt;/ins&gt;

</description>
<pubDate>Mon, 07 May 2012 09:28:29 +0000</pubDate>
</item>

</channel></rss>
