Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I add an instance declaration in GHCi

I was messing around with HashMap and tried to use a Data.Bson.ObjectId as a key. I, of course, discovered that there is not a Hashable instance for that structure. That's ok, because writing one is trivial.1

instance Hashable ObjectId where hash (Oid x y) = hash (x,y)

I typed that line into GHCi and was told "parse error on input `instance'". This actually makes sense as the GHCi prompt operates as if the lines were being typed into a do block in the IO monad and an instance can not be defined in this context.

My question then, is there a way to define a new instance within GHCi?


1 Why this instance is not provided by the library is another matter. I would believe the answer was to limit dependencies except that the bson package already depends on everything under the sun.

like image 735
John F. Miller Avatar asked Dec 16 '11 18:12

John F. Miller


People also ask

What is an instance in Haskell?

An instance of a class is an individual object which belongs to that class. In Haskell, the class system is (roughly speaking) a way to group similar types. (This is the reason we call them "type classes"). An instance of a class is an individual type which belongs to that class.

How do I load a Haskell file into GHCi?

Open a command window and navigate to the directory where you want to keep your Haskell source files. Run Haskell by typing ghci or ghci MyFile. hs. (The "i" in "GHCi" stands for "interactive", as opposed to compiling and producing an executable file.)

How do you define a function in GHCi?

Simply type a let followed by a newline: let ⏎. Then fac 0 = 1 ⏎. Then fac n = n * fac (n-1) ⏎ ⏎ and you're done!

What does GHCi mean in Haskell?

Introduction. GHCi is GHC's interactive environment, in which Haskell expressions can be interactively evaluated and programs can be interpreted.


Video Answer


1 Answers

The good news: Yes, there is a way to define a new instance within GHCi.

The bad news: At the moment, the first step in doing so is "install a development snapshot of GHC".

This has been an obvious bit of missing functionality in GHCi for quite a while. There was no inherent reason for it to be absent, but I assume it was somewhat difficult to implement and so it got set aside.

However, it seems that as of version 7.4.1, it's now available:

At the GHCi prompt you can also enter any top-level Haskell declaration, including data, type, newtype, class, instance, deriving, and foreign declarations. For example:

Prelude> data T = A | B | C deriving (Eq, Ord, Show, Enum)
Prelude> [A ..]
[A,B,C]
Prelude> :i T
data T = A | B | C      -- Defined at <interactive>:2:6
instance Enum T -- Defined at <interactive>:2:45
instance Eq T -- Defined at <interactive>:2:30
instance Ord T -- Defined at <interactive>:2:34
instance Show T -- Defined at <interactive>:2:39

Whether you think having that right now worth the hassle of installing a non-release version of GHC is up to you.

like image 193
C. A. McCann Avatar answered Nov 10 '22 17:11

C. A. McCann