Task:

  • Three friends - Peter, Roman and Sergey study at the Faculty of Mathematics, Physics and Chemistry.
  • If Peter is a mathematician, then Sergey is not a physicist.
  • If Roman is not a physicist, then Peter is a mathematician.
  • If Sergey is not a mathematician, then Roman is a chemist.

Determine the specialty of Sergei.

Code:

domains dep=symbol predicates faculty(dep) cond1(dep, dep) cond2(dep, dep) cond3(dep, dep) solve(dep, dep, dep) % Peter, Roman, Sergey clauses faculty(physics). faculty(math). faculty(chemistry). cond1(Fa1, Fa3):- Fa1=math, Fa3<>physics. cond2(Fa2, Fa1):- Fa2<>physics, Fa1=math. cond3(Fa3, Fa2):- Fa3<>math, Fa2=chemistry. solve(Fa1, Fa2, Fa3):- faculty(Fa1), faculty(Fa2), faculty(Fa3), Fa1<>Fa2, Fa2<>Fa3, Fa1<>Fa3, cond1(Fa1, Fa3); cond2(Fa2, Fa1); cond3(Fa3, Fa2). 

When you run this code, the following error pops up:

enter image description here

How can I correct this error and does this problem have any other solution on the prolog?
The answer to the problem should be this: Peter is a chemist, Roman is a physicist, Sergey is a mathematician

  • and where are you trying to run? - Grundy
  • In principle, faculty should be in the facts section - Grundy
  • @Grundy, in DOSBox via prolog.exe, turbo prolog. - enkelad
  • Yes, I just wanted to know the Turbo Prolog version - Grundy
  • @Grundy turbo prolog 1.1 - enkelad

1 answer 1

Use instead of <> construction not (a = b).

 domains dep=symbol predicates faculty(dep) cond1(dep, dep) cond2(dep, dep) cond3(dep, dep) solve(dep, dep, dep) % Peter, Roman, Sergey clauses faculty(physics). faculty(math). faculty(chemistry). cond1(Fa1, Fa2):- Fa1=math, not(Fa2=physics). cond1(Fa1, _):- not(Fa1=math). cond2(Fa1, Fa2):- not(Fa1=physics), Fa2=math. cond2(physics, _). cond3(Fa1, Fa2):- not(Fa1=math), Fa2=chemistry. cond3(math, _). solve(Fa1, Fa2, Fa3):- faculty(Fa1), faculty(Fa2), faculty(Fa3), Fa1<>Fa2, Fa2<>Fa3, Fa1<>Fa3, cond1(Fa1, Fa3), cond2(Fa2, Fa1), cond3(Fa3, Fa2). goal solve(Fa1, Fa2, Fa3), write(Fa1),nl, write(Fa2),nl, write(Fa3). 
  • Thank you very much. And why, after each rule, we still insert a predicate with the same name? - enkelad
  • To handle the situation when the condition is not met: for example, if Peter is not a mathematician, then Sergei is also a physicist. The prologue in the search will take into account such situations. - Oleg Brezhnev