<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"> <HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <META name="GENERATOR" content="hevea 1.06-7 of 2001-11-14"> <TITLE> The GNU Prolog interactive interpreter </TITLE> </HEAD> <BODY TEXT=black BGCOLOR=white> <A HREF="manual005.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual004.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual007.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> <HR> <TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#66dbff"><DIV ALIGN=center><TABLE> <TR><TD><FONT SIZE=4><B><A NAME="htoc5">3.2</A></B></FONT></TD> <TD WIDTH="100%" ALIGN=center><FONT SIZE=4><B>The GNU Prolog interactive interpreter</B></FONT></TD> </TR></TABLE></DIV></TD> </TR></TABLE><UL> <LI><A HREF="manual006.html#toc1"> Starting/exiting the interactive interpreter</A> <LI><A HREF="manual006.html#toc2"> The interactive interpreter read-execute-write loop</A> <LI><A HREF="manual006.html#toc3"> Consulting a Prolog program</A> <LI><A HREF="manual006.html#toc4"> Interrupting a query</A> <LI><A HREF="manual006.html#toc5"> The line editor</A> </UL> <A NAME="The-GNU-Prolog-interactive-interpreter"></A><BR> <A NAME="toc1"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc6">3.2.1</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Starting/exiting the interactive interpreter</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> GNU Prolog offers a classical Prolog interactive interpreter also called <EM>top-level</EM>. It allows the user to execute queries, to consult Prolog programs, to list them, to execute them and to debug them. The top-level can be invoked using the following command: <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP><TT>% gprolog </TT>[<I><TT>OPTION</TT></I>]...</TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(the <TT>%</TT> symbol is the operating system shell prompt)</TD> </TR></TABLE></DL> <B>Options</B>:<BR> <TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD VALIGN=top ALIGN=left><TT>--init-goal</TT> <I><TT>GOAL</TT></I></TD> <TD VALIGN=top ALIGN=left NOWRAP>execute <I><TT>GOAL</TT></I> before top_level/0</TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>--entry-goal</TT> <I><TT>GOAL</TT></I></TD> <TD VALIGN=top ALIGN=left NOWRAP>execute <I><TT>GOAL</TT></I> inside top_level/0</TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>--query-goal</TT> <I><TT>GOAL</TT></I></TD> <TD VALIGN=top ALIGN=left NOWRAP>execute <I><TT>GOAL</TT></I> as a query for top_level/0</TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>--help</TT></TD> <TD VALIGN=top ALIGN=left NOWRAP>print a help and exit</TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>--version</TT></TD> <TD VALIGN=top ALIGN=left NOWRAP>print version number and exit</TD> </TR> <TR><TD VALIGN=top ALIGN=left><TT>--</TT></TD> <TD VALIGN=top ALIGN=left NOWRAP>do not parse the rest of the command-line</TD> </TR></TABLE><BR> The main role of the <TT>gprolog</TT> command is to execute the top-level itself, i.e. to execute the built-in predicate <TT>top_level/0</TT> (section <A HREF="manual041.html#abort/0">7.18.1</A>) which will produce something like: <DL COMPACT=compact><DT><DD><TT> </TT><PRE><TT> GNU Prolog 1.2.9 By Daniel Diaz Copyright (C) 1999-2001 Daniel Diaz | ?- </TT></PRE></DL> The top-level is ready to execute your queries as explained in the next section.<BR> <BR> To quit the top-level type the end-of-file key sequence (<TT>Ctl-D</TT>) or its term representation: <TT>end_of_file.</TT> It is also possible to use the built-in predicate <TT>halt/0</TT> (section <A HREF="manual041.html#abort/0">7.18.1</A>).<BR> <BR> However, before entering the top-level itself, the command-line is processed to treat all known options (those listed above). All unrecognized arguments are collected together to form the argument list which will be available using <TT>argument_value/2</TT> (section <A HREF="manual050.html#argument-value/2">7.27.2</A>) or <TT>argument_list/1</TT> (section <A HREF="manual050.html#argument-list/1">7.27.3</A>). The <TT>--</TT> option stops the parsing of the command-line, all remainding options are collected into the argument list.<BR> <BR> Several options are provided to execute a goal before entering the interaction with the user: <UL><LI>The <TT>--init-goal</TT> option executes the <I><TT>GOAL</TT></I> as soon as it is encountered (while the commnad-line is processed). <I><TT>GOAL</TT></I> is thus executed before entering <TT>top_level/0</TT>.<BR> <BR> <LI>The <TT>--entry-goal</TT> option executes the <I><TT>GOAL</TT></I> at the entry of <TT>top_level/0</TT> just after the banner is displayed.<BR> <BR> <LI>The <TT>--query-goal</TT> option executes the <I><TT>GOAL</TT></I> as if the user has typed in.</UL> The above order is thus the order in which each kind of goal (init, entry, query) is executed. If there are several goals of a same kind they are executed in the oder of appearance. Thus, all init goals are executed (in the order of appearance) before all entry goals and all entry goals are executed before all query goals.<BR> <BR> Each <I><TT>GOAL</TT></I> is passed as a shell argument (i.e. one shell string) and should not contain a terminal dot. Example: <TT>--init-goal 'write(hello), nl'</TT> under a sh-like. To be executed, a <I><TT>GOAL</TT></I> is transformed into a term using <TT>read_term_from_atom(Goal, Term, [end_of_term(eof)])</TT>. Respecting both the syntax of shell strings and of Prolog can be heavy. For instance, passing a backslash character <TT>\</TT> can be difficult since it introduces an escape sequence both in sh and inside Prolog quoted atoms. The use of back quotes can then be useful since, by default, no escape sequence is processed inside back quotes (this behavior can be controlled using the <TT>back_quotes</TT> Prolog flag (section <A HREF="manual045.html#set-prolog-flag/2">7.22.1</A>)).<BR> <BR> Since the Prolog argument list is created when the whole command-line is parsed, if a <TT>--init-goal</TT> option uses <TT>argument_value/2</TT> or <TT>argument_list/1</TT> it will obtained the original command-line arguments (i.e. including all recognized arguments). <BR> <BR> Here is an example of using execution goal options: <DL COMPACT=compact><DT><DD><TT> % gprolog --init-goal 'write(before), nl' --entry-goal 'write(inside), nl'<BR> --query-goal 'append([a,b],[c,d],X)' </TT></DL> will produce the following: <DL COMPACT=compact><DT><DD><TT> </TT><PRE><TT> before GNU Prolog 1.2.9 By Daniel Diaz Copyright (C) 1999-2001 Daniel Diaz inside | ?- append([a,b],[c,d],X). X = [a,b,c,d] yes | ?- </TT></PRE></DL> <A NAME="toc2"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc7">3.2.2</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>The interactive interpreter read-execute-write loop</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> The GNU Prolog top-level is built on a classical read-execute-write loop that also allows for re-executions (when the query is not deterministic) as follows: <UL><LI>display the prompt, i.e. '<TT>| ?-</TT>'.<BR> <BR> <LI>read a query (i.e. a goal).<BR> <BR> <LI>execute the query.<BR> <BR> <LI>in case of success display the values of the variables of the query.<BR> <BR> <LI>if there are remaining alternatives (i.e. the query is not deterministic), display a <TT>?</TT> and ask the user who can use one of the following commands: <TT>RETURN</TT> to stop the execution, <TT>;</TT> to compute the next solution or <TT>a</TT> to compute all remaining solution.</UL> Here is an example of execution of a query (``find the lists <TT>X</TT> and <TT>Y</TT> such that the concatenation of <TT>X</TT> and <TT>Y</TT> is <TT>[a,b]</TT>''): <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- append(X,Y,[a,b,c]).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>X = []</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>Y = [a,b,c] ? ;</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>;</TT> to compute another solution)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>X = [a]</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>Y = [b,c] ? a</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>a</TT> to compute all remaining solutions)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>X = [a,b]</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>Y = [c]</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user is not asked and the next solution is computed)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>X = [a,b,c]</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>Y = []</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user is not asked and the next solution is computed)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>no</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(no more solution)</TD> </TR></TABLE></DL> In some cases the top-level can detect that the current solution is the last one (no more alternatives remaining). In such a case it does not display the <TT>?</TT> symbol (and does not ask the user). Example: <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- (X=1 ; X=2).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = 1 ? ;</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>;</TT> to compute another solution)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = 2</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user is not prompted since there are no more alternatives)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>yes</TT></TD> </TR></TABLE></DL> The user can stop the execution even if there are more alternatives by typing <TT>RETURN</TT>. <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- (X=1 ; X=2).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = 1 ?</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>RETURN</TT> to stop the execution)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>yes</TT></TD> </TR></TABLE></DL> The top-level tries to display the values of the variables of the query in a readable manner. For instance, when a variable is bound to a query variable, the name of this variable appears. When a variable is a singleton an underscore symbol <TT>_</TT> is displayed (<TT>_</TT> is a generic name for a singleton variable, it is also called an anonymous variable). Other variables are bound to new brand variable names. When a query variable name <TT>X</TT> appears as the value of another query variable <TT>Y</TT> it is because <TT>X</TT> is itself not instantiated otherwise the value of <TT>X</TT> is displayed. In such a case, nothing is output for <TT>X</TT> itself (since it is a variable). Example: <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- X=f(A,B,_,A), A=k.</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>A = k</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(the value of <TT>A</TT> is displayed also in <TT>f/3</TT> for <TT>X</TT>)</TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = f(k,B,_,k)</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(since <TT>B</TT> is a variable which is also a part of <TT>X</TT>, <TT>B</TT> is not displayed)</TD> </TR></TABLE></DL> <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- functor(T,f,3), arg(1,T,X), arg(3,T,X).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>T = f(X,_,X)</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(the 1<FONT SIZE=2><I><SUP>st</SUP></I></FONT> and 3<FONT SIZE=2><I><SUP>rd</SUP></I></FONT> args are equal to <TT>X</TT>, the 2<FONT SIZE=2><I><SUP>nd</SUP></I></FONT> is an anonymous variable)</TD> </TR></TABLE></DL> <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- read_from_atom('k(X,Y,X).',T).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>T = k(A,_,A)</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(the 1<FONT SIZE=2><I><SUP>st</SUP></I></FONT> and 3<FONT SIZE=2><I><SUP>rd</SUP></I></FONT> args are unified, a new variable name <TT>A</TT> is introduced)</TD> </TR></TABLE></DL> The top-level uses variable binding predicates (section <A HREF="manual028.html#Variable-naming/numbering">7.5</A>). To display the value of a variable, the top-level calls <TT>write_term/3</TT> with the following option list: <TT>[quoted(true),numbervars(false), namevars(true)]</TT> (section <A HREF="manual037.html#write-term/3">7.14.6</A>). A term of the form <TT>'$VARNAME'(Name)</TT> where <TT>Name</TT> is an atom is displayed as a variable name while a term of the form <TT>'$VAR'(N)</TT> where <TT>N</TT> is an integer is displayed as a normal compound term (such a term could be output as a variable name by <TT>write_term/3</TT>). Example: <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- X='$VARNAME'('Y'), Y='$VAR'(1).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = Y</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(the term <TT>'$VARNAME'('Y')</TT> is displayed as <TT>Y</TT>)</TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>Y = '$VAR'(1)</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(the term <TT>'$VAR'(1)</TT> is displayed as is)</TD> </TR></TABLE></DL> <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- X=Y, Y='$VAR'(1).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>X = '$VAR'(1)</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>Y = '$VAR'(1)</TT></TD> </TR></TABLE></DL> In the first example, <TT>X</TT> is explicitly bound to <TT>'$VARNAME'('Y')</TT> by the query so the top-level displays <TT>Y</TT> as the value of <TT>X</TT>. <TT>Y</TT> is unified with <TT>'$VAR'(1)</TT> so the top-level displays it as a normal compound term. It should be clear that <TT>X</TT> is not bound to <TT>Y</TT> (whereas it is in the second query). This behavior should be kept in mind when doing variable binding operations.<BR> <BR> Finally, the top-level computes the user-time (section <A HREF="manual047.html#user-time/1">7.24.2</A>) taken by a query and displays it when it is significant. Example: <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- retractall(p(_)), assertz(p(0)),</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT> repeat,</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT> retract(p(X)),</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT> Y is X + 1,</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT> assertz(p(Y)),</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT> X = 1000, !.</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>X = 1000</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>Y = 1001</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>(180 ms) yes</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(the query took 180ms of user time)</TD> </TR></TABLE></DL> <A NAME="toc3"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc8">3.2.3</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Consulting a Prolog program</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="Consulting-a-Prolog-program"></A> The top-level allows the user to consult Prolog source files. Consulted predicates can be listed, executed and debugged (while predicates compiled to native-code cannot). For more information about the difference between a native-code predicate and a consulted predicate refer to the introduction of this section (section <A HREF="manual005.html#Introduction:(Using-GNU-Prolog)">3.1</A>) and to the part devoted to the compiler (section <A HREF="manual008.html#Different-kinds-of-codes">3.4.1</A>).<BR> <BR> To consult a program use the built-in predicate <TT>consult/1</TT> (section <A HREF="manual046.html#consult/1">7.23.1</A>). The argument of this predicate is a Prolog file name or <TT>user</TT> to specify the terminal. This allows the user to directly input the predicates from the terminal. In that case the input shall be terminated by the end-of-file key sequence (<TT>Ctl-D</TT>) or its term representation: <TT>end_of_file.</TT> A shorthand for <TT>consult(<I>FILE</I>)</TT> is <TT>[<I>FILE</I>]</TT>. Example: <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- [user].</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>{compiling user for byte code...}</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>even(0).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>even(s(s(X))):-</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT> even(X).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP> </TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>Ctl-D</TT> to end the input)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>{user compiled, 3 lines read - 350 bytes written, 1180 ms}</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- even(X).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = 0 ? ;</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>;</TT> to compute another solution)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = s(s(0)) ? ;</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>;</TT> to compute another solution)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>X = s(s(s(s(0)))) ?</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>RETURN</TT> to stop the execution)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>yes</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>| ?- listing.</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>even(0).</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>even(s(s(A))) :-</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT> even(A).</TT></TD> </TR></TABLE></DL> When <TT>consult/1</TT> (section <A HREF="manual046.html#consult/1">7.23.1</A>) is invoked on a Prolog file it first runs the GNU Prolog compiler (section <A HREF="manual008.html#The-GNU-Prolog-compiler">3.4</A>) as a child process to generate a temporary WAM file for byte-code. If the compilation fails a message is displayed and nothing is loaded. If the compilation succeeds, the produced file is loaded into memory using <TT>load/1</TT> (section <A HREF="manual046.html#load/1">7.23.2</A>). Namely, the byte-code of each predicate is loaded. When a predicate <I><TT>P</TT></I> is loaded if there is a previous definition for <I><TT>P</TT></I> it is removed (i.e. all clauses defining <I><TT>P</TT></I> are erased). We say that <I><TT>P</TT></I> is redefined. Note that only consulted predicates can be redefined. If <I><TT>P</TT></I> is a native-code predicate, trying to redefine it will produce an error at load-time: the predicate redefinition will be ignored and the following message displayed: <DL COMPACT=compact><DT><DD><TT>native code procedure <I>P</I> cannot be redefined</TT></DL> Finally, an existing predicate will not be removed if it is not re-loaded. This means that if a predicate <I><TT>P</TT></I> is loaded when consulting the file <I><TT>F</TT></I>, and if later the definition of <I><TT>P</TT></I> is removed from the file <I><TT>F</TT></I>, consulting <I><TT>F</TT></I> again will not remove the previously loaded definition of <I><TT>P</TT></I> from the memory.<BR> <BR> Consulted predicates can be debugged using the Prolog debugger. Use the debugger predicate <TT>trace/0</TT> or <TT>debug/0</TT> (section <A HREF="manual012.html#Running-and-stopping-the-debugger">4.3.1</A>) to activate the debugger.<BR> <BR> <A NAME="toc4"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc9">3.2.4</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>Interrupting a query</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="Interrupting-a-query"></A> Under the top-level it is possible to interrupt the execution of a query by typing the interruption key (<TT>Ctl-C</TT>). This can be used to abort a query, to stop an infinite loop, to activate the debugger,...When an interruption occurs the top-level displays the following message: <TT>Prolog interruption (h for help) ?</TT> The user can then type one of the following commands:<BR> <TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1> <TR><TD ALIGN=center NOWRAP>Command</TD> <TD ALIGN=center NOWRAP>Name</TD> <TD ALIGN=left NOWRAP>Description</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>a</TT></TD> <TD ALIGN=center NOWRAP>abort</TD> <TD ALIGN=left NOWRAP>abort the current execution. Same as <TT>abort/0</TT> (section <A HREF="manual041.html#abort/0">7.18.1</A>)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>e</TT></TD> <TD ALIGN=center NOWRAP>exit</TD> <TD ALIGN=left NOWRAP>quit the current Prolog process. Same as <TT>halt/0</TT> (section <A HREF="manual041.html#abort/0">7.18.1</A>)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>b</TT></TD> <TD ALIGN=center NOWRAP>break</TD> <TD ALIGN=left NOWRAP>invoke a recursive top-level. Same as <TT>break/0</TT> (section <A HREF="manual041.html#abort/0">7.18.1</A>)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>c</TT></TD> <TD ALIGN=center NOWRAP>continue</TD> <TD ALIGN=left NOWRAP>resume the execution</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>t</TT></TD> <TD ALIGN=center NOWRAP>trace</TD> <TD ALIGN=left NOWRAP>start the debugger using <TT>trace/0</TT> (section <A HREF="manual012.html#Running-and-stopping-the-debugger">4.3.1</A>)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>d</TT></TD> <TD ALIGN=center NOWRAP>debug</TD> <TD ALIGN=left NOWRAP>start the debugger using <TT>debug/0</TT> (section <A HREF="manual012.html#Running-and-stopping-the-debugger">4.3.1</A>)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>h</TT> or <TT>?</TT></TD> <TD ALIGN=center NOWRAP>help</TD> <TD ALIGN=left NOWRAP>display a summary of available commands</TD> </TR></TABLE><BR> <A NAME="toc5"></A><TABLE CELLPADDING=0 CELLSPACING=0 WIDTH="100%"> <TR><TD BGCOLOR="#98e7ff"><DIV ALIGN=center><TABLE> <TR><TD><B><A NAME="htoc10">3.2.5</A></B></TD> <TD WIDTH="100%" ALIGN=center><B>The line editor</B></TD> </TR></TABLE></DIV></TD> </TR></TABLE> <A NAME="The-line-editor"></A> The line editor (<TT>linedit</TT>) allows the user to build/update the current input line using a variety of commands. This facility is available if the <TT>linedit</TT> part of GNU Prolog has been installed. <TT>linedit</TT> is implicitly called by any built-in predicate reading from a terminal (e.g. <TT>get_char/1</TT>, <TT>read/1</TT>,...). This is the case when the top-level reads a query.<BR> <BR> <B>Bindings</B>: each command of <TT>linedit</TT> is activated using a key. For some commands another key is also available to invoke the command (on some terminals this other key may not work properly while the primary key always works). Here is the list of available commands:<BR> <TABLE BORDER=1 CELLSPACING=0 CELLPADDING=1> <TR><TD ALIGN=center NOWRAP>Key</TD> <TD ALIGN=center NOWRAP>Alternate key</TD> <TD ALIGN=left NOWRAP>Description</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-B</TT></TD> <TD ALIGN=center NOWRAP><TT><-</TT></TD> <TD ALIGN=left NOWRAP>go to the previous character</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-F</TT></TD> <TD ALIGN=center NOWRAP><TT>-></TT></TD> <TD ALIGN=left NOWRAP>go to the next character</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-B</TT></TD> <TD ALIGN=center NOWRAP><TT>Ctl-<-</TT></TD> <TD ALIGN=left NOWRAP>go to the previous word</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-F</TT></TD> <TD ALIGN=center NOWRAP><TT>Ctl--></TT></TD> <TD ALIGN=left NOWRAP>go to the next word</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-A</TT></TD> <TD ALIGN=center NOWRAP><TT>Home</TT></TD> <TD ALIGN=left NOWRAP>go to the beginning of the line</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-E</TT></TD> <TD ALIGN=center NOWRAP><TT>End</TT></TD> <TD ALIGN=left NOWRAP>go to the end of the line</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-H</TT></TD> <TD ALIGN=center NOWRAP><TT>Backspace</TT></TD> <TD ALIGN=left NOWRAP>delete the previous character</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-D</TT></TD> <TD ALIGN=center NOWRAP><TT>Delete</TT></TD> <TD ALIGN=left NOWRAP>delete the current character</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-U</TT></TD> <TD ALIGN=center NOWRAP><TT>Ctl-Home</TT></TD> <TD ALIGN=left NOWRAP>delete from beginning of the line to the current character</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-K</TT></TD> <TD ALIGN=center NOWRAP><TT>Ctl-End</TT></TD> <TD ALIGN=left NOWRAP>delete from the current character to the end of the line</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-L</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>lower case the next word</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-U</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>upper case the next word</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-C</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>capitalize the next word</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-T</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>exchange last two characters</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-V</TT></TD> <TD ALIGN=center NOWRAP><TT>Insert</TT></TD> <TD ALIGN=left NOWRAP>switch on/off the insert/replace mode</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-I</TT></TD> <TD ALIGN=center NOWRAP><TT>Tab</TT></TD> <TD ALIGN=left NOWRAP>complete word (twice displays all possible completions)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-Ctl-I</TT></TD> <TD ALIGN=center NOWRAP><TT>Esc-Tab</TT></TD> <TD ALIGN=left NOWRAP>insert spaces to emulate a tabulation</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-space</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>mark beginning of the selection</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-W</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>copy (from the begin selection mark to the current character)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-W</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>cut (from the begin selection mark to the current character)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-Y</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>paste</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-P</TT></TD> <TD ALIGN=center NOWRAP><TT>uparrow</TT></TD> <TD ALIGN=left NOWRAP>recall previous history line</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-N</TT></TD> <TD ALIGN=center NOWRAP><TT>downarrow</TT></TD> <TD ALIGN=left NOWRAP>recall next history line</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-P</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>recall previous history line beginning with the current prefix</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-N</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>recall next history line beginning with the current prefix</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-<</TT></TD> <TD ALIGN=center NOWRAP><TT>Page Up</TT></TD> <TD ALIGN=left NOWRAP>recall first history line</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-></TT></TD> <TD ALIGN=center NOWRAP><TT>Page Down</TT></TD> <TD ALIGN=left NOWRAP>recall last history line</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-C</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>generate an interrupt signal (section <A HREF="#Interrupting-a-query">3.2.4</A>)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Ctl-D</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>generate an end-of-file character (at the begin of the line)</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>RETURN</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>validate a line</TD> </TR> <TR><TD ALIGN=center NOWRAP><TT>Esc-?</TT></TD> <TD ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>display a summary of available commands</TD> </TR></TABLE><BR> <B>History</B>: when a line is entered (i.e. terminated by <TT>RETURN</TT>), <TT>linedit</TT> records it in an internal list called history. It is later possible to recall history lines using appropriate commands (e.g. <TT>Ctl-P</TT> recall the last entered line) and to modify them as needed. It is also possible to recall a history line beginning with a given prefix. For instance to recall the previous line beginning with <TT>write</TT> simply type <TT>write</TT> followed by <TT>Esc-P</TT>. Another <TT>Esc-P</TT> will recall an earlier line beginning with <TT>write</TT>,...<BR> <BR> <B>Completion</B>: another important feature of <TT>linedit</TT> is its completion facility. Indeed, <TT>linedit</TT> maintains a list of known words and uses it to complete the prefix of a word. Initially this list contains all predefined atoms and the atoms corresponding to available predicates. This list is dynamically updated when a new atom appears in the system (whether read at the top-level, created with a built-in predicate, associated to a new consulted predicate,...). When the completion key (<TT>Tab</TT>) is pressed <TT>linedit</TT> acts as follows: <UL><LI>use the current word as a prefix.<BR> <BR> <LI>collect all words of the list that begin with this prefix.<BR> <BR> <LI>complete the current word with the longest common part of all matching words.<BR> <BR> <LI>if more than one word matches emit a beep (a second <TT>Tab</TT> will display all possibilities).</UL> Example: <DL COMPACT=compact><DT><DD><TABLE CELLSPACING=2 CELLPADDING=0> <TR><TD ALIGN=left NOWRAP><TT>| ?- argu</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(here the user presses <TT>Tab</TT> to complete the word)</TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>| ?- argument_</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(<TT>linedit</TT> completes <TT>argu</TT> with <TT>argument_</TT> and emits a beep)</TD> </TR> <TR><TD ALIGN=left NOWRAP> </TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(the user presses again <TT>Tab</TT> to see all possible completions)</TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>argument_counter</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(<TT>linedit</TT> shows 3 possible completions)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>argument_list</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3><TT>argument_value</TT></TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>| ?- argument_</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(<TT>linedit</TT> redisplays the input line)</TD> </TR> <TR><TD ALIGN=left NOWRAP COLSPAN=3> </TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>| ?- argument_c</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(to select <TT>argument_counter</TT> the user presses <TT>c</TT> and <TT>Tab</TT>)</TD> </TR> <TR><TD ALIGN=left NOWRAP><TT>| ?- argument_counter</TT></TD> <TD VALIGN=top ALIGN=center NOWRAP> </TD> <TD ALIGN=left NOWRAP>(<TT>linedit</TT> completes with <TT>argument_counter</TT>)</TD> </TR></TABLE></DL> Finally, <TT>linedit</TT> allows the user to check that (square/curly) brackets are well balanced. For this, when a close bracket symbol, i.e. <TT>)</TT>, <TT>]</TT> or <TT>}</TT>, is typed, <TT>linedit</TT> determines the associated open bracket, i.e. <TT>(</TT>, <TT>[</TT> or <TT>{</TT>, and temporarily repositions the cursor on it to show the match.<BR> <BR> <HR SIZE=2> Copyright (C) 1999-2002 Daniel Diaz <BR> <BR> Verbatim copying and distribution of this entire article is permitted in any medium, provided this notice is preserved. <BR> <BR> <A HREF="index.html#copyright">More about the copyright</A> <HR> <A HREF="manual005.html"><IMG SRC ="previous_motif.gif" ALT="Previous"></A> <A HREF="manual004.html"><IMG SRC ="contents_motif.gif" ALT="Contents"></A> <A HREF="manual007.html"><IMG SRC ="next_motif.gif" ALT="Next"></A> </BODY> </HTML>