Index: xsb_writ.P
===================================================================
--- xsb_writ.P	(revision 8473)
+++ xsb_writ.P	(working copy)
@@ -565,13 +565,21 @@
 file_write_quoted(File, Term) :- 
 	xsb_stream_lock(File),
 %	xsb_sys_mutex_lock(MUTEX_IO),
-	l_write_quoted(File, Term, 999), 
+	l_write_quoted_arg(File, Term, 1200),
 %	xsb_sys_mutex_unlock(MUTEX_IO),
 	xsb_stream_unlock(File),
 	fail.
 file_write_quoted(_, _).
 
 
+l_write_quoted_arg(File, Atom, _P) :-
+	term_type(Atom, Type),
+	Type =:= TK_ATOM,
+	!,
+	file_writequoted(File, Atom).
+l_write_quoted_arg(File, Atom, Prec) :-
+	l_write_quoted(File, Atom, Prec).
+
 l_write_quoted(File, Var, _) :-
 	term_type(Var, Type),
 	(Type =:= XSB_FREE; Type =:= XSB_ATTV),
@@ -587,7 +595,7 @@
 l_write_quoted(File, [X|Y], _) :-
 	!, 
 	file_put(File, CH_LBRACKET), 	% [
-	l_write_quoted(File, X, 999), 
+	l_write_quoted_arg(File, X, 999),
 	l_writetail_quoted(File, Y).
 l_write_quoted(File, Atom, _) :-	% This takes care of all atoms but []
 	term_type(Atom, Type),
@@ -650,14 +658,14 @@
 	file_writequoted(File, Name), 
 	file_put(File, CH_LPAREN),		% (
 	term_arg(Term, 1, X),
-	l_write_quoted(File, X, 999),
+	l_write_quoted_arg(File, X, 999),
         l_writearg_quoted(File, Term, N, 1), 
 	file_put(File, CH_RPAREN).		% )
 
 l_write_curly_quoted(File, Term) :- 
 	file_put(File, CH_LBRACE),	% {
 	term_arg(Term, 1, Arg),
-	l_write_quoted(File, Arg, 999),
+	l_write_quoted(File, Arg, 1200),
 	file_put(File, CH_RBRACE).	% }
 
 l_hilog_writestr_quoted(File, N, Term) :-
@@ -699,11 +707,40 @@
 	    l_write_quoted_chk_min(File, Arg, Newp, Op)
 	).
 
+
+
+to_be_output_as_postfix(Op) :-
+   '_$op'(_,Fix,Op),
+   ( Fix == xf -> true
+   ; Fix == yf -> true
+   ).
+
+to_be_output_as_infix(Op) :-
+   Op \== ('.'),
+   '_$op'(_,Fix,Op),
+   ( Fix == xfy -> true
+   ; Fix == yfx -> true
+   ; Fix == xfx -> true
+   ).
+
+
 % special case for, e.g.,  - (111)
 l_write_quoted_chk_min(File, Arg, Newp, Op) :-
-	(Op == (-),number(Arg)
+	( ( Op == (-), nonvar(Arg)
+            -> ( number(Arg)
+                 -> Arg >= 0
+		 ; compound(Arg),
+                   functor(Arg, FArg, FN),
+                   FN =< 2,
+                   ( FN == 1
+                    -> to_be_output_as_postfix(FArg)
+                    ;  to_be_output_as_infix(FArg)
+  	       	   )
+	       )
+            ; fail
+          )
 	 ->	file_put(File, CH_LPAREN),
-		l_write_quoted(File, Arg, Newp),
+		l_write_quoted(File, Arg, 1200),
 		file_put(File, CH_RPAREN)
 	 ;	%file_put(File, CH_SPACE),
 		l_write_quoted(File, Arg, Newp)
@@ -727,7 +764,7 @@
 	L is M + 1, 
 	file_put(File, CH_COMMA),	% ','
 	term_arg(T, L, X),
-        l_write_quoted(File, X, 999), 
+        l_write_quoted_arg(File, X, 999),
 	l_writearg_quoted(File, T, N, L).
 
 l_writetail_quoted(File, Var) :-
@@ -739,13 +776,13 @@
 	file_put(File, CH_RBRACKET).	% ']'
 l_writetail_quoted(File, [X|Y]) :- !, 
 	file_put(File, CH_COMMA),	% ','
-	l_write_quoted(File, X, 999), 
+	l_write_quoted_arg(File, X, 999),
 	l_writetail_quoted(File, Y).
 l_writetail_quoted(File, []) :- 
 	!, file_put(File, CH_RBRACKET).		% ]
 l_writetail_quoted(File, X) :- 
 	file_put(File, CH_BAR), 	% |
-	l_write_quoted(File, X, 999), 
+	l_write_quoted_arg(File, X, 999),
 	file_put(File, CH_RBRACKET).	% ]
 
 
