在 SWI Prolog 中,list(Xs) :- length(Xs, _).
它是“纯粹的”,因为我可以将具有任何类型实例化的变量传递给它,并且它将不确定地将它与特定长度的所有最通用的统一器统一起来。
list/1
是否可以在 Mercury中编写纯文本?该手册似乎暗示可以做到这一点,但我在实际实施时遇到了麻烦。
到目前为止,我所拥有的是:
:- module mylist.
:- interface.
:- import_module list.
:- inst frees for list/1
---> []
; [free | frees].
:- mode free_to_frees == free >> frees.
:- pred mylist(list(_)).
:- mode mylist(in) is det.
:- mode mylist(free_to_frees) is multi.
:- implementation.
:- pragma promise_pure(mylist/1).
mylist(_::in).
mylist([]::free_to_frees).
mylist([_|Xs]::free_to_frees) :- mylist(Xs).
但是,当我尝试这个时:
:- module main.
:- interface.
:- implementation.
:- import_module list, mylist.
:- pred getlist(list(int)).
:- mode getlist(free >> ground) is multi.
getlist(Xs) :- Xs = [1, 2, 3].
getlist(Xs) :- mylist(Xs), Xs = [5].
我收到以下错误:
main.m:011: In clause for `getlist(out)':
main.m:011: mode error in conjunction. The next 2 error messages indicate
main.m:011: possible causes of this error.
main.m:011:
main.m:011: In clause for `getlist(out)':
main.m:011: mode error in unification of `Xs' and `list.[V_10 | V_16]'.
main.m:011: Variable `Xs' has instantiatedness
main.m:011: bound(
main.m:011: []
main.m:011: ;
main.m:011: '[|]'(
main.m:011: free,
main.m:011: named inst mylist.listskel,
main.m:011: which expands to
main.m:011: bound(
main.m:011: []
main.m:011: ;
main.m:011: '[|]'(
main.m:011: free,
main.m:011: named inst mylist.listskel
main.m:011: )
main.m:011: )
main.m:011: )
main.m:011: ),
main.m:011: term `list.[V_10 | V_16]' has instantiatedness
main.m:011: `named inst list.'[|]'(unique(5), free)'.
main.m:011:
main.m:011: In clause for `getlist(out)':
main.m:011: in argument 1 of clause head:
main.m:011: mode error in unification of `HeadVar__1' and `Xs'.
main.m:011: Variable `HeadVar__1' has instantiatedness `free',
main.m:011: variable `Xs' has instantiatedness
main.m:011: bound(
main.m:011: []
main.m:011: ;
main.m:011: '[|]'(
main.m:011: free,
main.m:011: named inst mylist.listskel,
main.m:011: which expands to
main.m:011: bound(
main.m:011: []
main.m:011: ;
main.m:011: '[|]'(
main.m:011: free,
main.m:011: named inst mylist.listskel
main.m:011: )
main.m:011: )
main.m:011: )
main.m:011: ).
我猜我的使用free
可能不正确,否则我需要添加一个额外的模式或概括我的listskel
inst 以涵盖mylist(Xs), Xs = [5]
.
基本上,我应该怎么写mylist/1
,以便它可以在尽可能多的模式下使用?
谢谢!