Logic in Prolog

How can I express the following 3 sentences in Prolog?

All summers are warm. If it not summer, then it is winter. Now it is winter.
+5
source share
4 answers

Good question. Like @larsman (well, it seems to me, @FredFoo, I think) can be a big topic. And his answer is really good.

How can your question be caused by the need for a custom language (using one of the main Prolog applications), here I offer syntactic sugar for fictitious DSL (which means that it is completely empty now ...)

:- op(500, fx, all).
:- op(500, fx, now).
:- op(600, xfx, are).
:- op(700, fx, if).
:- op(399, fx, it).
:- op(398, fx, is).
:- op(397, fx, not).
:- op(701, xfx, then).

all summers are warm.
if it is not summer then it is winter.
now it is winter.

SWI-Prolog is kind enough to redden these operations that are stored, that is, can be easily requested. These are higher priority words declared: that is now.

?- now X.
X = it is winter.
+4
source

, . -

warm :- summer.
winter.

" , " - , . , -

winter :- \+ summer.

Prolog , , , , .

+2
winter(now).

warm(X) :- summer(X). 
summer(X) :- \+ winter(X).
winter(X) :- \+ summer(X).

It would be one way to do this.

In action:

6 ?- summer(now).
false.
7 ?- summer(tomorrow).
ERROR: Out of local stack
8 ?- warm(now).
false.
0
source

Here is a solution without using negation, not the universe of seasons indicated.

season(summer).
season(winter).

now(winter).

warm(S) :-
    season(S),
    S = summer.

Some sample queries:

?- now(S).
S = winter ;
false.

?- now(S), warm(S).
false.

?- warm(S).
S = summer ;
false.
0
source

All Articles