I'm trying to work through some of the exercises in the Typeclassopedia, but I'm having trouble defining my own Monad instance of []
, because I can't seem to hide it. I was able to hide Maybe
effectively, but when I try to hide []
, I get this error: parse error on input '['
I'm using this line of code to import:
import Prelude hiding (Maybe, Just, Nothing, [])
Changing []
to ([])
doesn't fix this issue, either.
I'm not sure how to do this. Any help would be great! Thanks!
You could try -XNoImplicitPrelude
, but easiest is probably to define your own List
type with semantics equivalent to []
and implement your instances for this type.
Hiding instances is not possible, as even import Prelude ()
would import instances.
Essentially the list syntax is magic and built-in. When Haskell was created, lists were considered so universal and so useful that they deserved special square-bracket syntax to make them extra convenient to use. Hence you cannot define your own list type with the same syntax as the built-in [a]
, and likewise you cannot hide the []
syntax any more than you can hide keywords like if
or where
. That doesn't stop you from defining your own list type, defining conversion functions to and from the inbuilt list type. As others have pointed out, defining list functions for yourself is both not very hard and quite educational.
Of course, you could also define your own Monad
class with an identical signature, and then use that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With