酷兔英语

章节正文
文章总共2页


However, you might wish to get input/read from somewhere else during the execution of your Prolog program - for example, you might want to read from a file held on the computer on which the program is running, or on some local file server. To change the current input stream, you use the Prolog built-in extra-logical predicate see. If Prolog executes the goal see('input.dat'), then input will subsequently come from the file input.dat, in the current working directory of the workstation* that is running Prolog. If the specified file cannot be found in the current working directory, an error may be reported, as in this interaction with SWI Prolog:


?- see('nosuchfile.dat').
ERROR: see/1: source_sink `nosuchfile.dat' does not exist (No such file or directory)


Other errors are possible - you may not have the right to read the file in question, for example. If the file does exist and is readable, then subsequent read operations get their data from the file. The parameter to see can be just the file name, as illustrated above, or it could be a path to the file that is wanted: e.g. see('/Users/billw/prologstuff/input.dat') Prolog will continue to issue prompts for more queries while you are "seeing" a file, but any explicit read operations access the file. The built-in extra-logical predicate seen (with no argument) allows you to revert to reading data from the keyboard. Example (assuming info.dat starts with the line hungry(jack).):


?- see('info.dat'), read(X).
X = hungry(jack)

?- seen, read(Y).
|: full(jack).
Y = full(jack)


See also current output stream, input, output, files.



*Strictly speaking, input.dat will be expected to be in the current working directory of the command interpreter that started Prolog. The command interpreter will be running on the workstation/computer, and sending output to a window on that workstation or computer.




 




current output stream


At any given time, input to Prolog goes to the "current output stream". When Prolog starts up, the current outputstream is the window or console of the computer/workstation from which Prolog was started. In other words, when your program writes things using the predicates described in the article on writing, they appear on your screen.


 



However, you might wish to write to somewhere else during the execution of your Prolog program - for example, you might want to write to a file held on the computer on which the program is running, or on some local file server.



To change the current output stream, you use one of the Prolog built-in extra-logical predicates tell and append/1 *. If Prolog executes the goal tell('output.dat'), then output will subsequently go to the file output.dat, in the current working directory of the workstation* that is running Prolog.



If the specified file cannot be found in the current working directory, it will be created. If the file does exist, it will be overwritten. If you use append/1, subsequent write operations will add material to the end of the file, instead of overwriting the file. If you do not have permission to write files in the current directory, you will see an error message:


?- tell('/usr/bin/ztrash').  
ERROR: tell/1: No permission to open source_sink `/usr/bin/ztrash' (Permission denied)


This means that either you do not have permission to write files in the directory /usr/bin, or if the file ztrash already exists in this directory, that you do not have permission to write to that file.



If the file is able to be written, then subsequent write operations send their data to the file. The parameter to tell can be a path to the file that is wanted, as in the example above, or it could be just the file name, e.g. tell('output.dat'). Prolog will continue to issue prompts for more queries and print bindings while you are "telling" or "appending" a file, but any explicit write operations access the file. The built-in extra-logical predicate told (with no argument) allows you to revert to writing data to the original window. Example:


?- tell('info.dat'), write(thirsty(jack)), nl.
true.
?- told, write(drunk(jack)).
drunk(jack)
true.
?- halt.
% cat info.dat # - # is Unix comment char, cat lists info.dat
thirsty(jack)
%


See also current input stream, input, output, files.



* append/1 is not related to append/3, which in turn has nothing to do with output streams.



# Strictly speaking, output.dat will be expected to be in the current working directory of the command interpreter that started Prolog. The command interpreter will be running on the workstation/computer, and sending output to a window on that workstation or computer.




 




cut, !


The cut, in Prolog, is a goal, written as !, which always succeeds, but cannot be backtracked past. It is used to prevent unwanted backtracking, for example, to prevent extra solutions being found by Prolog.

The cut should be used sparingly. There is a temptation to insert cuts experimentally into code that is not working correctly. If you do this, bear in mind that when debugging is complete, you should understand the effect of, and be able to explain the need for, every cut you use. The use of a cut should thus be commented.


 



Example: Suppose we have the following facts:











teaches(dr_fred, history).

teaches(dr_fred, english).

teaches(dr_fred, drama).

teaches(dr_fiona, physics).

         

studies(alice, english).

studies(angus, english).

studies(amelia, drama).

studies(alex, physics).


 



Then consider the following queries and their outputs:


?- teaches(dr_fred, Course), studies(Student, Course).

Course = english
Student = alice ;

Course = english
Student = angus ;

Course = drama
Student = amelia ;

false.


Backtracking is not inhibited here. Course is initially bound to history, but there are no students of history, so the second goals fails, backtracking occurs, Course is re-bound to english, the second goal is tried and two solutions found (alice and angus), then backtracking occurs again, and Course is bound to drama, and a final Student, amelia, is found.


?- teaches(dr_fred, Course), !, studies(Student, Course).

false.


This time Course is initially bound to history, then the cut goal is executed, and then studies goal is tried and fails (because nobody studieshistory). Because of the cut, we cannot backtrack to the teaches goal to find another binding for Course, so the whole query fails.


?- teaches(dr_fred, Course), studies(Student, Course), !.

Course = english
Student = alice ;

false.


Here the teaches goal is tried as usual, and Course is bound to history, again as usual. Next the studies goal is tried and fails, so we don't get to the cut at the end of the query at this point, and backtracking can occur. Thus the teaches goal is re-tried, and Course is bound to english. Then the studies goal is tried again, and succeeds, with Student = alice. After that, the cut goal is tried and of course succeeds, so no further backtracking is possible and only one solution is thus found.


?- !, teaches(dr_fred, Course), studies(Student, Course).

Course = english
Student = alice ;

Course = english
Student = angus ;

Course = drama
Student = amelia ;

false.
?-


In this final example, the same solutions are found as if no cut was present, because it is never necessary to backtrack past the cut to find the next solution, so backtracking is never inhibited.


Cuts in Rules


In practice, the cut is used in rules rather than in multi-goal queries, and some particular idioms apply in such cases. For example, consider the following code for max(X, Y, Max), which is supposed to bind Max to the larger of X and Y (which are assumed to be numbers).


max(X, Y, X) :- X > Y, !.
max(X, Y, Y).
This is a way of saying: "if the first rule succeeds, use it and don't try the second rule. (Otherwise, use the second rule.) We could instead have written:
max(X, Y, X) :- X > Y.
max(X, Y, Y) :- X =< Y.
in which case both rules will normally be tried (unless backtracking is prevented by a cut in some other part of the code). This is slightly less efficient if X is in fact greater than Y (unnecessary backtracking occurs) but easier for people to understand, though regular Prolog programmers rapidly get to recognise this type of idiom. The extra computation in the case of max is trivial, but in cases where the second rule involves a long computation, there might be a strong argument for using the cut on efficiency grounds.


 





 



 



debugging

means removing errors from program code. This is done by (1) observing the errors (i.e. testing); (2) locating the cause in the code; (3) correcting the errors.


 



(1) and (2) are often the hard part. Once you have observed or detected an error, tracing the code in question can help find the problem. Sometimes inserting calls to write into parts of the code where you think that the problem might be can help to localise the error.



Ultimately, reading your code carefully will be part of the task. So it would be a good idea to write the code carefully in the first place, in order to make it easy to understand. See also error and warning messages and commenting and white space.




 




declarative


A declarative programming language is one in which the relationships between the data are stated (or declared in a (usually logic-based) language, and then some automatic mechanism, e.g. a theorem-prover, is used to answer queries about the data. Prolog is a declarative programming language. Haskell, Miranda, Lisp, and Scheme are functional programming languages, in which all constructs are expressed using functions, function arguments, and function results, and C, C++, Java, Perl, Pascal, Modula-2, and Fortran are examples of (high-level) procedural programming languages, in which the program code expresses procedures to follow in manipulating the data.


 




 




dynamic


In Prolog, a procedure is either static or dynamic. A static procedure is one whose facts/rules are predefined at the start of execution, and do not change during execution. Normally, the facts/rules will be in a file of Prolog code which will be loaded during the Prolog session. Sometimes, you might want to add extra facts (or maybe even extra rules) to the procedure, during execution of a Prolog query, using assert/asserta/assertz", or maybe remove facts/rules using retract/retractall. To do this, you must declare the procedure to be dynamic.


 



You can declare a procedure to be dynamic by including in your code (normally adjacent to the facts/rules for that procedure) a suitabledynamic directive. Example - suppose you have a procedure called likes, with arity 2, and you have a "starter set" of facts/rules in your Prolog program, but you want to infer extra facts about likes during execution, and add them to the data base so that they don't need to be recomputed each time they are used. [You would normally only do this - add the new facts to the database - if the extra facts were slow to compute.] You need to declare likes (with arity 2) to be dynamic. You do this as follows:


:- dynamic likes/2.


[By the way, notice that this leaves open the possibility that a different version of likes (with arity 3, say) might not be dynamic.]



See also memoisation.  




  • comparison [kəm´pærisən] 移动到这儿单词发声  n.比较;对照;比喻   (初中英语单词)
  • operator [´ɔpəreitə] 移动到这儿单词发声  n.操作者;接线员   (初中英语单词)
  • briefly [´bri:fli] 移动到这儿单词发声  ad.简短地;简略地   (初中英语单词)
  • computer [kəm´pju:tə] 移动到这儿单词发声  n.计算机;电子计算器   (初中英语单词)
  • running [´rʌniŋ] 移动到这儿单词发声  a.奔跑的;流动的   (初中英语单词)
  • interpreter [in´tə:pritə] 移动到这儿单词发声  n.译员;解释者;翻译器   (初中英语单词)
  • contents [´kɔ:ntents] 移动到这儿单词发声  n.容纳物;要旨   (初中英语单词)
  • program [´prəugræm] 移动到这儿单词发声  n.说明v.为…安排节目   (初中英语单词)
  • relate [ri´leit] 移动到这儿单词发声  v.阐明;使联系;涉及   (初中英语单词)
  • convenient [kən´vi:niənt] 移动到这儿单词发声  a.方便的   (初中英语单词)
  • consult [kən´sʌlt] 移动到这儿单词发声  v.商量;磋商;请教   (初中英语单词)
  • surprising [sə´praiziŋ] 移动到这儿单词发声  a.惊人的;意外的   (初中英语单词)
  • stream [stri:m] 移动到这儿单词发声  n.河 vi.流出;飘扬   (初中英语单词)
  • working [´wə:kiŋ] 移动到这儿单词发声  a.工人的;劳动的   (初中英语单词)
  • reading [´ri:diŋ] 移动到这儿单词发声  n.(阅)读;朗读;读物   (初中英语单词)
  • output [´autput] 移动到这儿单词发声  n.产品;产品;计算结果   (初中英语单词)
  • writing [´raitiŋ] 移动到这儿单词发声  n.书写;写作;书法   (初中英语单词)
  • permission [pə´miʃən] 移动到这儿单词发声  n.允许;同意;许可   (初中英语单词)
  • comment [´kɔment] 移动到这儿单词发声  n.&v.评论;评注;注意   (初中英语单词)
  • solution [sə´lu:ʃən] 移动到这儿单词发声  n.解答;解决;溶解   (初中英语单词)
  • supposed [sə´pəuzd] 移动到这儿单词发声  a.想象的;假定的   (初中英语单词)
  • slightly [´slaitli] 移动到这儿单词发声  ad.轻微地;细长的   (初中英语单词)
  • argument [´ɑ:gjumənt] 移动到这儿单词发声  n.辩论;争论;论证   (初中英语单词)
  • scheme [ski:m] 移动到这儿单词发声  n.计划;阴谋,诡计   (初中英语单词)
  • function [´fʌŋkʃən] 移动到这儿单词发声  n.机能;职责 vi.活动   (初中英语单词)
  • suitable [´su:təbəl, ´sju:-] 移动到这儿单词发声  a.合适的,适当的   (初中英语单词)
  • possibility [,pɔsə´biliti] 移动到这儿单词发声  n.可能(性);希望;前途   (初中英语单词)
  • relevant [´relivənt] 移动到这儿单词发声  a.有关的;中肯的   (高中英语单词)
  • identical [ai´dentikəl] 移动到这儿单词发声  a.完全相同的   (高中英语单词)
  • equality [i´kwɔliti] 移动到这儿单词发声  n.同等,平等   (高中英语单词)
  • execution [,eksi´kju:ʃən] 移动到这儿单词发声  n.执行;演奏;表演   (高中英语单词)
  • subsequently [´sʌbsikwəntli] 移动到这儿单词发声  a.其次,接着   (高中英语单词)
  • subsequent [´sʌbsikwənt] 移动到这儿单词发声  a.其次的;其后的   (高中英语单词)
  • access [´ækses] 移动到这儿单词发声  n.接近;通路;进入   (高中英语单词)
  • related [ri´leitid] 移动到这儿单词发声  a.叙述的;有联系的   (高中英语单词)
  • strictly [´striktli] 移动到这儿单词发声  ad.严格地   (高中英语单词)
  • temptation [temp´teiʃən] 移动到这儿单词发声  n.引诱,诱惑(物)   (高中英语单词)
  • insert [in´sə:t, ´insə:t] 移动到这儿单词发声  vt.插入;夹入 n.插入物   (高中英语单词)
  • correctly [kə´rektli] 移动到这儿单词发声  ad.正确地;恰当地   (高中英语单词)
  • saying [´seiŋ, ´sei-iŋ] 移动到这儿单词发声  n.言语;言论;格言   (高中英语单词)
  • efficient [i´fiʃənt] 移动到这儿单词发声  a.有效的,有能力的   (高中英语单词)
  • efficiency [i´fiʃənsi] 移动到这儿单词发声  n.效力;效率;有能力   (高中英语单词)
  • automatic [,ɔ:tə´mætik] 移动到这儿单词发声  a.自动的 n.自动装置   (高中英语单词)
  • procedure [prə´si:dʒə] 移动到这儿单词发声  n.过程;手续;方法   (高中英语单词)
  • session [´seʃən] 移动到这儿单词发声  n.会议;会期;(开庭)期   (高中英语单词)
  • arithmetic [ə´riθmətik] 移动到这儿单词发声  n.算术   (英语四级单词)
  • approximate [ə´prɔksimit] 移动到这儿单词发声  a.近似的 v.接近   (英语四级单词)
  • trying [´traiiŋ] 移动到这儿单词发声  a.难堪的;费劲的   (英语四级单词)
  • terminal [´tə:minəl] 移动到这儿单词发声  n.终点(站) a.末端的   (英语四级单词)
  • console [kən´səul] 移动到这儿单词发声  vt.安慰;慰问   (英语四级单词)
  • binding [´baindiŋ] 移动到这儿单词发声  a.捆绑的 n.捆绑(物)   (英语四级单词)
  • trivial [´triviəl] 移动到这儿单词发声  a.琐碎的;不重要的   (英语四级单词)
  • warning [´wɔ:niŋ] 移动到这儿单词发声  n.警告;前兆 a.预告的   (英语四级单词)
  • mechanism [´mekənizəm] 移动到这儿单词发声  n.机械装置;机制   (英语四级单词)
  • adjacent [ə´dʒeisənt] 移动到这儿单词发声  a.接近的;紧接着的   (英语四级单词)
  • version [´və:ʃən, ´və:rʒən] 移动到这儿单词发声  n.翻译;说明;译本   (英语四级单词)
  • colouring [´kʌləriŋ] 移动到这儿单词发声  n.色彩;外貌;伪装   (英语六级单词)
  • novice [´nɔvis] 移动到这儿单词发声  n.新手;初学者   (英语六级单词)
  • variable [´veəriəbəl] 移动到这儿单词发声  a.易变的 n.可变量   (英语六级单词)
  • revert [ri´və:t] 移动到这儿单词发声  v.使颠倒;使回转   (英语六级单词)
  • speaking [´spi:kiŋ] 移动到这儿单词发声  n.说话 a.发言的   (英语六级单词)
  • normally [´nɔ:məli] 移动到这儿单词发声  ad.正常情况下;通常   (英语六级单词)


  • 文章总共2页
    文章标签:词典  

    章节正文