On Fri, Mar 4, 2011 at 3:45 PM, Yves Parès <span dir="ltr"><<a href="mailto:limestrael@gmail.com">limestrael@gmail.com</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
Hello,<br><br>For testing purposes, I am trying to make an overlay to IO which carries a phantom type to ensure a context.<br>I define contexts using empty type classes :<br><br>class CtxFoo c<br>class CtxBar c<br><br>The overlay :<br>
<br>newtype MyIO c a = MyIO (IO a)<br><br>Then I define some methods that run only a specific context :<br><br>runFoo :: (CtxFoo c) => MyIO c a -> IO a<br>runFoo (MyIO x) = x<br><br>runBar :: (CtxBar c) => MyIO c a -> IO a<br>
runBar (MyIO x) = x<br><br>And then an action that runs in context 'Foo' :<br><br>someAction :: (CtxFoo c) => MyIO c ()<br>someAction = putStrLn "FOO"<br><br>Then I run it :<br><br>main = runFoo someAction<br>
<br>But obiously, GHC complains that my type 'c' remains uninstantiated :<br><br> Ambiguous type variable `c' in the constraint:<br> (CtxFoo c) arising from a use of `runFoo'<br> Probable fix: add a type signature that fixes these type variable(s)<br>
In the expression: runFoo someAction<br> In an equation for `main': main = runFoo someAction<br><br><br>Is there a way to deal with this ?<br>The interest of using type classes and not empty types to represent the contexts is that it stays simple, and that I can do that :<br>
<br>someAction2 :: (CtxFoo c, CtxBar c) => MyIO c ()<br>someAction2 = putStrLn "FOO and BAR"<br><br>... a function that can run in both contexts.<br>
<br></blockquote><div><br>data X<br>instance CtxFoo X<br><br>runFoo (someAction :: MyIO X ())<br><br>data Y<br>instance CtxFoo Y<br>instance CtxBar Y<br><br>runFoo (someAction2 :: MyIO Y ())<br>runBar (someAction2 :: MyIO Y ())<br>
<br>runFoo (someAction :: MyIO Y ()) -- also works since Y provides both a Foo and Bar context)<br><br> -- ryan<br><br> </div><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
<br></blockquote></div><br>