Predicates for drawing terms, draw/1 and drawv/1

These predicates are from the example code (download) for COT5315 Foundations of Programming Languages and Software Systems. (ref)

Robert van Engelen, the author, has given permission to make this public under the MIT license.


% PROLOG FILE: draw.pl
% MODULE: draw
% EXPORT: draw/1, drawv/1
%

/*
Copyright 2022 Robert van Engelen

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

% Robert van Engelen, Florida State University, 2012
%
% Draw expression trees
% Trees = any term over atoms, variables, unary/binary/ternary functors
%
% draw(T)  draws variables "as is"
% drawv(T) draws variables as A, B, ...
%
% ?- draw(2*(b/10)+a+1).
% ?- draw([1,2,3]).
% ?- drawv(fix:(P->(N->snd:P:N,(M->if:(eq:M:0):1:(fst:P:M))))).
%
%    ____________________________:_                               
%   /                              \                              
% fix     _________________________->                             
%        /                           \                            
%       A            _________________,_______                    
%                   /                         \                   
%             _____.            ______________->                  
%            /      \          /                \                 
%           B        :___     C             _____:________        
%                   /    \                 /              \       
%                  :_     B               :______          :___   
%                 /  \                   /       \        /    \  
%               snd   A             ____:         1      :_     C 
%                                  /     \              /  \      
%                                if       :__         fst   A     
%                                        /   \                    
%                                       :     0                   
%                                      / \                        
%                                    eq   C                       

:- module(draw, [draw/1, drawv/1]).

draw(T) :- size(T, S), tree([S]).

drawv(T) :- numbervars(T, 0, _), draw(T), fail.
drawv(_).

size(V, A-7) :- var(V), !, format(atom(A), '~w', [V]).
size(V, A-3) :- V = '$VAR'(_), !, format(atom(A), '~w', [V]).
size(T, S-W) :- T =.. [F,A,B,C], !, size(A, X-N), size(B, Y-M), size(C, Z-L), W is N + M + L + 2, S =.. [F,X-N,Y-M,Z-L].
size(T, S-W) :- T =.. [F,A,B], !, size(A, X-N), size(B, Y-M), W is N + M + 1, S =.. [F,X-N,Y-M].
size(T, S-W) :- T =.. [F,A], !, size(A, X-W), S =.. [F,X-W].
size(A, A-W) :- atomic(A), atom_length(A, N), W is N + 1 + N mod 2.

tree(Ts) :- memberchk(_-_, Ts), !, labs(Ts), nl, line(Ts, Xs, Xs), nl, tree(Xs).
tree(_ ).

labs([A-W|Ts]) :- atomic(A), !, cent(A, W), labs(Ts).
labs([T-W|Ts]) :- T =.. [F,_-N,_-M,_-L], !, disp(F, N, M, L, W), labs(Ts).
labs([T-W|Ts]) :- T =.. [F,_-N,_-M], !, disp(F, N, M, W), labs(Ts).
labs([T-W|Ts]) :- T =.. [F|_], !, cent(F, W), labs(Ts).
labs([  W|Ts]) :- !, tab(W), labs(Ts).
labs([]      ).


line([A-W|Ts], Xs, [  W            |Ys]) :- atomic(A), !, tab(W), line(Ts, Xs, Ys).
line([T-W|Ts], Xs, [A-N,K,B-M,K,C-L|Ys]) :- T =.. [_,A-N,B-M,C-L], !, K is (W - N - M - L)//2, fork(N, M, L, W), line(Ts, Xs, Ys).
line([T-W|Ts], Xs, [A-N,K,B-M      |Ys]) :- T =.. [_,A-N,B-M], !, K is W - N - M, fork(N, M, W), line(Ts, Xs, Ys).
line([T-W|Ts], Xs, [A-N            |Ys]) :- T =.. [_,A-N], !, cent('|', W), line(Ts, Xs, Ys).
line([  W|Ts], Xs, [  W            |Ys]) :- !, tab(W), line(Ts, Xs, Ys).
line([],       _,  []                  ).

disp(A, N, M, W) :-
	atom_length(A, L),
	K1 is N//2 + 2,
	K3 is M//2 + 2,
	K4 is (W-L)//2,
	K5 is W - K1 - K3,
	K6 is K4 - K1,
	K7 is K5 - K6 - L,
	(	K5 < L
	->	cent(A, W)
	;	K6 >= 0, K7 >= 0
	->	tab(K1),
		dash(K6),
		write(A),
		dash(K7),
		tab(K3)
	;	K6 >= 0
	->	K8 is K6 + K7,
		tab(K1),
		dash(K8),
		write(A),
		tab(K3)
	;	K7 >= 0
	->	K8 is K7 + K6,
		tab(K1),
		write(A),
		dash(K8),
		tab(K3)
	).

disp(A, N, _, L, W) :-
	disp(A, N, L, W).

cent(A, W) :-
	chop(A, W, A1, L),
	K1 is (W-L)//2,
	K2 is K1 + (W-L) mod 2,
	tab(K1),
	write(A1),
	tab(K2).

fork(N, M, W) :-
	K1 is N//2 + 1,
	tab(K1),
	write('/'),
	K2 is M//2 + 1,
	K3 is K1 + K2 + W - N - M - 4,
	tab(K3),
	write('\\'),
	tab(K2).

fork(N, M, L, W) :-
	K1 is N//2 + 1,
	tab(K1),
	write('/'),
	K2 is M//2 + 1,
	K3 is L//2 + 1,
	K4 is K1 + K2 + (W - N - M - L)//2 - 3,
	tab(K4),
	write('|'),
	K5 is K2 + K3 + (W - N - M - L)//2 - 3,
	tab(K5),
	write('\\'),
	tab(K3).

chop(A, W, A1, L1) :-
	atom_length(A, L),
	(	L =< W
	->	A1 = A,
		L1 = L
	;	sub_atom(A, 0, W, _, A1),
		L1 = W
	).

dash(K) :- K > 0, !, write('_'), K1 is K-1, dash(K1).
dash(_).
4 Likes