yai*_*chu 4 haskell ghci type-families
介绍:
在查看snoyman的"持久性"库时,我发现自己想要ghci(或其他工具)帮助搞清楚.
:info对于类型系列和数据系列,ghci 似乎不像"普通"类型那样好用:
> :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)
Run Code Online (Sandbox Code Playgroud)
人们总是可以在源代码中查找实例,但有时可能很难找到它,它可能隐藏在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
|]
Run Code Online (Sandbox Code Playgroud)
上面的代码示例中发生的是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
Run Code Online (Sandbox Code Playgroud)
题:
有没有更简单的方法来获取类型系列和数据系列的实例信息,使用ghci或任何其他工具?