You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source_md/for-a-few-monads-more.md
+5-44Lines changed: 5 additions & 44 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1084,14 +1084,13 @@ Left "out of cheese error" :: Either [Char] b
1084
1084
1085
1085
This is pretty much just an enhanced `Maybe`, so it makes sense for it to be a monad, because it can also be viewed as a value with an added context of possible failure, only now there's a value attached when there's an error as well.
1086
1086
1087
-
Its `Monad` instance is similar to that of `Maybe` and it can be found in `Control.Monad.Error`:
1087
+
Its `Monad` instance is similar to that of `Maybe`:
1088
1088
1089
1089
```{.haskell:hs}
1090
-
instance (Error e) => Monad (Either e) where
1090
+
instance Monad (Either e) where
1091
1091
return x = Right x
1092
1092
Right x >>= f = f x
1093
1093
Left err >>= f = Left err
1094
-
fail msg = Left (strMsg msg)
1095
1094
```
1096
1095
1097
1096
`return`, as always, takes a value and puts it in a default minimal context.
@@ -1102,21 +1101,7 @@ The `>>=` examines two possible cases: a `Left` and a `Right`.
1102
1101
In the case of a `Right`, the function `f` is applied to the value inside it, similar to how in the case of a `Just`, the function is just applied to its contents.
1103
1102
In the case of an error, the `Left` value is kept, along with its contents, which describe the failure.
1104
1103
1105
-
The `Monad` instance for `Either e` makes an additional requirement, and that is that the type of the value contained in a `Left`, the one that's indexed by the `e` type parameter, has to be an instance of the `Error` type class.
1106
-
The `Error` type class is for types whose values can act like error messages.
1107
-
It defines the `strMsg` function, which takes an error in the form of a string and returns such a value.
1108
-
A good example of an `Error` instance is, well, the `String` type!
1109
-
In the case of `String`, the `strMsg` function just returns the string that it got:
1110
-
1111
-
```{.haskell:hs}
1112
-
ghci> :t strMsg
1113
-
strMsg :: (Error a) => String -> a
1114
-
ghci> strMsg "boom!" :: String
1115
-
"boom!"
1116
-
```
1117
-
1118
-
But since we usually use `String` to describe the error when using `Either`, we don't have to worry about this too much.
1119
-
When a pattern match fails in `do` notation, a `Left` value is used to signify this failure.
1104
+
Note that unlike `Maybe`, pattern match failures in `do` notation will result in a runtime exception, not a `Left` value.
1120
1105
1121
1106
Anyway, here are a few examples of usage:
1122
1107
@@ -1130,33 +1115,9 @@ Left "no way!"
1130
1115
When we use `>>=` to feed a `Left` value to a function, the function is ignored and an identical `Left` value is returned.
1131
1116
When we feed a `Right` value to a function, the function gets applied to what's on the inside, but in this case that function produced a `Left` value anyway!
1132
1117
1133
-
When we try to feed a `Right` value to a function that also succeeds, we're tripped up by a peculiar type error!
1134
-
Hmmm.
1135
-
1136
-
```{.haskell:hs}
1137
-
ghci> Right 3 >>= \x -> return (x + 100)
1138
-
1139
-
<interactive>:1:0:
1140
-
Ambiguous type variable `a' in the constraints:
1141
-
`Error a' arising from a use of `it' at <interactive>:1:0-33
1142
-
`Show a' arising from a use of `print' at <interactive>:1:0-33
1143
-
Probable fix: add a type signature that fixes these type variable(s)
1144
-
```
1145
-
1146
-
Haskell says that it doesn't know which type to choose for the `e` part of our `Either e a` typed value, even though we're just printing the `Right` part.
1147
-
This is due to the `Error e` constraint on the `Monad` instance.
1148
-
So if you get type errors like this one when using `Either` as a monad, just add an explicit type signature:
1149
-
1150
-
```{.haskell:hs}
1151
-
ghci> Right 3 >>= \x -> return (x + 100) :: Either String Int
1152
-
Right 103
1153
-
```
1154
-
1155
-
Alright, now it works!
1156
-
1157
-
Other than this little hangup, using this monad is very similar to using `Maybe` as a monad.
1118
+
Using this monad is very similar to using `Maybe` as a monad.
1158
1119
In the previous chapter, we used the monadic aspects of `Maybe` to simulate birds landing on the balancing pole of a tightrope walker.
1159
-
As an exercise, you can rewrite that with the error monad so that when the tightrope walker slips and falls, we remember how many birds were on each side of the pole when he fell.
1120
+
As an exercise, you can rewrite that with `Either` so that when the tightrope walker slips and falls, we remember how many birds were on each side of the pole when he fell.
1160
1121
1161
1122
## Some useful monadic functions {#useful-monadic-functions}
0 commit comments