Terri Brasher
Terri Brasher

Reputation: 75

Prolog program- implementing rules and list matching

I am trying to write a program in Prolog that would recognize 'is a' statements and apply the transitive property in inquired. For example:

Input: Mary is a girl.
Output: ok.
Input: A girl is a human.
Output: ok.
Input: Is Mary a human?
Output: yes.

Here's my code:

begin :-
    begin(Input).

begin(Input) :-
    write('Begin.. '),
    write('\n'),
    readln(Input),
    tokenize_atom(Atom, List),
    rules(List).
    begin.

rules(['A', Subj, is, a, What]) :-
    asserta(a(Subj, What)),
    write('ok'),
    write('\n').

rules([Subj, is, a, What]) :-
    asserta(is(Subj, What)),
    write('ok'),
    write('\n').

rules(['Is', Subj, a, What]) :-
    (is(Subj, Z) ; a(Z, What)) -> (write('Yes.'), nl) 
    ; (write('Unknown.'), nl).

It doesn't go into any of the cases, it will just say true and terminate when given a statement. What am I doing wrong?

Upvotes: 0

Views: 377

Answers (1)

Daniel Lyons
Daniel Lyons

Reputation: 22803

Prolog itself wants to help you. Look at the helpful messages you get:

|:     begin :-
|:      begin(Input).
Warning: user://1:9:
        Singleton variables: [Input]

This is pointing out that this rule is no different from:

begin :- begin(_).

This is highlighting that the role of Input in begin/1 is ambiguous. Is it input or output? If you can use it like this, it must be output, and that's consistent with how it's used in its definition, but look at the other problem you have there:

|:     begin(Input) :-
|:     write('Begin.. '),
|:     write('\n'),
|:     readln(Input),
|:     tokenize_atom(Atom, List),
|:     rules(List).
Warning: user://1:14:
        Singleton variables: [Atom]

Where did Atom and List come from? Presumably you wanted one of those to show up from readln/1. What's actually going on here is that you're asking Prolog to input a value, which you then return, having done nothing with it; meanwhile Prolog materializes Atom and List from thin air and uses them with your rules/1 predicate. So clearly something isn't hooked up here that should be.

Then you have an obvious typo:

|:         begin.
Warning: user://1:22:
        Clauses of begin/0 are not together in the source-file

I suspect you meant a comma after rules(List) instead of a period.

Try fixing those problems and see if you make some progress.

Upvotes: 1

Related Questions