Search code examples
prologoperatorsdcgclpfd

how to do Arithmetic Operations in DCG in prolog


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?


Solution

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