|Did you know ...||Search Documentation:|
|The string type and its double quoted syntax|
As of SWI-Prolog version 7, text enclosed in double quotes
"Hello world") is read as objects of the type string.
A string is a compact representation of a character sequence that lives
on the global (term) stack. Strings represent sequences of Unicode
characters including the character code 0 (zero). The length strings is
limited by the available space on the global (term) stack (see
Strings are distinct from lists, which makes it possible to detect them
at runtime and print them using the string syntax, as illustrated below:
?- write("Hello world!"). Hello world! ?- writeq("Hello world!"). "Hello world!"
Back quoted text (as in
`text`) is mapped to a
list of character codes in version 7. The settings for the flags
that control how double and back quoted text is read is summarised in
Programs that aim for compatibility should realise that the ISO standard
defines back quoted text, but does not define the back_quotes
Prolog flag and does not define the term that is produced by back quoted
|Version 7 default||string||codes|
Section 5.2.4 motivates the introduction of strings and mapping double quoted text to this type.
Strings may be manipulated by a set of predicates that is similar to the manipulation of atoms. In addition to the list below, string/1 performs the type check for this type and is described in section 4.5.
SWI-Prolog's string primitives are being synchronized with ECLiPSe. We expect the set of predicates documented in this section to be stable, although it might be expanded. In general, SWI-Prolog's text manipulation predicates accept any form of text as input argument and produce the type indicated by the predicate name as output. This policy simplifies migration and writing programs that can run unmodified or with minor modifications on systems that do not support strings. Code should avoid relying on this feature as much as possible for clarity as well as to facilitate a more strict mode and/or type checking in future releases.
"1e10"is a valid number.
quoted(true)and the result is converted to String.
?- term_string(Term, 'a(A)', [variable_names(VNames)]). Term = a(_G1466), VNames = ['A'=_G1466].
''is ambiguous and interpreted as an empty string.
string_code(-,+,+)is deterministic if the searched-for Code appears only once in String. See also sub_string/5.
% a simple split ?- split_string("a.b.c.d", ".", "", L). L = ["a", "b", "c", "d"]. % Consider sequences of separators as a single one ?- split_string("/home//jan///nice/path", "/", "/", L). L = ["home", "jan", "nice", "path"]. % split and remove white space ?- split_string("SWI-Prolog, 7.0", ",", " ", L). L = ["SWI-Prolog", "7.0"]. % only remove leading and trailing white space ?- split_string(" SWI-Prolog ", "", "\s\t\n", L). L = ["SWI-Prolog"].
In the typical use cases, SepChars either does not overlap PadChars or is equivalent to handle multiple adjacent separators as a single (often white space). The behaviour with partially overlapping sets of padding and separators should be considered undefined. See also read_string/5.
name_value(String, Name, Value) :- sub_string(String, Before, _, After, "="), !, sub_string(String, 0, Before, _, NameString), atom_string(Name, NameString), sub_string(String, _, After, 0, Value).
?- atomics_to_string([gnu, "gnat", 1], ', ', A). A = "gnu, gnat, 1"
The predicate read_string/5 called repeatedly on an input until Sep is -1 (end of file) is equivalent to reading the entire file into a string and calling split_string/4, provided that SepChars and PadChars are not partially overlapping.155Behaviour that is fully compatible would require unlimited look-ahead. Below are some examples:
% Read a line read_string(Input, "\n", "\r", End, String) % Read a line, stripping leading and trailing white space read_string(Input, "\n", "\r\t ", End, String) % Read upto , or ), unifying End with 0', or 0') read_string(Input, ",)", "\t ", End, String)
With the introduction of strings as a Prolog data type, there are three main ways to represent text: using strings, atoms or code lists. This section explains what to choose for what purpose. Both strings and atoms are atomic objects: you can only look inside them using dedicated predicates. Lists of character codes are compound data structures.
'Boeing 747'), but also individual words in a natural language processing system. They are also used where other languages would use enumerated types, such as the names of days in the week. Unlike enumerated types, Prolog atoms do not form a fixed set and the same atom can represent different things in different contexts.
The predicates in this section can help adapting your program to the new convention for handling double quoted strings. We have adapted a huge code base with which we were not familiar in about half a day.
[X] = "a"into
X = 0'a.
:- set_prolog_flag(double_quotes, codes).
`text`). Note that this will not make your code run regardless of the --traditional command line option and code exploiting this mapping is also not portable to ISO compliant systems.
:- multifile check:string_predicate/1. check:string_predicate(user:help_info/2).
:- multifile check:valid_string_goal/1. check:valid_string_goal(system:format(_,S,_)) :- string(S).
Prolog defines two forms of quoted text. Traditionally, single quoted text is mapped to atoms while double quoted text is mapped to a list of character codes (integers) or characters represented as 1-character atoms. Representing text using atoms is often considered inadequate for several reasons:
Representing text as a list of character codes or 1-character atoms also comes at a price:
s("hello world")could be used to indicate that we are dealing with a string.
Lacking runtime information, debuggers and the toplevel can only use heuristics to decide whether to print a list of integers as such or as a string (see portray_text/1).
While experienced Prolog programmers have learned to cope with this, we still consider this an unfortunate situation.
We observe that in many programs, most strings are only handled as a single unit during their lifetime. Examining real code tells us that double quoted strings typically appear in one of the following roles:
[X] = "a"is a commonly used template for getting the character code of the letter’a'. ISO Prolog defines the syntax
0'afor this purpose. Code using this must be modified. The modified code will run on any ISO compliant processor.
append("name:", Rest, Codes). Such code needs to be modified. In this particular example, the following is a good portable alternative:
phrase("name:", Codes, Rest)
memberchk(C, "~!@#$"). This is a rather inefficient check in a traditional Prolog system because it pushes a list of character codes cell-by-cell the Prolog stack and then traverses this list cell-by-cell to see whether one of the cells unifies with C. If the test is successful, the string will eventually be subject to garbage collection. The best code for this is to write a predicate as below, which pushes nothing on the stack and performs an indexed lookup to see whether the character code is in‘my_class'.
my_class(0'~). my_class(0'!). ...
An alternative to reach the same effect is to use term expansion to create the clauses:
term_expansion(my_class(_), Clauses) :- findall(my_class(C), string_code(_, "~!@#$", C), Clauses). my_class(_).
Finally, the predicate string_code/3 can be exploited directly as a replacement for the memberchk/2 on a list of codes. Although the string is still pushed onto the stack, it is more compact and only a single entity.
We offer the predicate list_strings/0 to help porting your program.