how to do Arithmetic Operations in DCG in prolog

为君一笑 提交于 2019-12-12 18:18:58

问题


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

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

(...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?


回答1:


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

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).


来源:https://stackoverflow.com/questions/34226727/how-to-do-arithmetic-operations-in-dcg-in-prolog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!