[m-dev.] for review: Added a new format "newpretty" to the browser.

Sarvamanan THURAIRATNAM sthur at students.cs.mu.oz.au
Fri Feb 16 13:01:13 AEDT 2001


Estimated hours taken : 185

For Fergus Henderson to Review. 

Added a new pretty printing format to the term browser. This new format 
helps put a limit on the size of the term printed during debugging. 
This limit is specified by setting the number of lines you want the term to be 
printed on and the width of these lines. Refer to sized_pretty.m for Examples. 

browser/sized_pretty.m: 
	New file that does what's described above. 

browser/browse.m: 
browser/browser_info.m: 
browser/mdb.m: 
browser/parse.m: 
trace/mercury_trace_browse.c: 
trace/mercury_trace_browse.h: 
trace/mercury_trace_internal.c: 
	Modified to accommodate the new format.

tests/debugger/browse_pretty.inp:
	Included test cases for the new pretty printing format.

tests/debugger/browse_pretty.exp:
	Changed the expected output.

Index: browser/browse.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/browse.m,v
retrieving revision 1.17
diff -u -r1.17 browse.m
--- browser/browse.m	2001/01/09 23:30:13	1.17
+++ browser/browse.m	2001/02/16 01:13:51
@@ -1,5 +1,5 @@
 %---------------------------------------------------------------------------%
-% Copyright (C) 1998-2000 The University of Melbourne.
+% Copyright (C) 1998-2001 The University of Melbourne.
 % This file may only be copied under the terms of the GNU Library General
 % Public License - see the file COPYING.LIB in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -82,7 +82,7 @@
 %---------------------------------------------------------------------------%
 :- implementation.
 
-:- import_module mdb__parse, mdb__util, mdb__frame.
+:- import_module mdb__parse, mdb__util, mdb__frame, mdb__sized_pretty.
 :- import_module string, list, parser, require, std_util, int, char, pprint.
 :- import_module bool.
 
@@ -294,8 +294,8 @@
 	% XXX We can't yet give options to the `set' command.
 	%
 	No = bool__no,
-	browser_info__set_param(No, No, No, No, No, No, Setting, Info0 ^ state,
-			NewState),
+	browser_info__set_param(No, No, No, No, No, No, No, Setting, 
+			Info0 ^ state, NewState),
 	Info = Info0 ^ state := NewState.
 
 :- pred help(debugger::in, io__state::di, io__state::uo) is det.
@@ -317,7 +317,8 @@
 "\th              -- help\n",
 "\n",
 "-- settings:\n",
-"--    size; depth; path; format (flat pretty verbose); width; lines\n",
+"--    size; depth; path; format (flat raw_pretty verbose pretty); width; ",
+"lines\n",
 "--    Paths can be Unix-style or SICStus-style: /2/3/1 or ^2^3^1\n",
 "\n"],
 		HelpMessage) },
@@ -342,11 +343,14 @@
 			{ Format = flat },
 			portray_flat(Debugger, SubUniv, Params)
 		;
-			{ Format = pretty },
-			portray_pretty(Debugger, SubUniv, Params)
+			{ Format = raw_pretty },
+			portray_raw_pretty(Debugger, SubUniv, Params)
 		;
 			{ Format = verbose },
 			portray_verbose(Debugger, SubUniv, Params)
+		;
+			{ Format = pretty },
+			portray_pretty(Debugger, SubUniv, Params)
 		)
 	;
 		write_string_debugger(Debugger, "error: no such subterm")
@@ -393,14 +397,23 @@
 			Params ^ width, Params ^ lines, Str) },
 	write_string_debugger(Debugger, Str).
 
+:- pred portray_raw_pretty(debugger, univ, format_params, io__state, io__state).
+:- mode portray_raw_pretty(in, in, in, di, uo) is det.
+
+portray_raw_pretty(Debugger, Univ, Params) -->
+	{ term_to_string_raw_pretty(Univ, Params ^ width, 
+			Params ^ depth, Str) },
+	write_string_debugger(Debugger, Str).
+
+
 :- pred portray_pretty(debugger, univ, format_params, io__state, io__state).
 :- mode portray_pretty(in, in, in, di, uo) is det.
 
 portray_pretty(Debugger, Univ, Params) -->
-	{ term_to_string_pretty(Univ, Params ^ width, Params ^ depth, Str) },
+	{ sized_pretty__univ_to_string_line(Univ, Params ^ width, 
+			Params ^ lines, Str) },
 	write_string_debugger(Debugger, Str).
 
-
 	% The maximum estimated size for which we use `io__write'.
 :- pred max_print_size(int::out) is det.
 max_print_size(60).
@@ -509,10 +522,10 @@
 % provides no way of doing this.
 %
 
-:- pred term_to_string_pretty(univ, int, int, string).
-:- mode term_to_string_pretty(in, in, in, out) is det.
+:- pred term_to_string_raw_pretty(univ, int, int, string).
+:- mode term_to_string_raw_pretty(in, in, in, out) is det.
 
-term_to_string_pretty(Univ, Width, MaxDepth, Str) :-
+term_to_string_raw_pretty(Univ, Width, MaxDepth, Str) :-
 	Value = univ_value(Univ),
 	Doc = to_doc(MaxDepth, Value),
 	Str = to_string(Width, Doc).
@@ -866,11 +879,14 @@
 		{ X = flat },
 		send_term_to_socket(browser_str("flat"))
 	;
-		{ X = pretty },
-		send_term_to_socket(browser_str("pretty"))
+		{ X = raw_pretty },
+		send_term_to_socket(browser_str("raw_pretty"))
 	;
 		{ X = verbose },
 		send_term_to_socket(browser_str("verbose"))
+	;
+		{ X = pretty },
+		send_term_to_socket(browser_str("pretty"))
 	).
 
 :- pred send_term_to_socket(term_browser_response, io__state, io__state).
Index: browser/browser_info.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/browser_info.m,v
retrieving revision 1.1
diff -u -r1.1 browser_info.m
--- browser/browser_info.m	2000/10/27 08:38:49	1.1
+++ browser/browser_info.m	2001/02/16 01:13:51
@@ -1,5 +1,5 @@
 %---------------------------------------------------------------------------%
-% Copyright (C) 2000 The University of Melbourne.
+% Copyright (C) 2000-2001 The University of Melbourne.
 % This file may only be copied under the terms of the GNU Library General
 % Public License - see the file COPYING.LIB in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -54,8 +54,12 @@
 	%
 :- type portray_format
 	--->	flat
-	;	pretty
-	;	verbose.
+	;	raw_pretty	% calls pprint module directly, without first 
+				% attempting to manipulate the term in any way.
+	;	verbose
+	;	pretty.		% It allows the user to specify the maximum 
+				% number of lines which the term has to be 
+				% printed within.
 
 :- type format_params
 	--->	format_params(
@@ -110,8 +114,9 @@
 	% and -v, in that order.
 	%
 :- pred browser_info__set_param(bool::in, bool::in, bool::in, bool::in,
-		bool::in, bool::in, setting::in, browser_persistent_state::in,
-		browser_persistent_state::out) is det.
+		bool::in, bool::in, bool::in, setting::in, 
+		browser_persistent_state::in, browser_persistent_state::out) 
+		is det.
 
 %---------------------------------------------------------------------------%
 
@@ -127,40 +132,40 @@
 	%
 
 :- pred set_param_depth(bool::in, bool::in, bool::in, bool::in, bool::in,
-		bool::in, int::in, browser_persistent_state::in,
+		bool::in, bool::in, int::in, browser_persistent_state::in,
 		browser_persistent_state::out) is det.
-:- pragma export(set_param_depth(in, in, in, in, in, in, in, in, out),
+:- pragma export(set_param_depth(in, in, in, in, in, in, in, in, in, out),
 		"ML_BROWSE_set_param_depth").
 
-set_param_depth(P, B, A, F, Pr, V, Depth) -->
-	browser_info__set_param(P, B, A, F, Pr, V, depth(Depth)).
+set_param_depth(P, B, A, F, Pr, V, NPr, Depth) -->
+	browser_info__set_param(P, B, A, F, Pr, V, NPr,  depth(Depth)).
 
 :- pred set_param_size(bool::in, bool::in, bool::in, bool::in, bool::in,
-		bool::in, int::in, browser_persistent_state::in,
+		bool::in, bool::in, int::in, browser_persistent_state::in,
 		browser_persistent_state::out) is det.
-:- pragma export(set_param_size(in, in, in, in, in, in, in, in, out),
+:- pragma export(set_param_size(in, in, in, in, in, in, in, in, in, out),
 		"ML_BROWSE_set_param_size").
 
-set_param_size(P, B, A, F, Pr, V, Size) -->
-	browser_info__set_param(P, B, A, F, Pr, V, size(Size)).
+set_param_size(P, B, A, F, Pr, NPr, V, Size) -->
+	browser_info__set_param(P, B, A, F, Pr, V, NPr, size(Size)).
 
 :- pred set_param_width(bool::in, bool::in, bool::in, bool::in, bool::in,
-		bool::in, int::in, browser_persistent_state::in,
+		bool::in, bool::in, int::in, browser_persistent_state::in,
 		browser_persistent_state::out) is det.
-:- pragma export(set_param_width(in, in, in, in, in, in, in, in, out),
+:- pragma export(set_param_width(in, in, in, in, in, in, in, in, in, out),
 		"ML_BROWSE_set_param_width").
 
-set_param_width(P, B, A, F, Pr, V, Width) -->
-	browser_info__set_param(P, B, A, F, Pr, V, width(Width)).
+set_param_width(P, B, A, F, Pr, V, NPr, Width) -->
+	browser_info__set_param(P, B, A, F, Pr, V, NPr, width(Width)).
 
 :- pred set_param_lines(bool::in, bool::in, bool::in, bool::in, bool::in,
-		bool::in, int::in, browser_persistent_state::in,
+		bool::in, bool::in, int::in, browser_persistent_state::in,
 		browser_persistent_state::out) is det.
-:- pragma export(set_param_lines(in, in, in, in, in, in, in, in, out),
+:- pragma export(set_param_lines(in, in, in, in, in, in, in, in, in, out),
 		"ML_BROWSE_set_param_lines").
 
-set_param_lines(P, B, A, F, Pr, V, Lines) -->
-	browser_info__set_param(P, B, A, F, Pr, V, lines(Lines)).
+set_param_lines(P, B, A, F, Pr, V, NPr, Lines) -->
+	browser_info__set_param(P, B, A, F, Pr, V, NPr, lines(Lines)).
 
 :- pred set_param_format(bool::in, bool::in, bool::in, portray_format::in,
 		browser_persistent_state::in, browser_persistent_state::out)
@@ -172,7 +177,7 @@
 	%
 	% Any format flags are ignored for this parameter.
 	%
-	browser_info__set_param(P, B, A, no, no, no, format(Format)).
+	browser_info__set_param(P, B, A, no, no, no, no, format(Format)).
 
 %---------------------------------------------------------------------------%
 
@@ -211,8 +216,9 @@
 	--->	caller_params(
 			default_format		:: portray_format,
 			flat_params		:: format_params,
-			pretty_params		:: format_params,
-			verbose_params		:: format_params
+			raw_pretty_params	:: format_params,
+			verbose_params		:: format_params,
+			pretty_params		:: format_params
 		).
 
 	% Initialise the persistent settings with default values.  The
@@ -244,37 +250,42 @@
 
 caller_type_print_defaults(Params) :-
 	DefaultFormat = flat,
-	Flat	= format_params(3, 10, 80, 25),
-	Pretty	= format_params(3, 10, 80, 25),
-	Verbose	= format_params(3, 10, 80, 25),
-	Params	= caller_params(DefaultFormat, Flat, Pretty, Verbose).
+	Flat	  = format_params(3, 10, 80, 25),
+	RawPretty = format_params(3, 10, 80, 25),
+	Verbose	  = format_params(3, 10, 80, 25),
+	Pretty    = format_params(3, 10, 80, 25),
+	Params = caller_params(DefaultFormat, Flat, RawPretty, Verbose, Pretty).
 
 :- pred caller_type_browse_defaults(caller_params).
 :- mode caller_type_browse_defaults(out) is det.
 
 caller_type_browse_defaults(Params) :-
 	DefaultFormat = verbose,
-	Flat	= format_params(10, 30, 80, 25),
-	Pretty	= format_params(10, 30, 80, 25),
-	Verbose	= format_params(10, 30, 80, 25),
-	Params	= caller_params(DefaultFormat, Flat, Pretty, Verbose).
+	Flat	  = format_params(10, 30, 80, 25),
+	RawPretty = format_params(10, 30, 80, 25),
+	Verbose	  = format_params(10, 30, 80, 25),
+	Pretty    = format_params(10, 30, 80, 25),
+	Params = caller_params(DefaultFormat, Flat, RawPretty, Verbose, Pretty).
 
 :- pred caller_type_print_all_defaults(caller_params).
 :- mode caller_type_print_all_defaults(out) is det.
 
 caller_type_print_all_defaults(Params) :-
 	DefaultFormat = flat,
-	Flat	= format_params(3, 10, 80, 2),
-	Pretty	= format_params(3, 10, 80, 2),
-	Verbose	= format_params(3, 10, 80, 5),
-	Params	= caller_params(DefaultFormat, Flat, Pretty, Verbose).
+	Flat	  = format_params(3, 10, 80, 2),
+	RawPretty = format_params(3, 10, 80, 2),
+	Verbose   = format_params(3, 10, 80, 5),
+	Pretty = format_params(3, 10, 80, 2),
+	Params = caller_params(DefaultFormat, Flat, RawPretty, Verbose, Pretty).
 
-browser_info__set_param(P0, B0, A0, F0, Pr0, V0, Setting, State0, State) :-
+browser_info__set_param(P0, B0, A0, F0, Pr0, V0, NPr0, Setting, State0, State):-
 	default_all_yes(P0, B0, A0, P, B, A),
-	default_all_yes(F0, Pr0, V0, F, Pr, V),
-	maybe_set_param(P, F, Pr, V, Setting, State0 ^ print_params, PParams),
-	maybe_set_param(B, F, Pr, V, Setting, State0 ^ browse_params, BParams),
-	maybe_set_param(A, F, Pr, V, Setting, State0 ^ print_all_params,
+	default_all_yes(F0, Pr0, V0, NPr0, F, Pr, V, NPr),
+	maybe_set_param(P, F, Pr, V, NPr, Setting, State0 ^ print_params, 
+			PParams),
+	maybe_set_param(B, F, Pr, V, NPr, Setting, State0 ^ browse_params, 
+			BParams),
+	maybe_set_param(A, F, Pr, V, NPr, Setting, State0 ^ print_all_params,
 			AParams),
 	State = browser_persistent_state(PParams, BParams, AParams).
 
@@ -299,25 +310,52 @@
 		B = B0,
 		C = C0
 	).
+
+:- pred default_all_yes(bool, bool, bool, bool, bool, bool, bool, bool).
+:- mode default_all_yes(in, in, in, in, out, out, out, out) is det.
+
+default_all_yes(A0, B0, C0, D0, A, B, C, D) :-
+	%
+	% If none of the flags are set, the command by default
+	% applies to _all_ caller types/formats.
+	%
+	(
+		A0 = no,
+		B0 = no,
+		C0 = no,
+		D0 = no
+	->
+		A = yes,
+		B = yes,
+		C = yes,
+		D = yes
+	;
+		A = A0,
+		B = B0,
+		C = C0,
+		D = D0
+	).
 
-:- pred maybe_set_param(bool, bool, bool, bool, setting, caller_params,
+:- pred maybe_set_param(bool, bool, bool, bool, bool, setting, caller_params,
 		caller_params).
-:- mode maybe_set_param(in, in, in, in, in, in, out) is det.
+:- mode maybe_set_param(in, in, in, in, in, in, in, out) is det.
 
-maybe_set_param(no, _, _, _, _, Params, Params).
-maybe_set_param(yes, F, Pr, V, Setting, Params0, Params) :-
+maybe_set_param(no, _, _, _, _, _, Params, Params).
+maybe_set_param(yes, F, Pr, V, NPr, Setting, Params0, Params) :-
 	(
 		Setting = format(NewFormat)
 	->
 		Params = Params0 ^ default_format := NewFormat
 	;
 		maybe_set_param_2(F, Setting, Params0 ^ flat_params, FParams),
-		maybe_set_param_2(Pr, Setting, Params0 ^ pretty_params,
+		maybe_set_param_2(Pr, Setting, Params0 ^ raw_pretty_params,
 				PrParams),
 		maybe_set_param_2(V, Setting, Params0 ^ verbose_params,
 				VParams),
+		maybe_set_param_2(NPr, Setting, Params0 ^ pretty_params,
+				NPrParams),
 		Params = caller_params(Params0 ^ default_format, FParams,
-				PrParams, VParams)
+				PrParams, VParams, NPrParams)
 	).
 
 :- pred maybe_set_param_2(bool, setting, format_params, format_params).
@@ -343,8 +381,9 @@
 :- mode get_caller_format_params(in, in, out) is det.
 
 get_caller_format_params(Params, flat, Params ^ flat_params).
-get_caller_format_params(Params, pretty, Params ^ pretty_params).
+get_caller_format_params(Params, raw_pretty, Params ^ raw_pretty_params).
 get_caller_format_params(Params, verbose, Params ^ verbose_params).
+get_caller_format_params(Params, pretty, Params ^ pretty_params).
 
 %---------------------------------------------------------------------------%
 
Index: browser/mdb.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/mdb.m,v
retrieving revision 1.4
diff -u -r1.4 mdb.m
--- browser/mdb.m	2000/10/27 08:38:49	1.4
+++ browser/mdb.m	2001/02/16 01:13:51
@@ -1,5 +1,5 @@
 %---------------------------------------------------------------------------%
-% Copyright (C) 1998-2000 The University of Melbourne.
+% Copyright (C) 1998-2001 The University of Melbourne.
 % This file may only be copied under the terms of the GNU Library General
 % Public License - see the file COPYING.LIB in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -20,7 +20,7 @@
 
 :- implementation.
 
-:- include_module frame, parse, util.
+:- include_module frame, parse, util, sized_pretty.
 :- include_module declarative_analyser, declarative_oracle, declarative_user.
 
 	% XXX these modules are more generally useful, but the
Index: browser/parse.m
===================================================================
RCS file: /home/mercury1/repository/mercury/browser/parse.m,v
retrieving revision 1.9
diff -u -r1.9 parse.m
--- browser/parse.m	2000/10/27 08:38:50	1.9
+++ browser/parse.m	2001/02/16 01:13:51
@@ -1,5 +1,5 @@
 %---------------------------------------------------------------------------%
-% Copyright (C) 1998-2000 The University of Melbourne.
+% Copyright (C) 1998-2001 The University of Melbourne.
 % This file may only be copied under the terms of the GNU Library General
 % Public License - see the file COPYING.LIB in the Mercury distribution.
 %---------------------------------------------------------------------------%
@@ -45,8 +45,9 @@
 %
 %	fmt:
 %		"flat"
-%		"pretty"
+%		"raw_pretty"
 %		"verbose"
+%		"pretty"
 %
 %	path:
 %		["/"] [dirs]
@@ -166,7 +167,7 @@
 	; char__is_digit(C) ->
 		dig_to_int(C, N),
 		lexer_num(N, Cs, Toks)
-	; char__is_alpha(C) ->
+	; char__is_alpha_or_underscore(C) ->
 		lexer_name(C, Cs, Toks)
 	; char__is_whitespace(C) ->
 		lexer(Cs, Toks)
@@ -217,7 +218,7 @@
 :- pred lexer_name(char, list(char), list(token)).
 :- mode lexer_name(in, in, out) is det.
 lexer_name(C, Cs, Toks) :-
-	list__takewhile(char__is_alpha, Cs, Letters, Rest),
+	list__takewhile(char__is_alpha_or_underscore, Cs, Letters, Rest),
 	string__from_char_list([C | Letters], Name),
 	lexer(Rest, Toks2),
 	Toks = [name(Name) | Toks2].
@@ -331,11 +332,13 @@
 		Toks = [Fmt],
 		( Fmt = name("flat") ->
 			Setting = format(flat)
-		; Fmt = name("pretty") ->
-			Setting = format(pretty)
-		;
-			Fmt = name("verbose"),
+		; Fmt = name("raw_pretty") ->
+			Setting = format(raw_pretty)
+		; Fmt = name("verbose") ->
 			Setting = format(verbose)
+		; 
+			Fmt = name("pretty"),
+			Setting = format(pretty)
 		)
 	;
 		fail
@@ -426,9 +429,11 @@
 :- mode show_format(in, di, uo) is det.
 show_format(flat) -->
 	io__write_string("flat").
-show_format(pretty) -->
-	io__write_string("pretty").
+show_format(raw_pretty) -->
+	io__write_string("raw_pretty").
 show_format(verbose) -->
 	io__write_string("verbose").
+show_format(pretty) -->
+	io__write_string("pretty").
 
 %---------------------------------------------------------------------------%
Index: browser/sized_pretty.m
===================================================================
RCS file: sized_pretty.m
diff -N sized_pretty.m
--- /dev/null	Wed Nov 15 09:24:47 2000
+++ sized_pretty.m	Fri Feb 16 12:13:51 2001
@@ -0,0 +1,922 @@
+%---------------------------------------------------------------------------%
+% Copyright (C) 2001 The University of Melbourne.
+% This file may only be copied under the terms of the GNU Library General
+% Public License - see the file COPYING.LIB in the Mercury distribution.
+%---------------------------------------------------------------------------%
+
+% sized_pretty-	When printing a term during debugging this module allows
+%		the user to put a limit on the size of the term displayed.
+%               This limit is specified by setting the number of lines you 
+%               want and the width of these lines. 
+%
+%
+% author: sthur
+%
+% How to use sized_pretty.m :
+% ---------------------------
+%
+% Call univ_to_string_line with the follwing variables:
+% 	univ_to_string_line(Univ, LineWidth, Lines, String)
+%		where Univ 	: is the Term (in univ type) you want to convert
+%		      LineWidth : is the length of the lines
+%		      Lines 	: is the number of lines you want the term to be
+%		            	  printed on
+%		      String	: output string
+%
+% EXAMPLES
+% --------
+%
+% Term Used in these examples:
+%
+%	Term = big(
+%                 big(
+%                    big(
+%                       small,
+%			"Level 3",
+%                       small
+%                       ),
+%		     "Level 2",
+%                    small
+%                    ),
+%		  "Level 1",
+%                 big(
+%                    big(
+%                       small,
+%		        "Level 3",
+%			small
+%                       ),
+%		     "Level 2",
+%		     small
+%                    )).
+%
+%------------------------------------------------------------------------------%
+% Width = 18, Line(s) = 16
+%
+% big(
+%   big(
+%     big(
+%       small, 
+%       "Level 3", 
+%       small), 
+%     "Level 2", 
+%     small), 
+%   "Level 1", 
+%   big(
+%     big(
+%       small, 
+%       "Level 3", 
+%       small), 
+%     "Level 2", 
+%     small))
+% 
+%------------------------------------------------------------------------------%
+% Width = 16, Line(s) = 16
+% 
+% big(
+%   big(
+%     big/3, 
+%     "Level 2", 
+%     small), 
+%   "Level 1", 
+%   big(
+%     big/3, 
+%     "Level 2", 
+%     small))
+% 
+%------------------------------------------------------------------------------%
+% Width = 50, Line(s) = 4
+% 
+% big(
+%   big(big/3, "Level 2", small), 
+%  "Level 1", 
+%   big(big/3, "Level 2", small))
+% 
+%------------------------------------------------------------------------------%
+% Width = 56, Line(s) = 4
+% 
+% big(
+%   big(big(small, "Level 3", small), "Level 2", small), 
+%   "Level 1", 
+%   big(big(small, "Level 3", small), "Level 2", small))
+% 
+%------------------------------------------------------------------------------%
+% Width = 30, Line(s) = 5
+% 
+% big(big/3, "Level 1", big/3)
+% 
+%------------------------------------------------------------------------------%
+% Width = 33, Line(s) = 5
+% 
+% big(
+%   big(big/3, "Level 2", small), 
+%   "Level 1", 
+%   big(big/3, "Level 2", small))
+% 
+%------------------------------------------------------------------------------%
+% Width = 75, Line(s) = 1
+% 
+% big(big(big/3, "Level 2", small), "Level 1", big(big/3, "Level 2", small))
+% 
+%------------------------------------------------------------------------------%
+% Width = 20, Line(s) = 10
+% 
+% big(
+%   big(
+%     big/3, 
+%     "Level 2", 
+%     small), 
+%   "Level 1", 
+%   big(
+%     big/3, 
+%     "Level 2", 
+%     small))
+% 
+%------------------------------------------------------------------------------%
+% Width = 40, Line(s) = 10
+% 
+% big(
+%   big(
+%     big(small, "Level 3", small), 
+%     "Level 2", 
+%     small), 
+%   "Level 1", 
+%   big(
+%     big(small, "Level 3", small), 
+%     "Level 2", 
+%     small))
+% 
+%------------------------------------------------------------------------------%
+% Width = 29, Line(s) = 1
+% 
+% big(big/3, "Level 1", big/3)
+% 
+%------------------------------------------------------------------------------%
+% Width = 20, Line(s) = 1
+% 
+% big/3
+% 
+%------------------------------------------------------------------------------%
+
+:- module mdb__sized_pretty.
+
+:- interface.
+
+:- import_module std_util, int, string.
+
+	% This may throw an exception or cause a runtime abort if the term
+	% in question has user-defined equality. 
+	% The Limit is number of lines.
+:- pred univ_to_string_line(univ::in, int::in, int::in, string::out) is det.
+
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module list, require, assoc_list, pprint, bool.
+
+:- type no_measure_params --->	no_measure_params.
+:- type measure_params
+	--->	measure_params(int).	% This parameter specifies Linewidth
+
+
+:- type maybe_deconstructed(T)
+	--->	not_deconstructed
+	;	deconstructed(
+			string,			% Functor 
+			int,			% Arity
+			size_annotated_args(T) 	% arguments
+		).
+
+	% The exact type indicates the term has been fully deconstructed.
+	% The at_least type indicates that the term has been deconstructed
+	% upto the point where the specified limit was reached.
+:- type size_annotated_term(T)
+	--->	exact(
+			univ,		% univ(Term)
+			T,		% size of the term
+			string,		% Functor
+			int,		% Arity
+			list(maybe(pair(T, size_annotated_term(T)))) % arguments
+		)
+	;	at_least(
+			univ,		% univ(Term)
+			T,		% size of the term upto the point
+					% where it's deconstructed
+			maybe_deconstructed(T)
+		).
+
+:- type size_annotated_args(T) == 
+	list(maybe(pair(T, size_annotated_term(T)))). 
+
+:- typeclass measure(T) where [
+	func max_measure(T, T) = T is det,
+	func zero_measure = T is det,
+	func compare_measures(T, T) = comparison_result is det
+].
+
+:- typeclass measure_with_params(T, MeasureParams) <= measure(T) where [
+	func add_measures(T, T, MeasureParams) = T is det,
+	func subtract_measures(T, T, MeasureParams) = T is det,
+
+		% given a term, it's arity, and a limit, this method decides
+		% the partial limit that each of the argument should be
+		% given. It's arguments in order are term, measure parameter(s),
+		% limit, arity, a flag (once the limit for the subterms 
+		% is determined the subterms are flagged with this limit), size 
+		% of the Functor, partial limit, adjusted limit, adjusted 
+		% measure parameter(s).
+		% Also a term is not deconstructed unless it has enough space
+		% to print functor and the functors of it's arguments.
+		% If the bool is `yes', we check that there is enough space to 
+		% print the functor and the functors of its arguments before 
+		% deconstructing the term. 
+	pred measured_split(univ::in, MeasureParams::in, T::in, int::in,
+	     bool::in, T::out, maybe(T)::out, T::out, MeasureParams::out) is det
+		
+].
+
+%------------------------------------------------------------------------------%
+	% When the space (to print the term) is given in number of lines there
+	% is an inital check that has to be done. This is due to the fact that
+	% size_count_split assumes that every term is part of a bigger term and
+	% therefore assumes that a term will have a comma and a space after it.
+	% This is not true for the biggest term (Head Term) therefore if the
+	% Head Term is going to be printed on a single line then it should be
+	% given a limit of character_count(LineWidth - 1) instead of
+	% character_count(LineWidth - 3).
+univ_to_string_line(Univ, LineWidth, Lines, String) :-
+	Params = measure_params(LineWidth),
+	deconstruct(univ_value(Univ), _, Arity, _),
+	( 	Arity \= 0,
+		Lines \= 0,
+		(Lines - 1) // Arity = 0 
+	->
+		% "- 1" is to account for the newline character
+		Limit = character_count(LineWidth - 1)
+	;
+		Limit = line_count(Lines)
+	),
+	annotate_with_size(Univ, Params, Limit, AnnotTerm),
+	Doc = to_doc_sized(AnnotTerm),
+	String = pprint__to_string(LineWidth, Doc).
+
+%------------------------------------------------------------------------------%
+	% first_pass gives an idea of how much space each term takes
+	% In this pass the space is unevenly distributed. First come first
+	% served. And once the space runs out, the term is not deconstructed
+	% further. 
+	% In The Second pass the space is evenly distributed between
+	% the terms and therefore the subterms are deconstructed evenly.
+:- pred annotate_with_size(univ::in, MeasureParams::in, T::in,
+	size_annotated_term(T)::out) is det
+	<= measure_with_params(T, MeasureParams).
+
+annotate_with_size(Univ, Params, Limit, Size2) :-
+	first_pass(Univ, Params, Limit, Size1),
+	second_pass(Size1, Params, Limit, Size2).
+
+%------------------------------------------------------------------------------%
+	
+:- pred first_pass(univ::in, MeasureParams::in, T::in,
+	size_annotated_term(T)::out) is det
+	<= measure_with_params(T, MeasureParams).
+
+first_pass(Univ, Params, Limit, Size) :-
+	deconstruct(univ_value(Univ), Functor, Arity, Args),	
+	measured_split(Univ, Params, Limit, Arity, yes, FunctorSize, 
+					Flag, NewLimit, NewParams),
+	flag_with(Args, Flag, FlaggedUnivArgs),
+	( (Arity \= 0, Flag = no) ->
+		Exact0 = no
+	;
+		Exact0 = yes
+	),
+        annotate_args_with_size(FlaggedUnivArgs, NewParams, NewLimit, 
+		FunctorSize, SoFar, Exact0, Exact, MaybeArgSizes),
+	(
+		Exact = no,
+	        Size = at_least(Univ, SoFar,
+	                        deconstructed(Functor, Arity, MaybeArgSizes))
+	;
+	        Exact = yes,
+	        Size = exact(Univ, SoFar, Functor, Arity, MaybeArgSizes)
+	).
+
+%------------------------------------------------------------------------------%
+	% annotating the arguments.
+:- pred annotate_args_with_size(assoc_list(maybe(T), univ)::in,
+	MeasureParams::in, T::in, T::in, T::out, bool::in, bool::out, 
+	size_annotated_args(T)::out) is det <= measure_with_params(T, 
+	MeasureParams).
+
+annotate_args_with_size([], _, _, SoFar, SoFar, Exact, Exact, []).
+annotate_args_with_size([Flag - Arg | FlaggedArgs], Params, Limit,
+		SoFar0, SoFar, Exact0, Exact,
+		[MaybeFlaggedSize | MaybeFlaggedSizes]) :-
+	(
+		Flag = yes(ArgLimit),
+		( compare_measures(SoFar0, Limit) = (>) ->
+			AppliedArgLimit = ArgLimit
+		;
+			AppliedArgLimit = max_measure(ArgLimit,
+				subtract_measures(Limit, SoFar0, Params))
+		),
+		first_pass(Arg, Params, AppliedArgLimit, Size),
+		MaybeFlaggedSize = yes(ArgLimit - Size),
+		extract_size_from_annotation(Size) = ArgSize,
+		SoFar1 = add_measures(SoFar0, ArgSize, Params),
+		(
+			Size = exact(_, _, _, _, _),
+			Exact1 = Exact0
+		;
+			Size = at_least(_, _, _),
+			Exact1 = no
+		)
+	;
+		Flag = no,
+		MaybeFlaggedSize = no,
+		SoFar1 = SoFar0,
+		Exact1 = Exact0
+	),
+	( compare_measures(SoFar1, Limit) = (>) ->
+		Exact2 = no
+	;
+		Exact2 = Exact1
+	),
+	annotate_args_with_size(FlaggedArgs, Params, Limit, SoFar1, SoFar, 
+		Exact2, Exact, MaybeFlaggedSizes).
+
+%------------------------------------------------------------------------------%
+	% Annotates the arguments with zero limit.
+:- pred annotate_args_with_zero_size(assoc_list(maybe(T), univ)::in, T::in,
+	size_annotated_args(T)::out) is det <= measure(T).
+
+annotate_args_with_zero_size([], _, []).
+annotate_args_with_zero_size([Flag - Univ | FlaggedArgs], ZeroMeasure,
+		[FlaggedSize | FlaggedSizes]) :-
+	(
+		Flag = yes(ArgLimit),
+		FlaggedSize = yes(ArgLimit -
+			at_least(Univ, ZeroMeasure, not_deconstructed))
+	;
+		Flag = no,
+		FlaggedSize = no
+	),
+	annotate_args_with_zero_size(FlaggedArgs, ZeroMeasure, FlaggedSizes).
+
+%------------------------------------------------------------------------------%
+
+:- func extract_size_from_annotation(size_annotated_term(T)) = T.
+
+extract_size_from_annotation(exact(_, Size, _, _, _)) = Size.
+extract_size_from_annotation(at_least(_, Size, _)) = Size.
+
+%------------------------------------------------------------------------------%
+
+:- func extract_univ_from_annotation(size_annotated_term(T)) = univ.
+
+extract_univ_from_annotation(exact(Univ, _, _, _, _)) = Univ.
+extract_univ_from_annotation(at_least(Univ, _, _)) = Univ.
+
+%------------------------------------------------------------------------------%
+	% This predicate basically ensures that the arguments that
+	% take up smaller "Space" than their fair share is fully
+	% printed and the rest the Space is shared equally between
+	% the other terms which could take up more than their share.
+	% If a term can be fully printed within the given space,
+	% ("exact" type) then the Term is not altered.
+:- pred second_pass(size_annotated_term(T)::in, MeasureParams::in, T::in,
+	size_annotated_term(T)::out) is det 
+	<= measure_with_params(T, MeasureParams).
+
+second_pass(OldSizeTerm, Params, Limit, NewSizeTerm) :-
+	(
+    		OldSizeTerm = exact(_Univ, _Size, _, _Arity, _MaybeArgs),
+		NewSizeTerm = OldSizeTerm
+	;
+    		OldSizeTerm = at_least(_Univ, _Size, not_deconstructed),
+		NewSizeTerm = OldSizeTerm
+	;
+    		OldSizeTerm = at_least(Univ, _Size, deconstructed(Functor, 
+			Arity,MaybeArgs)),
+		measured_split(Univ, Params, Limit, Arity, yes, FSize, Flag,
+			NewLimit, NewParams),
+		( if Flag = yes(X) then
+	    		ArgLimit = X,
+	    		check_args(NewParams, MaybeArgs, ArgLimit, Passed, 
+				FSize, Used),
+			LeftOver = add_measures(subtract_measures(NewLimit, 
+			  	Used, Params), FSize, Params),
+	    		measured_split(Univ, Params, LeftOver, Arity - Passed, 
+				no, _, Flag2, _, _),
+	    		( if Flag2 = yes(Y) then
+	        		SplitLimit = Y,
+	        		process_args(NewParams, MaybeArgs, ArgLimit, 
+					SplitLimit, NewArgs, NewSize0),
+				NewSize = add_measures(FSize, NewSize0, 
+					NewParams),
+				Result0 = list__map(check_if_exact, NewArgs),
+    				list__remove_adjacent_dups(Result0, Result),
+				( Result = [yes] ->
+					NewSizeTerm = exact(Univ, NewSize, 
+						Functor, Arity, NewArgs) 	
+	        		;
+					NewSizeTerm = at_least(Univ, NewSize, 
+						deconstructed(Functor, Arity, 
+						NewArgs))
+				)
+	    		else
+	        		NewSizeTerm = at_least(Univ, FSize, 
+					not_deconstructed)
+	    		)
+		else
+	    	NewSizeTerm = at_least(Univ, FSize, not_deconstructed)
+		)
+	).
+	
+%------------------------------------------------------------------------------%
+	% Given a list of size annotated terms(ie arguments) and a
+	% Limit, this predicate returns the values "Passed" and 
+	% "Used". Where "Passed" represents the number of terms that
+	% obey the Limit and are fully represented("exact") and "Used"
+	% represents the space that these terms take up.
+:- pred check_args(MeasureParams::in, list(maybe(pair(T, size_annotated_term(T)
+	)))::in, T::in, int::out, T::in, T::out) is det
+	<= measure_with_params(T, MeasureParams).
+
+check_args(_, [], _, 0, Used0, Used0).
+check_args(Params, [HeadArg | Rest], ArgLimit, Passed, Used0, Used) :-
+	if HeadArg = yes(X) then
+		X = _ - STerm,
+		Size = extract_size_from_annotation(STerm), 
+		( if STerm = exact(_, _, _, _, _) then
+	    		( if compare_measures(ArgLimit, Size) = (<) then
+	    			check_args(Params, Rest, ArgLimit, Passed, 
+					Used0, Used)
+	    		else
+	    			Passed = 1 + PassedRest,
+				UsedSofar = add_measures(Used0, Size, Params),
+	    			check_args(Params, Rest, ArgLimit, PassedRest, 
+					UsedSofar, Used)
+	    		)
+		else
+	    		check_args(Params, Rest, ArgLimit, Passed, Used0, Used)
+		)
+    	else
+		check_args(Params, Rest, ArgLimit, Passed, Used0, Used).
+
+%------------------------------------------------------------------------------%
+	% This predicate accepts a list of size annotated terms(paired
+	% with a flag) and returns a list of the same type. This new
+	% list would consist of the same number of terms as the other
+	% but the terms which do not obey the limit or not fully 
+	% represented would be annoted again with a new limit
+	% (SplitLimit). The rest of the terms are left alone.
+:- pred process_args(MeasureParams::in, 
+	size_annotated_args(T)::in, T::in, T::in, size_annotated_args(T)::out, 
+	T::out) is det <= measure_with_params(T, MeasureParams).
+
+process_args(_, [], _, _, [], zero_measure).
+process_args(Params, [HeadArg | Rest], ArgLimit, SplitLimit, 
+		[NewHeadArg | NewRest], SizeOut) :-
+    	( if HeadArg = yes(X) then
+		X = _ - STerm,
+		Size = extract_size_from_annotation(STerm), 
+        	Univ = extract_univ_from_annotation(STerm), 
+		( 
+			STerm = exact(_, _, _, _, _),
+	    		(
+				compare_measures(ArgLimit, Size) = (>)
+			;
+	    			compare_measures(ArgLimit, Size) = (=)
+			)
+		->
+			NewHeadArg = HeadArg
+		;
+			NewHeadArg = yes(pair(SplitLimit, NewSTerm)),
+			annotate_with_size(Univ, Params, SplitLimit, NewSTerm)
+		)
+    	else
+		NewHeadArg = no
+    	),
+    	( NewHeadArg = yes(_ - Term) ->
+		NewSize = extract_size_from_annotation(Term),
+		SizeOut = add_measures(NewSize, RestSize, Params)
+    	;
+		SizeOut = RestSize
+    	),
+    	process_args(Params, Rest, ArgLimit, SplitLimit, NewRest, RestSize).
+
+%------------------------------------------------------------------------------%
+	% checking if an size-annotated arg is an exact type (fully represented)
+:- func check_if_exact(maybe(pair(T, size_annotated_term(T)))) = bool.
+
+check_if_exact(no) = no.
+check_if_exact(yes(_ - Term)) = Result:-
+	(
+		Term = exact(_, _, _, _, _),
+		Result = yes
+	;
+		Term = at_least(_, _, _),
+		Result = no
+	).	
+
+%------------------------------------------------------------------------------%
+	% A function to convert a size annotated term to a 'doc' type,
+	% a type defined in pprint.m.
+:- func to_doc_sized(size_annotated_term(T)) = doc.
+
+to_doc_sized(at_least(Univ, _, not_deconstructed)) = Doc :-
+	deconstruct(univ_value(Univ), Functor, Arity, _Args),
+	Doc = text(Functor) `<>` text("/") `<>` poly(i(Arity)).
+
+to_doc_sized(at_least(_, _, deconstructed(Functor, Arity, MaybeArgs))) = Doc :-
+	Doc = to_doc_sized_2(Functor, Arity, MaybeArgs).
+
+to_doc_sized(exact(_, _, Functor, Arity, MaybeArgs)) = Doc :-
+	Doc = to_doc_sized_2(Functor, Arity, MaybeArgs).
+
+%------------------------------------------------------------------------------%
+	% Assumes that every argument must be on a different line
+	% or all of them should be on the same line.
+:- func to_doc_sized_2(string, int, size_annotated_args(T)) = doc.
+
+to_doc_sized_2(Functor, _Arity, []) = text(Functor).
+
+to_doc_sized_2(Functor, Arity, [HeadArg|Tail]) = Doc :-
+    	Args = list__map(handle_arg, [HeadArg|Tail]),
+    	list__remove_adjacent_dups(Args, NewArgs),
+    	( NewArgs \= [nil] -> 
+        	Doc = text(Functor) `<>` parentheses(group(nest(2, line `<>` 
+			separated(id,comma_space_line, Args))))
+    	;
+        	Doc = text(Functor) `<>` text("/") `<>` poly(i(Arity))
+    	).
+	
+%------------------------------------------------------------------------------%
+
+:- func handle_arg(maybe(pair(T,size_annotated_term(T)))) = doc.
+
+handle_arg(yes(_ - Arg_Term)) = to_doc_sized(Arg_Term). 
+handle_arg(no) = nil.
+
+%------------------------------------------------------------------------------%
+	% A predicate that creates an associated list of Univ and their
+	% individual Limit
+:- pred flag_with(list(univ)::in, maybe(T)::in,
+	assoc_list(maybe(T), univ)::out) is det.
+flag_with([], _, []).
+flag_with([Arg | Args], Flag, [Flag - Arg | FlaggedArgs]) :-
+	flag_with(Args, Flag, FlaggedArgs).
+
+%------------------------------------------------------------------------------%
+	% functor_count is a representation where the size of a term
+	% is measured by the number of function symbols.
+
+:- type functor_count
+	--->	functor_count(int). 	% No of function symbols
+
+:- func add_functor_count(functor_count, functor_count, 
+	no_measure_params) = functor_count.
+
+add_functor_count(functor_count(A), functor_count(B), _) = functor_count(A + B).
+
+:- func subtract_functor_count(functor_count, functor_count, 
+	no_measure_params) = functor_count.
+
+subtract_functor_count(functor_count(A), functor_count(B), _) =
+	functor_count(A - B).
+
+:- func compare_functor_count(functor_count, functor_count) = comparison_result.
+
+compare_functor_count(functor_count(A), functor_count(B)) = R :-
+	compare(R, A, B).
+
+:- func max_functor_count(functor_count, functor_count) = functor_count.
+
+max_functor_count(functor_count(A), functor_count(B)) = functor_count(Max) :-
+	int__max(A, B, Max).
+
+:- func zero_functor_count = functor_count.
+
+zero_functor_count = functor_count(0).
+	
+:- pred functor_count_split(univ::in, no_measure_params::in, functor_count::in,
+	int::in, bool::in, functor_count::out, maybe(functor_count)::out,
+	functor_count::out, no_measure_params::out) is det.
+
+functor_count_split(_, Params, functor_count(Limit), Arity, _, functor_count(1),
+		Flag, functor_count(Limit), Params) :-
+	( Arity = 0 ->
+		Flag = no
+	;
+		( Limit =< (Arity + 1) ->			
+			Flag = no
+		;
+			RoundUp = (Limit + Arity - 1) // Arity,
+			Flag = yes(functor_count(RoundUp))
+		)
+	).
+
+:- instance measure(functor_count) where [
+	func(compare_measures/2) is compare_functor_count,
+	func(max_measure/2) is max_functor_count,
+	func(zero_measure/0) is zero_functor_count
+].
+
+:- instance measure_with_params(functor_count, no_measure_params) where [
+	func(add_measures/3) is add_functor_count,
+	func(subtract_measures/3) is subtract_functor_count,
+	pred(measured_split/9) is functor_count_split
+].
+
+
+%------------------------------------------------------------------------------%
+	% char_count is a representation where the size of a term is
+	% measured by the number of characters.
+
+:- type char_count
+	--->	char_count(int).	% No of characters
+
+:- func add_char_count(char_count, char_count, no_measure_params) = char_count.
+
+add_char_count(char_count(A), char_count(B), _) = char_count(A + B).
+
+:- func subtract_char_count(char_count, char_count, 
+	no_measure_params) = char_count.
+
+subtract_char_count(char_count(A), char_count(B), _) =
+	char_count(A - B).
+
+:- func compare_char_count(char_count, char_count) = comparison_result.
+
+compare_char_count(char_count(A), char_count(B)) = R :-
+	compare(R, A, B).
+
+:- func max_char_count(char_count, char_count) = char_count.
+
+max_char_count(char_count(A), char_count(B)) = char_count(Max) :-
+	int__max(A, B, Max).
+
+:- func zero_char_count = char_count.
+
+zero_char_count = char_count(0).
+
+:- pred char_count_split(univ::in, no_measure_params::in, char_count::in,
+	int::in, bool::in, char_count::out, maybe(char_count)::out,
+	char_count::out, no_measure_params::out) is det.
+
+char_count_split(Univ, Params, char_count(Limit), Arity, Check, 
+		char_count(FunctorSize), Flag, char_count(Limit), Params) :-
+	deconstruct(univ_value(Univ), Functor, _, Args),
+	( Check = yes ->
+		get_arg_length(Args, TotalLength, _)
+	;
+		TotalLength = 0
+	),
+	FunctorSize = string__length(Functor) + 2*(Arity),
+	( Arity = 0 ->
+		Flag = no
+	;
+		( Limit =< (FunctorSize + TotalLength) ->
+			Flag = no
+		;
+			RoundUp = (Limit + Arity - FunctorSize) // Arity,
+			Flag = yes(char_count(RoundUp))
+		)
+	).
+
+:- instance measure(char_count) where [
+        func(compare_measures/2) is compare_char_count,
+        func(max_measure/2) is max_char_count,
+        func(zero_measure/0) is zero_char_count
+].
+
+:- instance measure_with_params(char_count, no_measure_params) where [
+        func(add_measures/3) is add_char_count,
+        func(subtract_measures/3) is subtract_char_count,
+        pred(measured_split/9) is char_count_split
+].
+
+%------------------------------------------------------------------------------%
+	% size_count is representation where the size of a term is
+	% measured by number of lines or number of characters.
+
+:- type size_count
+	--->	line_count(int)		% no of lines
+	;	character_count(int).	% no of characters
+
+:- func add_size_count(size_count, size_count, measure_params) = size_count.
+
+add_size_count(character_count(A), character_count(B), Params) = Result :-
+	Params = measure_params(LineWidth),
+	CharSum = A + B,
+	( CharSum > LineWidth ->
+		Result = line_count(1)
+	;
+		Result = character_count(CharSum)
+	).
+
+add_size_count(character_count(A), line_count(B), _) = Result :-
+	( A > 0 -> 
+		Result = line_count(B + 1)
+	;
+		Result = line_count(B)
+	).
+
+add_size_count(line_count(A), character_count(B), _) = Result :-
+	( B > 0 -> 
+		Result = line_count(A + 1)
+	;
+		Result = line_count(A)
+	).
+
+add_size_count(line_count(A), line_count(B), _) = line_count(A + B).
+
+	% Rounding up the Lines and subtracting works because we assume
+	% that each argument is a different line or they are all on 
+	% the same line. But this requires you to determine which case
+	% likely to happen before hand. For example if a term is to be
+	% on one line, you should do subtract_size_count(character_count(
+	% LineLength), charater_count(arglength)) rather than
+	% subtract_size_count(line_count(1), character_count(arglength)).
+	% The reason that this situation cannot be detected in this code is:
+	% A term can be printed on a single line only if, all of it's 
+	% arguments can be printed on the same line. And you cannot determine
+	% which case is likely to happen in this code using the information 
+	% it has. Therefore size_count_split determines which case is true 
+	% (and changes the limit accordingly).
+:- func subtract_size_count(size_count, size_count,measure_params) = size_count.
+
+subtract_size_count(character_count(A), character_count(B), _) = Result :-
+	CharDiff = A - B,
+	( CharDiff < 0 ->
+		Result = character_count(0)
+	;
+		Result = character_count(CharDiff)
+	).
+
+subtract_size_count(character_count(A), line_count(B), _) = Result :-
+	( B = 0 -> 
+		Result = character_count(A)
+	;
+		Result = character_count(0)
+	).
+
+subtract_size_count(line_count(A), character_count(B), _) = Result :-
+	( B = 0 -> 
+		Result = line_count(A)
+	;
+		( A - 1 >= 0 ->
+			Result = line_count(A - 1)
+		;
+			Result = line_count(0)
+		)
+	).
+
+subtract_size_count(line_count(A), line_count(B), _) = Result :-
+	( A - B >= 0 ->
+		Result = line_count(A - B)
+	;
+		Result = line_count(0)
+	).
+
+:- func compare_size_count(size_count, size_count) = comparison_result.
+
+compare_size_count(character_count(C1), character_count(C2)) = R :-
+	compare(R, C1, C2).
+
+compare_size_count(character_count(_), line_count(_)) = (<).
+
+compare_size_count(line_count(_), character_count(_)) = (>).
+
+compare_size_count(line_count(L1), line_count(L2)) = R :-
+	compare(R, L1, L2).
+
+:- func max_size_count(size_count, size_count) = size_count.
+
+max_size_count(A, B) = Max :-
+	( compare_size_count(A, B) = (>) ->
+		Max = A
+	;
+		Max = B
+	).
+
+:- func zero_size_count = size_count.
+
+zero_size_count = character_count(0).
+
+	% We assume that all arguments have to be on separate lines, or 
+	% the whole term should be printed on a single line.
+:- pred size_count_split(univ::in, measure_params::in, size_count::in,
+	int::in, bool::in, size_count::out, maybe(size_count)::out,
+	size_count::out, measure_params::out) is det.
+
+size_count_split(Univ, Params, Limit, Arity, Check, FunctorSize, 
+		Flag, NewLimit, NewParams) :-
+	% LineWidth is length of the line in which the functor is printed.
+	Params = measure_params(LineWidth),
+    	deconstruct(univ_value(Univ), Functor, ActualArity, Args),
+    	FSize = string__length(Functor) + 2 * (ActualArity),
+    	( Check = yes ->
+    		get_arg_length(Args, TotalLength, MaxArgLength),
+		int__max(MaxArgLength, (string__length(Functor) + 1), MaxLength)
+    	;
+    		TotalLength = 0,
+		MaxLength = 0
+    	), 
+    	( 
+		Arity = 0 
+	->
+		Flag = no,
+    		FunctorSize = character_count(FSize),
+		NewLimit = Limit,
+		NewParams = Params
+    	;
+		(
+			Limit = line_count(LineLimit),
+			% we need one line for the functor and atleast 
+			% one line for each argument
+			LineLimit >= (Arity + 1),
+			% linewidth is decreased by two characters to account 
+			% for indentation
+			(LineWidth - 2) >= MaxLength
+		->
+			Line = (LineLimit - 1) // Arity,
+			Flag = yes(line_count(Line)),
+			FunctorSize = line_count(1),
+	    		NewLimit = Limit,
+	    		NewParams = measure_params(LineWidth - 2)
+		;
+			Limit = line_count(LineLimit),
+			LineLimit > 0,
+			% Since every term is part of a bigger term (in this 
+			% context anyway) it will have a comma, space and a 
+			% newline at the end of it (Hence the "- 3").
+			LineWidth - 3 >= (FSize + TotalLength) 
+		->
+	    		% "Arity - 1" is for rounding up.
+			Char = (LineWidth - 3 - FSize + Arity - 1) // Arity ,
+	    		Flag = yes(character_count(Char)),
+	    		FunctorSize = character_count(FSize),
+	    		NewLimit = character_count(LineWidth - 3),
+	    		NewParams = Params
+   		;
+			Limit = character_count(CharLimit),
+			CharLimit >= (FSize + TotalLength)
+		->
+	   		Char = (CharLimit - FSize + Arity - 1) // Arity,
+	   		Flag = yes(character_count(Char)),
+	   		FunctorSize = character_count(FSize),
+	   		NewLimit = Limit,
+	   		NewParams = Params
+		;
+	   		Flag = no,
+			% If a term is not deconstructed, it is printed as
+			% "functor/Arity". The "+ 2" accounts for that.
+	   		FunctorSize = 
+				character_count(string__length(Functor) + 2),
+	   		NewLimit = Limit, 
+	   		NewParams = Params
+		)
+	).
+
+:- instance measure(size_count) where [
+	func(compare_measures/2) is compare_size_count,
+	func(max_measure/2) is max_size_count,
+	func(zero_measure/0) is zero_size_count
+].
+
+:- instance measure_with_params(size_count, measure_params) where [
+	func(add_measures/3) is add_size_count,
+	func(subtract_measures/3) is subtract_size_count,
+	pred(measured_split/9) is size_count_split
+].
+
+%------------------------------------------------------------------------------%
+	% This predicate determines how many characters it will take
+	% to print the functors of the arguments. Also determines the
+	% length of biggest functor.
+:- pred get_arg_length(list(univ)::in, int::out, int::out) is det.
+
+get_arg_length([], 0, 0).
+get_arg_length([HeadUniv | Rest], TotalLength, MaxLength) :-
+	deconstruct(univ_value(HeadUniv), Functor, Arity, _),
+	( Rest = [] ->
+		Correction = 2
+	;
+		Correction = 3
+	),
+	( Arity = 0 -> 
+		Length = string__length(Functor)
+	;
+		% 2 is added because if a term has arguments then the
+		% shortest way to print it is "functor/Arity"
+		% Assuming Arity is a single digit
+		Length = string__length(Functor) + 2
+	),
+	TotalLength = Length + RestTotalLength,
+	int__max((Length + Correction), RestMaxLength, MaxLength),
+	get_arg_length(Rest, RestTotalLength, RestMaxLength).
+
+%------------------------------------------------------------------------------%
Index: tests/debugger/browse_pretty.exp
===================================================================
RCS file: /home/mercury1/repository/tests/debugger/browse_pretty.exp,v
retrieving revision 1.3
diff -u -r1.3 browse_pretty.exp
--- tests/debugger/browse_pretty.exp	2000/10/27 08:38:54	1.3
+++ tests/debugger/browse_pretty.exp	2001/02/16 01:13:52
@@ -6,7 +6,7 @@
 mdb> print *
        HeadVar__1             	big(big(big(small, ./2, small), .(1, ./2), small), .(1, .(2, ./2)), big(big(small, ./2, big/3), ./2, small))
 mdb> browse 1
-browser> set format pretty
+browser> set format raw_pretty
 browser> set depth 10
 browser> ls
 big(
@@ -143,6 +143,41 @@
   big(big(small, ./2, small), .(1, ./2), small), 
   .(1, .(2, ./2)), 
   big(big(small, ./2, big/3), .(1, ./2), small))
+browser> set format pretty
+browser> set lines 4
+browser> set width 40
+browser> ls
+big(
+  big(big/3, .(1, ./2), small), 
+  .(1, .(2, .(3, []))), 
+  big(big/3, .(1, ./2), small))
+browser> set width 80
+browser> ls
+big(
+  big(big(small, .(1, []), small), .(1, .(2, [])), small), 
+  .(1, .(2, .(3, []))), 
+  big(big(small, ./2, big/3), .(1, .(2, .(3, ./2))), small))
+browser> set lines 12
+browser> set width 40
+browser> ls
+big(
+  big(
+    big(small, .(1, []), small), 
+    .(1, .(2, [])), 
+    small), 
+  .(1, .(2, .(3, []))), 
+  big(
+    big(small, .(1, ./2), big/3), 
+    .(1, .(2, .(3, .(4, .(5, ./2))))), 
+    small))
+browser> cd 3
+browser> set lines 4
+browser> set width 30
+browser> ls
+big(
+  big(small, ./2, big/3), 
+  .(1, .(2, .(3, ./2))), 
+  small)
 browser> quit
 mdb> continue
 big(big(big(small, [1], small), [1, 2], small), [1, 2, 3], big(big(small, [1, 2, 3, 4], big(small, [1, 2, 3, 4, 5], small)), [1, 2, 3, 4, 5, 6], small)).
Index: tests/debugger/browse_pretty.inp
===================================================================
RCS file: /home/mercury1/repository/tests/debugger/browse_pretty.inp,v
retrieving revision 1.2
diff -u -r1.2 browse_pretty.inp
--- tests/debugger/browse_pretty.inp	2000/10/27 08:38:54	1.2
+++ tests/debugger/browse_pretty.inp	2001/02/16 01:13:52
@@ -2,7 +2,7 @@
 goto 3
 print *
 browse 1
-set format pretty
+set format raw_pretty
 set depth 10
 ls
 set width 131
@@ -13,6 +13,19 @@
 ls
 set width 79
 set depth 3
+ls
+set format pretty
+set lines 4
+set width 40
+ls
+set width 80
+ls
+set lines 12
+set width 40
+ls
+cd 3
+set lines 4
+set width 30
 ls
 quit
 continue
Index: trace/mercury_trace_browse.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_browse.c,v
retrieving revision 1.18
diff -u -r1.18 mercury_trace_browse.c
--- trace/mercury_trace_browse.c	2000/11/23 02:01:07	1.18
+++ trace/mercury_trace_browse.c	2001/02/16 01:13:53
@@ -1,5 +1,5 @@
 /*
-** Copyright (C) 1998-2000 The University of Melbourne.
+** Copyright (C) 1998-2001 The University of Melbourne.
 ** This file may only be copied under the terms of the GNU Library General
 ** Public License - see the file COPYING.LIB in the Mercury distribution.
 */
@@ -142,8 +142,8 @@
 
 bool
 MR_trace_set_browser_param(MR_Bool print, MR_Bool browse, MR_Bool print_all,
-		MR_Bool flat, MR_Bool pretty, MR_Bool verbose,
-		const char *param, const char *value)
+		MR_Bool flat, MR_Bool raw_pretty, MR_Bool verbose, 
+		MR_Bool pretty, const char *param, const char *value)
 {
 	int			depth, size, width, lines;
 	MR_Browse_Format	new_format;
@@ -163,7 +163,7 @@
 	{
 		MR_TRACE_CALL_MERCURY(
 			ML_BROWSE_set_param_depth(print, browse, print_all,
-				flat, pretty, verbose, depth,
+				flat, raw_pretty, verbose, pretty, depth,
 				MR_trace_browser_persistent_state,
 				&MR_trace_browser_persistent_state);
 		);
@@ -172,7 +172,7 @@
 	{
 		MR_TRACE_CALL_MERCURY(
 			ML_BROWSE_set_param_size(print, browse, print_all,
-				flat, pretty, verbose, size,
+				flat, raw_pretty, verbose, pretty, size,
 				MR_trace_browser_persistent_state,
 				&MR_trace_browser_persistent_state);
 		);
@@ -181,7 +181,7 @@
 	{
 		MR_TRACE_CALL_MERCURY(
 			ML_BROWSE_set_param_width(print, browse, print_all,
-				flat, pretty, verbose, width,
+				flat, raw_pretty, verbose, pretty, width,
 				MR_trace_browser_persistent_state,
 				&MR_trace_browser_persistent_state);
 		);
@@ -190,7 +190,7 @@
 	{
 		MR_TRACE_CALL_MERCURY(
 			ML_BROWSE_set_param_lines(print, browse, print_all,
-				flat, pretty, verbose, lines,
+				flat, raw_pretty, verbose, pretty, lines,
 				MR_trace_browser_persistent_state,
 				&MR_trace_browser_persistent_state);
 		);
@@ -214,14 +214,16 @@
 	if (streq(str, "flat")) {
 		*format = MR_BROWSE_FORMAT_FLAT;
 		return TRUE;
-	} else if (streq(str, "pretty")) {
-		*format = MR_BROWSE_FORMAT_PRETTY;
+	} else if (streq(str, "raw_pretty")) {
+		*format = MR_BROWSE_FORMAT_RAW_PRETTY;
 		return TRUE;
 	} else if (streq(str, "verbose")) {
 		*format = MR_BROWSE_FORMAT_VERBOSE;
 		return TRUE;
+	} else if (streq(str, "pretty")) {
+		*format = MR_BROWSE_FORMAT_PRETTY;
+		return TRUE;
 	}
-
 	return FALSE;
 }
 
Index: trace/mercury_trace_browse.h
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_browse.h,v
retrieving revision 1.10
diff -u -r1.10 mercury_trace_browse.h
--- trace/mercury_trace_browse.h	2001/02/13 08:28:27	1.10
+++ trace/mercury_trace_browse.h	2001/02/16 01:13:53
@@ -30,8 +30,9 @@
 
 typedef enum {
 	MR_BROWSE_FORMAT_FLAT,
-	MR_BROWSE_FORMAT_PRETTY,
-	MR_BROWSE_FORMAT_VERBOSE
+	MR_BROWSE_FORMAT_RAW_PRETTY,
+	MR_BROWSE_FORMAT_VERBOSE,
+	MR_BROWSE_FORMAT_PRETTY
 } MR_Browse_Format;
 
 /*
@@ -59,8 +60,9 @@
 ** Set browser parameters.
 */
 extern	bool	MR_trace_set_browser_param(MR_Bool print, MR_Bool browse,
-			MR_Bool print_all, MR_Bool flat, MR_Bool pretty,
-			MR_Bool verbose, const char *param, const char *value);
+			MR_Bool print_all, MR_Bool flat, MR_Bool raw_pretty,
+			MR_Bool verbose, MR_Bool pretty, const char *param, 
+			const char *value);
 
 /*
 ** Invoke an interactive query.
Index: trace/mercury_trace_internal.c
===================================================================
RCS file: /home/mercury1/repository/mercury/trace/mercury_trace_internal.c,v
retrieving revision 1.97
diff -u -r1.97 mercury_trace_internal.c
--- trace/mercury_trace_internal.c	2001/01/18 03:01:48	1.97
+++ trace/mercury_trace_internal.c	2001/02/16 01:13:53
@@ -178,9 +178,10 @@
 			const char *item);
 static	bool	MR_trace_options_param_set(MR_Bool *print_set,
 			MR_Bool *browse_set, MR_Bool *print_all_set,
-			MR_Bool *flat_format, MR_Bool *pretty_format,
-			MR_Bool *verbose_format, char ***words,
-			int *word_count, const char *cat, const char *item);
+			MR_Bool *flat_format, MR_Bool *raw_pretty_format,
+			MR_Bool *verbose_format, MR_Bool *pretty_format, 
+			char ***words, int *word_count, const char *cat, 
+			const char *item);
 static	void	MR_trace_usage(const char *cat, const char *item);
 static	void	MR_trace_do_noop(void);
 
@@ -1093,21 +1094,23 @@
 		MR_Bool			browse_set;
 		MR_Bool			print_all_set;
 		MR_Bool			flat_format;
-		MR_Bool			pretty_format;
+		MR_Bool			raw_pretty_format;
 		MR_Bool			verbose_format;
+		MR_Bool			pretty_format;
 
 		if (! MR_trace_options_param_set(&print_set, &browse_set,
-				&print_all_set, &flat_format, &pretty_format,
-				&verbose_format, &words, &word_count,
-				"browsing", "set"))
+				&print_all_set, &flat_format, 
+				&raw_pretty_format, &verbose_format, 
+				&pretty_format, &words, &word_count, "browsing",
+				"set"))
 		{
 			; /* the usage message has already been printed */
 		}
 		else if (word_count != 3 ||
 				! MR_trace_set_browser_param(print_set,
 					browse_set, print_all_set, flat_format,
-					pretty_format, verbose_format,
-					words[1], words[2]))
+					raw_pretty_format, verbose_format, 
+					pretty_format, words[1], words[2]))
 		{
 			MR_trace_usage("browsing", "set");
 		}
@@ -2407,8 +2410,9 @@
 static struct MR_option MR_trace_format_opts[] = 
 {
 	{ "flat",	FALSE,	NULL,	'f' },
-	{ "pretty",	FALSE,	NULL,	'p' },
+	{ "raw_pretty",	FALSE,	NULL,	'r' },
 	{ "verbose",	FALSE,	NULL,	'v' },
+	{ "pretty",	FALSE,	NULL,	'p' },
 	{ NULL,		FALSE,	NULL,	0 }
 };
 
@@ -2420,7 +2424,7 @@
 
 	*format = MR_BROWSE_DEFAULT_FORMAT;
 	MR_optind = 0;
-	while ((c = MR_getopt_long(*word_count, *words, "fpv",
+	while ((c = MR_getopt_long(*word_count, *words, "frvp",
 			MR_trace_format_opts, NULL)) != EOF)
 	{
 		switch (c) {
@@ -2429,14 +2433,18 @@
 				*format = MR_BROWSE_FORMAT_FLAT;
 				break;
 
-			case 'p':
-				*format = MR_BROWSE_FORMAT_PRETTY;
+			case 'r':
+				*format = MR_BROWSE_FORMAT_RAW_PRETTY;
 				break;
 
 			case 'v':
 				*format = MR_BROWSE_FORMAT_VERBOSE;
 				break;
 
+			case 'p':
+				*format = MR_BROWSE_FORMAT_PRETTY;
+				break;
+
 			default:
 				MR_trace_usage(cat, item);
 				return FALSE;
@@ -2451,8 +2459,9 @@
 static struct MR_option MR_trace_param_set_opts[] = 
 {
 	{ "flat",	FALSE,	NULL,	'f' },
-	{ "pretty",	FALSE,	NULL,	'p' },
+	{ "raw_pretty",	FALSE,	NULL,	'r' },
 	{ "verbose",	FALSE,	NULL,	'v' },
+	{ "pretty",	FALSE,	NULL,	'p' },	
 	{ "print",	FALSE,	NULL,	'P' },
 	{ "browse",	FALSE,	NULL,	'B' },
 	{ "print-all",	FALSE,	NULL,	'A' },
@@ -2461,9 +2470,10 @@
 
 static bool
 MR_trace_options_param_set(MR_Bool *print_set, MR_Bool *browse_set,
-	MR_Bool *print_all_set, MR_Bool *flat_format, MR_Bool *pretty_format,
-	MR_Bool *verbose_format, char ***words, int *word_count,
-	const char *cat, const char *item)
+	MR_Bool *print_all_set, MR_Bool *flat_format, 
+	MR_Bool *raw_pretty_format, MR_Bool *verbose_format, 
+	MR_Bool *pretty_format, char ***words, int *word_count, const char *cat,
+	const char *item)
 {
 	int	c;
 
@@ -2471,11 +2481,12 @@
 	*browse_set = FALSE;
 	*print_all_set = FALSE;
 	*flat_format = FALSE;
-	*pretty_format = FALSE;
+	*raw_pretty_format = FALSE;
 	*verbose_format = FALSE;
+	*pretty_format = FALSE;
 
 	MR_optind = 0;
-	while ((c = MR_getopt_long(*word_count, *words, "PBAfpv",
+	while ((c = MR_getopt_long(*word_count, *words, "PBAfrvp",
 			MR_trace_param_set_opts, NULL)) != EOF)
 	{
 		switch (c) {
@@ -2484,12 +2495,16 @@
 				*flat_format = TRUE;
 				break;
 
-			case 'p':
-				*pretty_format = TRUE;
+			case 'r':
+				*raw_pretty_format = TRUE;
 				break;
 
 			case 'v':
 				*verbose_format = TRUE;
+				break;
+
+			case 'p':
+				*pretty_format = TRUE;
 				break;
 
 			case 'P':


--------------------------------------------------------------------------
mercury-developers mailing list
Post messages to:       mercury-developers at cs.mu.oz.au
Administrative Queries: owner-mercury-developers at cs.mu.oz.au
Subscriptions:          mercury-developers-request at cs.mu.oz.au
--------------------------------------------------------------------------



More information about the developers mailing list