简体   繁体   English

加载动态Haskell模块

[英]Loading dynamic haskell module

I'm looking for a way to load a Haskell function from a string to run. 我正在寻找一种从字符串加载Haskell函数以运行的方法。 I know the type before hand, but don't know the contents of the function. 我事先知道类型,但是不知道函数的内容。

Ideally, a solution would be quick and not need to run in IO. 理想情况下,解决方案将是快速的,并且不需要在IO中运行。

I've been looking at hint (Language.Haskell.Interpreter), but it doesn't fit bill (eval calls show, modules must be in files). 我一直在查看提示(Language.Haskell.Interpreter),但是它不适合使用帐单(显示eval调用,模块必须在文件中)。

Any help would be appreciated. 任何帮助,将不胜感激。

hint and plugins are the main options. hintplugins是主要选项。 hint lets you interpret functions as bytecode, plugins uses compiled object code. hint使您可以将函数解释为字节码, plugins使用已编译的目标代码。

Note that since these 'eval' functions must be type-checked prior to running them, they're rarely pure values, as evaluation may fail with a type error. 请注意,由于这些“ eval”函数必须在运行之前进行类型检查,因此它们很少是纯值,因为评估可能会因类型错误而失败。

The abstract answer is that you just have to make (->) an instance of Read (and possibly Show while you're at it) 抽象的答案是,您只需要使(->)成为Read的实例(并且可能在显示显示 )。

How on earth you are supposed to do that, I don't know. 我不知道你到底应该怎么做。 It's no small task to interpret code. 解释代码绝非易事。

If you are dealing with simple functions, the I would suggest creating an algebraic data type to represent them. 如果您要处理简单的函数,则建议创建一个代数数据类型来表示它们。

data Fun = Add | Subtract | Multiply deriving (Eq, Show, Read)

runFun Add      = (+)
runFun Subtract = (-)
runFun Multiply = (*)

*Main> runFun (read "Add") 2 3
5
*Main> runFun (read "Multiply") 2 3
6
*Main> runFun (read "Subtract") 2 3
-1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM