Search code examples
prolognested-listsguard

Boolean guard in PROLOG


I have this prolog code that generates a txt file with latex code

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).

The code generates the following txt file that needs to be compiled with latex tools:

\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]:$1,2,3,4$}]    [below of = 0][right = 19mm] (3){};
\draw [->] (1) to (3);
node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$1$}][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]:$1,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]:$1,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]:$1,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]:$1,2,3,4$}][below of = 3][right = 64mm] (6){};
\draw [->] (4) to (6);

And what i want is something like this eliminating "below of 0" for the nodes of the first world:

\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]:$1,2,3,4$}][right = 19mm] (3){};
\draw [->] (1) to (3);
\node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$1$}][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]:$1,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]:$1,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]:$1,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]:$1,2,3,4$}][below of = 3][right = 64mm] (6){};
\draw [->] (4) to (6);

I'm new to prolog, I try to add some guards but it doesn't work. Can anyone help me?


Solution

  • Full working example with fix to your question and cut to remove choice points.

    File: 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).
    

    Example run

    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.
    
    

    File: 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]:$1,2,3,4$}][right = 19mm] (3){};
    \draw [->] (1) to (3);
    \node[circle, minimum size=5mm, draw, label = {[label distance=0cm]:$1$}][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]:$1,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]:$1,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]:$1,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]:$1,2,3,4$}][below of = 3][right = 64mm] (6){};
    \draw [->] (4) to (6);