Question about correct GHC-API use for type checking (or zonking, or tidying)

p.k.f.holzenspies at p.k.f.holzenspies at
Wed Sep 11 11:06:18 CEST 2013

Well, my tcLocalBinds seems to have a different type then yours (this is from my copy of 7.6.3 and it's the same in the HEAD that I just synced):

tcLocalBinds :: HsLocalBinds Name -> TcM thing -> TcM (HsLocalBinds TcId, thing)

If I want to get a GblEnv out, I can use getGblEnv, but doing this:

mkId :: Maybe FreeVars -> LHsExpr Name -> Name -> TcM Id
mkId fvs expr@(L l _) nm = do
  ((binds', gbl_env),lie) <- captureConstraints $ tcLocalBinds binds getGblEnv
  setGblEnv gbl_env (tcLookupId nm)
    binds    = HsValBinds $ ValBindsOut [(NonRecursive, unitBag fixbnd)] []
    the_bind = mkTopFunBind (L l nm) [mkMatch [] expr emptyLocalBinds]
    fixbnd   = L l $ maybe the_bind (\vs -> the_bind { bind_fvs = vs }) fvs

fails on the tcLookupId with a GHC internal error complaining that my name is "not in scope during type checking, but it passed the renamer."


From: Simon Peyton-Jones [mailto:simonpj at]
Sent: dinsdag 10 september 2013 14:19
To: Holzenspies, P.K.F. (EWI)
Cc: glasgow-haskell-users at
Subject: RE: Question about correct GHC-API use for type checking (or zonking, or tidying)

What goes wrong if you follow my suggestion below?

tcLocalBinds takes a set of bindings, such as x=e
and returns a GblEnv whose type envt is extended with a binding for x with its generalised type.
This type wil indeed be closed, unless the current environment (in which tcLocalBinds runs) has bindings with open types. Which in your case it probably doesn't.

I feel that I am not being helpful but I'm not sure how to help more.


From: "Philip K.F. Hölzenspies" [mailto:p.k.f.holzenspies at]
Sent: 04 September 2013 21:25
To: Simon Peyton-Jones
Cc: glasgow-haskell-users at<mailto:glasgow-haskell-users at>
Subject: Re: Question about correct GHC-API use for type checking (or zonking, or tidying)

Ah, this is good to know. What I really would like is a function:

mkId :: Name -> LHsExpr Name -> TcM Id

where that Id is something I can store in my own monad (IA). The meaning of this, is indeed "let <name> = <expression>" as a top-level binding. The behaviour should actually be the same as that statement at the ghci-prompt. My IA monad implements liftTcM as something that invokes a TcM-monad, i.e.

liftTcM :: TcM b -> IA b
liftTcM thing_inside = do
    hsc_env <- getSession
    stored_ids <- getStoredIds :: IA [Id]    -- this is the list of all Ids made through mkId mentioned above
    ioMsgMaybe . initTcPrintErrors hsc_env iNTERACTIVE $
        setInteractiveContext hsc_env (hsc_IC hsc_env) $
            tcExtendGlobalValEnv stored_ids $ -- or tcExtendIdEnv??

In the example you give below, I'm curious which "thing_inside" you give to tcLocalBinds to get you the correct global environment. Also, if I do what you suggest, i.e.

poly_id <- setGblEnv gbl_env (tcLookupId the_id_name)

is that poly_id "self contained," in the sense that I can put it in a new instantiation as shown above?


[cid:image001.jpg at 01CEAEDB.407DA510]
Simon Peyton-Jones<mailto:simonpj at>
September 4, 2013 6:00 PM
The id you are getting is a monomorphic id, with a type like a->a, not the polymorphic forall a. a->a.  You don't want to go round arbitrarily creating a new Id with the same unique but a different type. I have no idea what would happen then.

It's hard for me to understand just what you code is trying to do.  I think you are making bindig
                it = <some expr>

and then you want the type of "it".  Maybe something like

   (binds', gbl_env) <- tcLocalBinds (..your bindin..)
   poly_id <- setGblEnv gbl_env (tcLooupId the_id_name)

But I'm not totally sure.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.jpg
Type: image/jpeg
Size: 1239 bytes
Desc: image001.jpg
URL: <>

More information about the Glasgow-haskell-users mailing list