Is there a way to generate instances from the GHCi REPL?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Is there a way to generate instances from the GHCi REPL?

Christopher Done-2

Good evening,

Is there a way with TH to generate instances for a class in GHCi? I want to generate instances for a serialization class.

I tried to write some TH to do it, but $(foo) in GHCi’s REPL says that it can only be Q Exp, not Q [Dec]. Sadness.

I found qAddTopDecls. I tried to add the class declarations via $(foo >>= qAddTopDecls; stringE "OK!") and it says “Only function, value, and foreign import declarations may be added with addTopDecl". Woe.

I could create a file and then load in that file, but loading modules loses all GHCi's state, which would defeat the purpose of using a REPL in the first place.

Any hope?

Ciao!


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to generate instances from the GHCi REPL?

Artyom Kazak-2

If your instance-generating function is defined in an external module, you can use it in GHCi like this:

-- Enable TH
> :set -XTemplateHaskell 

-- Import modules (e.g. Aeson)
> import Data.Aeson.TH
> import Data.Aeson

-- Define the type for which I'm going to generate instances
> data X = X {x :: Int}

-- Derive the instance
Prelude Data.Aeson.TH> data Dummy; deriveJSON defaultOptions ''X

-- Check that it works
> toJSON (X 1)
Object (fromList [("x",Number 1.0)])

(More about the data Dummy trick here: https://www.reddit.com/r/haskelltil/comments/3ghacj/you_can_use_template_haskell_functions_like/.)


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Is there a way to generate instances from the GHCi REPL?

Michael Sloan
In reply to this post by Christopher Done-2
One thing to note is that the qAddTopDecls issue isn't a fundamental issue, just a matter of implementation: https://ghc.haskell.org/trac/ghc/ticket/10853

On Thu, Feb 4, 2016 at 6:56 AM, Christopher Done <[hidden email]> wrote:

Good evening,

Is there a way with TH to generate instances for a class in GHCi? I want to generate instances for a serialization class.

I tried to write some TH to do it, but $(foo) in GHCi’s REPL says that it can only be Q Exp, not Q [Dec]. Sadness.

I found qAddTopDecls. I tried to add the class declarations via $(foo >>= qAddTopDecls; stringE "OK!") and it says “Only function, value, and foreign import declarations may be added with addTopDecl". Woe.

I could create a file and then load in that file, but loading modules loses all GHCi's state, which would defeat the purpose of using a REPL in the first place.

Any hope?

Ciao!


_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/haskell-cafe