SlideShare a Scribd company logo
1 of 26
Download to read offline
Functional Reactive Programming and Elm
Sergei Winitzki
BayHac 2015
June 14, 2015
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 1 / 26
Part 1. Functional reactive programming and Elm
FRP has little to do with...
multithreading, message-passing concurrency, “actors”
distributed computing on massively parallel, load-balanced clusters
map/reduce, “reactive extensions”, the “reactive manifesto”
FRP means (in my definition)...
pure functions using temporal types as primitives
(temporal type ≈ lazy stream of values)
FRP is probably most useful for:
GUI and event-driven programming
Elm is...
a viable implementation of FRP geared for Web GUI apps
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 2 / 26
Difficulties in reactive programming
Imperative implementation is one problem among many...
Input signals may come at unpredictable times
Imperative updates are difficult to keep in the correct order
Flow of events becomes difficult to understand
Asynchronous (out-of-order) callback logic becomes opaque
“callback hell”: deeply nested callbacks, all mutating data
Inverted control (“the system will call you”) obscures the flow of data
Some concurrency is usually required (e.g. background tasks)
Explicit multithreaded code is hard to write and debug
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 3 / 26
FRP basics
Reactive programs work on infinite streams of input/output values
Main idea: make streams implicit, as a new “temporal” type
Σα — an infinite stream of values of type α
alternatively, Σα is a value of type α that “changes with time”
Reactive programs are viewed as pure functions
a GUI is a pure function of type Σ Inputs → Σ View
a Web server is a pure function Σ Request → Σ Response
all mutation is implicit in the program
instead of updating an x:Int, we define a value of type Σ Int
our code is 100% immutable, no side effects, no IO monads
asynchronous behavior is implicit: our code has no callbacks
concurrency / parallelism is implicit
the FRP runtime will provide the required scheduling of events
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 4 / 26
Czaplicki’s (2012) core Elm in a nutshell
Elm is a pure polymorphic λ-calculus with products and sums
Temporal type Σα — a lazy sequence of values of type α
Temporal combinators in core Elm:
constant: α → Σα
map2: (α → β → γ) → Σα → Σβ → Σγ
scan: (α → β → β) → β → Σα → Σβ
No nested temporal types: constant (constant x) is ill-typed!
Domain-specific primitive types: Bool, Int, Float, String, View
Standard library with data structures, HTML, HTTP, JSON, ...
...and signals Time.every, Mouse.position, Window.dimensions, ...
...and some utility functions: map, merge, drop, sampleOn, ...
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 5 / 26
Details: Elm type judgments [Czaplicki 2012]
Polymorphically typed λ-calculus (also with temporal types)
Γ, (x : α) e : β
Γ (λx.e) : α → β
Lambda
Γ e1 : α → β Γ e2 : α
Γ (e1 e2) : β
Apply
Temporal types are denoted by Στ
In these rules, type variables α, β, γ cannot involve Σ:
Γ e : α
Γ (constant e) : Σα
Const
Γ m : α → β → γ Γ p : Σα Γ q : Σβ
Γ (map2 m p q) : Σγ
Map2
Γ u : α → β → β Γ e : β Γ q : Σα
Γ (scan u e q) : Σβ
Scan
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 6 / 26
Elm operational semantics 0: Current values
An Elm program is a temporal expression of type ... → ΣView
Temporal expressions are built from input signals and combinators
It is not possible to “consume” a signal (Σα → β)
A value of type ΣΣα is impossible in a well-typed expression
Every temporal expression has a current value denoted by e[c]
Γ e : Σα Γ c : α
Γ e[c] : Σα
CurVal
Some current values are “freshly updated” while others are “stale”
Denote a fresh value by asterisk: e[c]∗
In Elm, current values are implemented with an Either type
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 7 / 26
Elm operational semantics 1: Initial values
Every temporal expression has an initial (stale) current value
Every predefined input signal i : Σα, i ∈ I has an initial value: i[a]
Initial current values for all expressions are derived:
Γ (constant c) : Σα
Γ (constant c)[c]
: Σα
ConstInit
Γ map2 m p[a] q[b] : Σγ
Γ (map2 m p q)[m a b]
: Σγ
Map2Init
Γ (scan u e q) : Σβ
Γ (scan u e q) [e] : Σβ
ScanInit
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 8 / 26
Elm operational semantics 2: Updating signals
Update steps happen only to input signals s ∈ I and one at a time
Update steps Us←a {...} are applied to the whole program at once:
Γ s : Σα s ∈ I Γ a : α Γ e[c] : Σβ Γ c : β
Γ Us←a e[c] ⇒ e[c ]
An update step on s will leave all other input signals unchanged:
∀s = s ∈ I : Us←b s[a]
⇒ s[b]∗
Us←b s [c]
⇒ s [c]
Elm has an efficient implementation:
The instances of input signals within expressions are not duplicated
Stale current values are cached and not recomputed
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 9 / 26
Elm operational semantics 3: Updating combinators
Operational semantics does not reduce temporal expressions:
The whole program remains a static temporal expression tree
Only the current values are updated in subexpressions"
Us←a (constant c)
[c]
⇒ (constant c)
[c]
ConstUpd
Us←a {map2 m p q}
⇒ map2 m Us←a {p}
[v]
Us←a {q}
[w]
[m v w]
Map2Upd
Us←a (scan u e q)
[b]
⇒ scan u e Us←a {q}
[c]∗
[u c b]∗
ScanPUpd
All computations during an update step are synchronous
The expression Us←b e[c]
is reduced after all subexpressions of e
Current values are non-temporal and are evaluated eagerly
map2 updates whenever one of its subexpressions update
scan updates only if its subexpression is freshly updated
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 10 / 26
GUI building: “Hello, world” in Elm
The value called main will be visualized by the runtime
import Graphics.Element (..)
import Text (..)
import Signal (..)
text : Element
text = plainText "Hello, World!"
main : Signal Element
main = constant text
Try Elm online at http://elm-lang.org/try
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 11 / 26
Example of using scan
Specification:
I work only after the boss comes by and unless the phone rings
Implementation:
after_unless : (Bool, Bool) -> Bool -> Bool
after_unless (b,p) w = (w || b) && not p
boss_and_phone : Signal (Bool,Bool)
i_work : Signal Bool
i_work = scan after_unless False (boss_and_phone)
Demo (boss_phone_work.elm)
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 12 / 26
Typical GUI boilerplate in Elm
A state machine with stepwise update:
update : Command → State → State
A rendering function (View is either Element or Html):
draw : State → View
A manager that merges the required input signals into one:
may use Mouse, Keyboard, Time, HTML stuff, etc.
merge_inputs : Signal Command
Main boilerplate:
init_state : State
main : Signal View
main = map draw (scan update init_state merge_inputs)
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 13 / 26
Some limitations of Elm-style FRP
No higher-order signals: Σ(Σα) is disallowed by the type system
No distinction between continuous time and discrete time
The signal processing logic is fully specified statically
No constructors for user-defined signals
No recursion possible in signal definition!
Awkward mechanisms for “tying the knot” in Elm
No full concurrency (e.g. “dining philosophers”)
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 14 / 26
Elm-style FRP: the good parts
Transparent, declarative modeling of data through ADTs
Immutable and safe data structures (Array, Dict, ...)
No runtime errors or exceptions!
Space/time leaks are impossible!
Language is Haskell-like but simpler for beginners
Full type inference
Easy deployment and interop in Web applications
Good support for HTML/CSS, HTTP requests, JSON
Good performance of caching HTML views
Support for Canvas and HTML-free UI building
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 15 / 26
Part 2. Temporal logic and FRP
Reminder (Curry-Howard): logical expressions will be types
...and the axioms will be primitive terms
We only need to control the order of events: no “hard real-time”
How to understand temporal logic:
classical propositional logic ≈ Boolean arithmetic
intuitionistic propositional logic ≈ same but without true / false
dichotomy
(linear-time) temporal logic LTL≈ Boolean arithmetic for infinite
sequences
intuitionistic temporal logic ITL≈ same but without true / false
dichotomy
In other words:
an ITL type represents a single infinite sequence of values
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 16 / 26
Boolean arithmetic: notation
Classical propositional (Boolean) logic: T, F, a ∨ b, a ∧ b, ¬a, a → b
A notation better adapted to school-level arithmetic: 1, 0, a + b, ab, a
The only “new rule” is 1 + 1 = 1
Define a → b = a + b
Some identities:
0a = 0, 1a = a, a + 0 = a, a + 1 = 1,
a + a = a, aa = a, a + a = 1, aa = 0,
(a + b) = a b , (ab) = a + b , a = a
a (b + c) = ab + ac, (a + b) (a + c) = a + bc
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 17 / 26
Boolean arithmetic: example
Of the three suspects A, B, C, only one is guilty of a crime.
Suspect A says: “B did it”. Suspect B says: “C is innocent.”
The guilty one is lying, the innocent ones tell the truth.
φ = ab c + a bc + a b c a b + ab b c + bc
Simplify: expand the brackets, omit aa , bb , cc , replace aa = a etc.:
φ = ab c + 0 + 0 = ab c
The guilty one is A.
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 18 / 26
Propositional linear-time temporal logic (LTL)
We work with infinite boolean sequences (“linear time”)
Boolean operations:
a = [a0, a1, a2, ...] ; b = [b0, b1, b2, ...] ;
a + b = [a0 + b0, a1 + b1, ...] ; a = a0, a1, ... ; ab = [a0b0, a1b1, ...]
Temporal operations:
(Next) Na = [a1, a2, ...]
(Sometimes) Fa = [a0 + a1 + a2 + ..., a1 + a2 + ..., ...]
(Always) Ga = [a0a1a2a3..., a1a2a3..., a2a3..., ...]
Other notation (from modal logic):
Na ≡ a; Fa ≡ ♦a; Ga ≡ a
Weak Until: pUq = “p holds from now on until q first becomes true”
pUq = q + pN (q + pN (q + ...))
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 19 / 26
LTL: temporal specification
Whenever the boss comes by my office, I will start working.
Once I start working, I will keep working until the telephone rings.
G ((b → Fw) (w → wUr)) = G b + Fw w + wUr
Whenever the button is pressed, the dialog will appear.
The dialog will disappear after 1 minute of user inactivity.
G (b → Fd) (d → Ft) d → dUtd
The timer t is an external event and is not specified here
Difficult to say “x stays true until further notice”
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 20 / 26
Temporal logic redux
Designers of FRP languages must face some choices:
LTL as type theory: do we use Nα, Fα, Gα as new types?
Are they to be functors, monads, ...?
Which temporal axioms to use as language primitives?
What is the operational semantics? (I.e., how to compile this?)
A sophisticated example: [Krishnaswamy 2013]
uses full LTL with higher-order temporal types and fixpoints
uses linear types to control space/time leaks
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 21 / 26
Interpreting values typed by LTL
What does it mean to have a value x of type, say, G(α → αUβ) ??
x : Nα means that x : α will be available only at the next time tick
(x is a deferred value of type α)
x : Fα means that x : α will be available at some future tick(s)
(x is an event of type α)
x : Gα means that a (different) value x : α is available at every tick
(x is an infinite stream of type α)
x : αUβ means a finite stream of α that may end with a β
Some temporal axioms of intuitionistic LTL:
(deferred apply) N (α → β) → (Nα → Nβ) ;
(streamed apply) G (α → β) → (Gα → Gβ) ;
(generate a stream) G (α → Nα) → (α → Gα) ;
(read infinite stream) Gα → αN(Gα)
(read finite stream) αUβ → β + αN(αUβ)
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 22 / 26
Elm as an FRP language
λ-calculus with type Gα, primitives constant, map2, scan
map2 : (α → β → γ) → Gα → Gβ → Gγ
scan : (α → β → β) → β → Gα → Gβ
constant : α → Gα
(map2 makes G an applicative functor)
Limitations:
Cannot have a type G(Gα), also not using N or F
Cannot construct temporal values by hand
This language is an incomplete Curry-Howard image of LTL!
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 23 / 26
Conclusions
There are some languages that implement FRP in various ad hoc ways
The ideal is not (yet) reached
Elm-style FRP is a promising step in the right direction
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 24 / 26
Abstract
In my day job, most bugs come from implementing reactive programs
imperatively. FRP is a declarative approach that promises to solve these
problems.
FRP can be defined as a λ-calculus that admits temporal types, i.e. types
given by a propositional intuitionistic linear-time temporal logic (LTL).
Although the Elm language uses only a subset of LTL, it achieves high
expressivity for GUI programming. I will formally define the operational
semantics of Elm. I discuss the advantages and the current limitations of
Elm. I also review the connections between temporal logic, FRP, and Elm.
My talk will be understandable to anyone familiar with Curry-Howard and
functional programming. The first part of the talk is a self-contained
presentation of Elm that does not rely on temporal logic or Curry-Howard.
The second part of the talk will explain the basic intuitions behind
temporal logic and its connection with FRP.
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 25 / 26
Suggested reading
E. Czaplicki, S. Chong. Asynchronous FRP for GUIs. (2013)
E. Czaplicki. Concurrent FRP for functional GUI (2012).
N. R. Krishnaswamy.
https://www.mpi-sws.org/∼neelk/simple-frp.pdfHigher-order functional
reactive programming without spacetime leaks(2013).
M. F. Dam. Lectures on temporal logic. Slides: Syntax and semantics of
LTL, A Hilbert-style proof system for LTL
E. Bainomugisha, et al. A survey of reactive programming (2013).
W. Jeltsch. Temporal logic with Until, Functional Reactive Programming
with processes, and concrete process categories. (2013).
A. Jeffrey. LTL types FRP. (2012).
D. Marchignoli. Natural deduction systems for temporal logic. (2002). –
See Chapter 2 for a natural deduction system for modal and temporal
logics.
Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 26 / 26

More Related Content

What's hot

Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingFilip De Sutter
 
Sets, maps and hash tables (Java Collections)
Sets, maps and hash tables (Java Collections)Sets, maps and hash tables (Java Collections)
Sets, maps and hash tables (Java Collections)Fulvio Corno
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scalaPiotr Paradziński
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS ResponsibilitiesBrendan Eich
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In ScalaSkills Matter
 
Scope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesScope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesEelco Visser
 
Declarative Language Definition
Declarative Language DefinitionDeclarative Language Definition
Declarative Language DefinitionEelco Visser
 
Structured Interactive Scores with formal semantics
Structured Interactive Scores with formal semanticsStructured Interactive Scores with formal semantics
Structured Interactive Scores with formal semanticsMauricio Toro-Bermudez, PhD
 
Variational Autoencoded Regression of Visual Data with Generative Adversarial...
Variational Autoencoded Regression of Visual Data with Generative Adversarial...Variational Autoencoded Regression of Visual Data with Generative Adversarial...
Variational Autoencoded Regression of Visual Data with Generative Adversarial...NAVER Engineering
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...Philip Schwarz
 
A born-again programmer's odyssey
A born-again programmer's odysseyA born-again programmer's odyssey
A born-again programmer's odysseyIgor Rivin
 
Functional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic ProgrammerFunctional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic ProgrammerRaúl Raja Martínez
 
Machine learning with scikit-learn
Machine learning with scikit-learnMachine learning with scikit-learn
Machine learning with scikit-learnQingkai Kong
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with ScalaDenis
 
VAE-type Deep Generative Models
VAE-type Deep Generative ModelsVAE-type Deep Generative Models
VAE-type Deep Generative ModelsKenta Oono
 
NIPS2017 Few-shot Learning and Graph Convolution
NIPS2017 Few-shot Learning and Graph ConvolutionNIPS2017 Few-shot Learning and Graph Convolution
NIPS2017 Few-shot Learning and Graph ConvolutionKazuki Fujikawa
 
Parallel Optimization in Machine Learning
Parallel Optimization in Machine LearningParallel Optimization in Machine Learning
Parallel Optimization in Machine LearningFabian Pedregosa
 

What's hot (20)

Presentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional ProgrammingPresentation of GetTogether on Functional Programming
Presentation of GetTogether on Functional Programming
 
Sets, maps and hash tables (Java Collections)
Sets, maps and hash tables (Java Collections)Sets, maps and hash tables (Java Collections)
Sets, maps and hash tables (Java Collections)
 
Contravariant functors in scala
Contravariant functors in scalaContravariant functors in scala
Contravariant functors in scala
 
JS Responsibilities
JS ResponsibilitiesJS Responsibilities
JS Responsibilities
 
Web futures
Web futuresWeb futures
Web futures
 
Rewriting Java In Scala
Rewriting Java In ScalaRewriting Java In Scala
Rewriting Java In Scala
 
Scope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languagesScope Graphs: A fresh look at name binding in programming languages
Scope Graphs: A fresh look at name binding in programming languages
 
Declarative Language Definition
Declarative Language DefinitionDeclarative Language Definition
Declarative Language Definition
 
Structured Interactive Scores with formal semantics
Structured Interactive Scores with formal semanticsStructured Interactive Scores with formal semantics
Structured Interactive Scores with formal semantics
 
Variational Autoencoded Regression of Visual Data with Generative Adversarial...
Variational Autoencoded Regression of Visual Data with Generative Adversarial...Variational Autoencoded Regression of Visual Data with Generative Adversarial...
Variational Autoencoded Regression of Visual Data with Generative Adversarial...
 
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...The Functional Programming Triad of Folding, Scanning and Iteration - a first...
The Functional Programming Triad of Folding, Scanning and Iteration - a first...
 
A born-again programmer's odyssey
A born-again programmer's odysseyA born-again programmer's odyssey
A born-again programmer's odyssey
 
그림 그리는 AI
그림 그리는 AI그림 그리는 AI
그림 그리는 AI
 
Using Parallel Computing Platform - NHDNUG
Using Parallel Computing Platform - NHDNUGUsing Parallel Computing Platform - NHDNUG
Using Parallel Computing Platform - NHDNUG
 
Functional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic ProgrammerFunctional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic Programmer
 
Machine learning with scikit-learn
Machine learning with scikit-learnMachine learning with scikit-learn
Machine learning with scikit-learn
 
Demystifying functional programming with Scala
Demystifying functional programming with ScalaDemystifying functional programming with Scala
Demystifying functional programming with Scala
 
VAE-type Deep Generative Models
VAE-type Deep Generative ModelsVAE-type Deep Generative Models
VAE-type Deep Generative Models
 
NIPS2017 Few-shot Learning and Graph Convolution
NIPS2017 Few-shot Learning and Graph ConvolutionNIPS2017 Few-shot Learning and Graph Convolution
NIPS2017 Few-shot Learning and Graph Convolution
 
Parallel Optimization in Machine Learning
Parallel Optimization in Machine LearningParallel Optimization in Machine Learning
Parallel Optimization in Machine Learning
 

Similar to Elm talk bayhac2015

How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...Eugene Kirpichov
 
Splash: User-friendly Programming Interface for Parallelizing Stochastic Lear...
Splash: User-friendly Programming Interface for Parallelizing Stochastic Lear...Splash: User-friendly Programming Interface for Parallelizing Stochastic Lear...
Splash: User-friendly Programming Interface for Parallelizing Stochastic Lear...Turi, Inc.
 
Enrichment lecture EE Technion (parts A&B) also including the subject of VHDL...
Enrichment lecture EE Technion (parts A&B) also including the subject of VHDL...Enrichment lecture EE Technion (parts A&B) also including the subject of VHDL...
Enrichment lecture EE Technion (parts A&B) also including the subject of VHDL...Amos Zaslavsky
 
Automated Program Repair, Distinguished lecture at MPI-SWS
Automated Program Repair, Distinguished lecture at MPI-SWSAutomated Program Repair, Distinguished lecture at MPI-SWS
Automated Program Repair, Distinguished lecture at MPI-SWSAbhik Roychoudhury
 
Simulating Large-scale Aggregate MASs with Alchemist and Scala
Simulating Large-scale Aggregate MASs with Alchemist and ScalaSimulating Large-scale Aggregate MASs with Alchemist and Scala
Simulating Large-scale Aggregate MASs with Alchemist and ScalaDanilo Pianini
 
Integrative Parallel Programming in HPC
Integrative Parallel Programming in HPCIntegrative Parallel Programming in HPC
Integrative Parallel Programming in HPCVictor Eijkhout
 
Cling the llvm based interpreter
Cling the llvm based interpreterCling the llvm based interpreter
Cling the llvm based interpreterRoberto Nogueira
 
The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)Ralf Laemmel
 
Subtle Asynchrony by Jeff Hammond
Subtle Asynchrony by Jeff HammondSubtle Asynchrony by Jeff Hammond
Subtle Asynchrony by Jeff HammondPatrick Diehl
 
Practical Aggregate Programming in Scala
Practical Aggregate Programming in ScalaPractical Aggregate Programming in Scala
Practical Aggregate Programming in ScalaRoberto Casadei
 
ooc - A hybrid language experiment
ooc - A hybrid language experimentooc - A hybrid language experiment
ooc - A hybrid language experimentAmos Wenger
 
ooc - A hybrid language experiment
ooc - A hybrid language experimentooc - A hybrid language experiment
ooc - A hybrid language experimentAmos Wenger
 
Minimal Introduction to C++ - Part I
Minimal Introduction to C++ - Part IMinimal Introduction to C++ - Part I
Minimal Introduction to C++ - Part IMichel Alves
 

Similar to Elm talk bayhac2015 (20)

How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...
 
Splash: User-friendly Programming Interface for Parallelizing Stochastic Lear...
Splash: User-friendly Programming Interface for Parallelizing Stochastic Lear...Splash: User-friendly Programming Interface for Parallelizing Stochastic Lear...
Splash: User-friendly Programming Interface for Parallelizing Stochastic Lear...
 
Enrichment lecture EE Technion (parts A&B) also including the subject of VHDL...
Enrichment lecture EE Technion (parts A&B) also including the subject of VHDL...Enrichment lecture EE Technion (parts A&B) also including the subject of VHDL...
Enrichment lecture EE Technion (parts A&B) also including the subject of VHDL...
 
Slides
SlidesSlides
Slides
 
Automated Program Repair, Distinguished lecture at MPI-SWS
Automated Program Repair, Distinguished lecture at MPI-SWSAutomated Program Repair, Distinguished lecture at MPI-SWS
Automated Program Repair, Distinguished lecture at MPI-SWS
 
An Efficient and Parallel Abstract Interpreter in Scala — Presentation
An Efficient and Parallel Abstract Interpreter in Scala — PresentationAn Efficient and Parallel Abstract Interpreter in Scala — Presentation
An Efficient and Parallel Abstract Interpreter in Scala — Presentation
 
Simulating Large-scale Aggregate MASs with Alchemist and Scala
Simulating Large-scale Aggregate MASs with Alchemist and ScalaSimulating Large-scale Aggregate MASs with Alchemist and Scala
Simulating Large-scale Aggregate MASs with Alchemist and Scala
 
Integrative Parallel Programming in HPC
Integrative Parallel Programming in HPCIntegrative Parallel Programming in HPC
Integrative Parallel Programming in HPC
 
Cling the llvm based interpreter
Cling the llvm based interpreterCling the llvm based interpreter
Cling the llvm based interpreter
 
The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)The Expression Problem (as part of the the PTT lecture)
The Expression Problem (as part of the the PTT lecture)
 
Symbolic Execution And KLEE
Symbolic Execution And KLEESymbolic Execution And KLEE
Symbolic Execution And KLEE
 
Subtle Asynchrony by Jeff Hammond
Subtle Asynchrony by Jeff HammondSubtle Asynchrony by Jeff Hammond
Subtle Asynchrony by Jeff Hammond
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
ATS Programming
ATS ProgrammingATS Programming
ATS Programming
 
lec10.pdf
lec10.pdflec10.pdf
lec10.pdf
 
DesmedtXSB
DesmedtXSBDesmedtXSB
DesmedtXSB
 
Practical Aggregate Programming in Scala
Practical Aggregate Programming in ScalaPractical Aggregate Programming in Scala
Practical Aggregate Programming in Scala
 
ooc - A hybrid language experiment
ooc - A hybrid language experimentooc - A hybrid language experiment
ooc - A hybrid language experiment
 
ooc - A hybrid language experiment
ooc - A hybrid language experimentooc - A hybrid language experiment
ooc - A hybrid language experiment
 
Minimal Introduction to C++ - Part I
Minimal Introduction to C++ - Part IMinimal Introduction to C++ - Part I
Minimal Introduction to C++ - Part I
 

Recently uploaded

Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxupamatechverse
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...ranjana rawat
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSSIVASHANKAR N
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordAsst.prof M.Gokilavani
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduitsrknatarajan
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...Call Girls in Nagpur High Profile
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)simmis5
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Dr.Costas Sachpazis
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlysanyuktamishra911
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSISrknatarajan
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdfankushspencer015
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Call Girls in Nagpur High Profile
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Christo Ananth
 

Recently uploaded (20)

Introduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptxIntroduction to Multiple Access Protocol.pptx
Introduction to Multiple Access Protocol.pptx
 
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
The Most Attractive Pune Call Girls Budhwar Peth 8250192130 Will You Miss Thi...
 
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLSMANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
MANUFACTURING PROCESS-II UNIT-5 NC MACHINE TOOLS
 
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete RecordCCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
CCS335 _ Neural Networks and Deep Learning Laboratory_Lab Complete Record
 
UNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular ConduitsUNIT-II FMM-Flow Through Circular Conduits
UNIT-II FMM-Flow Through Circular Conduits
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...Booking open Available Pune Call Girls Koregaon Park  6297143586 Call Hot Ind...
Booking open Available Pune Call Girls Koregaon Park 6297143586 Call Hot Ind...
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
Sheet Pile Wall Design and Construction: A Practical Guide for Civil Engineer...
 
KubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghlyKubeKraft presentation @CloudNativeHooghly
KubeKraft presentation @CloudNativeHooghly
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(PRIYA) Rajgurunagar Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
UNIT-III FMM. DIMENSIONAL ANALYSIS
UNIT-III FMM.        DIMENSIONAL ANALYSISUNIT-III FMM.        DIMENSIONAL ANALYSIS
UNIT-III FMM. DIMENSIONAL ANALYSIS
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
AKTU Computer Networks notes --- Unit 3.pdf
AKTU Computer Networks notes ---  Unit 3.pdfAKTU Computer Networks notes ---  Unit 3.pdf
AKTU Computer Networks notes --- Unit 3.pdf
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...Top Rated  Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
Top Rated Pune Call Girls Budhwar Peth ⟟ 6297143586 ⟟ Call Me For Genuine Se...
 
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
Call for Papers - Educational Administration: Theory and Practice, E-ISSN: 21...
 

Elm talk bayhac2015

  • 1. Functional Reactive Programming and Elm Sergei Winitzki BayHac 2015 June 14, 2015 Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 1 / 26
  • 2. Part 1. Functional reactive programming and Elm FRP has little to do with... multithreading, message-passing concurrency, “actors” distributed computing on massively parallel, load-balanced clusters map/reduce, “reactive extensions”, the “reactive manifesto” FRP means (in my definition)... pure functions using temporal types as primitives (temporal type ≈ lazy stream of values) FRP is probably most useful for: GUI and event-driven programming Elm is... a viable implementation of FRP geared for Web GUI apps Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 2 / 26
  • 3. Difficulties in reactive programming Imperative implementation is one problem among many... Input signals may come at unpredictable times Imperative updates are difficult to keep in the correct order Flow of events becomes difficult to understand Asynchronous (out-of-order) callback logic becomes opaque “callback hell”: deeply nested callbacks, all mutating data Inverted control (“the system will call you”) obscures the flow of data Some concurrency is usually required (e.g. background tasks) Explicit multithreaded code is hard to write and debug Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 3 / 26
  • 4. FRP basics Reactive programs work on infinite streams of input/output values Main idea: make streams implicit, as a new “temporal” type Σα — an infinite stream of values of type α alternatively, Σα is a value of type α that “changes with time” Reactive programs are viewed as pure functions a GUI is a pure function of type Σ Inputs → Σ View a Web server is a pure function Σ Request → Σ Response all mutation is implicit in the program instead of updating an x:Int, we define a value of type Σ Int our code is 100% immutable, no side effects, no IO monads asynchronous behavior is implicit: our code has no callbacks concurrency / parallelism is implicit the FRP runtime will provide the required scheduling of events Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 4 / 26
  • 5. Czaplicki’s (2012) core Elm in a nutshell Elm is a pure polymorphic λ-calculus with products and sums Temporal type Σα — a lazy sequence of values of type α Temporal combinators in core Elm: constant: α → Σα map2: (α → β → γ) → Σα → Σβ → Σγ scan: (α → β → β) → β → Σα → Σβ No nested temporal types: constant (constant x) is ill-typed! Domain-specific primitive types: Bool, Int, Float, String, View Standard library with data structures, HTML, HTTP, JSON, ... ...and signals Time.every, Mouse.position, Window.dimensions, ... ...and some utility functions: map, merge, drop, sampleOn, ... Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 5 / 26
  • 6. Details: Elm type judgments [Czaplicki 2012] Polymorphically typed λ-calculus (also with temporal types) Γ, (x : α) e : β Γ (λx.e) : α → β Lambda Γ e1 : α → β Γ e2 : α Γ (e1 e2) : β Apply Temporal types are denoted by Στ In these rules, type variables α, β, γ cannot involve Σ: Γ e : α Γ (constant e) : Σα Const Γ m : α → β → γ Γ p : Σα Γ q : Σβ Γ (map2 m p q) : Σγ Map2 Γ u : α → β → β Γ e : β Γ q : Σα Γ (scan u e q) : Σβ Scan Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 6 / 26
  • 7. Elm operational semantics 0: Current values An Elm program is a temporal expression of type ... → ΣView Temporal expressions are built from input signals and combinators It is not possible to “consume” a signal (Σα → β) A value of type ΣΣα is impossible in a well-typed expression Every temporal expression has a current value denoted by e[c] Γ e : Σα Γ c : α Γ e[c] : Σα CurVal Some current values are “freshly updated” while others are “stale” Denote a fresh value by asterisk: e[c]∗ In Elm, current values are implemented with an Either type Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 7 / 26
  • 8. Elm operational semantics 1: Initial values Every temporal expression has an initial (stale) current value Every predefined input signal i : Σα, i ∈ I has an initial value: i[a] Initial current values for all expressions are derived: Γ (constant c) : Σα Γ (constant c)[c] : Σα ConstInit Γ map2 m p[a] q[b] : Σγ Γ (map2 m p q)[m a b] : Σγ Map2Init Γ (scan u e q) : Σβ Γ (scan u e q) [e] : Σβ ScanInit Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 8 / 26
  • 9. Elm operational semantics 2: Updating signals Update steps happen only to input signals s ∈ I and one at a time Update steps Us←a {...} are applied to the whole program at once: Γ s : Σα s ∈ I Γ a : α Γ e[c] : Σβ Γ c : β Γ Us←a e[c] ⇒ e[c ] An update step on s will leave all other input signals unchanged: ∀s = s ∈ I : Us←b s[a] ⇒ s[b]∗ Us←b s [c] ⇒ s [c] Elm has an efficient implementation: The instances of input signals within expressions are not duplicated Stale current values are cached and not recomputed Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 9 / 26
  • 10. Elm operational semantics 3: Updating combinators Operational semantics does not reduce temporal expressions: The whole program remains a static temporal expression tree Only the current values are updated in subexpressions" Us←a (constant c) [c] ⇒ (constant c) [c] ConstUpd Us←a {map2 m p q} ⇒ map2 m Us←a {p} [v] Us←a {q} [w] [m v w] Map2Upd Us←a (scan u e q) [b] ⇒ scan u e Us←a {q} [c]∗ [u c b]∗ ScanPUpd All computations during an update step are synchronous The expression Us←b e[c] is reduced after all subexpressions of e Current values are non-temporal and are evaluated eagerly map2 updates whenever one of its subexpressions update scan updates only if its subexpression is freshly updated Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 10 / 26
  • 11. GUI building: “Hello, world” in Elm The value called main will be visualized by the runtime import Graphics.Element (..) import Text (..) import Signal (..) text : Element text = plainText "Hello, World!" main : Signal Element main = constant text Try Elm online at http://elm-lang.org/try Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 11 / 26
  • 12. Example of using scan Specification: I work only after the boss comes by and unless the phone rings Implementation: after_unless : (Bool, Bool) -> Bool -> Bool after_unless (b,p) w = (w || b) && not p boss_and_phone : Signal (Bool,Bool) i_work : Signal Bool i_work = scan after_unless False (boss_and_phone) Demo (boss_phone_work.elm) Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 12 / 26
  • 13. Typical GUI boilerplate in Elm A state machine with stepwise update: update : Command → State → State A rendering function (View is either Element or Html): draw : State → View A manager that merges the required input signals into one: may use Mouse, Keyboard, Time, HTML stuff, etc. merge_inputs : Signal Command Main boilerplate: init_state : State main : Signal View main = map draw (scan update init_state merge_inputs) Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 13 / 26
  • 14. Some limitations of Elm-style FRP No higher-order signals: Σ(Σα) is disallowed by the type system No distinction between continuous time and discrete time The signal processing logic is fully specified statically No constructors for user-defined signals No recursion possible in signal definition! Awkward mechanisms for “tying the knot” in Elm No full concurrency (e.g. “dining philosophers”) Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 14 / 26
  • 15. Elm-style FRP: the good parts Transparent, declarative modeling of data through ADTs Immutable and safe data structures (Array, Dict, ...) No runtime errors or exceptions! Space/time leaks are impossible! Language is Haskell-like but simpler for beginners Full type inference Easy deployment and interop in Web applications Good support for HTML/CSS, HTTP requests, JSON Good performance of caching HTML views Support for Canvas and HTML-free UI building Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 15 / 26
  • 16. Part 2. Temporal logic and FRP Reminder (Curry-Howard): logical expressions will be types ...and the axioms will be primitive terms We only need to control the order of events: no “hard real-time” How to understand temporal logic: classical propositional logic ≈ Boolean arithmetic intuitionistic propositional logic ≈ same but without true / false dichotomy (linear-time) temporal logic LTL≈ Boolean arithmetic for infinite sequences intuitionistic temporal logic ITL≈ same but without true / false dichotomy In other words: an ITL type represents a single infinite sequence of values Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 16 / 26
  • 17. Boolean arithmetic: notation Classical propositional (Boolean) logic: T, F, a ∨ b, a ∧ b, ¬a, a → b A notation better adapted to school-level arithmetic: 1, 0, a + b, ab, a The only “new rule” is 1 + 1 = 1 Define a → b = a + b Some identities: 0a = 0, 1a = a, a + 0 = a, a + 1 = 1, a + a = a, aa = a, a + a = 1, aa = 0, (a + b) = a b , (ab) = a + b , a = a a (b + c) = ab + ac, (a + b) (a + c) = a + bc Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 17 / 26
  • 18. Boolean arithmetic: example Of the three suspects A, B, C, only one is guilty of a crime. Suspect A says: “B did it”. Suspect B says: “C is innocent.” The guilty one is lying, the innocent ones tell the truth. φ = ab c + a bc + a b c a b + ab b c + bc Simplify: expand the brackets, omit aa , bb , cc , replace aa = a etc.: φ = ab c + 0 + 0 = ab c The guilty one is A. Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 18 / 26
  • 19. Propositional linear-time temporal logic (LTL) We work with infinite boolean sequences (“linear time”) Boolean operations: a = [a0, a1, a2, ...] ; b = [b0, b1, b2, ...] ; a + b = [a0 + b0, a1 + b1, ...] ; a = a0, a1, ... ; ab = [a0b0, a1b1, ...] Temporal operations: (Next) Na = [a1, a2, ...] (Sometimes) Fa = [a0 + a1 + a2 + ..., a1 + a2 + ..., ...] (Always) Ga = [a0a1a2a3..., a1a2a3..., a2a3..., ...] Other notation (from modal logic): Na ≡ a; Fa ≡ ♦a; Ga ≡ a Weak Until: pUq = “p holds from now on until q first becomes true” pUq = q + pN (q + pN (q + ...)) Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 19 / 26
  • 20. LTL: temporal specification Whenever the boss comes by my office, I will start working. Once I start working, I will keep working until the telephone rings. G ((b → Fw) (w → wUr)) = G b + Fw w + wUr Whenever the button is pressed, the dialog will appear. The dialog will disappear after 1 minute of user inactivity. G (b → Fd) (d → Ft) d → dUtd The timer t is an external event and is not specified here Difficult to say “x stays true until further notice” Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 20 / 26
  • 21. Temporal logic redux Designers of FRP languages must face some choices: LTL as type theory: do we use Nα, Fα, Gα as new types? Are they to be functors, monads, ...? Which temporal axioms to use as language primitives? What is the operational semantics? (I.e., how to compile this?) A sophisticated example: [Krishnaswamy 2013] uses full LTL with higher-order temporal types and fixpoints uses linear types to control space/time leaks Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 21 / 26
  • 22. Interpreting values typed by LTL What does it mean to have a value x of type, say, G(α → αUβ) ?? x : Nα means that x : α will be available only at the next time tick (x is a deferred value of type α) x : Fα means that x : α will be available at some future tick(s) (x is an event of type α) x : Gα means that a (different) value x : α is available at every tick (x is an infinite stream of type α) x : αUβ means a finite stream of α that may end with a β Some temporal axioms of intuitionistic LTL: (deferred apply) N (α → β) → (Nα → Nβ) ; (streamed apply) G (α → β) → (Gα → Gβ) ; (generate a stream) G (α → Nα) → (α → Gα) ; (read infinite stream) Gα → αN(Gα) (read finite stream) αUβ → β + αN(αUβ) Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 22 / 26
  • 23. Elm as an FRP language λ-calculus with type Gα, primitives constant, map2, scan map2 : (α → β → γ) → Gα → Gβ → Gγ scan : (α → β → β) → β → Gα → Gβ constant : α → Gα (map2 makes G an applicative functor) Limitations: Cannot have a type G(Gα), also not using N or F Cannot construct temporal values by hand This language is an incomplete Curry-Howard image of LTL! Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 23 / 26
  • 24. Conclusions There are some languages that implement FRP in various ad hoc ways The ideal is not (yet) reached Elm-style FRP is a promising step in the right direction Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 24 / 26
  • 25. Abstract In my day job, most bugs come from implementing reactive programs imperatively. FRP is a declarative approach that promises to solve these problems. FRP can be defined as a λ-calculus that admits temporal types, i.e. types given by a propositional intuitionistic linear-time temporal logic (LTL). Although the Elm language uses only a subset of LTL, it achieves high expressivity for GUI programming. I will formally define the operational semantics of Elm. I discuss the advantages and the current limitations of Elm. I also review the connections between temporal logic, FRP, and Elm. My talk will be understandable to anyone familiar with Curry-Howard and functional programming. The first part of the talk is a self-contained presentation of Elm that does not rely on temporal logic or Curry-Howard. The second part of the talk will explain the basic intuitions behind temporal logic and its connection with FRP. Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 25 / 26
  • 26. Suggested reading E. Czaplicki, S. Chong. Asynchronous FRP for GUIs. (2013) E. Czaplicki. Concurrent FRP for functional GUI (2012). N. R. Krishnaswamy. https://www.mpi-sws.org/∼neelk/simple-frp.pdfHigher-order functional reactive programming without spacetime leaks(2013). M. F. Dam. Lectures on temporal logic. Slides: Syntax and semantics of LTL, A Hilbert-style proof system for LTL E. Bainomugisha, et al. A survey of reactive programming (2013). W. Jeltsch. Temporal logic with Until, Functional Reactive Programming with processes, and concrete process categories. (2013). A. Jeffrey. LTL types FRP. (2012). D. Marchignoli. Natural deduction systems for temporal logic. (2002). – See Chapter 2 for a natural deduction system for modal and temporal logics. Sergei Winitzki (Versal Group Inc.) FRP and Elm June 14, 2015 26 / 26