PROLOG 中的布尔保护
Boolean guard in PROLOG
我有这个 prolog 代码,它用乳胶代码生成一个 txt 文件
worlds(_,_,_,_,[]) :- !.
worlds(Stream,Count,Right,Number,[Head|Tail]) :-
C is Count+1,
Number2 is Number+1,
single_world(Stream,Right,Number2,C,Head),
Right2 is Right+15,
worlds(Stream,C,Right2,Number2,Tail).
single_world(_,_,_,_,[]) :- !.
single_world(Stream,Right,Number,C,[Head|Tail]) :-
Right2 is Right+15,
atomic_list_concat(Head,',',Label),
world_sphere(Stream,5,Right2,Label,Number,C,Head),
write(Stream,"\draw [->] ("),
write(Stream, C),
write(Stream, ") to ("),
write(Stream, Number),
writeln(Stream, ");"),
single_world(Stream,Right2,Number,C,Tail).
world_sphere(_,_,_,_,_,_,[]) :- !.
world_sphere(Stream,Width,Right,Label,Number,C,[_|[]]):-
write(Stream, "\node[circle, minimum size="),
write(Stream, Width),
write(Stream,"mm, draw, label = {[label distance=0cm]:$"),
write(Stream, Label),
write(Stream, "$}]"),
write(Stream, "[below of = "),
Below is C-1,
write(Stream, Below),
write(Stream,"][right = "),
write(Stream,Right),
write(Stream,"mm] ("),
write(Stream, Number),
writeln(Stream,"){};").
world_sphere(Stream,Width,Right,Label,Number,C,[_|Tail]) :-
write(Stream, "\node[circle, minimum size="),
write(Stream, Width),
write(Stream,"mm, draw]"),
write(Stream,"[below of = "),
Below is C-1,
write(Stream, Below),
write(Stream,"]"),
write(Stream,"[right = "),
write(Stream,Right),
write(Stream,"mm] ("),
write(Stream, Number),
writeln(Stream,"){};"),
Width2 is Width+5,
world_sphere(Stream,Width2,Right,Label,Number,C,Tail).
list_length(Xs,L) :- list_length(Xs,0,L) .
list_length( [] , L , L ) .
list_length( [_|Xs] , T , L ) :-
T1 is T+1 ,
list_length(Xs,T1,L).
draw_spheres(FileName):-
open(FileName, write, Stream),
list_length([1,2], Worlds),
worlds(Stream,0,4,Worlds,[[[1,2,3,4]],[[1],[1,3],[1,2,3,4]],[[1,2,3,4]], [[1,2,3,4]]]),!,
close(Stream).
代码生成如下txt文件,需要用latex工具编译:
\node[circle, minimum size=5mm, draw][below of = 0][right = 19mm] (3){};
\node[circle, minimum size=10mm, draw][below of = 0][right = 19mm] (3){};
\node[circle, minimum size=15mm, draw][below of = 0][right = 19mm] (3){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}] [below of = 0][right = 19mm] (3){};
\draw [->] (1) to (3);
node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$}][below of = 1][right = 34mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 49mm] (4){};
\node[circle, minimum size=10mm, draw, label = {[label distance=0cm]:,3$}][below of = 1][right = 49mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=10mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=15mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 1][right = 64mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=10mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=15mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 2][right = 49mm] (5){};
\draw [->] (3) to (5);
\node[circle, minimum size=5mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=10mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=15mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 3][right = 64mm] (6){};
\draw [->] (4) to (6);
我想要的是消除第一世界节点的“0 以下”:
\node[circle, minimum size=5mm, draw][right = 19mm] (3){};
\node[circle, minimum size=10mm, draw][right = 19mm] (3){};
\node[circle, minimum size=15mm, draw][right = 19mm] (3){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][right = 19mm] (3){};
\draw [->] (1) to (3);
\node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$}][below of = 1][right = 34mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 49mm] (4){};
\node[circle, minimum size=10mm, draw, label = {[label distance=0cm]:,3$}][below of = 1][right = 49mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=10mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=15mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}] [below of = 1][right = 64mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=10mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=15mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 2][right = 49mm] (5){};
\draw [->] (3) to (5);
\node[circle, minimum size=5mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=10mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=15mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 3][right = 64mm] (6){};
\draw [->] (4) to (6);
我是序言新手,
我尝试添加一些警卫,但它不起作用。
谁能帮帮我?
完整的工作示例,修复了您的问题并删除了选择点。
文件:spheres.pl
:- module(spheres,
[
draw_spheres/1
]).
% -----------------------------------------------------------------------------
worlds(_,_,_,_,[]) :- !.
worlds(Stream,Count,Right,Number,[Head|Tail]) :-
C is Count+1,
Number2 is Number+1,
single_world(Stream,Right,Number2,C,Head),
Right2 is Right+15,
worlds(Stream,C,Right2,Number2,Tail).
single_world(_,_,_,_,[]) :- !.
single_world(Stream,Right,Number,C,[Head|Tail]) :-
Right2 is Right+15,
atomic_list_concat(Head,',',Label),
world_sphere(Stream,5,Right2,Label,Number,C,Head),
write(Stream,"\draw [->] ("),
write(Stream, C),
write(Stream, ") to ("),
write(Stream, Number),
writeln(Stream, ");"),
single_world(Stream,Right2,Number,C,Tail).
world_sphere(_,_,_,_,_,_,[]) :- !.
world_sphere(Stream,Width,Right,Label,Number,C,[_|[]]) :-
write(Stream, "\node[circle, minimum size="),
write(Stream, Width),
write(Stream,"mm, draw, label = {[label distance=0cm]:$"),
write(Stream, Label),
write(Stream, "$}]"),
Below is C-1,
(
Below == 0
->
true
;
write(Stream, "[below of = "),
write(Stream, Below),
write(Stream,"]")
),
write(Stream,"[right = "),
write(Stream,Right),
write(Stream,"mm] ("),
write(Stream, Number),
writeln(Stream,"){};"), !.
world_sphere(Stream,Width,Right,Label,Number,C,[_|Tail]) :-
write(Stream, "\node[circle, minimum size="),
write(Stream, Width),
write(Stream,"mm, draw]"),
Below is C-1,
(
Below == 0
->
true
;
write(Stream,"[below of = "),
write(Stream, Below),
write(Stream,"]")
),
write(Stream,"[right = "),
write(Stream,Right),
write(Stream,"mm] ("),
write(Stream, Number),
writeln(Stream,"){};"),
Width2 is Width+5,
world_sphere(Stream,Width2,Right,Label,Number,C,Tail).
list_length(Xs,L) :-
list_length(Xs,0,L) .
list_length( [] , L , L ) .
list_length( [_|Xs] , T , L ) :-
T1 is T+1 ,
list_length(Xs,T1,L).
draw_spheres(FileName):-
open(FileName, write, Stream),
list_length([1,2], Worlds),
worlds(Stream,0,4,Worlds,[[[1,2,3,4]],[[1],[1,3],[1,2,3,4]],[[1,2,3,4]], [[1,2,3,4]]]),
close(Stream).
示例运行
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.28-20-g6f8a68f2b)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- working_directory(_,'C:/Users/groot').
true.
?- [spheres].
true.
?- draw_spheres('a.txt').
true.
文件:a.txt
\node[circle, minimum size=5mm, draw][right = 19mm] (3){};
\node[circle, minimum size=10mm, draw][right = 19mm] (3){};
\node[circle, minimum size=15mm, draw][right = 19mm] (3){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][right = 19mm] (3){};
\draw [->] (1) to (3);
\node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$}][below of = 1][right = 34mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 49mm] (4){};
\node[circle, minimum size=10mm, draw, label = {[label distance=0cm]:,3$}][below of = 1][right = 49mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=10mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=15mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 1][right = 64mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=10mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=15mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 2][right = 49mm] (5){};
\draw [->] (3) to (5);
\node[circle, minimum size=5mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=10mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=15mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 3][right = 64mm] (6){};
\draw [->] (4) to (6);
我有这个 prolog 代码,它用乳胶代码生成一个 txt 文件
worlds(_,_,_,_,[]) :- !.
worlds(Stream,Count,Right,Number,[Head|Tail]) :-
C is Count+1,
Number2 is Number+1,
single_world(Stream,Right,Number2,C,Head),
Right2 is Right+15,
worlds(Stream,C,Right2,Number2,Tail).
single_world(_,_,_,_,[]) :- !.
single_world(Stream,Right,Number,C,[Head|Tail]) :-
Right2 is Right+15,
atomic_list_concat(Head,',',Label),
world_sphere(Stream,5,Right2,Label,Number,C,Head),
write(Stream,"\draw [->] ("),
write(Stream, C),
write(Stream, ") to ("),
write(Stream, Number),
writeln(Stream, ");"),
single_world(Stream,Right2,Number,C,Tail).
world_sphere(_,_,_,_,_,_,[]) :- !.
world_sphere(Stream,Width,Right,Label,Number,C,[_|[]]):-
write(Stream, "\node[circle, minimum size="),
write(Stream, Width),
write(Stream,"mm, draw, label = {[label distance=0cm]:$"),
write(Stream, Label),
write(Stream, "$}]"),
write(Stream, "[below of = "),
Below is C-1,
write(Stream, Below),
write(Stream,"][right = "),
write(Stream,Right),
write(Stream,"mm] ("),
write(Stream, Number),
writeln(Stream,"){};").
world_sphere(Stream,Width,Right,Label,Number,C,[_|Tail]) :-
write(Stream, "\node[circle, minimum size="),
write(Stream, Width),
write(Stream,"mm, draw]"),
write(Stream,"[below of = "),
Below is C-1,
write(Stream, Below),
write(Stream,"]"),
write(Stream,"[right = "),
write(Stream,Right),
write(Stream,"mm] ("),
write(Stream, Number),
writeln(Stream,"){};"),
Width2 is Width+5,
world_sphere(Stream,Width2,Right,Label,Number,C,Tail).
list_length(Xs,L) :- list_length(Xs,0,L) .
list_length( [] , L , L ) .
list_length( [_|Xs] , T , L ) :-
T1 is T+1 ,
list_length(Xs,T1,L).
draw_spheres(FileName):-
open(FileName, write, Stream),
list_length([1,2], Worlds),
worlds(Stream,0,4,Worlds,[[[1,2,3,4]],[[1],[1,3],[1,2,3,4]],[[1,2,3,4]], [[1,2,3,4]]]),!,
close(Stream).
代码生成如下txt文件,需要用latex工具编译:
\node[circle, minimum size=5mm, draw][below of = 0][right = 19mm] (3){};
\node[circle, minimum size=10mm, draw][below of = 0][right = 19mm] (3){};
\node[circle, minimum size=15mm, draw][below of = 0][right = 19mm] (3){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}] [below of = 0][right = 19mm] (3){};
\draw [->] (1) to (3);
node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$}][below of = 1][right = 34mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 49mm] (4){};
\node[circle, minimum size=10mm, draw, label = {[label distance=0cm]:,3$}][below of = 1][right = 49mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=10mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=15mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 1][right = 64mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=10mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=15mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 2][right = 49mm] (5){};
\draw [->] (3) to (5);
\node[circle, minimum size=5mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=10mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=15mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 3][right = 64mm] (6){};
\draw [->] (4) to (6);
我想要的是消除第一世界节点的“0 以下”:
\node[circle, minimum size=5mm, draw][right = 19mm] (3){};
\node[circle, minimum size=10mm, draw][right = 19mm] (3){};
\node[circle, minimum size=15mm, draw][right = 19mm] (3){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][right = 19mm] (3){};
\draw [->] (1) to (3);
\node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$}][below of = 1][right = 34mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 49mm] (4){};
\node[circle, minimum size=10mm, draw, label = {[label distance=0cm]:,3$}][below of = 1][right = 49mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=10mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=15mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}] [below of = 1][right = 64mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=10mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=15mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 2][right = 49mm] (5){};
\draw [->] (3) to (5);
\node[circle, minimum size=5mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=10mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=15mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 3][right = 64mm] (6){};
\draw [->] (4) to (6);
我是序言新手, 我尝试添加一些警卫,但它不起作用。 谁能帮帮我?
完整的工作示例,修复了您的问题并删除了选择点。
文件:spheres.pl
:- module(spheres,
[
draw_spheres/1
]).
% -----------------------------------------------------------------------------
worlds(_,_,_,_,[]) :- !.
worlds(Stream,Count,Right,Number,[Head|Tail]) :-
C is Count+1,
Number2 is Number+1,
single_world(Stream,Right,Number2,C,Head),
Right2 is Right+15,
worlds(Stream,C,Right2,Number2,Tail).
single_world(_,_,_,_,[]) :- !.
single_world(Stream,Right,Number,C,[Head|Tail]) :-
Right2 is Right+15,
atomic_list_concat(Head,',',Label),
world_sphere(Stream,5,Right2,Label,Number,C,Head),
write(Stream,"\draw [->] ("),
write(Stream, C),
write(Stream, ") to ("),
write(Stream, Number),
writeln(Stream, ");"),
single_world(Stream,Right2,Number,C,Tail).
world_sphere(_,_,_,_,_,_,[]) :- !.
world_sphere(Stream,Width,Right,Label,Number,C,[_|[]]) :-
write(Stream, "\node[circle, minimum size="),
write(Stream, Width),
write(Stream,"mm, draw, label = {[label distance=0cm]:$"),
write(Stream, Label),
write(Stream, "$}]"),
Below is C-1,
(
Below == 0
->
true
;
write(Stream, "[below of = "),
write(Stream, Below),
write(Stream,"]")
),
write(Stream,"[right = "),
write(Stream,Right),
write(Stream,"mm] ("),
write(Stream, Number),
writeln(Stream,"){};"), !.
world_sphere(Stream,Width,Right,Label,Number,C,[_|Tail]) :-
write(Stream, "\node[circle, minimum size="),
write(Stream, Width),
write(Stream,"mm, draw]"),
Below is C-1,
(
Below == 0
->
true
;
write(Stream,"[below of = "),
write(Stream, Below),
write(Stream,"]")
),
write(Stream,"[right = "),
write(Stream,Right),
write(Stream,"mm] ("),
write(Stream, Number),
writeln(Stream,"){};"),
Width2 is Width+5,
world_sphere(Stream,Width2,Right,Label,Number,C,Tail).
list_length(Xs,L) :-
list_length(Xs,0,L) .
list_length( [] , L , L ) .
list_length( [_|Xs] , T , L ) :-
T1 is T+1 ,
list_length(Xs,T1,L).
draw_spheres(FileName):-
open(FileName, write, Stream),
list_length([1,2], Worlds),
worlds(Stream,0,4,Worlds,[[[1,2,3,4]],[[1],[1,3],[1,2,3,4]],[[1,2,3,4]], [[1,2,3,4]]]),
close(Stream).
示例运行
Welcome to SWI-Prolog (threaded, 64 bits, version 8.3.28-20-g6f8a68f2b)
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software.
Please run ?- license. for legal details.
For online help and background, visit https://www.swi-prolog.org
For built-in help, use ?- help(Topic). or ?- apropos(Word).
?- working_directory(_,'C:/Users/groot').
true.
?- [spheres].
true.
?- draw_spheres('a.txt').
true.
文件:a.txt
\node[circle, minimum size=5mm, draw][right = 19mm] (3){};
\node[circle, minimum size=10mm, draw][right = 19mm] (3){};
\node[circle, minimum size=15mm, draw][right = 19mm] (3){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][right = 19mm] (3){};
\draw [->] (1) to (3);
\node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$}][below of = 1][right = 34mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 49mm] (4){};
\node[circle, minimum size=10mm, draw, label = {[label distance=0cm]:,3$}][below of = 1][right = 49mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=10mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=15mm, draw][below of = 1][right = 64mm] (4){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 1][right = 64mm] (4){};
\draw [->] (2) to (4);
\node[circle, minimum size=5mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=10mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=15mm, draw][below of = 2][right = 49mm] (5){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 2][right = 49mm] (5){};
\draw [->] (3) to (5);
\node[circle, minimum size=5mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=10mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=15mm, draw][below of = 3][right = 64mm] (6){};
\node[circle, minimum size=20mm, draw, label = {[label distance=0cm]:,2,3,4$}][below of = 3][right = 64mm] (6){};
\draw [->] (4) to (6);