Note that there are some explanatory texts on larger screens.

plurals
  1. POIn Haskell, is there some way to forcefully coerce a polymorphic call?
    primarykey
    data
    text
    <p>I have a list of values (or functions) of any type. I have another list of functions of any type. The user at runtime will choose one from the first list, and another from the second list. I have a mechanism to ensure that the two items are type compatible (value or output from first is compatible with input of second).</p> <p>I need some way to call the function with the value (or compose the functions). If the second function has concrete types, unsafeCoerce works fine. But if it's of the form:</p> <pre><code>polyFunc :: MyTypeclass a =&gt; a -&gt; IO () polyFunc x = print . show . typeclassFunc x </code></pre> <p>Then unsafeCoerce doesn't work since it can't resolve to a concrete type.</p> <p>Is there any way to do what I'm trying to do?</p> <p>Here's an example of what the lists might look like. However... I'm not limited to this, if there is some other way to represent these that will solve the problem, I would like to know. A critical thing to consider is that: the list can change at runtime so I do not know at compile time all the possible types that might be involved.</p> <pre><code>data Wrapper = forall a. Wrapper a firstList :: [Wrapper] firstList = [Wrapper "blue", Wrapper 5, Wrapper valueOfMyTypeclass] data OtherWrapper = forall a. Wrapper (a -&gt; IO ()) secondList :: [OtherWrapper] secondList = [OtherWrapper print, OtherWrapper polyFunc] </code></pre> <p>Note: As for why I want to do such a crazy thing: I'm generating code and typechecking it with hint. But that happens at runtime. The problem is that hint is slow at actually executing things and high performance for this is critical. Also, at least in certain cases, I do not want to generate code and run it through ghc at runtime (though we have done some of that, too). So... I'm trying to find somewhere in the middle: dynamically hook things together without having to generate code and compile, but run it at compiled speed instead of interpreted.</p>
    singulars
    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.
 

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