Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I hide the Monad instance of [] (or [] in general)?

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!

like image 717
Benjamin Kovach Avatar asked Aug 17 '12 19:08

Benjamin Kovach


2 Answers

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.

like image 185
alternative Avatar answered Nov 16 '22 18:11

alternative


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.

like image 20
Ben Millwood Avatar answered Nov 16 '22 19:11

Ben Millwood