为 GADT 定义您自己的 Typeable 实例
有人可以给我指出一组在 Haskell 中为 GADT 定义 Typeable 或 Typeable1 实例的好示例吗?
或者,有人可以向我展示如何为以下 GADT 定义 Typeable(手动)。
data V a where
Unit :: V ()
Pair :: V a -> V b -> V (a, b)
L :: V a -> V (Either a b)
R :: V b -> V (Either a b)
Fresh :: Int -> V a
或者,指向介绍该想法的论文也会有所帮助。
Can someone point me at a good set of examples for defining Typeable or Typeable1 instances for GADTs in Haskell.
Or, can someone just show my how to define Typeable (manually) for the following GADT.
data V a where
Unit :: V ()
Pair :: V a -> V b -> V (a, b)
L :: V a -> V (Either a b)
R :: V b -> V (Either a b)
Fresh :: Int -> V a
Alternately a pointer to the paper that introduced the idea would also be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来该网站现在已经消失了,但是回溯机仍然有链接到所有原始论文的网站: http://web.archive.org/web/20080622204226/http://www.cs.vu.nl/boilerplate/
无论如何,Typeable 几乎完全是机械的。即使对于 GADT,您也可以通过 DeriveDataTypeable 扩展来派生它。至少当种类是
* -> 时*
,如您的示例所示。我还可以举一个手动提供 Typeable1 实例的示例,但它将在下一版本的 GHC 中被弃用。用于手动创建实例的界面正在发生变化。
NOINLINE
pragma 实际上很重要,因为 mkTyCon 在幕后做了不安全的事情。这就是为什么如果可能的话最好让 GHC 手动派生实例。我的理解是,在 GHC 的未来版本中将要更改的部分是,您应该使用不同的函数
mkTyCon3
,它将包名称、模块名称和类型名称作为单独的参数。这是一个明显的改进,即使使支持多个版本的 GHC 变得更加困难。请参阅:对 Data.Typeable 的更改。以上所有内容确实很旧。
来自 GHC 文档:
您永远不需要甚至被允许使用任何现代版本的 GHC 手动编写
Typeable
实例。事实上,您甚至不需要告诉 GHC 来导出它们 - 它会自动执行此操作。Looks like the website is gone now, but the wayback machine still has the site that links to all the original papers: http://web.archive.org/web/20080622204226/http://www.cs.vu.nl/boilerplate/
In any case, Typeable is almost completely mechanical. You can just derive it, even for GADTs, via the DeriveDataTypeable extension. At least when the kind is
* -> *
, as in your example.I can also give an example of manually providing a Typeable1 instance, but it will be deprecated in the next version of GHC. The interface used for creating instances by hand is changing.
The
NOINLINE
pragma is actually important, since mkTyCon does unsafe stuff under the hood. This is why it's best to just let GHC derive the instance manually, if possible.My understanding is the part that will be changing in future versions of GHC is that you should use a different function,
mkTyCon3
, which takes the package name, module name, and type name as separate arguments. That's a clear improvement, even if makes supporting multiple version of GHC tougher. See: Changes to Data.Typeable.All of the above is really old.
From the GHC docs:
You should never need, or even be allowed, to write a
Typeable
instance by hand with any modern version of GHC. In fact, you don't even need to tell GHC to derive them - it will do that automatically.