Getting started with PureScript

Posted on June 13, 2019 by Janey Muñoz
Tags: functional-programming, purescript

In this post I detail the steps I took in amalgamating a development environment for programming in the PureScript1 language. PureScript is “A strongly-typed functional programming language that compiles to JavaScript.” Dissecting that definition will elucidate my motivations for learning and developing with this language.

  1. A strongly-typed …

    The types of values are not implicitly converted by the compiler. This enables one to write fewer incorrect programs. Although not stated in the definition, another tangentially related feature of PureScript is that it is statically typed. In short, this means that your program is checked for correctness enforced by type specification at compile-time, avoiding a slew of errors at run-time.

  2. … functional programming language …

    The functional programming (FP) paradigm enforces several constraints and manners of program composition that enable the programmer to write less incorrect code. Essential to this is the explicit separation of ‘pure’ code from that with side effects, sometimes referred to as ‘IO.’ Further, information carried through the program is immutable. This has the benefits of avoiding erroneous ‘altering’ of information, in addition to leading the programmer to consider the computational ‘cost’ of manipulating the information. There are many who can better wax eloquent on the nuances and benefits of the FP paradigm, so I will leave it at that.

  3. … that compiles to JavaScript.

    JavaScript is the programming language that is used for making sites do interesting things on the World Wide Web.

In sum, I see benefit in being able to write more-correct code that does something useful and practical. By working with PureScript I hope to deepen my understanding of FP, along with widening my skill-set to command of web applications. On a more personal level, I have fun creating programs, and find satisfaction in doing that well.

Choosing where to begin

A brief search led me to an article2 outlining several of the build tools and package managers available. Spacchetti seemed the most appealing, mainly because it would be an opportunity to try out using Dhall3. Upon pursuit of Spacchetti, I learned that it is effectively deprecated, and that using spago4 would be the more appropriate option.

Getting spago going

I opted for compiling spago from source5.

$ git clone https://github.com/spacchetti/spago.git
...
$ stack install
...

On my machine6, the stack install command took about three minutes to complete. Upon completion, I was greeted with one warning7, which I hope to be effectively benign, and two executables installed in my /bin: spago and spago-curator.

Installing the PureScript compiler

At the time of this writing, the latest version of the PureScript compiler available on Homebrew is 0.12.5; spago loosely requires8 the most up-to-date version of the compiler, which is 0.13.0. Thus, I will install from source9 in lieu of Homebrew.

$ stack unpack purescript
...
$ cd purescript-0.13.0
$ stack install --flag purescript:RELEASE
...

It takes my machine about 18 minutes and lots of fan-spinning to progress through the installation. I am now rewarded with the purs executable installed in my /bin.

Testing the environment

Following the “Super quick tutorial”10 for spago, I can now see that my environment will successfully build.

$ spago build
...
Build succeeded.

Next I try to use node to see the project running, given that it was just successfully built. I already have node installed via Homebrew, so I input the following command readily.

$ node -e "require('.output/Main/index').main()"

I received an error in response11. In life, I choose to be an optimist, and that is why I hoped and believed that upgrading my node installation would solve my problem.

$ brew upgrade node
...
node -e "require('./output/Main/index').main()"
🍝

Success! Never have I been so pleased to see the spaghetti emoji.

References


  1. PureScript home page

  2. A quick overview of PureScript package managers as of October 2018

  3. dhall-lang on Github

  4. spago on Github

  5. spago on Github - installation

  6. Hardware: 2013 MacBook Air, 1.7 GHz Intel Core i7, 8 GB memory. Software: stack and git.

  7. A warning from stack after installing spago from source:

    warning: non-portable path to file
    '".stack-work/dist/x86_64-osx/Cabal-2.2.0.1/build/Spago/autogen/
        cabal_macros.h"';
    specified path differs in case from file name on disk
        [-Wnonportable-include-path]
    #include ".stack-work/dist/x86_64-osx/Cabal-2.2.0.1/build/spago/
        autogen/cabal_macros.h"
  8. The error message after calling spago build with the Homebrew version of the PureScript compiler:

      spago: Oh noes! It looks like the PureScript version installed
          on your system is not compatible with the package-set
          you're using.
    
      installed `purs` version:    0.12.5
      minimum package-set version: 0.13.0
    
      ...
    
      - if you know what you're doing and you want to void this check,
        you can override the `version` of the `metadata` package in
        the packages.dhall
  9. PureScript - Compiling from source

  10. spago - Super quick tutorial

  11. The error message when trying to use the node command in the terminal:

      dyld: Library not loaded: /usr/local/opt/icu4c/lib/
            libicui18n.62.dylib
        Referenced from: /usr/local/bin/node
        Reason: image not found
      Abort trap: 6