在 Haskell 中确定运行时函数参数的兼容性
假设我有一个函数 f1 :: a -> b-> ...-> c 和另一个函数 f2 :: MaybeTypeClass xy ... => x-> y-> ...-> z,我想在运行时确定 c(f1 的输出)是否可以作为 x 或 y 或任何其他输入传递给 f2。
我确实对源代码有一定的控制权,所以如果有一种方法可以生成元数据(模板 haskell,也许?)以供在运行时使用,那就没问题了。
换句话说......
我有许多具有一组输入和输出的函数(可能认为是类似箭头的计算事物,可能是有状态的)。我想知道哪些输入能够与哪些输出连接,以便用户可以将它们连接在一起。一旦发生这种情况,我们就可以根据用户的配置方式生成代码(或类似的东西)。
我拥有的函数要么是我自己编写的,要么用户也可以编写它们,所以我可能会以编译的形式获得它们。但如果非常简单,我可以要求他们的代码中的某些内容来生成元数据(如果需要)。
最大的挑战是类型类——如何确定输出的具体类型是否是输入的类型类的实例。
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 to 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从你对问题的评论来看,我猜你想要的是 GHC API 或提示之类的东西。这两种方法都可以访问您刚刚编译的代码的某些 AST 表示形式。 GHCi 基于 GHC API,提示是 GHC API 的一个很好的包装。
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.
我想,您想要的可以通过模块
Data.Dynamic
实现。它提供了代数数据类型Dynamic
来进行动态编程。尽管您只能使用单态类型,但该模块可以保存以供使用。I guess, what you want is possible with the module
Data.Dynamic
. It provides an algebraic data typeDynamic
to do dynamic programming. The module is save to use, though you may only use monomorphic types.