4

介绍:

在查看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)

人们总是可以在源代码中查找实例,但有时可能很难找到它,它可能隐藏在模板-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 或任何其他工具来获取有关类型族和数据族实例的信息?

4

1 回答 1

5

在这种情况下,是否-ddump-splices向您展示了 TH 生成的代码?

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

您可能想要提交ghc 票证-:browse输出看起来很混乱,并且人们可能希望数据族实例像类实例一样被报告,由:info.

于 2010-06-10T17:00:28.303 回答