Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between ' and " in Prolog?

I am new to Prolog and noticed that ' and " give different behavior, but am curious as to why. Specifically, when loading a file, ?- ['test1.pl']. works, while ?- ["test1.pl"]. doesn't.

like image 899
astay13 Avatar asked Nov 25 '11 04:11

astay13


People also ask

What is the difference between \= and \== in Prolog?

\= means the two terms cannot be unified, i.e. that unification fails. As with all applications of negation as failure, "not unified" does not (and cannot) result in any unification between terms. \== means the two terms are not identical. Here also no unification takes place even if this succeeds.

What does \+ mean in Prolog?

Because of the problems of negation-as-failure, negation in Prolog is represented in modern Prolog interpreters using the symbol \+ , which is supposed to be a mnemonic for not provable with the \ standing for not and the + for provable.

What is the or symbol in Prolog?

Performing an "or" in Prolog can also be done with the "disjunct" operator or semi-colon: registered(X, Y) :- X = ct101; X = ct102; X = ct103.

How do you compare two variables in Prolog?

The conjunct X=Y first unifies the variables X and Y . Thus when the second conjunct X==Y is evaluated, the two variables are exactly the same Prolog object, and the second conjunct succeeds as well. It should now be clear that = and == are very different, nonetheless there is an important relation between them.


2 Answers

Single quoted items are always atoms.

The meaning of double quotes depends on the Prolog flag double_quotes:


atom — with this value "a" = a. Nowadays, this is rarely used. But you will find Prolog books where ["abc.pl"] is written.


codes — a list of character codes. This is frequently the default, but it leads to very unreadable answers like

?- set_prolog_flag(double_quotes,codes).
   true.
?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz).
   Satz = [74,97,32,116,118,111,105,32,115,108,117,103,97,33]
;  Satz = [74,97,32,116,118,111,105,32,114,97,98,111,116,110,105,107,33].

Even worse, if you use characters beyond ASCII:

?- phrase(("Я твой ",("слуга"|"работник"),"!"), Satz).
   Satz = [1071,32,1090,1074,1086,1081,32,1089,1083,1091,1075,1072,33]
;  Satz = [1071,32,1090,1074,1086,1081,32,1088,1072,1073,1086,1090,1085,1080,1082,33].

chars — a list of one-char atoms. See this for more about it.

?- set_prolog_flag(double_quotes,chars).
   true.
?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz).
   Satz = ['J',a,' ',t,v,o,i,' ',s,l,u,g,a,!]
;  Satz = ['J',a,' ',t,v,o,i,' ',r,a,b,o,t,n,i,k,!].
?- phrase(("Я твой ",("слуга"|"работник"),"!"), Satz).
   Satz = ['Я',' ',т,в,о,й,' ',с,л,у,г,а,!]
;  Satz = ['Я',' ',т,в,о,й,' ',р,а,б,о,т,н,и,к,!].

This notation gives more readable answers and is the default in Scryer, Tau, Trealla, and Ichiban. Scryer and Trealla display them even more compactly with the double quote notation for printing any list of one-char atoms. For SICStus and SWI this can be emulated with the following library.

?- use_module(library(double_quotes)).
   true.
?- phrase(("Ja tvoi ",("sluga"|"rabotnik"),"!"), Satz).
   Satz = "Ja tvoi sluga!"
;  Satz = "Ja tvoi rabotnik!".
?- phrase(("Я твой ",("слуга"|"работник"),"!"), Satz).
   Satz = "Я твой слуга!"
;  Satz = "Я твой работник!".

If you have difficulties installing double_quotes.pl as a library, simply put it into the directory of your other Prolog files and say: use_module(double_quotes).

like image 67
false Avatar answered Oct 20 '22 13:10

false


Strings in Prolog are written in single quotes. Terms written in double quotes are immediately converted to a list of character codes.

?- write('sdf').
sdf
true.

?- write("sdf").
[115, 100, 102]
true.
like image 41
CamilleLDN Avatar answered Oct 20 '22 13:10

CamilleLDN