The Remote Monad. Dissertation Defense. Justin Dawson.

Similar documents
The Remote Monad. Justin Dawson

Monads. Bonus lecture 2017 David Sands

Domain Specific Languages for Small Embedded Systems

Background Type Classes (1B) Young Won Lim 6/28/18

Applicatives Comparisons (2C) Young Won Lim 3/6/18

Monad Background (3A) Young Won Lim 11/18/17

JVM ByteCode Interpreter

Advanced Programming Handout 7. Monads and Friends (SOE Chapter 18)

CSCE 314 Programming Languages Functors, Applicatives, and Monads

Monad Background (3A) Young Won Lim 11/8/17

Programming Languages

Side Effects (3B) Young Won Lim 11/23/17

Side Effects (3B) Young Won Lim 11/20/17

Background Operators (1E) Young Won Lim 7/7/18

Side Effects (3B) Young Won Lim 11/27/17

Monads seen so far: IO vs Gen

Background Type Classes (1B) Young Won Lim 6/14/18

Monads. Functional Programming (CS4011) Monads

Functional Programming in C++

Side Effects (3A) Young Won Lim 1/13/18

Introduction to Haskell

More on functional programming

All About Comonads (Part 1) An incomprehensible guide to the theory and practice of comonadic programming in Haskell

Haskell in the corporate environment. Jeff Polakow October 17, 2008

Applicative, traversable, foldable

Advanced Functional Programming

Functional Programming

Software System Design and Implementation

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Haskell Programming

Haskell Monads CSC 131. Kim Bruce

Parallel & Concurrent Haskell 1: Basic Pure Parallelism. Simon Marlow (Microsoft Research, Cambridge, UK)

Monad (3A) Young Won Lim 8/9/17

An introduction to functional programming. July 23, 2010

Operating Systems. 18. Remote Procedure Calls. Paul Krzyzanowski. Rutgers University. Spring /20/ Paul Krzyzanowski

Maybe Monad (3B) Young Won Lim 12/21/17

College Functors, Applicatives

Monad Overview (3B) Young Won Lim 1/16/18

References. Monadic I/O in Haskell. Digression, continued. Digression: Creating stand-alone Haskell Programs

Lecture 8: Summary of Haskell course + Type Level Programming

CS 6456 OBJCET ORIENTED PROGRAMMING IV SEMESTER/EEE

CSCE 314 Programming Languages. Interactive Programming: I/O

PROGRAMMING IN HASKELL. Chapter 10 - Interactive Programming

Applicative, traversable, foldable

Distributed Systems 8. Remote Procedure Calls

Advances in Programming Languages

describe the functions of Windows Communication Foundation describe the features of the Windows Workflow Foundation solution

UDP and TCP. Introduction. So far we have studied some data link layer protocols such as PPP which are responsible for getting data

Introduction to Functional Programming and Haskell. Aden Seaman

Programming with Math and Logic

Box-and-arrow Diagrams

Monad Background (3A) Young Won Lim 11/20/17

Intro to Haskell Notes: Part 5

Monad (1A) Young Won Lim 6/26/17

A Brief Haskell and GHC Refresher

CS 457/557: Functional Languages

1 Delimited continuations in Haskell

CS 417 9/18/17. Paul Krzyzanowski 1. Socket-based communication. Distributed Systems 03. Remote Procedure Calls. Sample SMTP Interaction

Haskell & functional programming, some slightly more advanced stuff. Matteo Pradella

Distributed Systems. 03. Remote Procedure Calls. Paul Krzyzanowski. Rutgers University. Fall 2017

f() ! " Run a higher-priority task? ! " Yield the processor to another task because our ! " Use the processor for some other activity while

CS 410/510: Advanced Programming

The story so far. Elements of Programming Languages. Pairs in various languages. Pairs

Maybe Monad (3B) Young Won Lim 1/3/18

Monads. COS 441 Slides 16

IO Monad (3D) Young Won Lim 1/18/18

I/O in Haskell. To output a character: putchar :: Char -> IO () e.g., putchar c. To output a string: putstr :: String -> IO () e.g.

The Haskell HOP: Higher-order Programming

Structural polymorphism in Generic Haskell

Polymorphism Overview (1A) Young Won Lim 2/20/18

Full Stack boot camp

Haskell in the Software Industry

Monads and all that I. Monads. John Hughes Chalmers University/Quviq AB

Denotational Semantics. Domain Theory

Architectural Styles. Software Architecture Lecture 5. Copyright Richard N. Taylor, Nenad Medvidovic, and Eric M. Dashofy. All rights reserved.

Middleware. Adapted from Alonso, Casati, Kuno, Machiraju Web Services Springer 2004

Chapter 3 Processes. Process Concept. Process Concept. Process Concept (Cont.) Process Concept (Cont.) Process Concept (Cont.)

Snap und Heist - HaL7 Halle (Saale), Germany July Matthias Fischmann & Sönke Hahn

LOON. Language Reference Manual THE LANGUAGE OF OBJECT NOTATION. Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee

Monads in Haskell. Nathanael Schilling. December 12, 2014

Functional Programming and the Web

CIS552: Advanced Programming

Topic 9: Type Checking

Topic 9: Type Checking

Adam Chlipala University of California, Berkeley ICFP 2006

Embedded Domain Specific Language Implementation using Dependent Types

Functional Programming Patterns And Their Role Instructions

Overview. Declarative Languages. General monads. The old IO monad. Sequencing operator example. Sequencing operator

Idris: Implementing a Dependently Typed Programming Language

Operating System Design

INTRODUCTION TO HASKELL

Chapter 3: Processes. Operating System Concepts 9 th Edition

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

GRITS AJAX & GWT. Trey Roby. GRITS 5/14/09 Roby - 1

Lecture 4: Higher Order Functions

Chapter 4 Communication

M-Structures (Continued)

New Programming Paradigms

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

The Typed Racket Guide

Type checking by theorem proving in IDRIS

Transcription:

The Remote Monad Dissertation Defense Justin Dawson jdawson@ittc.ku.edu Information and Telecommunication Technology Center University of Kansas, USA 1 / 66

Sandwiches! How do you make a sandwich? 2 / 66

Sandwiches! How do you make a sandwich? get out the bread, ham, lettuce, cheese and condiments cut lettuce and cheese spread condiments on bread add remaining ingredients put bread and other ingredients away Time taken: 2:00 3 / 66

Sandwiches! How do you make a 2 sandwiches? get out the bread, ham, lettuce, cheese and condiments cut lettuce and cheese spread condiments on bread add remaining ingredients put bread and other ingredients away Time taken: 2:00 4 / 66

Sandwiches! 2x How do you make a 2 sandwiches? get out the bread, ham, lettuce, cheese and condiments cut lettuce and cheese spread condiments on bread add remaining ingredients put bread and other ingredients away Time taken: 2:00 4:00 5 / 66

Sandwiches! How do you make a 2 sandwiches? 2x get out the bread, ham, lettuce, cheese and condiments cut lettuce and cheese spread condiments on bread add remaining ingredients put bread and other ingredients away Time taken: 2:00 4:00 2:45 6 / 66

Would you like your sandwich toasted? Bridging to the Internet of Things This toaster has artificial intelligence and can make toast, give you the temperature, and in this specific example, most notably talks Just as we avoided extra work with making sandwiches we want to avoid the network latency that comes from talking to our toaster 7 / 66

Outline 1 Remote Procedure Calls (RPCs) 2 Introducing Haskell 3 Remote Monad (and Remote Applicative Functors) 4 Case Studies of Remote Monad Usage 5 Performance of Remote Monad in Situ 6 Related Work 7 Conclusion 8 / 66

Remote Procedure Calls Examples of usage: Supercomputing Cloud Computing Internet of Things [[ClientMachine]] [ ] Remote Resource 9 / 66

Remote Procedure Calls Examples of usage: Supercomputing Cloud Computing Internet of Things [[ClientMachine]] [ ] Remote Resource Problem: RPCs are expensive because networks have latency (Old) Solution: Multiple RPC requests per network transaction RPCs therefore amortize the cost of remoteness New Problem: Need a robust mechanism for bundling RPC calls without obfuscating the RPC API 10 / 66

Remote Procedure Calls What is needed for RPCs? A remote machine listening for requests A local machine that has knowledge of the remote API and protocol to be used A network transmission mechanism 11 / 66

Remote Procedure Calls What is needed for RPCs? A remote machine listening for requests A local machine that has knowledge of the remote API and protocol to be used A network transmission mechanism <?xml version="1.0"?> <methodcall> <methodname>circlearea</methodname> <params> <param> <value><double>2.41</double></value> </param> </params> </methodcall> 12 / 66

Remote Procedure Calls What is needed for RPCs? A remote machine listening for requests A local machine that has knowledge of the remote API and protocol to be used A network transmission mechanism --> {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1} <-- {"jsonrpc": "2.0", "result": 19, "id": 1} 13 / 66

Remote Procedure Calls What is needed for RPCs? A remote machine listening for requests A local machine that has knowledge of the remote API and protocol to be used A network transmission mechanism --> [ {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method": "subtract", "params": [42,23], "id": "2"} ] <-- [ {"jsonrpc": "2.0", "result": 7, "id": "1"}, {"jsonrpc": "2.0", "result": 19, "id": "2"} ] 14 / 66

Haskell Why Haskell? What sets Haskell apart from other languages? strongly typed with automatic inference no reassignment recursion/map/reduce instead of loops explicit side-effects determinicity expression evaluation instead of sequence evaluation 15 / 66

Haskell Why Haskell? What sets Haskell apart from other languages? strongly typed with automatic inference no reassignment recursion/map/reduce instead of loops explicit side-effects determinicity expression evaluation instead of sequence evaluation first-class control 16 / 66

Functional Programming Functional Programming Pure Functions + Immutability f(4) => 9 Structures that can construct and compose effect out of pure functions putstr "Hello " *> putstr "World" Two flavors of effect composition: Applicative Functor Monad (Super Applicative Functor) 17 / 66

Haskell Side-effects addpure :: Int -> Int -> Int addpure x y = x + y 18 / 66

Haskell Side-effects addpure :: Int -> Int -> Int addpure x y = x + y addio :: Int -> Int-> IO Int addio x y = do putstrln "Writing to file" writefile "tmp.txt" "side-effect" return (x + y) 19 / 66

Haskell Structures Applicative Functors Functors - Values wrapped in some context. data Maybe a = Just a Nothing Image Credit: Aditya Bhargava - adit.io 20 / 66

Haskell Structures Applicative Functors Applicative Functors - Wrapped functions applied to wrapped values Just (+3) < > Just 2 Image Credit: Aditya Bhargava - adit.io 21 / 66

Haskell Structures Monads Monads Used for side-effects Can be composed together Some require a run function before any side effects occur return :: (Monad m) => a -> m a (>>=) :: m a -> (a -> m b) -> m b runm :: m a ->... 22 / 66

Haskell Structures Monads Monads Used for side-effects Can be composed together Some require a run function before any side effects occur return :: (Monad m) => a -> m a (>>=) :: m a -> (a -> m b) -> m b runm :: m a ->... Can we execute runm remotely? 23 / 66

Haskell Structures Let s model running a monad remotely in Haskell 24 / 66

Internet of Things Toaster - IO Say {String} Temperature Uptime {String} example :: IO (Int,Double) example = do say "Hello " t <- temperature say "World!" u <- uptime "orange" return (t,u) 25 / 66

Internet of Things Toaster - GADT data R where Say :: String -> R () Temperature :: R Int Uptime :: String -> R Double say :: String -> R () say s = Say s temperature :: R Int temperature = Temperature uptime :: String -> R Double uptime s = Uptime s 26 / 66

Internet of Things Execution function runr :: forall a. R a -> IO a runr (Say s) = print s runr (Temperature) = return 23 runr (Uptime s) = getuptime s runr gives us an interpretation of R in IO 27 / 66

Internet of Things Execution function runr :: forall a. R a -> IO a runr (Say s) = void $ post "http://toaster.com/1234/say" (tojson s) runr (Temperature) = get "http://toaster.com/1234?temperature" runr (Uptime s) = get "http://toaster.com/1234?uptime=" ++ s runr gives us an interpretation of R in IO 28 / 66

Naming things: Natural Transformation In mathematics, R a -> IO a is called a natural transformation Definition A natural transformation arrow F G α. F α G α In Haskell: type f > g = forall a. f a -> g a runr :: R > IO 29 / 66

Batching We ve handled modeling single RPCs, can we incorporate batching? First Attempt: [R a] -> IO [a] All results need to be of the same type Lacks composability This is the space where most other batching RPC libraries reside Let s be more systematic 30 / 66

Remote Monad data RM :: * -> * where Bind :: RM a -> (a -> RM b) -> RM b Return :: a -> RM a Prim :: R a -> RM a 31 / 66

Remote Monad data RM :: * -> * where Bind :: RM a -> (a -> RM b) -> RM b Return :: a -> RM a Prim :: R a -> RM a runremotemonad :: (R > IO) -> (RM > IO) example :: IO (Int,Double) example = (run $ runremotemonad runr) $ do say "Hello " t <- temperature say "World!" u <- uptime "orange" return (t,u) 32 / 66

Packet Bundling Notation [ Remote Monad ] [ Remote Monad [ ] Weak [ ] Better Packet Packet ] Remote monad evaluator requires a packet evaluator 33 / 66

Serializing Bind prim1 >>= \ x ->... prim2... 34 / 66

Serializing Bind prim1 >>= \ x ->... prim2... Definition Command - a request to perform an action for remote effect, where there is no result value or temporal consequence Definition Procedure - a request to perform an action for its remote effect, where there is a result value or temporal consequence cmd >>= \ () ->... prim2... 35 / 66

Bundling Strategies Weak Bundling Command Procedure Strong Bundling Command* Procedure Can we get a better bundling? 36 / 66

Bundling Strategies Weak Bundling Command Procedure Strong Bundling Command* Procedure Applicative Bundling (Command Procedure)* f <$> prim1 <*> prim2 <*>... 37 / 66

Bundling Strategies Weak Bundling Command Procedure Strong Bundling Command* Procedure Applicative Bundling (Command Procedure)* f <$> prim1 <*> prim2 <*>... example = do say "Hello " t <- temperature say "World!" u <- uptime "orange" return (t,u) 38 / 66

Bundling Strategies Weak Bundling Command Procedure Strong Bundling Command* Procedure Applicative Bundling (Command Procedure)* f <$> prim1 <*> prim2 <*>... example = (,) <$> (say "Hello " *> temperature) <*> (say "World!" *> uptime "orange") 39 / 66

Bundling Strategies Weak Bundling Command Procedure Strong Bundling Command* Procedure Applicative Bundling (Command Procedure)* f <$> prim1 <*> prim2 <*>... 40 / 66

Packet Bundling Landscape [ Remote Monad ] [ Remote Monad ] [ Remote Monad ] [ ] Weak [ ] Strong [ ] Applicative Packet Packet Packet [ Remote ] [ Remote ] [ Remote ] Applicative Applicative Applicative [ ] Weak [ ] Strong [ ] Applicative Packet Packet Packet 41 / 66

Packet Bundling Landscape [ Remote Monad ] [ Remote Monad ] [ Remote Monad ] [ ] Weak [ ] Strong [ ] Applicative Packet Packet Packet [ Remote ] [ Remote ] [ Remote ] Applicative Applicative Applicative [ ] Weak [ ] Strong [ ] Applicative Packet Packet Packet 42 / 66

Stack of evaluators [ Remote Monad ] RemoteMonad > IO [ Applicative Packet ] ApplicativePacket > IO runmonad :: (Monad m) => (ApplicativePacket R > m) -> (RemoteMonad R > m) 43 / 66

Remote Monad & Remote Applicative data RemoteMonad p a where Appl :: RemoteApplicative p a -> RemoteMonad p a Bind :: RemoteMonad p a -> (a -> RemoteMonad p b) -> RemoteMonad p b... data RemoteApplicative p a where Prim :: p a -> RemoteApplicative p a Ap :: RemoteApplicative p (a -> b) -> RemoteApplicative p a -> RemoteApplicative p b Pure :: a -> RemoteApplicative p a 44 / 66

Splitting up Monads instance Applicative (RemoteMonad p) where pure a = Appl (pure a) Appl f <*> Appl g = Appl (f <*> g) f <*> g = Ap f g instance Monad (RemoteMonad p) where return = pure m >>= k = Bind m k m1 >> m2 = m1 *> m2 45 / 66

Example data R :: * where Say :: String -> R () Temperature :: R Int Uptime :: String -> R Double -- RemoteMonad R a say :: String -> RemoteMonad R () say s = Appl $ Prim (Say s)... runr :: R > IO runrpacket :: WeakPacket R > IO send :: RemoteMonad R a -> IO a send = run $ runmonad runrpacket 46 / 66

Other Investigations How to handle failure: Alternative Construct ( a < > b ) Procedure encapsulates failure Alternative Packet Serialize Exceptions Remote Monad as a Monad Transformer Effects of bundling with ApplicativeDo Extension Haxl implementation Exception Handling 47 / 66

Case Studies Transformations over natural transformations of monads results in a useful API and allows us to model a network stack Goal: Show the Remote Monad being used in a variety of situations [ Remote Monad ] [[Packet]] 48 / 66

Case Study blank-canvas Blank Canvas Haskell code to interact and draw on HTML5 Canvas Weak, Strong, Applicative bundling Created by KU Functional Programming Group including Ryan Scott and David Young as well as other developers from the community Client [[Canvas]] [[RemoteMonad]] [[Packet]] [ JavaScript Packets ] Server [[Browser]] 49 / 66

Case Study remote-json Remote JSON Client Server JSON-RPC implementation Id s used to pair results with requests Weak, Strong and Applicative Bundling [[RPC]] [[RemoteMonad]] [[Packet]] [[Call]] [[SendAPI ]] [[ReceiveAPI ]] 50 / 66

Case Study remote-binary Remote Binary Serialization to byte strings Results start with success/error byte Applicative Bundling Client [[RemoteMonad]] [[ApplicativePacket]] Server [ Fmap ] ApplicativePacket [[SendAPI ]] [[SendAPI ]] 51 / 66

Case Study haskino Haskino Created by Mark Grebe Haskell programs interacting with an Arduino commands sent as bytecode to interpreter ported to use remote monad in 10 hours Host [[Arduino]] [[RemoteMonad]] [[ApplicativePacket]] Arduino Board [[Interpreter]] 52 / 66

Case Study PlistBuddy PlistBuddy Property List files (.plist) interacts with shell program Weak Bundling Client [[RemoteMonad]] [[WeakPacket]] [[Text]] Server [[InteractiveShell]] 53 / 66

Case Study haxl Haxl Read only queries Query Bundling Optimized to use arbitrarily ordering capability Client [[R]] [[RemoteMonad]] [[QueryPacket]] Server [[QueryPacket]] 54 / 66

Performance Command-Centric Benchmarks Bezier ImageMark CirclesRandomSize StaticAsteroids CirclesUniformSize Rave FillText 55 / 66

Performance Procedure-Centric Benchmarks IsPointInPath MeasureText ToDataURL 56 / 66

Performance Example: StaticAsteroids benchmark :: CanvasBenchmark benchmark ctx = do xs <- replicatem 1000 $ randomxcoord ctx ys <- replicatem 1000 $ randomycoord ctx dxs <- replicatem 1000 $ randomrio (-15, 15) dys <- replicatem 1000 $ randomrio (-15, 15) send ctx $ do clearcanvas sequence_ [showasteroid (x,y) (mkpts (x,y) ds) x <- xs y <- ys ds <- cycle $ splitevery 6 $ zip dxs dys ] showasteroid :: Point -> [Point] -> Canvas () showasteroid (x,y) pts = do beginpath() moveto (x,y) mapm_ lineto pts closepath() stroke() 57 / 66

Performance StaticAsteroids Packet Distribution # Commands Procedures Packets per packet per packet Weak 1x 0 1 9992x 1 0 Strong 1x 9992 1 Applicative 1x 9992 1 Table: StaticAsteroids Packet profile from a single test run 58 / 66

Performance MeasureText Packet Distribution Weak Strong Applicative # Commands Procedures Packets per packet per packet 2002x 0 1 5x 1 0 2000x 0 1 1x 2 1 1x 3 1 1x 0 1 1x 2 2000 1x 3 1 Table: MeasureText Packet profile from a single run of the test 59 / 66

Performance Benchmark Weak (ms) Strong (ms) Applicative (ms) Bezier 113.7 71.4 80.0 CirclesRandomSize 138.5 52.2 59.6 CirclesUniformSize 134.9 48.5 55.6 FillText 150.4 75.6 87.4 ImageMark 184.7 70.2 76.0 StaticAsteroids 374.3 112.4 128.2 Rave 48.8 20.9 26.0 IsPointInPath 447.8 359.1 199.3 MeasureText 682.9 689.2 142.8 ToDataURL 211.1 208.2 238.9 Table: Performance Comparison of Bundling Strategies (Chrome v64.0.3282.186) 60 / 66

Performance Results Weak - Globally slower Strong - fastest in non interaction Applicative - fastest with interactions but additional overhead cost when compared to Strong (Only noticeable when sending packets of the same composition) Possibility of a hybrid packet between the Strong and Applicative 61 / 66

Related Work Outside of Haskell RPCs and batching RPCs: B.J. Nelson - PhD Dissertation on RPC Shakib et al. - Patent for bundling asyncronous calls with synchronous RPC Bogle et al. - Batched futures, batches as transactions Gifford et al. - RPCs as remote pipes, buffered sends Alfred Spector - No response for Asynchronous calls 62 / 66

Related Work Haskell Haxl - Facebook Uses Applicative Functor to split monad Procedures are read-only Optimized for parallelism Free Delivery - Jeremy Gibbons Free Applicative Functors Applicative bundling Cloud Haskell Distributed system using Erlang-style messages GHC Static pointers used for server functions 63 / 66

Contributions Investigations Remote choices and failure handling Relationship between Haxl and Remote Monad Applicative packet optimization for blank-canvas Publications/Talks Haskell Symposium 2015 paper IFL 2016 - invited talk Haskell Symposium 2017 paper Open Source Libraries remote-monad library remote-json library remote-binary library 64 / 66

Future Work Remote Monad-Transformer Local IO Use of GHC static keyword Template Haskell Is there a better packet than applicative? 65 / 66

Conclusion We can systematically bundle primitives in an environment with first-class control We examined the properties of remote primitives yielding different bundling strategies We observe that we can model network stacks by chaining natural transformations together We conclude that applicative functors make a great packet structure 66 / 66