How to hide Monad instance from [] (or [] in general)?

I am trying to do some exercises in Typeclassopedia , but it’s hard for me to define my own instance of Monad from [] , because I cannot hide it. I managed to hide Maybe effectively, but when I try to hide [] , I get this error: parse error on input '['

I use this line of code to import:

 import Prelude hiding (Maybe, Just, Nothing, []) 

Changing [] to ([]) also does not resolve this issue.

I am not sure how to do this. Any help would be great! Thanks!

+6
source share
2 answers

You can try -XNoImplicitPrelude , but the easiest way is to define your own List type with semantics equivalent to [] , and implement your own instances for this type.

Hiding instances is not possible, since even import Prelude () will import instances.

+10
source

Essentially, the list syntax is magic and inline. When Haskell was created, lists were considered so versatile and so useful that they deserved special square-bracket syntax to make them easier to use. Therefore, you cannot define your own list type with the same syntax as the built-in [a] , nor can you hide the [] syntax any more than you can hide keywords such as if or where . This does not stop you from defining your own list type by defining conversion functions to and from the type of the built-in list. As others have pointed out, defining list functions for you is not very complicated and quite educational.

Of course, you can also define your own Monad class with an identical signature, and then use it.

+4
source

Source: https://habr.com/ru/post/923143/


All Articles