Matrix prolog中的矩阵乘法

Matrix prolog中的矩阵乘法,matrix,prolog,clpfd,Matrix,Prolog,Clpfd,我发现了一个矩阵乘法的代码 % SWI-Prolog has transpose/2 in its clpfd library :- use_module(library(clpfd)). % N is the dot product of lists V1 and V2. dot(V1, V2, N) :- maplist(product,V1,V2,P), sumlist(P,N). product(N1,N2,N3) :- N3 is N1*N2. % Matrix multiplic

我发现了一个矩阵乘法的代码

% SWI-Prolog has transpose/2 in its clpfd library
:- use_module(library(clpfd)).

% N is the dot product of lists V1 and V2.
dot(V1, V2, N) :- maplist(product,V1,V2,P), sumlist(P,N).
product(N1,N2,N3) :- N3 is N1*N2.

% Matrix multiplication with matrices represented
% as lists of lists. M3 is the product of M1 and M2
mmult(M1, M2, M3) :- transpose(M2,MT), maplist(mm_helper(MT), M1, M3).
mm_helper(M2, I1, M3) :- maplist(dot(I1), M2, M3).
如果我输入:
mult([[1,2],[3,4],[[5,6],[7,8]],X)。
那么我得到
X=[[19,22],[43,50].

但是我怎样才能得到一个
X=[[1*5+2*7,1*6+2*8],[3*5+4*7,3*6+4*8].

另外,我对序言还不熟悉。
谢谢

这很容易:与其用is/2计算算术表达式,不如不计算它们,而使用复合项代替它们的数值。我这样做是为了产品/3:而不是

product(N1,N2,N3) :- N3 is N1*N2.
我写道:

product(N1, N2, N1*N2).

您只需要编写相应版本的sumlist/2。

我应该如何更改sumlist/2?与我向您展示的更改类似:使用sumlist/2,而不是使用is/2计算算术表达式,将总和本身表示为一个项(这次使用函子+而不是*)。只需编写您自己版本的
sumlist
,但是使用
=
而不是
is