Skip to content

Commit f75324b

Browse files
Xingye-DujingMatthijsBlomulysses4ever
authored
Remove outdated Error type class and Either Monad constraints (#148)
Signed-off-by: Xingye-Dujing <63134338+Xingye-Dujing@users.noreply.github.com> Co-authored-by: Matthijs <19817960+MatthijsBlom@users.noreply.github.com> Co-authored-by: Artem Pelenitsyn <a.pelenitsyn@gmail.com>
1 parent 9518627 commit f75324b

File tree

1 file changed

+5
-44
lines changed

1 file changed

+5
-44
lines changed

source_md/for-a-few-monads-more.md

Lines changed: 5 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1084,14 +1084,13 @@ Left "out of cheese error" :: Either [Char] b
10841084

10851085
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.
10861086

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`:
10881088

10891089
```{.haskell:hs}
1090-
instance (Error e) => Monad (Either e) where
1090+
instance Monad (Either e) where
10911091
return x = Right x
10921092
Right x >>= f = f x
10931093
Left err >>= f = Left err
1094-
fail msg = Left (strMsg msg)
10951094
```
10961095

10971096
`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`.
11021101
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.
11031102
In the case of an error, the `Left` value is kept, along with its contents, which describe the failure.
11041103

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.
11201105

11211106
Anyway, here are a few examples of usage:
11221107

@@ -1130,33 +1115,9 @@ Left "no way!"
11301115
When we use `>>=` to feed a `Left` value to a function, the function is ignored and an identical `Left` value is returned.
11311116
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!
11321117

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.
11581119
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.
11601121

11611122
## Some useful monadic functions {#useful-monadic-functions}
11621123

0 commit comments

Comments
 (0)