If I have the below piece of code, how would I make it produce Answer= 5 and Answer2= 10?
. I run the goal ?- test(Data),lpsolve(Data, [Answer1,Answer2]).
:-use_module(library(clpfd)).
test([the, variable, X, is, five,fullstop,
the,variable, Y, is, ten, fullstop]).
lpsolve(Data, [Answer,Answer2]):- sentence(Answer, Data,[]).
sentence(X) --> nounphrase, verbphrase(X).
nounphrase --> [the], [variable].
verbphrase(X) --> [X], [is], [five],[fullstop], {X = 5}.
sentence(Y) --> nounphrase, verbphrase(Y).
nounphrase --> [the], [variable].
verbphrase(Y) --> [Y], [is], [ten],[fullstop], {Y = 10}.
Example of a program that actually runs and is closely related is the following:
:-use_module(library(clpfd)).
test([the, variable, X, is, five,fullstop]).
lpsolve(Data, Answer):- sentence(Answer, Data,[]).
sentence(X) --> nounphrase, verbphrase(X).
nounphrase --> [the], [variable].
verbphrase(X) --> [X], [is], [five],[fullstop], {X = 5}.
I have just one sentence to test and the goal succeeds as shown below.
?- test(Data),lpsolve(Data, Answer).
Data = [the, variable, 5, is, five],
Answer = 5.
EDIT
I try the following as per the first comment:
:-use_module(library(clpfd)).
test([the, variable, x, is, five,fullstop,
the,variable, y, is, ten, fullstop]).
lpsolve(Data, Answer):- sentence(Answer, Data,[]).
sentence(X) --> nounphrase, verbphrase(X).
nounphrase --> [the], [variable].
verbphrase(X) --> [x], [is], [five],[fullstop], {X = 5}.
verbphrase(Y) --> [y], [is], [ten],[fullstop], {Y = 10}.
I get the following:
-? test(Data),lpsolve(Data, Answer).
false.
I'm not really sure what you're trying to do here, but I feel your DCG is broken down in completely strange ways and you may benefit from seeing another way to arrange it.
You have a list of variable bindings, so you should already be thinking in terms of obtaining a list of results rather than a single result:
What is a sentence? It is a noun phrase and a verb phrase, in your simple system. But you have broken the noun and verb phrases apart incorrectly for English, where a sentence like "The variable X is 5" should be broken into a noun phrase subject "The variable X" and a verb phrase "is 5". Ideally, the verb phrase should be decomposed further into a verb another noun phrase, but we're ignoring that detail for now. I am looking for the verb "is" to relate it to the Prolog predicate
=/2
, and handlingfullstop
here:OK, so now we need your noun phrase:
And your verb phrase:
I'm only handling the two decodings and I'm doing it implicitly in the DCG definition:
You'll find this works for the use case you've defined above: