I have a list L =[a+b,b+c] and I want to convert it to a string and print the output a+bb+c.
Can you please help me convert this list to a string? I tried using atomic_list_concat in SWI-Prolog but it gave a type error for a+b.
In SWI-Prolog:
?- with_output_to(atom(Atom), maplist(write, [a+b, b+c])).
Atom = 'a+bb+c'.
You can replace write with a call to a custom predicate if you need more control over how your terms (e.g. a+b) are written.
The members of your list are compound terms, so you have to make them atomic before calling atomic_list_concat:
custom_print(L) :-
maplist(term_to_atom, L, L1),
atomic_list_concat(L1, L2),
print(L2).
Related
I am trying to check if a string is a working call that can be executed. To do this I parse the string, get the first word, and if it matches a database of predefined functions, it should succeed.
Q has the string, A will be used later, not now. Example of string is: append a and b.
is_uni(Q, A):-
split_string(Q, " ", ",", [X|Y]),
uni_db(Z),
member(X, Z).
uni_db([
append,
member,
append1
]).
You need to use atom_codes/2 predicate to convert stings to atoms, in example you need to convert "append" to append in order to work.
is_uni(Q,A):-
split_string(Q, " ", ",", [X|Y]),
atom_codes(W,X),
uni_db(Z),
member(W, Z).
Example:
?- is_uni("append a and b",A).
true ;
false.
You are confusing strings with atoms.
"append" and 'append', a.k.a. append, are different. You can use atom_string/2 to convert between them:
..., atom_string(A, X), ...
You are re-implementing built-in features.
Why store commands in a list and iterate with member/2? Just define some facts:
uni_db(append).
uni_db(member).
uni_db(append1).
And then, you simply have to check whether uni_db(A). This is better supported by an implementation and more likely to be done efficiently.
I want to use atom_chars/2 on the expression of 3+4, but I get
ERROR: atom_chars/2: Type error: 'atom' expected, found '3+4' (a compound).
I'm thinking that if I can add " " on both sides of the compound, it would work, e.g.
atom_chars("3+4", Result).
but I don't know how I can do that, or is there other approaches to do this?
Please give me some advice.
EDIT: What I mean is that the input has to be 3+4, instead of '3+4', so what I want to do is to write a predicate before the atom_chars/2 to convert 3+4 to '3+4'.
For instance: for compound2atom(X,Y),
-?compound2atom(3+4,Y).
Y='3+4'.
If you are using SWI-Prolog, there is with_output_to/2 or format/3:
?- with_output_to(atom(A), write(3+4)).
A = '3+4'.
?- with_output_to(chars(C), write(3+4)).
C = ['3', +, '4'].
?- format(atom(A), "~w", [3+4]).
A = '3+4'.
?- format(chars(C), "~w", [3+4]).
C = ['3', +, '4'].
But if you look hard enough you should be able to find some predicate that does that, for example term_to_atom/2.
My personal preference leans towards format/3.
Given this program, why am I forced to define every atom in the predicate, even if they're anonymous. Why is it that undefined variables in a dict predicate aren't thought of as anonymous?
funt2(X) :-
X = point{x:5, y:6}.
evalfunt(point{x:5, y : 6}) :-
write('hello world!').
evalfunt(point{x:_, y : _} ) :-
write('GoodBye world!').
Why can't I just say
evalfunt(point{x:5}) :-
write('GoodBye world!').
^that won't match, by the way.
I may as well just use a structure if I have to define every possible value in the dict to use dicts.
What's the motivation here? Can I do something to make my predicate terse? I'm trying to define a dict with 30 variables and this is a huge roadblock. It's going to increase my program size by a magnitude if I'm forced to define each variables (anonymous or not).
Dict is just a complex data type, like tuple, which has data AND structure. If you have, for example two facts:
fact(point{x:5, y:6}).
fact(point{x:5}).
Then the query
fact(point{x:_}).
will match the second one, but not the first one.
And the query
fact(point{x:_, y:_}).
Will match the first one, but not the second.
Now, if you want to match facts of the form fact(point{x:_, y:_, z:_}) only by one specific field, you can always write a helper rule:
matchByX(X, P) :- fact(P), P=point{x:X, y:_, z:_}.
So having facts:
fact(point{x:5, y:6, z:1}).
fact(point{x:1, y:2, z:3}).
fact(point{x:2, y:65, z:4}).
and quering
matchByX(1, P).
will return:
P = point{x:1, y:2, z:3}
UPDATE:
Moreover, in SWI-Prolog 7 version the field names can be matched as well, so it can be written in much more generic way, even for facts with different structures:
fact(point{x:5, y:6, z:1}).
fact(point{x:1, y:2}).
fact(point{x:2}).
fact(point{x:2, y:2}).
matchByField(F, X, P) :- fact(P), P.F = X.
So query:
?- matchByField(x, 2, P).
P = point{x:2} ;
P = point{x:2, y:2}.
I was able to accomplish what I needed by doing the following
checkiffive(Y) :-
get_dict(x, Y, V), V=5.
You need to use the built in methods for unifying values from a dict.
Described in chapter 5.4 of the SWI prolog reference
http://www.swi-prolog.org/download/devel/doc/SWI-Prolog-7.1.16.pdf
I would like to determine in Prolog the type of a string of characters, if it is alphabetic, alphanumeric or numeric.
For example:
"I use this page" alphabetic
"0c0d24e" alphanumeric
How can i do?
the predicate available is char_type/2, or better, code_type/2.
To apply to each code in string, use maplist/2. The only problem it's the wrong arguments order of code_type. Then a service predicate is needed (or download lambda, if you're using SWI-Prolog, with ?- pack_install(lambda).).
Without lambda:
code_type_(X,Y) :- code_type(Y,X).
?- maplist(code_type_(alpha), "abc").
true.
With lambda:
?- [library(lambda)].
?- maplist(\C^code_type(C,alpha), "abc").
true.
edit after comments, it's apparent that more flexible parsing is required. A DCG it's the recommended way to go: library(dcg/basics) offers some prebuilt 'categorizer', and highlights the proper way to write your own, combining with code_type: for instance, here is a recently added rule:
%% prolog_var_name(-Name:atom)// is semidet.
%
% Matches a Prolog variable name. Primarily intended to deal with
% quasi quotations that embed Prolog variables.
prolog_var_name(Name) -->
[C0], { code_type(C0, prolog_var_start) }, !,
prolog_id_cont(CL),
{ atom_codes(Name, [C0|CL]) }.
prolog_id_cont([H|T]) -->
[H], { code_type(H, prolog_identifier_continue) }, !,
prolog_id_cont(T).
prolog_id_cont([]) --> "".
see how code_type/2 is used to qualify single characters...
more edit - note: untested
qualify_atom(Atom, Type) :-
atom_codes(Atom, Codes),
qualify_codes(Codes, Type).
qualify_codes(Codes, Type) :-
( maplist(code_type_(alnum), Codes)
-> Type = alnum
; maplist(code_type_(alpha), Codes)
-> Type = alpha
; Type = unknown
).
then, to work on a list
?- maplist(qualify_atom, Atoms, Types).
edit
An update of this answer is mandatory: since library(yall) has been released in SWI-Prolog, and is autoloaded, we can now write:
?- maplist([C]>>code_type(C,alpha), `abc`).
Also, note the change in literal representation: double quotes in SWI-Prolog ver.7+ don't represent anymore a list of character codes.
I am new to prolog, and using BProlog.
I have been reading some example program to execute query on group of related data. But in order to infer from facts with similar structure, they wrote many predicates like search_by_name,search_by_point, which are partly duplicated.
% working search in example
search_by_name(Key,Value) :-
Key == name,
sname(ID,Value),
point(ID,Point),
write(Value),write(Point),nl.
And when I try to replace them with a more general version like this:
% a more general search I want to write
% but not accepted by BProlog
search_by_attr(Key,Value) :-
Key(ID,Value),
sname(ID,Name),
point(ID,Point),
write(Name),write(Point),nl.
error arised:
| ?- consult('students.pl')
consulting::students.pl
** Syntax error (students.pl, 17-21)
search_by_attr(Key,Value) :-
Key<<HERE>>(ID,Value),
sname(ID,Name),
point(ID,Point),
write(Name),write(Point),nl.
1 error(s)
Am I doing it the wrong way, or is such subtitution impossible in prolog?
code and example data can be found at https://gist.github.com/2426119
I don't know any Prolog that accept variables functors.
There is call/N, or univ+call/1.
search_by_attr(Key,Value) :-
call(Key, ID, Value), % Key(ID,Value)
...
or
search_by_attr(Key,Value) :-
C =.. [Key, ID, Value], % univ
call(C), % Key(ID,Value)
...