tag:blogger.com,1999:blog-28458034050209515842018-03-06T02:05:38.168-08:00Functional NonsenseA radical approach to purely functional programming, by a programmer who's fed up with Haskell's impurity, pragmatism, and willingness to compromise.jcasthttp://www.blogger.com/profile/03266202925733663072noreply@blogger.comBlogger6125tag:blogger.com,1999:blog-2845803405020951584.post-16912362029696894362011-04-18T08:26:00.000-07:002014-10-02T13:25:05.733-07:00Type-Class SillinessThe first code example of <a href="http://byorgey.wordpress.com/2011/04/18/monoids-for-maybe/">this post</a> is a perfect example of how type classes are over-used, how the newtype hack makes matters worse, and why Global Script uses functors instead. In Global Script, it would be:
<br />
<pre><code>
liftedMonoid :: ∀ 'f 'm. applicative.t f → monoid.t m → monoid.t (f m);</code></pre>
<pre><code>implicit liftedMonoid (module applicative.c);
'liftedMonoid (module 'app) (module 'mon) = 〈
'id = app.return mon.id;
'op = map2 @(module app) mon.op;
〉;
</code></pre>
More re-usable and no silly newtype wrapper.
<br />
Oh, and by the way: to use the above to write an instance for (→), if Global Script had a monoid type class (which it doesn't need):
<br />
<pre><code>
instance module monoid.c ('α → 'β) | module monoid.c β =</code></pre>
<pre><code> liftedMonoid (instance module monoid.c β)
;
</code></pre>
<pre><code>
</code></pre>
jcasthttp://www.blogger.com/profile/03266202925733663072noreply@blogger.com0tag:blogger.com,1999:blog-2845803405020951584.post-53147729162770573972010-08-11T18:40:00.000-07:002010-08-13T18:41:42.842-07:00The Iron Beach I/O Language, Part I<p>This is a Global Script-based concurrent procedural stream I/O language. The IBIO-specific portion of its standard library will be detailed in this post and a following one; subsequent posts will explain the concurrency and exception libraries (which are not IBIO-specific), and there will probably be a final follow-up post.</p>
<p>Rather than define a specific type which programs must have, IBIO provides a set of overloads and allows programs to be polymorphic over them. This allows IBIO programs to work more easily with monad transformers, and also allows other Global Script-based languages to support running IBIO programs and sub-programs (and, hence, libraries).</p>
<h4>Basic Functions</h4>
<pre><code>overload module ibio.c m</code></pre>
<p>The most important overload is <code>module ibio.c m</code>. An instance of this overload declares that subprograms of the type <code>m in out α</code> are capable of performing stream input of symbols of type <code>in</code> and stream output of symbols of type <code>out</code>; it is a sub-overload of <code>∀ 'in 'out. module monad.c (m in out)</code>.</p>
<pre><code>ibio.print :: ∀ 'm 'in ('out :: •). {module ibio.c m} →
[out] → m in out 〈〉</code></pre>
<p>This is IBIO's only exposed output subprogram. Operationally, the intention is that executing <code>print xn</code> <em>schedules</em> output of <code>xn</code> to the current output channel; outputs scheduled will be performed, in the order they were scheduled, in parallel with on-going execution. The list <code>xn</code> is operationally a stream, not a buffer, so some values may not be available immediately; output (but not execution) will be halted until the next symbol needed is available. In general, symbols will be output as soon as they are available; however, if the current output channel is attached to an external file, output may be halted for a bounded length of time to accumulate more symbols before sending them all off together.
<pre><code>ibio.parse :: ∀ 'm ('in :: •) 'out α. {module ibio.c m} →
(∀ 'p. {module ibio.parser.c p} → p in α) → m in out α</code></pre>
<p>This is IBIO's only exposed input subprogram. Operationally, the intent is that executing <code>parse p</code> will specialize <code>p</code> in an online parser type (see also the next section). The program will then schedule input of the maximal prefix of the contents of the current input channel which is recognized by the parser <code>p</code>; it is an error for there to be no such prefix. Input, like output, is performed in the order it is scheduled, and in parallel with on-going execution. <code>parse p</code> returns the parse tree resulting from matching the input scheduled against the parser <code>p</code>. Branching on a portion of the result may block until enough input has been performed to decide which constructor to use for that portion.</p>
<p>IBIO performs input lazily; in other words, only a bounded amount of input will be performed beyond the furthest symbol which has so far been actually required for execution. The exception to this is in the case where input is scheduled, and the result is discarded; in this case the input will be performed eagerly, since doing so does not consume excess memory to hang onto the input.</p>
<h4>Parsers</h4>
<p><code>mdoule ibio.parser.c p</code> is a sub-overload of <code>∀ 's. module monad.c (p s)</code> and <code>∀ 's. mdoule alternative.c (p s)</code>; it also defines the following methods:</p>
<pre><code>ibio.parser.symbol :: ∀ 'p 's. {module ibio.parser.c p} → p s s</code></pre>
<p>This permits input of a single symbol, provided the rest of the parser succeeds.</p>
<pre><code>ibio.parser.match :: ∀ 'p 's. {module ibio.parser.c p} →
regex.t s → p s [s]</code></pre>
<p>This permits input of a maximal string matching a supplied regular expression; the standard Global Script library may be assumed to supply a reasonable regular expression combinator library for this purpose. (<code>char</code> regular expressions may in Global Script be written <code>qr/re/</code>, where <code>re</code> indicates normal regular expression syntax; IBIO permits <code>match qr/re/</code> to be abbreviated <code>m/re/</code> as well.) <code>symbol</code> and <code>match</code> are both considered to match a single ‘token’; in the event of ambiguity, the branch whose initial token is of maximal length will be preferred. In the event this rule fails to eliminate the ambiguity, the length of the next token will be considered, and so on until the ambiguity is eliminated or every token has been considered. It is an error for ambiguity to remain after all accepted tokens in all branches have been accepted.</p>
<pre><code>ibio.parser.eof :: ∀ 'p 's. {module ibio.parser.c p} → p s 〈〉</code></pre>
IBIO makes the assumption that the operating system it is running on lacks an explicit EOF condition; instead, reading from a regular file with the file pointer at EOF successfully reads in an empty buffer and does not move the file pointer. Unix and Plan 9, at least, satisfy this condition. Therefore, when an IBIO input channel is attached to an external file, IBIO keeps track of when a read returns an empty buffer, and refuses to permit matching <code>symbol</code> or <code>match re</code> to read past that point. Instead, the special parser <code>eof</code> is supplied. This matches precisely when <code>symbol</code> would fail to match because input is at a point where an empty buffer was read in; it accepts the empty buffer and, in the event further reads would return data, permits input to proceed past it.</p>
<h4>Channels and Re-Direction</h4>
<pre><code>type ibio.in.p, ibio.out.p, ibio.socket, ibio.channel :: * → *</code></pre>
<p>These types store input channels, output channels, sockets, and in-process channels, respectively. A ‘socket’ for IBIO's purposes is just a pair of an input channel and an output channel with the same symbol type; it may represent something like one end of a network connection, or it may represent the read and write ends of two in-process channels.</p>
<pre><code>ibio.c.in.local :: ∀ 'm 'in0 'in1 'out 'α. {module ibio.c m} →
ibio.in.p in0 → m in0 out α → m in1 out α</code></pre>
<p>When an IBIO program begins executing, its input and output channels are attached to the initial process's standard input and standard output. <code>ibio.c.in.local</code> permits a sub-program to have its input attached to another input channel; <code>ibio.c.in.local p a</code> executes <code>a</code>, but with its input attached to <code>p</code> rather than to <code>ibio.c.in.local</code>'s input channel.</p>
<pre><code>ibio.c.out.local :: ∀ 'm 'in 'out0 'out1 'α. {module ibio.c m} →
ibio.out.p out0 → m in out0 α → m in out1 α</code></pre>
<p><code>ibio.c.out.local</code> permits a sub-program to have its output attached to an input channel other than the program's standard output; <code>ibio.c.out.local p a</code> executes <code>a</code>, but with its output attached to <code>p</code> rather than to <code>ibio.c.out.local</code>'s output channel.</p>
<pre><code>overload open a m s :: a → m s</code></pre>
<p><code>open</code> is, obviously, an incredibly general operation; it is intended to allow input and output channels to be obtained from other types, such as filenames and first-class channels. Major client of <code>open</code> are the high-level re-direction operators:</p>
<pre><code>(ibio.<<) :: ∀ 'in0 'in1 'out 'm 'a 'α.
{module ibio.c m} →
{ibio.open a (m in1 out) (ibio.in.p out0)} →
m in0 out α →
a →
m in1 out α
;
(ibio.>>) :: ∀ 'in0 'in1 'out 'm 'a 'α.
{module ibio.c m} →
{ibio.open a (m in out1) (ibio.out.p out0)} →
m in out0 α →
a →
m in out1 α
;
(ibio.<>) :: ∀ 'in 'out ('s :: •) 'm 'a 'α.
{module ibio.c m} →
{ibio.open a (m ibio.in out) (ibio.socket s)} →
m s s α →
a →
m in out α
;</code></pre>
<p>These permit input, output, or both to be re-directed from/to anything that can be opened at the appropriate type. In particular, to permit input and output to be re-directed from/to channels and sockets, <code>module ibio.c m</code> is a sub-overload of the following overloads:
<pre><code>
∀ 'in 'out ('s :: •). ibio.open (ibio.channel s) (m in out) (ibio.in.p s),
∀ 'in 'out ('s :: •). ibio.open (ibio.channel s) (m in out) (ibio.out.p s),
∀ 'in 'out ('s :: •). ibio.open (ibio.socket s) (m in out) (ibio.in.p s),
∀ 'in 'out ('s :: •). ibio.open (ibio.socket s) (m in out) (ibio.out.p s),
</code></pre>
<br/>
<pre><code>ibio.channel :: ∀ 'm 'in 'out ('s :: •). {module ibio.c m} →
m in out (channel s);
ibio.socket.pair :: ∀ 'm 'in 'out ('s :: •). {module ibio.c m} →
m in out 〈 0 :: socket s; 1 :: socket s; 〉;</code></pre>
<p>These two functions permit fresh in-process channels and socket-pairs, respectively, to be allocated, allowing for communication between different subprograms and particularly between subprograms running in different threads.</p>jcasthttp://www.blogger.com/profile/03266202925733663072noreply@blogger.com0tag:blogger.com,1999:blog-2845803405020951584.post-57482257223237039862010-07-02T13:25:00.000-07:002010-07-02T13:53:37.759-07:00Quick note on imports<pre><code>
import m.f; -- f must have only a single component
import module m.n; -- n must have only a single component
import type m.t; -- t must have only a single component
import m.(..); -- import everything
import m.(names); -- import certain names with a single declaration
import m.(..\names); -- import all but certain names
</code></pre>
To do this we basically want a way to say 'gimme a name provided it is not followed by a period' which we can probably do since it needs to be followed by a semicolon instead. We can parse the other cases pretty simply.jcasthttp://www.blogger.com/profile/03266202925733663072noreply@blogger.com0tag:blogger.com,1999:blog-2845803405020951584.post-13756040520812855742010-07-01T19:52:00.001-07:002010-07-01T21:05:49.079-07:00Haskell Package Review: regex-posixThe 15th most popular Haskell library is <a href="http://sourceforge.net/projects/lazy-regex"><code>regex-posix</code></a>. This library is an official component of the Haskell Platform, and is part of the "batteries included" concept. The following commentary is based primarily on its Haddock documentation; links are provided. <code>regex-posix</code> exports six modules:
<ul>
<li><a href="http://hackage.haskell.org/packages/archive/regex-posix/0.94.2/doc/html/Text-Regex-Posix.html"><code>Text.Regex.Posix</code></a> — directly exports only one symbol, a constant. Also re-exports instances from the remainder fo the package.</li>
<li><a href="http://hackage.haskell.org/packages/archive/regex-posix/0.94.2/doc/html/Text-Regex-Posix-ByteString.html"><code>Text.Regex.Posix.ByteString</code></a> — Exports four data types (including newtypes), three type synonyms, nine constants, and three functions, all in the IO monad; acocrding to the documentation, it also provides orphan instances for two type classes, <code>RegexLike</code> and <code>RegexMaker</code>, provided by the <code>regex-base</code> package.</li>
<li><a href="http://hackage.haskell.org/packages/archive/regex-posix/0.94.2/doc/html/Text-Regex-Posix-ByteString-Lazy.html"><code>Text.Regex.Posix.ByteString.Lazy</code></a> — Exports exactly the same API as the previous package, but with different orphan instances.</li>
<li><a href="http://hackage.haskell.org/packages/archive/regex-posix/0.94.2/doc/html/Text-Regex-Posix-Sequence.html"><code>Text.Regex.Posix.Sequence</code></a> — Exports exactly the same API as the previous package, but with different orphan instances.</li>
<li><a href="http://hackage.haskell.org/packages/archive/regex-posix/0.94.2/doc/html/Text-Regex-Posix-String.html"><code>Text.Regex.Posix.String</code></a> — Exports exactly the same API as the previous package, but with different orphan instances.</li>
<li><a href="http://hackage.haskell.org/packages/archive/regex-posix/0.94.2/doc/html/Text-Regex-Posix-Wrap.html"><code>Text.Regex.Posix.Wrap</code></a> — Exports four data types (including newtypes), three type synonyms, 21 constants, five functions in the IO monad, one constrained polymorphic function returning a simple value, and one constrained polymorphic function returning a value in an arbitrary monad.</li>
</ul>
So, in total, the library provides (including duplicates in multiple modules' export lists) 20 data types, 15 type synonyms, 58 constants, 17 functions in the IO monad, one constrained polymorphic function returning a simple value, and one constrained polymorphic function returning a value in an arbitrary monad.
A necessary condition for a function to be a combinator is that it have at least one argument of the same type as its result. Of the functions directly exported by this library, only four come close to satisfying this criterion: the four modules with nearly identical signatures all export a function
<pre><code>
regexec :: Regex -> string -> IO (Either WrapError (Maybe (string, string, string, [string])))
</code></pre>
for different types <code>string</code>. However, in fact, these functions are not combinators, for the following reasons:
<ul>
<li>The argument is of type <code>string</code>; the result is not. The result strings are in fact nested in a mult-result return wrapped inside three monads.</li>
<li>All three monads involved are exception monads, meaning that attempting to un-pack the result of a <code>regexec</code> call may fail to produce any <code>string</code> values at all.
<li>The <code>string</code> values returned, if any, are shorter and simpler than the input.
</ul>
All of these characteristics are decidedly un-combinatorial in nature.
The possibility remains that the instances exported by this package include one or more combinators as methods. Since the classes these instances implement are defined by the <code>regex-base</code> package, which these reviews will be reaching in a few days, we defer consideration to the review of that package. In the meantime, we can conclude that there is no evidence that this library is written in a combinatorial, rather than imperative, style.jcasthttp://www.blogger.com/profile/03266202925733663072noreply@blogger.com0tag:blogger.com,1999:blog-2845803405020951584.post-25159300917809685782010-06-30T18:29:00.000-07:002010-06-30T20:21:57.316-07:00Haskell Libraries: Procedural or CombinatorialFunctional programming is a style of programming that emphasizes the use of functions, especially pure functions.
That's boring. It's also not terribly obvious that functional programming, in that sense, is an improvement; going from this:
<pre><code>
int accum = 0, number;
while (getnumber(&number) != EOF)
accum += number;
printf("%d\n", accum);
</code></pre>
to this:
<pre><code>
int
get_accumulated_number(int accum)
{
int number;
if (getnumber(&number) == EOF)
return accum;
return get_accumulated_number(accum + number);
}
int accum = get_accumulated_number(0);
printf("%d\n", accum);
</code></pre>
is a pretty terrible regression, style-wise. I can't find who to cite this to, but it's been said that tail recursion is the goto of functional programming. So let's forget about using functions instead of loops and try to find a <em>good</em> reason to abandon conventional imperative languages.
Here's a better distinction. <em>Imperative</em> programming is a style of programming that emphasizes constructing programs out of sequences of actions for the machine to take. Imperative libraries tend to be collections of re-usable routines that encode commonly-performed sequences of actions; if they take parameters, those parameters tend to be flags controlling conditionals, or pieces of data to be manipulated. If they return results, those results tend to be data calculated produced the machine as it carries out the sub-routines actions.
<em>Combinatorial</em> programming is a style of programming that emphasizes constructing programs by composing sub-programs, without limiting how the sub-programs are composed. Combinatorial libraries tend to be collections of re-usable patterns of program composition. The results of their members tend to be sub-programs that can be combined with each other in a variety of ways; their parameters tend to likewise be sub-programs that the function will combine in interesting ways.
Combinatorial programming is the style of programming encouraged by the program calculus Global Script. As an example, in the Iron Beach I/O language, which is based on Global Script, the column-sum example might be written as follows:
<pre></code>
for (
'nums ← parse $ many $ number.parse;
'total = foldl (+) 0 $ nums;
) print $ number.toString total ++ qq{\n}
</code></pre>
Note that, unlike both the imperative and functional examples above, this program uses neither recursion nor general looping constructs; instead, combinators such as <code>many</code> and <code>foldl</code> provide special-purpose loops.
Haskell advertises itself as a functional programming language. But, as seen above, that's boring. Which style of programming does Haskell encourage, imperative or combinatorial? Do Haskell libraries, and the functions they offer, mostly allow you to re-use canned packages of commands for the machine to carry out? Or do they allow you to compose your own programs in elegant and expressive ways?
This is not an idle question: for an academic language, Haskell is a veritable hot-bed of library writing. The standard Haskell package distribution channel, known as <a href="http://hackage.haskell.org/packages/hackage.html">Hackage</a>, <a href="http://donsbot.wordpress.com/2010/06/30/popular-haskell-packages-q2-2010-report/">now has</a>:
<ul>
<li>2,182 packages, written by</li>
<li>575 separate programmers, uploading</li>
<li>12 new packages per day, with a total of</li>
<li>130,000 downloads per month.</li>
</ul>
It's time for a reckoning: is all that programming done in a combinatorial style, or is Haskell — in practice — just another conventional language with funny syntax?
Over the next week, I will examine the top 15 libraries, by total recent downloads, and attempt to classify the APIs they offer as combinatorial or imperative. My thesis is that Haskell development has, in the last 5-10 years, gone from a primarily combinatorial style to a primarily imperative one; as such, I will attempt to classify each library into one of two categories:
<ol>
<li>Legacy Haskell: more than 5-10 years old.</li>
<li>Imperative Haskell.</li>
</ol>
For the record, here are the most popular 15 libraries on Hackage, according to Haskell god Don Stewart:
<ul>
<li><a href="http://hackage.haskell.org/package/HTTP">HTTP</a></li>
<li><a href="http://hackage.haskell.org/package/zlib">zlib</a></li>
<li><a href="http://hackage.haskell.org/package/binary">binary</a></li>
<li><a href="http://hackage.haskell.org/package/network">network</a></li>
<li><a href="http://hackage.haskell.org/package/utf8-string">utf8-string</a></li>
<li><a href="http://hackage.haskell.org/package/Cabal">Cabal</a></li>
<li><a href="http://hackage.haskell.org/package/QuickCheck">QuickCheck</a></li>
<li><a href="http://hackage.haskell.org/package/mtl">mtl</a></li>
<li><a href="http://hackage.haskell.org/package/haskell-src-exts">haskell-src-exts</a></li>
<li><a href="http://hackage.haskell.org/package/regex-base">regex-base</a></li>
<li><a href="http://hackage.haskell.org/package/deepseq">deepseq</a></li>
<li><a href="http://hackage.haskell.org/package/ghc-paths">ghc-paths</a></li>
<li><a href="http://hackage.haskell.org/package/hslogger">hslogger</a></li>
<li><a href="http://hackage.haskell.org/package/regex-posix">regex-posix</a></li>
</ul>
I will examine each of these in turn, primarily looking for evidence against my thesis that Haskell programming has become primarily imperative in nature.jcasthttp://www.blogger.com/profile/03266202925733663072noreply@blogger.com0tag:blogger.com,1999:blog-2845803405020951584.post-11675731883296263212010-05-23T13:19:00.001-07:002010-05-23T13:40:37.345-07:00Re-thinking importsGlobal Script has a short import syntax that looks like this:
<pre><code>
import m.f; -- > 'f = m.f;
import type m.t -- > type 't = m.t;
import module m.n -- > module 'n = module m.n;
</code></pre>
I quite like this syntax; it has the nice bonus that, if you do need a string of imports, by formatting them this way:
<pre><code>
import xml.parser.attribute;
import xml.parser.cdata;
import xml.parser.element;
</code></pre>
you can alphabetize the import list by piping the entire code segment by M-2 on <code>|sort</code>. As an added bonus, this respects module names as well. This is fairly straight-forward to parse: take a name after the <code>import</code> keyword, check that it has multiple components, split it into a module name and a field name on the final component division, check that the field name is a valid function name, and store the module name and function name separately.
This syntax really only doesn't work for two cases:
<ul>
<li>Importing everything from a module, and</li>
<li>Importing a qualified name from a module.</li>
</ul>
For the second case, we can just say the programmer can suck it up and write:
<pre><code>
'n.f = m.n.f;
<code></pre>
For the first case, we can use the fact that spaces are never legal in identifiers to permit a syntax like:
<pre><code>
import m ..;
</code></pre>jcasthttp://www.blogger.com/profile/03266202925733663072noreply@blogger.com2