Purescript For Haskellers by Benjamin James Mikel Hart

January 19, 2022

In the Purescript For Haskellers presentation by Benjamin James Mikel Hart, he JavaScript, Haskell, and Purescript, highlighting their strengths and weaknesses. Ben is the director of cardano operations at MLabs.City. He introduce Purescript, demonstrate its features, and provide resources for learning. Ben expresses appreciation for JavaScript’s ability to run anywhere but criticizes its lack of type system and refactoring challenges. Haskell is praised for its advanced type system and correctness guarantees but criticized for its issues with multiple types of strings, performance, and unreliable maintenance. Purescript, a type-safe, Haskell-like language, is introduced as a solution, offering type classes, most Haskell features, and json and foreign parsing. The speaker discusses boolean values, records, FFI, and native Purescript function frameworks like Halogen and React. He sets up a project using Spago and Parcel, demonstrating hot reloading and DOM manipulation using FFI. The video concludes with Ben sharing his personal story of learning Haskell and encouraging viewers to explore Purescript further, with resources like Spago docs, “Learn Purescript by Example,” and the Real World Halogen project.

Purescript For Haskellers: A comprehensive overview

Comparing JavaScript and Haskell

Ben Hart began by comparing JavaScript and Haskell, emphasizing their respective strengths and weaknesses. JavaScript, despite lacking a type system and correctness guarantees, boasts a vast ecosystem and cross-platform compatibility. However, its runtime type errors and null types present significant challenges.

Conversely, Haskell shines with its advanced type system, correctness guarantees, and robust testing ecosystem. Yet, it faces issues like multiple string types, performance concerns, unreliable maintenance on critical libraries, and a complex ecosystem.

 

Enter Purescript

Purescript emerges as a type-safe, Haskell-like language that compiles to JavaScript, blending Haskell’s strengths with JavaScript’s compatibility. It supports type classes, most of Haskell’s features, and offers demanding user experiences. Purescript excels in converting untrusted JavaScript types into trusted Purescript types through JSON and foreign parsing.

 

Working with Booleans and Records

Ben elaborated on using boolean values and records in Purescript. Booleans can be used directly or as functions returning boolean values, with composition achievable through operators like or and and. Records in Purescript are standalone types that accept anonymous records or any record fitting the specified shape.

He also touched on FFI and native Purescript frameworks like Halogen and React, highlighting the explicit import system and the availability of generic implementations for various type classes. Purescript’s compiler offers linting and warnings, and symbolic inline functions enhance code readability. Debugging is facilitated through the spy function, and the language supports flexible back-ends for compiling to languages beyond JavaScript.

 

Setting up a Purescript project

Ben discussed the maturity of the Purescript library, its extensive package collection, performance, and compatibility with npm libraries. He recommended tools like Spago, PSCID, and PSC Ide, and described the use of effects instead of I/O in Purescript.

He walked through setting up a project using Spago, explaining dependencies and package sets, and demonstrated a basic “hello world” program. His goal was to run the program in a browser, using Parcel as a bundler. He detailed initializing a JavaScript project, installing Parcel, and adding necessary files and scripts.

 

DOM manipulation and FFI

Ben showcased how to run a Purescript project in the browser with hot reloading using Parcel. He introduced DOM manipulation using FFI and creating an opaque type for HTML, emphasizing the importance of treating string-to-HTML as an effect due to potential memory leaks and error handling.

 

Creating foreign modules

Ben explained creating foreign modules in Purescript for JavaScript interaction, highlighting the need for type safety and the use of libraries to enforce runtime type safety. He demonstrated a function to append a string to the document and discussed Purescript’s handling of units compared to Haskell.

 

Resources and learning Purescript

Ben concluded with recommendations for learning Purescript, such as the Spago documentation, the book Learn Purescript by Example, and the Real World Halogen project on GitHub. He shared his personal journey from learning Haskell to embracing Purescript, noting that learning Haskell significantly boosted his career despite its initial challenges.

 

Final thoughts

Ben Hart’s presentation provided a thorough introduction to Purescript for Haskell developers, highlighting its benefits, features, and practical applications. Whether you’re a seasoned Haskell developer or new to functional programming, exploring Purescript can open up new possibilities and enhance your development toolkit. For more in-depth information, be sure to check out the resources Hart recommended and dive into the world of Purescript.

 

Additional resources

Check out more from the MeetUp Func Prog Sweden. Func Prog Sweden is the community for anyone interested in functional programming. At the MeetUps the community explore different functional languages like Erlang, Elixir, Haskell, Scala, Clojure, OCaml, F# and more.