Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9097661
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:10:16+00:00 2026-06-17T00:10:16+00:00

I just start learning Template Haskell, and stuck on simple problem with splicing. In

  • 0

I just start learning Template Haskell, and stuck on simple problem with splicing.
In one module I’ve implemented function tupleN which replies N-th element of the tuple:

tupleN :: Lift a => a -> Int -> Q Exp
tupleN a n = do
   (TupE as) <- lift a
   return $ as !! n

In the Main module I have:

main :: IO ()
main = do
   let tup = (1::Int,'a',"hello")
   putStrLn $ show $(tupleN $tup 1)

This seems to be working, but it wouldn’t. Compiler prints error:

GHC stage restriction: `tup'
  is used in a top-level splice or annotation,
  and must be imported, not defined locally
In the expression: tup
In the first argument of `tupleN', namely `$tup'
In the expression: tupleN ($tup) 1

If I put tuple description right into spliced expression, code become working:

main :: IO ()
main = do
   putStrLn $ show $(tupleN (1::Int,'a',"hello") 1)

What I missing with the first variant?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T00:10:18+00:00Added an answer on June 17, 2026 at 12:10 am

    You’ve tried to use tup as a splice, but tup is just an ordinary value. You don’t want to prefix it with a $.

    Moreover, as the compile error states, since Template Haskell runs during the compilation process, GHC really needs to know what it’s doing before it has finished compiling the current module. That means your splice expression can’t depend on tup, because that’s still being compiled. Inside splices, you can only use literals, imported values, and the special 'name and ''TypeName forms (which you can think of as a sort of literal, I suppose). You can get at some of the information from this compilation by using e.g. reify, but even that can only give you data that’s available at compile time – if you want a function you can pass user input, or data constructed from user input, to, that’s just impossible.

    In short, you can’t do exactly what you want to do using Template Haskell. You could, however, define a splice that expands to a function to get the ith element of a tuple of size sz:

    import Control.Monad (unless)
    import Language.Haskell.TH
    
    tupleN :: Int -> Int -> Q Exp
    tupleN sz i = do
      unless (i < sz) . reportError $ "tupleN: index " ++ show i
        ++ " out of bounds for " ++ show sz ++ "-tuple"
      lamE
        [tupP (replicate i wildP
          ++ [varP (mkName "x")]
          ++ replicate (sz - i - 1) wildP)]
        (varE (mkName "x"))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just start learning C++ and trying to make a simple function that do substring
Just start to learning Cocos2d and getting errors in Xcode 4.3.3 (4E3002). Problem in
I just start learning to write web services yesterday, kinda fascinating. I created an
I just start learning WPF, is that possible I can create an IE toolbar
I just start learning Python + Tornado for my web servers. Every time I
I just installed the latest SDK from Apple so I can start learning iPhone
I wrote simple class that on the start it just increase the value of
hi i am a student and just start learning low level c programming.i tried
Just looking for some good reasons so i could start learning about them :/
I am trying to start learning OO JS and have one quick question (i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.