如何查看为Haskell派生的实例/派生的代码 [英] How to view the generated code for derived instances / deriving in Haskell

查看:83
本文介绍了如何查看为Haskell派生的实例/派生的代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在Haskell中,做到这一点真的很容易:

So, in Haskell, it's really easy to do this:

data Foo = Bar | Baz
    deriving (Read, Show)

很好,但是我希望能够将某些数据作为字符串从Haskell传递到Elm语言.这些语言非常相似,如果我有Haskell Read的实现,则可以轻松地将其手动转换为Elm.

This is great, but I'd like to be able to pass some data as a string from Haskell to the Elm language. The languages are similar enough that, if I had a Haskell implementation of Read, I could easily convert it to Elm by hand.

问题是,当我使用推导时,该函数会自动生成,但实际上看不到它的作用.

The problem is, when I use deriving, the function is automatically generated, but I can't actually see what it does.

我想知道,有没有一种方法可以自动生成用于使用Read and Show进行解析和显示的代码,这样我实际上就可以看到代码本身?

I'm wondering, is there a way to automatically generate the code for parsing and showing using Read and Show, so that I could actually see the code itself?

推荐答案

您可以使用-ddump-deriv GHC选项查看派生实例的代码:

You can use the the -ddump-deriv GHC option to see the code for derived instances:

ghc -ddump-deriv test.hs 
[1 of 1] Compiling Test             ( test.hs, test.o )

==================== Derived instances ====================
Derived instances:
  instance GHC.Show.Show Test.FooBar where
    GHC.Show.showsPrec _ Test.Foo = GHC.Show.showString "Foo"
    GHC.Show.showsPrec _ Test.Bar = GHC.Show.showString "Bar"
    GHC.Show.showList = GHC.Show.showList__ (GHC.Show.showsPrec 0)


Generic representation:

  Generated datatypes for meta-information:

  Representation types:

这篇关于如何查看为Haskell派生的实例/派生的代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆