(Today's lecture is not on the midterm)

Similar documents
Haskell a lazy, purely func1onal language

Haskell. COS 326 Andrew W. Appel Princeton University. a lazy, purely functional language. slides copyright David Walker and Andrew W.

David Walker. Thanks to Kathleen Fisher and recursively to Simon Peyton Jones for much of the content of these slides.

COS 326 David Walker. Thanks to Kathleen Fisher and recursively to Simon Peyton Jones for much of the content of these slides.

CS 11 Haskell track: lecture 1

A Func'onal Introduc'on. COS 326 David Walker Princeton University

Haskell Monads CSC 131. Kim Bruce

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

Introduction to Haskell

Thinking Induc,vely. COS 326 David Walker Princeton University

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

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

Thinking Induc,vely. COS 326 David Walker Princeton University

Programming with Math and Logic

CS The IO Monad. Slides from John Mitchell, K Fisher, and S. Peyton Jones

I/O in Purely Functional Languages. Stream-Based I/O. Continuation-Based I/O. Monads

CS153: Compilers Lecture 15: Local Optimization

Informatics 1 Functional Programming 19 Tuesday 23 November IO and Monads. Philip Wadler University of Edinburgh

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CS457/557 Functional Languages

Muta%on. COS 326 David Walker Princeton University

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

Haskell Introduction Lists Other Structures Data Structures. Haskell Introduction. Mark Snyder

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

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

Mutation. COS 326 David Walker Princeton University

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

A Functional Evaluation Model

CSCI-GA Scripting Languages

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

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

CS 360: Programming Languages Lecture 10: Introduction to Haskell

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

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

Haskell: From Basic to Advanced. Part 2 Type Classes, Laziness, IO, Modules

IO Monad (3C) Young Won Lim 1/6/18

Introduction to Functional Programming and Haskell. Aden Seaman

Implemen'ng OCaml in OCaml

The Haskell HOP: Higher-order Programming

Programming Languages and Techniques (CIS120)

Harvard School of Engineering and Applied Sciences CS 152: Programming Languages

Advanced Topics in Programming Languages Lecture 2 - Introduction to Haskell

Principles of Programming Languages

Informatics 1 Functional Programming Lectures 15 and 16. IO and Monads. Don Sannella University of Edinburgh

CS 457/557: Functional Languages

Topics Covered Thus Far CMSC 330: Organization of Programming Languages

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

Monads. COS 441 Slides 16

Programming in Haskell Aug Nov 2015

Lecture 10: Potpourri: Enum / struct / union Advanced Unix #include function pointers

LECTURE 16. Functional Programming

OCaml Data CMSC 330: Organization of Programming Languages. User Defined Types. Variation: Shapes in Java

Mutable Data Types. Prof. Clarkson Fall A New Despair Mutability Strikes Back Return of Imperative Programming

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

Introduction to OCaml

!!"!#"$%& Atomic Blocks! Atomic blocks! 3 primitives: atomically, retry, orelse!

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

Programming Paradigms

More on functional programming

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

Lecture 4: Build Systems, Tar, Character Strings

Programming Languages and Techniques (CIS120)

Programming Languages

JVM ByteCode Interpreter

IO Monad (3C) Young Won Lim 8/23/17

Background. CMSC 330: Organization of Programming Languages. Useful Information on OCaml language. Dialects of ML. ML (Meta Language) Standard ML

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

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

Lazy Functional Programming in Haskell

Monads. Prof. Clarkson Fall Today s music: Vámanos Pal Monte by Eddie Palmieri

Haske k ll An introduction to Functional functional programming using Haskell Purely Lazy Example: QuickSort in Java Example: QuickSort in Haskell

Lecture 13: Abstract Data Types / Stacks

CSCE 314 Programming Languages. Interactive Programming: I/O

CSC324 Principles of Programming Languages

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

Condi(onals and Loops

An introduction introduction to functional functional programming programming using usin Haskell

Programming Languages and Techniques (CIS120)

CMSC 330: Organization of Programming Languages. OCaml Imperative Programming

CSE341: Programming Languages Lecture 11 Type Inference. Dan Grossman Spring 2016

Futures. COS 326 David Walker Princeton University

Introduction to Functional Programming

CSE399: Advanced Programming

Haskell An Introduction

Monads in Haskell. Nathanael Schilling. December 12, 2014

Monad Background (3A) Young Won Lim 10/5/17

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

Programming Languages Fall 2013

CS 440: Programming Languages and Translators, Spring 2019 Mon 2/4

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

Modules and Representa/on Invariants

A Func'onal Space Model

HASKELL I/O. Curt Clifton Rose-Hulman Institute of Technology. Please SVN Update your HaskellInClass folder, then open eieio.hs

Processadors de Llenguatge II. Functional Paradigm. Pratt A.7 Robert Harper s SML tutorial (Sec II)

Tuples. CMSC 330: Organization of Programming Languages. Examples With Tuples. Another Example

To figure this out we need a more precise understanding of how ML works

CMSC 330: Organization of Programming Languages

Principles of Programming Languages

Programming Languages and Techniques (CIS120)

Ruby: Introduction, Basics

Transcription:

Midterm on Wednesday of this week in class 3-4:20pm Tenta=ve loca=on: Friend 101 (we are s.ll checking... will no.fy on Piazza... will direct you on the day) Topics: Possibly revisi.ng ques.ons similar to midterm 1 (eg: equa.onal reasoning) Anything in lecture or assigned reading beginning with modules (eg: modules, functors, reasoning about modules, laziness, concurrency) Material oriented around assignments 5 and 6 (Today's lecture is not on the midterm)

A Digression: Introducing Haskell COS 326 David Walker Princeton University

Haskell Another cool, typed, func.onal programming language Like OCaml in that: it is a func.onal language with parametric polymorphism Unlike OCaml in that it is: pure: func.ons with type a - > b have no effect! lazy: f e does not evaluate e right away; passes e to f has a weak module system; uses type classes has a very sophis.cated type system with higher kinds has several cool extensions for concurrent and parallel programming including transac.onal memory

HASKELL BASICS

Haskell Defini.ons Mostly like OCaml, with a few small syntac.c differences parameters are immutable by default let declara.ons introduce new func.ons and value foo z = let triple x = x*3 in triple z equivalently, we might use a "where" defini.on: foo z = triple z where triple x = x*3

Haskell Indenta.on Haskell, like Python, but unlike Java, OCaml or math wri`en in a notebook, has seman.cally meaningful indenta.on Wrong: must indent func.on body copies k n = if n == 0 then [] else k : copies k (n- 1) indent y =... zap z = let x = z y = z + z in x + y indent z + z zap z = let x = z y = z + z in x + y

Haskell Indenta.on Haskell, like Python, but unlike Java, OCaml or math wri`en in a notebook, has seman.cally meaningful indenta.on Right: beginning of x defines indenta.on level copies k n = if n == 0 then [] else k : copies k (n- 1) zap z = let x = z y = z + z in x + y zap z = let x = z y = z + z in x + y

Haskell Types We have the op.on of declaring the type of a defini.on prior to the defini.on itself zap :: Int - > Int zap z = let x = z y = z + z in x + y just to be annoying, Haskell uses "::" for "has type" and uses ":" for "cons" the opposite of OCaml

Tuples Haskell uses tuples like OCaml constructed by enclosing a sequence of values in parens: ( b, 4) :: (Char, Integer) deconstructed (used) via pa`ern matching: easytoo :: (Integer, Integer, Integer) - > Integer easytoo (x, y, z) = x + y * z

Lists Lists are very similar to OCaml lists but the type is wri`en [t] instead of "t list" [1, 2, 3] :: [Integer] [ a, b, c ] :: [Char] [ ] is the empty list (called nil) 1:2:[] is a list with 2 elements String is a synonym for [Char] We can build lists of lists: [ [1, 2], [3], [8, 9, 10] ] :: [ [ Integer ] ]

Func.ons over lists - - Sum the elements of a list listsum :: [ Integer ] - > Integer listsum [ ] = 0 listsum (x:xs) = x + listsum xs common to write func.ons using mul.ple clauses, where each clause matches on a different case

Func.ons deconstruc.ng lists - - Sum the elements of a list listsum :: [ Integer ] - > Integer lower case le`er = any type at all (a type variable) length :: [a] - > Int listsum [ ] = 0 listsum (x:xs) = x + listsum xs upper case le`er = a concrete type length [ ] = 0 length (x:xs) = 1 + length xs

Func.ons deconstruc.ng lists - - Sum the elements of a list listsum :: [ Integer ] - > Integer listsum [ ] = 0 listsum (x:xs) = x + listsum xs cat :: [a] - > [a] - > [a] length :: [a] - > Int length [ ] = 0 length (x:xs) = 1 + length xs cat [ ] xs2 = xs2 cat (x:xs) xs2 = x:(cat xs xs2)

Func.ons deconstruc.ng lists - - Sum the elements of a list listsum :: [ Integer ] - > Integer listsum [ ] = 0 listsum (x:xs) = x + listsum xs cat :: [a] - > [a] - > [a] length :: [a] - > Int length [ ] = 0 length (x:xs) = 1 + length xs cat [ ] xs2 = xs2 cat (x:xs) xs2 = x:(cat xs xs2) (++) :: [a] - > [a] - > [a] (++) [ ] xs2 = xs2 (++) (x:xs) xs2 = x:(xs ++ xs2)

PURITY & SUBSTITUTION OF EQUALS FOR EQUALS

Subs.tu.on of Equals for Equals A key law about Haskell programs: let x = <exp> in... x... x... =... <exp>... <exp>... For example: let x = 4 `div` 2 in x + 5 + x = (4 `div` 2) + 5 + (4 `div` 2) = 9

Subs.tu.on of Equals for Equals We'd also like to use func.onal abstrac.on without penalty halve :: Int - > Int halve n = n `div` 2 And instead of telling clients about all implementa.on details, simply expose key laws: Lemma 1: for all n, if n is even then (halve n + halve n) = n Now we can reason locally within the client: let x = halve 4 in x + x = = = = (halve 4) + 5 + (halve 4) (halve 4) + (halve 4) + 5 4 + 5 9 (subs.tu.on) (arithme.c) (Lemma 1) (arithme.c)

Computa.onal Effects What happens when we add mutable data structures? Consider this OCaml program: let x = ref 0 let foo (y:int) : int = x :=!x + 1; arg +!x; foo : int - > int We lose a lot of reasoning power! let y = foo 3 in y + y foo 3 + foo 3

Computa.onal Effects What happens when we add mutable data structures? Consider this OCaml program: let x = ref 0 let foo (y:int) : int = x :=!x + 1; arg +!x; foo : int - > int We lose a lot of reasoning power! let y = foo 3 in y + y foo 3 + foo 3 8 9

Computa.onal Effects What happens when we add mutable data structures? Consider this OCaml program: foo : int - > int let foo (y:int) : int = print_int y; arg +!x; We lose a lot of reasoning power! let y = foo 3 in y + y foo 3 + foo 3 6 prin.ng "3" 6 prin.ng "33"

Computa.onal Effects A func.on has an effect if its behavior cannot be specified exclusively as a rela.on between its input and its output I/O is an effect An impera.ve update of a data structure is an effect When func.ons can no longer be described exclusively in terms of the rela.onship between arguments and results many, many fewer equa.onal laws hold In general, in OCaml, let x = <exp> in... x... x...... <exp>... <exp>... In general, in Haskell, let x = <exp> in... x... x... =... <exp>... <exp>...

HASKELL EFFECTS INPUT AND OUTPUT

I/O in Haskell Haskell has a special kind of value called an ac.on that describes an effect on the world Pure ac.ons, which just do something and have no interes.ng result are values of type IO () Eg: putstr takes a string and yields an ac.on describing the act of displaying this string on stdout - - writes string to stdout putstr :: String - > IO () - - writes string to stdout followed by newline putstrln :: String - > IO ()

I/O in Haskell When do ac.ons actually happen? Ac.ons happen under two circumstances:* 1. the ac.on defined by main happens when your program is executed ie: you compile your program using ghc; then you execute the resul.ng binary 2. the ac.on defined by any expression happens when that expression is wri`en at the ghci prompt * there is one other circumstance: Haskell contains some special, unsafe func.ons that will perform I/O, most notably System.IO.Unsafe.unsafePerformIO so I lied when earlier when I said the equa.on holds in general for Haskell programs...

I/O in Haskell hello.hs: main :: IO () main = putstrln Hello world in my shell: dpw@schenn ~/cos441/code/trial $ ghc hello.hs [1 of 1] Compiling Main ( hello.hs, hello.o ) Linking hello.exe... dpw@schenn ~/cos441/code/trial $./hello.exe hello world!

bar.hs: bar :: Int - > IO () bar n = putstrln (show n ++ is a super number ) main :: IO () main = bar 6 in my shell: dpw@schenn ~/cos441/code/trial $ ghcii.sh GHCi, version 7.0.3: h`p://www.haskell.org/ghc/ :? for help Loading package ghc- prim... linking... done. Loading package integer- gmp... linking... done. Loading package base... linking... done. Loading package ffi- 1.0... linking... done. Prelude> :l bar [1 of 1] Compiling Main ( bar.hs, interpreted ) Ok, modules loaded: Main. *Main> bar 17 17 is a super number *Main> main 6 is a super number *Main>

Ac.ons Ac.ons are descrip.ons of effects on the world. Simply wri.ng an ac.on does not, by itself cause anything to happen bar.hs: hellos :: [IO ()] hellos = [putstrln Hi, putstrln Hey, putstrln Top of the morning to you ] main = hellos!! 2 in my shell: Prelude> :l hellos... *Main> main Top of the morning to you *Main>

Ac.ons Ac.ons are just like any other value - - we can store them, pass them to func.ons, rearrange them, etc: sequence_ :: [IO ()] - > IO () baz.hs: hellos :: [IO ()] hellos = [putstrln Hi, putstrln Hey, putstrln Top of the morning to you ] main = sequence_ (reverse hellos) in my shell: Prelude> :l hellos... *Main> main Top of the morning to you Hey HI

Combining Ac.ons The infix operator >> takes two ac.ons a and b and yields an ac.on that describes the effect of execu.ng a then execu.ng b a erward howdy :: IO () howdy = putstr how >> putstrln dy To combine many ac.ons, use do nota.on: bonjour :: IO () bonjour = do putstr Bonjour! putstr putstrln Comment ca va?

Quick Aside: Back to SEQEQ* Do we s.ll have it? Yes! let a = PutStrLn "hello" in do a a = do PutStrLn "hello" PutStrLn "hello" an ac.on made up of doing a and then doing a again where a is the PutStrLn "hello" ac.on an ac.on made up of doing the PutStrLn "hello" ac.on and then doing the PutStrLn "hello" ac.on again * SEQEQ = subs.tu.on of equals for equals

Input Ac.ons Some ac.ons have an effect and yield a result: - - get a line of input getline :: IO String - - get all of standard input un.l end- of- file encountered getcontents :: IO String - - get command line argument list getargs :: IO [String] What can we do with these kinds of ac.ons? we can extract the value and sequence the effect with another:

Input Ac.ons Some ac.ons have an effect and yield a result: - - get a line of input getline :: IO String - - get all of standard input un.l end- of- file encountered getcontents :: IO String - - get command line argument list getargs :: IO [String] What can we do with these kinds of ac.ons? we can extract the value and sequence the effect with another: do s <- getline putstrln s

Input Ac.ons Some ac.ons have an effect and yield a result: - - get a line of input getline :: IO String - - get all of standard input un.l end- of- file encountered getcontents :: IO String - - get command line argument list getargs :: IO [String] What can we do with these kinds of ac.ons? we can extract the value and sequence the effect with another: s has type string do s <- getline putstrln s getline has type IO string

Input Ac.ons Some ac.ons have an effect and yield a result: - - get a line of input getline :: IO String - - get all of standard input un.l Think end- of- file of type encountered IO String getcontents :: IO String as a box containing a computa.on that will - - get command line argument do list some work and then The "do" packages getargs up :: the IO [String] produce a string. getline getline and the putstrln is such a box. the <- gets s ac.ons up in to a bigger What can we do with s these kinds the of String ac.ons? out of the box composite box we can extract the value and sequence the effect with another: do s <- getline putstrln s

A whole program: Input Ac.ons main :: IO () main = do putstrln What s your name? s <- getline putstr Hey, putstr s putstrln, cool name!

import modules import System.IO import System.Environment processargs :: [String] - > String processargs [a] = a processargs _ = "" echo :: String - > IO () echo "" = putstrln "Bad Args!" echo filename = do s <- readfile filename putstrln "Here it is:" putstrln "***********" putstr s putstrln "\n***********" main :: IO () main = do args <- getargs let filename = processargs args echo filename contains readfile contains getargs, getprogname <- nota.on: RHS has type IO T LHS has type T let nota.on: RHS has type T LHS has type T

SEQEQ (Again!) Recall: s1 ++ s2 concatenates String s1 with String s2 A valid reasoning step: let s = "hello" in do putstrln (s ++ s) = do putstrln ("hello" ++ "hello")

SEQEQ (Again!) Recall: s1 ++ s2 concatenates String s1 with String s2 A valid reasoning step: let s = "hello" in do putstrln (s ++ s) = do putstrln ("hello" ++ "hello") A valid reasoning step: do let s = "hello" putstrln (s ++ s) = do putstrln ("hello" ++ "hello")

SEQEQ (Again!) Recall: s1 ++ s2 concatenates String s1 with String s2 A valid reasoning step: let s = "hello" in do putstrln (s ++ s) = do putstrln ("hello" ++ "hello") A valid reasoning step: do let s = "hello" putstrln (s ++ s) Wait, what about this: do s <- getline putstrln (s ++ s) = do putstrln ("hello" ++ "hello") wrong type: getline :: IO String do putstrln (getline ++ getline)

Invalid reasoning step? SEQEQ (Again!) let s = getline in do putstrln (s ++ s)? = do putstrln (getline ++ getline)

Invalid reasoning step? SEQEQ (Again!) let s = getline in do putstrln (s ++ s)? = do putstrln (getline ++ getline) wrong type: s :: IO String wrong type: getline :: IO String

Invalid reasoning step? SEQEQ (Again!) let s = getline in do putstrln (s ++ s)? = do putstrln (getline ++ getline) wrong type: s :: IO String wrong type: getline :: IO String The Haskell type system shows x <- e is different from let x = e x has a different type in each case let x = e enables subs.tu.on of e for x in what follows x <- e does not enable subs.tu.on - - a`emp.ng subs.tu.on leaves you with code that won't even type check because x and e have different types (type T vs. type IO T)

HASKELL EFFECTS: REFERENCES

Coming Back to References Again Remember this OCaml func.on: let x = ref 0 foo : int - > int We no.ced that: let foo (y:int) : int = x :=!x + 1; arg +!x; let y = foo 3 in y + y foo 3 + foo 3 What if we write something similar in Haskell? : int

Coming Back to References Again Remember this OCaml func.on: let x = ref 0 foo : int - > int We no.ced that: let foo (y:int) : int = x :=!x + 1; arg +!x; let y = foo 3 in y + y foo 3 + foo 3 What if we write something similar in Haskell? x :: Ref int foo :: int - > int foo y = x := read x + 1; arg +!x; doesn't type check : int

Haskell Types Tell You Where the Effects Aren't! Haskell func3on types are pure - - totally effect- free foo : int -> int Haskell s type system forces* purity on func.ons with type a -> b no prin.ng no mutable data no reading from files no concurrency no benign effects (like memoiza.on) * except for unsafeperformio exp : int Same with expressions. Expressions with just type int have no effect!

Haskell Types Tell You Where the Effects Aren't! foo :: int -> int totally pure func3on <code> :: IO int suspended (lazy) computa3on that performs effects when executed

Haskell Types Tell You Where the Effects Aren't! foo :: int -> int totally pure func3on <code> :: IO int suspended (lazy) computa3on that performs effects when executed bar :: int -> IO int totally pure func3on that returns suspended effec@ul ac3on

Haskell Types Tell You Where the Effects Aren't! print :: string -> IO () reverse :: string -> string reverse hello :: string print (reverse hello ) :: IO () the type system always tells you when an effect has happened effects can t escape the I/O monad

References read :: Ref a -> IO a (+) :: int -> int -> int r :: Ref int (read r) + 3 :: int Doesn t type check

References read :: Ref a -> IO a (+) :: int -> int -> int r :: Ref int do x <- read r return (x + 3) the "return" ac.on has no effect; it just returns its value creates a composi.on ac.on that reads a references and produces the value in that reference plus 3

Mutable State new :: a -> IO (Ref a) read :: Ref a -> IO a write :: Ref a -> a -> IO () Haskell uses new, read, and write* func.ons within the IO Monad to manage mutable state. main = do r <- new 0; -- let r = ref 0 in inc r; -- r :=!r+1; s <- read r; -- s :=!r; print s inc :: Ref Int -> IO () inc r = do v <- read r; -- v :=!r write r (v+1) -- r :=!v +1 * actually newref, readref, writeref,

MONADS

The Bigger Picture Haskell's type system (at least the part of it that we have seen so far) is very similar to the ML type system eg: typing rules for pure primi.ves (func.ons, pairs, lists, etc) are similar in both cases: if f : t1 - > t2 and e : t1 then (f e) : t2 if e1 : t1 and e2 : t2 then (e1, e2) : (t1, t2) What Haskell has done that is special is: it has been very careful to give effec ul primi.ves special types involving "IO t" it has a func.ons to compose ac.ons with type "IO t" the IO data type + its composi.on func.ons are called a monad it has special syntax for programming with monads

We can talk about what monads are by referring to their interface Recall an interface declares some new abstract types and some opera.ons over values with those abstract types. For example: module type CONTAINER = sig type a t (* the type of the container *) There are lots of different implementations of such containers: queues, stacks, sets, randomized sets,... val empty : a t val insert : a -> a t -> a t val remove : a t -> a option * a t val fold : ( a -> b -> b) -> b -> a t -> b end

We can talk about what monads are by referring to their interface Recall an interface declares some new abstract types and some opera.ons over values with those abstract types. For example: module type CONTAINER = sig type a t (* the type of the container *) val empty : a t val insert : a -> a t -> a t val remove : a t -> a option * a t val fold : ( a -> b -> b) -> b -> a t -> b end There are lots of different implementations of such containers: queues, stacks, sets, randomized sets,... Interfaces can come with some equations one expects every implementation to satisfy. eg: fold f base empty == base The equations specify some, but not all of the behavior of the module (eg: stacks and queues remove elements in different orders)

Monads A monad is just a par.cular interface. Two views: (1) interface for a very generic container, with opera.ons designed to support composi3on of computa.ons over the contents of containers (2) interface for an abstract computa.on that does some book keeping on the side. Book keeping is code for has an effect. Once again, the support for composi.on is key. since func.onal programmers know that func.ons are data, the two views actually coincide

Monads A monad is just a par.cular interface. Two views: (1) interface for a very generic container, with opera.ons designed to support composi3on of computa.ons over the contents of containers (2) interface for an abstract computa.on that does some book keeping on the side. Book keeping is code for has an effect. Once again, the support for composi.on is key. since func.onal programmers know that func.ons are data, the two views actually coincide Many different kinds of monads: monads for handling/accumula.ng errors monads for processing collec.ons en masse monads for logging strings that should be printed monads for coordina.ng concurrent threads (see OCaml Asynch library) monads for back- tracking search monads for transac3onal memory

Monads A monad is just a par.cular interface. Two views: (1) interface for a very generic container, with opera.ons designed to support composi3on of computa.ons over the contents of containers (2) interface for an abstract computa.on that does some book keeping on the side. Book keeping is code for has an effect. Once again, the support for composi.on is key. since func.onal programmers know that func.ons are data, the two views actually coincide Many different kinds of monads: monads for handling/accumula.ng errors monads for processing collec.ons en masse monads for logging strings that should be printed monads for coordina.ng concurrent threads (see OCaml Asynch library) monads for back- tracking search monads for transac3onal memory Because a monad is just a par.cular interface (with many useful implementa.ons), you can implement monads in any language But, Haskell is famous for them because it has a special built- in syntax that makes monads par.cularly easy and elegant to use F#, Scala have adopted similar syntac.c ideas Monads also play a very special role in the overall design of the Haskell language

What is the monad interface? module type MONAD = sig type a M val return : a -> a M val (>>=) : a M -> ( a -> b M) -> b M + some equations specifying how return and bind are required to interact end Consider first the container interpretation : a M is a container for values with type a return x puts x in the container bind c f takes the values in c out of the container and applies f to them, forming a new container holding the results - bind c f is often written as: c >>= f

The Op.ons as a Container module type MONAD = sig type a M val return : a -> a M val (>>=) : a M -> ( a -> b M) -> b M end module OptionMonad = struct type a M = a option let return x = Some x let (>>=) c f = match c with None -> None Some v -> f v end put value in a container take value v out of a container c and then apply f, producing a new container

The Op.ons as a Container module type MONAD = sig type a M val return : a -> a M val (>>=) : a M -> ( a -> b M) -> b M end using the option container: type file_name = string val read_file : file_name -> string M let concat f1 f2 = readfile f1 >>= (fun contents1 -> readfile f2 >>= (fun contents2 -> return (contents1 ^ contents2) ;; module OptionMonad = struct type a M = a option let return x = Some x let (>>=) c f = match c with None -> None Some v -> f v end put value in a container take value v out of a container c and then apply f, producing a new container

Book- keeping Interpreta.on: The Op.on Monad as Possibly Erroneous Computa.on module type MONAD = sig type a M val return : a -> a M val (>>=) : a M -> ( a -> b M) -> b M end using the error monad: type file_name = string module ErrorMonad = struct type a M = a option let return x = Some x let (>>=) c f = match c with None -> None Some v -> f v end val read_file : file_name -> string M let concat f1 f2 = readfile f1 >>= (fun contents1 -> readfile f2 >>= (fun contents2 -> return (contents1 ^ contents2) ;; compose book-keeping: check to see if error has occurred, if so return None, else continue setting up book keeping for error processing

Lists as Containers module type MONAD = sig type a M val return : a -> a M val (>>=) : a M -> ( a -> b M) -> b M end module ListMonad = struct type a M = a list let return x = [x] let (>>=) c f = List.flatten (List.map f c) using the list monad: random_sample : unit -> int M monte_carlo : int -> int -> int -> result let experiments : result M = random_sample() >>= (fun s1 -> random_sample() >>= (fun s2 -> random_sample() >>= (fun s3 -> return (monte_carlo s1 s2 s3) ;; end apply f to all elements of the list c, creating a list of lists and then flatten results in to single list put element in to list container

The List Monad as Nondeterminis.c Computa.on module type MONAD = sig type a M val return : a -> a M val (>>=) : a M -> ( a -> b M) -> b M end using the non-determinism monad: random_sample : unit -> int M monte_carlo : int -> int -> int -> result let experiments : result M = random_sample() >>= (fun s1 -> random_sample() >>= (fun s2 -> random_sample() >>= (fun s3 -> return (monte_carlo s1 s2 s3) ;; module ListMonad = struct type a M = a list let return x = [x] let (>>=) c f = List.flatten (List.map f c) end one result; compose many no non-determinism possible results (c) with a non-deterministic continuation f

A Container with a String on the Side (aka: A logging/prin.ng monad) module type MONAD = sig type a M val return : a -> a M val (>>=) : a M -> ( a -> b M) -> b M end using the logging monad: record : ( a -> b) -> a -> string -> b M module LoggingMonad = struct type a M = a * string let return x = (x, ) let (>>=) c f = let (v, s) = c in let (v,s ) = f v in (v, s ^ s ) end let record f x s = (f x, s) let do x = record read x read it >>= (fun v -> record write v wrote it >>= (fun _ -> record write v wrote it again >>= (fun _ -> return v ;; concatenate the log of c with the log produced by running f nothing logged yet

A Container with a State on the Side (aka: A logging/prin.ng monad) module type MONAD = sig type a M val return : a -> a M val (>>=) : a M -> ( a -> b M) -> b M end module StateMonad = struct type state = address int map type a M = a * (state -> state) let return x =??? let (>>=) c f =??? let read a =??? let write a v =??? end

Monad Laws Just like one expects any CONTAINER to behave in a par.cular way, one has expecta.ons of MONADs. Le iden.ty: return does nothing observable (1) return v >>= f == f v Right iden.ty: return s.ll doesn t do anything observable (2) m >>= return == m Associa.vity: composing m with f first and then doing g is the same as doing m with the composi.on of f and g (3) (m >>= f) >>= g == m >>= (fun x - > f x >>= g)

Breaking the Law Just like one expects any CONTAINER to behave in a par.cular way, one has expecta.ons of MONADs. Le iden.ty: return does nothing observable (1) return v >>= f == f v module LoggingMonad = struct type a M = a * string return 3 >>= fun x -> return x == (3, start ) >>= fun x -> return x == (3, start ^ start ) == (3, startstart ) let return x = (x, start ) let (>>=) c f = let (v, s) = c in let (v,s ) = f v in (v, s ^ s ) end (fun x -> return x) 3 == return 3 == (3, start )

Breaking the Law What are the consequences of breaking the law? Well, if you told your friend you ve implemented a monad and they can use it in your code, they will expect that they can rewrite their code using equa.ons like this one: return x >>= f == f x If you tell your friend you ve implemented the monad interface but none of the monad laws hold your friend will probably say: Ok, tell me what your func.ons do then and please stop using the word monad because it is confusing. It is like you are claiming to have implemented the QUEUE interface but insert and remove are First- In, First- Out like a stack. In Haskell or Fsharp or Scala, breaking the monad laws may have more severe consequences, because the compiler actually uses those laws to do some transforma.ons of your code.

HASKELL WRAPUP

A Monadic Skin In languages like ML or Java, there is no way to dis.nguish between: pure func.ons with type int - > int effec ul func.ons with type int - > int In Haskell, the programmer can choose when to live in the IO monad and when to live in the realm of pure func.onal programming. Counter- point: We have shown that it is useful to be able to build pure abstrac.ons using impera.ve infrastructure (eg: laziness, futures, parallel sequences, memoiza.on). You can t do that in Haskell (without escaping the type system via unsafei0) Interes.ng perspec.ve: It is not Haskell that lacks impera.ve features, but rather the other languages that lack the ability to have a sta.cally dis.nguishable pure subset. At any rate, a checked pure- impure separa.on facilitates concurrent programming.

The Central Challenge Useful Arbitrary effects Useless No effects Dangerous Safe

The Challenge of Effects Plan A (everyone else) Useful Arbitrary effects Nirvana Plan B (Haskell) Useless Dangerous No effects Safe

Two Basic Approaches: Plan A Arbitrary effects Examples Regions Ownership types Vault, Spec#, Cyclone Default = Any effect Plan = Add restrictions

Two Basic Approaches: Plan B Default = No effects Plan = Selectively permit effects Types play a major role Two main approaches: Domain specific languages (SQL, Xquery, Google map/ reduce) Wide- spectrum func.onal languages + controlled effects (e.g. Haskell) Value oriented programming

Lots of Cross Over Useful Arbitrary effects Plan A (everyone else) Nirvana Useless Dangerous according to Simon Peyton Jones, inventor of Haskell :- ) Envy No effects Safe Plan B (Haskell)

Lots of Cross Over Useful Arbitrary effects Plan A (everyone else) Nirvana Ideas; e.g. Software Transactional Memory Plan B (Haskell) Useless Dangerous No effects Safe

An Assessment and a Predic.on One of Haskell s most significant contributions is to take purity seriously, and relentlessly pursue Plan B. Imperative languages will embody growing (and checkable) pure subsets. -- Simon Peyton Jones