我在Prolog中在线找到了用于矩阵乘法的代码, 有Prolog经验的人可以向我解释吗?
FROM continuumio/miniconda
RUN apt-get update
RUN apt-get install git gcc g++ -y
答案 0 :(得分:1)
以%
开头的行是注释
大多数编程语言都有方法和功能。 Prolog的predicates成功或失败,可以将值传递给变量。
谓词由其名称和别名transpose/2
表示。
大多数编程语言都使用赋值,但是Prolog使用unification。对于解决数学表达式的特殊情况,Prolog具有is/2
:- use_module(library(clpfd)).
是指令:-,它引入了一个名为clpfd的库。 clpfd通常用于约束,但在这种情况下,用于访问transpose/2
谓词。
dot(V1, V2, N) :-
maplist(product,V1,V2,P),
sumlist(P,N).
dot/3
是一个包含两个向量的谓词,我想在这种情况下实现为Prolog list,并将dot product绑定为N
。
maplist/4将乘积/ 3谓词应用于V1
和V2
中的值以创建列表P
。
sumlist/2将P
中的值列表加起来并绑定N
。
product(N1,N2,N3) :- N3 is N1*N2.
product/3
是一个辅助谓词,取两个数字N1
和N2
并将它们相乘。
N3 is N1*N2
可以被认为是N3 = (N1 * N2)
mmult(M1, M2, M3) :-
transpose(M2,MT),
maplist(mm_helper(MT), M1, M3).
transpose/2是典型的数组转置。
maplist/3
使用mm_helper/3
和MT
来绑定M1
来使用辅助谓词M3
。
mm_helper(M2, I1, M3) :-
maplist(dot(I1), M2, M3).
maplist/3
将dot/3
应用于I1
和M2
来绑定M3
。
我认为您是从RosettaCode那里获取代码的,或者它是与之交叉的。
示例运行:
?- mmult([[1,2],[3,4]],[[5,6],[7,8]],R).
R = [[19, 22], [43, 50]] ;
true.
Verification的示例。
来自评论。
I1在mm_helper谓词中代表什么?
有关详细信息,请参见下面的跟踪
mm_helper/3
的签名为mm_helper(M2, I1, M3)
,因此第二个参数包含I1
。从跟踪中的行
Exit: (11) mm_helper([[5, 7], [6, 8]], [3, 4], [43, 50])
第二个参数是[3,4]
。
Call: (10) mm_helper([[5, 7], [6, 8]], [1, 2], _3596)
第二个参数是[1,2]
。
所以I2
是第一个矩阵的行切片。
请小心您的要求,您可能会得到超出预期的收益? :)
完整的运行轨迹:
仅在这里是因为评论中有一个问题。
[trace] ?- mmult([[1,2],[3,4]],[[5,6],[7,8]],R).
Call: (8) mmult([[1, 2], [3, 4]], [[5, 6], [7, 8]], _3238)
Unify: (8) mmult([[1, 2], [3, 4]], [[5, 6], [7, 8]], _3238)
Call: (9) clpfd:transpose([[5, 6], [7, 8]], _3536)
Unify: (9) clpfd:transpose([[5, 6], [7, 8]], _3536)
Call: (10) error:must_be(list(list), [[5, 6], [7, 8]])
Unify: (10) error:must_be(list(list), [[5, 6], [7, 8]])
Exit: (10) error:must_be(list(list), [[5, 6], [7, 8]])
Call: (10) clpfd:lists_transpose([[5, 6], [7, 8]], _3540)
Unify: (10) clpfd:lists_transpose([[5, 6], [7, 8]], _3540)
Call: (11) clpfd:'__aux_maplist/2_same_length+1'([[7, 8]], [5, 6])
Unify: (11) clpfd:'__aux_maplist/2_same_length+1'([[7, 8]], [5, 6])
Call: (12) lists:same_length([5, 6], [7, 8])
Unify: (12) lists:same_length([5, 6], [7, 8])
Exit: (12) lists:same_length([5, 6], [7, 8])
Call: (12) clpfd:'__aux_maplist/2_same_length+1'([], [5, 6])
Unify: (12) clpfd:'__aux_maplist/2_same_length+1'([], [5, 6])
Exit: (12) clpfd:'__aux_maplist/2_same_length+1'([], [5, 6])
Exit: (11) clpfd:'__aux_maplist/2_same_length+1'([[7, 8]], [5, 6])
^ Call: (11) apply:foldl(transpose_, [5, 6], _3548, [[5, 6], [7, 8]], _3552)
^ Unify: (11) apply:foldl(clpfd:transpose_, [5, 6], _3554, [[5, 6], [7, 8]], _3558)
Call: (12) apply:foldl_([5, 6], _3552, clpfd:transpose_, [[5, 6], [7, 8]], _3558)
Unify: (12) apply:foldl_([5, 6], [_3536|_3538], clpfd:transpose_, [[5, 6], [7, 8]], _3564)
Call: (13) clpfd:transpose_(5, _3536, [[5, 6], [7, 8]], _3562)
Unify: (13) clpfd:transpose_(5, _3536, [[5, 6], [7, 8]], _3562)
Call: (14) clpfd:'__aux_maplist/4_list_first_rest+0'([[5, 6], [7, 8]], _3536, _3560)
Unify: (14) clpfd:'__aux_maplist/4_list_first_rest+0'([[5, 6], [7, 8]], [_3542|_3544], [_3548|_3550])
Call: (15) clpfd:list_first_rest([5, 6], _3542, _3548)
Unify: (15) clpfd:list_first_rest([5, 6], 5, [6])
Exit: (15) clpfd:list_first_rest([5, 6], 5, [6])
Call: (15) clpfd:'__aux_maplist/4_list_first_rest+0'([[7, 8]], _3544, _3550)
Unify: (15) clpfd:'__aux_maplist/4_list_first_rest+0'([[7, 8]], [_3554|_3556], [_3560|_3562])
Call: (16) clpfd:list_first_rest([7, 8], _3554, _3560)
Unify: (16) clpfd:list_first_rest([7, 8], 7, [8])
Exit: (16) clpfd:list_first_rest([7, 8], 7, [8])
Call: (16) clpfd:'__aux_maplist/4_list_first_rest+0'([], _3556, _3562)
Unify: (16) clpfd:'__aux_maplist/4_list_first_rest+0'([], [], [])
Exit: (16) clpfd:'__aux_maplist/4_list_first_rest+0'([], [], [])
Exit: (15) clpfd:'__aux_maplist/4_list_first_rest+0'([[7, 8]], [7], [[8]])
Exit: (14) clpfd:'__aux_maplist/4_list_first_rest+0'([[5, 6], [7, 8]], [5, 7], [[6], [8]])
Exit: (13) clpfd:transpose_(5, [5, 7], [[5, 6], [7, 8]], [[6], [8]])
Call: (13) apply:foldl_([6], _3538, clpfd:transpose_, [[6], [8]], _3588)
Unify: (13) apply:foldl_([6], [_3566|_3568], clpfd:transpose_, [[6], [8]], _3594)
Call: (14) clpfd:transpose_(6, _3566, [[6], [8]], _3592)
Unify: (14) clpfd:transpose_(6, _3566, [[6], [8]], _3592)
Call: (15) clpfd:'__aux_maplist/4_list_first_rest+0'([[6], [8]], _3566, _3590)
Unify: (15) clpfd:'__aux_maplist/4_list_first_rest+0'([[6], [8]], [_3572|_3574], [_3578|_3580])
Call: (16) clpfd:list_first_rest([6], _3572, _3578)
Unify: (16) clpfd:list_first_rest([6], 6, [])
Exit: (16) clpfd:list_first_rest([6], 6, [])
Call: (16) clpfd:'__aux_maplist/4_list_first_rest+0'([[8]], _3574, _3580)
Unify: (16) clpfd:'__aux_maplist/4_list_first_rest+0'([[8]], [_3584|_3586], [_3590|_3592])
Call: (17) clpfd:list_first_rest([8], _3584, _3590)
Unify: (17) clpfd:list_first_rest([8], 8, [])
Exit: (17) clpfd:list_first_rest([8], 8, [])
Call: (17) clpfd:'__aux_maplist/4_list_first_rest+0'([], _3586, _3592)
Unify: (17) clpfd:'__aux_maplist/4_list_first_rest+0'([], [], [])
Exit: (17) clpfd:'__aux_maplist/4_list_first_rest+0'([], [], [])
Exit: (16) clpfd:'__aux_maplist/4_list_first_rest+0'([[8]], [8], [[]])
Exit: (15) clpfd:'__aux_maplist/4_list_first_rest+0'([[6], [8]], [6, 8], [[], []])
Exit: (14) clpfd:transpose_(6, [6, 8], [[6], [8]], [[], []])
Call: (14) apply:foldl_([], _3568, clpfd:transpose_, [[], []], _3618)
Unify: (14) apply:foldl_([], [], clpfd:transpose_, [[], []], [[], []])
Exit: (14) apply:foldl_([], [], clpfd:transpose_, [[], []], [[], []])
Exit: (13) apply:foldl_([6], [[6, 8]], clpfd:transpose_, [[6], [8]], [[], []])
Exit: (12) apply:foldl_([5, 6], [[5, 7], [6, 8]], clpfd:transpose_, [[5, 6], [7, 8]], [[], []])
^ Exit: (11) apply:foldl(clpfd:transpose_, [5, 6], [[5, 7], [6, 8]], [[5, 6], [7, 8]], [[], []])
Exit: (10) clpfd:lists_transpose([[5, 6], [7, 8]], [[5, 7], [6, 8]])
Exit: (9) clpfd:transpose([[5, 6], [7, 8]], [[5, 7], [6, 8]])
Call: (9) '__aux_maplist/3_mm_helper+1'([[1, 2], [3, 4]], _3238, [[5, 7], [6, 8]])
Unify: (9) '__aux_maplist/3_mm_helper+1'([[1, 2], [3, 4]], [_3596|_3598], [[5, 7], [6, 8]])
Call: (10) mm_helper([[5, 7], [6, 8]], [1, 2], _3596)
Unify: (10) mm_helper([[5, 7], [6, 8]], [1, 2], _3596)
Call: (11) '__aux_maplist/3_dot+1'([[5, 7], [6, 8]], _3596, [1, 2])
Unify: (11) '__aux_maplist/3_dot+1'([[5, 7], [6, 8]], [_3602|_3604], [1, 2])
Call: (12) dot([1, 2], [5, 7], _3602)
Unify: (12) dot([1, 2], [5, 7], _3602)
Call: (13) '__aux_maplist/4_product+0'([1, 2], [5, 7], _3626)
Unify: (13) '__aux_maplist/4_product+0'([1, 2], [5, 7], [_3608|_3610])
Call: (14) product(1, 5, _3608)
Unify: (14) product(1, 5, _3608)
Call: (15) _3608 is 1*5
Exit: (15) 5 is 1*5
Exit: (14) product(1, 5, 5)
Call: (14) '__aux_maplist/4_product+0'([2], [7], _3610)
Unify: (14) '__aux_maplist/4_product+0'([2], [7], [_3620|_3622])
Call: (15) product(2, 7, _3620)
Unify: (15) product(2, 7, _3620)
Call: (16) _3620 is 2*7
Exit: (16) 14 is 2*7
Exit: (15) product(2, 7, 14)
Call: (15) '__aux_maplist/4_product+0'([], [], _3622)
Unify: (15) '__aux_maplist/4_product+0'([], [], [])
Exit: (15) '__aux_maplist/4_product+0'([], [], [])
Exit: (14) '__aux_maplist/4_product+0'([2], [7], [14])
Exit: (13) '__aux_maplist/4_product+0'([1, 2], [5, 7], [5, 14])
Call: (13) backward_compatibility:sumlist([5, 14], _3602)
Unify: (13) backward_compatibility:sumlist([5, 14], _3602)
Call: (14) lists:sum_list([5, 14], _3602)
Unify: (14) lists:sum_list([5, 14], _3602)
Exit: (14) lists:sum_list([5, 14], 19)
Exit: (13) backward_compatibility:sumlist([5, 14], 19)
Exit: (12) dot([1, 2], [5, 7], 19)
Call: (12) '__aux_maplist/3_dot+1'([[6, 8]], _3604, [1, 2])
Unify: (12) '__aux_maplist/3_dot+1'([[6, 8]], [_3644|_3646], [1, 2])
Call: (13) dot([1, 2], [6, 8], _3644)
Unify: (13) dot([1, 2], [6, 8], _3644)
Call: (14) '__aux_maplist/4_product+0'([1, 2], [6, 8], _3668)
Unify: (14) '__aux_maplist/4_product+0'([1, 2], [6, 8], [_3650|_3652])
Call: (15) product(1, 6, _3650)
Unify: (15) product(1, 6, _3650)
Call: (16) _3650 is 1*6
Exit: (16) 6 is 1*6
Exit: (15) product(1, 6, 6)
Call: (15) '__aux_maplist/4_product+0'([2], [8], _3652)
Unify: (15) '__aux_maplist/4_product+0'([2], [8], [_3662|_3664])
Call: (16) product(2, 8, _3662)
Unify: (16) product(2, 8, _3662)
Call: (17) _3662 is 2*8
Exit: (17) 16 is 2*8
Exit: (16) product(2, 8, 16)
Call: (16) '__aux_maplist/4_product+0'([], [], _3664)
Unify: (16) '__aux_maplist/4_product+0'([], [], [])
Exit: (16) '__aux_maplist/4_product+0'([], [], [])
Exit: (15) '__aux_maplist/4_product+0'([2], [8], [16])
Exit: (14) '__aux_maplist/4_product+0'([1, 2], [6, 8], [6, 16])
Call: (14) backward_compatibility:sumlist([6, 16], _3644)
Unify: (14) backward_compatibility:sumlist([6, 16], _3644)
Call: (15) lists:sum_list([6, 16], _3644)
Unify: (15) lists:sum_list([6, 16], _3644)
Exit: (15) lists:sum_list([6, 16], 22)
Exit: (14) backward_compatibility:sumlist([6, 16], 22)
Exit: (13) dot([1, 2], [6, 8], 22)
Call: (13) '__aux_maplist/3_dot+1'([], _3646, [1, 2])
Unify: (13) '__aux_maplist/3_dot+1'([], [], [1, 2])
Exit: (13) '__aux_maplist/3_dot+1'([], [], [1, 2])
Exit: (12) '__aux_maplist/3_dot+1'([[6, 8]], [22], [1, 2])
Exit: (11) '__aux_maplist/3_dot+1'([[5, 7], [6, 8]], [19, 22], [1, 2])
Exit: (10) mm_helper([[5, 7], [6, 8]], [1, 2], [19, 22])
Call: (10) '__aux_maplist/3_mm_helper+1'([[3, 4]], _3598, [[5, 7], [6, 8]])
Unify: (10) '__aux_maplist/3_mm_helper+1'([[3, 4]], [_3686|_3688], [[5, 7], [6, 8]])
Call: (11) mm_helper([[5, 7], [6, 8]], [3, 4], _3686)
Unify: (11) mm_helper([[5, 7], [6, 8]], [3, 4], _3686)
Call: (12) '__aux_maplist/3_dot+1'([[5, 7], [6, 8]], _3686, [3, 4])
Unify: (12) '__aux_maplist/3_dot+1'([[5, 7], [6, 8]], [_3692|_3694], [3, 4])
Call: (13) dot([3, 4], [5, 7], _3692)
Unify: (13) dot([3, 4], [5, 7], _3692)
Call: (14) '__aux_maplist/4_product+0'([3, 4], [5, 7], _3716)
Unify: (14) '__aux_maplist/4_product+0'([3, 4], [5, 7], [_3698|_3700])
Call: (15) product(3, 5, _3698)
Unify: (15) product(3, 5, _3698)
Call: (16) _3698 is 3*5
Exit: (16) 15 is 3*5
Exit: (15) product(3, 5, 15)
Call: (15) '__aux_maplist/4_product+0'([4], [7], _3700)
Unify: (15) '__aux_maplist/4_product+0'([4], [7], [_3710|_3712])
Call: (16) product(4, 7, _3710)
Unify: (16) product(4, 7, _3710)
Call: (17) _3710 is 4*7
Exit: (17) 28 is 4*7
Exit: (16) product(4, 7, 28)
Call: (16) '__aux_maplist/4_product+0'([], [], _3712)
Unify: (16) '__aux_maplist/4_product+0'([], [], [])
Exit: (16) '__aux_maplist/4_product+0'([], [], [])
Exit: (15) '__aux_maplist/4_product+0'([4], [7], [28])
Exit: (14) '__aux_maplist/4_product+0'([3, 4], [5, 7], [15, 28])
Call: (14) backward_compatibility:sumlist([15, 28], _3692)
Unify: (14) backward_compatibility:sumlist([15, 28], _3692)
Call: (15) lists:sum_list([15, 28], _3692)
Unify: (15) lists:sum_list([15, 28], _3692)
Exit: (15) lists:sum_list([15, 28], 43)
Exit: (14) backward_compatibility:sumlist([15, 28], 43)
Exit: (13) dot([3, 4], [5, 7], 43)
Call: (13) '__aux_maplist/3_dot+1'([[6, 8]], _3694, [3, 4])
Unify: (13) '__aux_maplist/3_dot+1'([[6, 8]], [_3734|_3736], [3, 4])
Call: (14) dot([3, 4], [6, 8], _3734)
Unify: (14) dot([3, 4], [6, 8], _3734)
Call: (15) '__aux_maplist/4_product+0'([3, 4], [6, 8], _3758)
Unify: (15) '__aux_maplist/4_product+0'([3, 4], [6, 8], [_3740|_3742])
Call: (16) product(3, 6, _3740)
Unify: (16) product(3, 6, _3740)
Call: (17) _3740 is 3*6
Exit: (17) 18 is 3*6
Exit: (16) product(3, 6, 18)
Call: (16) '__aux_maplist/4_product+0'([4], [8], _3742)
Unify: (16) '__aux_maplist/4_product+0'([4], [8], [_3752|_3754])
Call: (17) product(4, 8, _3752)
Unify: (17) product(4, 8, _3752)
Call: (18) _3752 is 4*8
Exit: (18) 32 is 4*8
Exit: (17) product(4, 8, 32)
Call: (17) '__aux_maplist/4_product+0'([], [], _3754)
Unify: (17) '__aux_maplist/4_product+0'([], [], [])
Exit: (17) '__aux_maplist/4_product+0'([], [], [])
Exit: (16) '__aux_maplist/4_product+0'([4], [8], [32])
Exit: (15) '__aux_maplist/4_product+0'([3, 4], [6, 8], [18, 32])
Call: (15) backward_compatibility:sumlist([18, 32], _3734)
Unify: (15) backward_compatibility:sumlist([18, 32], _3734)
Call: (16) lists:sum_list([18, 32], _3734)
Unify: (16) lists:sum_list([18, 32], _3734)
Exit: (16) lists:sum_list([18, 32], 50)
Exit: (15) backward_compatibility:sumlist([18, 32], 50)
Exit: (14) dot([3, 4], [6, 8], 50)
Call: (14) '__aux_maplist/3_dot+1'([], _3736, [3, 4])
Unify: (14) '__aux_maplist/3_dot+1'([], [], [3, 4])
Exit: (14) '__aux_maplist/3_dot+1'([], [], [3, 4])
Exit: (13) '__aux_maplist/3_dot+1'([[6, 8]], [50], [3, 4])
Exit: (12) '__aux_maplist/3_dot+1'([[5, 7], [6, 8]], [43, 50], [3, 4])
Exit: (11) mm_helper([[5, 7], [6, 8]], [3, 4], [43, 50])
Call: (11) '__aux_maplist/3_mm_helper+1'([], _3688, [[5, 7], [6, 8]])
Unify: (11) '__aux_maplist/3_mm_helper+1'([], [], [[5, 7], [6, 8]])
Exit: (11) '__aux_maplist/3_mm_helper+1'([]
答案 1 :(得分:0)
这看起来太可怕了!但这是有道理的。
很明显%是评论的开头(以防万一,不清楚)
:-正在定义一个函数
一个函数是一系列其他函数,用逗号分隔。
参数通过引用传递(因此没有返回值),在最后一个参数上方的代码中,每个函数的结果都存储在该参数中。我想这是序言中的某种约定,因为库函数似乎也是如此。maplist听起来像是将一个函数应用于传递的列表中的每个元素(即列表中的map),将值存储在结果列表中(最后一个参数)。您可以用谷歌此功能进行确认。但这对于定义点积函数的方式很有用。
转置大概只是普通的矩阵转置(从库中导入-根据顶部注释)。
涵盖了我所看到的所有语法。因此最终它定义了mmult()函数,该函数需要3个输入,M1和M2是数字(即矩阵)列表的列表,而M3是输出。