将Prolog集成到C程序中
Integrate Prolog in C program
我想用C++编写一个主程序,程序会动态生成事实来构建知识库,然后程序可以调用一些.pl
文件中定义的规则并得到结果。例如主.c
int main()
{
//generate facts like
// blue(car), red(bike), etc
// do queries
PL_call( "consult( 'pred.pl' )" );
...
PL_call(goal_term, NULL);
}
pred.pl
whatisblue(X) :- blue(X).
whatisred(X) :- red(X).
我该怎么做?
我在 C 界面上阅读了 swi-prolog 手册,但只看到如何在 Prolog 中调用 C 模块或在 C 中调用 Prolog,我没有看到如何混合在 C 和 Prolog 中定义的谓词。可能吗?谢谢。
现在我
。
不可能有一个详细的答案,但总的来说,你的问题的答案是"是"。
只需在C++中使用C++界面。它比C容易得多。所以使用PlCall,PlTermv等...
问题是在 PlCall(s) 之前和之后正确绑定变量,以便您可以使用 Prolog 交换值。
例如,为了简化结构化值的构造,请查看这些宏注释、谓词0、谓词 1、...和结构1,结构2,...,或考虑
#define unary(X) PlTerm X; PlCompound X ## _t(#X, X);
允许输入代码
attrs2format_t::const_iterator p = attrs2format.find(k);
if (p == attrs2format.end()) {
// attributes documented here:
// http://www.swi-prolog.org/pldoc/doc_for?object=prolog_colour:syntax_colour/2
unary(colour)
unary(background)
unary(bold)
unary(underline)
// use unification to match list' elements
PlTail attrs(attr_list);
PlTerm attr;
...
你可以在这里找到一些关于连接SWI-Prolog的一般说明
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 在C应用程序中运行C++(带有STL)函数
- 编译C++使用SWI-Prolog接口的程序
- 将Prolog集成到C程序中