Neural Networks Part 1: Haskell

Neural Networks get a lot of buzz lately thanks to pretty much every tech firm buying up academic AI specialists. I wanted to play with some simple AI models, and because I’m warped, I chose to do it in Haskell.

Haskell. Because Lisp didn’t scare me off.

Below, I’ll describe what Haskell is, who uses it, and why I chose it.

What is Haskell?
Haskell is a strongly typed pure functional language with world-class concurrency and parallel libraries.
Strongly Typed: Languages like Java and C++ are strongly statically typed. This means that each declaration comes with its own type information. Languages like Lisp and JavaScript are weakly typed, meaning that a variable can be declared without any information about what type it is. Strongly typed languages can make guarantees about the code that you’ve written the moment that you’ve compiled it. Weakly-typed languages have this tendency to blow up in your face at runtime, as every JavaScript developer ever has encountered:
65215918

I prefer when my programs fail early, preferably before deployment, kind of like how NASA prefers not to put defective rocket parts into orbit.
Pure: Referentially transparent. When I call a function f(x) with x = 2, it always returns the same answer, just like we learned in middle school. In most programming languages, this isn’t the default behavior — these are often called imperative languages — C, C++, Java, JavaScript, and many others, including early functional languages like Lisp, allow updates to an internal state variable (or worse, a global variable) which affects the outcome of f(x) so that f(2) might not return the same result.
Another result of pure code is that the compiler can substitute a function call with the result of that call. This can sometimes save the program a great deal of work. While that’s theoretically interesting, I mostly care only that pure code lets me use algebraic reasoning.
Functional Language: Focuses on functions as first-class citizens, which can be passed around just like any other piece of data. In modern-day programming, often contrasted against Object-Oriented Programming. Functional languages have a lot of fancy tricks that make code a lot tighter and cleaner reading, provided that you know how to read it.
Concise, easy-to-read code saves debugging time.
World-class Concurrency and Parallelism: I can easily take advantage of my machine’s extra cores.

Who normally uses Haskell?
For a long time, academics. Recently, Haskell’s been used for applications in finance, biotechnology, distributed systems, and very recently, as a hobby language for wannabe SF/F writers who pretend they can code.

Why did I choose Haskell?
It gives me better-than-Java performance in a slim fraction of the lines of code. I probably haven’t illustrated that enough above, so I’ll indulge in some illustrative code now:

Java:

class Item {
    public double price;
    public boolean sold;
    public String name;
}

Haskell:

data Item = Item Double Boolean String

I included no constructor in the Java sample, committed heresy by breaking encapsulation and making each field public so I didn’t have to write getters and setters, and it’s still not as concise. This is far from production-level Java, where I’d have two options:

1) The standard way:

class Item {
    private double price;
    private boolean sold;
    private String name;

    public Item(double price, boolean sold, String name) {
        this.price = price;
        this.sold = sold;
        this.name = name;
    }

    public double getPrice() {
        return price;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public boolean isSold() {
        return sold;
    }

    public void setSold(boolean sold) {
        this.sold = sold;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

2) A more concurrency-safe approach that’ll work the garbage collector harder, since any changes to the object must result in creating a new object.

class Item {
    public final double price;
    public final boolean sold;
    public final String name;

    public Item(double price, boolean sold, String name) {
        this.price = price;
        this.sold = sold;
        this.name = name;
    }
}

This is only one example of Haskell’s devtime-saving features. There are a good many others.

About my claim of “better than Java” speed: Haskell compiles to native code, and in some cases competes with C and C++. Right now, I make my living as a full-stack developer. Were I using the Haskell Servant libraries to build a webservice, it’d be faster than a Tomcat Java server or a NodeJS server even before hand-tuning. It’d also be less lines of code. For most applications Haskell is “fast enough” without any monkey business.

For the use-cases I have in mind, I want my Neural Network to be really fast. I’ll probably say it again in those future posts, but keep in mind that for most cases, the naive Haskell prototype is plenty fast enough to put into production.

I hope I’ve made a compelling argument for why I chose Haskell as my prototyping language. In my next post, I’ll talk about my Neural Network implementation, including working Haskell code.

3 thoughts on “Neural Networks Part 1: Haskell

  1. Hey Greg!

    Interesting to see you landed on Haskell. I went to Evergreen State College to finish up my CS degree and they were really big on Haskell. They actually taught a quarter of Haskell a first language to new CS students prior to introducing them to Java. Beyond that, there was a ton of Haskell sprinkled into other curriculum (namely Computability classes).

    I remember my first time trying to write something in Haskell and thinking it was just the most useless piece of garbage, but it definitely grew on me over time. Being introduced to Functional concepts early really comes in handy nowadays, since so many languages have functional constructs in them now (lambdas, etc).

    Anyways just wanted to say hi, great to see you blogging on here!

  2. Hey Greg!

    Interesting to see you landed on Haskell. I went to Evergreen State College to finish up my CS degree and they were really big on Haskell. They actually taught a quarter of Haskell a first language to new CS students prior to introducing them to Java. Beyond that, there was a ton of Haskell sprinkled into other curriculum (namely Computability classes).

    I remember my first time trying to write something in Haskell and thinking it was just the most useless piece of garbage, but it definitely grew on me over time. Being introduced to Functional concepts early really comes in handy nowadays, since so many languages have functional constructs in them now (lambdas, etc).

    Anyways just wanted to say hi, great to see you blogging on here!

    1. I think that Racket/Scheme LISP is a much better starter language for beginning students than Haskell since there’s much less syntactic sugar (because you can’t sugar up parentheses easily.)

      Haskell has a lot of advantages that make it useful for production-quality systems so long as C/C++ levels of speed aren’t required, as I plan to cover in Part 6 and onward of this series, where I contrast the Haskell Neural Network’s performance against my C++ implementation’s performance.

      Glad to hear from you again, Justin! I hope the blog topics are enjoyable! 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s