Slide 1

Slide 1 text

Building Web Apps with Haskell and Elm Rebecca Skinner @cercerilla October 15, 2018 1

Slide 2

Slide 2 text

1 Introduction 2

Slide 3

Slide 3 text

Experience Report This presentation will focus on the experience of using these two technologies together to build a working application. 3

Slide 4

Slide 4 text

MetaLanguage, Not Machine Learning ML stands for MetaLanguage. It’s a language originally designed for working with the LCF theorem prover. Today, there are quite a few ML dialects, as well as several languages that have been influenced by ML 4

Slide 5

Slide 5 text

Haskell Haskell is a functional programming language with a syntax that is heavily influenced by the style of ML dialects. Haskell compiles to: • x86 • ARM • LLVM • Web Assembly • Javascript 5

Slide 6

Slide 6 text

Elm Elm is a functional language written in haskell and built for creating client-side single-page applications. Elm currently compiles to: • Javascript 6

Slide 7

Slide 7 text

2 Motivation 7

Slide 8

Slide 8 text

Make Programs that Don’t Go Wrong Debugging is much harder than writing code, so I would like the computer to help me find my errors early. 8

Slide 9

Slide 9 text

Make Refactoring Easier For long-lived applications, code is refactored much more often than it’s written. I want to work with tools that make refactoring easier and less error-prone. 9

Slide 10

Slide 10 text

Automate Away Tests I want the biggest return on investment with automated checking. I want to have to write as few tests as possible to ensure my programs work well. 10

Slide 11

Slide 11 text

Ergonomics ML-like languages have comfortable ergonomics that make them nicer to work with compared to languages like Javascript. 11

Slide 12

Slide 12 text

2.1 Non-Motivators 12

Slide 13

Slide 13 text

Popularity I don’t care how many people already know my implementation language. 13

Slide 14

Slide 14 text

Performance Raw performance is less important than good-enough performance along with correctness and ease of refactoring. 14

Slide 15

Slide 15 text

Library Support I only care about the libraries I need for my project, and not about the totality of the library ecosystem. 15

Slide 16

Slide 16 text

3 Building A Server 16

Slide 17

Slide 17 text

Haskell for Backend Services Haskell is a great choice for building web services. In particular, it has reasonable performance, good libraries, and can generally runs without crashing. 17

Slide 18

Slide 18 text

An Embarassement of Frameworks There are several web frameworks for haskell. My favorites are Scotty and Servant. • Scotty • Spock • Yesod • Happstack • Servant 18

Slide 19

Slide 19 text

Beam Me Up, Scotty main = scotty 8080 $ do S. get " / " d e f a u l t F i l e S. post " / : render " $ do outputFormat <− param " render " inputFormat <− param " format " toConvert <− getPostBody case convertDoc inputFormat outputFormat toConvert of Left errMsg −> do status status500 f i n i s h Right r e s u l t −> t e x t r e s u l t 19

Slide 20

Slide 20 text

The Toolchain • nix • stack • docker • quickcheck • hlint 20

Slide 21

Slide 21 text

If It Compiles, It Works Haskell’s type system helps find errors for us that we might miss in other languages. This can be a big help when we’re prototyping because it can save us from having to write a lot of tests or spending a lot of time debugging. 21

Slide 22

Slide 22 text

QuickCheck Once we decide to write tests, QuickCheck is a great way to write many test cases very quickly and easily. test_wordWrap : : Spec test_wordWrap = do i t " returns words in the r i g h t order " $ property $ \ str ’ ( Positive width ) −> l e t s t r = BS. pack str ’ words = concatMap BS. words $ HCat . wordWrap width s t r in words ‘ shouldBe ‘ (BS. words s t r ) i t " does not break a word " $ property $ \ ( Positive width ) −> l e t width ’ = width + 5 s t r = BS. pack $ replicate width ’ ’a ’ wrapped = HCat . wordWrap width s t r in do ( length wrapped ) ‘ shouldBe ‘ 1 (head wrapped ) ‘ shouldBe ‘ s t r i t " breaks l i n e s to the appropriate length " $ property $ \ str ’ ( Positive width ) −> l e t s t r = BS. pack str ’ lines = HCat . wordWrap width s t r in shouldSatisfy lines $ a l l $ \ l i n e −> (BS. length l i n e ) <= width | | ( length . BS. words $ l i n e ) == 1 22

Slide 23

Slide 23 text

4 Building A Frontend in Elm 23

Slide 24

Slide 24 text

Why Elm? Elm borrows syntax from Haskell while being narrowly focused on providing a useful language for the frontend. 24

Slide 25

Slide 25 text

The Elm Architecture The Elm Architecture is the control flow that elm applications follow. Elm applications are functional reactive event loops that should look familiar to you if you’ve worked with frameworks like react.js 25

Slide 26

Slide 26 text

The Differences Between Elm and Haskell Elm is a simpler language that is focused on ease of use, approachability, and cohesiveness. It’s less flexible, and offers fewer features than Haskell, but makes up for that with a consistent and coherent sandbox for developing SPAs. 26

Slide 27

Slide 27 text

5 A Survey of Other Options 27

Slide 28

Slide 28 text

Backend Languages • Go • Rust • Erlang • Ruby • Python • Javascript 28

Slide 29

Slide 29 text

Frontend Languages • PureScript • Haskell • Javascript • TypeScript 29

Slide 30

Slide 30 text

6 Demo 30