Skip to content

Instantly share code, notes, and snippets.

@max630
Created November 28, 2011 23:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save max630/1402642 to your computer and use it in GitHub Desktop.
Save max630/1402642 to your computer and use it in GitHub Desktop.
stage restriction for instances in quotations
{-# LANGUAGE TemplateHaskell #-}
module LibTest where
import Language.Haskell.TH.Ppr (pprint)
import Language.Haskell.TH.Syntax (runQ, Lit(StringL), reify)
import Language.Haskell.TH.Lib (litE)
import Monad (liftM)
import Data.Typeable (typeOf)
import Language.Haskell.TH.Syntax (Q, Exp)
testEqIO :: Q Exp -> Q Exp -> Q Exp
testEqIO expr1 expr2 = do
e1 <- expr1
e2 <- expr2
let
q :: Q Exp -> Q Exp
q = (\e -> e >>= (litE . StringL . pprint))
[|do
v1 <- $(expr1)
v2 <- $(expr2)
print (typeOf v1)
print (typeOf v2)
if (v1 == v2)
then putStr "."
else do
putStrLn ($(q expr1) ++ " == " ++ $(q expr2) ++ " failed:")
putStrLn ("First: " ++ show v1)
putStrLn ("Second: " ++ show v2)
|]
{-# LANGUAGE TemplateHaskell, DeriveDataTypeable #-}
-- just run $ghc TestA.hs -e main
-- I have ghc=7.0.4
module TestA where
import LibTest
import Data.Typeable (Typeable)
import List (find)
import Maybe (fromJust)
class A a where
runA :: a -> Integer
findA l = fromJust (find ((/= 0) . runA) l)
newtype TestA = TestA Integer deriving (Eq, Show, Typeable)
instance A TestA where
runA (TestA v) = v
main = $(testEqIO [|return $ findA ([]{- :: [TestA] -})|] -- if uncomment the (correct) type - compilation fails due to stage restriction, otherwise it works
[|return $ TestA 1|])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment