Fwd: Type/type class information in GHC plugin

Hello everyone, I would like to write a GHC plugin which adds wrapper to functions. Say I have a function addWrapper :: Ord a => (a -> b) -> a -> b which is supposed to transform (assuming the argument is in the Ord class) foo = \ x -> ... into foo = addWrapper (\ x -> ...) This works fine if I'm only interested in wrapping functions of type Int -> Int, in which case I make addWrapper to also have type addWrapper :: (Int -> Int) -> Int -> Int and use mkCoreApp to apply addWrapper to the right hand side. However, if I want it to be polymorphic as described above, the transformed GHC Core for an Int -> Int function should look like foo = addHook @ GHC.Types.Int @ GHC.Types.Int GHC.Classes.$fOrdInt (\ x -> ...) My question is, given the right hand side of foo, how do I construct the type/type class information inside Core shown above? Cheers, Shen

Hello Shen,
I’m not sure if I’m answering exactly what you’re asking, but perhaps this can move you in the right direction:
- To apply an expression to some types, you can use mkTyApps, in the CoreSyn module. The type of that function (Expr b -> [Type] -> Expr b) should be self-explanatory.
- To get the instance environments (where all class instances are stored), use tcGetInstEnvs, in the Inst module.
- Then, to get the actual dictionary, you can do the lookup with lookupInstEnv, in the InstEnv module.
Does this move you in the right direction?
Richard
On Apr 24, 2014, at 5:37 PM, Shen Chen Xu
Hello everyone,
I would like to write a GHC plugin which adds wrapper to functions. Say I have a function
addWrapper :: Ord a => (a -> b) -> a -> b
which is supposed to transform (assuming the argument is in the Ord class)
foo = \ x -> ...
into
foo = addWrapper (\ x -> ...)
This works fine if I'm only interested in wrapping functions of type Int -> Int, in which case I make addWrapper to also have type
addWrapper :: (Int -> Int) -> Int -> Int
and use mkCoreApp to apply addWrapper to the right hand side.
However, if I want it to be polymorphic as described above, the transformed GHC Core for an Int -> Int function should look like
foo = addHook @ GHC.Types.Int @ GHC.Types.Int GHC.Classes.$fOrdInt (\ x -> ...)
My question is, given the right hand side of foo, how do I construct the type/type class information inside Core shown above?
Cheers, Shen
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users
participants (2)
-
Richard Eisenberg
-
Shen Chen Xu