Created
November 28, 2011 23:44
-
-
Save max630/1402642 to your computer and use it in GitHub Desktop.
stage restriction for instances in quotations
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
{-# 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) | |
|] |
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
{-# 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