Related
I don't understand what exactly these following 4 predicates do:
predicate1([H1,H2|T1],X) :−
append([H1,H2|T1],[H2,H1|T1],X).
predicate2wrapper(In, Out) :-
predicate2([ ], In, Out).
predicate2(L, [ ], L).
predicate2(A,[H| T], Out) :-
predicate2([H,H|A],T, Out).
predicate3([X,X], [X,X|_]).
predicate3([X,X], [_,H|T]) :−
predicate3([X,X],[H|T]).
predicate4([ ], _) .
predicate4([H|T], L) :−
member(H,L),
predicate4(T,L).
I know that all these predicates take lists as their argument(s) and I also tried to run them but I always get a Syntax error: operator expected warning.
Could someone please help me understand what these predicates do and maybe also help me fix this error?
Did you copy this from a pdf or a word-document? The --signs in your :- are not minus signs - this is causing the error. Retype them to solve the issue and then you can analyze the predicates. How to know if chars looking the same are not the same? I would suggest using a diff tool (a program to compare files, as command line or with a gui (winmerge), even available online) or an ascii to hex converter like this.
Example queries which give you a hint what these predicates do:
?- predicate1([1,2,3],L).
?- append([1,2,3],[4,5],L).
?- predicate2wrapper([1,2,3],L).
?- predicate3([c,c],[a,a,b,b]).
?- predicate3([b,b],[a,a,b,b,c,c]).
?- predicate3([X,X],[a,a,c,b,b]).
?- predicate4([a,b],[a,c,b]).
?- predicate4([a,d],[a,c,b]).
?- member(a,[a,c,b]).
?- member(d,[a,c,b]).
SWI-prolog version: threaded, 64 bits, version 7.6.4
OS: Ubuntu bionic 18.04
Still working on learning prolog and ran into an interesting situation. I would like to use a predicate that is 'asserted' over multiple files (just makes the code look a little cleaner in organization), but that predicate also needs to be able to process rdf prefixes as part of the semweb package and it does not seem to work.
Here is a code block that might show the problem:
:- module(multifile1,
[
test/2
]
).
:- use_module(library(semweb/rdf_db)).
:- use_module(library(semweb/rdfs)).
:- multifile
multifile1:bar/1,
multifile1:foo/1.
:- rdf_meta
test(-, r),
foo(r).
test(bar, X) :-
bar(X).
test(foo, X) :-
foo(X).
multifile1:bar(abc) :-
format('bar: abc~n', []).
foo(rdf:about) :-
format('foo: rdf:about~n', []).
And asserting the same foo in another file:
:- module(multifile2,
[
]
).
:- use_module(library(semweb/rdf_db)).
:- use_module(library(semweb/rdfs)).
:- multifile
multifile1:foo/1.
:- rdf_meta
multifile1:foo(r).
multifile1:foo(rdf:type) :-
format('rdf:type~n', []).
In this form calling test(foo, rdf:about) works, but asserting test(foo, rdf:type) does not work. Is rdf_meta and multifile not supposed to work together or there is a bug in this code?
PS: I had added the multifile bar to make sure that works over multiple files.
I think I figured out. multifile and rdf_meta do work together. The bug in the code was related to the fact that I had defined foo in rdf_meta as foo(r). I think that expansion was causing confusion at compile time with regards to pattern match. Changing the definition to foo(-) fixed the problem. Hope this helps someone in the future.
I decided to start playing with Prolog (SWI-Prolog). I wrote a program, and now I'm trying to write a simple main predicate so that I can create a .exe and run the program from the command line. That way, I can find true/false relationships from the command line rather than from the prolog GUI. However, I am having trouble understanding what actually goes in the main predicate. Here is the program so far:
mother(tim, anna).
mother(anna, fanny).
mother(daniel, fanny).
mother(celine, gertrude).
father(tim, bernd).
father(anna, ephraim).
father(daniel, ephraim).
father(celine, daniel).
parent(X,Y) :- mother(X,Y).
parent(X,Y) :- father(X,Y).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
First Try:
I put all of the relationship definitions in a predicate called family_func().Then, I tried to call that function from main by typing main. into the command line. I expected to be able to start finding relationships like I did before I created the predicates, but instead, the program started with this error:
ERROR: c:/.../ancestor.pl:18:0: Syntax error: Operator expected
Here is the code:
family_func():-
mother(tim, anna).
...
parent(X,Y) :- mother(X,Y).
...
main:-
family_func().
Second Try:
I tried putting all of the definitions in the main predicate. I expected to be able to type main. and then have the program pause and wait for me to start entering clauses (almost like in Java where you run a program and it waits for user input). Instead, when I typed main., it returned false.
Question 1:
I am used to writing code in Java. So, in my mind, The first thing I tried should work. I basically defined local variables in family_func() and then I called smaller "methods" (i.e. parent(X,Y) :- mother(X,Y).) that should find the relationships between those variables. When I call main, at the very least, I would have expected the program to wait for me to enter the relationship, return the result, and then close. Why doesn't this work?
Question 2:
How would I actually write a main predicate? Are there any good examples for a program like this? I've tried the example here, but couldn't get it to work.
Thanks for any help.
Edit:
New Attempt - main. returns false, and running parent(tim, anna). right after running main. returns false even though it should be true.
:- dynamic mother/2.
:- dynamic father/2.
family_func:-
assert(mother(tim, anna)).
assert(father(tim, bernd)).
parent(X,Y) :- mother(X,Y).
parent(X,Y) :- father(X,Y).
ancestor(X, Y) :- parent(X, Y).
ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y).
main:-
family_func.
Edit:
Just in case anyone else needs to know, as #CapelliC states in the comments under the answer, there needs to be a comma between calls. For example:
family_func:-
assert(mother(tim, anna)),
assert(mother(anna, fanny)),
assert(mother(daniel, fanny)),
assert(mother(celine, gertrude)),
assert(father(tim, bernd)),
assert(father(anna, ephraim)),
assert(father(daniel, ephraim)),
assert(father(celine, daniel)).
I think should be (no empty argument list allowed)
:- dynamic mother/2.
... other dynamically defined relations...
family_func:-
assert(mother(tim, anna)).
...
% rules can be dynamic as well, it depends on your application flow...
parent(X,Y) :- mother(X,Y).
...
main:-
family_func.
I've been struggling trying to edit Eliza chatbot in Prolog. every time I try to edit something, a new error show up. Is it protected to any sort of editing?
I edited using SWI-prolog editor. The problem is that I'm trying to minimize the code without fully understanding it. I'm trying to do a simple short version of it. So, I might removed something essential perhaps! like "my_char_type" for example. The error I got is " retract/1: No permission to modify static procedure `rules/1'"
Is there any code for a smaller chatbot that I can understand?
Please help :'(
Prolog has a static store and a dynamic store. If you open up a file, say program.pl and you put some lines in it like this:
foo(tabitha).
foo(darlene).
those facts wind up in the static store. They're not a mutable part of your program (by default).
The asserta/1, assertz/1 and retract/1 and retractall/1 procedures form the basis of the dynamic store. If you are just sitting at the console you could just add some facts to the dynamic store and remove them by doing something like this:
?- asserta(baz(tabitha)).
true.
?- baz(X).
X = tabitha.
?- retract(baz(tabitha)).
true.
?- baz(X).
false.
However, if you are sitting at the prompt after loading program.pl and you try to retract foo(tabitha) you're going to get the static procedure message:
?- retract(foo(tabitha)).
ERROR: retract/1: No permission to modify static procedure `foo/1'
ERROR: Defined at /Users/fusion/program.pl:1
The reason is because the foo/1 facts were placed in the static store rather than the dynamic store, because you didn't put them there with asserta/1 or assertz/1 or declare the predicate as dynamic, like this:
:- dynamic foo/1.
So there are two ways forward:
Edit the program source directly and reload it.
Declare the rules/1 predicate dynamic as above.
Incidentally, reloading in SWI-Prolog is best done by running make. from the prompt.
I would recommend option #1 since otherwise it will be difficult to reconstruct your working program's state when you like what it is doing.
SWISH has the simplest Eliza ever, I have the old code below, used to test my Prolog interpreter.
here is an example session
1 ?- eliza.
? i am hungry
how long have you been hungry ?
? very long
please go on
? bye
Goodbye. I hope I have helped you
true.
SWI-Prolog tested version, ported from below ELIZA.IL (alas, SWISH is apparently missing IO primitive like read_line_from_codes, so it's simpler to paste the full code)
eliza :-
write('? '), read_word_list(Input), eliza(Input), !.
eliza([bye]) :-
write('Goodbye. I hope I have helped you'), nl.
eliza(Input) :-
pattern(Stimulus, Response),
match(Stimulus, Dictionary, Input),
match(Response, Dictionary, Output),
reply(Output),
!, eliza.
match([N|Pattern], Dictionary, Target) :-
integer(N), lookup(N, Dictionary, LeftTarget),
append(LeftTarget, RightTarget, Target),
match(Pattern, Dictionary, RightTarget).
match([Word | Pattern], Dictionary, [Word | Target]) :-
atom(Word), match(Pattern, Dictionary, Target).
match([], _Dictionary, []).
pattern([i,am,1],[how,long,have,you,been,1,'?']).
pattern([1,you,2,me],[what,makes,you,think,i,2,you,'?']).
pattern([i,like,1],[does,anyone,else,in,your,family,like,1,'?']).
pattern([i,feel,1],[do,you,often,feel,that,way,'?']).
pattern([1,X,2],[can,you,tell,me,more,about,your,X,'?']) :- important(X).
pattern([1],[please,go,on]).
important(father).
important(mother).
important(son).
important(sister).
important(brother).
important(daughter).
reply([Head | Tail]) :-
write(Head), write(' '), reply(Tail).
reply([]) :- nl.
lookup(Key, [(Key, Value) | _Dict], Value).
lookup(Key, [(Key1, _Val1) | Dictionary], Value) :-
Key \= Key1, lookup(Key, Dictionary, Value).
read_word_list(Ws) :-
read_line_to_codes(user_input, Cs),
atom_codes(A, Cs),
tokenize_atom(A, Ws).
Older code: eliza and rwl
How would I state things "in general" about the facts? Suppose I need to state "everyone likes the person who likes him/her", and I have a list of people who may or may not like each other.
This is what I tried so far, but it's sure not the way to do it:
likes(dana, cody).
hates(bess, dana).
hates(cody, abby).
likes(first(Girl, OtherGirl), first(OtherGirl, Girl)).
hates(Girl, OtherGirl):- \+ likes(Girl, OtherGirl).
because this won't even compile.
everybody([dana, cody, bess, abby]).
likes_reflexive(dana, cody).
hates(bess, dana).
hates(cody, abby).
likes_reflexive(X, Y):- likes(X, Y), likes(Y, X).
hates(Girl, OtherGirl):- \+ likes(Girl, OtherGirl).
%% likes_reflikes_reflexive(X, Y):- likes(X, Y), likes(Y, X).
%% user:6: warning: discontiguous predicate likes_reflexive/2 - clause ignored
%% hates(Girhates(Girl, OtherGirl):- \+ likes(Girl, OtherGirl).
%% user:8: warning: discontiguous predicate hates/2 - clause ignored
Unfortunately I don't understand what the warnings say. Hope it makes my intention more clear. I.e. by stating one fact, I also want to state the other related fact.
If you want to change your knowledge base dynamically, you can use asserts. If you want to modify existing predicate, you should define it as dynamic, e.g. :- dynamic(likes/2).. If predicate is undefined, you can omit it.
add_mutual_likes(X, Y) :- asserta(likes(X, Y)), asserta(likes(Y, X)).
:- initialization(add_mutual_likes(dana, cody)).
initialization/1 calls add_mutual_likes(data, cody) goal when file is loaded. add_mutual_likes/2 adds two facts to a database. asserta/1 converts it's argument into a clause and adds it to a database.
| ?- [my].
yes
| ?- listing(likes/2).
% file: user_input
likes(cody, dana).
likes(dana, cody).
yes
| ?- likes(cody, dana).
yes
| ?- likes(dana, cody).
yes
| ?- add_mutual_likes(oleg, semen).
yes
| ?- listing(likes/2).
% file: user_input
likes(semen, oleg).
likes(oleg, semen).
likes(cody, data).
likes(data, cody).
yes
I use gprolog.
Let's start with the warnings. They are merely "style" suggestions. They are telling you that all the definitions for likes and hates should be together. Trust me if you have a big Prolog program it becomes a nightmare to go around tour code to get the full definition of your predicate. It would be like writing half a function in C++ and finish it in another file.
Now, you want to say "everyone likes the person who likes him/her". I'm not sure why you are using that function "first" in the code. This would be sufficient:
likes(dana, cody).
likes(Girl, OtherGirl) :- likes(OtherGirl, Girl).
The second clause reads "Girl likes OtherGirl if OtherGirl likes Girl. This won't work.
If you ask your program "is it true that cody likes dana"
? likes(cody, dana)
Prolog will reason like this:
The answer is yes if dana likes cody (using the second clause).
Yes! Because dana likes cody (using the first clause).
This is not enough to make it a correct program. Since we are in Prolog you can say: "give me another solution" (usually by entering ";" in the prompt).
Prolog will think "I only used the first clause, I haven't tried the second".
The answer is Yes also if dana likes cody (using the second clause).
The answer is Yes according to the second clause, if cody likes dana.
But that's our initial query. Prolog will give you the same answer again and again, looping forever if you asked for all the solutions.
You can do two things here. The first is telling Prolog that one solution is enough. You do this adding a "!" (that basically says, clear all the open branches left to explore).
likes(dana, cody) :- !.
likes(Girl, OtherGirl) :- likes(OtherGirl, Girl).
Another alternative is to "stratify the program".
direct_likes(dana, cody).
likes(Girl, OtherGirl) :- direct_likes(OtherGirl, Girl), !.
likes(Girl, OtherGirl) :- direct_likes(Girl, OtherGirl).
What you want is a fact where Prolog does not care about the order of arguments. Alas, something like that does not exist. What you can do instead is define facts where the implied meaning is that it is valid for all argument orders (like_each in the example below). But of course, you cannot use these facts in that way. Instead, you define the actual predicate to try (hence the or ;) all possible argument orders.
Thus, the solution is:
%% bi-directional like
like_each(dana, cody).
likes(A, B) :- like_each(A, B); like_each(B, A).
%% optional: one-directional like
% likes(cody, sarah).
Also, be careful with
hates(Girl, OtherGirl):- \+ likes(Girl, OtherGirl).
If both variables are unbound (e.g., ?- hates(A,B)), it will always fail. This happens because Prolog first tries to find a match for likes, which always succeeds for two variables, and then negates the result. Thus, you cannot use hates to find all pairs who don't like each other.