您可以使用您的语言构建图灵机,从而将当前磁带和内部状态表示为“累加器项”。只是你不能使用“!” 承诺选择一个基本确定性“证明”的子句,因此除了不断增长的术语之外,真正的实现将充满不断增长的堆栈(将永远不会再次访问)。但是在图灵宇宙中,空间是自由的,时间是无限的,热力学上可用的能量是丰富的(而且周围有一个很大的散热器)。不用担心!
实际上,在纯 Prolog 中构建 Marvin Minsky 的最小通用图灵机是一个很好的练习。
编辑:这个“相交”的实现怎么样。少了什么东西?
% horn_intersect(++List1,++List2,?List3)
% List3 is the intersection of List1 and List2
% Assumptions:
% 1) All the members of List1, List2, List3 are grounded
% No unbound variables (a non-logical concept)
% 2) We don't care about determinism. The same solution
% may be generated several times (as long as it's right)
% Choicepoints are not removed.
% 3) There is a dataflow direction: (List1,List2) --> List3.
% This also ensures that this is a function (only one solution,
% though represented by a set of equivalent solutions)
% These are not foreseen:
% Going the other ways (List1,List3) --> "an infinite set of List2 templates"
% Going the other ways (List2,Listd) --> "an infinite set of List1 templates"
% Going the other ways List3 --> "an infinite set of (List1,List2) templates tuples"
% However, accepting a (List1,List2,List3) is ok.
horn_intersect([],_,[]).
horn_intersect(_,[],[]).
horn_intersect([X|Xs],List2,[X|Ms]) :- in(X,List2),horn_intersect(Xs,List2,Ms).
horn_intersect([X|Xs],List2,Ms) :- not_in(X,List2),horn_intersect(Xs,List2,Ms).
in(X,[X|_]).
in(X,[K|Ms]) :- X\=K,in(X,Ms).
not_in(_,[]).
not_in(X,[K|Ms]) :- X\=K,not_in(X,Ms).
:- begin_tests(horn_horn_intersect).
test(1,[true,nondet]) :- horn_intersect([a,b,c],[a,b,c],[a,b,c]).
test(2,[true,nondet]) :- horn_intersect([b],[a,b,c],[b]).
test(3,[true,nondet]) :- horn_intersect([a,b,c],[b],[b]).
test(4,[true,nondet]) :- horn_intersect([a,b,c],[],[]).
test(5,[true,nondet]) :- horn_intersect([],[a,b,c],[]).
test(6,[true,nondet]) :- horn_intersect([x,y],[a,b],[]).
test(7,fail) :- horn_intersect([x,y],[a,b],[u,v]).
test(8,[Out == [], nondet]) :- horn_intersect([x,y],[a,b],Out).
test(9,[Out == [a,b], nondet]) :- horn_intersect([a,b,c],[a,b],Out).
test(10,[Out == [a,b], nondet]) :- horn_intersect([a,b],[a,b,c],Out).
test(11,[Out == [], nondet]) :- horn_intersect([x,y],[a,b],Out).
:- end_tests(horn_horn_intersect).