Sockets in Swipl - c++

So I'm currently in the starting phase of building a game and I'm using prolog as the server side of the game to do validation checks of plays in a board.
I'm having two issues at the moment.
One:
Can't seem to close the server without aborting and thus leaving the socket open.
Code is as follows
Server:
:- use_module(library(socket)).
create_server(Port) :-
tcp_socket(Socket),
tcp_bind(Socket, Port),
tcp_listen(Socket, 5),
tcp_open_socket(Socket, AcceptFd, _),
dispatch(AcceptFd).
dispatch(AcceptFd) :-
tcp_accept(AcceptFd, Socket, Peer),
thread_create(process_client(Socket, Peer), _,
[ detached(true)
]),
dispatch(AcceptFd).
process_client(Socket, _Peer) :-
setup_call_cleanup(tcp_open_socket(Socket, In, Out),
handle_service(In, Out),
close_connection(In, Out)).
close_connection(In, Out) :-
close(In, [force(true)]),
close(Out, [force(true)]).
handle_service(In, Out) :-
read(In, Int),
writeln(Int),
( Int == end_of_file
-> true
;
call_test(Int,Term),
format(Out, 'seen(~q).~n', [Term]),
flush_output(Out),
handle_service(In, Out)
).
call_test(test,Term):-Term = 'really test'.
call_test(validate(teste),Term):-
String = "validate(test)",
string_to_list(String,List),
read_from_chars(List,Stringf),
writeln(Stringf),
Term = 'foo'.
Client:
:- use_module(library(streampool)).
create_client(Host, Port) :-
setup_call_catcher_cleanup(tcp_socket(Socket),
tcp_connect(Socket, Host:Port),
exception(_),
tcp_close_socket(Socket)),
setup_call_cleanup(tcp_open_socket(Socket, In, Out),
chat_to_server(In, Out),
close_connection(In, Out)).
chat_to_server(In, Out) :-
read(Term),
( Term == end_of_file
-> true
; format(Out, '~q .~n', [Term]),
flush_output(Out),
read(In, Reply),
write(Reply),
%format('Reply: ~q.~n', [Reply]),
chat_to_server(In, Out)
).
close_connection(In, Out) :-
close(In, [force(true)]),
close(Out, [force(true)]).
I can close the client with no issues using ctrl+D (which is end_of_file), but my server doesn't close... It receives end_of_file, it prints end_of_file but it doesn't close. Not even when inserting it directly into the server. What am I doing wrong?
Two: I need to pass a string from C++ to Swipl that has the name of the predicate to use and the arguments. Can someone please tell me how to do this or at least point me in the right direction?
Thank you very much.

Shouldn't Int == end_of_file be Int = end_of_file, to check unification? I might be wrong here, I use sicstus.
As for how pass a predicate name and arguments, something like this should work
call_test(Request, Answer) :-
Request = Pred(Arg1, Arg2, Arg3),
Request,
Answer = somethingHere.
Then in C++ simply do
//pseudo-code
write(socket, "myPredicate(myArg1, myArg2, myArg3).\n");
Prolog will convert the incoming string into actual code if you tell it to.
An alternative, more generic, implementation is
call_test(Request, Answer) :-
Pred =.. Request,
Pred,
Answer = somethingHere.
and in C++
write(socket, "[myPredicate, Arg1, Arg2, Arg3].\n");
//supports variable number of args

One: You can solve that in a simple way or a complicated way. The simple way is restarting not just your server, but also the Prolog interpreter. The complicated one is calling a predicate that cleanly exits the program when a "close" message is received.
Two: The read/2 predicate, which takes as parameters an input stream (the read end of your socket, in this case) and a variable where the parsed input will be put, does just what you want: it reads a term from the input and converts it to the Prolog term it represents, as if you'd written it in a Prolog program. Note that after the term, the input must contain a dot to tell Prolog the term has ended (as the other answer shows).

Related

Converting list of clauses to a query?

let say i have the following facts :
book(65).
own(named('Peter'), 65).
now got the query as a list of clauses :
[what(A), own(named('Peter'), A)]
or
[who(X), book(A), own(X, A)] .
how do I make a rule that accept this list and return the result. Keep in mind that the question could be Why,When,Who...
I went the usual way :
query_lst([]).
%% query_lst([what(Q)|T], Q) :- query_lst(T).
query_lst([H|T]) :- write('?- '),writeln(H),
call(H), query_lst(T).
but this does not allow binding of Q in wh(Q) to the answer which could be in any of the facts that are called by call()
Additional complication I did not forsee is that the query :
(what(A), own(named('Peter'), A).
would fail, because there is no what(X), fact in the DB.
I have to just bind somehow the variable A /that is in what()/ to query_lst(Goals,A) and of course remove what(X) from the list /which i can do with select/3 /
any idea how to bind list-Wh-var to query_lst result ?
my current solution (assumes Q is first element):
query_lst([G|Gs],Res) :- G =.. [Q,Res], member(Q,[what,why,who,when]), lst2conj(Gs,Conj), call(Conj).
Simply convert the list of goals into a conjunction and call it:
list_to_conjunction([], true).
list_to_conjunction([Goal| Goals], Conjunction) :-
list_to_conjunction(Goals, Goal, Conjunction).
list_to_conjunction([], Conjunction, Conjunction).
list_to_conjunction([Next| Goals], Goal, (Goal,Conjunction)) :-
list_to_conjunction(Goals, Next, Conjunction).
Then:
query_list(Goals) :-
list_to_conjunction(Goals, Conjunction),
call(Conjunction).
You got an answer, but it was an answer to your question, not to what you really wanted. Also, you edited your question after you accepted that answer, which isn't very helpful. Typically it's better to open a new question when you have... a new question.
Here is an answer to what you seem to want, which is not exactly what you asked. You have lists of the form [WhPart | Rest] where the WhPart is a wh-word with a variable, and the Rest is a list of goals. You want to execute these goals and get the variable in the wh-term bound.
The good news is that, since the variable in the wh-word also occurs in the goals, it will be bound if you execute them. No extra work is needed. Executing the goals is enough. If the wh-part is really at the start of the list, you can do the whole thing like this:
query([_WhPart | Body]) :-
call_body(Body).
call_body([]).
call_body([Goal | Goals]) :-
call(Goal),
call_body(Goals).
For example:
?- query([who(X), book(A), own(X, A)]).
X = named('Peter'),
A = 65.
?- query([what(A), own(named('Peter'), A)]).
A = 65.
As you can see, there is no need to convert the query to a conjunctive goal: Executing the queries in sequence is exactly the same as executing their conjunction.
Also, it doesn't actually matter which wh-word is used; the only thing that really matters is the variable contained within the term. For this reason the above version does no checking at all, and the _WhPart could be anything. If you want to check that it is a valid term, you can do the following:
query([WhPart | Body]) :-
wh(WhPart),
call_body(Body).
wh(who(_X)).
wh(what(_X)).
wh(when(_X)).
This buys you some "type checking":
?- query([foo(A), own(named('Peter'), A)]).
false.
But not a lot, since you don't know if the wh-word actually fits what is being asked:
?- query([when(A), own(named('Peter'), A)]).
A = 65.

Prolog answers Argument = Argument, instead of answer No

How can I make prolog answer No , if search_answer didn't find an answer , and
Yes with L = [Answer]
search_answer : predicate that returns a list or let's A as a free variable.
found_list(L) :-
search_answer(L).
For example , when asked found_list(L) , although search_answer didn't find an answer , Prolog still answers Yes. I print L , and it is equal to _496 , a free variable.
Given the above piece of code, found_list answers L = [...] if search_answer found a list , else returns L = L, while I want to answer no
I tried the following , but doesn't work
found_list(L) :-
search_answer(L) , is_list(L).
liar_detector is my search_answer predicate , with L = answer
and liars is my found_answer
In found_list(L) :- search_answer(A). both L and A are singleton. I assume you saw the warning. You need to fix that for this predicate to make sense. Do you really want L = [Answer] or L = Answer?
You can achieve what you're after this way:
found_list(A) :-
search_answer(A).
This will fail (result in "no") if search_answer(A) doesn't succeed, and your result will be A if it does succeed.
If you want the result as an answer within a list, you can do this:
found_list([A]) :-
search_answer(A).
I'm not sure what the value is of either of these. The first found_list/1 is just a simple wrapper on search_answer/1 without any additional logic. The second simply makes a single-element list out of the search_answer/1 result. If A is already a list, you don't need to put it inside of brackets ([...]), otherwise you just get a list within a list. I suspect you are really trying to do something else but haven't explained.
In response to the updated question, the following code should work if A is a simple unbound term:
found_list(A) :-
search_answer(A),
is_list(A).
However, is_list/1 will succeed if its argument has a list structure even though it's elements may be unbound:
| ?- X = [_], is_list(X).
X = [_]
yes
| ?-
So, for example, if search_answer(A) succeeds with A = [_], then found_list(A) will suceed with A = [_].
ground/1 can be useful here since:
| ?- ground(X).
no
| ?- ground([_|_]).
no
| ?- ground([a,b]).
yes
| ?-
Thus, the following solution should work:
found_list(A) :-
search_answer(A),
ground(A).
If your intention is not to backtrack to search_answer(A) if A is not ground, but just fail, you could implement found_list/1 as:
found_list(A) :-
search_answer(A),
( ground(A) -> true ; !, false ).
I think, though, there may be a more fundamental issue with the code, as it shouldn't have a behavior that you feel compelled to work around like this.

Return from Prolog predicate

I have a predicate, which is true, if passed such list of pairs, for instance:
translatable([(dog,perro)], [(perro,hund)], [(dog,hund)])
Means - if "dog" translates to "perro", and "perro" translates to "hund", then it is true that "dog" translates to "hund".
Here follows full code. Returns/suggests first member of pair - given ((a, b), a) returns true, given ((a, b), X) returns X = a:
first((First, _), First).
Similar to "first", but for second pair member:
second((_, Second), Second).
This returns true if translatable word exists in list of tuples, and saves translation to Translation: (dog, Translation, [(bed,cama),(dog,perro)]
translation_exists(Word, Translation, [H|T]) :-
first(H, Word), second(H, Translation), !;
translation_exists(Word, Translation, T).
And resulting:
translatable(EnglishSpanish, SpanishGerman, EnglishGerman) :-
forall(member(Pair, EnglishGerman), (
first(Pair, Word),
second(Pair, ResultTranslation),
translation_exists(Word, Translation, EnglishSpanish),
translation_exists(Translation, ResultTranslation, SpanishGerman)
)).
This code returns true/false correctly.
But why, given
translatable([(dog,perro)], [(perro,hund)], X).
It does not returns X = [(dog,hund)]?
EDIT
To be more specific, actual goal is:
to find out if LAST dictionary has translatable pairs (and them only).
Daniel, thanks a lot, I have adopted your suggested member function - great simplification, thank you! This is all the code I have now:
lastIsTranslatable(_, _, []).
lastIsTranslatable(EngSpan, SpanGerm, [(Eng, Germ) | T]) :-
member((Eng, Span), EngSpan),
member((Span, Germ), SpanGerm),
% this is to protect endless [(dog,hund), (dog, hund), ...]
not(member((Eng, Germ), T)),
lastIsTranslatable(EngSpan, SpanGerm, T),
!.
And still, this works great finding True & False:
lastIsTranslatable([(a,b)], [(b,c)], [(a,c)]).
lastIsTranslatable([(a,b)], [(b,c)], [(a,no)]).
But for
lastIsTranslatable([(a,b)], [(b,c)], X).
result is X= [], then, after hitting ";" - false. Why?
Well, running with trace option, I see execution is failing on
not(member((Eng, Germ), T))
But otherwise resulting X will be endlessly filled with (a,c), (a,c)... Maybe there is better way to protect from duplicates?
The reason, basically, is that because EnglishGerman is uninstantiated, member/2 is free to come up with possible lists for it:
?- member((perro,X), List).
member((perro,X), List).
List = [ (perro, X)|_G18493911] ;
List = [_G18493910, (perro, X)|_G18493914] ;
List = [_G18493910, _G18493913, (perro, X)|_G18493917] ;
List = [_G18493910, _G18493913, _G18493916, (perro, X)|_G18493920]
...
This is the most direct issue, but even if you change the flow of data I think you'll still have problems:
translatable1(EnglishSpanish, SpanishGerman, EnglishGerman) :-
member((English,Spanish), EnglishSpanish),
member((Spanish,German), SpanishGerman),
member((English,German), EnglishGerman).
Note that I have foregone your first/2 and second/2 predicates in favor of pattern matching; I think this reads more clearly.
Aside: If you know your list is concrete and you don't want to generate multiple solutions, you can use memberchk/2 to verify that an element exists instead of member/2; it's cheaper and deterministic.
This works better (you get solutions, anyway) but still you get a lot more solutions than you need:
?- translatable1([(dog,perro)], [(perro,hund)], X).
X = [ (dog, hund)|_G18493925] ;
X = [_G18493924, (dog, hund)|_G18493928] ;
X = [_G18493924, _G18493927, (dog, hund)|_G18493931] a
Something which we know that our code does not know is that the cardinality of the result set should be less than or equal to the lowest cardinality of our inputs; if I have fifteen English-Spanish words and twelve Spanish-German words, I can't have more than twelve words in my English-German result. The reason our code doesn't know that is because it is trying to behave like math: our code is basically saying "for every element of English-Spanish, if there exists a matching element of Spanish-German, that is also an element of English-German." This does not tell us how to construct English-German! It only tells us a fact about English-German that we can verify with English-Spanish and Spanish-German! So it's cool, but it isn't quite enough to compute English-German.
Aside: it's conventional in Prolog to use a-b instead of (a,b); it's too easy to lull yourself into believing that Prolog has tuples when it doesn't and the operator precedence can get confusing.
So, how do we tell Prolog how to compute English-German? There are probably lots of ways but I would prefer to use select/3 because our set cardinality constraints (as well as a general sense that it will converge/halt) will emerge naturally from a computation that "uses up" the input sets as it goes.
translatable2([], _, []).
translatable2(_, [], []).
translatable2([Eng-Span|EngSpanRem], SpanGerm, EngGerm) :-
(select(Span-Germ, SpanGerm, SpanGermRem) ->
translatable2(EngSpanRem, SpanGermRem, EngGermRem),
EngGerm = [Eng-Germ|EngGermRem]
;
translatable2(EngSpanRem, SpanGerm, EngGerm)
).
The base cases should be obvious; if we are out of English-Spanish or Spanish-German, there's nothing left to compute. Then the inductive case peels the first item off the English-Spanish list and searches for a Spanish-German translation that matches. If it finds one, it uses it to build the result; otherwise, it just recurs on the remaining English-Spanish list. This way, on each iteration we at least discard an English-Spanish translation from that list, and we discard Spanish-German translations as they are used. So it seems intuitively likely that this will work and terminate without producing a bunch of extra choice points.
It seems to do the trick:
?- translatable2([dog-perro], [perro-hund], X).
X = [dog-hund] ;
X = [dog-hund].
The extra result there is because we hit both terminal cases because both lists became []; this isn't attractive but it isn't anything to worry about really either.
Now one thing that sucks about this solution is that it treats the first two parameters as in-parameters and the last one as an out-parameter and there isn't really anything you can do about this. I don't know if this is an issue for you; translatable/1 should not have this limitation, but because member((Spanish,German), SpanishGerman) happens before member((English,German), EnglishGerman) it winds up generating an infinitely large list, searching in effect for the missing Spanish-German translation.
Still, it feels like it should be possible to come up with a general purpose predicate that works as long as you supply any two of these inputs. I can do that if I know that all three lists are complete and in the same order:
translatable3([], [], []).
translatable3([X-Y|XYs], [Y-Z|YZs], [X-Z|XZs]) :-
translatable3(XYs, YZs, XZs).
And you can see it work like so:
?- translatable3([dog-perro], [perro-hund], X).
X = [dog-hund].
?- translatable3([dog-perro], X, [dog-hund]).
X = [perro-hund].
?- translatable3(X, [perro-hund], [dog-hund]).
X = [dog-perro].
But I don't know enough about your constraints to know if that could be a legitimate answer. My suspicion is no, because languages don't work that way, but who knows?
Anyway, that's three different approaches; I hope one of them is helpful to you!

How to print all the facts?

I am stuck for this problem...
isAt(keys, room3).
isAt(book, room3).
isAt(keys, room6).
isAt(keys, room4).
currently, room3 have keys and book.
I want to print keys and book.
I tried this code and apparently prints only one. (just keys)
look :- isIn(Location),
write('You are in '),
write(Location),
nl,
items_inroom(Location),
nl.
items_inroom(Location) :-
isIn(Location),
isAt(Item, Location),
write('Available Item(s):'),
write(Item),
nl.
items_inroom(_) :-
write('Available Item(s): None'),
nl.
items_inroom is the code that trying to print all these facts.
How can I approach this?
any help will be great! Thank you.
From Chapter 11 in "The Craft of Prolog" by Richard O'Keefe, a bit simplified/refactored to save keystrokes:
print_item_report(Location) :-
( setof(Item, isAt(Item, Location), Items)
-> format("Items available in ~w:~n", [Location]),
forall(member(I, Items),
format("~w~n", [I]))
% print_item_report_footer
; format("No items in ~w~n", [Location])
).
% etc
If you don't have format for whatever reason, you can still use write. If you don't have forall, then this:
forall(Condition, Action)
is defined as
\+ (Condition, \+ Action )
so you can use that instead. See the SWI-Prolog forall/2 documentation for details.
Find all items and display them.
items_inroom(Location) :-
write('Available Item(s):'),
findall(Item, isAt(Item, Location), Items),
show_items(Items).
show_items([]) :-
write('None'), !.
show_items(Items) :-
write(Items).
Actually you can implement the show_items(Items) in any way you want.
items_inroom/1 predicate will always print the first occurrence of Item on all facts isAt/2. You need to loop over all the facts isAt/2, use a metapredicate setof/3, bagog/3 or findall/3, I will recomend setof/3 like #Boris did, or build your own bucle (maybe not the best idea, but it's an option):
show_items(Location):- isAt(Item, Location), % Condition
write(Item), nl, % Process result
fail. % force backtracking to evaluate condition and find a new result
show_items(_). % return true when all options have been evaluated

Find largest value with text streaming

I need the program that can be recognize the largest value in text.
First streaming a text file in it and
I get the infos char by char but cannot do the calculation and result wheter it is square or not.. If it is square it give the number in sample.txt's coordinates.
setup:-process('sample.txt').
process(File) :-
open(File, read, In),
get_char(In, Char1),
process_stream(Char1, In),
close(In).
process_stream(end_of_file, _) :- !.
process_stream(Char, In) :-
get_char(In, Char2),
look(Char,Char2),
process_stream(Char2, In).
In Prolog the easier to use input analysis tool is an extension called DCG (Definite Clause Grammar). It's available in almost any system, thanks to its simplicity.
Using that facility, the builtin read_line_to_codes and the utility integer//1 we can write:
:- [library(readutil),
library(http/dcg_basics)].
setup :- process('sample.txt').
process(File) :-
open(File, read, Stream),
repeat,
read_line_to_codes(Stream, Codes),
( Codes == end_of_file
-> close(Stream), !
; phrase(check_rectangle, Codes, _), fail
).
% just print when |X2-X1|=|Y2-Y1|, fails on each other record
check_rectangle -->
integer(X1), ",", integer(X2), ",",
integer(Y1), ",", integer(Y2),
{ abs(X2-X1) =:= abs(Y2-Y1)
-> writeln(found(X1,Y1,X2,Y2))
}.
The output (I've added a scaled up rectangle just to test):
?- setup.
found(10,30,20,40)
found(100,300,200,400)
true.