RE: How to extract name and type of exported functions in modules

Simon Peyton-Jones <[email protected]> Tue, 20 Oct 2009 14:38:33 +0000
Newsgroups gmane.comp.lang.haskell.template,gmane.comp.lang.haskell.glasgow.user
Message-ID <59543203684B2244980D7E4057D5FBC1061B62A1@DB3EX14MBXC310.europe.corp.microsoft.com>
[ccing ghc-users]

| I'm trying to extract the names and types of exported functions in a modu=
le.

Quite a reasonable request.  But it's not conveniently possible at the mome=
nt.  Here's what you can do in the Q monad:

class (Monad m, Functor m) =3D> Quasi m where
	-- Fresh names
  qNewName :: String -> m Name

	-- Error reporting and recovery
  qReport  :: Bool -> String -> m ()	-- Report an error (True) or warning (=
False)
					-- ...but carry on; use 'fail' to stop
  qRecover :: m a -> m a -> m a		-- Recover from the monadic 'fail'
					-- The first arg is the error handler
=20
	-- Inspect the type-checker's environment
  qReify :: Name -> m Info
  qLocation :: m Loc

	-- Input/output (dangerous)
  qRunIO :: IO a -> m a

But you want one more function:

  qReifyModule :: String -> m ModuleInfo

where ModuleInfo is something like:

data ModuleInfo =3D MI { mi_exports :: [Name]
                     , ... instances,rules, etc }

Then you could use qReify to get info about a particular thing.


This would not be hard in principle, the details need thought.  For example=
, if module exports T( C1, C2), where C1, C2 are constructors of T which al=
so has constructors C3, C4, what should appear in mi_exports?  Should mi_ex=
ports reflect the structure of the export list (see the AvailInfo type in G=
HC).

What is the info for an instance?
Do we need a way to ask for all the instances of a class (or rules for a fu=
nction) regardless of what module those instances come from?   etc


Does this ring bells for anyone else?  Would someone like to write the spec=
?  Are there other reify-related things that we need?

Simon

| -----Original Message-----
| From: [email protected] [mailto:template-haskell-
| [email protected]] On Behalf Of Oscar Finnsson
| Sent: 16 October 2009 18:47
| To: [email protected]
| Subject: [Template-haskell] How to extract name and type of exported func=
tions in
| modules
|=20
| Hi,
|=20
| I'm trying to extract the names and types of exported functions in a modu=
le.
|=20
| At the moment I've managed to get a list of all functions in a module
| but I can't seem to figure out how to get their types.
|=20
| Lets say I got the module
|=20
|     module Foo where
|=20
|     foo1 :: String -> String
|     foo1 value =3D value
|=20
|     foo2 =3D "hej"
|=20
| and then in anothor module...
|=20
|     module Bar where
|=20
|     bar =3D $(getAllFunctions "<some-path>/Foo.hs")
|=20
| At the moment I got getAllFunctions returning ["foo1","foo2"], but I
| would really like to get it to return [("foo1",AppT (ConT "String")
| (ConT "String")), ("foo2",ConT "String")]
|=20
| Using "parseModuleWithMode" from Language.Haskell.Exts I can get hold
| of the names and the type signature of foo1 (since it's specified in
| the source code) but I cannot get hold of the type signature of foo2
| (since it's not specified in the source code).
|=20
| Is there another way to get the names/signatures of exported functions
| from a module other than using parseModuleWithMode so the developer
| writing the Foo-module isn't forced to explicitly supply type
| signatures of the exported functions?
|=20
| If I try "reify" to get information about the functions I get the error m=
essage:
| "foo1 is not in scope at a reify"
|=20
| This seems to be a known bug about reify (reported here
| http://hackage.haskell.org/trac/ghc/ticket/2339). My problem is that I
| cannot use the workaround since I don't know the name of the
| functions.
|=20
| Another disadvantage with this approach is that "getAllFunctions" must
| have access to the source code of the module and that I must supply
| the path the the module. If possible I would like to have code such as
|=20
|     bar =3D $(getAllFunctions "Foo")
|=20
| instead of "<come-path>/Foo.hs".
|=20
| Regards,
| Oscar Finnsson
| _______________________________________________
| template-haskell mailing list
| [email protected]
| http://www.haskell.org/mailman/listinfo/template-haskell