Haskell Dealing with impurity 30-Nov-18.

Slides:



Advertisements
Similar presentations
THE ELEPHANT And the 6 (billion) blind men A treatise based on an old poem by John Godfrey Saxe ( )
Advertisements

Chapter 16 Communicating With the Outside World. Motivation  In Chapter 3 we learned how to do basic IO, but it was fairly limited.  In this chapter.
A Paradigm - a perspective of way of looking at the world.
The Blind Men and the Elephant
CS 490 Design Patterns. Introduce Yourself Your name Your name What attracted you to this course? What attracted you to this course? Experience with Design.
Blind Men and the Elephant (by John Godfrey Saxe) American poet John Godfrey Saxe ( ) based this poem, "The Blind Men and the Elephant", on a fable.
Science According to the National Academy of Sciences, Science is the use of evidence to construct testable explanations and predictions of natural phenomena,
Modeling Lecture 1. Modeling and Sustainability CE4505 Surface Water Quality Engineering CE4505 – Surface Water Quality Engineering.
Making Movies, Building Community: Community Produced Video As A Tool for Community Development Amy Lake Grace Njeru, PhD Steve Jeanetta, PhD University.
Observation, Perception & Theory Theoretical Framework.
® Sponsored by Towards a Systems Model for Urban Planning: Moving SDI Towards Active Models of Reality 92nd OGC Technical Committee Calgary Canada John.
Supporting Inclusion Through Value Based Practice Best Start Annual Conference January 2006 Presented by Leslie McDiarmid Better Beginnings Better Futures.
THE ROLE OF MODELS IN THE CONVERGENCE OF SCIENTIFIC KNOWLEDGE Linda Weiser Friedman Zicklin School of Business, Baruch College and the Graduate Center.
Banksy – graffiti artist & elephant wrangler “The show is about the elephant in the room, the problems that we don't talk about. The fact that 10 billion.
A poem written by John Godfrey Saxe ( )
The Fifth, who chanced to touch the ear, It was six men of Indostan To learning much inclined, Said: "E'en the blindest man Who went to see the Elephant.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Story of Six Blind Men It was six men of Indostan To learning much inclined Who went to see an animal (Though all of them were blind), That each by observation.
Grab Bag of Interesting Stuff. Topics Higher kinded types Files and handles IOError Arrays.
General Lecture 1. Modeling and Sustainability CE5504 Surface Water Quality Modeling.
0 PROGRAMMING IN HASKELL Chapter 9 - Interactive Programs.
09-10 Biology. Vocabulary Control: Controlled Experiment: Controlled Variable: Hypothesis: Inference: Observation: Manipulated Variable: (aka ________________)
Session 6 The Renaissance/ Reformation and Piestist/Revivalist Eras SF 665: Christian Devotional Classics Evangelical Seminary Spring, 2013 Jo Ann Kunz.
Haskell Chapter 8. Input and Output  What are I/O actions?  How do I/O actions enable us to do I/O?  When are I/O actions actually performed?
Comp150PLD Reading: “Tackling the Awkward Squad,” Sections 1-2Tackling the Awkward Squad “Real World Haskell,” Chapter 7: I/OReal World Haskell Thanks.
© M. Winter COSC 4P41 – Functional Programming Programming with actions Why is I/O an issue? I/O is a kind of side-effect. Example: Suppose there.
World Geography Mrs. Robertson. Starter: 8/26 What goals do you have for your FRESHMAN year of High School? How do you plan on accomplishing these goals?
Planning for learners and learning in the 21 st Century Robert Fitzgerald Faculty of Education University of Canberra ICT in Learning Symposium, Wesley.
0 Odds and Ends in Haskell: Folding, I/O, and Functors Adapted from material by Miran Lipovaca.
It was six men of Indostan, To learning much inclined Who went to see the Elephant (Though all of them were blind), That each by observation.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L13-1 October 26, 2006http:// Monadic Programming October.
Lee CSCE 314 TAMU 1 CSCE 314 Programming Languages Interactive Programs: I/O and Monads Dr. Hyunyoung Lee.
Grab Bag of Interesting Stuff. Higher Order types Type constructors are higher order since they take types as input and return types as output. Some type.
Haskell Chapter 9. More Input and More Output  Files and Streams  Transforming Input  Not covered  brackets  command-line arguments  bytestrings.
The blind men and the elephant. Six men, eager to expand their knowledge, went to see an elephant (despite the fact that they were all blind), so that.
Scientific Processes How we know what we know!. Observations Use senses and tools to study life Descriptions: i.e. behaviors, movements, measurements,
24-Jun-16 Haskell Dealing with impurity. Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side.
Blind Men and the Elephant (by John Godfrey Saxe) American poet John Godfrey Saxe ( ) based this poem, "The Blind Men and the Elephant", on a fable.
It was six men of Indostan To learning much inclined, Who went to see the Elephant~(Though all of them were blind), That each by observation~Might satisfy.
Haskell Chapter 8.
Scientific Method W L K.
the IO Monad Kathleen Fisher cs242
While reading the poem, think about how
Theory of Computation Lecture 4: Programs and Computable Functions II
PROGRAMMING IN HASKELL
Geography & Six Blind Men from India
The Blind Men and the Elephant by John Godfrey Saxe
Ruth Aydt Quincey Koziol The HDF Group
PROGRAMMING IN HASKELL
World Geography Mrs. Robertson.
Haskell Dealing with impurity 30-Nov-18.
Literary Point of view= NARRATOR
World Geography Mrs. Robertson.
The Blind Men and the Elephant by John Godfrey Saxe
Type & Typeclass Syntax in function
Input and Output.
An Analogy with “The Blind Men and the Elephant”
Haskell Dealing with impurity 8-Apr-19.
The Blind Men and the Elephant
PROGRAMMING IN HASKELL
Grab Bag of Interesting Stuff
Programming Languages
Computer Science 312 I/O and Side Effects 1.
Unit 6 -- Point of View Take a sheet of paper. Divide it into four sections.
Theory of Computation Lecture 4: Programs and Computable Functions II
Exceptions 10-May-19.
World Geography Mrs. Robertson.
Haskell Dealing with impurity 29-May-19.
Haskell Dealing with impurity 28-Jun-19.
Presentation transcript:

Haskell Dealing with impurity 30-Nov-18

Purity Haskell is a “pure” functional programming language Functions have no side effects Input/output is a side effect Given the same parameters, a function will always return the same result This doesn’t work for a function that does input There are other needs that can’t be met in a pure fashion Obtaining the date and time Getting a random number Haskell “quarantines” these impure actions so as not to contaminate the rest of the code

Laziness All values in Haskell are “lazy”—they are not computed until they are needed Laziness does not work well for I/O Consider [ print "x" ; print "y" ] If these values are computed only when needed, x might be printed before y y might be printed before x Neither might be printed Similar observations hold for input Most functional languages will temporarily enter an “imperative world” to perform I/O operations This approach works for simple cases but is fraught with problems Haskell enforces a strict separation between the “functional world” and the “imperative world” This separation is done by the use of monads

main Haskell programs usually have a main method The type of main is IO () IO () is an I/O action The () is the unit; it is an empty tuple, of type () and value () The body of the main method is one I/O action When main is executed, the I/O action is performed The do expression groups a series of I/O actions into a single I/O action Think of do as like a compound statement, {...;...;...}, in Java The body of the main function is usually a do expression

main is not a pure function In a typical Haskell program, all I/O is under control of the main function “Impure” functions such as main can call pure functions “Impure” functions can call other impure functions Pure functions cannot call “impure” functions Thus, main, along with perhaps a small coterie of impure helper functions, is in the “imperative world,” but can make use of the “functional world” This turns out to be a good design for programs in other languages, such as Java

getLine and putStrLn getLine reads in a line of text from the user The type of getLine is IO String This is an I/O action that contains, “in quarantine,” a String The <- operator removes a value from quarantine line <- getLine gets the contained String “out of quarantine” and puts it in the (normal, immutable) variable line putStr string displays text to the user putStrLn string displays a complete line of text to the user The type of these functions is IO ()

Bind GHCi> :t getLine getLine :: IO String GHCi> :t putStrLn putStrLn :: String -> IO () putStrLn prints a String, but getLine returns an IO String What we might like to do is take the String out of the IO String, but that is “unsafe”—it brings impurity into the world of pure functions You can think of the IO monad as a kind of “quarantine” What we need is an operator to go into the IO world of getLine, take its String, and pass it to putStrLn, without leaving “quarantine” That operator is “bind”, >>= The type of >>= is: Monad m => m a -> (a -> m b) -> m b do is syntactic sugar for a sequence of bind operations

return return doesn’t mean what it means in any other language! return is a function that quarantines its argument, and returns that argument in an “isolation cell” This “isolation cell” is called a monad Inside do, the operator <- can be used to get a value out of a monad Prelude> :t return "hello" return "hello" :: (Monad m) => m [Char] Prelude> foo <- return "hello" Prelude> foo "hello" One use of return is to provide an “empty value” in a do

More I/O actions putChar takes a character and returns an I/O action that will print it out to the terminal getChar is an I/O action that reads a character from the input print is putStrLn . show

Example program using I/O import Data.Char main = do putStrLn "Type something in: " line <- getLine if null line then return () else do putStrLn $ "You said: " ++ map toUpper line main In Haskell, the if is an expression and must return a value; hence it requires both a then and an else The do requires a sequence of I/O actions return () is an IO action and returns a value, so it’s okay to use

Why purity matters Pure functions are like immutable values--safe and reliable No dependency on state, so... A function that works once will work always Functions may be computed in any order Lazy evaluation becomes possible Laziness and side effects are incompatible Suppose “print” were a function Consider a list of print functions... ...when are the print functions evaluated? Input changes the state of the computation But pure functions have no dependency on state, so computations cannot depend on state

The Blind Men and the Elephant It was six men of Indostan To learning much inclined, Who went to see the Elephant (Though all of them were blind), That each by observation Might satisfy his mind. The First approached the Elephant, And happening to fall Against his broad and sturdy side, At once began to bawl: "God bless me! but the Elephant Is very like a WALL!" The Second, feeling of the tusk, Cried, "Ho, what have we here, So very round and smooth and sharp? To me 'tis mighty clear This wonder of an Elephant Is very like a SPEAR!" The Third approached the animal, And happening to take The squirming trunk within his hands, Thus boldly up and spake: "I see," quoth he, "the Elephant Is very like a SNAKE!"

by John Godfrey Saxe (1816-1887) The Fourth reached out an eager hand, And felt about the knee "What most this wondrous beast is like Is mighty plain," quoth he: "'Tis clear enough the Elephant Is very like a TREE!" The Fifth, who chanced to touch the ear, Said: "E'en the blindest man Can tell what this resembles most; Deny the fact who can, This marvel of an Elephant Is very like a FAN!" The Sixth no sooner had begun About the beast to grope, Than seizing on the swinging tail That fell within his scope, "I see," quoth he, "the Elephant Is very like a ROPE!" And so these men of Indostan Disputed loud and long, Each in his own opinion Exceeding stiff and strong, Though each was partly in the right, And all were in the wrong!

So, what’s a monad?

How to learn about monads Be very confused Read lots of explanations on the web by people who have finally understood how simple monads really are Remain confused Program in Haskell for a while See the light Realize that all the explanations you had read were wrong Write a tutorial on the web explaining how monads are actually very simple

Dealing with state To have state and pure functions, the old state of the world must be passed in as a parameter, and the new state of the world returned as a result A monad is a way of automatically maintaining state IO a can be thought of as a function whose type is World -> (a, World)

The “bind” operator, >>= We will want to take the “state of the world” resulting from one function, and pass it into the next function Suppose we want to read a character and then print it Types: getChar :: IO Char putChar :: Char -> IO () The result of getChar isn’t something that can be given to putChar The IO Char “contains” a Char that has to be extracted to be given to putChar (>>=) :: IO a -> (a -> IO b) -> IO b Hence, Prelude> getChar >>= putChar a aPrelude>

The “then” operator, >> The second argument to >>= is a function (such as putChar) This is what we need for passing along a result It is convenient to have another function that doesn’t demand a function as its second argument The “then” operator simply throws away its contents (>>) :: IO a -> IO b -> IO b Prelude> putChar 'a' >> putChar 'b' >> putChar '\n' ab Prelude>

The return function Finally, it is helpful to be able to create a monad container for arbitrary values return :: a -> IO a The action (return v) is an action that does no I/O, and immediately returns v without having any side effects getTwoChars :: IO (Char,Char) getTwoChars = getChar >>= \ c1 -> getChar >>= \ c2 -> return (c1,c2)

do notation From the last slide: That’s pretty hard to read getTwoChars :: IO (Char,Char) getTwoChars = getChar >>= \ c1 -> getChar >>= \ c2 -> return (c1,c2) That’s pretty hard to read The do provides “syntactic sugar” get2Chars :: IO (Char,Char) get2Chars = do c1 <- getChar c2 <- getChar return (c1,c2) The do also allows the let form (but without in)

Building control structures An infinite loop: forever :: IO () -> IO () forever a = a >> forever a Repeating a fixed number of times: repeatN :: Int -> IO a -> IO () repeatN 0 a = return () repeatN n a = a >> repeatN (n-1) a A “for loop” for I/O actions: for :: [a] -> (a -> IO ()) -> IO () for [] fa = return () for (n:ns) fa = fa n >> for ns fa printNums = for [1..10] print

Formal definition of a monad A monad consists of three things: A type constructor M A bind operation, (>>=) :: (Monad m) => m a -> (a -> m b) -> m b A return operation, return :: (Monad m) => a -> m a And the operations must obey some simple rules: return x >>= f = f x return just sends its result to the next function m >>= return = m Returning the result of an action is equivalent to just doing the action do {x <- m1; y <- m2; m3} = do {y <- do {x <- m1; m2} m3} >>= is associative

when Earlier, we had this function: main = do putStrLn "Type something in: " line <- getLine if null line then return () else do putStrLn $ "You said: " ++ map toUpper line main The return () seems like an unnecessary annoyance, so let’s get rid of it when :: (Monad m) => Bool -> m () -> m () when True m = m when False m = return () main = do putStrLn "Type something in: " line <- getLine when (not (null line)) $ do putStrLn $ "You said: " ++ map toUpper line main

sequence sequence takes a list of I/O actions and produces a list of results sequence :: [IO a] -> IO [a] main = do rs <- sequence [getLine, getLine, getLine] print rs is equivalent to main = do a <- getLine b <- getLine c <- getLine print [a,b,c]

File I/O A first example: Where: import System.IO main = do handle <- openFile "myFile.txt" ReadMode contents <- hGetContents handle putStr contents hClose handle Where: openFile :: FilePath -> IOMode -> IO Handle type FilePath = String data IOMode = ReadMode | WriteMode | AppendMode | ReadWriteMode getContents :: IO String -- reads from stdIn hGetContents :: Handle -> IO String This is a lazy method hClose :: Handle -> IO ()

More file I/O withFile is like openFile, but it takes care of closing the file afterward The “h” methods work with a specific file, given by the file “handle” hGetLine :: Handle -> IO String hPutStr :: Handle -> String -> IO () hPutStrLn :: Handle -> String -> IO () hGetChar :: Handle -> IO Char readFile and writeFile read and write the entire thing

Doing I/O There are two ways you can have code that does I/O: Run from the REPL, GHCi In the REPL you can call any defined method, including main Write a program with a main method You can interpret the program, or compile and run it The program will run from the main method The main method encapsulates all the I/O “Normal” methods can be called from main to do the work

References The monad explanations are based on a great article, “Tackling the Awkward Squad,” by Simon Peyton Jones http://research.microsoft.com/en-us/um/people/simonpj/papers/marktoberdorf/ The pictures are copied from this article Some examples are taken, with minor revisions, from “Learn You a Haskell for Great Good”

The End