English 中文(简体)
Help with prolog s clauses
原标题:
emissionOf(alpha).
emissionOf(beta).

detected(proton), detected(electron) :- emissionOf(alpha), emissionOf(beta).

I m facing the problem that for some (probably obvious) reason Prolog doesn t accept the last line of the above code. The reason has something to do with:

detected(proton), detected(electron)

If I try just

detected(proton)

it behaves correctly.

What is the problem? I am trying to state that if there is both emission of alpha and beta, then there there are detections of both protons and electrons.

Thanks

最佳回答

You are correct, clauses in PROLOG are Horn clauses. Furthermore, it so happens that a conjunction in the head of a clause such as:

detected(proton), detected(electron) :- emissionOf(alpha), emissionOf(beta).

...(which is not a Horn clause) is in fact equivalent to the following two separate Horn clauses:

detected(proton) :- emissionOf(alpha), emissionOf(beta).
detected(electron) :- emissionOf(alpha), emissionOf(beta).

...since both facts detected(proton) and detected(electron) are implied by the conjunction of the body goals.

Note that there may be several other equivalent ways to encode what you intend the program to mean, such as the following (as an example):

emissionOf(alpha).
emissionOf(beta).

detected(X) :- 
  emissionOf(alpha), 
  emissionOf(beta), 
  (X = proton; X = electron).

Executing goal detected(X) will bind X to atom proton, then to electron on backtracking.

问题回答

It s equivalent to

,(detected(proton),detected(electron)) :- emissionOf(alpha), emissionOf(beta).

You are essentially trying to redefine the ,/2 operator, which is not allowed. What error message did you get?

What is it you are trying to say? That both detected(proton) and detected(electron) are true if emissionOf(alpha) and emissionOf(beta) are true? In that case you need to split in into two clauses as @sharky said:

detected(proton) :- emissionOf(alpha), emissionOf(beta).
detected(electron) :- emissionOf(alpha), emissionOf(beta).

This will give you both solutions on backtracking. I think this is definitely clearer then his second suggestion.

From what was told to me, it is inherent to Prolog s algorithm. Each stated sentence must be a Horn clause.





相关问题
Prolog : Learning by example

I am trying to learn a little bit about swi-prolog (beyond the basic, useless programs). Can anyone explain (perhaps in pseudocode) what this sudoku solver and the related functions are doing? If ...

Working with lists in Prolog

First off let me state that this is part of a class exercise given as homework. But, the entire assignment is much more involved than the subject of this question. So.. I am searching through two ...

SWI-Prolog conditional statements

I m trying to write a function that will test to see if the word hello is contained in a list. If it is contained, i don t want it to say "true", i want it to say : "yes, the word hello is contained ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [...

Max out of values defined by prolog clauses

I know how to iterate over lists in Prolog to find the maximum, but what if each thing is a separate clause? For example if I had a bunch of felines and their ages, how would I find the oldest kitty? ...

热门标签