Reputation: 5949
To help with debugging and writing programs in Haskell, I am thinking about ability of Haskell programs to output types of variables as part of the program. For example, I have following code:
listHEADFiles :: ReaderT LgRepo IO ()
listHEADFiles = do
ref <- resolveReference $ T.pack "HEAD"
case ref of
Nothing -> fail "Could not resolve reference named 'HEAD'"
Just reference -> do
obj <- lookupObject reference
case obj of
CommitObj commit -> do
objects <- listAllObjects Nothing (commitOid commit)
for_ objects (\case
TreeObjOid toOid -> do
tree <- lookupTree toOid
treeEntries <- sourceTreeEntries tree
entries <- lift $ treeEntries
outputTypeOf entries
)
_ -> fail "'HEAD' is not a commit object"
I want to output type of variable entries
because I fail to understand what exactly happens after I lift the value. I can go to documentation, but it always perplexes me to calculate it by hand. I would like to know for sure what type it is when my program is executed. In other words, I want functionality of :t
in ghci
as a part of my program. Is it possible?
Upvotes: 3
Views: 55
Reputation: 92117
You don't really want your program to output a type: you want the compiler to output a type when it compiles your program. The feature you're looking for is Partial type signatures. The idea is you put an incomplete signature on an expression, and you get out a compiler "error" telling you how to fill in the blanks. If you have no idea at all of the type, an acceptable incomplete signature would be just _
:
(entries :: _) <- lift $ treeEntries
Upvotes: 4