the power of functional programming and static type ... · 2/18/2017  · the power of functional...

155
The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström https://wickstrom.tech Kats Conf , Dublin, Feb @owickstrom

Upload: others

Post on 25-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

The Power of Functional

Programming and Static Type

Systems in Server-Side Web

Applications

Oskar Wickström

https://wickstrom.tech

Kats Conf 2, Dublin, Feb 2017

@owickstrom

Page 2: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Elegant Weapons for a More CivilizedPage

@owickstrom

Page 3: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Me

• I live and work in Malmö, Sweden

• Started out in music

• Found PHP around 2011, then saved by FP soon after

• Worked on the Oden language last year

• Currently building CodeScene at work, mostly in Clojure

• Building Hyper in free time

@owickstrom

Page 4: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Me

• I live and work in Malmö, Sweden

• Started out in music

• Found PHP around 2011, then saved by FP soon after

• Worked on the Oden language last year

• Currently building CodeScene at work, mostly in Clojure

• Building Hyper in free time

@owickstrom

Page 5: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Me

• I live and work in Malmö, Sweden

• Started out in music

• Found PHP around 2011, then saved by FP soon after

• Worked on the Oden language last year

• Currently building CodeScene at work, mostly in Clojure

• Building Hyper in free time

@owickstrom

Page 6: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Me

• I live and work in Malmö, Sweden

• Started out in music

• Found PHP around 2011, then saved by FP soon after

• Worked on the Oden language last year

• Currently building CodeScene at work, mostly in Clojure

• Building Hyper in free time

@owickstrom

Page 7: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Me

• I live and work in Malmö, Sweden

• Started out in music

• Found PHP around 2011, then saved by FP soon after

• Worked on the Oden language last year

• Currently building CodeScene at work, mostly in Clojure

• Building Hyper in free time

@owickstrom

Page 8: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Me

• I live and work in Malmö, Sweden

• Started out in music

• Found PHP around 2011, then saved by FP soon after

• Worked on the Oden language last year

• Currently building CodeScene at work, mostly in Clojure

• Building Hyper in free time

@owickstrom

Page 9: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Me

• I live and work in Malmö, Sweden

• Started out in music

• Found PHP around 2011, then saved by FP soon after

• Worked on the Oden language last year

• Currently building CodeScene at work, mostly in Clojure

• Building Hyper in free time

@owickstrom

Page 10: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 11: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 12: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 13: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 14: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 15: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 16: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 17: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 18: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Agenda

• Overview: Functional Programming and Web Applications

• What about Server-Side Rendering?

• Static Typing for Server-Side Web

• Hyper

• Design

• Type-Level Routing

• XHR Clients

• Future Work

@owickstrom

Page 19: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Overview: Functional Programmingand Web Applications

@owickstrom

Page 20: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Functional Programming Influence

• FP influences Javascript

• ECMAScript 6 has higher-order functions, arrow function syntax

• Libraries like Underscore, Rambda, Fantasy Land

• React is functional at its core

• Functional Reactive Programming (FRP)

• Javascript as a compile target for FP languages

• Still, main focus is single-page apps

@owickstrom

Page 21: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Functional Programming Influence

• FP influences Javascript

• ECMAScript 6 has higher-order functions, arrow function syntax

• Libraries like Underscore, Rambda, Fantasy Land

• React is functional at its core

• Functional Reactive Programming (FRP)

• Javascript as a compile target for FP languages

• Still, main focus is single-page apps

@owickstrom

Page 22: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Functional Programming Influence

• FP influences Javascript

• ECMAScript 6 has higher-order functions, arrow function syntax

• Libraries like Underscore, Rambda, Fantasy Land

• React is functional at its core

• Functional Reactive Programming (FRP)

• Javascript as a compile target for FP languages

• Still, main focus is single-page apps

@owickstrom

Page 23: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Functional Programming Influence

• FP influences Javascript

• ECMAScript 6 has higher-order functions, arrow function syntax

• Libraries like Underscore, Rambda, Fantasy Land

• React is functional at its core

• Functional Reactive Programming (FRP)

• Javascript as a compile target for FP languages

• Still, main focus is single-page apps

@owickstrom

Page 24: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Functional Programming Influence

• FP influences Javascript

• ECMAScript 6 has higher-order functions, arrow function syntax

• Libraries like Underscore, Rambda, Fantasy Land

• React is functional at its core

• Functional Reactive Programming (FRP)

• Javascript as a compile target for FP languages

• Still, main focus is single-page apps

@owickstrom

Page 25: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Functional Programming Influence

• FP influences Javascript

• ECMAScript 6 has higher-order functions, arrow function syntax

• Libraries like Underscore, Rambda, Fantasy Land

• React is functional at its core

• Functional Reactive Programming (FRP)

• Javascript as a compile target for FP languages

• Still, main focus is single-page apps

@owickstrom

Page 26: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Functional Programming Influence

• FP influences Javascript

• ECMAScript 6 has higher-order functions, arrow function syntax

• Libraries like Underscore, Rambda, Fantasy Land

• React is functional at its core

• Functional Reactive Programming (FRP)

• Javascript as a compile target for FP languages

• Still, main focus is single-page apps

@owickstrom

Page 27: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Functional Programming Influence

• FP influences Javascript

• ECMAScript 6 has higher-order functions, arrow function syntax

• Libraries like Underscore, Rambda, Fantasy Land

• React is functional at its core

• Functional Reactive Programming (FRP)

• Javascript as a compile target for FP languages

• Still, main focus is single-page apps

@owickstrom

Page 28: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Single-Page Applications

• Work more like desktop applications

• There are a lot of these frameworks in JS

• Angular, Ember, Meteor, React (with friends)

• Without Javascript, you get nothing

• Reinventing the browser

@owickstrom

Page 29: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Single-Page Applications

• Work more like desktop applications

• There are a lot of these frameworks in JS

• Angular, Ember, Meteor, React (with friends)

• Without Javascript, you get nothing

• Reinventing the browser

@owickstrom

Page 30: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Single-Page Applications

• Work more like desktop applications

• There are a lot of these frameworks in JS

• Angular, Ember, Meteor, React (with friends)

• Without Javascript, you get nothing

• Reinventing the browser

@owickstrom

Page 31: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Single-Page Applications

• Work more like desktop applications

• There are a lot of these frameworks in JS

• Angular, Ember, Meteor, React (with friends)

• Without Javascript, you get nothing

• Reinventing the browser

@owickstrom

Page 32: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Single-Page Applications

• Work more like desktop applications

• There are a lot of these frameworks in JS

• Angular, Ember, Meteor, React (with friends)

• Without Javascript, you get nothing

• Reinventing the browser

@owickstrom

Page 33: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Single-Page Applications

• Work more like desktop applications

• There are a lot of these frameworks in JS

• Angular, Ember, Meteor, React (with friends)

• Without Javascript, you get nothing

• Reinventing the browser

@owickstrom

Page 34: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

What about Server-Side Rendering?

@owickstrom

Page 35: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Progressive Enhancement

@owickstrom

Page 36: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

80/20

@owickstrom

Page 37: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

“Isomorphic” Web Applications

• Goal: one framework that runs on both client and server

• “Free progressive enhancements”

• The client and server state machines

• Are we really talking about initial rendering?

@owickstrom

Page 38: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

“Isomorphic” Web Applications

• Goal: one framework that runs on both client and server

• “Free progressive enhancements”

• The client and server state machines

• Are we really talking about initial rendering?

@owickstrom

Page 39: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

“Isomorphic” Web Applications

• Goal: one framework that runs on both client and server

• “Free progressive enhancements”

• The client and server state machines

• Are we really talking about initial rendering?

@owickstrom

Page 40: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

“Isomorphic” Web Applications

• Goal: one framework that runs on both client and server

• “Free progressive enhancements”

• The client and server state machines

• Are we really talking about initial rendering?

@owickstrom

Page 41: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

“Isomorphic” Web Applications

• Goal: one framework that runs on both client and server

• “Free progressive enhancements”

• The client and server state machines

• Are we really talking about initial rendering?

@owickstrom

Page 42: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

PJAX

• Hooks in on link and form events

• Requests pages over XHR with special header

• Server responds with only inner content

• PJAX swaps the inner content on the client

@owickstrom

Page 43: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

PJAX

• Hooks in on link and form events

• Requests pages over XHR with special header

• Server responds with only inner content

• PJAX swaps the inner content on the client

@owickstrom

Page 44: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

PJAX

• Hooks in on link and form events

• Requests pages over XHR with special header

• Server responds with only inner content

• PJAX swaps the inner content on the client

@owickstrom

Page 45: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

PJAX

• Hooks in on link and form events

• Requests pages over XHR with special header

• Server responds with only inner content

• PJAX swaps the inner content on the client

@owickstrom

Page 46: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

PJAX

• Hooks in on link and form events

• Requests pages over XHR with special header

• Server responds with only inner content

• PJAX swaps the inner content on the client

@owickstrom

Page 47: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

If server-side web has toolingproblems, let’s build nice tools!

@owickstrom

Page 48: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Static Typing for Server-Side Web

@owickstrom

Page 49: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Static Typing for Server-Side Web

• Mainstream languages in web server programming

• Compile-time guarantees

• Safely abstract and compose

• Maintainable code

• Developer experience

@owickstrom

Page 50: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Static Typing for Server-Side Web

• Mainstream languages in web server programming

• Compile-time guarantees

• Safely abstract and compose

• Maintainable code

• Developer experience

@owickstrom

Page 51: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Static Typing for Server-Side Web

• Mainstream languages in web server programming

• Compile-time guarantees

• Safely abstract and compose

• Maintainable code

• Developer experience

@owickstrom

Page 52: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Static Typing for Server-Side Web

• Mainstream languages in web server programming

• Compile-time guarantees

• Safely abstract and compose

• Maintainable code

• Developer experience

@owickstrom

Page 53: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Static Typing for Server-Side Web

• Mainstream languages in web server programming

• Compile-time guarantees

• Safely abstract and compose

• Maintainable code

• Developer experience

@owickstrom

Page 54: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Static Typing for Server-Side Web

• Mainstream languages in web server programming

• Compile-time guarantees

• Safely abstract and compose

• Maintainable code

• Developer experience

@owickstrom

Page 55: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 56: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 57: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 58: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 59: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 60: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 61: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 62: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 63: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 64: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Things I’ve Found

• Haskell:

• Yesod

• Servant

• Scala:

• Play

• Rho

• PureScript:

• purescript-express

• purescript-rest

@owickstrom

Page 65: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 66: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 67: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 68: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 69: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 70: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 71: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 72: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 73: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 74: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Statically Typed Middleware

• Middleware is a common abstraction

• Very easy to mess up if dynamically typed

• Incorrect ordering

• Corrupt or incomplete responses

• Conflicting writes

• Incorrect error handling

• Consuming non-parsed, or badly parsed, request body

• Missing authentication and/or authorization

• Idea: use extensible records in PureScript!

@owickstrom

Page 75: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström
Page 76: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Let’s use extensible records inPureScript!

@owickstrom

Page 77: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Type-safe, statically checked composition of HTTP servers

@owickstrom

Page 78: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Goals

• A safe HTTP middleware architecture

• Make the effects of applying middleware explicit in types

• Ensure correct composition of middleware and application

components

• Interoperability with NodeJS and other backends (purerl,

purescript-native)

• No magic

@owickstrom

Page 79: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Goals

• A safe HTTP middleware architecture

• Make the effects of applying middleware explicit in types

• Ensure correct composition of middleware and application

components

• Interoperability with NodeJS and other backends (purerl,

purescript-native)

• No magic

@owickstrom

Page 80: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Goals

• A safe HTTP middleware architecture

• Make the effects of applying middleware explicit in types

• Ensure correct composition of middleware and application

components

• Interoperability with NodeJS and other backends (purerl,

purescript-native)

• No magic

@owickstrom

Page 81: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Goals

• A safe HTTP middleware architecture

• Make the effects of applying middleware explicit in types

• Ensure correct composition of middleware and application

components

• Interoperability with NodeJS and other backends (purerl,

purescript-native)

• No magic

@owickstrom

Page 82: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Goals

• A safe HTTP middleware architecture

• Make the effects of applying middleware explicit in types

• Ensure correct composition of middleware and application

components

• Interoperability with NodeJS and other backends (purerl,

purescript-native)

• No magic

@owickstrom

Page 83: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Goals

• A safe HTTP middleware architecture

• Make the effects of applying middleware explicit in types

• Ensure correct composition of middleware and application

components

• Interoperability with NodeJS and other backends (purerl,

purescript-native)

• No magic

@owickstrom

Page 84: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

How?

• Track middleware effects in type system, pure transformations and

side effects

• Leverage extensible records in PureScript

• Provide a common API for middleware

• Write backend-agnostic middleware where possible

• Integrate with existing NodeJS libraries

@owickstrom

Page 85: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

How?

• Track middleware effects in type system, pure transformations and

side effects

• Leverage extensible records in PureScript

• Provide a common API for middleware

• Write backend-agnostic middleware where possible

• Integrate with existing NodeJS libraries

@owickstrom

Page 86: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

How?

• Track middleware effects in type system, pure transformations and

side effects

• Leverage extensible records in PureScript

• Provide a common API for middleware

• Write backend-agnostic middleware where possible

• Integrate with existing NodeJS libraries

@owickstrom

Page 87: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

How?

• Track middleware effects in type system, pure transformations and

side effects

• Leverage extensible records in PureScript

• Provide a common API for middleware

• Write backend-agnostic middleware where possible

• Integrate with existing NodeJS libraries

@owickstrom

Page 88: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

How?

• Track middleware effects in type system, pure transformations and

side effects

• Leverage extensible records in PureScript

• Provide a common API for middleware

• Write backend-agnostic middleware where possible

• Integrate with existing NodeJS libraries

@owickstrom

Page 89: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

How?

• Track middleware effects in type system, pure transformations and

side effects

• Leverage extensible records in PureScript

• Provide a common API for middleware

• Write backend-agnostic middleware where possible

• Integrate with existing NodeJS libraries

@owickstrom

Page 90: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Design

@owickstrom

Page 91: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Conn

type Conn req res components ={ request :: req, response :: res, components :: components}

@owickstrom

Page 92: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Middleware (Old Design)

type Middleware m c c’ = c -> m c’

@owickstrom

Page 93: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Middleware (Old Design)

authenticateUser >=> parseForm >=> saveTodo

@owickstrom

Page 94: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Whoops, Not Safe

badMiddleware conn = do_ <- respond "First response" connrespond "Second response, crash!" conn

@owickstrom

Page 95: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Middleware (Revised)

newtype Middleware m i o a =Middleware (i -> m (Tuple a o))

@owickstrom

Page 96: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Response State Transitions

• Hyper tracks the state of response writing

• Guarantees correctness in response side effects

• Abstractions can be built on top safely

• Response-writing middleware can be backend-agnostic

@owickstrom

Page 97: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Response State Transitions

• Hyper tracks the state of response writing

• Guarantees correctness in response side effects

• Abstractions can be built on top safely

• Response-writing middleware can be backend-agnostic

@owickstrom

Page 98: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Response State Transitions

• Hyper tracks the state of response writing

• Guarantees correctness in response side effects

• Abstractions can be built on top safely

• Response-writing middleware can be backend-agnostic

@owickstrom

Page 99: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Response State Transitions

• Hyper tracks the state of response writing

• Guarantees correctness in response side effects

• Abstractions can be built on top safely

• Response-writing middleware can be backend-agnostic

@owickstrom

Page 100: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Response State Transitions

• Hyper tracks the state of response writing

• Guarantees correctness in response side effects

• Abstractions can be built on top safely

• Response-writing middleware can be backend-agnostic

@owickstrom

Page 101: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseStateTransition

type ResponseStateTransition m rw from to =

forall req res c.Middlewarem(Conn req {writer :: rw from | res} c)(Conn req {writer :: rw to | res} c)Unit

@owickstrom

Page 102: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseStateTransition

type ResponseStateTransition m rw from to =forall req res c.

Middlewarem(Conn req {writer :: rw from | res} c)(Conn req {writer :: rw to | res} c)Unit

@owickstrom

Page 103: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseStateTransition

type ResponseStateTransition m rw from to =forall req res c.Middlewarem

(Conn req {writer :: rw from | res} c)(Conn req {writer :: rw to | res} c)Unit

@owickstrom

Page 104: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseStateTransition

type ResponseStateTransition m rw from to =forall req res c.Middlewarem(Conn req {writer :: rw from | res} c)

(Conn req {writer :: rw to | res} c)Unit

@owickstrom

Page 105: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseStateTransition

type ResponseStateTransition m rw from to =forall req res c.Middlewarem(Conn req {writer :: rw from | res} c)(Conn req {writer :: rw to | res} c)

Unit

@owickstrom

Page 106: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseStateTransition

type ResponseStateTransition m rw from to =forall req res c.Middlewarem(Conn req {writer :: rw from | res} c)(Conn req {writer :: rw to | res} c)Unit

@owickstrom

Page 107: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseWriterclass ResponseWriter rw m b | rw -> b where

writeStatus:: Status-> ResponseStateTransition m rw StatusLineOpen HeadersOpen

writeHeader:: Header-> ResponseStateTransition m rw HeadersOpen HeadersOpen

closeHeaders:: ResponseStateTransition m rw HeadersOpen BodyOpen

send:: b-> ResponseStateTransition m rw BodyOpen BodyOpen

end:: ResponseStateTransition m rw BodyOpen ResponseEnded

@owickstrom

Page 108: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseWriterclass ResponseWriter rw m b | rw -> b wherewriteStatus:: Status-> ResponseStateTransition m rw StatusLineOpen HeadersOpen

writeHeader:: Header-> ResponseStateTransition m rw HeadersOpen HeadersOpen

closeHeaders:: ResponseStateTransition m rw HeadersOpen BodyOpen

send:: b-> ResponseStateTransition m rw BodyOpen BodyOpen

end:: ResponseStateTransition m rw BodyOpen ResponseEnded

@owickstrom

Page 109: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseWriterclass ResponseWriter rw m b | rw -> b wherewriteStatus:: Status-> ResponseStateTransition m rw StatusLineOpen HeadersOpen

writeHeader:: Header-> ResponseStateTransition m rw HeadersOpen HeadersOpen

closeHeaders:: ResponseStateTransition m rw HeadersOpen BodyOpen

send:: b-> ResponseStateTransition m rw BodyOpen BodyOpen

end:: ResponseStateTransition m rw BodyOpen ResponseEnded

@owickstrom

Page 110: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseWriterclass ResponseWriter rw m b | rw -> b wherewriteStatus:: Status-> ResponseStateTransition m rw StatusLineOpen HeadersOpen

writeHeader:: Header-> ResponseStateTransition m rw HeadersOpen HeadersOpen

closeHeaders:: ResponseStateTransition m rw HeadersOpen BodyOpen

send:: b-> ResponseStateTransition m rw BodyOpen BodyOpen

end:: ResponseStateTransition m rw BodyOpen ResponseEnded

@owickstrom

Page 111: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseWriterclass ResponseWriter rw m b | rw -> b wherewriteStatus:: Status-> ResponseStateTransition m rw StatusLineOpen HeadersOpen

writeHeader:: Header-> ResponseStateTransition m rw HeadersOpen HeadersOpen

closeHeaders:: ResponseStateTransition m rw HeadersOpen BodyOpen

send:: b-> ResponseStateTransition m rw BodyOpen BodyOpen

end:: ResponseStateTransition m rw BodyOpen ResponseEnded

@owickstrom

Page 112: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

ResponseWriterclass ResponseWriter rw m b | rw -> b wherewriteStatus:: Status-> ResponseStateTransition m rw StatusLineOpen HeadersOpen

writeHeader:: Header-> ResponseStateTransition m rw HeadersOpen HeadersOpen

closeHeaders:: ResponseStateTransition m rw HeadersOpen BodyOpen

send:: b-> ResponseStateTransition m rw BodyOpen BodyOpen

end:: ResponseStateTransition m rw BodyOpen ResponseEnded

@owickstrom

Page 113: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

What if we do it wrong?

v20 writeStatus statusOK21 :*> respond "Hello, Hyper!"22 :*> closeHeaders

^

Could not match type

BodyOpen

with type

HeadersOpen

@owickstrom

Page 114: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Writing Headers

headers

:: forall t m req res rw b c.(Traversable t, Monad m, ResponseWriter rw m b) =>t Header

-> Middlewarem(Conn req { writer :: rw HeadersOpen | res } c)(Conn req { writer :: rw BodyOpen | res } c)Unit

headers hs =traverse_ writeHeader hs:*> closeHeaders

@owickstrom

Page 115: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Writing Headers

headers:: forall t m req res rw b c.

(Traversable t, Monad m, ResponseWriter rw m b) =>t Header

-> Middlewarem(Conn req { writer :: rw HeadersOpen | res } c)(Conn req { writer :: rw BodyOpen | res } c)Unit

headers hs =traverse_ writeHeader hs:*> closeHeaders

@owickstrom

Page 116: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Writing Headers

headers:: forall t m req res rw b c.

(Traversable t, Monad m, ResponseWriter rw m b) =>t Header

-> Middlewarem(Conn req { writer :: rw HeadersOpen | res } c)(Conn req { writer :: rw BodyOpen | res } c)Unit

headers hs =traverse_ writeHeader hs:*> closeHeaders

@owickstrom

Page 117: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Writing Headers

headers:: forall t m req res rw b c.

(Traversable t, Monad m, ResponseWriter rw m b) =>t Header

-> Middlewarem(Conn req { writer :: rw HeadersOpen | res } c)(Conn req { writer :: rw BodyOpen | res } c)Unit

headers hs =traverse_ writeHeader hs:*> closeHeaders

@owickstrom

Page 118: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Type-Level Routing

@owickstrom

Page 119: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

A Routing Type

data Home = Home

type Site1 = Get HTML Home

@owickstrom

Page 120: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Handler

home :: forall m. Monad m=> ExceptT RoutingError m Home

home = pure Home

@owickstrom

Page 121: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

HTMLEncode

instance encodeHTMLHome :: EncodeHTML Home whereencodeHTML Home =p [] [ text "Welcome to my site!" ]

@owickstrom

Page 122: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Proxy

The Proxy type and values are for situations where type information is

required for an input to determine the type of an output, but where it is

not possible or convenient to provide a value for the input.

@owickstrom

Page 123: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Site Proxy

site1 :: Proxy Site1site1 = Proxy

@owickstrom

Page 124: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Site Router

onRoutingError status msg =writeStatus status:*> contentType textHTML:*> closeHeaders:*> respond (maybe "" id msg)

site1Router = router site1 home onRoutingError

@owickstrom

Page 125: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Main Entrypoint

main =runServer defaultOptions

onListeningonRequestError{}site1Router

whereonListening (Port port) =

log ("Listening on http://localhost:" <> show port)

onRequestError err =log ("Request failed: " <> show err)

@owickstrom

Page 126: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

More Routes!

@owickstrom

Page 127: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Multiple Endpoints with Captures

data Home = Home

data AllUsers = AllUsers (Array User)

newtype User = User { id :: Int, name :: String }

type Site2 =Get HTML Home:<|> "users" :/ Get HTML AllUsers:<|> "users" :/ Capture "user-id" Int :> Get HTML User

@owickstrom

Page 128: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Multiple Handlers

home :: forall m. Monad m => ExceptT RoutingError m Homehome = pure Home

allUsers :: forall m. Monad m => ExceptT RoutingError m AllUsersallUsers = AllUsers <$> getUsers

getUser :: forall m. Monad m => Int -> ExceptT RoutingError m UsergetUser id’ =

find userWithId <$> getUsers >>=case _ of

Just user -> pure userNothing ->

throwError (HTTPError { status: statusNotFound, message: Just "User not found."})

whereuserWithId (User u) = u.id == id’

@owickstrom

Page 129: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Multiple Handlers

home :: forall m. Monad m => ExceptT RoutingError m Homehome = pure Home

allUsers :: forall m. Monad m => ExceptT RoutingError m AllUsersallUsers = AllUsers <$> getUsers

getUser :: forall m. Monad m => Int -> ExceptT RoutingError m UsergetUser id’ =

find userWithId <$> getUsers >>=case _ of

Just user -> pure userNothing ->

throwError (HTTPError { status: statusNotFound, message: Just "User not found."})

whereuserWithId (User u) = u.id == id’

@owickstrom

Page 130: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Multiple Handlers

home :: forall m. Monad m => ExceptT RoutingError m Homehome = pure Home

allUsers :: forall m. Monad m => ExceptT RoutingError m AllUsersallUsers = AllUsers <$> getUsers

getUser :: forall m. Monad m => Int -> ExceptT RoutingError m UsergetUser id’ =find userWithId <$> getUsers >>=case _ ofJust user -> pure userNothing ->

throwError (HTTPError { status: statusNotFound, message: Just "User not found."})

whereuserWithId (User u) = u.id == id’

@owickstrom

Page 131: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Type-Safe Links

instance encodeHTMLAllUsers :: EncodeHTML AllUsers whereencodeHTML (AllUsers users) =

element_ "div" [ h1 [] [ text "Users" ], ul [] (map linkToUser users)]

wherelinkToUser (User u) =case linksTo site2 of_ :<|> _ :<|> getUser’ ->li [] [ linkTo (getUser’ u.id) [ text u.name ] ]

@owickstrom

Page 132: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Type-Safe Links

instance encodeHTMLAllUsers :: EncodeHTML AllUsers whereencodeHTML (AllUsers users) =element_ "div" [ h1 [] [ text "Users" ]

, ul [] (map linkToUser users)]

wherelinkToUser (User u) =case linksTo site2 of_ :<|> _ :<|> getUser’ ->li [] [ linkTo (getUser’ u.id) [ text u.name ] ]

@owickstrom

Page 133: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Type-Safe Links

instance encodeHTMLAllUsers :: EncodeHTML AllUsers whereencodeHTML (AllUsers users) =element_ "div" [ h1 [] [ text "Users" ]

, ul [] (map linkToUser users)]

wherelinkToUser (User u) =case linksTo site2 of_ :<|> _ :<|> getUser’ ->

li [] [ linkTo (getUser’ u.id) [ text u.name ] ]

@owickstrom

Page 134: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Multiple Endpoint Router

site2Router =router site2 (home :<|> allUsers :<|> getUser) onRoutingError

@owickstrom

Page 135: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Automatically Derived XHR Clients

@owickstrom

Page 136: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Shared Routing Type

type TaskId = Int

data Task = Task TaskId String

@owickstrom

Page 137: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Shared Routing Type

derive instance genericTask :: Generic Task

instance showTask :: Show Task whereshow = gShow

instance encodeJsonTask :: EncodeJson Task whereencodeJson = gEncodeJson

instance decodeJsonTask :: DecodeJson Task wheredecodeJson = gDecodeJson

@owickstrom

Page 138: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Shared Routing Type

type Site ="tasks" :/ (Get Json (Array Task)

:<|> Capture "id" TaskId :> Get Json Task)

site :: Proxy Sitesite = Proxy

@owickstrom

Page 139: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Shared Routing Type

type Site ="tasks" :/ (Get Json (Array Task)

:<|> Capture "id" TaskId :> Get Json Task)

site :: Proxy Sitesite = Proxy

@owickstrom

Page 140: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Shared Routing Type

update :: Action-> State-> EffModel State Action (ajax :: AJAX)

update RequestTasks state =case asClients site of

allTasks :<|> _ ->{ state: state { status = "Fetching tasks..." }, effects: [ ReceiveTasks <$> allTasks ]}

@owickstrom

Page 141: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Shared Routing Type

update :: Action-> State-> EffModel State Action (ajax :: AJAX)

update RequestTasks state =case asClients site ofallTasks :<|> _ ->

{ state: state { status = "Fetching tasks..." }, effects: [ ReceiveTasks <$> allTasks ]}

@owickstrom

Page 142: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Shared Routing Type

update :: Action-> State-> EffModel State Action (ajax :: AJAX)

update RequestTasks state =case asClients site ofallTasks :<|> _ ->{ state: state { status = "Fetching tasks..." }, effects: [ ReceiveTasks <$> allTasks ]}

@owickstrom

Page 143: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Other Possibilites/Future Work

• Type-safe forms

• PJAX, but with JSON data and client-side templates

• Mocked servers and clients using Arbitrary instances

• Ring-like response map abstraction

• Other backends

• Continue to Quest For Type-Safe Web!

@owickstrom

Page 144: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Other Possibilites/Future Work

• Type-safe forms

• PJAX, but with JSON data and client-side templates

• Mocked servers and clients using Arbitrary instances

• Ring-like response map abstraction

• Other backends

• Continue to Quest For Type-Safe Web!

@owickstrom

Page 145: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Other Possibilites/Future Work

• Type-safe forms

• PJAX, but with JSON data and client-side templates

• Mocked servers and clients using Arbitrary instances

• Ring-like response map abstraction

• Other backends

• Continue to Quest For Type-Safe Web!

@owickstrom

Page 146: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Other Possibilites/Future Work

• Type-safe forms

• PJAX, but with JSON data and client-side templates

• Mocked servers and clients using Arbitrary instances

• Ring-like response map abstraction

• Other backends

• Continue to Quest For Type-Safe Web!

@owickstrom

Page 147: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Other Possibilites/Future Work

• Type-safe forms

• PJAX, but with JSON data and client-side templates

• Mocked servers and clients using Arbitrary instances

• Ring-like response map abstraction

• Other backends

• Continue to Quest For Type-Safe Web!

@owickstrom

Page 148: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Other Possibilites/Future Work

• Type-safe forms

• PJAX, but with JSON data and client-side templates

• Mocked servers and clients using Arbitrary instances

• Ring-like response map abstraction

• Other backends

• Continue to Quest For Type-Safe Web!

@owickstrom

Page 149: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Other Possibilites/Future Work

• Type-safe forms

• PJAX, but with JSON data and client-side templates

• Mocked servers and clients using Arbitrary instances

• Ring-like response map abstraction

• Other backends

• Continue to Quest For Type-Safe Web!

@owickstrom

Page 150: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Summary

@owickstrom

Page 151: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Thank You!

@owickstrom

Page 152: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Useful References I

I Gustaf Nilsson Kotte. 6 Reasons Isomorphic Web Apps is not the

Silver Bullet You’re Looking For. url:

https://blog.jayway.com/2016/05/23/6-reasons-isomorphic-web-apps-not-

silver-bullet-youre-looking/.

I PJAX = pushState + ajax. url: https://github.com/defunkt/jquery-pjax.

I Yesod: Web Framework for Haskell. url: http://www.yesodweb.com/.

I Servant: A Type-Level Web DSL. url: https://haskell-servant.github.io/.

@owickstrom

Page 153: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Useful References II

I Play: The High Velocity Web Framework For Java and Scala. url:

https://www.playframework.com/.

I Rho: A DSL for building HTTP services with http4s. url:

https://github.com/http4s/rho.

I purescript-express: Purescript wrapper around Node.js Express

web-framework. url:

https://github.com/dancingrobot84/purescript-express.

I purescript-rest: A toolkit for creating REST services with Node and

PureScript. url: https://github.com/dicomgrid/purescript-rest.

@owickstrom

Page 154: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

Useful References III

I Hyper: Type-safe, statically checked composition of HTTP servers.

url: https://owickstrom.github.io/hyper/.

I purescript-proxy: Value proxy for type inputs. url:

https://pursuit.purescript.org/packages/purescript-proxy/1.0.0.

I Ring: Clojure HTTP server abstraction. url:

https://github.com/ring-clojure.

I Automatically derived XHR clients for Hyper routing types. url:

https://github.com/owickstrom/purescript-hyper-routing-xhr.

@owickstrom

Page 155: The Power of Functional Programming and Static Type ... · 2/18/2017  · The Power of Functional Programming and Static Type Systems in Server-Side Web Applications Oskar Wickström

The Power of Functional

Programming and Static Type

Systems in Server-Side Web

Applications

Oskar Wickström

https://wickstrom.tech

Kats Conf 2, Dublin, Feb 2017

@owickstrom