I have a list of values โโ(or functions) of any type. I have another list of functions of any type. The user at runtime selects one from the first list and the other from the second list. I have a mechanism for ensuring the compatibility of two elements (the value or output from the first is compatible with the input of the second).
I need to somehow call a function with a value (or compose a function). If the second function has specific types, unsafeCoerce works fine. But if it has the form:
polyFunc :: MyTypeclass a => a -> IO () polyFunc x = print . show . typeclassFunc x
Then unsafeCoerce does not work, because it cannot solve a specific type.
Is there a way to do what I'm trying to do?
Here is an example of how the lists look. However ... I am not limited to this, if there is any other way to present them that will solve the problem, I would like to know. It is important to consider the following: the list may change at run time, so at compile time I do not know all the possible types that can be involved.
data Wrapper = forall a. Wrapper a firstList :: [Wrapper] firstList = [Wrapper "blue", Wrapper 5, Wrapper valueOfMyTypeclass] data OtherWrapper = forall a. Wrapper (a -> IO ()) secondList :: [OtherWrapper] secondList = [OtherWrapper print, OtherWrapper polyFunc]
Note. As for why I want to do such a crazy thing: I generate code and test it with a hint. But this happens at runtime. The problem is that the hint is slow, actually doing things, and high performance is crucial. Also, at least in some cases, I donโt want to generate code and run it through ghc at runtime (although we did it too). So ... Iโm trying to find somewhere in the middle: dynamically connecting things without creating code or compiling, but starting it at compiled speed instead of interpreted.
source share