Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the current module name in Haskell

Tags:

haskell

So, I could accomplish this by using 'topLevelSomething and removing the last token after ., or I could use moduleName 'something but that returns a Maybe...

Is there a more straightforward way to get the module name of the current context?

So, given the code:

module My.Module.Blah where
test = magicHere

What goes in that magicHere spot such that test = "My.Module.Blah" ?

like image 236
mentics Avatar asked Mar 29 '11 23:03

mentics


People also ask

How to define module in Haskell?

Haskell's module design is relatively conservative: the name-space of modules is completely flat, and modules are in no way "first-class." Module names are alphanumeric and must begin with an uppercase letter. There is no formal connection between a Haskell module and the file system that would (typically) support it.

What is module Main where in Haskell?

A Haskell module is a collection of related functions, types and typeclasses. A Haskell program is a collection of modules where the main module loads up the other modules and then uses the functions defined in them to do something. Having code split up into several modules has quite a lot of advantages.


2 Answers

I thought this was a nice question, so I figured out the answer using Template Haskell:

{-# LANGUAGE TemplateHaskell #-}
module A.B.C where

import Language.Haskell.TH
import Language.Haskell.TH.Syntax

e :: String
e = $(fmap loc_module qLocation >>= \mod ->  return (LitE (StringL mod) ))

main = print e
like image 151
Chris Kuklewicz Avatar answered Sep 29 '22 18:09

Chris Kuklewicz


There's a rather roundabout way to get the current module name using Typeable.

module My.Module.Blah where
import Data.Typeable

data T = T deriving Typeable
test = init $ init $ show $ typeOf T
like image 22
augustss Avatar answered Sep 29 '22 20:09

augustss