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


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




[22]

liftListToNDS[a] -> NDS a

liftListToNDS = lift . lift

-- perform a series of computations in the combined monad, lifting computations from other -- monads as necessary. main :: IO ()

main = do mapM print $ runWriterT $ ("evalStateT" 0) $ do x <- lift $ getLogLength $ logString "hello"

addDigits x x <- lift $

logEach [1,3,5]

lift $ logVal x liftListToNDS $

getDigits 287

Once you fully understand how the various lifts in the example work and how lifting promotes code reuse, you are ready for real-world monadic programming. All that is left to do is to hone your skills writing real software. Happy hacking!

3.8. Continuing Exploration

This brings us to the end of this tutorial. If you want to continue learning about the mathematical foundations of monads, there are numerous category theory resources on the internet. For more examples of monads and their applications in the real world, you might want to explore the design of the Parsec monadic parser combinator library and/or the QuickCheck testing tool. You may also be interested in arrows, which are similar to monads but more general.

If you discover any errors - no matter how small - in this document, or if you have suggestions for how it can be improved, please write to the author at jnewbern@yahoo.com.

ФП 02005-03 01

Лист 72

№ докум.

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


Приложение A

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

A PHYSICAL ANALOGY FOR MONADS

Because monads are such abstract entities, it is sometimes useful to think about a physical system that is analogous to a monad instead of thinking about monads directly. In this way, we can use our physical intuition and experiences to gain insights that we can relate back to the abstract world of computational monads.

The particular physical analogy developed here is that of a mechanized assembly line. It is not a perfect fit for monads - especially with some of the higher-order aspects of monadic computation - but I believe it could be helpful to gain the initial understanding of how a monad works.

Begin by thinking about a Haskell program as a conveyor belt. Input goes on end of the conveyor belt and is carried to a succession of work areas. At each work area, some operation is performed on the item on the conveyor belt and the result is carried by the conveyor belt to the next work area. Finally, the conveyor belt carries the final product to the end of the assembly line to be output.

In this assembly line model, our physical monad is a system of machines that controls how successive work areas on the assembly line combine their functionality to create the overall product.

Our monad consists of three parts:

1)Trays that hold work products as they move along the conveyor belt.

2)Loader machines that can put any object into a tray.

3)Combiner machines that can take a tray with an object and produce a tray with a new object. These combiner machines are attached to worker machines that actualy produce the new objects.

We use the monad by setting up our assembly line as a loader machine which puts materials into trays at the beginning of the assembly line. The conveyor belt then carries these trays to each work area, where a combiner machine takes the tray and may decide based on its contents whether to run them through a worker machine, as shown in Figure A-1.

ФП 02005-03 01

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


£ со

Figure A-1. An assembly line using a monad architecture

The important thing to notice about the monadic assembly line is that it separates out the work of combining the output of the worker machines from the actual work done by the worker machines. Once they are separated, we can vary them independently. So the same combiner machines could be used on an assembly line to make airplanes and an assembly line to make chopsticks. Likewise, the same worker machines could be used with different combiners to alter the way the final product is produced.

Lets take the example of an assembly line to make chopsticks, and see how it is handled in our physical analogy and how me might represent it as a program in Haskell. We will have three worker machines. The first takes small pieces of wood as input and outputs a tray containing a pair of roughly shaped chopsticks. The second takes a pair of roughly shaped chopsticks and outputs a tray containing a pair of smooth, polished chopsticks with the name of the restaurant printed on them. The third takes a pair of polished chopsticks and outputs a tray containing a finished pair of chopsticks in a printed paper wrapper. We could represent this in Haskell as:

-the basic types we are dealing with type Wood = ...

type Chopsticks = ...

data Wrapper x = Wrapper x

-NOTE: the Tray type comes from the Tray monad

-worker function 1: makes roughly shaped chopsticks makeChopsticks :: Wood -> Tray Chopsticks makeChopsticks w = ...

-worker function 2: polishes chopsticks polishChopsticks :: Chopsticks -> Tray Chopsticks polishChopsticks c = ...

-worker function 3: wraps chopsticks

wrapChopsticks :: Chopsticks -> Tray Wrapper Chopsticks wrapChopsticks c = ...

ФП 02005-03 01

Лист 74

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



[стр.Начало] [стр.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]