Opened 11 months ago
Last modified 7 months ago
#15833 new bug
Typed template haskell quote fails to typecheck when spliced due to an ambiguous type variable
Reported by: | mpickering | Owned by: | |
---|---|---|---|
Priority: | normal | Milestone: | |
Component: | Template Haskell | Version: | 8.6.1 |
Keywords: | TypedTemplateHaskell | Cc: | |
Operating System: | Unknown/Multiple | Architecture: | Unknown/Multiple |
Type of failure: | None/Unknown | Test Case: | |
Blocked By: | Blocking: | ||
Related Tickets: | Differential Rev(s): | ||
Wiki Page: |
Description
It should be the case that a code value constructed using typed template haskell should never fail to type check when spliced. Running ghc Test.hs
with the following two modules produces an error about an ambiguous type variable.
https://gist.github.com/5890c14dda73da738d2041c7f677b786
{-# LANGUAGE TemplateHaskell #-} {-# OPTIONS_GHC -Wall #-} module Compiler where import Language.Haskell.TH data Operator = Scan | Join Operator Operator deriving Show queryJoin :: Operator queryJoin = Join Scan Scan type QTExp a = Q (TExp a) fix :: (a -> a) -> a fix f = let x = f x in x while :: Monoid m => QTExp (IO m -> IO m) -> QTExp (IO m) while b = [|| fix (\r -> whenM True ($$b r)) ||] whenM :: Monoid m => Bool -> m -> m whenM b act = if b then act else mempty execOp :: Monoid m => Operator -> QTExp (IO m) -> QTExp (IO m) execOp op yld = case op of Scan -> while [|| \r -> ($$(yld) >> r)||] Join left right -> execOp left (execOp right yld) runQuery :: QTExp (IO ()) runQuery = execOp (Join Scan Scan) ([|| return () ||])
{-# LANGUAGE TemplateHaskell #-} {-# OPTIONS_GHC -ddump-splices #-} module Test where import qualified Compiler as C main :: IO () main = do $$(C.runQuery)
Test.hs:9:6: error: • Ambiguous type variable ‘a0’ arising from a use of ‘C.whenM’ prevents the constraint ‘(Monoid a0)’ from being solved. Relevant bindings include r_a5GX :: IO a0 (bound at Test.hs:9:6) Probable fix: use a type annotation to specify what ‘a0’ should be. These potential instances exist: instance Monoid a => Monoid (IO a) -- Defined in ‘GHC.Base’ instance Monoid Ordering -- Defined in ‘GHC.Base’ instance Semigroup a => Monoid (Maybe a) -- Defined in ‘GHC.Base’ ...plus 7 others (use -fprint-potential-instances to see them all) • In the expression: (C.whenM True) ((\ r_a5GY -> ((return ()) >> r_a5GY)) r_a5GX) In the first argument of ‘C.fix’, namely ‘(\ r_a5GX -> (C.whenM True) ((\ r_a5GY -> ((return ()) >> r_a5GY)) r_a5GX))’ In the first argument of ‘(>>)’, namely ‘(C.fix (\ r_a5GX -> (C.whenM True) ((\ r_a5GY -> ((return ()) >> r_a5GY)) r_a5GX)))’ | 9 | $$(C.runQuery) |
The generated code
Test.hs:9:6-15: Splicing expression C.runQuery ======> C.fix (\ r_a5GV -> (C.whenM True) ((\ r_a5GW -> ((C.fix (\ r_a5GX -> (C.whenM True) ((\ r_a5GY -> ((return GHC.Tuple.()) >> r_a5GY)) r_a5GX))) >> r_a5GW)) r_a5GV))
Change History (5)
comment:1 Changed 11 months ago by
comment:2 Changed 11 months ago by
Here is a minimised version which still exhibits the same failure.
{-# LANGUAGE TemplateHaskell #-} {-# LANGUAGE ScopedTypeVariables #-} {-# LANGUAGE TypeApplications #-} {-# OPTIONS_GHC -Wall #-} module Compiler where import Language.Haskell.TH type QTExp a = Q (TExp a) fix :: (a -> a) -> a fix f = let x = f x in x while :: Monoid m => QTExp (IO m -> IO m) -> QTExp (IO m) while b = [|| fix (\r -> whenM ($$b r)) ||] whenM :: Monoid m => a -> m whenM _ = mempty execOp :: forall m . Monoid m => QTExp (IO m) execOp = while [|| \r -> $$(while @m [|| id ||]) >> r ||] runQuery :: QTExp (IO ()) runQuery = execOp
comment:3 Changed 8 months ago by
Keywords: | TypedTemplateHaskell added |
---|
comment:4 Changed 8 months ago by
Component: | Compiler → Template Haskell |
---|
comment:5 Changed 7 months ago by
My office mate ran into exactly this bug when trying to stage a non-trivial (500 line) program.
Note: See
TracTickets for help on using
tickets.
If I inline both recursive calls to
execOp
then the code inCompiler
fails to typecheck.