开发者

Info on type family instances

开发者 https://www.devze.com 2023-01-02 20:09 出处:网络
Intro: While checking out snoyman\'s \"persistent\" library I found myself wanting ghci\'s (or another tool) assistance in figuring out stuff.

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消