-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
128 changed files
with
2,633 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
|
||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
import Language.Haskell.TH | ||
import Language.Haskell.TH.Syntax | ||
|
||
listMembers :: Q [Dec] | ||
listMembers = do | ||
info <- reify (mkName "Language.Haskell.TH.Syntax") | ||
case info of | ||
TyConI (DataD _ _ _ _ constructors _) -> return constructors | ||
_ -> fail "Invalid type" | ||
|
||
main :: IO () | ||
main = do | ||
members <- runQ listMembers | ||
putStrLn "Members:" | ||
mapM_ (putStrLn . pprint) members |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
import Language.Haskell.TH | ||
import Language.Haskell.TH.Syntax | ||
|
||
listMembers :: Q [Con] | ||
listMembers = do | ||
info <- reify (mkName "Language.Haskell.TH.Syntax") | ||
case info of | ||
TyConI (DataD _ _ _ _ constructors _) -> return constructors | ||
_ -> fail "Invalid type" | ||
|
||
main :: IO () | ||
main = do | ||
members <- runQ listMembers | ||
putStrLn "Members:" | ||
mapM_ (putStrLn . pprint) members |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module THExample where | ||
|
||
import Language.Haskell.TH | ||
import Language.Haskell.TH.Syntax | ||
|
||
listMembers :: Q [Con] | ||
listMembers = do | ||
info <- reify (mkName "Language.Haskell.TH.Syntax") | ||
case info of | ||
TyConI (DataD _ _ _ _ constructors _) -> return constructors | ||
_ -> fail "Invalid type" | ||
import Language.Haskell.TH | ||
|
||
getMembers :: Q [Dec] -> Q [Dec] | ||
getMembers qDecs = qDecs | ||
|
||
doTheThing :: Q [Dec] -> Q [Dec] | ||
doTheThing = getMembers | ||
|
||
main :: IO () | ||
main = do | ||
let constructors = $(doTheThing [d| data Color = Red |]) | ||
mapM_ (putStrLn . pprint) constructors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Language.Haskell.TH | ||
|
||
getMembers :: Q [Dec] -> Q [Dec] | ||
getMembers qDecs = qDecs | ||
|
||
doTheThing :: Q [Dec] -> Q [Dec] | ||
doTheThing = getMembers | ||
|
||
main :: IO () | ||
main = do | ||
let constructors = $(doTheThing [d| data Color = Red |]) | ||
mapM_ (putStrLn . pprint) constructors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
import Language.Haskell.TH | ||
|
||
getMembers :: Q [Dec] -> Q [Dec] | ||
getMembers qDecs = qDecs | ||
listMembers :: Q a -> Q [Con] | ||
listMembers continuation = do | ||
info <- reify (mkName "Language.Haskell.TH.Syntax" | ||
) | ||
case info of | ||
TyConI (DataD _ _ _ _ constructors _) -> continu | ||
ation >> return constructors | ||
_ -> fail "Invalid type" | ||
main :: IO () | ||
main = do | ||
let constructors = listMembers | ||
mapM_ (putStrLn . pprint) constructors |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Main where | ||
|
||
import Language.Haskell.TH | ||
|
||
-- Helper function to list the names of functions in the current module | ||
listFunctionNames :: Q [Name] | ||
listFunctionNames = do | ||
info <- reify ''Main -- Replace 'Main' with the actual module name | ||
case info of | ||
TyConI (DataD _ _ _ _ decs _) -> return $ extractFunctionNames decs | ||
_ -> fail "Invalid module" | ||
|
||
-- Extracts the names of functions from a list of declarations | ||
extractFunctionNames :: [Dec] -> [Name] | ||
extractFunctionNames = foldr extract [] where | ||
extract (FunD name _) names = name : names | ||
extract _ names = names | ||
|
||
-- Print the names of functions in the current module | ||
main :: IO () | ||
main = do | ||
functionNames <- runQ listFunctionNames | ||
putStrLn "Functions in the current module:" | ||
mapM_ print functionNames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
module Main where | ||
|
||
import Language.Haskell.TH | ||
|
||
-- Helper function to list the names of functions in the current module | ||
listFunctionNames :: Q [Name] | ||
listFunctionNames = do | ||
info <- reify (mkName "Main") | ||
case info of | ||
ModuleInfo _ declarations _ _ -> return $ extractFunctionNames declarations | ||
_ -> fail "Invalid module" | ||
|
||
-- Extracts the names of functions from a list of declarations | ||
extractFunctionNames :: [Dec] -> [Name] | ||
extractFunctionNames = foldr extract [] where | ||
extract (FunD name _) names = name : names | ||
extract _ names = names | ||
|
||
-- Print the names of functions in the current module | ||
main :: IO () | ||
main = do | ||
functionNames <- runQ listFunctionNames | ||
putStrLn "Functions in the current module:" | ||
mapM_ print functionNames |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
tt: | ||
ghc test24.hs | ||
./test24 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
{-# LANGUAGE TemplateHaskell #-} | ||
|
||
import Language.Haskell.TH | ||
|
||
-- Foo data type | ||
data Foo | ||
= FooIdentifier Name | ||
| FooVariable Name String | ||
| FooComposed [Foo] | ||
|
||
-- Convert Q Exp to Foo | ||
qToFoo :: Q Exp -> Q Foo | ||
qToFoo qexp = qexp >>= convertExp | ||
where | ||
convertExp (VarE name) = return (FooVariable name "") | ||
convertExp (ConE name) = return (FooIdentifier name) | ||
convertExp (AppE e1 e2) = do | ||
foo1 <- qToFoo (return e1) | ||
foo2 <- qToFoo (return e2) | ||
return (FooComposed [foo1, foo2]) | ||
convertExp _ = error "Unsupported expression" | ||
|
||
-- Convert Foo to String | ||
fooToString :: Foo -> String | ||
fooToString (FooIdentifier name) = show name | ||
fooToString (FooVariable name value) = show name ++ " (" ++ value ++ ")" | ||
fooToString (FooComposed foos) = concatMap fooToString foos | ||
|
||
-- Example usage | ||
main :: IO () | ||
main = do | ||
let example = [| fooToString |] | ||
foo <- runQ (qToFoo example) | ||
putStrLn "Original Q expression:" | ||
-- putStrLn (pprint example) | ||
putStrLn "\nFoo conversion:" | ||
putStrLn (fooToString foo) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
data AnnLookup = AnnLookupModule Module | AnnLookupName Name | ||
= ModuleAnnotation | TypeAnnotation Name | ValueAnnotation Name | ||
= ModuleDoc | DeclDoc Name | ArgDoc Name Int | InstDoc Type | ||
loc_module :: String, | ||
type Module :: * | ||
data Module = Module PkgName ModName | ||
type ModuleInfo :: * | ||
data ModuleInfo = ModuleInfo [Module] | ||
qReifyModule :: Module -> m ModuleInfo | ||
qReifyAnnotations, qReifyModule, qReifyConStrictness, qLocation, | ||
nameModule :: Name -> Maybe String | ||
reifyModule :: Module -> Q ModuleInfo | ||
moduleAnnotation :: AnnTarget | ||
thisModule :: Q Module |
Oops, something went wrong.