I'm curious if Logic Programs can do algebra

前端 未结 4 1450
陌清茗
陌清茗 2021-01-02 06:32

I read a brief article about Prolog and Logic Programming. I\'m curious if Logic Programs can do algebra. Like would you be able to ask what the Variable of X is in the equa

4条回答
  •  独厮守ぢ
    2021-01-02 07:24

    All serious Prolog systems provide constraint logic programming over finite domains, called CLP(FD) for short, with which you can solve many such equations easily. For example, with SICStus Prolog, SWI and Yap:

    ?- use_module(library(clpfd)).
    true.
    
    ?- 5+X #= 7.
    X = 2.
    

    Apparently, the answer is 2 instead of -2. Also check out constraint logic programming over other domains, like the rationals with library(clpq).

提交回复
热议问题