BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News PureScript: A Haskell-like Language that Compiles to JavaScript

PureScript: A Haskell-like Language that Compiles to JavaScript

This item in japanese

PureScript is a strongly, statically typed language which compiles to JavaScript. It is written in and inspired by Haskell and aims at "allowing to write very expressive code which is still clear and readable" when translated into JavaScript. Furthermore, says PureScript creator Phil Freeman, PureScript provides interoperability with other languages which target JavaScript.

PureScript inherits some of its distinguishing features from Haskell. Among them:

  • Type inference: which allows to deduce concrete types wherever it is possible;
  • Higher Kinded Polymorphism: which is an abstraction mechanism over type constructors essential with monads and DSLs.
  • Pattern matching: a technique that allows the developer to write compact, complex functions by breaking their implementation down into multiple cases.
  • Type Classes: an abstraction mechanism over data structures that allows to use them without having to think directly about the structure of the data itself.

Interoperability is another strength of PureScript, according to Phil. PureScript provides indeed the ability to import existing JavaScript code, by providing types for its values and functions, and then to use those functions in regular PureScript code. This makes it possible to use PureScript for just a subset of an app, and JavaScript or a JavaScript variant for the rest.

InfoQ has talked to Phil Freeman to learn more about the language.

Could you explain the process that led you to create PureScript? What were you main motivations and goals?

I started PureScript about a year ago. At the time, I was working on a medium-sized JavaScript application, which I was rewriting in TypeScript. For the most part, I'm very productive in TypeScript, but there was a fraction of the application which I felt could benefit from a stronger type system. At the time, there were a number of languages available which offered more expressive type systems, but none of them offered the combination of other features that I was looking for. Specifically, I wanted a language with Haskell-like syntax and the ability to generate clean, readable JavaScript, without the need for a runtime system. I knew that if I was going to write my own compiler then I would design the language in Haskell, but I made the decision to only use the simple parts of Haskell to implement PureScript, because I wanted to attract more compiler developers. In that regard, the project has been a success, because the compiler now has a great developer community.

What are the main features of PureScript?

A simple FFI, an expressive type system, and predictable code generation. The short answer is that PureScript provides "a better environment in which to write principled JavaScript".

Could you summarize the advantages that PureScript brings over plain JavaScript development?

One of the biggest benefits is the ability to refactor a large code base with confidence that you will not break existing functionality. Once you decide to use the techniques of pure functional programming - pure functions, immutable data structures, explicit side effects, etc. - you can refactor your code quite aggressively with the help of the compiler. Another benefit is the expressiveness of the type system, which allows the developer to assert properties of their programs which are difficult to express using existing JavaScript tools. For example, we can create domain-specific languages whose data structures are correct-by-construction - binary trees which are guaranteed to be balanced, HTML nodes which are guaranteed to have only the correct properties, etc. And we can accomplish this in a portable way, without having to rely on a specific IDE. Finally, languages like PureScript enable powerful forms of abstraction which aren't available in other languages, using tools like parametric polymorphism, higher order functions and type classes.

PureScript inherits some features from Haskell, such as type inference, type classes, and so on. Where do the two languages diverge mostly?

The simple answer is that PureScript looks a lot like Haskell syntactically, but the semantics differ because PureScript uses the semantics of JavaScript. For example, evaluation in PureScript is strict, unlike in Haskell. Also, because PureScript only performs tail-call optimization at the individual function level, it is important to watch out for stack overflows when using typical idioms from Haskell code (monadic recursion is one example of this). There are also some important differences in the type systems. PureScript has some neat features which were influenced by Haskell (multi-parameter type classes, and rank-N types, for example), but there are lots of features in Haskell which we don't have (type families, polykinds, datakinds, etc.) and some novel features in PureScript which don't exist in Haskell (extensible records and extensible effects).

How would you compare PureScript to other Haskell-like languages compiling to JavaScript such as [UHC], [ghcjs], etc.?

Many of the Haskell-like language AltJS languages, such as Fay, Haste and GHCJS, attempt to compile some subset of Haskell to JavaScript while preserving Haskell's semantics. This is a very powerful idea, because we can have a massive selection of existing Haskell code which can be reused in our JavaScript code. PureScript isn't a subset of Haskell, and uses JavaScript's semantics. However, one advantage is that PureScript's FFI is very easy to use, which means that we can easily integrate with existing JavaScript libraries, such as those provided by the NPM package manager. This leads to two very different development styles. PureScript is closer in spirit to languages like Roy or JMacro. In fact, Roy was a big influence on the design of PureScript. Elm is another very interesting Haskell-like language. Elm applies the techniques of pure functional programming, and functional reactive programming in particular, to the implementation of interactive web applications.

Could you briefly share your ideas about where PureScript is headed? What is on PureScript's roadmap?

Right now, we're focussed on the next release of the compiler and tools, which will hopefully include binary distributions for the three major platforms. We have also been spending quite a lot of time working on a self-hosted version of the PureScript compiler. It would be nice to be able to install the compiler using "npm install purescript". After the next release, we have plenty of interesting features to think about: type wildcards, template metaprogramming, idiom brackets and source maps, to name a few. There has also been some interest in alternative backends, with the addition of a new Lua backend quite recently, and some discussion of a possible Python backend as well. Of course, we have no shortage of interesting libraries and tools to write as well. The developer community is very welcoming, and anyone who is interested in contributing should check out the #purescript IRC channel on Freenode.

PureScript is an open source project, but prebuilt binaries are also available for Linux. Two great resources to get started with the language are a LeanPub book written by Phil Freeman, and the Try PureScript webpage, which allows to experiment with the language without even needing to install it.

Rate this Article

Adoption
Style

BT