• Complain

Zimmer - From Simple IO to Monad Transformers

Here you can read online Zimmer - From Simple IO to Monad Transformers full text of the book (entire story) in english for free. Download pdf and epub, get meaning, cover and reviews about this ebook. year: 2014, publisher: Smashwords Edition, genre: Computer. Description of the work, (preface) as well as reviews are available. Best literature library LitArk.com created for fans of good reading and offers a wide selection of genres:

Romance novel Science fiction Adventure Detective Science History Home and family Prose Art Politics Computer Non-fiction Religion Business Children Humor

Choose a favorite category and find really read worthwhile books. Enjoy immersion in the world of imagination, feel the emotions of the characters or learn something new for yourself, make an fascinating discovery.

Zimmer From Simple IO to Monad Transformers
  • Book:
    From Simple IO to Monad Transformers
  • Author:
  • Publisher:
    Smashwords Edition
  • Genre:
  • Year:
    2014
  • Rating:
    4 / 5
  • Favourites:
    Add to favourites
  • Your mark:
    • 80
    • 1
    • 2
    • 3
    • 4
    • 5

From Simple IO to Monad Transformers: summary, description and annotation

We offer to read an annotation, description, summary or preface (depends on what the author of the book "From Simple IO to Monad Transformers" wrote himself). If you haven't found the necessary information about the book — write in the comments, we will try to find it.

From Simple IO to Monad Transformers is written for you if you know some Haskell, have seen a description or example of Haskells monads, and are not yet comfortable with the concept. You are also expected to have enough math backgroundto understand that function composition makes up some kind of algebraic system.This ebooks objective is to help you develop an intuitive notion of monad that is accurate enough to be useful. You may have found this difficult to achieve because the concept is abstract and explanations are often aimed at the mathematically sophisticated. When they are not, they tend to be of the See how to do this! Wasnt that easy? variety.Neither approach works very well. What you need is help understanding how abstractions organize details. Contrary to popular opinion, we cannot create abstractions by ignoring details. What abstractions do is control the time and place for such thinking. Edsger Dijkstra once reacted to the top-down programming movement-which he helped start-by writing that he himself did not create programs purely from the top down. He merely organized them that way for clarity and correctness.Here the author discusses monads by looking at them from both the outside in and the inside out. The focus is on building understanding. Functors and applicative types are omitted. Those concepts can be stepping stones on the way to understanding monads but they are unnecessary with shorter approach I have chosen to use.In prioritizing clearer explanations over content the author has also omitted almost all of the monads included with Haskell. You will find plenty of documentation online for those. The monads the author has chosen to present will give you a mental model for makingsense of that documentation.Most sections have one worked exercise. It is OK if you cannot work it. Just pausing to think about it will give you a better understanding than merely reading through an example would. Read more...

Zimmer: author's other books


Who wrote From Simple IO to Monad Transformers? Find out the surname, the name of the author of the book and a list of all author's works by series.

From Simple IO to Monad Transformers — read online for free the complete book (whole text) full work

Below is the text of the book, divided by pages. System saving the place of the last page read, allows you to conveniently read the book "From Simple IO to Monad Transformers" online for free, without having to search again every time where you left off. Put a bookmark, and you can go to the page where you finished reading at any time.

Light

Font size:

Reset

Interval:

Bookmark:

Make

Consider this example which supposes a function readALine that returns the next line ofinput entered into the console.

x = readALine
print x
y = readALine
print y

If there were referential transparency we could conclude that x equals y . That isn't necessarily true because there is a sideeffect to evaluating readALine . Something changes so that adifferent line is read each time readALine is evaluated. Inthe language of functional programming readALine is calledimpure.

In all three examples we have to think what values our symbols andexpressions represent. In the examples with f , there arecontextual rules to help us. These rules do not vary from programto program. After we become comfortable with them, we don't have tothink about which values of x appear where. The example with readALine is different. In such examples we must always thinkabout how values change as control flows around our source code.

Not having to think consciously about how values change is thereason referential transparency makes a computer program easier toread. But there is a price because referential transparency alsomakes a program more difficult to apply to the real world. Thedifficulty in accommodating a readALine function is an exampleof this.

Thus functional programming provides us with a dilemma. On one handwe like the simplicity and clarity of a purely functional program.On the other we do not like the complications that arise when wewant to do IO or play with other side effects.

This monograph is about Haskell's way of dealing with that dilemma.

II. Functional Programming and the Real World

To understand how a functional program can handle side effects, think of a side effect as changing something in the world outside of theprogram's data space. Imagine that the world flickersfrom one reality to another as things change. A program which changes theworld then is something like this

my_prog old_world --> new_world

Instead of changing the world it makes a new one, flickering fromchange to change on the way.

A Haskell programis a world remaking function whose component parts are worldremaking functions. Here is an example.

be_nice =
putStrLn "Hello World"
say_hello =
putStrLn "Nice to meet you!"
main = say_hello >> be_nice

The functions be_nice , say_hello , and main can allbe seen as having this type

World -> World

The function (>>) combines say_hello and be_nice tocreate main . We can view its type signature as

(>>) ::
(World -> World) ->
(World -> World) ->
(World -> World)

Think of (>>) as then because we are talking aboutone world changing action after another. Leaving aside, forthe moment, precisely how (>>) works, we can understandhow to get sequentiality within a functional paradigm by imaginingthat our program has been defined this way

be_nice world =
(putStrLn "Hello World") world
say_hello world =
(putStrLn "Nice to meet you!") world
main world = say_hello(be_nice world)

Only we cannot write it that way because Haskellabstracts the concept of world away from the programmer.

A Haskell programmer is more concerned with creating a program thanwith remaking worlds. In fact, the world is full of things theHaskell programmer ought not be playing with. So it is better to make (>>) apply to some simple data type than to make itrefer to world remaking functions. Haskell provides a parameterizedtype, IO a for this purpose. As an examples

putStrLn "hi" :: IO ()
putStrLn :: String -> IO ()

The type for (>>) is

(>>) :: IO a -> IO b -> IO b

This is much simpler than the version above which uses world remakingfunctions. Moreover this is Haskell.

You may have noticed that IO needs a parameter before itbecomes a real type. Such a thing is usually called aparameterized type. I prefer to think of it as a meta type. Iwill discuss the purpose of this parameter in .

Another way to think of IO is that it is a typeconstructor. Give it an argument and it constructs a type.In sections you will see a use for monad type constructors that take more than one parameter.

The IO meta type is known as a monad. The concept of monad is useful because itemphasizes certain similarities between otherwise different parts ofour programs. These similarities give us a framework in which tounderstand new things. Once you get used to the framework, it helpswith the understanding.

One way to think of a monad m is to see it as a workshop inwhich we can create and work with objects of type m a .

All monadic workshops m have a (>>) function. Itsgeneral type is

(>>) :: Monad m =>
m a -> m b -> m b

and it is often called a combinator. The type signaturefor (>>) particularizes to

(>>) :: IO a -> IO b -> IO b

in the IO monad.

I use the term object for anything a Haskell program canmanipulate. This includes functions. Objects whose type is ofthe form m a where m is a monad I will call monadicobjects.

Although the syntax requirements for (>>) are the same in all workshops, thebehavior of (>>) differs. One way to think of this would beto imagine that each workshop exists in its own universe with itsown physical laws. This of course would imply that the (>>) you see in one workshop wouldn't necessarily behave the same as inanother. It does not imply that the physical laws within any oneworkshop are completely arbitrary. As you read on, you will seethat the similarities between one monad and another go deeperthan mere syntax.

Each workshop has its own tools for manipulating its objects. The (>>) combinator is only one of four that must be present inall workshops.

Question What will the following program do?

main =
say_hello >> be_nice >> main

Answer The function main will write the following on the console

Hello World
Nice to meet you!
Hello World
Nice to meet you!
...

and so on until the user stops it with ctl-c .

III. Monads and the (>>=) Combinator

We have seen two functions, putStrLn and (>>) , belonging to the IO monad. Of these,only (>>) is required of all monads. Both putStrLn and its coworker getLine help give the IO monadits unique capabilities. This section introduces a second functionrequired of all monads and also provides a rather concrete explanation ofthe parameter a in IO a .

Suppose we want to echo user input. We would want to use the getLine function which obtains a line of user input from theconsole. But we cannot write

putStrLn getLine

because the type signatures don't match; getLine produces an IO String and putStrLn requires a String . The typesignatures don't match because of Haskell's way of dealing with thepotential impurity of getLine . Haskell has getLine returna monadic object instead of a string.

Because getLine represents a monadic object it is opaque. That means the string it obtains cannot beseen. The potential impurity of getLine is squashed.The string that getLine obtains is stillthere but unavailable in any context that would violate referentialtransparency. Said bluntly, code that invokes getLine cannot tellthe difference between different evaluations of getLine .

This situation is something like riding a bicycle and having a carturn left immediately in front of you. Your immediate concern isavoiding the car. If the car hits you, the police will be concernedwith finding the driver. When you deal with a monadic object, it is similarly opaque.But there are ways itsdoor can be opened and then you are dealing withsomething on the interior rather than the object as a whole.

I will call the string obtained by an execution of getLine an

Next page
Light

Font size:

Reset

Interval:

Bookmark:

Make

Similar books «From Simple IO to Monad Transformers»

Look at similar books to From Simple IO to Monad Transformers. We have selected literature similar in name and meaning in the hope of providing readers with more options to find new, interesting, not yet read works.


Reviews about «From Simple IO to Monad Transformers»

Discussion, reviews of the book From Simple IO to Monad Transformers and just readers' own opinions. Leave your comments, write what you think about the work, its meaning or the main characters. Specify what exactly you liked and what you didn't like, and why you think so.