Further to my goal of reducing the boilerplate required to use Haxl with a relational database, I am trying to package up the result of a raw SQL request via Persistent in an existentially quantified type. However the type checker won't allow it:
data SomeRawSql where
SomeRawSql :: forall b. RawSql b => [b] -> SomeRawSql
packedVal = let res = runDB $ rawSql "SELECT * FROM ..." [toPersistValue (pack "ABC")]
in fmap SomeRawSql res
This results in a type error on the line with fmap: Ambiguous type variable ‘b0’ arising from a use of ‘SomeRawSql’ prevents the constraint ‘(RawSql b0)’ from being solved.
The type of rawSql from persistent is:
rawSql :: (RawSql a, MonadIO m)
=> Text -- ^ SQL statement, possibly with placeholders.
-> [PersistValue] -- ^ Values to fill the placeholders.
-> ReaderT SqlBackend m [a]
runDB
is a helper function that connections to the database and returns IO [a]
. Based on the definition of rawSql I would expect the RawSql constraint to be satisfied. I don't understand why this error arises.