I’m trying to build a module to support matrix arithmetic using the []
block operator (SWI-Prolog -- Manual). So to start:
module(matrices,
[
op(100, yf, []),
new_array/2
]).
new_array(Array, Contents) :-
Array =.. ['$arr'|Contents].
arithmetic:evaluable(_[_],user).
[]([Ix], Array, X) :-
functor(Array,'$arr',_),
arg(Ix,Array,X).
The first issue I ran into was the operator didn’t appear to be exported:
ERROR: /Users/rworkman/Documents/PrologDev/swiCLPBNR/matrices_proto.pl:10:22: Syntax error: Operator expected
?- current_op(P,A,[]).
false.
?- op(100, yf, []).
true.
?- current_op(P,A,[]).
P = 100,
A = yf.
% /Users/rworkman/Documents/PrologDev/swiCLPBNR/matrices_proto compiled 0.00 sec, 1 clauses
Thread 1 (main): foreign predicate system:ground/1 did not clear exception:
error(type_error(atom,[]),context(system:sub_atom/5,_911962))
?- A[I]=..L.
L = [[], [I], A].
By using the top level to define the operator, the module now loads and A[I]
is parsed as documented, although an (inconsequential?) error message is output.
Now when I create a new matrix and try to evaluate and matrix expression:
?- new_array(M,[1,2,3]), X is M[2].
ERROR: Type error: `callable' expected, found `new_array(_920036,[1,2,3]),[]([2],_920036,_920064)' (a compound)
ERROR: In:
ERROR: [9] toplevel_call(user:user: ...)
?- new_array(M,[1,2,3]),call([]([2],M,X)).
M = '$arr'(1, 2, 3),
X = 2.
It appears that some check isn’t recognizing []
as a callable functor, yet using call
explicitly works fine.
Bugs? (or operator error).