Viv*_*ath 5 prolog unification unify iso-prolog
假设我有以下规则:
unify('test', 'this is a test').
run :- write('Enter something: '),
read(X),
unify(X, Y),
write('The answer is '), write(Y).
Run Code Online (Sandbox Code Playgroud)
然后我运行如下:
?- ['unify.pl'].
% unify.pl compiled 0.00 sec, -48 bytes
true.
?- run.
Enter something: test.
The answer is this is a test
true.
?- run.
Enter something: 'test'.
The answer is this is a test
true.
Run Code Online (Sandbox Code Playgroud)
为什么SWI-Prolog的统一都test和'test'来unify('test', 'this is a test').?我在回答关于SO的Prolog问题时遇到了这个问题.虽然我能够回答这个人的问题,但我无法解释这个特殊的行为,我想知道是否有其他人可以.
虽然SWI-PROLOG中的原子可以用单引号表示,例如,当SWI-PROLOG解析器可以从字符序列中识别原子时,不需要'This is an atom'单引号,通常以小写字母字符开头,例如.如果序列包含空格(或其他一些字符),则需要单引号来正确表示原子.字母数字字符和某些标点符号如下划线很好,例如.test_test5_6
如果没有单引号的字符序列以其他任何内容开头,例如数字6k,则解析器会将其视为number; 如果它是一个大写的字母字符,例如Test,解析器会将其视为变量.