Commit 77662e13 authored by Ryan Scott's avatar Ryan Scott Committed by thomie

Add namePackage function to template-haskell

Currently there exists a nameBase function (for retrieving a Name's OccName)
and a nameModule function (for retrieving a Name's ModName), but there is no
counterpart for PkgNames.

This would be useful for implementing Template Haskell features which need
to have easy access to a Name's package (e.g., automatically derived Lift
instances).

Reviewed By: goldfire, austin, thomie

Differential Revision: https://phabricator.haskell.org/D1237
parent 8ecf6d8f
......@@ -50,6 +50,7 @@ module Language.Haskell.TH(
-- ** Deconstructing names
nameBase, -- :: Name -> String
nameModule, -- :: Name -> Maybe String
namePackage, -- :: Name -> Maybe String
-- ** Built-in names
tupleTypeName, tupleDataName, -- Int -> Name
unboxedTupleTypeName, unboxedTupleDataName, -- :: Int -> Name
......
......@@ -842,16 +842,48 @@ data NameSpace = VarName -- ^ Variables
type Uniq = Int
-- | The name without its module prefix
-- | The name without its module prefix.
--
-- ==== __Examples__
--
-- >>> nameBase ''Data.Either.Either
-- "Either"
-- >>> nameBase (mkName "foo")
-- "foo"
-- >>> nameBase (mkName "Module.foo")
-- "foo"
nameBase :: Name -> String
nameBase (Name occ _) = occString occ
-- | Module prefix of a name, if it exists
-- | Module prefix of a name, if it exists.
--
-- ==== __Examples__
--
-- >>> nameModule ''Data.Either.Either"
-- Just "Data.Either"
-- >>> nameModule (mkName "foo")
-- Nothing
-- >>> nameModule (mkName "Module.foo")
-- Just "Module"
nameModule :: Name -> Maybe String
nameModule (Name _ (NameQ m)) = Just (modString m)
nameModule (Name _ (NameG _ _ m)) = Just (modString m)
nameModule _ = Nothing
-- | A name's package, if it exists.
--
-- ==== __Examples__
--
-- >>> namePackage ''Data.Either.Either"
-- Just "base"
-- >>> namePackage (mkName "foo")
-- Nothing
-- >>> namePackage (mkName "Module.foo")
-- Nothing
namePackage :: Name -> Maybe String
namePackage (Name _ (NameG _ p _)) = Just (pkgString p)
namePackage _ = Nothing
{- |
Generate a capturable name. Occurrences of such names will be
resolved according to the Haskell scoping rules at the occurrence
......
......@@ -10,6 +10,8 @@
according to the fixities of the operators. The `ParensT` constructor can be
used to explicitly group expressions.
* Add `namePackage`
* TODO: document API changes and important bugfixes
......
{-# LANGUAGE TemplateHaskell #-}
module Main where
import Language.Haskell.TH
eitherName, fooName, moduleFooName :: Name
eitherName = ''Either
fooName = mkName "foo"
moduleFooName = mkName "Module.foo"
main :: IO ()
main = do
print $ nameBase eitherName
print $ nameBase fooName
print $ nameBase moduleFooName
print $ nameModule eitherName
print $ nameModule fooName
print $ nameModule moduleFooName
print $ namePackage eitherName
print $ namePackage fooName
print $ namePackage moduleFooName
"Either"
"foo"
"foo"
Just "Data.Either"
Nothing
Just "Module"
Just "base"
Nothing
Nothing
......@@ -352,3 +352,4 @@ test('T10704',
multimod_compile_and_run,
['T10704', '-v0'])
test('T6018th', normal, compile_fail, ['-v0'])
test('TH_namePackage', normal, compile_and_run, ['-v0'])
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment