A Sensible Intro to FRP
July 27, 2016
Newsletter ↳
People keep asking what functional reactive programming is all about. Too often descriptions of FRP fall into extremes of sterile definitions or sentimental hand-waving. In this lecture Tikhon Jelvis explains the subject sensibly, giving a careful definition while bringing it to life with an example application and a comparison of Haskell libraries.
Summary
- What is functional reactive programming?
- Many people have vaguely heard of FRP, but it’s hard to find a concise easy answer
- We want to know its definition AND why it’s interesting
- The laws and definitions alone can seem like unmotivated abstract nonsense
- On the other hand motivation without definitions (aka the “bad monad tutorial”) makes you feel like you understand something but what you understand is not the abstraction itself
- If you only have a gist of how an abstraction should behave and you try to recreate it you get something that’s less well-behaved, less simple, and more difficult to work with…like JavaScript promise libraries
- So what is it?
- It’s a model for programming with time-varying values
- Imperative languages use state to maintain how things are now, which relies indirectly on time
- The word “functional” is almost misleading, we’re actually attempting to build systems that are simple, composable, and declarative
- FRP has two primary abstractions: Behaviors and Events
- Behaviors change continuously
- Events happen at points in time
- Time’s continuity makes these abstractions composable
- Like a vector representation of graphics, we make our measurement discrete only at the last possible moment
- These abstractions have simple semantics. For instance a Behavior is a function from time to values. Events are pairs of time and value.
- The temporal logic connection
- General FRP library interface
- Time itself is hidden, you work with Behaviors and Events
- There are combinators for those types
- There are boundary functions for input and output which are specific to subject matter like web programming or robotics
- Hypothetical example of input boundary functions
- Example of the game of life in FRP
- Functions to build and change the life grid
- Input sources: ticks, mouse, click, pauses
- Review of some Haskell operators
- Combinators to run the game
- How to easily extend a codebase
- Libraries
- reactive-banana - fast, good semantics
- threepenny-gui - lightweight UI framework, good for prototyping or internal tools, FRP layer based on reactive-banana
- reflex-frp - fast good semantics, integrates with GHCJS
- reflex-platform - easy setup for reflex-frp with Nix
- Open questions
- FRP could form an alternative basis for IO in Haskell…