简体   繁体   English

我可以在运行时添加谓词吗?

[英]Can I add predicates at runtime?

The context is a system of predicates: rules and facts, that together determine an amount to be paid to a client, depending on his situation.上下文是一个谓词系统:规则和事实,它们根据客户的情况共同决定支付给客户的金额。 Most rules and facts are known at compile time, but facts about a specific situation are only known at runtime, when processing a pay out request.大多数规则和事实在编译时是已知的,但有关特定情况的事实仅在运行时处理支付请求时才知道。 I know how to program a system in Mercury in the case that all predicates are known at compile time (ie are part of the source code), but I don't know how to introduce predicates at runtime.我知道如何在 Mercury 中对系统进行编程,以防所有谓词在编译时都已知(即是源代码的一部分),但我不知道如何在运行时引入谓词。 Is it possible to write a function that constructs a predicate with the given parameters and then be able to use the constructed predicate as part of the context against which amounts are calculated?是否可以编写一个 function 来构造具有给定参数的谓词,然后能够将构造的谓词用作计算金额的上下文的一部分?

I am not sure exactly what your question is.我不确定你的问题是什么。

If by "construct a predicate with the given parameters", you are asking whether you can construct a closure that contains a reference to a predicate in the originally compiled program and a list of values for its initial arguments that are constructed at runtime, then the answer is "yes, you can do that".如果通过“使用给定参数构造谓词”,您是在询问是否可以构造一个闭包,该闭包包含对原始编译程序中谓词的引用以及在运行时构造的其初始 arguments 的值列表,那么答案是“是的,你可以做到”。

If you mean whether you can construct at runtime a predicate whose code is NOT part of the originally compiled program, then the answer is "no, you can't do that" in Mercury.如果您的意思是您是否可以在运行时构造一个谓词,其代码不是最初编译程序的一部分,那么在 Mercury 中答案是“不,您不能这样做”。 However, it is possible to work around this, by但是,可以通过以下方式解决此问题

  • writing the code of the new predicate to a new Mercury module;将新谓词的代码写入新的 Mercury 模块;
  • compiling that module to object code;将该模块编译为 object 代码;
  • using dynamic linking to make the new object file part of the existing executable;使用动态链接使新的 object 文件成为现有可执行文件的一部分; and
  • using the foreign code interface to transfer a target-language C pointer, or its equivalents in other languages, to the original code, for it to call.使用外部代码接口将目标语言 C 指针或其他语言的等价物传输到原始代码,以供其调用。

However, this method is not recommended for anyone who doesn't have an implementor's understanding of how the Mercury implementation works.但是,对于没有实现者对 Mercury 实现如何工作的理解的任何人,不建议使用此方法。

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

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