How to fill in the parameters of predicates by input in prolog? - prolog

I have the following code :
size(5).
black(1,3).
black(2,3).
black(3,2).
black(4,3).
black(5,1).
black(5,5).
words([do,ore,ma,lis,ur,as,po, so,pirus, oker,al,adam, ik]) .
:- use_module(library(lists),[nth1/3, select/3]).
crossword(Puzzle) :-
words(WordList),
word2chars(WordList,CharsList),
make_empty_words(EmptyWords) ,
fill_in(CharsList,EmptyWords),
word2chars(Puzzle,EmptyWords).
word2chars([],[]).
word2chars([Word|RestWords] ,[Chars|RestChars] ) :-
atom_chars(Word,Chars),
word2chars(RestWords,RestChars).
fill_in([],[]).
fill_in([Word|RestWords],Puzzle) :-
select(Word,Puzzle,RestPuzzle),
fill_in(RestWords,RestPuzzle).
make_empty_words(EmptyWords) :-
size(Size),
make_puzzle(Size,Puzzle),
findall(black(I,J),black(I,J),Blacks) ,
fillblacks(Blacks,Puzzle),
empty_words(Puzzle,EmptyWords).
make_puzzle(Size,Puzzle) :-
length(Puzzle,Size),
make_lines(Puzzle,Size).
make_lines([],_).
make_lines([L|Ls],Size) :-
length(L,Size),
make_lines(Ls,Size).
fillblacks([],_).
fillblacks([black(I,J)|Blacks],Puzzle) :-
nth1(I,Puzzle,LineI),
nth1(J,LineI,black),
fillblacks(Blacks,Puzzle).
empty_words(Puzzle,EmptyWords) :-
empty_words(Puzzle,EmptyWords,TailEmptyWords),
size(Size),
transpose(Size,Puzzle,[],TransposedPuzzle),
empty_words(TransposedPuzzle,TailEmptyWords,[] ).
empty_words([],Es,Es).
empty_words([L|Ls],Es,EsTail) :-
empty_words_on_one_line(L,Es,Es1) ,
empty_words(Ls,Es1,EsTail).
empty_words_on_one_line([], Tail, Tail).
empty_words_on_one_line([V1,V2|L],[[V1,V2|Vars]|R],Tail) :-
var(V1), var(V2), !,
more_empty(L,RestL,Vars),
empty_words_on_one_line(RestL,R,Tail) .
empty_words_on_one_line([_| RestL],R, Tail) :-
empty_words_on_one_line(RestL,R,Tail) .
more_empty([],[],[]).
more_empty([V|R],RestL,Vars) :-
( var(V) ->
Vars = [V|RestVars],
more_empty(R,RestL,RestVars)
;
RestL = R,
Vars = []
).
transpose(N,Puzzle,Acc,TransposedPuzzle) :-
( N == 0 ->
TransposedPuzzle = Acc
;
nth_elements(N,Puzzle,OneVert),
M is N - 1,
transpose(M,Puzzle,[OneVert|Acc], TransposedPuzzle)
).
nth_elements(_,[],[]).
nth_elements(N,[X|R],[NthX| S]) :-
nth1(N,X,NthX),
nth_elements(N,R,S).
It is used for solving a crossword like this:
The black squares places are given by default in the code but I want to find a way to give the black squares places by input when I want to query crossword.
something like this:
black(Y1,X1).
black(Y2,X2).
black(Y3,X3).
black(Y4,X4).
black(Y5,X5).
black(Y6,X6).
crossword(Puzzle,Y1,X1,Y2,X2,...) :-
words(WordList),
word2chars(WordList,CharsList),
make_empty_words(EmptyWords,Size) ,
fill_in(CharsList,EmptyWords),
word2chars(Puzzle,EmptyWords).

As #lurker mentioned I tried rewriting the code and giving the black squares as input to program as below:
:- use_module(library(lists),[nth1/3, select/3]).
crossword(Puzzle,Size,Blacks,WordList) :-
word2chars(WordList,CharsList),
make_empty_words(EmptyWords,Size,Blacks) ,
fill_in(CharsList,EmptyWords),
word2chars(Puzzle,EmptyWords).
word2chars([],[]).
word2chars([Word|RestWords] ,[Chars|RestChars] ) :-
atom_chars(Word,Chars),
word2chars(RestWords,RestChars).
fill_in([],[]).
fill_in([Word|RestWords],Puzzle) :-
select(Word,Puzzle,RestPuzzle),
fill_in(RestWords,RestPuzzle).
make_empty_words(EmptyWords,Size,Blacks) :-
make_puzzle(Size,Puzzle),
fillblacks(Blacks,Puzzle),
empty_words(Puzzle,EmptyWords).
make_puzzle(Size,Puzzle) :-
length(Puzzle,Size),
make_lines(Puzzle,Size).
make_lines([],_).
make_lines([L|Ls],Size) :-
length(L,Size),
make_lines(Ls,Size).
fillblacks([],_).
fillblacks([black(I,J)|Blacks],Puzzle) :-
nth1(I,Puzzle,LineI),
nth1(J,LineI,black),
fillblacks(Blacks,Puzzle).
empty_words(Puzzle,EmptyWords) :-
empty_words(Puzzle,EmptyWords,TailEmptyWords),
transpose(Size,Puzzle,[],TransposedPuzzle),
empty_words(TransposedPuzzle,TailEmptyWords,[] ).
empty_words([],Es,Es).
empty_words([L|Ls],Es,EsTail) :-
empty_words_on_one_line(L,Es,Es1) ,
empty_words(Ls,Es1,EsTail).
empty_words_on_one_line([], Tail, Tail).
empty_words_on_one_line([V1,V2|L],[[V1,V2|Vars]|R],Tail) :-
var(V1), var(V2), !,
more_empty(L,RestL,Vars),
empty_words_on_one_line(RestL,R,Tail) .
empty_words_on_one_line([_| RestL],R, Tail) :-
empty_words_on_one_line(RestL,R,Tail) .
more_empty([],[],[]).
more_empty([V|R],RestL,Vars) :-
( var(V)
-> Vars = [V|RestVars],
more_empty(R,RestL,RestVars)
; RestL = R,
Vars = []
).
transpose(N,Puzzle,Acc,TransposedPuzzle) :-
( N == 0
-> TransposedPuzzle = Acc
; nth_elements(N,Puzzle,OneVert),
M is N - 1,
transpose(M,Puzzle,[OneVert|Acc], TransposedPuzzle)
).
nth_elements(_,[],[]).
nth_elements(N,[X|R],[NthX| S]) :-
nth1(N,X,NthX),
nth_elements(N,R,S).
now by the following input the code returns the answer to the puzzle:
crossword(Puzzle,5,[black(1,3),black(2,3),black(3,2),black(4,3),
black(5,1),black(5,5)],[do,ore,ma,lis,ur,as,pu, so,pirus, uker,al,adam, ik]).
And the output will be:
Puzzle = [as,pu,do,ik,ore,ma,ur,lis,adam,so,al,pirus,uker]

Very nice, +1 for solving it yourself.
In addition, I would like to show you how to use DCGs to obtain predicates with fewer arguments for empty_words/2 and its related predicates, which are therefore easier to understand. Further, transpose/2 is already available as a library predicate in SICStus Prolog and SWI (see its source code if you are interested in how it is implemented), so I use that instead. Notice that size/1 is no longer necessary.
:- use_module(library(clpfd)). % for transpose/2 in SWI-Prolog
empty_words(Puzzle,EmptyWords) :-
phrase(empty_words(Puzzle), EmptyWords, RestEmptyWords),
transpose(Puzzle, TransposedPuzzle),
phrase(empty_words(TransposedPuzzle), RestEmptyWords).
empty_words([]) --> [].
empty_words([L|Ls]) --> empty_words_on_one_line(L), empty_words(Ls).
empty_words_on_one_line([]) --> [].
empty_words_on_one_line([V1,V2|Ls0]) -->
{ var(V1), var(V2) }, !,
[[V1,V2|Vars]],
{ more_empty(Ls0, Ls, Vars) },
empty_words_on_one_line(Ls) .
empty_words_on_one_line([_|Ls]) --> empty_words_on_one_line(Ls).
The other predicates remain unchanged.
Your fill_in/2 is available as permutation/2.
maplist/2 can help you in other places, like:
maplist(length_list(Size), Puzzle)
to replace make_lines/2 with a short definition of length_list/2 that I leave as a simple exercise.

Related

How to link constant with variable by assert?

I want to add in the DB a constant and a linked variable:
?- assertz(my(x, A))
So that in the future I can define A and get the only one entry. Sth like that:
?- assertz(my(x, A)), ..., A = 2.
?- my(A, B).
A = x,
B = 2.
Can this be done?
As I noted in the comments your idea of a link like a pointer is not the way to approach solving your problem.
A common solution is to walk the tree and construct a new tree as you walk the tree by replacing the leaf of the tree with a new leaf that contains the value from the input tree along with the associated value, what you are thinking should be linked.
Since you are somewhat new to Prolog I will do this with two examples. The first will just walk a tree and only return true when successfully walked. It can be used to understand how to walk a tree and run with gtrace to single step the code to understand it.
The second example will expand on the tree walk and add the type (link as you think) to the leaf item. The the old leaf for something simple like an atom a, will become a new leaf in the tree like (a,atom).
Also this was quickly written as a demonstration only. I am sure it will have problems if pressed into doing anything more than the single example.
:- module(example,
[
example/1
]).
example(walk) :-
Term = term_size(a(1,"Hello",'Atom',1+2,[a,$,T])),
walk(Term).
example(infer_type) :-
Term = term_size(a(1,"Hello",'Atom',1+2,[a,$,T])),
infer_type(Term,Is),
write(Is).
walk([]) :- !.
walk([T]) :- var(T), !.
walk(L) :- is_list(L), !, L = [H|T], walk(H), walk(T).
walk(T) :- compound(T), !, T =.. [_|Args], !, walk(Args).
walk(T) :- integer(T), !.
walk(T) :- var(T), !.
walk(T) :- atomic(T), !.
walk(T) :- T =.. [Arg|Args], !, walk(Arg), walk(Args).
infer_type([],[]) :- !.
infer_type([T],[(T,var)]) :- var(T), !.
infer_type(L,S) :- is_list(L), !, L = [H|T], infer_type(H,I), infer_type(T,Is), S = [I|Is].
infer_type(T,S) :- compound(T), !, T =.. [F|Args], !, infer_type(Args,Is), S =.. [F|Is].
infer_type(T,(T,integer)) :- integer(T), !.
infer_type(T,(T,var)) :- var(T), !.
infer_type(T,(T,atom)) :- atomic(T), !.
infer_type(T,S) :- T =.. [Arg|Args], !, infer_type(Arg,I), infer_type(Args,Is), S =.. [I|Is].
Example run
Note: I know there are warnings; it is a demo not production code.
Welcome to SWI-Prolog (threaded, 64 bits, version 8.5.3)
?- working_directory(_,'C:/Users/Groot').
true.
?- [example].
Warning: c:/users/Groot/example.pl:20:
Warning: Singleton variables: [T]
Warning: c:/users/Groot/example.pl:24:
Warning: Singleton variables: [T]
true.
?- example(walk).
true.
?- example(infer_type).
term_size(a((1,integer),(Hello,atom),(Atom,atom),(1,integer)+(2,integer),[(a,atom),(($),atom),(_25642,var)]))
true.
As an exercise I did not identify the string as a string, the change should be easy.

Prolog remove parenthesis when transforming list in polynomial

Basically when I try to transform a list into a polynomial or vice-versa, it always shows up with parenthesis (in case of the polynomials). Here is the code, the function not working is the poly2list, the other one are just to define what a monomial/polinomial is.
pvars([x,y,z]).
pvar(X):-pvars(V),member(X,V).
polinomial(X) :- monomial(X).
polinomial(P+M) :- monomial(M), polinomial(P).
monomial(X) :- pvar(X).
monomial(N) :- number(N).
monomial(X) :- power(X),!.
monomial(K*X) :- coefficient(K), power(X),!.
coefficient(N) :- number(N).
power(X) :- pvar(X),!.
power(X^Y) :- pvar(X), integer(Y), Y>1,!.
poly2list(X,[X]) :- monomial(X),!.
poly2list(X+P,[X|Y]) :- monomial(X), poly2list(P,Y).
For example, when i ask:
poly2list(X,[2*x^2,3,y]).
The result is:
X = 2*x^2+(3+y)
And I'm trying to get:
X = 2*x^2+3+y
Thanks in advance :)

How do I go through a database in prolog

I have a database composed of symptoms of a disease and the disease to which these symptoms belongs as follows.
disease([dordecabeca,febre,dormuscular,dorgarganta,cansaco],gripe).
disease([febre,dordecabeca,dorolhos,manchas,nauseas],dengue).
disease([febre,coceira,dordecabeca,dordebarriga,perdadeapetite],catapora).
disease([febre,dordecabeca,fadiga,perdadeapetite,inchacorosto],caxumba).
disease([congestaonasal,gargantairritada,espirros,febre,tosse],resfriado).
treatment([evitarmedicamentosdeaspirina,respouso],dengue).
treatment([evitarmedicamentosdeaspirina,repouso,paracetamol],dengue).
treatment([repouso,evitarpessoas,semmedicamento],catapora).
treatment([repouso,evitarpessoas,medicamento],catapora).
treatment([repouso,evitarpessoas],caxumba).
treatment([repouso,boaalimentacao],gripe).
treatment([repouso,boalimentacao,medicamentos],gripe).
treatment([repouso,boaalimentacao,semmedicamentos],refriado).
treatment([repouso,boaalimentacao,medicamento],resfriado).
symptoms(L1,X):-disease(L1,X).
treatdisease(L1,L2,Y):-symptoms(L1,Y),treatment(L2,Y).
With the symptons predicate, I can visualize all the symptoms and the corresponding disease. And with the predicate treatdisease, I can see the treatment based on the disease common to the two bases.
symptoms(L1,X):-disease(L1,X).
treatdisease(L1,L2,Y):-symptoms(L1,Y),treatment(L2,Y).
But what if I were to compare an entry list with the underlying disease like I would?
If it was just a list I already have the predicate, but on a multidimensional basis I have no idea how to go.
For example if I came in with:
?searchdisease([dordecabeca,febre,dormuscular,dorgarganta],Disease).
How do I go through the bases using this list with the database?
So I have predicates to pick up the different elements between two lists and a predicate to get the equal elements between two lists, but I do not know how to use them when the list is in a subset. Follow the predicates.
%---------------------------------------------------------
%Predicate to pick up equal elements between two lists.
equalelements([],[]).
equalelements([X|Xs0],Ys0) :-
tpartition(=(X),Xs0,Es,Xs),
if_(Es=[], Ys0=Ys, Ys0=[X|Ys]),
equalelements(Xs,Ys).
tpartition(P_2,List,Ts,Fs) :-
tpartition_ts_fs_(List,Ts,Fs,P_2).
tpartition_ts_fs_([],[],[],_).
tpartition_ts_fs_([X|Xs0],Ts,Fs,P_2) :-
if_(call(P_2,X), (Ts = [X|Ts0], Fs = Fs0),
(Ts = Ts0, Fs = [X|Fs0])),
tpartition_ts_fs_(Xs0,Ts0,Fs0,P_2).
if_(If_1, Then_0, Else_0) :-
call(If_1, T),
( T == true -> call(Then_0)
; T == false -> call(Else_0)
; nonvar(T) -> throw(error(type_error(boolean,T),_))
; /* var(T) */ throw(error(instantiation_error,_))
).
=(X, Y, T) :-
( X == Y -> T = true
; X \= Y -> T = false
; T = true, X = Y
; T = false,
dif(X, Y) % ISO extension
% throw(error(instantiation_error,_)) % ISO strict
).
equal_t(X, Y, T):-
=(X, Y, T).
%------------------------------------------------------------
%Predicate to pick up different elements between two lists.
displaydifference([],[],[]).
displaydifference(L1,L2,L4):-concatenate(L1,L2,L3), remove_dups(L3,L4).
concatenate(L1, L2, NL) :-
append(L1, L2, L12),
msort(L12, NL).
remove_dups([], []).
remove_dups([X], [X]).
remove_dups([X,Y|T], [X|R]) :-
X \= Y,
remove_dups([Y|T], R).
remove_dups([X,X|T], R) :-
skip(X, T, WithoutX),
remove_dups(WithoutX, R).
skip(_,[],[]).
skip(X, [X|T], T).
skip(X, [Y|T], [Y|T]) :- X \= Y.
Not sure to understand what do you exactly want obtain with searchdisease/2.
I suppose that you want a predicate that, given a list of symptoms, unifies the second parameter with one or more diseases with symptoms that are a superset of the first parameter.
In that case, I propose
subList([], _).
subList([H | T], S) :-
member(H, S),
subList(T, S).
searchdisease(Symptoms, Disease) :-
disease(Ls, Disease),
subList(Symptoms, Ls).
If you call searchdisease([dordecabeca,febre,dormuscular,dorgarganta],Disease), you unify Disease with gripe because [dordecabeca,febre,dormuscular,dorgarganta] is a subset of the symptoms of gripe.
If you call searchdisease([febre],D), you unify D with gripe and, trying again with backtracking, dengue, catapora, caxumba and resfriado, because febre is a symptom of all of this five diseases.
En passant: I don't understand the usefulness of symptoms/2; why don't you simply use disease/2?

dividing a list up to a point in prolog

my_list([this,is,a,dog,.,are,tigers,wild,animals,?,the,boy,eats,mango,.]).
suppose this is a list in prolog which i want to divide in three parts that is up to three full stops and store them in variables.
how can i do that...
counthowmany(_, [], 0) :- !.
counthowmany(X, [X|Q], N) :- !, counthowmany(X, Q, N1), N is N1+1.
counthowmany(X, [_|Q], N) :- counthowmany(X, Q, N).
number_of_sentence(N) :- my_list(L),counthowmany(.,L,N).
i already counted the number of full stops in the list(my_list) now i want to divide the list up to first full stop and store it in a variable and then divide up to second full stop and store in a variable and so on.........
UPDATE: the code slightly simplified after #CapelliC comment.
One of the many ways to do it (another, better way - is to use DCG - definite clause grammar):
You don't really need counthowmany.
split([], []).
split(List, [Part | OtherParts]) :-
append(Part, ['.' | Rest], List),
split(Rest, OtherParts).
Let's try it:
?- my_list(List), split(List, Parts).
List = [this, is, a, dog, '.', tigers, are, wild, animals|...],
Parts = [[this, is, a, dog], [tigers, are, wild, animals], [the, boy, eats, mango]]
Your problem statement did not specify what a sequence without a dot should correspond to. I assume that this would be an invalid sentence - thus failure.
:- use_module(library(lambda)).
list_splitted(Xs, Xss) :-
phrase(sentences(Xss), Xs).
sentences([]) --> [].
sentences([Xs|Xss]) -->
sentence(Xs),
sentences(Xss).
sentence(Xs) -->
% {Xs = [_|_]}, % add this, should empty sentences not be allowed
allseq(dif('.'),Xs),
['.'].
% sentence(Xs) -->
% allseq(\X^maplist(dif(X),['.',?]), Xs),
% (['.']|[?]).
allseq(_P_1, []) --> [].
allseq( P_1, [C|Cs]) -->
[C],
{call(P_1,C)},
allseq(P_1, Cs).
In this answer we define split_/2 based on splitlistIf/3 and list_memberd_t/3:
split_(Xs, Yss) :-
splitlistIf(list_memberd_t(['?','.','!']), Xs, Yss).
Sample queries:
?- _Xs = [this,is,a,dog,'.', are,tigers,wild,animals,?, the,boy,eats,mango,'.'],
split_(_Xs, Yss).
Yss = [ [this,is,a,dog] ,[are,tigers,wild,animals] ,[the,boy,eats,mango] ].
?- split_([a,'.',b,'.'], Yss).
Yss = [[a],[b]]. % succeeds deterministically

Change goal execution order in Prolog Interpreter

I'm attempting to write a Prolog meta-interpreter to choose the order of goal execution, for example executing first all goals with the minimum number of parameters.
I started from the vanilla meta-interpreter:
solve2(true).
solve2(A) :- builtin(A), !, A.
solve2((A,B)) :- solve2(A), solve2(B).
solve2(A) :- clause(A,B), solve2(B).
Then i went to something like
solve2(true).
solve2(A) :- builtin(A), !, A.
solve2((A,B)) :- count(A,Args), count(B,Args2), Args<Args2, solve2(A), solve2(B).
solve2((A,B)) :- count(A,Args), count(B,Args2), Args>Args2, solve2(B), solve2(A).
solve2(A) :- clause(A,B), solve2(B).
But if the 4th line is executed then the whole block B is executed before A which is wrong.
Ex. A=a(x,y), B=(b(x,y,z), c(x)) I'd like to execute c, then a, then b. - while in this method i'd get c, b and then a.
I'm thinking about transforming the goals in a list but i'm not too sure.
Any ideas?
Here is an (untested) vanilla meta interpreter, with conjunction order changed. I would be glad if you could try with your data.
solve2(true).
solve2(A) :- builtin(A), !, A.
solve2((A,B)) :- ordering(A,B, C,D), ! /* needed */, solve2(C), solve2(D).
solve2(A) :- clause(A,B), solve2(B).
ordering(A,B, C,D) :-
minargs(A, NA),
minargs(B, NB),
( NA =< NB -> C/D=A/B ; C/D=B/A ).
minargs((A,B), N) :-
minargs(A, NA),
minargs(B, NB),
!, ( NA =< NB -> N=NA ; N=NB ).
minargs(T, N) :-
functor(T, _, N).
edit I tested with this setting:
builtin(writeln(_)).
a(1):-writeln(1).
b(1,2):-writeln(2).
c(1,2,3):-writeln(3).
test :-
solve2((c(A,B,_),a(A),b(A,B))).
and got the expected output:
?- test.
1
2
3
true .
edit I had to resort to a list representation, but then it make sense to preprocess the clauses and get the right order before, then stick to plain vanilla interpreter:
test :-
sortjoin((b(A,B),a(A),c(A,B,_)), X),
solve2(X).
sortjoin(J, R) :-
findall(C-P, (pred(J, P), functor(P,_,C)), L),
sort(L, T),
pairs_values(T, V),
join(V, R).
join([C], C).
join([H|T], (H,R)) :- join(T, R).
pred((A, _), C) :-
pred(A, C).
pred((_, B), C) :-
!, pred(B, C).
pred(C, C).
where solve2((A,B)) :- ... it's the original solve2(A),solve2(B)

Resources