如何使用SWI-Prolog ./2 函数?
How to use SWI-Prolog ./2 function?
需要使用 SWI-Prolog 的示例 ./2。
签名是
.(+Int,[])
此外,如果知道此运算符的名称,那将是一件好事。正在搜索“.”毫无意义。
最接近名称的是 SWI 文档 F.3 部分 Arithmetic Functions
List of one character: character code
我试过的
?- X is .(97,[]).
结果
ERROR: Type error: `dict' expected, found `97' (an integer)
ERROR: In:
ERROR: [11] throw(error(type_error(dict,97),_5812))
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
也试过
?- X is .(97,['a']).
结果相同。
在标准 Prolog 中,'.'
函子是实际的列表项函子。具体来说,[H|T]
等同于 '.'(H, T)
。例如,如果你 运行 GNU Prolog,你会得到这个:
| ?- write_canonical([H|T]).
'.'(_23,_24)
yes
| ?- X = .(H,T).
X = [H|T]
yes
| ?-
is/2
运算符将允许您直接从由单个字符代码组成的列表中读取字符代码。所以以下在 Prolog 中有效:
| ?- X is [97].
X = 97
yes
| ?- X is .(97,[]).
X = 97
yes
开始变得有趣的是,在 SWI Prolog 中,他们引入了 dictionaries,它依赖于 '.'
作为指定字典键的方法。这与用作列表仿函数的 '.'
仿函数产生了冲突。 link on dictionaries 对此进行了解释。因此,当您尝试对列表使用 '.'
仿函数时,您会得到如下内容:
1 ?- X is [97].
X = 97.
2 ?- X is .(97, []).
ERROR: Type error: `dict' expected, found `97' (an integer)
ERROR: In:
ERROR: [11] throw(error(type_error(dict,97),_5184))
ERROR: [9] '$dicts':'.'(97,[],_5224) at /usr/local/lib/swipl-7.4.2/boot/dicts.pl:46
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
3 ?- X = .(H,T).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [11] throw(error(instantiation_error,_6914))
ERROR: [9] '$dicts':'.'(_6944,_6946,_6948) at /usr/local/lib/swipl-7.4.2/boot/dicts.pl:46
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
因此,SWI 定义了另一个函子 [|]
,以实现 '.'
传统上服务的目的,即列表函子。
4 ?- X is '[|]'(97, []).
X = 97.
5 ?- X = '[|]'(H, T).
X = [H|T].
6 ?-
奇怪的是,您当时可能会认为会发生这种情况:
7 ?- write_canonical([H|T]).
'[|]'(_, _)
然而,结果是这样的:
7 ?- write_canonical([H|T]).
[_|_]
true.
所以对于 SWI Prolog,列表表示 [H|T]
已经是列表的规范表示。
算术函数'.'/2
映射
包含单个 字符 或 字符代码 的列表([C]
或 '.'(C,[])
)对应字符code.
您观察到的特殊异常行为是 SWI-Prolog 特有的。
您可以通过添加命令行参数 --traditional
来禁用非标准 SWI-Prolog "extensions"(如字典),如下所示:
$ swipl --traditional
Welcome to SWI-Prolog (threaded, 64 bits, version 8.0.0) [...]
?- C = 97, V is [C].
C = 97, V = 97.
?- C = 0'a, V is [C].
C = 97, V = 97.
?- C = a, V is [C].
C = a, V = 97.
这也适用于字符串文字:
:- set_prolog_flag(double_quotes, codes).
?- Str = "a", Str = [C], V is Str.
Str = [97], C = 97, V = 97.
:- set_prolog_flag(double_quotes, chars).
?- Str = "a", Str = [C], V is Str.
Str = [a], C = a, V = 97.
需要使用 SWI-Prolog 的示例 ./2。
签名是
.(+Int,[])
此外,如果知道此运算符的名称,那将是一件好事。正在搜索“.”毫无意义。
最接近名称的是 SWI 文档 F.3 部分 Arithmetic Functions
List of one character: character code
我试过的
?- X is .(97,[]).
结果
ERROR: Type error: `dict' expected, found `97' (an integer)
ERROR: In:
ERROR: [11] throw(error(type_error(dict,97),_5812))
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
也试过
?- X is .(97,['a']).
结果相同。
在标准 Prolog 中,'.'
函子是实际的列表项函子。具体来说,[H|T]
等同于 '.'(H, T)
。例如,如果你 运行 GNU Prolog,你会得到这个:
| ?- write_canonical([H|T]).
'.'(_23,_24)
yes
| ?- X = .(H,T).
X = [H|T]
yes
| ?-
is/2
运算符将允许您直接从由单个字符代码组成的列表中读取字符代码。所以以下在 Prolog 中有效:
| ?- X is [97].
X = 97
yes
| ?- X is .(97,[]).
X = 97
yes
开始变得有趣的是,在 SWI Prolog 中,他们引入了 dictionaries,它依赖于 '.'
作为指定字典键的方法。这与用作列表仿函数的 '.'
仿函数产生了冲突。 link on dictionaries 对此进行了解释。因此,当您尝试对列表使用 '.'
仿函数时,您会得到如下内容:
1 ?- X is [97].
X = 97.
2 ?- X is .(97, []).
ERROR: Type error: `dict' expected, found `97' (an integer)
ERROR: In:
ERROR: [11] throw(error(type_error(dict,97),_5184))
ERROR: [9] '$dicts':'.'(97,[],_5224) at /usr/local/lib/swipl-7.4.2/boot/dicts.pl:46
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
3 ?- X = .(H,T).
ERROR: Arguments are not sufficiently instantiated
ERROR: In:
ERROR: [11] throw(error(instantiation_error,_6914))
ERROR: [9] '$dicts':'.'(_6944,_6946,_6948) at /usr/local/lib/swipl-7.4.2/boot/dicts.pl:46
ERROR: [8] '<meta-call>'(user:(...,...)) <foreign>
ERROR: [7] <user>
ERROR:
ERROR: Note: some frames are missing due to last-call optimization.
ERROR: Re-run your program in debug mode (:- debug.) to get more detail.
因此,SWI 定义了另一个函子 [|]
,以实现 '.'
传统上服务的目的,即列表函子。
4 ?- X is '[|]'(97, []).
X = 97.
5 ?- X = '[|]'(H, T).
X = [H|T].
6 ?-
奇怪的是,您当时可能会认为会发生这种情况:
7 ?- write_canonical([H|T]).
'[|]'(_, _)
然而,结果是这样的:
7 ?- write_canonical([H|T]).
[_|_]
true.
所以对于 SWI Prolog,列表表示 [H|T]
已经是列表的规范表示。
算术函数'.'/2
映射
包含单个 字符 或 字符代码 的列表([C]
或 '.'(C,[])
)对应字符code.
您观察到的特殊异常行为是 SWI-Prolog 特有的。
您可以通过添加命令行参数 --traditional
来禁用非标准 SWI-Prolog "extensions"(如字典),如下所示:
$ swipl --traditional Welcome to SWI-Prolog (threaded, 64 bits, version 8.0.0) [...] ?- C = 97, V is [C]. C = 97, V = 97. ?- C = 0'a, V is [C]. C = 97, V = 97. ?- C = a, V is [C]. C = a, V = 97.
这也适用于字符串文字:
:- set_prolog_flag(double_quotes, codes). ?- Str = "a", Str = [C], V is Str. Str = [97], C = 97, V = 97. :- set_prolog_flag(double_quotes, chars). ?- Str = "a", Str = [C], V is Str. Str = [a], C = a, V = 97.