Home > Archive > Prolog > June 2007 > exercise in prolog
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
exercise in prolog
|
|
| pachanguero 2007-05-17, 8:04 am |
| hi!
i got a little problem. we have to implement a program for prolog.
that's the situation:
we got some person who say something about another person. for
example:
a says: b lies when c says the truth.
b says: a says the truth.
c says: b lies.
that's only an example. our task is harder.
anyone has a hint for me? shall I use lists? in addition, we should
use the facts: bool(true) and bool(fail).
thanks for every answer!
| |
| Mauro Di Nuzzo 2007-05-17, 8:04 am |
| This could be the syntax... try to implement semantics by yourself.
:- op(500, xfy, says).
:- op(400, yf, lies).
:- op(400, yf, says_the_truth).
:- op(450, xfy, when).
person(a).
person(b).
person(c).
sentence(lies(Who)) :-
person(Who).
sentence(says_the_truth(Who)) :-
person(Who).
sentence(when(If, Then)) :-
sentence(If),
sentence(Then).
says(Who, What) :-
person(Who),
sentence(What).
Ciao
"pachanguero" <boehrs@gmail.com> ha scritto nel messaggio
news:1179395859.385898.254650@u30g2000hsc.googlegroups.com...
> hi!
> i got a little problem. we have to implement a program for prolog.
> that's the situation:
> we got some person who say something about another person. for
> example:
>
> a says: b lies when c says the truth.
> b says: a says the truth.
> c says: b lies.
>
> that's only an example. our task is harder.
>
> anyone has a hint for me? shall I use lists? in addition, we should
> use the facts: bool(true) and bool(fail).
>
> thanks for every answer!
>
| |
| Markus Triska 2007-05-17, 7:06 pm |
| "Mauro Di Nuzzo" <picorna@inwind.it> writes:
> person(a).
> person(b).
> person(c).
You can bridge this indirection by delegating variable instantiations
to Prolog. For example, with your operator declarations, the program
:- op(300, fx, a).
a liar says a nobleman lies.
a nobleman says a nobleman says_the_truth.
a nobleman says a nobleman lies when a liar says_the_truth.
already suffices to derive one solution:
?- A says B lies when C says_the_truth, B says A says_the_truth,
C says B lies.
%@ A = a nobleman,
%@ B = a nobleman,
%@ C = a liar
A simple meta-interpreter can solve more deeply nested puzzles:
true(a lier lies).
true(a nobleman says_the_truth).
...etc.
true(a nobleman says X) :- true(X).
true(a liar says X) :- false(X).
false(a nobleman lies).
false(a liar says_the_truth).
...etc.
--
comp.lang.prolog FAQ: http://www.logic.at/prolog/faq/
| |
|
|
|
|
|
|
|