有关类型系列实例的信息
简介:
在查看 snoyman 的“持久” 库时,我发现自己想要 ghci 的(或其他工具)帮助解决问题。
ghci 的 :info
似乎对于类型族和数据族的处理不如对于“普通”类型的处理好:
> :info Maybe
data Maybe a = Nothing | Just a -- Defined in Data.Maybe
...
> :info Persist.Key Potato -- "Key Potato" defined in example below
data family Persist.Key val -- Defined in Database.Persist
... (no info on the structure/identity of the actual instance)
人们总是可以在源代码中查找实例,但有时它可能很难找到它,并且它可能隐藏在 template-haskell 生成的代码等中。
代码示例:
{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, TypeFamilies, QuasiQuotes #-}
import qualified Database.Persist as Persist
import Database.Persist.Sqlite as PSqlite
PSqlite.persistSqlite [$persist|
Potato
name String
isTasty Bool
luckyNumber Int
UniqueId name
|]
上面的代码示例中发生的是 Template-Haskell 正在为我们生成代码。除了 QuasiQuotes
之外,上述所有扩展都是必需的,因为生成的代码使用它们。
我通过执行以下操作发现了 Persist.Key Potato
是什么:
-- test.hs:
test = PSqlite.persistSqlite [$persist|
...
-- ghci:
> :l test.hs
> import Language.Haskell.TH
> import Data.List
> runQ test >>= putStrLn . unlines . filter (isInfixOf "Key Potato") . lines . pprint
where newtype Database.Persist.Key Potato = PotatoId Int64
type PotatoId = Database.Persist.Key Potato
问题:
是否有一种更简单的方法可以使用 ghci 或任何其他方式获取有关类型系列和数据系列实例的信息工具?
Intro:
While checking out snoyman's "persistent" library I found myself wanting ghci's (or another tool) assistance in figuring out stuff.
ghci's :info
doesn't seem to work as nicely with type-families and data-families as it does with "plain" types:
> :info Maybe
data Maybe a = Nothing | Just a -- Defined in Data.Maybe
...
> :info Persist.Key Potato -- "Key Potato" defined in example below
data family Persist.Key val -- Defined in Database.Persist
... (no info on the structure/identity of the actual instance)
One can always look for the instance in the source code, but sometimes it could be hard to find it and it may be hidden in template-haskell generated code etc.
Code example:
{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving, MultiParamTypeClasses, TypeFamilies, QuasiQuotes #-}
import qualified Database.Persist as Persist
import Database.Persist.Sqlite as PSqlite
PSqlite.persistSqlite [$persist|
Potato
name String
isTasty Bool
luckyNumber Int
UniqueId name
|]
What's going on in the code example above is that Template-Haskell is generating code for us here. All the extensions above except for QuasiQuotes
are required because the generated code uses them.
I found out what Persist.Key Potato
is by doing:
-- test.hs:
test = PSqlite.persistSqlite [$persist|
...
-- ghci:
> :l test.hs
> import Language.Haskell.TH
> import Data.List
> runQ test >>= putStrLn . unlines . filter (isInfixOf "Key Potato") . lines . pprint
where newtype Database.Persist.Key Potato = PotatoId Int64
type PotatoId = Database.Persist.Key Potato
Question:
Is there an easier way to get information on instances of type families and data families, using ghci or any other tool?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,
-ddump-splices
是否显示 TH 生成的代码?否则,
:browse
确实会为您提供有关数据系列实例的信息,但不会提供有关类型系列的信息。您可能需要提交 ghc 票据 -
:browse
输出看起来很混乱,人们可能期望数据族实例像类实例一样通过:info
报告。Does
-ddump-splices
show you the TH-generated code in this case?Otherwise,
:browse
does give you info about data family instances, though not about type families.You might want to file a ghc ticket - the
:browse
output looks mangled, and one might expect data family instances to be reported like class instances, by:info
.