Prolog convert strings to pair list - string

I'm learning Prolog and I need idea how to convert a list of strings:
['f(a,45)', 'f(b,13)', 'f(c,12)']
into a list of pairs that looks like this:
[[45,'a'],[13,'b'],[12,'c']]

That's a list of atoms, not strings. Strings in Prolog are usually list of character codes, expressed like
["f(a,45)", "f(b,13)", "f(c,12)"]
Anyway, apply a conversion to each element with a recursive function:
convert([], []).
convert([Atom|Atoms], [Pair|Pairs]) :-
convert_element(Atom, Pair),
convert(Atoms, Pairs).
Instead of recursion you could use maplist/3 in this way:
convert(As, Ps) :- maplist(convert_element, As, Ps).
To convert an element, you need a parser. DCGs are convenient:
convert_element(Atom, [N, A]) :-
atom_codes(Atom, Codes),
phrase(("f(", atomc(Ac), ",", numc(Nc), ")"), Codes, []),
atom_codes(A, Ac),
number_codes(N, Nc).
atomc(A) --> ([C], {is_lowerc(C)}, atomc(Cs), {A = [C|Cs]}) ; {A = []}.
numc(N) --> ([C], {is_numc(C)}, numc(Cs), {N = [C|Cs]}) ; {N = []}.
is_lowerc(C) :- C #>= 0'a, C #=< 0'z.
is_numc(C) :- C #>= 0'0, C #=< 0'9.
test:
?- convert(['f(a,45)', 'f(b,13)', 'f(c,12)'],L).
L = [[45, a], [13, b], [12, c]] .
atomc//1 & numc//1 are expressed in a compact way, but are very simple recursive pattern matching procedures, i.e. atomc//1 could be
atomc([C|Cs]) --> [C], {is_lowerc(C)}, !, atomc(Cs).
atomc([]) --> [].

Alternatively, you can use this code :
convert :-
L = ["f(a,45)", "f(b,13)", "f(c,12)"],
maplist(extract_args,L, LA),
writeln(LA).
extract_args(S, [A1,A2]) :-
string_to_atom(S, A),
term_to_atom(T, A),
T =..[_, A2, A1].
If you have atoms, just use term_to_atom/2 in extract_args/2.

Related

How to concatenate strings in Prolog

I'm trying to make an IO-related exercise in Prolog but find it difficult working with strings.
The task is to take an input (an integer 1<=n<=180) and write is as a sum of three terms k*d where k=1,2, or 3 and 1<=d<=20.
For instance:
input: 180
output: triple 20, triple 20, triple 20
input: 96
output: triple 19, double 15, single 9
My problem is that I get error message:
"[some predicate I have tried]: Arguments are not sufficiently
instantiated".
Last thing I tried was a concatenate-predicate I found on another thread at StackOverflow Concatenation of Strings in Prolog. I think it looks nice but I still have the same problem. See my code below.
Before I used string_concat/3 instead.
main :-
repeat,
read(X),
(
X == end_of_file
;
integer(X),
dart_scores(X,N),
write(N),
fail
).
dart_scores(X,N) :-
concatenate([A1,B1,C1],N),
concatenate(["single", A], A1),
concatenate(["double", B], B1),
concatenate(["triple", C], C1),
find_values(X,A,B,C).
find_values(X,A,B,C) :-
X is A+B*2+C*3,
in_domain(A),
in_domain(B),
in_domain(C).
in_domain(D) :-
integer(D),
D>=1,
20>=D.
concatenate(StringList, StringResult) :-
maplist(atom_chars, StringList, Lists),
append(Lists, List),
atom_chars(StringResult, List).
The error you have has nothing to with string concatenation. I have modified your find_values and in_domain predicate to get rid of the error. The problem with your in_domain predicate is that it does not "generate" integers. As for the find_values predicate, you need to unify first A, B, and C with some integers before checking X is A+B*2+C*3, and generate integer for "single", "double", and "triple". Hope this will help you!
main :-
repeat,
read(X),
(
X == end_of_file
;
integer(X),
dart_scores(X,N),
write(N) /*,
fail */
).
dart_scores(R,N) :-
find_values(R,A,B,C,X,Y,Z),
mult(X, A1),
mult(Y, B1),
mult(Z, C1),
concatenate([A1,A], A2),
concatenate([B1,B], B2),
concatenate([C1,C], C2),
concatenate([A2,B2,C2],N).
mult(1, "single").
mult(2, "double").
mult(3, "triple").
find_values(R,A,B,C,X,Y,Z) :-
in_domain(A),
in_domain(B),
in_domain(C),
range(X,1,3),
range(Y,1,3),
range(Z,1,3),
R is A*X+B*Y+C*Z.
in_domain(D) :-
range(D, 1, 20).
range(Low, Low, _).
range(Out, Low, High) :- NewLow is Low+1, NewLow =< High, range(Out, NewLow, High).
concatenate(StringList, StringResult) :-
maplist(atom_chars, StringList, Lists),
append(Lists, List),
atom_chars(StringResult, List).

Replacing substring in second occurrence in prolog

First of all, this is not a homework. I'm studying Computer Sciences in my home, to learn a little more alone.
I'm doing an excercise. It says like this:
Construct a predicate called replaceAtomsString/4 so that given
a string s as the first parameter, a number N as the second parameter,
and a pair of atoms [g, h] (list) as the third parameter, unify in a
fourth parameter the replacement in the Nth apparition of g in s
replacing it by h. Example:
replaceAtomsString (sAbbbsAbbasA, 2, [sA, cc], X) should result in
X = sAbbbccbbasA
So, my first approach was trying to build a list with the string, just like prolog do with every string. After all, i've built this code:
substitute(X, S, T, Y) :-
append(S, Xt, X), % i.e. S is the first part of X, the rest is Xt
!,
substitute(Xt, S, T, Yt),
append(T, Yt, Y).
substitute([Xh|Xt], S, T, [Xh|Yt]) :-
substitute(Xt, S, T, Yt).
But it returns false on every attempt.
Any ideas?
Since you need substantial work to get your code done, here is how to perform the task using the available libraries.
sub_atom/5 it's a rather powerful predicate to handle atoms. Coupled with call_nth/2, the solution is straightforward and more general than what would result coding the loop around N.
replaceAtomsString(S,N,[G,H],X) :-
call_nth(sub_atom(S,Before,_,After,G),N),
sub_atom(S,0,Before,_,Left),
sub_atom(S,_,After,0,Right),
atomic_list_concat([Left,H,Right],X).
Example running your query, but leaving N to be computed:
?- replaceAtomsString(sAbbbsAbbasA, N, [sA, cc], X).
N = 1,
X = ccbbbsAbbasA ;
N = 2,
X = sAbbbccbbasA ;
N = 3,
X = sAbbbsAbbacc ;
false.

Prolog character string list product, can't figure out recursion

one product/3
one_product(+Nonterminal,+Cell,-Product)
this is the closest I've gotten but I can't get the string to multiply with the tail. I've done research on this for a whole week.
conca(String,[H|T],Product) :-
string_concat(String,H,Combined),
append([Combined],T,Product).
one_product(String,Cell,Product) :-
conca(String,Cell,Product).
With that I get
?- one_product("A",["B","C"],What).
What = ["AB", "C"].
A part of another attempt.
Product = String * H.
These are single case attempts.
conca([],[_],Product) :-
Product = [].
conca(String,[],Product) :-
Product = [].
These are examples
?- one_product([],["B"],What).
What = [].
?- one_product("A",["B"],What).
What = ["AB"].
?- one_product("A",["B","C"],What).
What = ["AB", "AC"].
?- one_product("A",[],What).
What = [].
You'll also notice a product with an empty cell is empty.
You don't don't want to use append/3 for something like this. You want a recursive list process:
one_product(_, [], []).
one_product(String, [H|T], [StringH|StringT]) :-
string_concat(String, H, StringH),
one_product(String, T, StringT).
This is a common pattern in Prolog and can be done using maplist/3:
one_product(String, StringList, Product) :-
maplist(string_concat(String), StringList, Product).
This will concatenate (prepend) string String with every string element of list StringList resulting in list Product.
Now, the only issue in the above is with your case:
one_product([],["B"],What).
This case uses a list for the first argument rather than a string. Is that the intent? If so, you can treat it separately:
one_product([], _, []).
one_product(String, StringList, Product) :-
string(String),
maplist(string_concat(String), StringList, Product).

Translate list comprehension to Prolog

I have a list comprehension in Haskell that I want to translate to Prolog.
The point of the list comprehension is rotating a 4 by 4 grid:
rotate :: [Int] -> [Int]
rotate grid = [ grid !! (a + 4 * b) | a <- [0..3], b <- [0..3] ]
Now in Prolog, I translated it like this:
rotateGrid([T0,T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11,T12,T13,T14,T15],
[T0,T4,T8,T12,T1,T5,T9,T13,T2,T6,T10,T14,T3,T7,T11,T15]).
Can we do better?
We can use findall/3 for list comprehensions (Cf. the SWI-Prolog Documentation). E.g.,
?- findall(X, between(1,10,X), Xs).
Xs = [1,2,3,4,5,6,7,8,9,10]
Xs is a list holding all values that can unify with X when X is a number between 1 and 10. This is roughly equivalent to the Haskell expression let Xs = [x | x <- [1..10]](1). You can read a findall/3 statement thus: "find all values of [First Argument] such that [Conditions in Second Argument] hold, and put those values in the list, [Third Argument]".
I've used findall/3 to write a predicate rotate_grid(+Grid, ?RotatedGrid). Here is a list of the approximate Haskell-Prolog equivalences I used in the predicate; each line shows the relation between the value that the Haskell expression will evaluate to and the Prolog variable with the same value:
a <- [0..3] = A in between(0, 3, A)
b <- [0..3] = B in between(0, 3, B)
(a + 4 * d) = X in X is A + 4 * D
<Grid> !! <Index> = Element in nth0(Index, Grid, Element)
Then we simply need to find all the values of Element:
rotate_grid(Grid, RotatedGrid) :-
findall( Element,
( between(0,3,A),
between(0,3,B),
Index is A + 4 * B,
nth0(Index, Grid, Element) ),
RotatedGrid
).
To verify that this produces the right transformation, I down-cased the Prolog code from the question and posed the following query:
?- rotate_grid([t0,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15],
[t0,t4,t8,t12,t1,t5,t9,t13,t2,t6,t10,t14,t3,t7,t11,t15]).
| true.
Footnotes:
(1): between/3 isn't actually the analogue of [m..n], since the latter returns a list of values from m to n where between(M,N,X) will instantiate X with each value between M and N (inclusive) on backtracking. To get a list of numbers in SWI-Prolog, we can use numlist(M,N,Ns). So a stricter analogue for x <- [1.10] would be the conjunction member(X, Ns), numlist(1, 10, Ns).
You want a permutation of a list. The concrete elements are not considered. Therefore, you can generalize your Haskell signature to
rotate :: [x] -> [x]
This is already a very valuable hint for Prolog: the list's elements will not be considered - elements will not even be compared. So a Prolog solution should be able to handle variables directly, like so:
?- rotateGrid(L,R).
L = [_A,_B,_C,_D,_E,_F,_G,_H,_I,_J,_K,_L,_M,_N,_O,_P],
R = [_A,_E,_I,_M,_B,_F,_J,_N,_C,_G,_K,_O,_D,_H,_L,_P].
And your original definition handles this perfectly.
Your version using list comprehensions suggests itself to be realized via backtracking, certain precautions have to be taken. Using findall/3, as suggested by #aBathologist will rename variables:
?- length(L,16),rotate_grid(L,R).
L = [_A,_B,_C,_D,_E,_F,_G,_H,_I,_J,_K,_L,_M,_N,_O,_P],
R = [_Q,_R,_S,_T,_U,_V,_W,_X,_Y,_Z,_A1,_B1,_C1,_D1,_E1,_F1].
The built-in predicate bagof/3 addresses this problem. Note that we have to declare all local, existential variables explicitly:
rotate_grid2(Grid, RotatedGrid) :-
bagof(
Element,
A^B^Index^ % declaration of existential variables
( between(0,3,A),
between(0,3,B),
Index is A + 4 * B,
nth0(Index, Grid, Element)
),
RotatedGrid).
For lists that are shorter than 16 elements, the Haskell version produces a clean error, but here we get pretty random results:
?- L=[1,2,3,4],rotate_grid(L,R).
L = [1,2,3,4], R = [1,2,3,4].
?- L=[1,2,3,4,5],rotate_grid(L,R).
L = [1,2,3,4,5], R = [1,5,2,3,4].
This is due to the unclear separation between the part that enumerates and "generates" a concrete element. The cleanest way is to add length(Grid, 16) prior to the goal bagof/3.
List comprehensions in Prolog
Currently, only B-Prolog offers a form of list comprehensions:
R#=[E: A in 0..3,B in 0..3,[E,I],(I is A+4*B,nth0(I,L,E))].
However, it does not address the second problem:
| ?- L = [1,2,3], R#=[E: A in 0..3,B in 0..3,[E,I],(I is A+4*B,nth0(I,L,E))].
L = [1,2,3]
R = [1,2,3]
yes
Use a loop predicate foreach/4
If the comprehension should retain variables, which is for example important in constraint programming, a Prolog system could offer a predicate foreach/4. This predicate is the DCG buddy of foreach/2.
Here is how variables are not retained via findall/3, the
result R contains fresh variables according to the ISO
core semantics of findall/3:
Welcome to SWI-Prolog (threaded, 64 bits, version 7.7.1)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
?- functor(L,foo,5), findall(X,
(between(1,5,N), M is 6-N, arg(M,L,X)), R).
L = foo(_5140, _5142, _5144, _5146, _5148),
R = [_5210, _5204, _5198, _5192, _5186].
And here is how variables can be retained via foreach/4,
the resulting list has the same variables as the compound
we started with:
Jekejeke Prolog 3, Runtime Library 1.3.0
(c) 1985-2018, XLOG Technologies GmbH, Switzerland
?- [user].
helper(N,L) --> [X], {M is 6-N, arg(M,L,X)}.
Yes
?- functor(L,foo,5), foreach(between(1,5,N),helper(N,L),R,[]).
L = foo(_A,_G,_M,_S,_Y),
R = [_Y,_S,_M,_G,_A]
Using foreach/4 instead of bagof/3 might seem a little bit over the top. foreach/4 will probably only show its full potential when implementing Picat loops, since it can build up constraints, what bagof/3 cannot do.
foreach/4 is an implementation without the full materialization of all solution that are then backtracked. It shares with bagof/3 the reconstruct of variables, but still allows backtracking in the conjunction of the closures.

Prolog importing facts from a formatted text file

I have the following input in a text file input.txt
atom1,atom2,atom3
relation(atom1 ,[10,5,2])
relation(atom2 ,[3,10,2])
relation(atom3 ,[6,5,10])
First line includes the list of atoms used in relation predicates in the file and each remaining line represents a relation predicate in order of the first line list.relation(atom1, [x,y,z]) means atom1 has a relation value of 10 with first atom, 5 with the second and 2 with the third
I need to read this file and add represent relation values for each atom seperately.For example , these are the relation values which will be added for atom1 :
assert(relation(atom1, atom1,10)).
assert(relation(atom1, atom2, 5)).
assert(relation(atom1, atom3, 2)).
I have read some prolog io tutorials and seen some recommendations on using DCG but I'm a beginner prolog programmer and having trouble to choose the method for the solving problem. So I'm here to ask help from experienced prolog programmers.
Since you didn't stated what Prolog you're using, here is a snippet written in SWI-Prolog. I attempted to signal non ISO builtins by means of SWI-Prolog docs reference.
parse_input :-
open('input.txt', read, S),
parse_line(S, atoms(Atoms)),
repeat,
( parse_line(S, a_struct(relation(A, L)))
-> store(Atoms, A, L), fail
; true ),
close(S).
:- meta_predicate(parse_line(+, //)).
parse_line(S, Grammar) :-
% see http://www.swi-prolog.org/pldoc/doc_for?object=read_line_to_codes/2
read_line_to_codes(S, L),
L \= end_of_file,
phrase(Grammar, L).
% match any sequence
% note - clauses order is mandatory
star([]) --> [].
star([C|Cs]) --> [C], star(Cs).
% --- DCGs ---
% comma sep atoms
atoms(R) -->
star(S),
( ",",
{atom_codes(A, S), R = [A|As]},
atoms(As)
; {atom_codes(A, S), R = [A]}
).
% parse a struct X,
% but it's far easier to use a builtin :)
% see http://www.swi-prolog.org/pldoc/doc_for?object=atom_to_term/3
a_struct(X, Cs, []) :-
atom_codes(A, Cs),
atom_to_term(A, X, []).
% storage handler
:- dynamic(relation/3).
store(Atoms, A, L) :-
nth1(I, L, W),
nth1(I, Atoms, B),
assertz(relation(A, B, W)).
with the sample input.txt, I get
?- parse_input.
true .
?- listing(relation).
:- dynamic relation/3.
relation(atom1, atom1, 10).
relation(atom1, atom2, 5).
relation(atom1, atom3, 2).
relation(atom2, atom1, 3).
relation(atom2, atom2, 10).
relation(atom2, atom3, 2).
relation(atom3, atom1, 6).
relation(atom3, atom2, 5).
relation(atom3, atom3, 10).
HTH

Resources