如何在序言中在DCG中进行算术运算 [英] how to do Arithmetic Operations in DCG in prolog

查看:95
本文介绍了如何在序言中在DCG中进行算术运算的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

verb_phrase(X,P)--> trans_verb(X,X+1,P1), noun_phrase(X+1,P1,P).

对于上面的代码,如果X = 1,我将得到

For the code above, if X=1, I will get

(...1+1...).

"..."表示不重要的代码. 但我真的想得到2而不是1 + 1. 有人可以告诉我该怎么做吗?

"..."means not important code. but I really want to get 2 instead of 1+1. Could someone tell me how to do it?

推荐答案

如果要对整数进行推理,最干净的方法是使用CLP(FD)约束进行算术运算.

If you are reasoning over integers, the cleanest way is to use CLP(FD) constraints for arithmetic.

您可以在DCG中使用{}/1嵌入Prolog目标.例如:

You can use {}/1 within DCGs to embed Prolog goals. For example:

:- use_module(library(clpfd)).

verb_phrase(X0, P)--> { X #= X0 + 1 }, trans_verb(X0, X, P1), noun_phrase(X, P1, P).

这篇关于如何在序言中在DCG中进行算术运算的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆