Functional Programming

A lot of programmers start with C, or C++, or Java. They’re currently the big three in the industry, with good reason: they’re time-tried, and cross-platform.

Other areas have their own (preferred) languages:

  • Web:
    • Ruby (Rails, Sinatra, Padrino)
    • Python (Django, Flask)
    • Java/Scala (Play)
    • Javascript (Node.js)
  • Academia (PLT):
    • Haskell
    • Standard ML
    • OCaml
    • Scheme
  • Systems Programming:
    • C/C++ (especially embedded)
    • Go
    • Rust
    • D

Of course, these languages can also be used for general-use programming. I regularly use Ruby for quick prototyping, and then move to another language more suited to what I actually need.

Functional programming is based on Alonzo Church’s λ-calculus.

One of the first real implementations of this was LISP in the late 1950’s. Lisp attempted to offer a simple mathematical style of programming to the then only imperitive field.

Later, other so-called “functional” languages emerged: Standard ML, Clean, Scheme, Common Lisp, Caml, OCaml, Haskell, Pure, etc. Only two of these are object-oriented: Common Lisp and OCaml, and neither are languages that traditional O-O programmers feel comfortable just look at. Common Lisp is obviously a Lisp, and OCaml is obviously an ML. Both are very powerful, but the syntax takes some getting used to.

In the age of languages like Ruby and Python, this shouldn’t be the case anymore. Ruby gives people a very close taste of what functional O-O might feel like. Scala does too. It just hasn’t been done well (Have you ever tried to curry a method in Scala?). And if you’re wondering why I’m not counting Javacript in here, it’s because Javascript doesn’t have a ‘classic’ object system, and instead uses prototypal inheritance (it’s also not technically functional, but that’s another rant).

This is something I’d like to have. Objects in functional language, that looks something like Ruby or Python. So I came up with Brick.

Brick is functional.
Brick is object-oriented.
Brick looks (something) like Ruby, Python, and Scala.

Here’s a taste:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Color(r : Fixnum, g : Fixnum, b : Fixnum) impl ToStr
  members
      r = r 
      g = g
      b = b

    impl ToStr
        method to_str
            "rgb({:d}, {:d}, {:d})".format!(this.r, this.g, this.b)


fn main
  let | c = Color(12, 18, 39)
      puts(c.to_str()) 

So let’s look at this. We have classes, declared with the class keyword. Following that is the class’ name, the default constructor, and the list of traits that we implement, here just ToStr.

After that is the members list, which lists all the slots our class has.

After the members list comes method declarations. Methods specifically all have the type of ThisClass -> something..... This means that there is an implicit first parameter that has the type of the current class. The name of this parameter is this.

The previous example can be simplified to

1
2
3
4
5
6
7
8
9
class Color(r:Fixnum, g:Fixnum, b:Fixnum) impl ToStr
    impl ToStr
        method to_str
            "rgb({:s}, {:s}, {:s})".format!(this.r, this.g, this.b)


fn main
  let | c = Color(12, 18, 39)
      puts(c.to_str()) 

So what do you think? Do you like this syntax? I like it. (It’s pretty clean)

Comments

A Brick a Day

I know a lot of programming languages, but I’m still not really happy with any of them. It started with PBASIC, then Objective-C, C, Common Lisp, Python, C++, Java, Ruby, Scheme, Assembly (Propeller), and now OCaml and Rust, in that order. Of all these languages, my favorites are probably Ruby, Common Lisp, and Rust.

As you can probably tell from that, I like functional programming. The idea that any call is merely a transformation of the initial data is really cool. But there’s a couple of bad problems I have with those three languages:

  • Speed
  • Portability, specifically to new architectures
  • Adoption

Each of these problems applies to at least two of the languages.

Ruby is flexible and powerful, but is slow as molasses. The Rubinius project is definitely helping with this, being a JIT for Ruby, but they still have a ways to go, as they’re currently working on 1.9.3 support. And, the overhead of a VM still exists. Out of the three languages, this definitely has the best community support (probably because of Rails, but beggers can’t be choosers). EDIT: Apparently, rubinius reached 2.0.0 without me noticing, and now supports Ruby 2.1 syntax. Way to go, RBX!

Common Lisp is the langauge for firsts. GC, cons pairs, compile-time AST editing (macros), type inference. If you can think of it, CL probably has it. And it has the speed Ruby doesn’t, with 30+ years of academic research poured into projects like SBCL. But it still falls short. The CL library scene is sad, and only recently has a decent dependency management system come to fruition. Yes, I’m looking at you, quicklisp. However, cross-compiling clisp is no easy feat, and running that on a microcontroller is not going to happen.

Rust is the newcomer to the scene, the proverbial hipster. There’s a great deal of hype surrounding Rust, at least at my school, and it looks absolutely amazing. But it’s a bit too C-like and low-level for my taste. Plus, there’s next to no documentation for the language, the best thing being the symbiotic project the language is being developed with, Servo. Out of all these languages, Rust is probably the closest to what I would like to work with on a daily basis.

Since no language has the features I want, or the facilities to modify it to the degree I want, I’ve decided to write my own language, called Brick.

This is not a small project.

Language design is a big deal, and I’ve spent the last two and a half months writing syntax ideas, reconciling possible conflicts, working out how systems might interact. And slowly, I came to a point where I started to know what this would be like. Next post, I’ll talk about some of the design decisions, and the syntax.

Comments