基于 Term 的对象化数组

Objectifying Array based on Term

我有这个数组:

ary1d_new(Size,Sym,ArySym,Ary) :- 
    functor(Ary,ArySym,Size), 
    forall(arg(X,Ary,_), nb_setarg(X,Ary,Sym)).
ary1d_get(Pos,Ary,Val) :- arg(Pos,Ary,Val).
ary1d_set(Pos,Ary,Val) :- nb_setarg(Pos,Ary,Val).

试图将其转换为 logtalk 对象,但我没有成功

:- object(array, instantiates(array)).

    :- initialization(init).
    :- private(init/0).

    :- public(ary/1).
    :- dynamic(ary/1).
    ary(_).

    :- public([new1d/2, set1d/2, get1d/2]).

    new1d(Size,Sym) :- 
        functor(Ary,ary,Size), 
        forall(arg(X,Ary,_), nb_setarg(X,Ary,Sym)),writeln(Ary).
    get1d(Pos,Val) :- arg(Pos,::ary,Val).
    set1d(Pos,Val) :- nb_setarg(Pos,::ary,Val).

    init :- true.

:- end_object.

:- object(g, instantiates(array)).
:- end_object.


?- g::new1d(5,0).
ary(0,0,0,0,0)
true.

?- g::set1d(2,7).
false.

?- g::get1d(2,D).
false.

正确的方法是什么?函子 vs ::ary 让我很困惑


为数组存储中的 Obj 属性想出了这个:

:- public(test/2).
test(Size, Value) :- test(Size,Value,i(Size)).
:- public(test/3).
test(Size, Value, Info) :-
    functor(A, s, Size),
    forall(
        arg(Arg, A, _),
        nb_setarg(Arg, A, Value)
    ),
    _Array_ = a(Info,A).

?- ary(A)::test(5,0).
A = a(i(5), s(0, 0, 0, 0, 0)).

请注意,您只能对复合术语参数调用 nb_setarg/3。一种可能的替代方法是使用 parametric object:

:- object(array(_Array_)).

    :- public(new/2).
    new(Size, Value) :-
        functor(_Array_, a, Size),
        forall(
            arg(Arg, _Array_, _),
            nb_setarg(Arg, _Array_, Value)
        ).

    :- public(get/2).
    get(Arg, Value) :-
        arg(Arg, _Array_, Value).

    :- public(set/2).
    set(Arg, Value) :-
        nb_setarg(Arg, _Array_, Value).

:- end_object.

示例查询为:

?- {arrays}.
% [ .../arrays.lgt loaded ]
% (0 warnings)
true.

?- array(Array)::(new(5,0), set(2, 7), get(2, Value)).
Array = a(0, 7, 0, 0, 0),
Value = 7.

注意_Array_ 参数变量 一个逻辑变量。因此,它无法从 top-level 查询到 top-level 查询:

?- array(Array)::(new(5,0), set(2, 7), get(2, Value)).
Array = a(0, 7, 0, 0, 0),
Value = 7.

?- array(Array)::new(5,0).
Array = a(0, 0, 0, 0, 0).

?- array(Array)::set(2, 7).
ERROR: Arguments are not sufficiently instantiated