The Theory and Practice of Causal Commutative Arrows › PaulLiu › download ›...
Transcript of The Theory and Practice of Causal Commutative Arrows › PaulLiu › download ›...
![Page 1: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/1.jpg)
The Theory and Practice ofCausal Commutative Arrows
Hai (Paul) Liu
Advisor: Paul Hudak
Computer Science Department
Yale University
October 2010
![Page 2: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/2.jpg)
1Contributions
1. Formalization of Causal Commutative Arrows (CCA):
◮ Definition of CCA and its laws.
◮ Definition of a CCA language that is strongly normalizing.
◮ Proof of the soundness and termination of CCA normalization.
2. Implementation of CCA normalization/optimization:
◮ Compile-time normalization through meta-programming.
◮ Run-time performance improvement by orders of magnitude.
3. Applications of CCA:
◮ Synchronous Dataflow
• relating CCA normal form to an operational semantics.
◮ Ordinary Differential Equations (ODE)
• designing embedded DSLs, solving space leaks.
◮ Functional Reactive Programming (FRP)
• solving space leaks, extending CCA for hybrid modeling.
![Page 3: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/3.jpg)
1Contributions
1. Formalization of Causal Commutative Arrows (CCA):
◮ Definition of CCA and its laws.
◮ Definition of a CCA language that is strongly normalizing.
◮ Proof of the soundness and termination of CCA normalization.
2. Implementation of CCA normalization/optimization:
◮ Compile-time normalization through meta-programming.
◮ Run-time performance improvement by orders of magnitude.
3. Applications of CCA:
◮ Synchronous Dataflow
• relating CCA normal form to an operational semantics.
◮ Ordinary Differential Equations (ODE)
• designing embedded DSLs, solving space leaks.
◮ Functional Reactive Programming (FRP)
• solving space leaks, extending CCA for hybrid modeling.
![Page 4: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/4.jpg)
2
Motivation
What is a good abstraction for Functional Reactive Program-
ming (FRP)?
![Page 5: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/5.jpg)
2-a
Motivation
What is a good abstraction for Functional Reactive Program-
ming (FRP)?
What is a good abstraction?
![Page 6: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/6.jpg)
2-b
Motivation
What is a good abstraction for Functional Reactive Program-
ming (FRP)?
What is a good abstraction?
◮ Abstract, high-level, more focus, less detail.
◮ General enough to express interesting programs.
◮ Specific enough to make use of domain knowledge.
![Page 7: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/7.jpg)
2-c
Motivation
What is a good abstraction for Functional Reactive Program-
ming (FRP)?
What is a good abstraction?
◮ Abstract, high-level, more focus, less detail.
◮ General enough to express interesting programs.
◮ Specific enough to make use of domain knowledge.
What is FRP?
![Page 8: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/8.jpg)
3
Part I: FRP
![Page 9: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/9.jpg)
4
Functional Reactive Programming
FRP is a paradigm for programming time based hybrid systems, with
applications in graphics, animation, robotics, GUI, vision, etc.
FRP belongs to a larger family of synchronous dataflow languages.
![Page 10: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/10.jpg)
4-a
Functional Reactive Programming
FRP is a paradigm for programming time based hybrid systems, with
applications in graphics, animation, robotics, GUI, vision, etc.
FRP belongs to a larger family of synchronous dataflow languages.
◮ Dataflow: data flow (along edges) between instructions (nodes).
◮ Synchronous: computation in each cycle is instantaneous.
◮ Hybrid: FRP models both continuous and discrete components.
![Page 11: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/11.jpg)
4-b
Functional Reactive Programming
FRP is a paradigm for programming time based hybrid systems, with
applications in graphics, animation, robotics, GUI, vision, etc.
FRP belongs to a larger family of synchronous dataflow languages.
◮ Dataflow: data flow (along edges) between instructions (nodes).
◮ Synchronous: computation in each cycle is instantaneous.
◮ Hybrid: FRP models both continuous and discrete components.
How do we program such systems?
![Page 12: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/12.jpg)
5
First-class Signals
Represent time changing quantities as an abstract data type:
Signal a ≈ Time → a
Example: a robot simulator. Its robots have a differential drive.
![Page 13: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/13.jpg)
6Example: Robot Simulator
The equations governing the x position of a differential drive robot:
x(t) =1
2
∫ t
0
(vr(t) + vl(t)) cos(θ(t))dt
θ(t) =1
l
∫ t
0
(vr(t)− vl(t))dt
The corresponding FRP program: (Note the lack of explicit time)
x = (1 / 2) ∗ integral ((vr + vl) ∗ cos θ)
θ = (1 / l) ∗ integral (vr − vl)
Domain specific operators:
(+) :: Signal a → Signal a → Signal a
(∗) :: Signal a → Signal a → Signal a
integral :: Signal a → Signal a
...
![Page 14: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/14.jpg)
7
First-class Signals: Good or Bad?
Good:
◮ Conceptually simple and concise.
◮ Easy to program with, no clutter.
◮ The basis for a large number of FRP implementations.
![Page 15: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/15.jpg)
7-a
First-class Signals: Good or Bad?
Good:
◮ Conceptually simple and concise.
◮ Easy to program with, no clutter.
◮ The basis for a large number of FRP implementations.
Bad:
◮ Higher-order signals Signal (Event (Signal a)) are ambiguous.
◮ Time and space leak: program slows down and consumes memory
at an unexpected rate.
![Page 16: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/16.jpg)
8
Improving the Abstraction with Signal Functions
Instead of first-class signals, use first-class signal functions:
SF a b ≈ Signal a → Signal b
Yampa is a FRP language that models signal functions using
arrows.
![Page 17: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/17.jpg)
9Signal Functions are Arrows
Arrows (Hughes 2000) are a generalization of monads. In Haskell:
class Arrow a where
arr :: (b → c)→ a b c
(≫) :: a b c → a c d → a b d
first :: a b c → a (b, d) (c, d)
Support both sequential and parallel composition.
second :: (Arrow a)⇒ a b c → a (d , b) (d , c)
second f = arr swap ≫ first f ≫ arr swap
where swap (a, b) = (b, a)
(⋆⋆⋆) :: (Arrow a)⇒ a b c → a b′ c′ → a (b, b′) (c, c′)
f ⋆⋆⋆ g = first f ≫ second g
(&&&) :: (Arrow a)⇒ a b c → a b c′ → a b (c, c′)
f &&&g = arr (λx → (x , x ))≫ (f ⋆⋆⋆ g)
![Page 18: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/18.jpg)
10
Picturing an Arrow
(a) arr f (b) f ≫ g (c) first f
(d) f ⋆⋆⋆ g (e) loop f
To model recursion, Paterson (2001) introduces ArrowLoop:
class Arrow a ⇒ ArrowLoop a where
loop :: a (b, d) (c, d)→ a b c
![Page 19: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/19.jpg)
11
Robot Simulator Revisit
xSF = (((vrSF&&&vlSF )≫ arr (uncurry (+)))&&&(thetaSF ≫ arr cos))
≫ arr (uncurry (∗))≫ integral ≫ arr (/2)
![Page 20: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/20.jpg)
11-a
Robot Simulator Revisit
&&&
xSF = (((vrSF&&&vlSF )≫ arr (uncurry (+)))&&&(thetaSF ≫ arr cos))
≫ arr (uncurry (∗))≫ integral ≫ arr (/2)
![Page 21: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/21.jpg)
11-b
Robot Simulator Revisit
&&&
≫
xSF = (((vrSF&&&vlSF )≫ arr (uncurry (+)))&&&(thetaSF ≫ arr cos))
≫ arr (uncurry (∗))≫ integral ≫ arr (/2)
![Page 22: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/22.jpg)
11-c
Robot Simulator Revisit
&&&
≫
≫
xSF = (((vrSF&&&vlSF )≫ arr (uncurry (+)))&&&(thetaSF ≫ arr cos))
≫ arr (uncurry (∗))≫ integral ≫ arr (/2)
![Page 23: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/23.jpg)
11-d
Robot Simulator Revisit
&&&
≫
≫
&&&
xSF = (((vrSF&&&vlSF )≫ arr (uncurry (+)))&&&(thetaSF ≫ arr cos))
≫ arr (uncurry (∗))≫ integral ≫ arr (/2)
![Page 24: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/24.jpg)
11-e
Robot Simulator Revisit
&&&
≫
≫
&&& ≫
xSF = (((vrSF&&&vlSF )≫ arr (uncurry (+)))&&&(thetaSF ≫ arr cos))
≫ arr (uncurry (∗))≫ integral ≫ arr (/2)
![Page 25: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/25.jpg)
11-f
Robot Simulator Revisit
&&&
≫
≫
&&& ≫ ≫
xSF = (((vrSF&&&vlSF )≫ arr (uncurry (+)))&&&(thetaSF ≫ arr cos))
≫ arr (uncurry (∗))≫ integral ≫ arr (/2)
![Page 26: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/26.jpg)
11-g
Robot Simulator Revisit
&&&
≫
≫
&&& ≫ ≫ ≫
xSF = (((vrSF&&&vlSF )≫ arr (uncurry (+)))&&&(thetaSF ≫ arr cos))
≫ arr (uncurry (∗))≫ integral ≫ arr (/2)
![Page 27: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/27.jpg)
12Robot Simulator in Arrow Syntax
xSF = proc inp → do
vr ← vrSF −≺ inp
vl ← vlSF −≺ inp
θ ← thetaSF−≺ inp
i ← integral −≺ (vr + vl) ∗ cos θ
returnA−≺ (i / 2)
![Page 28: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/28.jpg)
13
Modeling Discrete Events
Events are instantaneous and have no duration.
data Event a = Event a | NoEvent
Example: coerce from an discrete-time event stream to
continuous-time signal by “holding” a previous event value.
hold :: a → SF (Event a) a
![Page 29: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/29.jpg)
14
Infinitesimal Delay with iPre
As a more primitive operator than hold , iPre puts an infinites-
imal delay over the input signal, and initializes it with a new
value.
iPre :: a → SF a a
We can implement hold using iPre:
hold i = proc e → do
rec y ← iPre i−≺ z
let z = case e of Event x → x
NoEvent → y
returnA−≺ z
![Page 30: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/30.jpg)
15
What’s Good About Using Arrows in FRP
◮ Highly abstract, and yet allow domain specific extensions.
◮ Like monads, they are composable and can be stateful.
◮ Modular: both input and output are explicit.
◮ Higher-order signal function SF a (b,Event (SF a b)) as
event switch.
◮ Formal properties expressed as laws.
![Page 31: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/31.jpg)
16
Arrow Laws
left identity arr id ≫ f = f
right identity f ≫ arr id = f
associativity (f ≫ g)≫ h = f ≫ (g ≫ h)
composition arr (g . f ) = arr f ≫ arr g
extension first (arr f ) = arr (f × id)
functor first (f ≫ g) = first f ≫ first g
exchange first f ≫ arr (id × g) = arr (id × g)≫ first f
unit first f ≫ arr fst = arr fst ≫ f
association first (first f )≫ arr assoc = arr assoc ≫ first f
where assoc ((a, b), c) = (a, (b, c))
![Page 32: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/32.jpg)
17
Arrow Loop Laws
left tightening loop (first h ≫ f ) = h ≫ loop f
right tightening loop (f ≫ first h) = loop f ≫ h
sliding loop (f ≫ arr (id ∗ k)) = loop (arr (id × k)≫ f )
vanishing loop (loop f ) = loop (arr assoc−1≫ f ≫ arr assoc)
superposing second (loop f ) = loop (arr assoc≫ second f ≫ arr assoc−1)
extension loop (arr f ) = arr (trace f )
where trace f b = let (c, d) = f (b, d) in c
![Page 33: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/33.jpg)
18
FRP as a Domain Specific Language
What makes a good abstraction for FRP?
![Page 34: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/34.jpg)
18-a
FRP as a Domain Specific Language
What makes a good abstraction for FRP?
Signals?
![Page 35: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/35.jpg)
18-b
FRP as a Domain Specific Language
What makes a good abstraction for FRP?
Signals? flexible, but ... not enough discipline.
![Page 36: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/36.jpg)
18-c
FRP as a Domain Specific Language
What makes a good abstraction for FRP?
Signals? flexible, but ... not enough discipline.
Arrows?
![Page 37: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/37.jpg)
18-d
FRP as a Domain Specific Language
What makes a good abstraction for FRP?
Signals? flexible, but ... not enough discipline.
Arrows? Disciplined, but ... not specific enough.
![Page 38: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/38.jpg)
18-e
FRP as a Domain Specific Language
What makes a good abstraction for FRP?
Signals? flexible, but ... not enough discipline.
Arrows? Disciplined, but ... not specific enough.
What is domain specific about FRP?
![Page 39: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/39.jpg)
18-f
FRP as a Domain Specific Language
What makes a good abstraction for FRP?
Signals? flexible, but ... not enough discipline.
Arrows? Disciplined, but ... not specific enough.
What is domain specific about FRP? Causality.
(Causal: current output only depends on current and previous inputs.)
![Page 40: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/40.jpg)
18-g
FRP as a Domain Specific Language
What makes a good abstraction for FRP?
Signals? flexible, but ... not enough discipline.
Arrows? Disciplined, but ... not specific enough.
What is domain specific about FRP? Causality.
(Causal: current output only depends on current and previous inputs.)
Can we refine the arrow abstraction to capture causality?
![Page 41: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/41.jpg)
19
Part II. CCA
![Page 42: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/42.jpg)
20
Causal Commutative Arrows (CCA)
Introduce a new operator init :
class ArrowLoop a ⇒ ArrowInit a where
init :: b → a b b
and two additional laws:
commutativity first f ≫ second g = second g ≫ first f
product init i ⋆⋆⋆ init j = init (i , j )
and still remain abstract!
![Page 43: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/43.jpg)
21
What’s Good about CCA
CCA provides a core set of operators for dataflow computa-
tions.
◮ The init operator does not talk about time, and the
product law puts little restriction over its actual semantics.
◮ The commutativity law states an important non-interference
property so that side effects can only be local.
![Page 44: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/44.jpg)
21-a
What’s Good about CCA
CCA provides a core set of operators for dataflow computa-
tions.
◮ The init operator does not talk about time, and the
product law puts little restriction over its actual semantics.
◮ The commutativity law states an important non-interference
property so that side effects can only be local.
Quiz: why not make this a law?
init i ≫ arr f = arr f ≫ init (f i)
![Page 45: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/45.jpg)
22
The CCA Language: Syntax
Variables V ::= x | y | z | ...
Types A,B ,C ::= 1 | M × N | A→ B | A B
Expressions M ,N ::= () | V | (M ,N ) | fst M | snd M |
λV .M | M N | trace M
Programs P ,Q ::= arr M | P ≫ Q | first P | loop P | init M
Environment Γ ::= x0 : A0, ..., xn : An
◮ Typed lambda calculus extended with unit, product, arrow and trace.
◮ Instead of type classes, use A B to denote arrow type.
◮ Programs and expressions are separated on purpose, so that pro-
grams are only finite compositions of arrow combinators.
![Page 46: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/46.jpg)
23The CCA Language: Types
(UNIT) Γ ⊢ () : 1 (VAR)x : A ∈ Γ
Γ ⊢ x : A(TRACE)
Γ ⊢ M : A × C → B × C
Γ ⊢ trace M : A → B
(ABS)Γ, x : A ⊢ M : B
Γ ⊢ λx.M : A → B(APP)
Γ ⊢ M : A → B Γ ⊢ N : A
Γ ⊢ M N : B
(PAIR)Γ ⊢ M : A Γ ⊢ N : B
Γ ⊢ (M, N) : A × B(FST)
Γ ⊢ M : A × B
Γ ⊢ fst M : A(SND)
Γ ⊢ M : A × B
Γ ⊢ snd M : B
(ARR)⊢ M : A → B
⊢ arr M : A B(SEQ)
⊢ P : A B ⊢ Q : B C
⊢ P ≫Q : A C
(FIRST)⊢ P : A B
⊢ first P : A × C B × C(LOOP)
⊢ P : A × C B × C
⊢ loop P : A B
(INIT)⊢ M : A
⊢ init M : A A
![Page 47: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/47.jpg)
24
Causal Commutative Normal Form (CCNF)
(f) Original (g) Normalized
Theorem (CCNF) For all well typed CCA program p : A B, there exists
a normal form pnorm , called the Causal Commutative Normal Form,
which is either of the form arr f , or loopD i f for some i and f , such that
pnorm : A B, and p ⇓ pnorm . In unsugared form, the second form is
equivalent to
loopD i f = loop (arr f ≫ second (init i))
![Page 48: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/48.jpg)
25
Normalization Explained
◮ Based on arrow laws, but directed.
◮ The two new laws, commutativity and product, are essential.
◮ Best illustrated by pictures...
![Page 49: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/49.jpg)
25-a
Re-order Parallel Pure and Stateful Arrows
Related law: exchange (a special case of commutativity).
![Page 50: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/50.jpg)
25-b
Re-order Sequential Pure and Stateful Arrows
Related laws: tightening, sliding, and definition of second.
![Page 51: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/51.jpg)
25-c
Change Sequential to Parallel
Related laws: product, tightening, sliding, and definition of second.
![Page 52: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/52.jpg)
25-d
Move Sequential into Loop
Related law: tightening.
![Page 53: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/53.jpg)
25-e
Move Parallel into Loop
Related laws: superposing, and definition of second.
![Page 54: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/54.jpg)
25-f
Fuse Nested Loops
Related laws: commutativity, product, tightening, and vanishing.
![Page 55: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/55.jpg)
26
Part III. Applications
![Page 56: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/56.jpg)
27Synchronous Dataflow
Programs written in a stream based dataflow language (Lucid):
ones = 1 ‘fby ‘ ones fibs = let f = 0 ‘fby‘ g
sum x = x + 0 ‘fby‘ sum x g = 1 ‘fby ‘ (f + g)
nats = sum ones in f
Compare to programs written in arrows:
ones = arr (λ → 1) fibs = proc → do
sum = proc x → do rec f ← init 0−≺ g
rec s ← init 0−≺ s ′ g ← init 1−≺ (f + g)
let s ′ = s + x returnA−≺ f
returnA−≺ s ′
nats = ones ≫ sum
Stream functions over discrete streams are arrows. We instantiate CCA
by assigning init the meaning of a unit delay, just like ‘fby ‘.
![Page 57: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/57.jpg)
28
Synchronous Dataflow: Normalization Example
Same fibs program written in arrow combinators:
fibs = loop (arr snd ≫ loop (arr (uncurry (+))≫ init 1≫ arr dup)≫
init 0≫ arr dup)
where dup x = (x , x )
Its normal form:
ccnf fibs = loopD (0, 1) (λ( , (x , y))→ (x , (y , x + y)))
(a) Original (b) Normalized
![Page 58: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/58.jpg)
29
CCNF Tuple and Operational Semantics
We call the pair (i , f ) a CCNF tuple for a CCNF in the form
loopD i f .
runccnf :: (d , (b, d)→ (c, d))→ [b ]→ [c ]
runccnf (i , f ) = g i
where g i (x : xs) = let (y , i ′) = f (x , i) in y : g i ′ xs
runccnf implements an operational semantics for causal
stream functions that is also known as a Mealy machine, a
form of automata.
By using CCNF tuples directly, we avoid all arrow structures!
![Page 59: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/59.jpg)
30
Dataflow Benchmarks (Speed Ratio)
Name GHC 1 arrowp2 CCNF 3 CCNF Tuple4
sine 1.0 2.40 17.05 470.56
fibonacci 1.0 1.87 16.48 123.15
factorial 1.0 3.09 15.84 22.62
bounded counter 1.0 3.22 44.48 98.91
◮ Same arrow source programs written in arrow syntax.
◮ Same arrow implementation in Haskell.
◮ Only difference is syntactic:
1. Translated to combinators by GHC’s built-in arrow compiler.
2. Translated to combinators by Paterson’s arrowp preprocessor.
3. Arrow combinator after CCA normalization.
4. CCNF tuple after CCA normalization.
![Page 60: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/60.jpg)
31
Representing Autonomous ODE
An ordinary differential equation (ODE) of order n is of the
form:
f (n) = F (t, f, f ′, . . . , f (n−1))
for an unknown function f(t), with its nth derivative described
by f (n), where f ∈ R→ R and t ∈ R.
An initial value problem of a first order autonomous ODE is of
the form:
f ′ = F (f) s.t. f(t0) = f0
The given pair (t0, f0) ∈ R× R is called the initial condition.
![Page 61: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/61.jpg)
32DSL for ODE Using Tower of Derivatives
Function Mathematics Haskell
Sine wave y′′ = −y y = integral y0 y ′
y ′ = integral y1 (−y)
Damped oscillator y′′ = −cy′ − y y = integral y0 y ′
y ′ = integral y1 (−c ∗ y ′ − y)
Lorenz attractor x′ = σ(y − x) x = integral x0 (σ ∗ (y − x))
y′ = x(ρ− z)− y y = integral y0 (x ∗ (ρ− z )− y)
z′ = xy − βz z = integral z0 (x ∗ y − β ∗ z )
ODE represented as a tower-of-derivatives (Karczmarczuk 1998):
data D a = D {val :: a, der :: D a }
(+) :: D a → D a → D a
(∗) :: D a → D a → D a
integral :: a → D a → D a
integral v d = D v d
![Page 62: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/62.jpg)
33
DSL for ODE Using Arrows
Sine wave y′′ = −y proc ()→ do
rec y ← integral y0−≺ y ′
y ′ ← integral y1−≺ −y
returnA−≺ y
Damped oscillator y′′ = −cy′ − y proc ()→ do
rec y ← integral y0−≺ y ′
y ′ ← integral y1−≺ −c ∗ y ′ − y
returnA−≺ y
Lorenz attractor x′ = σ(y − x) proc ()→ do
y′ = x(ρ− z)− y rec x ← integral x0−≺ σ ∗ (y − x)
z′ = xy − βz y ← integral y0−≺ x ∗ (ρ− z )− y
z ← integral z0−≺ x ∗ y − β ∗ z
returnA−≺ (x , y , z )
![Page 63: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/63.jpg)
34
ODE Arrows are CCA
The integral function is indeed just the init operator in CCA.
(c) Original (d) Normalized
After normalization to an CCNF tuple (i , f ) :: (s, (a, s)→ (b, s))
◮ The state i is a nested tuple that represents a vector of initial values.
◮ The pure function f computes the value of derivatives.
ODEs can be numerically solved by using just CCNF tuples!
![Page 64: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/64.jpg)
35
Extending CCA for Yampa Arrows
Yampa models both discrete-time and continuous-time signals with two
essential arrow combinators:
iPre :: a → SF a a
integral :: a → SF a a
Both fit the type of init combinator of CCA.
![Page 65: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/65.jpg)
35-a
Extending CCA for Yampa Arrows
Yampa models both discrete-time and continuous-time signals with two
essential arrow combinators:
iPre :: a → SF a a
integral :: a → SF a a
Both fit the type of init combinator of CCA. Solution: extend CCA with
multi-sort inits!
The CCNF for a Yampa arrow is either arr f , or
loopD2 (i , j ) f = loop (arr f ≫ second (iPre i ⋆⋆⋆ integral j ))
![Page 66: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/66.jpg)
36Animate Yampa Arrow with CCNF 2
Represent the CCNF for Yampa arrow as a generalized algebraic data
type (GADT):
data CCNF 2 a b where
CCNF 2 :: (VectorSpace DTime d ,Num d)⇒
((c, d), (a, (c, d))→ (b, (c, d)))→ CCNF 2 a b
Interact with the world with just CCNF 2, no more arrows!
reactimate :: IO (DTime, a)→ (b → IO ())→ CCNF 2 a b → IO ()
reactimate sense actuate (CCNF 2 ((i , j ), f )) = run i j
where run i j = do
(dt , x )← sense
let (y , (inew, j ′)) = f (x , (i , j ))
jnew = euler dt j j ′
actuate y
run inew jnew
![Page 67: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/67.jpg)
37
Not All Yampa Arrows Are CCA
Yampa models dynamic systems with event switches:
switch :: SF a (b,Event c)→ (c → SF a b)→ SF a b
Or alternatively:
switch :: SF a (b,Event (SF a b))→ SF a b
But the normal form of CCA is static: both the state i and the function f
in a CCNF tuple are of a fixed structure.
Workaround: do not use CCNF tuple directly, but use switches on top of
normalized arrows.
![Page 68: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/68.jpg)
38
Related Work
◮ Single while loop (Harel 1980).
◮ Compilation of synchronous dataflow (Halbwachs et al. 1991,
Amagbagnon et al. 1995).
◮ Functional representation of streams (Caspi and Pouzet 1998).
◮ Functional stream derivatives (Rutten 2006).
◮ Stream Fusion (Coutts et al. 2007).
◮ FRP and arrow optimizations (Burchett et al. 2007, Nilsson 2005).
![Page 69: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/69.jpg)
39
Why We Love Arrows
CCA is a fine example demonstrating the power of abstraction through
arrows:
◮ High-level abstraction != sluggish performance.
◮ CCA extends generic arrows with domain knowledge. (ICFP2009)
◮ Use arrow for embedded DSLs and preserve sharing. (PADL2010)
◮ Arrows eliminate a certain form of space leaks in FRP. (ENTCS2007)
![Page 70: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/70.jpg)
40
Future Work
◮ Improve CCA implementation with a new meta-programming tool.
◮ Optimize CCNF code with a custom code inliner/generator.
◮ Extend CCA to handle concurrent I/O.
![Page 71: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/71.jpg)
41
Thank you!
![Page 72: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/72.jpg)
42
ODE Benchmarks (Speed Ratio)
Name Tagged Arrow CCA
Exponential 1 0.17 83.72
Sine wave 1 0.35 27.52
Damped oscillator 1 1.13 82.34
Lorenz attractor 1 3.55 159.54
◮ Tagged version gets slower as program gets more complex.
◮ Arrow version still has some overhead.
◮ CCA version generates very efficient code in a tight loop.
![Page 73: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/73.jpg)
43
Sound Sythesis Example
++ !x – x3
* feedbk1
lowpassEmbouchure delay
delayt (1/fqc/2)
emb
Flute bore delaydelayt (1/fqc)
bore
sinA5
* 0.1
!
x
rand1
flow
lineSeg
env1
lineSeg
envibr
+
* amp
* feedbk2
vibr * breath
sum1 out
lineSeg
env2
returnA
flute
Block diagram of Parry Cook’s Flute generator
![Page 74: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/74.jpg)
44
flute0 dur amp fqc press breath =
let en1 = arr $ lineSeg [0, 1.1 ∗ press, press, press, 0] [0.06, 0.2, dur − 0.16, 0.02]
en2 = arr $ lineSeg [0, 1, 1, 0] [0.01, dur − 0.02, 0.01]
enibr = arr $ lineSeg [0, 0, 1, 1] [0.5, 0.5, dur − 1]
emb = delayt (mkBuf 2 n) n
bore = delayt (mkBuf 1 (n ∗ 2)) (n ∗ 2)
n = truncate (1 / fqc / 2 ∗ fromIntegral sr)
in proc → do
rec tm ← timeA −≺ ()
env1 ← en1 −≺ tm
env2 ← en2 −≺ tm
envibr ← enibr −≺ tm
sin5 ← sineA 5 −≺ ()
rand ← arr rand f −≺ ()
let vibr = sin5 ∗ envibr ∗ 0.1
flow = rand ∗ env1
sum1 = breath ∗ flow + env1 + vibr
flute ← bore −≺ out
x ← emb −≺ sum1 + flute ∗ 0.4
out ← lowpassA 0.27−≺ x − x ∗ x ∗ x + flute ∗ 0.4
returnA−≺ out ∗ amp ∗ env2
![Page 75: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/75.jpg)
45loop (arr (λ( , out)→ ((), out))≫
(first timeA≫ arr (λ(tm, out)→ (tm, (out, tm))))≫
(first en1 ≫ arr (λ(env1 , (out, tm))→ (tm, (env1 , out, tm))))≫
(first en2 ≫
arr (λ(env2 , (env1 , out, tm))→ (tm, (env1 , env2 , out))))≫
(first enibr ≫
arr (λ(envibr, (env1 , env2 , out))→ ((), (env1 , env2 , envibr, out))))≫
(first (sineA 5)≫
arr (λ(sin5 , (env1 , env2 , envibr, out))→
((), (env1 , env2 , envibr, out, sin5))))≫
(first (arr rand f )≫
arr (λ(rand, (env1 , env2 , envibr, out, sin5))→
let vibr = sin5 ∗ envibr ∗ 0.1
flow = rand ∗ env1
sum1 = breath ∗ flow + env1 + vibr
in (out, (env2 , sum1 ))))≫
(first bore ≫
arr (λ(flute, (env2 , sum1))→ ((flute, sum1 ), (env2 , flute))))≫
(first (arr (λ(flute, sum1 )→ sum1 + flute ∗ 0.4)≫ emb)≫
arr (λ(x , (env2 , flute))→ ((flute, x), env2)))≫
(first (arr (λ(flute, x)→ x − x ∗ x ∗ x + flute ∗ 0.4)≫
lowpassA 0.27)
≫ arr (λ(out, env2)→ ((env2 , out), out))))
≫ arr (λ(env2 , out)→ out ∗ amp ∗ env2)
![Page 76: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/76.jpg)
46fluteOpt dur amp fqc press breath =
let env1 = upSample f (lineSeg am1 du1) 20
env2 = upSample f (lineSeg am2 du2) 20
env3 = upSample f (lineSeg am3 du3) 20
omh = 2 ∗ pi / (fromIntegral sr) ∗ 5
c = 2 ∗ cos omh
i = sin omh
dt = 1 / fromIntegral sr
sr = 44100
buf100 = mkArr 100
buf50 = mkArr 50
am1 = [0, 1.1 ∗ press, press, press, 0]
du1 = [0.06, 0.2, dur − 0.16, 0.02]
am2 = [0, 1, 1, 0]
du2 = [0.01, dur − 0.02, 0.01]
am3 = [0, 0, 1, 1]
du3 = [0.5, 0.5, dur − 1]
in loopD ((0, ((0, 0), 0)), (((((buf100), 0), 0), ((0), (((buf50), 0), 0))), (((0, i), (0, ((0, 0), 0))), ((0, ((0, 0), 0)), (0, ((0, 0), 0))))))
(λ((((( a, f ), e), d), c), (( b, ( h, i)), ((( g, l), ( k, ( m, n))), ((( j , q), ( p, ( r, s))), (( o, ( u, v)), ( t, ( w, x))))))) →
let randf = rand f f
(env1vu1, env1vu2) = env1 ( v, u)
(env1xw1, env1xw2) = env1 ( x, w)
(env3sr1, env3sr2) = env3 ( s, r)
(env2ih1, env2ih2) = env2 ( i, h)
d50nm = ((delay f 50) ( n, m))
d100lg = ((delay f 100) ( l, g))
foo = k + 0.27 ∗ (((−) ((+((polyx) (fstU d50nm))) baz)) k)
bar = (((+) (negate j)) ((c∗) q))
baz = (((+((+((∗breath) ((∗env1xw1) randf ))) env1vu1)) ((∗((∗0.1) env3sr1)) bar))) + (fstU d100lg ∗ 0.4)
in (((∗((∗amp) foo)) env2ih1), ((( b + dt), (env2ih2, b)), ((((sndU d100lg), foo), (foo, ((sndU d50nm), baz))),
((( q, bar), (( p + dt), (env3sr2, p))), ((( o + dt), (env1vu2, o)), (( t + dt), (env1xw2, t))))))))
![Page 77: The Theory and Practice of Causal Commutative Arrows › PaulLiu › download › thesis-slides.pdfFunctional Reactive Programming FRP is a paradigm for programming time based hybrid](https://reader033.fdocuments.us/reader033/viewer/2022060406/5f0f67707e708231d443fde4/html5/thumbnails/77.jpg)
47