Template Haskell の学習を始めたばかりで、スプライシングに関する単純な問題に固執しています。あるモジュールでは、タプルの N 番目の要素を返す
関数を実装しました。tupleN
tupleN :: Lift a => a -> Int -> Q Exp
tupleN a n = do
(TupE as) <- lift a
return $ as !! n
メインモジュールには次のものがあります。
main :: IO ()
main = do
let tup = (1::Int,'a',"hello")
putStrLn $ show $(tupleN $tup 1)
これは機能しているように見えますが、そうではありません。コンパイラはエラーを出力します:
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
タプルの説明を結合された式に入れると、コードが機能するようになります。
main :: IO ()
main = do
putStrLn $ show $(tupleN (1::Int,'a',"hello") 1)
最初のバリアントで欠けているものは何ですか?