On Fri, Mar 4, 2011 at 3:45 PM, Yves Parès <span dir="ltr">&lt;<a href="mailto:limestrael@gmail.com">limestrael@gmail.com</a>&gt;</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) =&gt; MyIO c a -&gt; IO a<br>runFoo (MyIO x) = x<br><br>runBar :: (CtxBar c) =&gt; MyIO c a -&gt; IO a<br>


runBar (MyIO x) = x<br><br>And then an action that runs in context &#39;Foo&#39; :<br><br>someAction :: (CtxFoo c) =&gt; MyIO c ()<br>someAction = putStrLn &quot;FOO&quot;<br><br>Then I run it :<br><br>main = runFoo someAction<br>


<br>But obiously, GHC complains that my type &#39;c&#39; remains uninstantiated :<br><br>    Ambiguous type variable `c&#39; in the constraint:<br>      (CtxFoo c) arising from a use of `runFoo&#39;<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&#39;: 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) =&gt; MyIO c ()<br>someAction2 = putStrLn &quot;FOO and BAR&quot;<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>