SWI-Prolog (SWISH):无权修改静态过程`(=)/2' [英] SWI-Prolog (SWISH): No permission to modify static procedure `(=)/2'
问题描述
在 SWI-Prolog (SWISH) 中使用不同的列表谓词时,我试图检查原子 a
是否是列表 List1
的一部分,我在程序中定义为 List1 = [a,b,c,d]
.
我将我的查询公式化为 member(a, List1).
,期待一些简单的是"(正如它在这个 youtube 视频 at 59:25),但我收到了警告
单变量:[List1]
和一个错误
<块引用>无权限修改静态过程`(=)/2'
根据我在网上查找的了解,警告在这里并不那么重要.但是,我不明白为什么在 a
显然是 List1
的成员时会收到错误消息.
我以两种不同的方式尝试了这个:
1) 通过在程序中添加 List1 = [a,b,c,d].
并使用 member(a,List1).
进行查询(结果是上面的错误);
2) 通过将 List1 = [a,b,c,d]
直接传递给解释器,然后使用相同的查询 ( member(a,List1).
),这导致了无数的结果,其中 a
移动了列表头部的位置,如下所示:
List1 = [a|_1186]List1 = [_1062, a|_1070]列表 1 = [_1062, _1068, a|_1076]列表 1 = [_1062, _1068, _1074, a|_1082]列表 1 = [_1062, _1068, _1074, _1080, a|_1088]
这是关于我正在使用的特定 Prolog 版本,还是我遗漏了一些非常简单的东西?
编辑
我知道在这里提出了类似的问题,但我没有设法完全理解答案(也不是问题),因为它立即处理了我在 Prolog 中还没有遇到过的 dynamic
事情.我一直在寻找一个更一般、更高级"的答案,我通过提出这个问题找到了这个答案.
我在程序中定义为
List1 = [a,b,c,d].
这不是它的作用.它所做的是定义一个谓词 =/2
:
2 ?- write_canonical( (List1 = [a,b,c,d]) ).=(_,[a,b,c,d])
(您在那里看到的 ?-
或 2 ?-
,是Prolog 系统的交互式提示;在我的情况下为 SWI Prolog.无论在那一行之后发生什么,都是我输入的内容;然后在下一行我们看到系统的响应).
当然,这践踏了 =
作为统一谓词的现有内置定义.因此,错误恰恰说明了这一点.是的,它很重要.
要在 Prolog 中定义"一个列表,我们可以定义一个谓词
8 ?- [用户].p([1,2,3,4]).
这样我们就可以查询
9 ?- p(List1).列表 1 = [1, 2, 3, 4].
并进一步使用 List1
,
10 ?- p(List1), member(A,List1).列表 1 = [1, 2, 3, 4],一 = 1 ;列表 1 = [1, 2, 3, 4],A = 2 ;列表 1 = [1, 2, 3, 4],一 = 3 ;列表 1 = [1, 2, 3, 4],A = 4.
我们也可以直接将列表指定为查询的子目标,
11 ?- List1 = [1,2,3,4], member(A,List1).列表 1 = [1, 2, 3, 4],一 = 1 ;列表 1 = [1, 2, 3, 4],A = 2 ;列表 1 = [1, 2, 3, 4],一 = 3 ;列表 1 = [1, 2, 3, 4],A = 4.
使使用谓词=/2
,而不是重新定义,这是禁止的.
以上回答了您的1)
.至于2)
,你并没有告诉我们全部真相.你似乎做了什么,是先进行查询
12 ?- List1 = [a,b,c,d].列表 1 = [a, b, c, d].
这很好,很花哨;然后然后进行另一个查询,
13 ?- member(a,List1).列表 1 = [a|_G2181] ;List1 = [_G2180, a|_G2184] ;List1 = [_G2180, _G2183, a|_G2187];List1 = [_G2180, _G2183, _G2186, a|_G2190] ;List1 = [_G2180, _G2183, _G2186, _G2189, a|_G2193] .
Prolog 提示不是 REPL.我们不对它下定义.我们进行查询.
During some playing around with different list predicates in SWI-Prolog (SWISH), I was trying to check if an atom a
was part of the list List1
which I defined in the program as List1 = [a,b,c,d]
.
I formulated my query as member(a, List1).
, expecting something along the lines of a simple 'yes' (just as it shows in this youtube video at 59:25), but instead I got a warning
Singleton variables: [List1]
and an error
No permission to modify static procedure `(=)/2'
From what I understand from looking this up online, the warning is not that important here. I do not understand, however, why I get an error message while a
is clearly a member of List1
.
I tried this in two different ways:
1) By adding List1 = [a,b,c,d].
to the program and querying with member(a,List1).
(which resulted in the error above);
2) By passing List1 = [a,b,c,d]
directly to the interpreter and then using the same query ( member(a,List1).
), which resulted in an endless amount of results where a
shifted positions in the Head of the list, like so:
List1 = [a|_1186]
List1 = [_1062, a|_1070]
List1 = [_1062, _1068, a|_1076]
List1 = [_1062, _1068, _1074, a|_1082]
List1 = [_1062, _1068, _1074, _1080, a|_1088]
Is this something about the specific Prolog version I am using, or am I missing something very simple?
EDIT
I was aware that a similar question was posed here , but I did not manage to fully understand the answer (nor the question) as it was immediately going about things as dynamic
which I have not yet encountered in Prolog. I was looking for a more general, more 'high-level' answer which I have found by posing this question.
I defined in the program as
List1 = [a,b,c,d].
This is not what it does. What it does is define a predicate =/2
:
2 ?- write_canonical( (List1 = [a,b,c,d]) ). =(_,[a,b,c,d])
(The ?-
, or 2 ?-
that you see there, is the interactive prompt of a Prolog system; SWI Prolog in my case. Whatever goes on that line after it is what I have typed; and then on the next line we see the system's response).
Of course this tramples over the already existing built-in definition for =
as the unification predicate. And hence the error which says precisely that. And yes, it is important.
To "define" a list in Prolog, we can define a predicate
8 ?- [user].
p([1,2,3,4]).
such that we can then query
9 ?- p(List1).
List1 = [1, 2, 3, 4].
and work further with List1
,
10 ?- p(List1), member(A,List1).
List1 = [1, 2, 3, 4],
A = 1 ;
List1 = [1, 2, 3, 4],
A = 2 ;
List1 = [1, 2, 3, 4],
A = 3 ;
List1 = [1, 2, 3, 4],
A = 4.
We could also just directly specify the list as a sub-goal of our query,
11 ?- List1 = [1,2,3,4], member(A,List1).
List1 = [1, 2, 3, 4],
A = 1 ;
List1 = [1, 2, 3, 4],
A = 2 ;
List1 = [1, 2, 3, 4],
A = 3 ;
List1 = [1, 2, 3, 4],
A = 4.
making use of the predicate =/2
, as opposed to redefining it, which is forbidden.
The above answers your 1)
. As for 2)
, you aren't telling us the whole truth. What you appear to have done, was to first make a query
12 ?- List1 = [a,b,c,d].
List1 = [a, b, c, d].
which is fine and dandy; and then make another query,
13 ?- member(a,List1).
List1 = [a|_G2181] ;
List1 = [_G2180, a|_G2184] ;
List1 = [_G2180, _G2183, a|_G2187] ;
List1 = [_G2180, _G2183, _G2186, a|_G2190] ;
List1 = [_G2180, _G2183, _G2186, _G2189, a|_G2193] .
Prolog prompt is not a REPL. We don't make definitions at it. We make queries.
这篇关于SWI-Prolog (SWISH):无权修改静态过程`(=)/2'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!