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

Similar documents
Overview) Declara've)Languages) The)essence)of)IO) IO)in)Haskell)

More on functional programming

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

Monad class. Example: Lambda laughter. The functional IO problem. EDAN40: Functional Programming Functors and Monads

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

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

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

Background Type Classes (1B) Young Won Lim 6/28/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.

JVM ByteCode Interpreter

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

INTERACTION FUNCTIONS

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

Standard prelude. Appendix A. A.1 Classes

Monads seen so far: IO vs Gen

Introduction to Haskell

Monads. Bonus lecture 2017 David Sands

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

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

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

Programming Languages

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

Haskell Monads CSC 131. Kim Bruce

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

Programming Systems in Artificial Intelligence Functional Programming

Informal Semantics of Data. semantic specification names (identifiers) attributes binding declarations scope rules visibility

Functional Programming

Imperative languages

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

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

Towards data-flow oriented workflow systems

COP4020 Programming Assignment 1 - Spring 2011

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

CSE Programming Languages Midterm - Winter 2010

LECTURE 16. Functional Programming

CS 11 Haskell track: lecture 5. This week: State monads

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

Haskell Overview II (2A) Young Won Lim 8/9/16

CS 440: Programming Languages and Translators, Spring 2019 Mon

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Programming in Haskell Aug Nov 2015

Embedded Domain Specific Languages in Idris Lecture 3: State, Side Effects and Resources

Stop coding Pascal. Saturday, April 6, 13

Introduction to Programming, Aug-Dec 2006

CSCE 314 Programming Languages. Interactive Programming: I/O

OCaml Language Choices CMSC 330: Organization of Programming Languages

PROGRAMMING IN HASKELL. Chapter 10 - Interactive Programming

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

Exercise 1 ( = 18 points)

Programming Languages Fall 2013

These notes are intended exclusively for the personal usage of the students of CS352 at Cal Poly Pomona. Any other usage is prohibited without

Haskell Scripts. Yan Huang

Monads in Haskell. Nathanael Schilling. December 12, 2014

Monads. Lecture 12. Prof. Aiken CS 264 Lecture 12 1

3.Constructors and Destructors. Develop cpp program to implement constructor and destructor.

Lazy Functional Programming in Haskell

Exercise 1 ( = 22 points)

Advanced features of Functional Programming (Haskell)

Applicative, traversable, foldable

CSC 372 Haskell Mid-term Exam Feburary 28, 2014

1 Delimited continuations in Haskell

Functional Programming

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

Lecture Topics. Administrivia

Optimising Functional Programming Languages. Max Bolingbroke, Cambridge University CPRG Lectures 2010

GHCi: Getting started (1A) Young Won Lim 5/31/17

A general introduction to Functional Programming using Haskell

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Functional Programming

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

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

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

An introduction to functional programming. July 23, 2010

Programming Languages and Techniques (CIS120)

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

Type checking by theorem proving in IDRIS

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CS457/557 Functional Languages

GHCi: Getting started (1A) Young Won Lim 6/3/17

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

Lecture 8: Summary of Haskell course + Type Level Programming

Functional Programming in Haskell Part I : Basics

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

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

Tree Equality: The Problem

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

Monad P1 : Several Monad Types (4A) Young Won Lim 2/13/19

Haskell Overview II (2A) Young Won Lim 8/23/16

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

Handout 10: Imperative programs and the Lambda Calculus

Box-and-arrow Diagrams

7. Introduction to Denotational Semantics. Oscar Nierstrasz

Fibonacci in Lisp. Computer Programming: Skills & Concepts (CP1) Programming Languages. Varieties of Programing Language

Haskell Refresher Informatics 2D

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

Programming with Math and Logic

Part 0. A (Not So) Brief Introduction to Haskell

Only to be used for arranged hours. Order of Operations

CS 320 Homework One Due 2/5 11:59pm

Transcription:

Overview Declarative Languages D7012E: General monads in haskell Fredrik Bengtsson IO-monad sequencing operator monad class requirements on monad Monadic computation trivial example useful example The old IO monad remember do-notation (sequencing): getnput :: IO () getnput = do line <- getline putstrln line only for handling I/O can we handle other side-effects? Why would we like to do that? might suit problem better functional style not good for everything General monads IO a comes with several functions: return :: a -> IO a putstr :: String -> IO () getline :: IO String sequencing using do construct sequencing operator Now, we will look at the theory......at least a little bit Sequencing operator >>= pronounced "then" used to sequence two operations >>= :: IO a -> (a -> IO b) -> IO b result of action (state) IO a feeds a into second argument function new action returns result from second argument function new state sequencing two actions Sequencing operator example do-notation addoneint :: IO () addoneint = do line <- getline putstrln (show (1 + read line :: Int)) sequencing operator notation: addoneint = getline >>= \line -> putstrln (show (1 + read line :: Int)) do is a shorthand for >>= I/O a I/O b 1

Monadic style programming monads makes sequencing explicit sequencing operator (for monad m): (>>=) :: m a -> (a -> m b) -> m b necessary? consider inputint - inputint order important rewrite as do e <- getint f <- getint return (e-f) looks like an imperative program can be incorporated into a pure functional language The Monad Class The definition of the Monad class class Monad m where (>>=) :: m a -> (a -> m b) -> m b return :: a -> m a (>>) :: m a -> m b -> m b fail :: String -> m a Informal requirements on a monad return x return value without effect (just add monad type) without I/O in case of IO monad sequencing from >>= irrelevant of way of bracketing fail s fails with message s Composition operator new operator easier to read (>@>) :: Monad m => (a -> m b) -> (b -> m c) -> (a -> m c) f >@> g = \x -> ((f x) >>= g) generalizes function composition composes two objects into one compose a -> m b and b -> m c into a -> m c effect of performing first and second action Requirements on monad return is identity 1: return >@> f = f 2: f >@> return = f 3: >@> associative (f >@> g) >@> h = f >@> (g >@> h) derived operator >> also associative Equivalent requirements ok, back to reality... since m >>= f = do x <- m f x remove monad type feed value into f gives new monad type the first two rules becomes do y <- return x (equivalent to f x) f y do x <- m (equivalent to m) return x Third rule is implicit in the do construct do is associative Simplest example: m >>= f = f m The identity monad return = id >@>becomes forward composition of functions, >.> represents trivial state no action performed value immediately returned 2

Two standard functions mapf :: Monad m => (a -> b) -> m a -> m b joinm :: Monad m => m (m a) -> m a mapf f m = do x <- m return (f x) joinm m = do x <- m x tree data type data Tree a = Nil Node a (Tree a) (Tree a) compute sum of integers in tree direct recursive solution: stree :: Tree Int -> Int stree Nil = 0 stree (Node n t1 t2) = n + stree t1 + stree t2 sumtree :: Tree Int -> St Int Stis a monad (not defined yet) sumtree Nil = return 0 sumtree (Node n t1 t2) = do num <- return n s1 <- sumtree t1 s2 <- sumtree t2 return (num + s1 + s2) No special side-effects use identity monad data St a = Id a instance Monad Id where return = Id (>>=) (Id x) f = f x thus, we could say sumtree :: Tree Int -> Id Int we would like the type Tree Int -> Int define extract :: Id a -> a extract (Id x) = x now extract. sumtree What s the point? Functional style would have been easier There s no point we don t use any side-effects Isn t side-effects a bad idea in general? yes, but it can be unavoidable this is a more controlled environment Another example... 3

State in monadic computation consider assigning integer to all values in tree same integer for same value we would like numtree :: Eq a => Tree a -> Tree Int how to do? we define numbertree :: Eq a => Tree a -> State a (Tree Int) monad type State adependent on tree type Tree a» monad for this type of tree State in monadic computation numbertree could look like this numbertree Nil = return Nil numbertree (Node x t1 t2) = do num <- numbernode x nt1 <- numbertree t1 nt2 <- numbertree t2 return (Node num nt1 nt2) A type for a table type Table a = [a] A state containing the table data State a b = State (Table a -> (Table a, b)) interpretation: takes a Table returns a b updates Table as side-effect The state monad instance Monad (State a) where return x = State (\tab -> (tab,x)) leave state unchanged, just return value (State st) >>= f = State (\tab -> let (newtab,y) = st tab (State trans) = f y in trans newtab) Just a reminder: data State a b = State (Table a -> (Table a, b)) (>>=) :: m a -> (a -> m b) -> m b interpretation of >>= we want to sequence st with f perform st(pass tab to st) new state: newtab(after st) and value y pass yto f get new State pass newtabto this new state function State in monadic computation numbernode :: Eq a => a -> State a Int numbernode x = State (nnode x) nnode :: Eq a => a -> Table a -> (Table a, Int) nnode x table elem x table = (table, lookup x table) otherwise = (table++[x], length table) if x in table new int retrive integer from table otherwise add new integer to table 4

State in monadic computation lookup :: Eq a => a -> Table a -> Int extractst :: State a b -> b extractst (State st) = snd (st []) function st applied to initial state [] returns pair take second part Int we're looking for numtree = extractst. numbertree Monadic style programming Incorporates imperative (monadic) style into pure functional languages Without poisoning other code theory complex fairly easy to use Next lecture functional style in imperative languages 5