Path: utzoo!attcan!uunet!seismo!sundc!pitstop!sun!quintus!ok From: ok@quintus.uucp (Richard A. O'Keefe) Newsgroups: comp.lang.prolog Subject: Re: Infix-to-prefix notation (Or: Why doesn't C-Prolog's "display" work?) Message-ID: <740@quintus.UUCP> Date: 24 Nov 88 02:37:09 GMT References: <1988Nov22.191600.26206@gpu.utcs.toronto.edu> Sender: news@quintus.UUCP Reply-To: ok@quintus.UUCP (Richard A. O'Keefe) Organization: Quintus Computer Systems, Inc. Lines: 53 In article <1988Nov22.191600.26206@gpu.utcs.toronto.edu> sarathy@gpu.utcs.utoronto.ca (Rajiv Sarathy) writes: >The example on page 99 of the edition I have of C & M says that >?- display(a+b*c*c). > should return >+(a,*(*(b,c),c)) >yes I don't know what you mean by "return", but that is indeed what should come out on your terminal. > However, the implementation of C-Prolog I'm using (on a SUN 3/280, if >it matters) merely returns X. Transcript, please! (Easy way to do that: use the Unix 'script' command. Which is why C Prolog doesn't implement log/0 and nolog/0.) Last time I used C Prolog, display/1 worked fine. >Does someone have the definition of the "display" predicate? If you have a copy of C Prolog, you have the source code, so YOU have the definition. If you have a copy of the DEC-10 Prolog library, look in WRITE.PL. Otherwise: display(Term) :- telling(CurrentOutput), tell(user), display_1(Term), tell(CurrentOutput). display_1(Term) :- % for a COMPOUND term, nonvar(Term), functor(Term, F, N), N > 0, !, write(F), put("("), % (,...,) display_1(1, N, Term). display_1(Term) :- % for a SIMPLE term, write(Term). % (variable, number, or atom) display_1(I, N, Term) :- arg(I, Term, Arg), display_1(Arg), ( I < N -> put(","), J is I+1, display_1(J, N, Term) ; put(")") ). There are some fine points that this doesn't get exactly right, but it's pretty close. (One of the fine points is that write/1 may round floating point numbers off; display/1 should write all the digits that make sense.)