Tuesday, April 14, 2009

What a practical modern FPL ought to be

As just about everyone using FPLs, except a few fanboys, realizes, the functional languages available today are often super-awesome in some aspects, and totally suck in others. I did a detailed write-up on Clean last time. What follows is my opinion on what a modern practical functional language ought to be, but, sadly, ain't. Creating a decent seed implementation like that would probably take someone who knows what he's doing a year or so, taking advantage of existing code.

Syntax: Haskell-like, with significant white space. This is where Haskell and Clean get it right.

Macros: the need for macros is diminished in languages that can be lazy. I don't believe a language should be designed around macros, like Lisp is. Programming in Haskell, I rarely feel the need for macros, if at all. As a classic example, || can be defined as a function in it.

Evaluation: Functions should be strict by default, optionally lazy. Default data structures: lazy by default. This way, you get the best of both worlds. This is where Clojure gets it right, and almost no one else.

Runtime: JVM. JVM is very fast these days, and it will only get faster. Some people will bring up the lack of full tail-call optimization in JVM, but it's coming soon. JVM can do "unthinkable" feats like inlining virtual function calls across separate compilation units and even code generated at runtime. Not having to develop your own runtime is big. Having easy access to huge libraries is important too. And the fundamental difference between calling C from Haskell and, say, Java from Clojure is that the latter is memory-safe, even if you screw up.

Type system: Statically typed (if you still think that cool hackers can write correct programs in dynamically typed languages and sleep at night, google for how Hacker News got hacked, it was a type error!). Uniqueness types a la Clean are the way to go, in my opinion.

How to build it:

Port/combine code from the relevant languages (functional code should be easy to compose, own dogfood and all that). Some bootstrapping may be necessary.

Relevant languages:
  1. CAL: A Haskell-like language for JVM. Unfortunately lazy by default with somewhat odd syntax. No uniqueness types
  2. Haskell: runtime not very performant, not JVM; lazy by default; no uniqueness types
  3. Clojure: dynamically typed; syntax less readable than Haskell's, in my opinion. Great call on JVM and strict/lazy evaluation/data structures.
  4. Clean: great type system; runtime with no future; bad license; lazy by default
  5. MLs: strict and statically typed; but everything else is not so good
  6. Strict Haskell: doesn't really exist

9 comments:

  1. You do not mention F# - my favorite. It is very pragmatic. We have aroudn 80k lines so some reasonable experience. It meet much of what you describe.

    ReplyDelete
  2. Tried googling for how Hacker News got hacked - couldn't find it. Any chance of posting a URL?

    ReplyDelete
  3. Your answer is Clojure.

    There is no doubt static typing can detect some bugs more easily than dynamic typing. However experience has shown that no-one can always write correct programs in any language, typed or untyped, so this concern is much weaker than you present it.

    I find the Hacker News example mostly unconvincing because this system requires *dynamic* checking somewhere, because the browser is outside the control of the type system of the code on the server. Also:

    1. It seems both variables were boolean so a type system may not have choked.

    2. The style of http interaction does not hide enough to avoid being hacked. This would be a problem and could be rectified with static or dynamic typing.

    Here is my source issue:

    http://news.ycombinator.com/item?id=519433

    List de-structuring with the length mismatch ignored seems brittle. Someone using dynamically typed Oz could potentially use a record, which may have prevented the problem:

    http://www.mozart-oz.org/home/doc/tutorial/node3.html#label19

    However there are certainly cases where say Python does better than Java, for example the expression:

    "3 + 4 = " + 3 + 4

    Anyway I am not off to learn Clojure because I already know functional programming, so Prolog or something like that offers more challenge.

    ReplyDelete
  4. If you need a strict haskell with interesting type system (no monads) look here: http://www.haskell.org/haskellwiki/DDC

    ReplyDelete
  5. @ Considered Opinion:

    There is a number of things I don't like about F#, no the least of which is OO/FP hybridization and being closed-source and tied to MS. Also, I don't know F# too well, so I decided not to mention it instead of criticizing it.

    @ sanity:

    Google for "it was a type error" + "hacker news" + "paul graham" + hacked. I'm too LAZY to do it for you :)

    @ artsrc:

    In a functional language, the "destructuring" would be a tuple assignment. Little opportunity to screw up. Browsers are not the issue in web site security. Browsers can be faked arbitrarily.

    @ Vagif:

    I mentioned DDC 2 posts ago. Interesting, but not practical, and doesn't quite fit the bill overall.

    ReplyDelete
  6. This comment has been removed by the author.

    ReplyDelete
  7. @sanity my source on the bug is:

    http://news.ycombinator.com/item?id=519433

    The description of the bug was:

    "I generated the same form whether the fields were editable or not, and then later if there were no editable fields I just omitted the submit button. So anyone looking at the source of one of these pages could find a fnid that would work to modify the object displayed on it."

    "But the code that decided whether a field was modifiable was using a four variable list to destructure on a five value description of the field."


    @ fpmatters

    He did two things:

    1. Left a key to a map of functions on a page where it was not needed.

    2. Assigned a 4 value list to a 4 value description:

    l = [ 1, 2, 3, 4 ]
    (l1, l2, l3, l4, l5) = l

    And it failed silently.

    In Python it would do this:

    Traceback (most recent call last):
    File "stdin", line 1, in module
    ValueError: need more than 4 values to unpack

    This issue can be detected without strong typing.

    So it does not support your thesis that strong typing is essential because "Hacker News got hacked".

    On the real topic, what should a modern practical function programming language be. Technical solutions should solve problems. The JVM is unlikely to be best choice for massively parallel functional programs on the Cell architecture.

    To solve a technical problem think about the space of problems to solve, and design a solution for that space.

    Static typing adds complexity, and I think should be left out of languages used to teach most programming concepts.

    I am a big fan of significant white space for humans, but what of problem domain where an object database is used to store the program, and the user interaction is with a structured graphical format.

    Then a text representation if it exists should not use white space since humans would not use it much, and computers can parse other syntax more easily.

    I am reminded that there is no current popular web based Programming IDE, or Access database replacement, as areas where the text program requirement may be inappropriate.

    I suggesting that rather than one set of constraints, we should start by thinking about a problem scope where new, useful functional programming languages can thrive and thinking about the solution with more context.

    ReplyDelete
  8. You say "Some people will bring up the lack of full tail-call optimization in JVM, but it's coming soon," but I'm curious where you're getting that information from.

    I'd love for it to be true, but the following thread in the JVM-languages group seems to suggest that it's not a sure thing to get included:

    http://groups.google.com/group/jvm-languages/browse_thread/thread/327c9d2aa393b8f

    ReplyDelete
  9. "So it does not support your thesis that strong typing is essential because "Hacker News got hacked"."
    Actually, the point was "dynamic language hackers can make type mistakes and they can pass unnoticed".

    ReplyDelete