<div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><div><div>Hi all,</div><div><br></div><div>I'm attempting to make a simple evaluator for GHC core, but I'm not</div><div>clear on how to reliably looking up names. I'm compiling each of</div><div>ghc-prim, integer-simple and base with a patched version of GHC which</div><div>performs an extra output step with the Core AST to a file for each</div><div>module.</div><div><br></div><div>Later, I load those files in. So for an input Haskell file like this:</div><div><br></div><div>    module Main (main,Foo(..)) where</div><div>    class Foo a where foo :: a -> Int</div><div>    instance Foo Int where foo x = x * x </div><div>    instance Foo Char where foo x = 99</div><div>    main = print (foo (123 :: Int))</div><div><br></div><div>I have an output set of bindings like this:</div><div><br></div><div><a href="https://gist.github.com/chrisdone/cb05a77d3fcb081a4580b5f85289674a">https://gist.github.com/chrisdone/cb05a77d3fcb081a4580b5f85289674a</a></div><div><br></div><div>One thing that I immediately notice is that the names of things are</div><div>completely non-unique, especially in generated names. So here are two</div><div>implementations of the method "foo" for the class "Foo":</div><div><br></div><div>( Id {idStableName = "main:Main:$cfoo", idUnique = Unique 6989586621679010917}, ...) -- Int</div><div>( Id {idStableName = "main:Main:$cfoo", idUnique = Unique 6989586621679010923}, ...) -- Char</div><div><br></div><div>So e.g. the instance for "Foo Int" refers to the above method</div><div>implementation via its Unique (6989586621679010923):</div><div><br></div><div>( Id</div><div>    {idStableName = "main:Main:$fFooInt", idUnique = Unique 8214565720323784705}</div><div>, CastE</div><div>    (VarE</div><div>       (Id</div><div>          { idStableName = "main:Main:$cfoo"</div><div>          , idUnique = Unique 6989586621679010923 <---- HERE</div><div>          })))</div><div><br></div><div>At first, I thought I would use the Unique associated with every Name to</div><div>make a lookup. This is completely reliable within one GHC compilation,</div><div>but I've read in the docs that it's not stable across multiple</div><div>invocations? What does that mean for my case?</div><div><br></div><div>Another thing I notice is that type-class methods are not generated at</div><div>the core level. I have, for example, this method call that provides it</div><div>the instance dictionary,</div><div><br></div><div>    (AppE</div><div>       (AppE</div><div>          (VarE</div><div>             (Id</div><div>                { idStableName = "main:Main:foo"</div><div>                , idUnique = Unique 8214565720323784707 <---- MISSING</div><div>                }))</div><div>          (TypE (Typ "Int")))</div><div>       (VarE</div><div>          (Id</div><div>             { idStableName = "main:Main:$fFooInt"</div><div>             , idUnique = Unique 8214565720323784705</div><div>             })))</div><div><br></div><div>But the "main:Main:foo" (8214565720323784707) is not produced in the</div><div>CoreProgram, it seems. My compile step is very simple:</div><div><br></div><div>    compile ::</div><div>         GHC.GhcMonad m</div><div>      => GHC.ModSummary</div><div>      -> m [CoreSyn.Bind GHC.Var]</div><div>    compile modSummary = do</div><div>      parsedModule <- GHC.parseModule modSummary</div><div>      typecheckedModule <- GHC.typecheckModule parsedModule</div><div>      desugared <- GHC.desugarModule typecheckedModule</div><div>      let binds = GHC.mg_binds (GHC.dm_core_module desugared)</div><div>      pure binds</div><div><br></div><div>It simply gets the bindings and that's all from the ModGuts.</div><div><br></div><div>    mg_binds :: !CoreProgram</div><div><br></div><div>Two questions:</div><div><br></div><div>1) How do I recognize class methods when I see one, like the</div><div>   "main:Main:foo" above?</div><div><br></div><div>   Maybe this? isClassOpId_maybe :: Id -> Maybe Class</div><div><br></div><div>   Is an "op" what GHC calls type-class methods?</div><div><br></div><div>2) If I compile e.g. ghc-prim and that generates a binding Name with ID</div><div>   123, and then I compile base -- will the ID 123 be re-used by base</div><div>   for something else, or will any reference to 123 in the compiled</div><div>   Names for base refer ONLY to that one in ghc-prim? In other words,</div><div>   when GHC loads the iface for ghc-prim, does it generate a fresh set</div><div>   of names for everything in ghc-prim, or does it load them from file?</div><div><br></div><div>Cheers!</div></div></div></div></div></div></div>