关于类型族实例的信息

时间:2010-06-10 14:11:14

标签: haskell ghci type-families

说明:

在查看snoyman's "persistent"库时,我发现自己想要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或任何其他工具获取类型系列和数据系列实例的信息?

1 个答案:

答案 0 :(得分:5)

在这种情况下-ddump-splices是否会显示TH生成的代码?

否则,:browse会为您提供有关数据系列实例的信息,但不会提供有关类型系列的信息。

您可能希望提交ghc ticket - :browse输出看起来已损坏,并且可能希望数据系列实例按类:info报告为类实例。

相关问题