Beating the Arc Challenge in Haskell
gist.github.com
gist.github.com
It's not obvious what counts as "the language" and what as "a library". Is printf part of C?
I suppose one could argue that if a library is general enough to make an entire class of programs shorter, as opposed to a few specific programs, then it gets to count as "the language". But that doesn't resolve the question so much as reword it.
Anyway, this is probably more of an objection to the original challenge than it is to this lovely Haskell code.
Also, everything in programming could be much less concise if you wanted it to be. Consider addition. It's an operator in your language. Then it's a CPU instruction. Then it's bits going through logic gates. Since it's a common thing to use, the complexity is abstracted away in many places.
If you are writing web apps, it makes sense for your web app libraries to abstract the web stuff away like this too.
So I write a templating module for OCaml that lets me beat or tie the Arc challenge. I compile it and never have to look at it again. It becomes part of my toolkit.
What do I have to do in order to win the challenge? Get my module accepted into some standard library? Why would what some language committee decides be important in terms of how expressive the language is?
It doesn't make sense to me. All it shows is that Paul controls Arc and has configured it to deliver small answers to questions that he finds important. I'm not sure it says anything about Arc or any other language for that matter. It might say something about functional versus imperative languages, but perhaps we already knew that?
What do I have to do in order to win the challenge?
I think the source of your perplexity may be that you're misunderstanding who (or rather what) is being challenged. It's not a contest between people, but between languages. So you don't win it; a language does.
The way a language wins is if you can write the program shorter using it plus some standard libraries. The definition of "standard" doesn't have to be exacting; anything short of a library written specifically to win this contest would be ok; so e.g. any library that existed prior to the challenge obviously would be.
Just another vote for 'I have no clue what the point of this is'.
The challenge seems to be:
* Write a library with helper functions. Like the Arc library.
* Now you can write a short program that uses that library to do things.
Obviously that's possible in any language, so I don't understand how this measures languages at all.The example in Arc:
(defop said req
(aform [onlink "click here" (pr "you said: " (arg _ "foo"))]
(input "foo")
(submit)))
Well this is a concise way to sum up what you want to do, but that could just be rewritten in any language of your choice. Then the appropriate libraries written. For example, java:createForm("click here", new Response("you said %foo"), new InputElement("foo"), new SubmitElement());
I think there's more important things to compare in languages. speed, error handling, stability, memory management etc
But I could just write 'arc' for Java, and the above example would fall out.
The solution for arc doesn't have anything unique in it that you can't do in other languages just as concisely.
So I don't think it says anything at all about the language, it just says how well you designed the interface to helper libraries.
>> "all the language features used in the Arc version of the challenge are used throughout news.arc."
I don't see any 'language features' in the solution for Arc. It's just functions and parameters. They're not language features, they're helper libraries. They may be well designed good libraries, but that's what they are. The language used is irrelevant. You could have written arc in BASIC and the solution would be the same.
Maybe I just don't understand why arc is referred to as a 'language' rather than a framework/library, and why that distinction is important.
If you wrote an Arc implementation in Java, then ran the program on top of that system, surely that would count as an instance of Arc winning the the challenge, not Java.
This is why the appropriate test is the ability for an average programmer to put together a DSL for a randomly-selected problem domain: it actually speaks to the power of the language. The characteristics of orthogonality and combinatorial flexibility is what DSLs are made to do. Arc doesn't have a monopoly on them. The only thing that is interesting here is your choice to include web-specific functionality in Arc. I think it's a great choice, but it just doesn't say much about the general expressiveness of the language as a whole.
We may have reached the point here where splitting hairs over DSLs versus included libraries is not going to get anybody anywhere. From what I understand, I would certainly agree that Arc programmers having such easy access to stateless web programming in a highly flexible manner is a great thing for the language.
But I would judge any language by the ability to easily add solutions to other problem domains that are highly orthogonal and flexible, not necessarily by the problem domains that are enabled by default.
Hope that makes sense. I think I finally figured out what your point was.
createForm("click here", new Response("you said %foo"), new InputElement("foo"), new SubmitElement());
Does this not look like a combination of highly orthogonal components that could be recombined to solve a variety of problems?
So no, I can't see any different whatsoever. I'm guessing we'll just have to agree to disagree at this point.
In terms of language design, if you were to write news.arc for Java, would you expect a solution to the challenge to just fall out? This probably says more about MzScheme+pg vs Java+you than it does about arc, however.
You've widened the scope of "language" to its libraries. And that's entirely fair. So, yes, Arc has it "built in", but Arc itself is (IMO) only marginally more mature than the libraries one might invent for (e.g.) Haskell to do the same thing (ok, I'm being a little unfair here, but not that much IMO). And one can invent those libraries for another language and can match Arc in the challenge using those invented libraries -- at least you don't seem to be denying this.
So, then, what's the point? That Arc already has the libraries available? That the Arc libraries meet the challenge? It certainly isn't that those libraries aren't possible in another language. The challenge means (almost) nothing with regards to comparing programming languages as you first implied, and is more about what tools and libraries were invented along with Arc to develop web apps.
Depends on the language, obviously. It seems unlikely you could in C, for example. Presumably the problems you'd encounter would gradually decrease as the language grew more powerful. That's why I phrased the problem as a challenge. I was curious to see what happened when you tried to solve this very simple problem using existing language/library options.
Yes, instead of creating a new library for the challenge you created the challenge to suit the library.
By the way, why did you choose to use string names for the data in the input form? Why not use variables directly and make aform work more like let (and like Mathematica's manipulate):
(form
(name (input-string))
(age (input-number))
(pr "Hello, " name ". You are " age " years old."))
Where input-X writes HTML output and returns a function that extracts the value of the field from the HTTP request. Validation works well too.A well-balanced "contest" would probably include a bunch of specifics (web, 3d, computation, ...) or stick to generic "programming" ("implement a self-balancing binary tree").
I see the Arc contest as more of something like, "take a look at what happens when you think about a problem space in depth and write a domain-specific language to make solving problems in that space really easy".
The only downside of Arc is that other domains are not as easy to work in. If you design a DSL on top of an existing language, then you don't have that limitation, which is what's good about the Haskell implementation. If I need to write a parser for part of my web app, I can use another nice DSL for that. If I used Arc, I would not have that option.
There's nothing about this problem that's biased towards Arc's strengths. Take input from a form and print it on the next page. Every popular language already had libraries for such basic things.
I suppose Arc was designed to win this contest in the sense that Arc is designed to make programs short, and winning in this contest is measured by brevity. Is that what you meant?
Not really. If I added a condition to the contest that you didn't take into account when writing Arc, Arc would no longer do as well. When the same person designs the programming language and the contest to "prove" it's the best, the contest is likely to prove that his programming language is the best. Not due to malice, but rather because both the language designer and contest designer think exactly the same way (as they are both the same person).
Ok. Now let's explore (or rather, return to) the question of whether that happened in this case.
Since I was aware people would make this type of criticism when I proposed the challenge, I made a conscious effort to make the problem very generic-- in fact to be the simplest stateful web app I could think of. In your opinion, did I succeed? Is it a simple, generic problem to ask for input on one page and display it on the next? Or is this a complex problem that requires unusual, Arc-specific functionality to solve?
Yes, that is the gist of it. More specifically, the challenge is, "at this point in time, has anyone gotten the appropriate code into a standard library?"
As far as the challenge is concerned, we have to take it on faith that when the challenge was posed, it was no more than a coincidence that pg's language supported an example written in a style pg likes.
So let's say I think CSV-file processing is important. If I show doing something useful and common with CSV files in 15-20 symbols with my language of choice would it be fair to challenge other languages to do the same?
If I understand you correctly, this all hinges around what libraries you think languages should have by default and how many symbols it takes to get something "common" done using these commonly-available pieces.
This is a little too subjective for me, although I'll easily grant that web programming is much more common than CSV-file processing.
Instead of relying on the arbitrariness of what components have been built or what various committees have approved, I would amend this to be something like "after an initial bit of programming not to exceed 3 days, how much symbology do I have to piece together to solve various problems in some sort of common domain like web question/response?" Or something like that. Because as a practical matter I'm always taking a bit to ramp up on new pieces of languages anyway and 3 days or so in this context is a nit.
I think that the most positive way of looking at it is to declare that session storage in a web app and parsing csv files are understood problems which we don't want to get bogged down solving again. Last week I was writing a small web app to browse a 2GB or so data set, which was provided to me variously in xml and csv data files in a zip file.
If I could have called:
import data_20091201.zip
and gotten something useful, I would be all the happier.I can now make that call, but in terms of getting stuff done, I'm judging my programming environment on the availability unzip utilities, xml and csv parsing libraries and database tools. As far as the language itself is concerned, I want to be able to structure my code neatly without worrying about forgotten temp files if the import fails.
I see the arc challenge as a proxy for making this sort of judgment, but it does come down to: "Do the people who maintain the language and contribute to its libraries worry about solving the sorts of problems that I would like to solve? And are they successful in making my life easier?"
[1] Subjective in the sense that the problem to solve is one that you may or may not care about solving, or may prefer to solve in a way that happens to use more code to make different aspects more explicit.
I honestly didn't think any of the things I did in the original Arc challenge required features that wouldn't already have existed in a language that had been used for web programming. All it does is ask the user to input something, then show it on another page. It's practically the hello world of stateful web apps. It seemed reasonable to expect any language that had been used for web apps would already have had convenient libraries for doing such basic things.
The reason I posted that was precisely because of the point where Arc shines: no inline HTML. But in practice, I want to avoid putting my presentation logic inline with my programming logic anyway.
And I fear that's the problem with the challenge; it seems to be highlighting the wrong kind of simplicity. I could add a few utility functions to make Ruby able to generate that HTML with a tiny amount of code, but I wouldn't ever actually use those functions in a real program.
Edit: Out of curiosity, I just implemented a pseudo-concision-obsessive "library" so see how a Ruby version might look:
I'll note that Rails does have things like those that are often used templates.
Another edit: Moved code samples to pastie for readability.
The three steps in user experience order:
first: a form to accept input,
second: a link to click
third: a printout of the input
Here are the orders used in code: Ruby+sinatra: third, first, second
arc version 1: third, second, first (original)
arc version 2: second, third, first (May 2009)
Haskell+custom: first, second, third
Interesting results, I think Haskell wins here. Though technically it fails the challenge (the arc version 2 example fails too) since the supporting library was written after the challenge was considered by the author of the example.Here is a version using the HTML::AsSubs module:
use Continuity;
use HTML::AsSubs;
Continuity->new->loop; # This starts the webserver
sub main {
my $request = shift;
my $p = 'foo';
$request->print( aform( $p )->as_HTML );
my $foo = $request->next->param( $p );
$request->print( w_link()->as_HTML );
$request->next->print( "You said: $foo" );
}
sub aform {
form(
input( { type => 'text', name => $_[0] } ),
input( { type => 'submit'} )
);
}
sub w_link { a( { href => '.' }, 'Click Here' ) }No, really. Web programming and continuation monads seem to have really it it off together. When you add them to another language, you get similar results (e.g., http://intensivesystems.net/tutorials/web_sessions.html).
http://arclanguage.org/item?id=979
The Haskell example is interesting, though, because it shows a working webapp in Haskell, and it's not even that scary.
The "meta" in Metaprogramming means that the program is able to manipulate itself (recursively) at runtime. I believe it's theoretically impossible to combine that with AOT type checking and Haskell without its type system would not be Haskell at all.
[edit] Of course you could argue that the sort of thing C++ does with templates is metaprogramming as well, only at compile time. In that case I suggest that we need another term for that because it's totally unlikle what Lisp is so good at.
And yes, the fact that I was essentially asking for a language with both static typing and runtime self-modification was the reason for the tongue-in-cheek "is that so much to ask". Might as well ask for a program that can compile any legal perl program [0].
That said, I suspect there's a lot that could be done to allow certain subsets of metaprogramming techniques in a static-typed language; some sort of crazy meta-type system that lets the compiler prove that something will only produce code with a particular polymorphic type, maybe? I don't know.
[0] EDIT: My bad, it's actually parsing perl that's impossible, cf. http://www.perlmonks.org/?node_id=663393
Also, you can generate code (at runtime) which is type-safe, compile the code, etc. Everything you ask for is possible in Haskell, however, it is definitely more complicated than in Lisp.
Static type checking proves that certain invariants hold at runtime and hence that certain defects are not possible. If a program can manipulate itself at runtime in arbitrary ways (not just reflect on itself in a read-only fashion), those proofs become invalid.
(edit: afaik it doesn't allow the full "recursive manipulation" you describe, but it does let you generate code with code)
For instance, in order to check that a particular function call conforms to the function signature, the function signature must at least exist. Type checking a call to a function for which not even the signature exists anwhere within the system is impossible.
To put it another way: metaprogramming might have effects at runtime, but not the kind of effects that change depending on what the runtime does. Metaprogramming should be deterministic for your program to be considered to be "in production."
Maybe it's not "native" and I have to run system level calls to force compiles and execution of created code.
Almost all programs do metaprogramming (interpreted code, scala on the jvm, etc).
What's the differentiatior for Lisps Metaprogramming (self contained languange structures?).
I'm pretty interested in simplification of programmer interfaces on iteratively more functional (but perhaps more complex) underbellies. I will certainly investigate.
But yes, C++ templates as well as Lisp macros are compile time metaprogramming. All languages that have eval and/or allow function/method bindings to be replaced at runtime allow runtime metaprogramming.
Since static typing guarantees certain invariants it cannot be compatible with a program that violates those invariants at runtime. You could still decide to consider it metaprogramming when the program manipulates itself only within the limits of those guarantees, but when you look at what real world runtime metaprogramming is being used for (for instance in Rails) you will realise that these things (e.g method_missing) would not be possible within the limits of a statically typed language.
[edit] Lisp makes both compile time and runtime metaprogramming exceptionally easy due to its homoiconic nature.
Whoa. That doesn't match my experience at all. I'd say it's simple and practically effortless (compared to what you'd have to do elsewhere). It would be surprising if it weren't, given that the whole language is organized around code=data.
http://cvs.haskell.org/Hugs/pages/libraries/base/Control-Con...
If someone built an AMQP library for haskell, we'd probably be 90% of the way there.
Not that it isn't easy to make a candied Lisp dialect with "friendly" syntax more appealing to non-Lisp programmers, but anyone who actually learns a Lisp quickly realizes that it just gets in the way...
If I were to personally try creating an "ideal language" to match my tongue-in-cheek plea, I'd probably start with a terse Lisp-like syntax and a HM-like type system and go from there.
See http://www.haskell.org/pipermail/xmonad/2008-November/006723... (if you don't like context, scroll down to 'I had to admit failure').
More practically, Haskell is going to ask you implicitly why you want a heterogenous list of functions. In all likelihood they're going to have a common interface at some point and you abstract around that so that the list is still "homogenous" (which is harder, but still pretty easy).
Shouldn't we confront them about this?
I do think there's a science of programming languages which can inform us how to best design the tools we use. Isn't it just possible that these Hindley-Milner languages are the next step in the progression that LISP started?
Look at the transition from Newtonian to Einsteinian mechanics. They were both great at the time of release, but one is clearly a step forward. The former is usually embeddable within the latter, but edge cases in the former don't quite work the same in the latter. And some cases in the former are just downright illegal in the latter. And unfortunately, the latter is a lot more complex for the power users. But, the latter is more expressive, teaches us more about the world... and enables completely new possibilities like nuclear fusion/fission.
This is like the difference between dynamic and static typing. Dynamic typing is certainly embeddable within Haskell. Just define a data type Dyn which is the sum of types Int, String, Float, [Dyn], and Dyn -> Dyn. You will see how much LISP is possible without much added syntactic cruft. But Haskell users don't need and want to operate in this way, because they can leverage the benefits of static typing.
Remember, these things don't come from whims of clever people or corporations. LISP and Haskell both come from research of very fundamental ideas. his is much more like real science than social science. And we should keep our eyes open to the future of this field, instead of proclaiming that we're finally done with our "100 year language".
Languages are a means to an end -- they help manage complexity while solving a problem. There's value in being able to extend a language's semantics to support a superset of several major language families, but unless handled very carefully, it can turn the host language into a sprawling mess in the process.
If I ever get past the first dozen projects on my list, I'd like to write a compiler for a dialect of Prolog designed with constraint analysis in mind. (I also need to read more about what's already been tried, first - this is just me being curious about how far constraint analysis could go and wondering out loud.) It would be tricky, but more feasible with Prolog-like semantics than in, say, C.
Dynamic typing is certainly embeddable within Haskell. Just define a data type Dyn which is the sum of types Int, String, Float, [Dyn], and Dyn -> Dyn. You will see how much LISP is possible without much added syntactic cruft. But Haskell users don't need and want to operate in this way, because they can leverage the benefits of static typing.
An enumerated type (as you describe) is very simple to implement, but true dynamic typing is a bit more complicated, and there's some constructs from dynamically-typed languages which can't be expressed within the bounds of Haskell's type system.The Data.Dynamic[1] module is a pretty good implementation of dynamic typing in Haskell, but it's still a bit awkward to use compared to a language which supports dynamic typing natively (eg Python).
[1] http://www.haskell.org/ghc/docs/latest/html/libraries/base-4...
It's possible they've discovered interesting ideas, but they're not on the line of development Lisp started. They grow more out of the Algol tradition.
I don't think anyone has ever proclaimed "we're finally done with our 100 year language." That would be extremely unlikely. The question is more which present languages are on the path to it.
Why do these preclude lisp? It seems to me you're drawing a false dichotomy.
Now, neither LISP nor Haskell are just lambda calculus... but I look at Haskell as a bigger language that subsumes most of LISP.
But, Haskell has no answer to LISP macros.
Lazy evaluation. Template Haskell. Quasiquotations (EDSLs). The GHC API. (Liskell.)
I believe between them anything Lisp macros can do they can do, though it may not be so easy. (Strangely, we seem to have little need of them, but I will leave it to the reader as to whether this is a Blub situation or regular Haskell is just that good.)
Really, how many programmers are out there who simultaneously 1) spend enough time with either language to master it 2) are smart enough to not only realize the language is limiting them, but to invent a new language to surpass those limits 3) aren't heavily tied to their current language 4) have enough spare time to bootstrap a new language with all the associated scaffolding (libraries, &c.) needed for anyone to want to try it?
[0] Feel free to substitute Scheme, an ML dialect, or other related languages into that sentence.
[1] http://en.wikipedia.org/wiki/Curry_(programming_language)
But that's a tradeoff you make. If you have anything more than the most rudimentary of basic syntax, macros become hard. Whereas most of the things that Haskell brings to the table can be incorporated into any other functional language.
I mean, I could write a one word web app:
dwim
(Details of the standard library implementation are left as an exercise.) def arc
with->>= name input
link "click here"
display++ "you said: " name Failed to load interface for `Control.Applicative.Error':
locations searched:
Control/Applicative/Error.hi
Control/Applicative/Error.hi-boot
Which library am I missing? apt-file search for Control/Applicative/Error.hi yielded none.Help appreciated from all HN-reading haskell'ers.