<div dir="ltr">Hi -<div><br></div><div>(Apologies for the long post)<br><div><br></div><div>I need some help integrating a typeclass based functionality within an existing API. I am using Control.Monad.Validate for my validation of objects. Here's a simple function based on this ..</div><div><br></div><div><font face="monospace">debit :: forall m. (MonadReader Env m, <br>                    MonadValidate [Error] m) => Y.Dense "USD" -> Account -> m Account<br>debit amount = updateBalance ((-1) * amount)</font></div><div><br></div><div>Now I want to make a change in the API where I would like to fetch the account from some database based on account number. For this I have a typeclass as below ..</div><div><br></div><div><font face="monospace">class (Monad m) => AccountDB m where<br>    query :: Text -> m (Maybe Account) </font></div><div><font face="monospace">.. elided details</font></div><div><br></div><div>and I am using persistent - so I have an instance of this class as follows ..</div><div><br></div><div><font face="monospace">-- | Instance of AccountDB for `SqlPersistT` (which is `ReaderT SqlBackend`)<br>instance (MonadIO m) => AccountDB (SqlPersistT m) where<br>  query ano = get (AccountKey ano)</font><br></div><div><font face="monospace">.. other details elided</font></div><div><br></div><div>My question is how can I integrate this typeclass within the above API ? I tried as yet another constraint in the function ..</div><div><br></div><div><font face="monospace">debit :: forall m. (MonadReader Env m, <br>                    MonadValidate [Error] m, <br>                    AccountDB m) => Y.Dense "USD" -> Text -> m Account<br>debit amount accNo = do <br>  maybeAccount <- query accNo  <br>  maybe (refuteErr $ InvalidAccountNumber accNo) (updateBalance ((-1) * amount)) maybeAccount</font><br></div><div><br></div><div>This compiles but I don't get the proper instance of the typeclass when I run it - possibly due to the reason that the "m" cannot be the same for AccountDB as the other constraints. And I don't want to hardcode SqlPersistT in the function debit as I would like to be able to run it with other instances of the typeclass as well (example for testing I may like to test against a HashMap).</div><div><br></div><div>What would be an idiomatic approach to this kind of composition ?</div><div><br></div><div>Any help will be appreciated.</div><div><br></div><div>regards.</div><div><br clear="all"><div><br></div>-- <br><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div>Debasish Ghosh<br><a href="http://manning.com/ghosh2" target="_blank">http://manning.com/ghosh2</a></div><div><a href="http://manning.com/ghosh" target="_blank">http://manning.com/ghosh</a><br></div><div><br>Twttr: @debasishg<br>Blog: <a href="http://debasishg.blogspot.com" target="_blank">http://debasishg.blogspot.com</a><br>Code: <a href="http://github.com/debasishg" target="_blank">http://github.com/debasishg</a></div></div></div></div></div></div>