1

以下示例代码使用 SWI-Prolog

% example from https://github.com/Anniepoo/swiplclpfd/blob/master/clpfd.adoc
:- use_module(library(clpfd)).

trains([[1,2,0,1], % from station, to station, departs at, arrives at
        [2,3,4,5],
        [2,3,0,1],
        [3,4,5,6],
        [3,4,2,3],
        [3,4,8,9]]).

threepath(A, D, Ps) :-
        Ps = [[A,B,_T0,T1],[B,C,T2,T3],[C,D,T4,_T5]],
        T2 #> T1,
        T4 #> T3,
        trains(Ts),
        tuples_in(Ps, Ts).

Gnu Prolog 有一个内置的 clp(fd) 求解器。语法基本相同,但我们不需要包含库,并且 tuples_in/2 谓词由 GNU Prolog 谓词 fd_relation/2 处理。

trains([[1,2,0,1], % from station, to station, departs at, arrives at
        [2,3,4,5],
        [2,3,0,1],
        [3,4,5,6],
        [3,4,2,3],
        [3,4,8,9]]).

threepath(A, D, Ps) :-
        Ps = [[A,B,_T0,T1],[B,C,T2,T3],[C,D,T4,_T5]],
        T2 #> T1,
        T4 #> T3,
        trains(Ts),
        fd_relation(Ts, Ps).

这不会像写的那样工作,因为 fd_relation/2 需要一个变量列表。我得到一个 type_error(fd_variable),大概是因为 Ps 是一个 var 列表。

显示的示例应该产生这个

?- threepath(1, 4, Ps).
Ps = [[1, 2, 0, 1], [2, 3, 4, 5], [3, 4, 8, 9]].

在 GNU Prolog 中如何实现呢?

4

1 回答 1

1

答案是使用 maplist/2

% example from https://github.com/Anniepoo/swiplclpfd/blob/master/clpfd.adoc
trains([[1,2,0,1], % from station, to station, departs at, arrives at
        [2,3,4,5],
        [2,3,0,1],
        [3,4,5,6],
        [3,4,2,3],
        [3,4,8,9]]).

threepath(A, D, Ps) :-
        Ps = [[A,B,_T0,T1],[B,C,T2,T3],[C,D,T4,_T5]],
        T2 #> T1,
        T4 #> T3,
        trains(Ts),
        maplist(fd_relation(Ts), Ps).

这将产生预期的解决方案。

?- threepath(1, 4, Ps).

Ps = [[1,2,0,1],[2,3,4,5],[3,4,8,9]]
于 2021-08-19T13:05:46.290 回答