Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's a horrible two typeclass solution. The first part (named, unhelpfully, <code>foo</code>) will take things of types like <code>Double -&gt; Double -&gt; CString -&gt; IO ()</code> and turn them into things like <code>IO (Double -&gt; IO (Double -&gt; IO (String -&gt; IO ())))</code>. So each conversion is forced into IO just to keep things fully uniform.</p> <p>The second part, (named <code>cio</code> for "collapse io) will take those things and shove all the <code>IO</code> bits to the end.</p> <pre><code>class Foo a b | a -&gt; b where foo :: a -&gt; b instance Foo (IO a) (IO a) where foo = id instance Foo a (IO b) =&gt; Foo (CString -&gt; a) (IO (String -&gt; IO b)) where foo f = return $ \s -&gt; withCString s $ \cs -&gt; foo (f cs) instance Foo a (IO b) =&gt; Foo (Double -&gt; a) (IO (Double -&gt; IO b)) where foo f = return $ \s -&gt; foo (f s) class CIO a b | a -&gt; b where cio :: a -&gt; b instance CIO (IO ()) (IO ()) where cio = id instance CIO (IO b) c =&gt; CIO (IO (a -&gt; IO b)) (a -&gt; c) where cio f = \a -&gt; cio $ f &gt;&gt;= ($ a) {- *Main&gt; let x = foo (undefined :: Double -&gt; Double -&gt; CString -&gt; IO ()) *Main&gt; :t x x :: IO (Double -&gt; IO (Double -&gt; IO (String -&gt; IO ()))) *Main&gt; :t cio x cio x :: Double -&gt; Double -&gt; String -&gt; IO () -} </code></pre> <p>Aside from being a generally terrible thing to do, there are two specific limitations. The first is that a catchall instance of <code>Foo</code> can't be written. So for every type you want to convert, even if the conversion is just <code>id</code>, you need an instance of <code>Foo</code>. The second limitation is that a catchall base case of <code>CIO</code> can't be written because of the <code>IO</code> wrappers around everything. So this only works for things that return <code>IO ()</code>. If you want it to work for something returning <code>IO Int</code> you need to add that instance too.</p> <p>I suspect that with sufficient work and some typeCast trickery these limitations can be overcome. But the code is horrible enough as is, so I wouldn't recommend it.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload