4 ms·
In SICStus Prolog, the query I showed yields C = 108 by default, because "Hello, world!" is interpreted as the list of character codes [72,101,108,...]. In con
by triska 4y ago
In SICStus Prolog, the query I showed yields C = 108 by default, because "Hello, world!" is interpreted as the list of character codes [72,101,108,...].
In contrast, in all the recent systems I mentioned, the double-quoted string "Hello, world!" is interpreted as the list of atoms of length one ['H',e,l,l,o,...]. This is preferable because it is more readable, and also because reasoning about such atoms in Prolog programs does not depend on any system-specific character encoding.
In all ISO conforming systems, we can switch between these two interpretations of double-quoted strings by setting the Prolog flag double_quotes to codes and chars, respectively. The interesting phenomenon I mentioned is that both the earliest and the most recent Prolog systems acted as if this flag (which was not available yet in the first Prolog systems) was set to chars, interrupted by a number of years where it was set to a different default value in the Edinburgh tradition of Prolog systems. It is interesting because the newest Prolog systems follow the Marseille tradition in this respect, which precedes the Edinburgh tradition.
Prolog does not have a native "string" type, neither SICStus nor the other systems I mentioned have it, because such a type does not fit into the language. Still, we often call a list of characters a string, much like we call a char array a string in C, even though C does not have a native string type either.
One can force an ISO conforming Prolog system to interpret double-quoted strings as atoms, i.e., to interpret "Hello, world!" as the atom 'Hello, world!', by setting the flag to the value atom. It is preferable to set it to chars though, because this allows the application of DCGs for reasoning about strings.
- YeGoblynQueenne 4y agoThanks. Prolog doesn't have a native string type- indeed. And so atoms must be converted into lists. That's were trouble begins, I fear. Split the atom, and what do you get? Fallout. I'll get my coat.
- triska 4y agoI would phrase it like this: If you are using, in Prolog code, an atom of length greater than 1, then consider using a list of characters instead, especially if you want to reason about the data itself within your program. If you use lists of characters throughout, then no conversion to or from longer atoms is needed. For example, in the sample queries you posted, instead of the atom abcd, consider using the list of characters "abcd" (i.e., [a,b,c,d]) instead, because such lists of characters can be readily described and analyzed with DCGs. In addition to this convenience, as a secondary concern, lists of characters provide an efficiency advantage, because they can be more transiently allocated on the Prolog heap instead of in the atom table, and are therefore immediately reclaimed on backtracking. Characters, i.e., 1-char atoms, are indeed a good use case of atoms. For anything longer, lists of characters are in many cases a better option: In addition to their being amenable to reasoning with DCGs and the mentioned efficiency advantage, they also allow partial instantiations, such as [a,b,c|Ls] and [a,B,c,d], a feature that atoms do not support. Hence, when using atoms, the code tends to become more moded, whereas lists of characters readily allow generalizations. Existing and upcoming Prolog implementation techniques allow a very compact internal representation of lists of characters as packed UTF-8 encoded sequences of bytes, and also a very efficient representation of each character as a single heap cell. Scryer Prolog and Trealla Prolog are the first Prolog systems that provide such an efficient internal representation of lists of characters, and are therefore already especially useful for efficient text processing.
- YeGoblynQueenne 4y ago>> I would phrase it like this: If you are using, in Prolog code, an atom of length greater than 1, then consider using a list of characters instead, especially if you want to reason about the data itself within your program. If you use lists of characters throughout, then no conversion to or from longer atoms is needed. That's a very good point and I agree. It reminds a little of how, in formal texts, constants are most often single characters, especially when we just want to refer to some abstract constant in which case we say "c" or "a" or "b" or "α", "β", "γ" and whatever (I do the latter because I have a Greek keyboard and it's easy). But there's a problem with that. In many cases in everyday Prolog programming it makes a lot of sense to represent constants by more than one character. For example, in the perennial family tree motivating example for ILP, you have (FOL) atoms like father(bob,alice) and mother(alice,dhalia) etc. That makes for a very natural notation- "bob" is clearly the name of a person. And then we end up with multi-character atoms that we might need to split or concatenate. Representing such constants as lists of characters would be cumbersome and unnatural- for example father([b,o,b],[a,l,i,c,e]), looks awful, even if it's actually very practical. That's partly what the double-quoted atoms are supposed to achieve, but father("bob", "alice") again introduces unnecessary visual clutter. Perhaps the standard should be for multi-character atoms (or, well, all atoms) to be expanded to lists of atoms by the interpreter by default (without having to double quote) so that you know that writing father(bob,alice) is exactly the same as writing father([b,o,b],[a,l,i,c,e]) and you can then do some list processing, e.g. starting with father(X,Y), then append(X,...) and so on. To be honest, I don't know much about how data are represented in memory in a Prolog engine- I think you know much more about all that, than me. So I can't imagine what the right answer is. A compact, efficient representation of lists of characters sounds useful, for sure.
- triska 4y agoIn my experience, data in Prolog programs will look like alice, bob and dhalia only in very artificial examples or exercises. For example, I have never seen a person's name spelled bob, and spelled like this, it is rather unlikely to be the name of a person one would represent in a Prolog program. In Prolog programs that arise in practice, names are far more likely to look like "Alice", "King Solomon's Mines" and "La Grande Bellezza", and these names would need single-quotes or escape sequences for atoms too.