3
votes

I would like to know what exactly string predicate in Swi-prolog is used for ? The example and definition is mentioned here: http://www.swi-prolog.org/pldoc/doc_for?object=string/1 But I never tried something and return true, so I was wondering if someone can give me more detailed and also provide example where it returns true and false to find the differences ?

Thanks,

2

2 Answers

1
votes

Strings are basically atoms that don't go into the atom table. There is a little advantage of having strings, since the atom table is mainly used to invoke predicates and but not very useful when you have many distincts atoms.

The need for strings has recently been a little bit mitigated, since some Prolog systems also feature atom table garbage collection. Some Prolog systems even don't have an atom table at all.

Here is a simple test case to see performance of atoms, that they often can be used instead of strings if either atom table garbage collection is present or if the Prolog system doesn't have an atom table.

SWI-Prolog atoms, some atom table GC:

?- time(test).
% 8,209,791 inferences, 1.125 CPU in 1.140 seconds (99% CPU, 7297592 Lips)
false.

SWI-Prolog strings:

?- time(test2).
% 8,209,791 inferences, 0.750 CPU in 0.749 seconds (100% CPU, 10946388 Lips)
false.

Jekejeke Prolog atoms, no atom table:

?- time(test).
% Up 1,398 ms, GC 14 ms, Thread Cpu 1,360 ms (Current 08/18/18 20:35:56)
No

So I guess there is some impact of strings. Maybe a better solution than SWI-Prolog strings would be atoms that can automatically also serve as strings on demand, like in Jekejeke Prolog. This would much less blow up the number of built-ins.

P.S.: I used the following test code:

test :- 
   between(1,127,A), between(1,127,B), between(1,127,C), 
   atom_codes(X,[A,B,C]), atom_codes(X, L), L\==[A,B,C].

test2 :- 
   between(1,127,A), between(1,127,B), between(1,127,C), 
   string_codes(X,[A,B,C]), string_codes(X, L), L\==[A,B,C].
0
votes

I guess you should read the page which describes what string are and why to use them, too:

http://www.swi-prolog.org/pldoc/man?section=strings

This looks quite detailed to me. Here are some strings:

?- string_to_atom(String, foobar), string(String).
String = "foobar".

?- string_to_list(String, [104,109,109]), string(String).
String = "hmm".

In other words, you can use the string built-ins to make strings. They are different from other types because other types are not strings?...