Ремонт принтеров, сканнеров, факсов и остальной офисной техники


назад Оглавление вперед




[23]

св а: =з

It is clear that the worker machines contain all of the functionality needed to produce chopsticks. What is missing is the specification of the trays, loader, and combiner machines that collectively make up the Tray monad. Our trays should either be empty or contain a single item. Our loader machine would simply take an item and place it in a tray on the conveyor belt. The combiner machine would take each input tray and pass along empty trays while feeding the contents of non-empty trays to its worker machine. In Haskell, we would define the Tray monad as:

-trays are either empty or contain a single item data Tray x = Empty Contains x

-Tray is a monad instance Monad Tray where

Empty>>= = Empty

(Contains x) >>= worker = worker x return= Contains

fail = Empty

You may recognize the Tray monad as a disguised version of the Maybe monad that is a standard part of Haskell 98 library.

All that remains is to sequence the worker machines together using the loader and combiner machines to make a complete assembly line, as shown in Figure A-2.

Figure A-2. A complete assembly line for making chopsticks using a monadic approach.

In Haskell, the sequencing can be done using the standard monadic functions:

assemblyLine :: Wood -> Tray Wrapped Chopsticks

assemblyLine w = (return w) >>= makeChopsticks >>= polishChopsticks >>= wrapChopsticks

or using the built in Haskell "do" notation for monads:

assemblyLine :: Wood -> Tray Wrapped Chopsticks assemblyLine w = do c <- makeChopsticks w

c <- polishChopsticks c

c <- wrapChopsticks c

ФП 02005-03 01

Лист 75

Копиоова Фоомат


£ со

return c

So far, you have seen how monads are like a framework for building assembly lines, but you probably havent been overawed by their utility. To see why we might want to build our assembly line using the monadic approach, consider what would happen if we wanted to change the manufacturing process.

Right now, when a worker machine malfunctions, it uses the fail routine to produce an empty tray. The fail routine takes an argument describing the failure, but our Tray type ignores this and simply produces an empty tray. This empty tray travels down the assembly line and the combiner machines allow it to bypass the remaining worker machines. It eventually reaches the end of the assembly line, where it is brought to you, the quality control engineer. It is your job to figure out which machine failed, but all you have to go on is an empty tray.

You realize that your job would be much easier if you took advantage of the failure messages that are currently ignored by the fail routine in your Tray monad. Because your assembly line is organized around a monadic approach, it is easy for you to add this functionality to your assembly line without changing your worker machines.

To make the change, you simply create a new tray type that can never be empty. It will always either contain an item or it will contain a failure report describing the exact reason there is no item in the tray.

- tray2s either contain a single item or contain a failure report data Tray2 x = Contains x Failed String

- Tray2 is a monad instance Monad Tray2 where

(Failed reason) >>=

(Contains x) >>= worker

fail reason

Failed reason worker x Contains Failed reason

You may recognize the Tray2 monad as a disguised version of the Error monad that is a standard part of the Haskell 98 libraries.

Replacing the Tray monad with the Tray2 monad instantly upgrades your assembly line. Now when a failure occurs, the tray that is brought to the quality control engineer contains a failure report detailing the exact cause of the failure!

ФП 02005-03 01

Лист 76

Копиоова Формат


Приложение B

(обязательное)

HASKELL CODE EXAMPLES

This appendix contains a list of all of the code examples supplied with the tutorial.

Example 1

This example is discussed in the section: Meet the monads.

The example code introduces the monad concept without using Haskell typeclasses. It shows how a monadic combinator can be used to simplify the construction of computations from sequences of computations which may not return a result.

Example 2

This example is discussed in the section: Doing it with class.

The example code builds on the first example, and shows how do-notation can be used with an instance of the Monad class (in this case, Maybe is the monad used).

Example 3

This example is discussed in the section: Monad support in Haskell.

The example code builds on the first two examples, and shows a somewhat atypical - but very powerful - use of the foldM function outside of a do-block.

Example 4

This example is discussed in the section: Monad support in Haskell.

The example code shows a more typical use of the foldM function within a do-block. It combines dictionary values read from different files into a single dictionary using the foldM function within the IO monad.

Example 5

This example is discussed in the section: Monad support in Haskell.

ФП 02005-03 01

№ докум.

Копиоова Фоомат



[стр.Начало] [стр.1] [стр.2] [стр.3] [стр.4] [стр.5] [стр.6] [стр.7] [стр.8] [стр.9] [стр.10] [стр.11] [стр.12] [стр.13] [стр.14] [стр.15] [стр.16] [стр.17] [стр.18] [стр.19] [стр.20] [стр.21] [стр.22] [стр.23] [стр.24] [стр.25]