Does Prolog have an if and only if statement?

Usually I write a statement like two sentences, for example:

x :- y.
y :- x.

In Prolog, is there any compressed way to write an instruction like "if x, then y and vice versa"?

In other words, is it possible to write "x if and only if y" as a single statement?

+4
source share
3 answers

You said that you usually write "p if and only if q" (I will write q <=> q ​​in the following) as

p :- q.
q :- p.

but this is only true if

  • no other suggestions for p and q
  • variables are not involved

, , p q , , .

,

p(X) :- q(X).
q(X) :- p(X).

forall X: p(X) <= q(X)  and  forall X': q(X') <= p(X')

forall X,X':  p(X) <= q(X)  and  q(X') <= p(X')

,

forall X:  p(X) <= q(X)  and  q(X) <= p(X)

, , " ".

p(X,Y) :- q(X,Z).

forall X,Y  exists Z:  p(X,Y) <=> q(X,Z)

q/2 , . , ..


- , , " p <= > q?", p q . -

p_iff_q(X) :- p(X) <=> q(X).

Prolog

p_iff_q(X) :- p(X),q(X) ; \+p(X),\+q(X).
+5

, swi-prolog.

, , , :

:- dynamic([system:term_expansion/2]).

:- op(900, xfx, user:(:::)).

system:term_expansion(:::(H,T),[H :- T, T :- H]) :-
    callable(H),
    callable(T).

x ::: y.

, , :::, (op/3) term_expansion/2 .


, , , . . Prolog - , : , .

+3

In Prolog, you can submit if-and-only-if rules using Restriction Processing Rules .

For example, in SWI-Prolog:

:- use_module(library(chr)).

:- chr_constraint class/2,species/2,animal/1,mammal/1,bird/1.

%X can be a member of only one species
species(X,X1),species(X,X2) <=> X1 = X2.

%X can be a member of only one class
class(X,X1),class(X,X2) <=> X1 = X2.

%X is a bird if and only if X is an animal and a pelican or a pigeon
bird(X) <=> animal(X),class(X,bird),(species(X,pelican);species(X,pigeon)).

%X is a mammal if and only if X is an animal
mammal(X) <=> animal(X),class(X,mammal),(species(X,cat);species(X,dog);species(X,bear)).
+1
source

Source: https://habr.com/ru/post/1538296/


All Articles