String comparison program in swi-prolog always fails - prolog

I'm trying to write a program using swi-prolog that randomly asks people for their first or last name and prints "correct" or "incorrect" based on what they type. The current correct answers are "Hello" and "World" but regardless of what the user types, the output is false and correct/incorrect isn't printed.
start:-(Q=['What is your first name?','What is your last name?'],
random_member(N,[0,1]),
nth0(N,Q,X),
writeln(X)),
readln(S),
check_answer(N,S).
check_answer(N,S):-A=['Hello','World'],
nth0(N,A,X),
writeln(X),
(S=#=X)->writeln('correct'),
not(S=#=X)->writeln('incorrect').
I later edited it to:
start:-(Q=['What is your first name?','What is your last name?'],
random_member(N,[0,1]),
nth0(N,Q,X),
writeln(X)),
read(S),
check_answer(N,S).
check_answer(N,S):-A=['Hello','World'],
nth0(N,A,X),
writeln(X),
writeln(S),
((S=#=X))->writeln('correct') ; writeln('incorrect').

I can spot two problems in your code.
a) readln/1 (undocumented) returns a list of items (then peek the first item or use read/1):
?- readln(X).
|: Hello.
X = ['Hello', '.'].
?- readln(X).
|: Hello
X = ['Hello'].
?- readln(X).
|: Hello .
X = ['Hello', '.'].
b) The pair of if then else operator (->) will always fail, because you omit the else branch on both, and the conditions are exclusives. Try
...
((S=#=X)->writeln('correct') ; writeln('incorrect')).
edit there are 2 problems. I wrongly suggested read/1. That read a Prolog term, and then read a variable if we write a variable, i.e. a UppercaseStartingSymbol. My fault. readln seems ok then, but change the pattern to select the first item.
Another problem, unrelated: you misplaced the closed parenthesis in -> 'statement'.
Here the code:
start:-(Q=['What is your first name?','What is your last name?'],
random_member(N,[0,1]),
nth0(N,Q,X),
writeln(X)),
readln(S),
check_answer(N,S).
check_answer(N,[S|_]):-A=['Hello','World'],
nth0(N,A,X),
writeln(X),
writeln(S),
((S=#=X)->writeln('correct') ; writeln('incorrect')).

Related

Words with repeating symbols in prolog v5.2

I need to write prolog predicate that reads file and creates a list of words with repeating symbols. For example from text:
A dog and an apple and a pipe.
the result should be:
['apple', 'pipe']
I wrote this:
domains
file_ = f
s=string
c=char
i=integer
list=s*
list1=c*
predicates
str_a_list(s,list)
readfile(s,s)
example(s)
write_symbols(list1)
search(list1,list1,list1)
check(list)
str_list(s,list1)
search1(list1,c,i,i)
clauses
readfile(S,N):-existfile(N),!,
openread(F,N),
readdevice(F),file_str(N,S).
str_a_list("",_):-!.
str_a_list(" ",_):-!.
str_a_list(S,[H|T]):-fronttoken(S,H,S1),
str_a_list(S1,T).
search1([],_,I,I):-!.
search1([H|T],H,I,M):-I1=I+1,search1(T,H,I1,M).
search1([H|T],X,I,M):-H<>X,search1(T,X,I,M).
search([],_,_):-!.
search([H|T1],L,L0):-search1(L,H,0,M),M>=2,write_symbols(L0).
search([_|T],L,L0):-search(T,L,L0).
write_symbols([]):-write(" "),!.
write_symbols([H|T]):-write(H),write_symbols(T).
str_list("",[]).
str_list(S,[H|T]):- frontchar(S,H,S1),str_list(S1,T).
check([]):-!.
check([H|T]):-str_list(H,L),search(L,L,L),check(T).
example(Y):-readfile(S,Y),str_a_list(S,L),check(L).
goal
write("Enter file name: "),
readln(Y),example(Y).
It's giving me this error:
This flow pattern doesn't exist openread(o,i)
on line:
openread(F,N)
I tried computing this prolog task, maybe you may find something useful in my solution that may help you with yours. I've written my code using basic prolog:
First: The first predicate separates the sentence into words. I have used the built-in function split_string.Example: "The dog" will become "The","dog".
g(B):-
split_string(B,' ','', L),
d(L).
Second: In the second predicate, we split each word into a separate list of characters. Example: "dog" will become ["d","o","g"].
stringsplit(A,L1):-
atom_chars(A, L1).
Third: Then check each list if it contains doubles. the base case predicate tell to stop when get empty brackets. The checkdouble second predicate checks if a character is in the remaining list (using member). If yes then load the character in List R. Else, don't load the character in R.
checkdouble([],[]):-!.
checkdouble([H|T],[H|R]):-
member(H,T),
checkdouble(T,R).
checkdouble([],[]).
checkdouble([H|T],List):-
\+member(H,T),
checkdouble(T,List).
Fourth: By this point you will have a number of list: empty and those containing duplicates from each word. Example: For [bat] [apple] [polo] we will get [][p][o].
So now we use a predicate that simply prints the list of words that have doubles ignoring those words with no doubles i.e [].
s(_,B):-
B=[].
s(D,B):-
B\=[],
write(D).
Finally: Putting the code together:
g(B):-
split_string(B,' ','', L),
d(L).
d([]).
d([H|T]):-
stringsplit(H,K),
checkdouble(K,R),
s([H],R),
d(T).
s(_,B):-
B=[].
s(D,B):-
B\=[],
write(D).
checkdouble([],[]):-!.
checkdouble([H|T],[H|R]):-
member(H,T),
checkdouble(T,R).
checkdouble([],[]).
checkdouble([H|T],List):-
\+member(H,T),
checkdouble(T,List).
stringsplit(A,L1):-
atom_chars(A, L1).
Example:
?-g("A dog and an apple and a pipe").
OUTPUT:
[apple][pipe]
1true
false
?-g("Two funny little red apples fell from a tree one day").
OUTPUT:
[funny][little][apples][fell][tree]
1true
false
?-g("On a hill upon the grass there sits a squirrel in the chill").
OUTPUT:
[hill][grass][there][sits][squirrel][chill]
1true
false

Prolog: How to read data from console and store into database. Getting errors

update :-
write("Name?:"),
read(Name),
assert(Name),nl,
write("Age?:"),
read(Age),
assert(Age),
write("Continue(y or n)?:"),
read(Respond),
process(Respond).
process(y) :-
write('Name?:'),
read(Name),
assert(Name),nl,
write("Age?:"),
read(Age),
assert(Age),
repeat,
write("y or n"),
read(Respond),
process(Respond).
process(n) :- !.
I want to run this Prolog to assert in the name and age, but when I write age for the number, it shows
?- update.
Name?:fred.
Age?:|: 25.
ERROR: Type error: `callable' expected, found `25' (an integer)
ERROR: In:
ERROR: [9] assert(25)
ERROR: [8] update at c:/example.pl:11
ERROR: [7] <user>
?-
How to figure out this problem.
Problem 1
Incorrect input for assert/1
The problem is not with just Age it is with any input that uses assert, e.g.
?- update.
Name?:Fred
|: .
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [9] assert(_4940)
ERROR: [8] update at c:/example.pl:8
ERROR: [7] <user>
?- update.
Name?:Jim.
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [9] assert(_5826)
ERROR: [8] update at c:/example.pl:8
ERROR: [7] <user>
The problem is that assert/1 is not being given a fact or rule.
assert/1 says:
Assert a clause (fact or rule) into the database.
See facts and rules
In the example above Fred is not a fact because it does not end with a period (.).
In the example above with Jim. a period was given but because Jim starts with a capital letter, it is not a fact or rule but a variable.
When the age is entered as a number, again this is not a fact or rule it is an integer.
Problem 2
Use of read/1 which says:
Read the next Prolog term from the current input stream and unify it with Term.
When reading a Prolog term the input must end with a period.
This not only requires the input to be a term, but end with a . which is even more confusing given the prompt, e.g Age. Most of the examples you find do what you did, the corrected code below does what you want.
Problem 3
Competing ways or repeating.
The code is using two ways:
Use of repeat/0
It is recursive, e.g.
process(y) :-
...
process(Respond).
This is making it hard to get the code working.
Problem 4
Duplicate code, e.g.
write("Name?:"),
read(Name),
assert(Name),nl,
write("Age?:"),
read(Age),
assert(Age),
write("Continue(y or n)?:"),
read(Respond),
process(Respond).
Duplicated code is more likely to lead to problems when one copy is corrected and the other copy is not corrected.
Problem 1 fix
Make the input a fact before storing in the database with assert/1, e.g.
Values in variables
Name
Age
Variables converted to facts by adding a functor
name(Name)
age(Age)
The facts used with assert/1
assert(name(Name))
assert(age(Age))
Problem 2 fix
Use read_string/5, e.g.
read_string(user, "\n", "\r", End, Name)
This reads the input into the variable Name as a string. Now that the input is a string, and not a Prolog term, the period is no longer required. There are predicates that operate on strings.
Problem 3 fix
Use the recursion form and remove repeat/0.
This could also use repeat/0 instead of recursion. The corrected code below uses recursion to demonstrate the change to process/1.
Problem 4 fix
Just refactor the code. You can see this in the corrected code at the end.
Now with the fixes in place.
Change 1
Since the input for continue is no longer a term, e.g. y or n, but a string, the parameter for process needs to be a string, e.g.
process("y") :-
process("n") :-
Change 2
Age will be asserted as a string but would be better asserted as an integer.
number_string/2 can solve this, e.g.
number_string(Age_n,Age),
assert(age(Age_n))
Change 3
user27815 Asked in a comment:
do you need the cut in process("n") :- !. ?
Since
process(Respond).
is not creating a choice point, the cut is not needed.
Corrected code:
update :-
% Respond will be read as a string and not as a term, so it needs "".
process("y").
process("y") :-
write('Name: '),
read_string(user, "\n", "\r", End, Name),
assert(name(Name)),
write("Age: "),
read_string(user, "\n", "\r", End, Age),
number_string(Age_n,Age),
assert(age(Age_n)),
write("Continue: (y or n) "),
read_string(user, "\n", "\r", End, Respond),
process(Respond).
process("n").
Example run:
?- update.
Name: Fred
Age: 30
Continue: (y or n) y
Name: Jim
Age: 21
Continue: (y or n) n
true.
To check that the database was updated use listing/1
?- listing(name/1).
:- dynamic name/1.
name("Fred").
name("Jim").
true.
?- listing(age/1).
:- dynamic age/1.
age(30).
age(21).
true.
A free enhancement.
Keeping the facts of name and age separate doesn't keep the relation between them intact. A better solution would be a person fact with both Name and Age values.
Here is the necessary modified code.
update :-
% Respond will be read as a string and not as a term, so it needs "".
process("y").
process("y") :-
write('Name: '),
read_string(user, "\n", "\r", End, Name),
write("Age: "),
read_string(user, "\n", "\r", End, Age),
number_string(Age_n,Age),
assert(person(Name,Age_n)),
write("Continue: (y or n) "),
read_string(user, "\n", "\r", End, Respond),
process(Respond).
process("n").
Example run:
?- update.
Name: Fred
Age: 30
Continue: (y or n) y
Name: Jim
Age: 21
Continue: (y or n) n
true.
To check that the database was updated use listing/1
?- listing(person/2).
:- dynamic person/2.
person("Fred", 30).
person("Jim", 21).
true.
After noticing your deleted answer.
In your deleted answer you have
?- person(name(N), age(A)).
N = nancy,
A = 22;
N= steve,
A = 100;
true.
The change needed for this variation of the fact to be created is
assert(person(name(Name),age(Age_n)))
however that might not be the optimal way to go.
In Prolog, positions typically indicate the meaning of a value, e.g. first position is name and second position is age. In this variation by adding the functors name and age to the fact person/2 you are duplicating known knowledge, but more importantly the possibility the amount of work Prolog has to do.
For example:
If the fact was person(Name,Age). to get at Name and Age Prolog only needs one unification. But with person(Name,Age). Prolog now needs to unify with person(name(nancy),age(22)) then to get Name has to unify again with name(nancy) and to get Age has to unify with age(22). You could also use person(name(Name),age(Age)). which requires only one unification, but now makes your code more verbose.
When first learning Prolog this crutch helps, but when working with larger data sets, this starts to impact performance.
Another item of note in your deleted answer is that the names of the people are still based on using read/1, e.g. nancy and steve. While a lot of Prolog examples do this, there is no requirement to keep them as such, they can be strings. Odds are the code will never need to exactly match on nancy or steve and instead will always reference them as a value in a variable. The nice thing about keeping them as strings is that when writing them out, they will appear correctly as Nancy and Steve.
This is because assert does not work on variables. It asserts a fact or rule; in other words, assert(something) asserts that something must be true.
From the SWI-Prolog documentation:
Assert a clause (fact or rule) into the database.
An integer value is not a rule or a fact. It is (in this case) an integer, not something that evaluates to a boolean value. There's no point in asserting a value.
I would write some helpers:
read_assert(P,V) :- format('~w ? ',[P]), read(V), A =.. [P,V], assert(A).
?- maplist(read_assert, [name,age], Vs).
name ? capellic.
age ? 99.
Vs = [capellic, 99].
?- name(N).
N = capellic.

Prolog programm returns yes instead of value

I got the following event: item(C,X,G,P), (where C is a number for the product,X it's name,G it's price,P it's cost).
When i use the command item(n3001,_,_,P) directly on the prolog console i get as answer
G = 1.25 X = 100 but when i write the equation p3(C)-: item(C,_,_,P). then i consult the text i get as answer yes.
My question clarified is how come the one time i get the value of P which i want and the other time i get whether it's true or false?
There are no return values in Prolog and p3/1 does not constitute a function but a relation. Your definition
p3(C) :-
item(C,_,_,P).
reads: If item(C,_,_,P) succeeds then p3(C) succeeds as well. For the sake of argument, let's assume that your code includes the following fact:
item(n3001,100,1.25,1).
If you query
?- p3(n3001).
Prolog unifies C in the head of your rule with n3001 and then tries your goal item(C,_,_,P) which succeeds. Hence the rule succeeds and Prolog tells you:
?- p3(n3001).
yes
If you want to know the price corresponding to n3001 you have to to define a rule where P appears in the head of the rule as well, e.g.:
p3(C,P) :-
item(C,_,_,P).
If you query that you'll get to see the value of P corresponding to n3001:
?- p3(n3001,P).
P = 1
If you query item/4 directly P appears in the arguments and therefore you get to see a substitution for it that satisfies your query:
?- item(n3001,_,_,P).
P = 1

If-else statement checking first item in list always returns false?

I have written a method which takes user input from the console and turns it into a list. Afterwards it enters an if statement where it checks if the first element of the list (or first word entered) is 'A'. If so, print 'Yes' else print 'No.' So far I have only been able to get it to print 'No' despite the user input.
start :-
read(String),
string_to_list(List, String),
(
nth0(0,List,'A') ->
writeln('Yes')
;
writeln('No.')
).
You seem to be using string_to_list/2 with swapped arguments. The first argument should be the string, and the second the list.
Then, to check if the first item is an 'A' you should use the code for that letter, not the atom 'A', that is 0'A.
start:-
read(String),
string_to_list(String, List),
(
nth0(0,List,0'A) ->
writeln('Yes')
;
writeln('No.')
).
Test:
?- start.
|: 'ABC'.
Yes
true.
?- start.
|: 'BCA'.
No.
true.

Prolog, how to show multiple output in write()

go :- match(Mn,Fn),
write('--Matching Result--'),
nl,
write(Mn),
write(' match with '),
write(Fn),
match(Mn1,Fn1).
person(may,female,25,blue).
person(rose,female,20,blue).
person(hock,male,30,blue).
person(ali,male,24,blue).
match(Mn,Fn):-person(Fn,'female',Fage,Fatt),
person(Mn,'male',Mage,Matt),
Mage>=Fage,
Fatt=Matt.
Hi,this is my code...but it's only can show the 1 output...but there are 3 pair of matching in match(X,Y).how to show them all in my go function.
Thank you
You get all your matches if you force backtracking, usually by entering ; (e.g. in SWI Prolog). But you also see that you are getting unnecessary outputs true. This is because the last clause in go is match(Mn1,Fn1). This clause succeeds three times and binds the variables Mn1,Fn1 but then only true is output, because you do not write() after that clause. The fourth time match(Mn1,Fn1) fails and by backtracking you come back to the first clause match(Mn,Fn) that matches, the match is output, etc.
You surely do not want to have this behavior. You should remove the last clause match(Mn1,Fn1) in go. Now by pressing ; you get the 3 matches without any output true in between.
But what you likely want is that the program does the backtracking. To achieve this, you just need to force backtracking by adding false as the last clause. To get proper formatting of the output, use the following program. The last clause go2. is added to get true at the very end.
go2 :- write('--Matching Result--'), nl,
match(Mn,Fn),
write(Mn), write(' match with '), write(Fn), nl,
fail.
go2.
This technique is called failure driven loop.
If you have any predicate that has multiple results and want to to find all of them, you should use findall/3
For example, in your case, you could do something like:
findall([X,Y], match(X,Y),L).
L will be a list that will contain all the X,Y that satisfy match(X,Y) in the format [X,Y].
for example, assuming that:
match(m1,f1).
match(m2,f2).
the result will be L = [ [m1,f1], [m2,f2] ]
note that you can define the format as you wish, for example you could write:
findall(pair(X,Y), match(X,Y), L).
L = [ pair(m1,f1), pair(m2,f2) ]
findall( X, match(X,Y), L).
L = [ m1, m2]
findall( 42, match(X,Y), L).
L = [42, 42]
then you have to recurse on the list to print them.
However, if you wish to find one result, run some code and then continue you could use forall/2:
forall(match(X,Y), my_print(X,Y).
Prolog is a lazy language. Which means that it will stop once it has found a condition that made your problem true. This will be the very first match alone.
IF your code is working (I haven't tried it), then you should try and run the match-statement like this in your prolog inspector: match(X,Y)
The prolog inspector will return all states and print them for you.

Resources