介绍:
在查看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 或任何其他工具来获取有关类型族和数据族实例的信息?