Say I have a function f1 :: a -> b -> ... -> c and another function f2 :: PossibleTypeClass x y ... => x -> y -> ... -> z, I want to determine at runtime if c (output of f1) can be passed in to f2 as x or y or any other input.
I do have some control over the source, so if there is a way to generate metadata (template haskell, maybe?) for use at runtime, that would be fine.
In other words...
I have a number of functions (maybe think arrow-like, computation things, may be stateful) that have a set of inputs and outputs. I want to know which inputs are able t开发者_如何转开发o hook up with which outputs so the user can tie them together. Once that happens, we can generate code (or something like that) based on how the user configured them.
The functions I have I either wrote myself, or users can write them as well, so I might get them in compiled form. But I could require certain things in their code to generate metadata (if necessary) if it was very simple.
The biggest challenge is with type classes--how to find out whether the concrete type of an output is an instance of a type class of an input.
From your comment on your question, I guess what you are after is something like GHC API or hint. Both are ways to access some AST representation of code you just compiled. GHCi is based on GHC API and hint is a nice wrapper around GHC API.
I guess, what you want is possible with the module Data.Dynamic
. It provides an algebraic data type Dynamic
to do dynamic programming. The module is save to use, though you may only use monomorphic types.
精彩评论