[comp.lang.prolog] Question on get0/read and call

duc@COS.COM (Duc Kim Nguyen) (12/30/87)

I have a question concerning get0/read and call (I am using Quintus Prolog):

(1) A predicate f is defined with
	:- op(700,fx,f).
(2) A prompt requests input from the terminal:
	a) when I use read/1 to get input, eg. f tata, and call/1 on the
	   input, the call takes place, ie., execution of predicate f occurs
	   with input tata;
	b) when I decided to use the prompt library which uses a loop of get0/1
	   to build up a list of input, and used atom_chars to convert list
	   into atom, the call/1 on this term `f tata' resulted in prediciate
	   'f data' undefined.

	Can someone explain to me what the difference between these two cases ?
	It seemed the blank made a difference in the two cases ?

	cheers
	duc@COS.COM

ok@quintus.UUCP (Richard A. O'Keefe) (12/31/87)

In article <720@cos.COM>, duc@COS.COM (Duc Kim Nguyen) writes:
> 
> I have a question concerning get0/read and call (I am using Quintus Prolog):
> 
> (1) A predicate f is defined with
> 	:- op(700,fx,f).
> (2) A prompt requests input from the terminal:
> 	a) when I use read/1 to get input, eg. f tata, and call/1 on the
> 	   input, the call takes place, ie., execution of predicate f occurs
> 	   with input tata;
> 	b) when I decided to use the prompt library which uses a loop of get0/1
> 	   to build up a list of input, and used atom_chars to convert list
> 	   into atom, the call/1 on this term `f tata' resulted in prediciate
> 	   'f data' undefined.
> 
> 	Can someone explain to me what the difference between these two cases ?
> 	It seemed the blank made a difference in the two cases ?
> 
> 	cheers
> 	duc@COS.COM

Here's the Answer, which I have also mailed to duc@COS.COM.

I'm afraid I don't understand your question.
I'll answer what I *think* is the question you are asking.

(1) An operator declaration doesn't define a predicate.
    The operator declaration has nothing to do with what I *think*
    your problem is.

(2) As near as I can figure out, you are trying two things.

    | ?- read(Goal), call(Goal).
    |: f data.

    This is the one that works.

    | ?- ensure_loaded(library(prompt)),
    |    prompted_line('Enter goal: ', Chars),
    |	 atom_chars(Atom, Chars),
    |    call(Atom).
    Enter goal: f data

    This is the one that doesn't work.

    The blank has nothing to do with anything at all.


The answer is this:
    The built-in predicate read/1 reads a ***TERM***.
    In this particular example, the term you read was f(data).
    That is, it is a compound term with principal function symbol 'f'
    and arity 1, whose single argument is the atom 'data'.

    So in the example that works, you are doing call(f(data)).

    The library predicate prompted_line/2 reads a list of character
    codes.  In this particular example, the list was "f data".
    The built-in predicate atom_chars(Atom, Chars) expresses the
    relation between an ***ATOM*** and the list of characters
    comprising the name of the atom.  In this particular example,
    the atom you got back was 'f data'.

    So in the example that doesn't work, you are doing call('f data').
    That is, you are trying to call a predicate whose predicate
    symbol is 'f data' and which has no arguments.  If you had said
    "f(data)" instead of "f data", you would have ended up trying to
    call a predicate of no arguments whose name was the atom 'f(data)'.

    You should have got an error message which looked like this:

    [Warning: The procedure 'f data'/0 is undefined]
       (1) 1 Call: 'f data' ? 

    DO pay attention to the single quotes in the error message,
    they ARE telling you something!

There are at least two ways of doing what you apparently want.

(a)	prompt_and_read(Prompt, Term) :-
		prompt(Prompt),
		read(Term).

    This version requires you to terminate the input term with a full
    stop and layout character, as always when using read/1.

(b)	:- ensure_loaded(library(charsio)).

	prompted_term(Prompt, Term) :-
		prompted_line(Prompt, Chars),
		chars_to_term(Chars, Term).

    This version doesn't require you to terminate the input with a
    full stop and layout character, but it won't let you use more
    than one line for the term either.

    On the grounds of portability, efficiency, and general hygiene,
    I'd go for (a).

lang@linc.cis.upenn.edu (Francois-Michel Lang) (12/31/87)

In article <720@cos.COM> duc@COS.COM (Duc Kim Nguyen) writes:
>
>I have a question concerning get0/read and call (I am using Quintus Prolog):
>
>(1) A predicate f is defined with
>	:- op(700,fx,f).
...

I'm not sure I fully understand the question/problem,
but I think I can clear up one apparent misconception:
The operator declaration 

:- op(700,fx,f).

does NOT define a predicate.  It only allows you to use the
atom 'f' as a prefix operator.  That sounds like it's part of the problem.
----------------------------------------------------------------------------
Francois-Michel Lang			    
Paoli Research Center, Unisys Corporation lang@prc.unisys.com (215) 648-7469
Dept of Comp & Info Science, U of PA      lang@cis.upenn.edu  (215) 898-9511