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

Sarvamanan THURAIRATNAM sthur at students.cs.mu.oz.au
Wed Feb 21 22:06:11 AEDT 2001


Estimated hours taken : 200

For Fergus or Mark 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. Also you could specify which 
arguments should be printed for terms that are of certain types. 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:
tests/debugger/browser_test.inp:
        Included test cases for the new pretty printing format.

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

Here is the Full Diff (I couldn't send both of these
diff's at the same time as there is a 100K limit). 

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%       The Full Diff         %%%%%%%%		
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


? make_all.log
? make_install.log
? browser/browser_info.dv
? compiler/code_gen.int.tmp
? scripts/canonical_grade
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/21 01:18:57
@@ -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.
 
@@ -275,6 +275,17 @@
 		write_path(Debugger, Info0 ^ dirs),
 		nl_debugger(Debugger),
 		{ Info = Info0 }
+	; { Command = add_to_database(String, List) } ->
+		{ State0 = Info0 ^ state },
+		{ browser_info__get_database(State0, Database) },
+		{ sized_pretty__adding_to_database(String, List, Database, 
+				NewDatabase) },
+		{ browser_info__set_database(State0, NewDatabase, State) },
+		{ Info = Info0 ^ state := State }
+	; { Command = clear_database } ->
+		{ State0 = Info0 ^ state },
+		{ browser_info__set_database(State0, [], State) },
+		{ Info = Info0 ^ state := State }
 	;	
 		write_string_debugger(Debugger,
 				"command not yet implemented\n"),
@@ -294,8 +305,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 +328,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 +354,15 @@
 			{ 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 },
+			{ browser_info__get_database(Info ^ state, Database) },
+			portray_pretty(Debugger, SubUniv, Params, Database)
 		)
 	;
 		write_string_debugger(Debugger, "error: no such subterm")
@@ -393,14 +409,24 @@
 			Params ^ width, Params ^ lines, 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.
+:- pred portray_raw_pretty(debugger, univ, format_params, io__state, io__state).
+:- mode portray_raw_pretty(in, in, in, di, uo) is det.
 
-portray_pretty(Debugger, Univ, Params) -->
-	{ term_to_string_pretty(Univ, Params ^ width, Params ^ depth, Str) },
+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, database, io__state, 
+		io__state).
+:- mode portray_pretty(in, in, in, in, di, uo) is det.
+
+portray_pretty(Debugger, Univ, Params, Database) -->
+	{ sized_pretty__univ_to_string_line(Univ, Params ^ width, 
+			Params ^ lines, Database, 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 +535,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 +892,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/21 01:18:57
@@ -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.
 %---------------------------------------------------------------------------%
@@ -14,6 +14,7 @@
 
 :- interface.
 :- import_module bool, list, std_util.
+:- import_module mdb__sized_pretty.
 
 	% The non-persistent browser information.  A new one of these is
 	% created every time the browser is called, based on the contents
@@ -54,8 +55,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(
@@ -92,6 +97,16 @@
 		portray_format, format_params).
 :- mode browser_info__get_format_params(in, in, in, out) is det.
 
+	% given the persistent state, retrieves the database.
+:- pred browser_info__get_database(browser_persistent_state, database).
+:- mode browser_info__get_database(in, out) is det.
+
+	% given the persistent state and a database this predicate gives back 
+	% a new persistent state that has the new database stored in it.
+:- pred browser_info__set_database(browser_persistent_state, database, 
+		browser_persistent_state).
+:- mode browser_info__set_database(in, in, out) is det.
+
 %---------------------------------------------------------------------------%
 
 	% An abstract data type that holds persistent browser settings.
@@ -110,8 +125,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 +143,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,8 +188,17 @@
 	%
 	% 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)).
 
+:- pragma export(browser_info__get_database(in, out),
+		"ML_BROWSE_get_database").
+browser_info__get_database(State, State ^ type_information).
+
+:- pragma export(browser_info__set_database(in, in, out),
+		"ML_BROWSE_set_database").
+browser_info__set_database(State0, Database, State) :-
+	State = State0 ^ type_information := Database.
+
 %---------------------------------------------------------------------------%
 
 browser_info__init(Term, MaybeFormat, State, Info) :-
@@ -204,15 +229,17 @@
 	--->	browser_persistent_state(
 			print_params		:: caller_params,
 			browse_params		:: caller_params,
-			print_all_params	:: caller_params
+			print_all_params	:: caller_params,
+			type_information	:: database
 		).
 
 :- type caller_params
 	--->	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
@@ -234,7 +261,7 @@
 	%		term will be shown.
 	%
 browser_info__init_persistent_state(State) :-
-	State = browser_persistent_state(Print, Browse, PrintAll),
+	State = browser_persistent_state(Print, Browse, PrintAll, []),
 	caller_type_print_defaults(Print),
 	caller_type_browse_defaults(Browse),
 	caller_type_print_all_defaults(PrintAll).
@@ -244,39 +271,45 @@
 
 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).
+	State = browser_persistent_state(PParams, BParams, AParams,
+			State0 ^ type_information).
 
 :- pred default_all_yes(bool, bool, bool, bool, bool, bool).
 :- mode default_all_yes(in, in, in, out, out, out) is det.
@@ -299,25 +332,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 +403,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/21 01:18:57
@@ -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/21 01:18:57
@@ -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]
@@ -79,6 +80,8 @@
 	;	print
 	;	display
 	;	write
+	;	add_to_database(string, list(int))
+	;	clear_database
 	;	unknown
 	.
 
@@ -166,7 +169,7 @@
 	; char__is_digit(C) ->
 		dig_to_int(C, N),
 		lexer_num(N, Cs, Toks)
-	; char__is_alpha(C) ->
+	; is_alpha_or_underscore_or_colon(C) ->
 		lexer_name(C, Cs, Toks)
 	; char__is_whitespace(C) ->
 		lexer(Cs, Toks)
@@ -217,12 +220,23 @@
 :- 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(is_alpha_or_underscore_or_colon, Cs, Letters, Rest),
 	string__from_char_list([C | Letters], Name),
 	lexer(Rest, Toks2),
 	Toks = [name(Name) | Toks2].
-	
 
+
+	% True iff the character is a letter or an underscore or a colon.
+:- pred is_alpha_or_underscore_or_colon(char).
+:- mode is_alpha_or_underscore_or_colon(in) is semidet.
+is_alpha_or_underscore_or_colon(Char) :-
+	( Char = (':') ->
+		true
+	;
+		char__is_alpha_or_underscore(Char)
+	).
+
+
 :- pred parse(list(token), command).
 :- mode parse(in, out) is semidet.
 parse(Toks, Comm) :-
@@ -270,6 +284,13 @@
 	; (Tok = name("print") ; Tok = name("p")) ->
 		Toks = [],
 		Comm = print
+	; Tok = name("add_to_database") ->
+		Toks = [name(Typename) | Rest],
+		parse_ints(Rest, List),
+		Comm = add_to_database(Typename, List)
+	; Tok = name("clear_database") ->
+		Toks = [],
+		Comm = clear_database
 	;
 		Tok = (<),
 		Toks = [num(Depth)],
@@ -331,17 +352,26 @@
 		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
 	).
 	
 
+:- pred parse_ints(list(token), list(int)).
+:- mode parse_ints(in, out) is semidet.
+parse_ints([], []).
+parse_ints([num(Head) | Toks], List) :-
+	List = [Head | Rest],
+	parse_ints(Toks, Rest).
+
 %---------------------------------------------------------------------------%
 
 :- pred show_command(command, io__state, io__state).
@@ -378,6 +408,15 @@
 	io__write_string("write\n").
 show_command(unknown) -->
 	io__write_string("unknown\n").
+show_command(add_to_database(String, List)) -->
+	io__write_string("add_to_database "),
+	io__write_string(String),
+	io__write_string(" "),
+	io__write(List),
+	nl.
+
+show_command(clear_database) -->
+	io__write_string("clear_database\n").
 
 :- pred show_path(path, io__state, io__state).
 :- mode show_path(in, di, uo) is det.
@@ -426,9 +465,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	Wed Feb 21 12:18:57 2001
@@ -0,0 +1,1053 @@
+%---------------------------------------------------------------------------%
+% 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, Database, 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
+%		      Database	: A database that has information about types
+%				  specified by the user.
+%		      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 = 75, Lines(s) = 1, Database = ["test:big" - [1, 2]] 
+%
+% big(big(big(small, "Level 3", ), "Level 2", ), "Level 1", )
+%
+%------------------------------------------------------------------------------%
+% 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, list.
+
+	% The idea of using a database is to be able specify that for
+	% a term of specific type only print the arguments specified by
+	% the user.
+:- type database == list(entry).	% A database that stores information
+					% about types.
+
+:- type entry
+	---> 	entry(
+			string,		% specifies the type. If it's a user
+					% defined type then "<modulename>:type"
+			list(int)	% specifies the arguments to print
+		).
+
+	% 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, database::in, 
+	string::out) is det.
+
+	% adding information about a type to the database.
+:- pred adding_to_database(string::in, list(int)::in, database::in, 
+	database::out) is det.
+
+%------------------------------------------------------------------------------%
+
+:- implementation.
+
+:- import_module 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
+			size_annotated_args(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), a 
+		% list that specifies which aruments to print (if a list is not
+		% provided we print all the arguments), 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, maybe(list(int)) ::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, Database, 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, Database, 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, database::in,
+	size_annotated_term(T)::out) is det
+	<= measure_with_params(T, MeasureParams).
+
+annotate_with_size(Univ, Params, Limit, Database, Size2) :-
+	first_pass(Univ, Params, Limit, Database, Size1),
+	second_pass(Size1, Params, Limit, Database, Size2).
+
+%------------------------------------------------------------------------------%
+	
+:- pred first_pass(univ::in, MeasureParams::in, T::in, database::in,
+	size_annotated_term(T)::out) is det
+	<= measure_with_params(T, MeasureParams).
+
+first_pass(Univ, Params, Limit, Database, Size) :-
+	deconstruct(univ_value(Univ), Functor, Arity, Args),
+	MaybeList = search_database(Database, type_name(univ_type(Univ))),
+	measured_split(Univ, Params, Limit, Arity, yes, MaybeList, 
+		FunctorSize, Flag, NewLimit, NewParams),
+	flag_with(Args, Flag, MaybeList, 1, FlaggedUnivArgs),
+	( (Arity \= 0, Flag = no) ->
+		Exact0 = no
+	;
+		Exact0 = yes
+	),
+        annotate_args_with_size(FlaggedUnivArgs, NewParams, Database, 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, database::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, Database, 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, Database, 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, Database, 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.
+	% Also the database is searched for the type of the term and if
+	% found the "list specifying the arguments to be printed" is passed
+	% to measured_split/10.
+:- pred second_pass(size_annotated_term(T)::in, MeasureParams::in, T::in,
+	database::in, size_annotated_term(T)::out) is det 
+	<= measure_with_params(T, MeasureParams).
+
+second_pass(OldSizeTerm, Params, Limit, Database, 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)),
+		MaybeList = search_database(Database, 
+			type_name(univ_type(Univ))),
+		measured_split(Univ, Params, Limit, Arity, yes, MaybeList, 
+			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, MaybeList, _, Flag2, _, _),
+	    		( if Flag2 = yes(Y) then
+	        		SplitLimit = Y,
+	        		process_args(NewParams, MaybeArgs, ArgLimit, 
+					SplitLimit, Database, 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, size_annotated_args(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, database::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, Database,
+		[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, Database, 
+				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, Database, 
+		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, maybe(list(int))::in, int::in, 
+	assoc_list(maybe(T), univ)::out) is det.
+
+flag_with([], _, _, _, []).
+flag_with([Arg | Args], Flag, no, Index, [Flag - Arg | FlaggedArgs]) :-
+	flag_with(Args, Flag, no, Index + 1, FlaggedArgs).
+
+flag_with([Arg | Args], Flag, yes(List), Index, [Head | FlaggedArgs]) :-
+	( list__member(Index, List) ->
+		Head = Flag - Arg
+	;
+		Head = no - Arg
+	),
+	flag_with(Args, Flag, yes(List), Index + 1, FlaggedArgs).
+
+%------------------------------------------------------------------------------%
+
+:- pragma export(sized_pretty__adding_to_database(in, in, in, out),
+	"SIZED_PRETTY_adding_to_database").
+
+adding_to_database(String, Arguments, [], DataBase) :-
+	list__remove_dups(Arguments, NoDups),
+	DataBase = [entry(String, NoDups)].
+
+	% if the type is already there modify it. Otherwise add the new
+	% entry to the end
+adding_to_database(String, Arguments, [Head | Rest], DataBase) :-
+	( Head = entry(String, _) ->
+		list__remove_dups(Arguments, NoDups),
+		DataBase = [entry(String, NoDups) | Rest]
+	;
+		DataBase = [Head | DataBase0],
+		adding_to_database(String, Arguments, Rest, DataBase0)
+	).
+
+%------------------------------------------------------------------------------%
+	% converts a database to a string that is equivalent to the commands
+	% that would be needed to create the database from the "mdb> prompt".
+:- pragma export(sized_pretty__database_to_string(in, out),
+	"SIZED_PRETTY_database_to_string").
+
+:- pred database_to_string(database::in, string::out) is det.
+
+database_to_string([], "").
+
+database_to_string([entry(Type, NumList) | Rest], StringOut) :-
+	list__map(string__int_to_string, NumList, StringList),
+	string__append("add_to_database ", Type, StringTemp),
+	string__append(StringTemp, " ", StringTemp2),
+	string__append(StringTemp2, string_list_to_string(StringList), String),
+	database_to_string(Rest, StringRest),
+	string__append(String, "\n", StringTemp3),
+	string__append(StringTemp3, StringRest, StringOut).
+
+:- func string_list_to_string(list(string)) = string.
+
+string_list_to_string([]) = "".
+
+string_list_to_string([Head | Rest]) = String :-
+	string__append(Head, " ", StringTemp),
+	string__append(StringTemp, string_list_to_string(Rest), String).
+%------------------------------------------------------------------------------%
+	% searching the database for the type.
+:- func search_database(database, string) = maybe(list(int)).
+
+search_database([], _) = no. 
+
+search_database([entry(String, Arguments) | Rest], KeyString) = Result :-
+	( String = KeyString ->
+		Result = yes(Arguments)
+	;
+		Result = search_database(Rest, KeyString)
+	).
+
+%------------------------------------------------------------------------------%
+	% 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, maybe(list(int))::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, _, MaybeList, 
+		functor_count(1), Flag, functor_count(Limit), Params) :-
+	( MaybeList = yes(List) ->
+		NewArity = list__length(list__filter(>=(Arity), List))
+	;
+		NewArity = Arity
+	),
+	( NewArity = 0 ->
+		Flag = no
+	;
+		( Limit =< (NewArity + 1) ->			
+			Flag = no
+		;
+			RoundUp = (Limit + NewArity - 1) // NewArity,
+			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/10) 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, maybe(list(int))::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, MaybeList,
+		char_count(FunctorSize), Flag, char_count(Limit), Params) :-
+	deconstruct(univ_value(Univ), Functor, _, Args),
+	( MaybeList = yes(List) ->
+		NewArity = list__length(list__filter(>=(Arity), List))
+	;
+		NewArity = Arity
+	),
+	( Check = yes ->
+		get_arg_length(Args, MaybeList, 1, TotalLength, _)
+	;
+		TotalLength = 0
+	),
+	FunctorSize = string__length(Functor) + 2*(Arity),
+	( NewArity = 0 ->
+		Flag = no
+	;
+		( Limit =< (FunctorSize + TotalLength) ->
+			Flag = no
+		;
+			RoundUp = (Limit + NewArity - FunctorSize) // NewArity,
+			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/10) 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, maybe(list(int))::in, size_count::out, 
+	maybe(size_count)::out, size_count::out, measure_params::out) is det.
+
+size_count_split(Univ, Params, Limit, Arity, Check, MaybeList, 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),
+	( MaybeList = yes(List) ->
+		NewArity = list__length(list__filter(>=(Arity), List))
+	;
+		NewArity = Arity
+	),
+	NotPrinted = ActualArity - NewArity,
+    	( Check = yes ->
+    		get_arg_length(Args, MaybeList, 1, TotalLength, MaxArgLength),
+		int__max(MaxArgLength, (string__length(Functor) + 1), MaxLength)
+    	;
+    		TotalLength = 0,
+		MaxLength = 0
+    	), 
+    	( 
+		NewArity = 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 >= (NewArity + 1 + NotPrinted),
+			% linewidth is decreased by two characters to account 
+			% for indentation
+			(LineWidth - 2) >= MaxLength
+		->
+			Line = (LineLimit - 1 - NotPrinted) // NewArity,
+			Flag = yes(line_count(Line)),
+			FunctorSize = line_count(1 + NotPrinted),
+	    		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 + NewArity - 1) // 
+				NewArity ,
+	    		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 + NewArity - 1)// NewArity,
+	   		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/10) 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, maybe(list(int))::in, int::in, int::out, 
+	int::out) is det.
+
+get_arg_length([], _, _, 0, 0).
+get_arg_length([HeadUniv | Rest], MaybeList, Index, TotalLength, MaxLength) :-
+	deconstruct(univ_value(HeadUniv), Functor, Arity, _),
+	( 
+		MaybeList = yes(List),
+		not list__member(Index, List)
+	->
+		 Length = 0
+	;
+		( 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
+		)
+	),
+	( Rest = [] ->
+		Correction = 2
+	;
+		Correction = 3
+	),
+	TotalLength = Length + RestTotalLength,
+	int__max((Length + Correction), RestMaxLength, MaxLength),
+	get_arg_length(Rest, MaybeList, Index + 1, 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/21 01:18:58
@@ -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, .(1, ./2), big/3), .(1, .(2, .(3, .(4, ./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/21 01:18:58
@@ -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: tests/debugger/browser_test.exp
===================================================================
RCS file: /home/mercury1/repository/tests/debugger/browser_test.exp,v
retrieving revision 1.9
diff -u -r1.9 browser_test.exp
--- tests/debugger/browser_test.exp	2000/10/27 08:38:54	1.9
+++ tests/debugger/browser_test.exp	2001/02/21 01:18:58
@@ -27,7 +27,7 @@
 mdb> set -AP format flat
 mdb> print -f 1
        HeadVar__1             	big(big(big(small, 1, small), 2, small), 3, big(big(small, 4, big/3), 6, small))
-mdb> print -p 1
+mdb> print -r 1
        HeadVar__1             	
 big(
   big(big(small, 1, small), 2, small), 
@@ -52,6 +52,12 @@
   2-6
   3-small
 
+mdb> print -p 1
+       HeadVar__1             	
+big(
+  big(big(small, 1, small), 2, small), 
+  3, 
+  big(big(small, 4, big(small, 5, small)), 6, small))
 mdb> print --xyzzy 1
 print: unrecognized option `--xyzzy'
 mdb: print: usage error -- type `help print' for help.
Index: tests/debugger/browser_test.inp
===================================================================
RCS file: /home/mercury1/repository/tests/debugger/browser_test.inp,v
retrieving revision 1.5
diff -u -r1.5 browser_test.inp
--- tests/debugger/browser_test.inp	2000/10/27 08:38:55	1.5
+++ tests/debugger/browser_test.inp	2001/02/21 01:18:58
@@ -7,8 +7,9 @@
 browse 1; ls; quit
 set -AP format flat
 print -f 1
-print -p 1
+print -r 1
 print -v 1
+print -p 1
 print --xyzzy 1
 browse 1; print; quit
 browse -f 1; ls; quit
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/21 01:18:58
@@ -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.
 */
@@ -40,6 +40,8 @@
 
 #include <stdio.h>
 
+typedef MR_Word 	MercuryList;
+
 static	MR_Word		MR_trace_browser_persistent_state;
 static	MR_TypeInfo	MR_trace_browser_persistent_state_type;
 
@@ -142,8 +144,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 +165,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 +174,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 +183,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 +192,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);
 		);
@@ -206,6 +208,106 @@
 	return TRUE;
 }
 
+/*
+** adding type information to the database stored in the persistent 
+** state
+*/
+bool
+MR_trace_set_database(char **words, int word_count)
+{
+	int i, arg, length = 0;
+	MR_String type_name; 
+	MercuryList arg_list, database;
+	bool flag = FALSE;
+
+	length = strlen(words[1]) + 1;
+
+	type_name = malloc(sizeof(char) * length);
+
+	strcpy(type_name, words[1]);
+	
+	arg_list = MR_list_empty();
+	database = MR_list_empty();
+	
+	
+	for(i = 2 ; (i < word_count && MR_trace_is_number(words[i], &arg)) ;
+			i++)
+	{
+		arg_list = MR_list_cons((MR_Word) arg, arg_list);
+	}
+	
+	if (i != word_count)
+	{
+		return FALSE;
+	}
+	
+	MR_trace_browse_ensure_init();
+	
+	MR_TRACE_CALL_MERCURY(
+		ML_BROWSE_get_database(MR_trace_browser_persistent_state, 
+			&database);
+	);
+
+	MR_TRACE_CALL_MERCURY(
+		SIZED_PRETTY_adding_to_database((MR_String) type_name, 
+			arg_list, database, &database);
+	);
+	
+	MR_TRACE_CALL_MERCURY(
+		ML_BROWSE_set_database(MR_trace_browser_persistent_state,
+			database, &MR_trace_browser_persistent_state);
+	);
+	
+	return TRUE;
+}
+
+/*
+** clearing the database stored in the persistent state. In other words
+** replace it with an empty list.
+*/
+void
+MR_trace_clear_database()
+{
+	MercuryList database;
+	
+	database = MR_list_empty();
+
+	MR_trace_browse_ensure_init();
+	
+	MR_TRACE_CALL_MERCURY(
+		ML_BROWSE_set_database(MR_trace_browser_persistent_state,
+			database, &MR_trace_browser_persistent_state);
+	);
+
+	return;
+}
+
+/*
+** saving the database into a file
+*/
+void
+MR_trace_save_database(FILE *file)
+{
+	MercuryList database;
+	MR_String string = NULL;
+        int i = 0;
+
+	database = MR_list_empty();
+	
+	MR_trace_browse_ensure_init();
+	
+	MR_TRACE_CALL_MERCURY(
+		ML_BROWSE_get_database(MR_trace_browser_persistent_state, 
+			&database);
+	);
+
+	MR_TRACE_CALL_MERCURY(
+		SIZED_PRETTY_database_to_string(database, &string);
+	);
+       	
+	fputs(string, file);
+}
+
 static bool
 MR_trace_is_portray_format(const char *str, MR_Browse_Format *format)
 {
@@ -214,14 +316,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/21 01:18:58
@@ -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,23 @@
 ** 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);
+
+extern	bool	MR_trace_set_database(char **words, int word_count);
+
+extern	void 	MR_trace_clear_database(void);
+
+extern  void 	MR_trace_save_database(FILE *file);
+
+extern 	void	ML_BROWSE_get_database(MR_Word persistent_state, 
+		MR_Word * database);
+
+extern 	void 	SIZED_PRETTY_adding_to_database(MR_String, MR_Word, MR_Word, 
+		MR_Word *);
+
+extern 	void 	SIZED_PRETTY_database_to_string(MR_Word, MR_String *);
 
 /*
 ** 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/21 01:18:58
@@ -29,6 +29,7 @@
 
 #include "mdb.browse.h"
 #include "mdb.program_representation.h"
+#include "mdb.sized_pretty.h"
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -178,9 +179,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 +1095,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");
 		}
@@ -2014,6 +2018,8 @@
 			MR_trace_print_all_aliases(fp, TRUE);
 			found_error = MR_save_spy_points(fp, MR_mdb_err);
 
+			MR_trace_save_database(fp);
+			
 			if (found_error) {
 				fflush(MR_mdb_out);
 				fprintf(MR_mdb_err, "mdb: could not save "
@@ -2116,6 +2122,35 @@
 				"available from EXIT, FAIL or EXCP events.\n");
 		}
 #endif  /* MR_USE_DECLARATIVE_DEBUGGER */
+        } else if (streq(words[0], "add_to_database")) {
+		if (word_count > 1)
+		{
+			if (! MR_trace_set_database(words, word_count))
+			{
+				fprintf(MR_mdb_err, "mdb: Invalid arguments to"
+						" add_to_database\n"); 
+				fprintf(MR_mdb_err, "Usage: add_to_database "
+						"<string> <int> <int> ..\n"); 
+				fflush(MR_mdb_out);
+			}
+		}
+		else {
+			fprintf(MR_mdb_err,
+				"mdb: add_to_database requires atleast one " 
+				"argument.\n");
+			fprintf(MR_mdb_err, "Usage: add_to_database <string> "
+					"<int> <int> ..\n"); 
+		}
+        } else if (streq(words[0], "clear_database")) {
+		if (word_count == 1)
+		{
+			MR_trace_clear_database();
+		}
+		else {
+			fprintf(MR_mdb_err,
+				"mdb: clear_database doesn't take any "
+				"arguments.\n");
+		}
 	} else {
 		fflush(MR_mdb_out);
 		fprintf(MR_mdb_err, "Unknown command `%s'. "
@@ -2407,8 +2442,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 +2456,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 +2465,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 +2491,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 +2502,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 +2513,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 +2527,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