SHOW:
|
|
- or go back to the newest paste.
1 | -- Environment to map values to var names in intepreter | |
2 | Type Env = [(String, Integer)] | |
3 | ||
4 | ||
5 | -- Some different type of error (types doesn't matter) | |
6 | data SomeError = A String | B String | |
7 | - | data SomeError = A String' | B String'' |
7 | + | |
8 | -- Test monad : | |
9 | -- The test monad does some computation in input Env. | |
10 | -- Returns either SomeError, or a result of type a. | |
11 | -- Side effect in form of output is added to [String] | |
12 | newtype Test a = Test {runTest :: Env -> (Either SomeError a, [String]) } | |
13 | ||
14 | instance Monad Test where | |
15 | return a = Test $ const (Right a, []) | |
16 | m >>= f = Test $ \e -> case runTest m e of | |
17 | (Left a, x) -> (Left a, x) | |
18 | (Right a, s) -> case runTest (f a) e of | |
19 | (Left a,x) -> (Left a,x) | |
20 | (Right a', s') -> (Right a', s++s') | |
21 | ||
22 | -- Monadic doesnt work | |
23 | -- m >>= f = Test $ \e -> | |
24 | -- runTest m e >>= \t -> | |
25 | -- runTest (f (first t)) >>= \t' -> | |
26 | -- return t' |