home *** CD-ROM | disk | FTP | other *** search
/ rtsi.com / 2014.01.www.rtsi.com.tar / www.rtsi.com / OS9 / OSK / EFFO / pd3.lzh / SBPROLOG2.2 / DOC / sbprolog_doc.readable < prev   
Text File  |  1991-08-10  |  183KB  |  10,949 lines

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
  10.  
  11.  
  12.  
  13.  
  14.  
  15.                                      The SB-Prolog System, Version 2.2
  16.  
  17.                                                A User Manual
  18.  
  19.                                                  edited by
  20.                                               _S_a_u_m_y_a _K. _D_e_b_r_a_y
  21.  
  22.                                               from material by
  23.  
  24.                                              _D_a_v_i_d _S_c_o_t_t _W_a_r_r_e_n
  25.                                               _S_u_z_a_n_n_e _D_i_e_t_r_i_c_h
  26.                                             SUNY at Stony Brook
  27.  
  28.                                               _F_e_r_n_a_n_d_o _P_e_r_e_i_r_a
  29.                                              SRI International
  30.  
  31.  
  32.  
  33.                                                  _M_a_r_c_h _1_9_8_7
  34.  
  35.  
  36.                                        Department of Computer Science
  37.                                            University of Arizona
  38.                                               Tucson, AZ 85721
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.  
  53.  
  54.  
  55.  
  56.  
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78.  
  79.  
  80.  
  81.  
  82.                                      The SB-Prolog System, Version 2.2
  83.  
  84.                                                A User Manual
  85.  
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.           Abstract: SB-Prolog is a public-domain Prolog system for Unix|- based systems  originally
  97.  
  98.  
  99.  
  100.           developed at SUNY, Stony Brook.  The core of the system is an emulator, written in C for
  101.  
  102.  
  103.  
  104.           portability, of a Prolog virtual machine that is an extension  of  the  Warren  Abstract
  105.  
  106.  
  107.  
  108.           Machine.   The remainder of the system, including the translator from Prolog to the vir-
  109.  
  110.  
  111.  
  112.           tual machine instructions, is written in Prolog.  Parts of this manual, specifically the
  113.  
  114.  
  115.  
  116.           sections  on Prolog syntax and descriptions of some of the builtins, are based on the C-
  117.  
  118.  
  119.  
  120.           Prolog User Manual by Fernando Pereira.
  121.           ____________________
  122.              |- Unix is a trademark of AT&T.
  123.  
  124.  
  125.  
  126.  
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
  139.  
  140.           _1.  _I_n_t_r_o_d_u_c_t_i_o_n
  141.  
  142.  
  143.  
  144.  
  145.  
  146.  
  147.                SB-Prolog is a public domain Prolog system based on  an  extension  of  the  Warren
  148.  
  149.  
  150.  
  151.           Abstract Machine[1].  The WAM simulator is written in C to enhance portability.   Prolog
  152.  
  153.  
  154.  
  155.           source  programs  can  be  compiled into _b_y_t_e _c_o_d_e files, which contain encodings of WAM
  156.  
  157.  
  158.  
  159.           instructions and are interpreted by the simulator.  Programs can also be interpreted via
  160.  
  161.  
  162.  
  163.           _c_o_n_s_u_l_t.
  164.  
  165.  
  166.  
  167.  
  168.                SB-Prolog offers several features  that  are  not  found  on  most  Prolog  systems
  169.  
  170.  
  171.  
  172.           currently  available.   These  include:  compilation to object files; dynamic loading of
  173.  
  174.  
  175.  
  176.           predicates; provision for generating executable code on the global stack, which  can  be
  177.  
  178.  
  179.  
  180.           later  be  reclaimed; an _e_x_t_e_n_s_i_o_n _t_a_b_l_e facility that permits memoization of relations.
  181.  
  182.  
  183.  
  184.           Other features include full integration between compiled and  interpreted  code,  and  a
  185.  
  186.  
  187.           ____________________
  188.              [1] D. H. D. Warren, ``An Abstract Prolog Instruction  Set'',  Tech.  Note  309,  SRI
  189.           International, 1983.
  190.  
  191.  
  192.  
  193.  
  194.  
  195.                                                      2
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.           facility for the definition and expansion of macros that is fully  compatible  with  the
  207.  
  208.  
  209.  
  210.           runtime system.
  211.  
  212.  
  213.  
  214.  
  215.                The following features are absent: we expect to incorporate  them  in  future  ver-
  216.  
  217.  
  218.  
  219.           sions.
  220.  
  221.  
  222.  
  223.  
  224.                A ``listing'' feature, which produces a listing of clauses in the system database.
  225.  
  226.  
  227.  
  228.  
  229.                A garbage collector for the global stack.
  230.  
  231.  
  232.  
  233.  
  234.                The _r_e_c_o_r_d/_r_e_c_o_r_d_e_d/_e_r_a_s_e  and  the  _c_u_r_r_e_n_t__a_t_o_m/_c_u_r_r_e_n_t__f_u_n_c_t_o_r/_c_u_r_r_e_n_t__p_r_e_d_i_c_a_t_e
  235.  
  236.  
  237.  
  238.                facilities of C-Prolog.
  239.  
  240.  
  241.  
  242.  
  243.                In addition, it should be mentioned that while interpreted and compiled code behave
  244.  
  245.  
  246.  
  247.           similarly  in  almost  all  cases, there are a few cases that we are aware of where they
  248.  
  249.  
  250.  
  251.           behave differently:
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.                                                      3
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.           (1)  In cuts buried within _i_f-_t_h_e_n-_e_l_s_es (->): such cuts are treated as ``hard'' cuts in
  273.  
  274.  
  275.  
  276.                interpreted code (i.e. cuts away alternative clauses as well), but as ``soft'' cuts
  277.  
  278.  
  279.  
  280.                in compiled code (i.e. cuts as far back as the head of the clause, but does not cut
  281.  
  282.  
  283.  
  284.                away alternative clauses).
  285.  
  286.  
  287.  
  288.  
  289.           (2)  In the evaluation  of  arithmetic  expressions  involving  compound  subexpressions
  290.  
  291.  
  292.  
  293.                created  dynamically: in compiled code, this cannot be handled using is/2, and must
  294.  
  295.  
  296.  
  297.                be handled using eval/2 (see the section on Arithmetic predicates), while in inter-
  298.  
  299.  
  300.  
  301.                preted code either is/2 or eval/2 is acceptable.
  302.  
  303.  
  304.  
  305.  
  306.  
  307.  
  308.  
  309.  
  310.  
  311.  
  312.  
  313.  
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322.  
  323.  
  324.  
  325.  
  326.  
  327.                                                      4
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.  
  335.  
  336.  
  337.  
  338.           _2.  _G_e_t_t_i_n_g _S_t_a_r_t_e_d
  339.  
  340.  
  341.  
  342.  
  343.  
  344.  
  345.                This section is intended to give a broad overview of the SB-Prolog system, so as to
  346.  
  347.  
  348.  
  349.           enable  the  new  user  to  begin using the system with a minimum of delay.  Many of the
  350.  
  351.  
  352.  
  353.           topics touched on here are covered in greater depth in later sections.
  354.  
  355.  
  356.  
  357.  
  358.           _2._1.  _T_h_e _D_y_n_a_m_i_c _L_o_a_d_e_r _S_e_a_r_c_h _P_a_t_h
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.                In SB-Prolog, it is not necessary for the user to load all the predicates necessary
  366.  
  367.  
  368.  
  369.           to execute a program.  Instead, if an undefined predicate _f_o_o is encountered during exe-
  370.  
  371.  
  372.  
  373.           cution, the system searches the  user's  directories  in  the  order  specified  by  the
  374.  
  375.  
  376.  
  377.           environment variable SIMPATH until it finds a directory containing a file _f_o_o whose name
  378.  
  379.  
  380.  
  381.           is that of the undefined predicate.  It then dynamically loads and links  the  file  _f_o_o
  382.  
  383.  
  384.  
  385.           (which  is  expected  to  be a byte code file defining the predicate _f_o_o), and continues
  386.  
  387.  
  388.  
  389.           with execution; if no such file can be found, an error message is  given  and  execution
  390.  
  391.  
  392.  
  393.                                                      5
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.  
  401.  
  402.  
  403.  
  404.           fails.  This feature makes it unnecessary for the user to have to explicitly link in all
  405.  
  406.  
  407.  
  408.           the predicates that might be necessary in a  program:  instead,  only  those  files  are
  409.  
  410.  
  411.  
  412.           loaded  which  are necessary to have the program execute.  This can significantly reduce
  413.  
  414.  
  415.  
  416.           the memory requirements of programs.
  417.  
  418.  
  419.  
  420.  
  421.                The key to this dynamic search-and-load behaviour is the SIMPATH environment  vari-
  422.  
  423.  
  424.  
  425.           able,  which specifies the order in which directories are to be searched.  It may be set
  426.  
  427.  
  428.  
  429.           by adding the following line to the user's ._c_s_h_r_c file:
  430.  
  431.  
  432.  
  433.  
  434.  
  435.                       setenv SIMPATH _p_a_t_h
  436.  
  437.  
  438.  
  439.  
  440.  
  441.  
  442.           where _p_a_t_h is a sequence of directory names separated by colons:
  443.  
  444.  
  445.  
  446.  
  447.  
  448.                       _d_i_r<1>:_d_i_r<2>: ... :_d_i_r<_n>
  449.  
  450.  
  451.  
  452.  
  453.  
  454.  
  455.           and _d_i_r<_i> are _f_u_l_l _p_a_t_h _n_a_m_e_s to the respective directories.   For  example,  executing
  456.  
  457.  
  458.  
  459.                                                      6
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470.           the command
  471.  
  472.  
  473.  
  474.                       setenv SIMPATH .:$HOME/prolog/modlib:$HOME/prolog/lib
  475.  
  476.  
  477.  
  478.  
  479.  
  480.  
  481.           sets the search order for undefined predicates to the following: first, the directory in
  482.  
  483.  
  484.  
  485.           which the program is executing is searched; if the appropriate file is not found in this
  486.  
  487.  
  488.  
  489.           directory, the directories searched are, in order, ~/prolog/modlib and ~/prolog/lib.  If
  490.  
  491.  
  492.  
  493.           the appropriate file is not found in any of these directories, the system gives an error
  494.  
  495.  
  496.  
  497.           message and execution fails.
  498.  
  499.  
  500.  
  501.  
  502.                The beginning user is advised to include the system directories (listed in the next
  503.  
  504.  
  505.  
  506.           section) in his SIMPATH, in order to be able to access the system libraries (see below).
  507.  
  508.  
  509.  
  510.  
  511.           _2._2.  _S_y_s_t_e_m _D_i_r_e_c_t_o_r_i_e_s
  512.  
  513.  
  514.  
  515.  
  516.  
  517.  
  518.  
  519.  
  520.  
  521.  
  522.  
  523.  
  524.  
  525.                                                      7
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.  
  533.  
  534.  
  535.  
  536.                There are four basic system directories: cmplib, lib, modlib and sim.  _c_m_p_l_i_b  con-
  537.  
  538.  
  539.  
  540.           tains  the Prolog to byte code translator; _l_i_b and _m_o_d_l_i_b contain library routines.  The
  541.  
  542.  
  543.  
  544.           _s_r_c subdirectory in each of these contains the  corresponding  Prolog  source  programs.
  545.  
  546.  
  547.  
  548.           The directory _s_i_m contains the simulator, the subdirectory _b_u_i_l_t_i_n contains code for the
  549.  
  550.  
  551.  
  552.           builtin predicates of the system.
  553.  
  554.  
  555.  
  556.  
  557.                It is recommended that the beginning user include the  system  directories  in  his
  558.  
  559.  
  560.  
  561.           SIMPATH, by setting SIMPATH to
  562.  
  563.  
  564.  
  565.                                        .:_S_B_P/modlib:_S_B_P/lib:_S_B_P/cmplib
  566.  
  567.  
  568.  
  569.  
  570.           where _S_B_P denotes the path to the root of the SB-Prolog system directories.
  571.  
  572.  
  573.  
  574.  
  575.           _2._3.  _I_n_v_o_k_i_n_g _t_h_e _S_i_m_u_l_a_t_o_r
  576.  
  577.  
  578.  
  579.  
  580.  
  581.  
  582.                The simulator is invoked by the command
  583.  
  584.  
  585.  
  586.                       sbprolog_b_c__f_i_l_e
  587.  
  588.  
  589.  
  590.  
  591.                                                      8
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.  
  599.  
  600.  
  601.  
  602.           where _b_c__f_i_l_e is a byte code file resulting from the compilation of  a  Prolog  program.
  603.  
  604.  
  605.  
  606.           In  almost all cases, the user will wish to interact with the SB-Prolog _q_u_e_r_y _e_v_a_l_u_a_t_o_r,
  607.  
  608.  
  609.  
  610.           in which case _b_c__f_i_l_e will be $_r_e_a_d_l_o_o_p, and the command will be
  611.  
  612.  
  613.  
  614.  
  615.  
  616.                        sbprolog /
  617.                                 _\ path \
  618.                                        //\$readloop
  619.  
  620.  
  621.  
  622.  
  623.  
  624.  
  625.           where /
  626.                 _\_p_a_t_h\
  627.                      / is the path to the directory containing the command interpreter  $_r_e_a_d_l_o_o_p.
  628.  
  629.  
  630.  
  631.           This directory, typically, is _m_o_d_l_i_b (see Section 2.2 above).
  632.  
  633.  
  634.  
  635.  
  636.                The command interpreter reads in a query typed in by the  user,  evaluates  it  and
  637.  
  638.  
  639.  
  640.           prints  the  answer(s),  repeating this until it encounters an end-of-file (the standard
  641.  
  642.  
  643.  
  644.           end-of-file character on the system, e.g. ctrl-D), or the user types in  _e_n_d__o_f__f_i_l_e  or
  645.  
  646.  
  647.  
  648.           _h_a_l_t.
  649.  
  650.  
  651.  
  652.  
  653.                The user should ensure that the the directory containing the  executable  file  _s_i_m
  654.  
  655.  
  656.  
  657.           (typically,  the  system  directory _s_i_m: see Section 2.2 above) is included in the shell
  658.  
  659.  
  660.  
  661.                                                      9
  662.  
  663.  
  664.  
  665.  
  666.  
  667.  
  668.  
  669.  
  670.  
  671.  
  672.           variable _p_a_t_h; if not, the full path to the simulator will have to be specified.
  673.  
  674.  
  675.  
  676.  
  677.                In general, the simulator may be invoked with a variety of options, as follows:
  678.  
  679.  
  680.  
  681.                        sbprolog-_o_p_t_i_o_n_s _b_c__f_i_l_e
  682.               or
  683.                        sbprolog -_o_p_t_i_o_n<1> -_o_p_t_i_o_n<2> ... -_o_p_t_i_o_n<_n> _b_c__f_i_l_e
  684.  
  685.  
  686.  
  687.  
  688.           The options recognized by the simulator are described in Section 4.2.
  689.  
  690.  
  691.  
  692.  
  693.                When called with a byte code file _b_c__f_i_l_e, the simulator begins execution with  the
  694.  
  695.  
  696.  
  697.           first  clause  in  that  file.   The first clause in such a file, therefore, should be a
  698.  
  699.  
  700.  
  701.           clause without any arguments in the head  (otherwise,  the  simulator  will  attempt  to
  702.  
  703.  
  704.  
  705.           dereference  argument pointers in the head that are really pointing into deep space, and
  706.  
  707.  
  708.  
  709.           usually come to a sad end).  If the user is executing a file in this manner rather  than
  710.  
  711.  
  712.  
  713.           using the command interpreter, he should also be careful to include the undefined predi-
  714.  
  715.  
  716.  
  717.           cate  handler  `_$_u_n_d_e_f_i_n_e_d__p_r_e_d'/1,   which   is   normally   defined   in   the   file
  718.  
  719.  
  720.  
  721.           _m_o_d_l_i_b/$_i_n_i_t__s_y_s._P.
  722.  
  723.  
  724.  
  725.  
  726.  
  727.                                                      10
  728.  
  729.  
  730.  
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.           _2._4.  _E_x_e_c_u_t_i_n_g _P_r_o_g_r_a_m_s
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.                There are two ways of executing a program: a source file may  be  compiled  into  a
  746.  
  747.  
  748.  
  749.           byte-code file, which can then be loaded and executed; or, the source file may be inter-
  750.  
  751.  
  752.  
  753.           preted via _c_o_n_s_u_l_t.  The system supports full integration of  compiled  and  interpreted
  754.  
  755.  
  756.  
  757.           code,  so  that some predicates of a program may be compiled, while others may be inter-
  758.  
  759.  
  760.  
  761.           preted.  However, the unit of compilation or consulting remains the file.  The remainder
  762.  
  763.  
  764.  
  765.           of this section describes each of these procedures in more detail.
  766.  
  767.  
  768.  
  769.  
  770.           _2._4._1.  _C_o_m_p_i_l_i_n_g _P_r_o_g_r_a_m_s
  771.  
  772.  
  773.  
  774.  
  775.  
  776.  
  777.                The compiler is invoked through the Prolog predicate _c_o_m_p_i_l_e.  It translates Prolog
  778.  
  779.  
  780.  
  781.           source programs into byte code that can then be executed on the simulator.  The compiler
  782.  
  783.  
  784.  
  785.           may be invoked as follows:
  786.  
  787.  
  788.  
  789.  
  790.  
  791.  
  792.  
  793.                                                      11
  794.  
  795.  
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.                       | ?- compile(_I_n_F_i_l_e [, _O_u_t_F_i_l_e ] [, _O_p_t_i_o_n_s_L_i_s_t ]).
  804.               or
  805.                       | ?- compile(_I_n_F_i_l_e, _O_u_t_F_i_l_e, _O_p_t_i_o_n_s_L_i_s_t, _P_r_e_d_L_i_s_t).
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.           where optional parameters are enclosed in brackets.  _I_n_F_i_l_e is the  name  of  the  input
  813.  
  814.  
  815.  
  816.           (i.e.  source)  file;  _O_u_t_F_i_l_e  is  the  name  of the output file (i.e. byte code) file;
  817.  
  818.  
  819.  
  820.           _O_p_t_i_o_n_s_L_i_s_t is a list of compiler options, and _P_r_e_d_L_i_s_t is a list of terms _P/_N  denoting
  821.  
  822.  
  823.  
  824.           the predicates defined in _I_n_F_i_l_e, where _P is a predicate name and _N its arity.
  825.  
  826.  
  827.  
  828.  
  829.                The input and output file names must be Prolog atoms,  i.e.  either  begin  with  a
  830.  
  831.  
  832.  
  833.           lower case letter and consist only of letters, digits, dollar signs and underscores; or,
  834.  
  835.  
  836.  
  837.           be enclosed within single quotes.  If the output file name is not specified, it defaults
  838.  
  839.  
  840.  
  841.           to  _I_n_F_i_l_e.out.  The list of options, if specified, is a Prolog list, i.e. a term of the
  842.  
  843.  
  844.  
  845.           form
  846.  
  847.  
  848.  
  849.  
  850.  
  851.                       [ _o_p_t_i_o_n<1>, _o_p_t_i_o_n<2>, ..., _o_p_t_i_o_n<_n> ].
  852.  
  853.  
  854.  
  855.  
  856.  
  857.  
  858.  
  859.                                                      12
  860.  
  861.  
  862.  
  863.  
  864.  
  865.  
  866.  
  867.  
  868.  
  869.  
  870.           If left unspecified, it defaults to the empty list [].  _P_r_e_d_L_i_s_t, if specified, is  usu-
  871.  
  872.  
  873.  
  874.           ally  given as an uninstantiated variable; its principal use is for setting trace points
  875.  
  876.  
  877.  
  878.           on the predicates in the file (see Sections 6 and 8).  Notice  that  _P_r_e_d_L_i_s_t  can  only
  879.  
  880.  
  881.  
  882.           appear in _c_o_m_p_i_l_e/4.
  883.  
  884.  
  885.  
  886.  
  887.                A list of compiler options appears in Section 8.3.
  888.  
  889.  
  890.  
  891.  
  892.           _2._4._2.  _L_o_a_d_i_n_g _B_y_t_e _C_o_d_e _F_i_l_e_s
  893.  
  894.  
  895.  
  896.  
  897.  
  898.  
  899.           Byte code files may be loaded into the simulator using the predicate _l_o_a_d:
  900.  
  901.  
  902.  
  903.  
  904.  
  905.                       | ?- load(_B_y_t_e_C_o_d_e__F_i_l_e).
  906.  
  907.  
  908.  
  909.  
  910.  
  911.  
  912.           where _B_y_t_e_C_o_d_e__F_i_l_e is a Prolog atom (see Section 3.1) that is the name of a  byte  code
  913.  
  914.  
  915.  
  916.           file.
  917.  
  918.  
  919.  
  920.  
  921.  
  922.  
  923.  
  924.  
  925.                                                      13
  926.  
  927.  
  928.  
  929.  
  930.  
  931.  
  932.  
  933.  
  934.  
  935.  
  936.                The _l_o_a_d predicate invokes the dynamic loader, which carries out a search according
  937.  
  938.  
  939.  
  940.           to  the sequence specified by the environment variable SIMPATH (see Section 2.1).  It is
  941.  
  942.  
  943.  
  944.           therefore not necessary to always specify the full path name to the file to be loaded.
  945.  
  946.  
  947.  
  948.  
  949.                Byte code files may be concatenated together to  produce  other  byte  code  files.
  950.  
  951.  
  952.  
  953.           Thus,  for  example, if _f_o_o_1 and _f_o_o_2 are byte code files resulting from the compilation
  954.  
  955.  
  956.  
  957.           of two Prolog source programs, then the file _f_o_o, obtained by executing the  shell  com-
  958.  
  959.  
  960.  
  961.           mand
  962.  
  963.  
  964.  
  965.                        cat foo1 foo2 > foo
  966.  
  967.  
  968.  
  969.  
  970.           is a byte code file as well, and may be loaded and executed.  In this case, loading  and
  971.  
  972.  
  973.  
  974.           executing  the  file _f_o_o would give the same result as loading _f_o_o_1 and _f_o_o_2 separately,
  975.  
  976.  
  977.  
  978.           which in turn would be the same as concatenating the original source files and compiling
  979.  
  980.  
  981.  
  982.           this  larger  file.  This makes it easier to compile large programs: one need only break
  983.  
  984.  
  985.  
  986.           them into smaller pieces, compile the individual pieces, and concatenate  the  resulting
  987.  
  988.  
  989.  
  990.  
  991.                                                      14
  992.  
  993.  
  994.  
  995.  
  996.  
  997.  
  998.  
  999.  
  1000.  
  1001.  
  1002.           byte code files together.
  1003.  
  1004.  
  1005.  
  1006.  
  1007.           _2._4._3.  _C_o_n_s_u_l_t_i_n_g _P_r_o_g_r_a_m_s
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.                Instead of compiling a file to generate a byte code  file  which  then  has  to  be
  1015.  
  1016.  
  1017.  
  1018.           loaded,  a  program  may  be executed interpretively by ``consulting'' the corresponding
  1019.  
  1020.  
  1021.  
  1022.           source file:
  1023.  
  1024.  
  1025.  
  1026.                       | ?- consult(_S_o_u_r_c_e_F_i_l_e [, _O_p_t_i_o_n_L_i_s_t ] ).
  1027.               or
  1028.                       | ?- consult(_S_o_u_r_c_e_F_i_l_e, _O_p_t_i_o_n_L_i_s_t, _P_r_e_d_L_i_s_t).
  1029.  
  1030.  
  1031.  
  1032.  
  1033.           where _S_o_u_r_c_e_F_i_l_e is a Prolog atom which is the name of a file containing a Prolog source
  1034.  
  1035.  
  1036.  
  1037.           program;  _O_p_t_i_o_n_L_i_s_t  is  a  list of options to consult; and _P_r_e_d_L_i_s_t is a list of terms
  1038.  
  1039.  
  1040.  
  1041.           _P/_N, where _P is a predicate name and _N its arity, specifying which predicates have  been
  1042.  
  1043.  
  1044.  
  1045.           consulted  from  _S_o_u_r_c_e_F_i_l_e; its principal use is for setting trace points on the predi-
  1046.  
  1047.  
  1048.  
  1049.           cates in the file (see Section 6).  Notice that _P_r_e_d_L_i_s_t can only appear in _c_o_n_s_u_l_t/3.
  1050.  
  1051.  
  1052.  
  1053.  
  1054.  
  1055.  
  1056.  
  1057.                                                      15
  1058.  
  1059.  
  1060.  
  1061.  
  1062.  
  1063.  
  1064.  
  1065.  
  1066.  
  1067.  
  1068.                At this point, the options recognized for _c_o_n_s_u_l_t are the following:
  1069.  
  1070.  
  1071.  
  1072.  
  1073.           first
  1074.  
  1075.  
  1076.  
  1077.  
  1078.                If on, causes each clause read in to be added as the first clause of  the  database
  1079.  
  1080.  
  1081.  
  1082.                (so  that  effectively,  the  clauses  appear in the reverse order as in the source
  1083.  
  1084.  
  1085.  
  1086.                file).  Default: off.
  1087.  
  1088.  
  1089.  
  1090.  
  1091.           index
  1092.  
  1093.  
  1094.  
  1095.  
  1096.                If on, causes an index to be generated on the first  argument  of  each  predicate.
  1097.  
  1098.  
  1099.  
  1100.                Default: off.
  1101.  
  1102.  
  1103.  
  1104.  
  1105.           index(N)
  1106.  
  1107.  
  1108.  
  1109.                If on, causes an index to be generated on the N[th]  argument  of  each  predicate.
  1110.  
  1111.  
  1112.  
  1113.                Default: off.
  1114.  
  1115.  
  1116.  
  1117.  
  1118.  
  1119.  
  1120.  
  1121.  
  1122.  
  1123.                                                      16
  1124.  
  1125.  
  1126.  
  1127.  
  1128.  
  1129.  
  1130.  
  1131.  
  1132.  
  1133.  
  1134.           q    ``quick''.  If on, invokes a consultation algorithm that is simpler and more  effi-
  1135.  
  1136.  
  1137.  
  1138.                cient than the general one.  However, the code generated with the simpler algorithm
  1139.  
  1140.  
  1141.  
  1142.                may not be correct if there are repeated variables within  compound  terms  in  the
  1143.  
  1144.  
  1145.  
  1146.                body of the clause, e.g. in
  1147.  
  1148.  
  1149.  
  1150.                            p(X) :- q([X, X]).
  1151.  
  1152.  
  1153.  
  1154.  
  1155.                Default: off.
  1156.  
  1157.  
  1158.  
  1159.  
  1160.           t    ``trace''.  Causes a trace point to be set on any predicate  in  the  current  file
  1161.  
  1162.  
  1163.  
  1164.                that does not already have a trace point set.
  1165.  
  1166.  
  1167.  
  1168.  
  1169.           v    ``verbose''.  Causes information regarding which predicates have been consulted  to
  1170.  
  1171.  
  1172.  
  1173.                be printed out. Default: off.
  1174.  
  1175.  
  1176.  
  1177.  
  1178.           In addition to the above, the options specified for the macro expander are  also  recog-
  1179.  
  1180.  
  1181.  
  1182.           nized (see Section 10)).
  1183.  
  1184.  
  1185.  
  1186.  
  1187.  
  1188.  
  1189.                                                      17
  1190.  
  1191.  
  1192.  
  1193.  
  1194.  
  1195.  
  1196.  
  1197.  
  1198.  
  1199.  
  1200.                It is important to note that SB-Prolog's _c_o_n_s_u_l_t predicate is similar  to  that  of
  1201.  
  1202.  
  1203.  
  1204.           Quintus  Prolog,  and behaves like C-Prolog's _r_e_c_o_n_s_u_l_t.  This means that if a predicate
  1205.  
  1206.  
  1207.  
  1208.           is defined across two or more files, consulting them will result in only the clauses  in
  1209.  
  1210.  
  1211.  
  1212.           the file consulted last being used.
  1213.  
  1214.  
  1215.  
  1216.  
  1217.           _2._5.  _E_x_e_c_u_t_i_o_n _D_i_r_e_c_t_i_v_e_s
  1218.  
  1219.  
  1220.  
  1221.  
  1222.  
  1223.  
  1224.                Execution directives may be specified to _c_o_m_p_i_l_e and _c_o_n_s_u_l_t through :-/1.  If,  in
  1225.  
  1226.  
  1227.  
  1228.           the  read  phase  of  _c_o_m_p_i_l_e or _c_o_n_s_u_l_t, a term with principal functor :-/1 is read in,
  1229.  
  1230.  
  1231.  
  1232.           this term is executed directly via _c_a_l_l/1.  This enables the user to dynamically  modify
  1233.  
  1234.  
  1235.  
  1236.           the environment, e.g. via _o_p declarations (see Section 3.2), _a_s_s_e_r_ts etc.
  1237.  
  1238.  
  1239.  
  1240.  
  1241.                A point to note is that if the environment is modified as a result of an  execution
  1242.  
  1243.  
  1244.  
  1245.           directive, the modifications are visible only in that environment.  This means that con-
  1246.  
  1247.  
  1248.  
  1249.           sulted code, which runs in the environment in which the source program is read  in  (and
  1250.  
  1251.  
  1252.  
  1253.  
  1254.  
  1255.                                                      18
  1256.  
  1257.  
  1258.  
  1259.  
  1260.  
  1261.  
  1262.  
  1263.  
  1264.  
  1265.  
  1266.           which  is  modified  by  such  execution  directives) feel the effects of such execution
  1267.  
  1268.  
  1269.  
  1270.           directives.  However, byte code resulting from compilation, which, in general,  executes
  1271.  
  1272.  
  1273.  
  1274.           in an environment different from that in which the source was compiled, does not inherit
  1275.  
  1276.  
  1277.  
  1278.           the effects of such directives.  Thus, an _o_p declaration can be used in a source file to
  1279.  
  1280.  
  1281.  
  1282.           change  the  syntax and allow the remainder of the program to be parsed according to the
  1283.  
  1284.  
  1285.  
  1286.           modified syntax; however, these modifications will not, in general, manifest  themselves
  1287.  
  1288.  
  1289.  
  1290.           if  the  byte  code  is executed in another environment.  Of course, if the byte code is
  1291.  
  1292.  
  1293.  
  1294.           loaded into the same environment as that in which the source program was compiled,  e.g.
  1295.  
  1296.  
  1297.  
  1298.           through
  1299.  
  1300.  
  1301.  
  1302.                       | ?- compile(foo, bar), load(bar).
  1303.  
  1304.  
  1305.  
  1306.  
  1307.           the effects of execution directives will continue to be felt.
  1308.  
  1309.  
  1310.  
  1311.  
  1312.  
  1313.  
  1314.  
  1315.  
  1316.  
  1317.  
  1318.  
  1319.  
  1320.  
  1321.                                                      19
  1322.  
  1323.  
  1324.  
  1325.  
  1326.  
  1327.  
  1328.  
  1329.  
  1330.  
  1331.  
  1332.           _3.  _S_y_n_t_a_x
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.           _3._1.  _T_e_r_m_s
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345.  
  1346.                The syntax of SB-Prolog is by and large compatible with that of C-Prolog.  The data
  1347.  
  1348.  
  1349.  
  1350.           objects of the language are called  _t_e_r_ms.  A  term  is either a _c_o_n_s_t_a_n_t, a _v_a_r_i_a_b_l_e or
  1351.  
  1352.  
  1353.  
  1354.           a _c_o_m_p_o_u_n_d _t_e_r_m.  Constants can be _i_n_t_e_g_e_r_s or _a_t_o_m_s.  The symbol for an atom must begin
  1355.  
  1356.  
  1357.  
  1358.           with  a  lower  case  letter or the dollar sign $, and consist of any number of letters,
  1359.  
  1360.  
  1361.  
  1362.           digits, underscores and dollar signs; if it contains any character other than these,  it
  1363.  
  1364.  
  1365.  
  1366.           must be enclosed within single quotes.[2] As in other  programming languages,  constants
  1367.  
  1368.  
  1369.  
  1370.           are definite elementary objects.
  1371.  
  1372.  
  1373.  
  1374.  
  1375.  
  1376.  
  1377.  
  1378.           ____________________
  1379.              [2] Users are advised against using symbols beginning with `$' or `_$',  however,  in
  1380.           order to minimize the possibility of conflicts with symbols internal to the system.
  1381.  
  1382.  
  1383.  
  1384.  
  1385.  
  1386.  
  1387.                                                      20
  1388.  
  1389.  
  1390.  
  1391.  
  1392.  
  1393.  
  1394.  
  1395.  
  1396.  
  1397.  
  1398.                Variables are distinguished by an initial capital  letter  or  by the initial char-
  1399.  
  1400.  
  1401.  
  1402.           acter "_", for example
  1403.  
  1404.  
  1405.  
  1406.               X   Value   A   A1   _3   _RESULT   _result
  1407.  
  1408.  
  1409.  
  1410.  
  1411.           If a variable is only referred to once, it does not need to   be   named  and   may   be
  1412.  
  1413.  
  1414.  
  1415.           written as an _a_n_o_n_y_m_o_u_s variable, indicated by the underline character "_".
  1416.  
  1417.  
  1418.  
  1419.  
  1420.                A variable should be thought of as  standing  for  some  definite  but unidentified
  1421.  
  1422.  
  1423.  
  1424.           object.   A variable  is  not  simply  a  writeable storage  location  as  in  most pro-
  1425.  
  1426.  
  1427.  
  1428.           gramming languages;  rather it is a local name for some data object, cf.   the  variable
  1429.  
  1430.  
  1431.  
  1432.           of  pure  LISP  and constant declarations in Pascal.
  1433.  
  1434.  
  1435.  
  1436.  
  1437.                The structured data objects of the language are the  compound  terms.   A  compound
  1438.  
  1439.  
  1440.  
  1441.           term  comprises  a  _f_u_n_c_t_o_r (called the _p_r_i_n_c_i_p_a_l functor of the term) and a sequence of
  1442.  
  1443.  
  1444.  
  1445.           one or more terms called _a_r_g_u_m_e_n_t_s.  A functor is characterised by its _n_a_m_e, which is an
  1446.  
  1447.  
  1448.  
  1449.           atom, and its _a_r_i_t_y or number of arguments.  For example the compound term whose functor
  1450.  
  1451.  
  1452.  
  1453.                                                      21
  1454.  
  1455.  
  1456.  
  1457.  
  1458.  
  1459.  
  1460.  
  1461.  
  1462.  
  1463.  
  1464.           is named `point' of arity 3, with arguments X, Y and Z, is written
  1465.  
  1466.  
  1467.  
  1468.               point(X,Y,Z)
  1469.  
  1470.  
  1471.  
  1472.  
  1473.           An atom is considered to be a functor of arity 0.
  1474.  
  1475.  
  1476.  
  1477.  
  1478.                A functor or predicate symbol is uniquely identified by  its  name  and  arity  (in
  1479.  
  1480.  
  1481.  
  1482.           other  words, it is possible for different symbols having different arities to share the
  1483.  
  1484.  
  1485.  
  1486.           same name).  A functor or predicate symbol _p with arity _n is usually written _p/_n.
  1487.  
  1488.  
  1489.  
  1490.  
  1491.                One  may  think  of  a  functor  as  a record type and the arguments of a  compound
  1492.  
  1493.  
  1494.  
  1495.           term  as  the   fields   of  a  record.   Compound terms are usefully pictured as trees.
  1496.  
  1497.  
  1498.  
  1499.           For example, the term
  1500.  
  1501.  
  1502.  
  1503.               s(np(john),vp(v(likes),np(mary)))
  1504.  
  1505.  
  1506.  
  1507.  
  1508.           would be pictured as the structure
  1509.  
  1510.  
  1511.  
  1512.  
  1513.  
  1514.  
  1515.  
  1516.  
  1517.  
  1518.  
  1519.                                                      22
  1520.  
  1521.  
  1522.  
  1523.  
  1524.  
  1525.  
  1526.  
  1527.  
  1528.  
  1529.                                                          s
  1530.                                                       /   \
  1531.                                                  np      vp
  1532.                                                   |      /  \
  1533.                                               john    v     np
  1534.                                                        |     |
  1535.                                                     likes  mary
  1536.  
  1537.  
  1538.  
  1539.  
  1540.  
  1541.                Sometimes it is convenient to write certain functors as _o_p_e_r_a_t_o_r_s -- 2-ary functors
  1542.  
  1543.  
  1544.  
  1545.           may  be  declared  as _i_n_f_i_x operators and 1-ary functors as _p_r_e_f_i_x or _p_o_s_t_f_i_x operators.
  1546.  
  1547.  
  1548.  
  1549.           Thus it is possible to write
  1550.  
  1551.  
  1552.  
  1553.               X+Y     (P;Q)     X<Y      +X     P;
  1554.  
  1555.  
  1556.  
  1557.  
  1558.           as optional alternatives to
  1559.  
  1560.  
  1561.  
  1562.               +(X,Y)   ;(P,Q)   <(X,Y)   +(X)   ;(P)
  1563.  
  1564.  
  1565.  
  1566.  
  1567.           Operators are described fully in the next section.
  1568.  
  1569.  
  1570.  
  1571.  
  1572.                _L_i_s_ts form an important class of data structures in Prolog.  They  are  essentially
  1573.  
  1574.  
  1575.  
  1576.           the  same  as  the  lists  of LISP: a list either is the atom [], representing the empty
  1577.  
  1578.  
  1579.  
  1580.           list, or is a compound term  with   functor   `.'/2  and   two   arguments   which   are
  1581.  
  1582.  
  1583.  
  1584.  
  1585.                                                      23
  1586.  
  1587.  
  1588.  
  1589.  
  1590.  
  1591.  
  1592.  
  1593.  
  1594.  
  1595.  
  1596.           respectively  the  head  and  tail  of  the  list.  Thus  a  list  of  the  first  three
  1597.  
  1598.  
  1599.  
  1600.           natural  numbers  is  the structure
  1601.  
  1602.  
  1603.  
  1604.                                                       .
  1605.                                                      / \
  1606.                                                    1    .
  1607.                                                       / \
  1608.                                                     2    .
  1609.                                                         / \
  1610.                                                       3   []
  1611.  
  1612.  
  1613.  
  1614.  
  1615.           which could be written, using the standard syntax, as
  1616.  
  1617.  
  1618.  
  1619.               .(1,.(2,.(3,[])))
  1620.  
  1621.  
  1622.  
  1623.  
  1624.           but which is normally written, in a special list notation, as [1,2,3].  The special list
  1625.  
  1626.  
  1627.  
  1628.           notation in the case when the tail of  a  list  is  a variable is exemplified by
  1629.  
  1630.  
  1631.  
  1632.               [X|L]     [a,b|L]
  1633.  
  1634.  
  1635.  
  1636.  
  1637.           representing
  1638.  
  1639.  
  1640.  
  1641.                                               .                .
  1642.                                              / \             / \
  1643.                                           X     L          a    .
  1644.                                                                / \
  1645.                                                             b     L
  1646.  
  1647.  
  1648.  
  1649.  
  1650.  
  1651.                                                      24
  1652.  
  1653.  
  1654.  
  1655.  
  1656.  
  1657.  
  1658.  
  1659.  
  1660.  
  1661.  
  1662.           respectively.
  1663.  
  1664.  
  1665.  
  1666.  
  1667.                Note that this list syntax is only syntactic sugar for terms of the  form  `.'(_,_)
  1668.  
  1669.  
  1670.  
  1671.           and does not provide any new facilities that were not available otherwise.
  1672.  
  1673.  
  1674.  
  1675.  
  1676.                For convenience, a  further   notational   variant   is   allowed   for  lists   of
  1677.  
  1678.  
  1679.  
  1680.           integers  which correspond to ASCII character codes.  Lists written in this notation are
  1681.  
  1682.  
  1683.  
  1684.           called _s_t_r_i_n_gs.  For example,
  1685.  
  1686.  
  1687.  
  1688.               "Prolog"
  1689.  
  1690.  
  1691.  
  1692.  
  1693.           represents exactly the same list as
  1694.  
  1695.  
  1696.  
  1697.               [80,114,111,108,111,103]
  1698.  
  1699.  
  1700.  
  1701.  
  1702.  
  1703.           _3._2.  _O_p_e_r_a_t_o_r_s
  1704.  
  1705.  
  1706.  
  1707.  
  1708.  
  1709.  
  1710.  
  1711.  
  1712.  
  1713.  
  1714.  
  1715.  
  1716.  
  1717.                                                      25
  1718.  
  1719.  
  1720.  
  1721.  
  1722.  
  1723.  
  1724.  
  1725.  
  1726.  
  1727.  
  1728.                Operators in Prolog are simply a notational convenience.  For example, the  expres-
  1729.  
  1730.  
  1731.  
  1732.           sion
  1733.  
  1734.  
  1735.  
  1736.               2 + 1
  1737.  
  1738.  
  1739.  
  1740.  
  1741.           could also be written +(2,1).  It should be noticed that this expression represents  the
  1742.  
  1743.  
  1744.  
  1745.           structure
  1746.  
  1747.  
  1748.  
  1749.                                                        +
  1750.                                                     /   \
  1751.                                                   2     1
  1752.  
  1753.  
  1754.  
  1755.  
  1756.           and not the number 3.  The addition would only be performed if the structure was  passed
  1757.  
  1758.  
  1759.  
  1760.           as an argument to an appropriate procedure (such as eval/2 - see Section 5.2).
  1761.  
  1762.  
  1763.  
  1764.  
  1765.                The Prolog syntax caters for operators  of  three  main  kinds  - _i_n_f_i_x, _p_r_e_f_i_x and
  1766.  
  1767.  
  1768.  
  1769.           _p_o_s_t_f_i_x.   An  infix operator appears between its two arguments, while a prefix operator
  1770.  
  1771.  
  1772.  
  1773.           precedes its single argument and a postfix operator is written after  its  single  argu-
  1774.  
  1775.  
  1776.  
  1777.           ment.
  1778.  
  1779.  
  1780.  
  1781.  
  1782.  
  1783.                                                      26
  1784.  
  1785.  
  1786.  
  1787.  
  1788.  
  1789.  
  1790.  
  1791.  
  1792.  
  1793.  
  1794.                Each operator has a _p_r_e_c_e_d_e_n_c_e, which is a number from  1   to   1200.   The   pre-
  1795.  
  1796.  
  1797.  
  1798.           cedence   is   used   to  disambiguate expressions  where  the  structure  of  the  term
  1799.  
  1800.  
  1801.  
  1802.           denoted is not made explicit through parenthesization.   The  general  rule is that  the
  1803.  
  1804.  
  1805.  
  1806.           operator  with  the  _h_i_g_h_e_s_t  precedence  is the principal functor.  Thus if `+'  has  a
  1807.  
  1808.  
  1809.  
  1810.           higher precedence than `/', then ``a+b/c'' and ``a+(b/c)'' are equivalent and denote the
  1811.  
  1812.  
  1813.  
  1814.           term "+(a,/(b,c))".  Note that the  infix form of the term "/(+(a,b),c)" must be written
  1815.  
  1816.  
  1817.  
  1818.           with explicit parentheses, ``(a+b)/c''.
  1819.  
  1820.  
  1821.  
  1822.  
  1823.                If there are two operators in the subexpression having   the   same  highest   pre-
  1824.  
  1825.  
  1826.  
  1827.           cedence,   the ambiguity must be resolved from the _t_y_p_e_s of the operators.  The possible
  1828.  
  1829.  
  1830.  
  1831.           types for an infix operator are
  1832.  
  1833.  
  1834.  
  1835.               xfx     xfy     yfx
  1836.  
  1837.  
  1838.  
  1839.  
  1840.           With an operator of type `xfx', it is a requirement that both  of  the  two   subexpres-
  1841.  
  1842.  
  1843.  
  1844.           sions  which  are  the  arguments of the operator must be of _l_o_w_e_r precedence  than  the
  1845.  
  1846.  
  1847.  
  1848.  
  1849.                                                      27
  1850.  
  1851.  
  1852.  
  1853.  
  1854.  
  1855.  
  1856.  
  1857.  
  1858.  
  1859.  
  1860.           operator  itself,  i.e.   their  principal functors  must  be   of   lower   precedence,
  1861.  
  1862.  
  1863.  
  1864.           unless the subexpression is explicitly  bracketed  (which  gives  it  zero  precedence).
  1865.  
  1866.  
  1867.  
  1868.           With  an operator of type `xfy', only the first or left-hand subexpression  must  be  of
  1869.  
  1870.  
  1871.  
  1872.           lower precedence;  the second can be of the _s_a_m_e  precedence  as the main operator;  and
  1873.  
  1874.  
  1875.  
  1876.           vice versa for an operator of type `yfx'.
  1877.  
  1878.  
  1879.  
  1880.  
  1881.                For example, if the operators `+' and `-' both  have  type  `yfx' and  are  of  the
  1882.  
  1883.  
  1884.  
  1885.           same  precedence,  then  the  expression ``a-b+c'' is valid, and means ``(a-b)+c'', i.e.
  1886.  
  1887.  
  1888.  
  1889.           ``+(-(a,b),c)''.  Note that the expression would be invalid if the  operators  had  type
  1890.  
  1891.  
  1892.  
  1893.           `xfx', and would mean ``a-(b+c)'', i.e. ``-(a,+(b,c))'', if the types were both `xfy'.
  1894.  
  1895.  
  1896.  
  1897.  
  1898.                The possible types for a prefix operator are
  1899.  
  1900.  
  1901.  
  1902.               fx        fy
  1903.  
  1904.  
  1905.  
  1906.  
  1907.           and for a postfix operator they are
  1908.  
  1909.  
  1910.  
  1911.  
  1912.  
  1913.  
  1914.  
  1915.                                                      28
  1916.  
  1917.  
  1918.  
  1919.  
  1920.  
  1921.  
  1922.  
  1923.  
  1924.  
  1925.               xf        yf
  1926.  
  1927.  
  1928.  
  1929.  
  1930.           The meaning of the types should be clear by  analogy  with  those  for infix  operators.
  1931.  
  1932.  
  1933.  
  1934.           As  an example, if `not' were declared as a prefix operator of type `fy', then
  1935.  
  1936.  
  1937.  
  1938.               not not P
  1939.  
  1940.  
  1941.  
  1942.  
  1943.           would be a permissible way to write "not(not(P))". If  the  type  were `fx', the preced-
  1944.  
  1945.  
  1946.  
  1947.           ing expression would not be legal, although
  1948.  
  1949.  
  1950.  
  1951.               not P
  1952.  
  1953.  
  1954.  
  1955.  
  1956.           would still be a permissible form for "not(P)".
  1957.  
  1958.  
  1959.  
  1960.  
  1961.                In SB-Prolog, a functor named _n_a_m_e is  declared  as  an operator of type  _t_y_p_e  and
  1962.  
  1963.  
  1964.  
  1965.           precedence _p_r_e_c_e_d_e_n_c_e by calling the evaluable predicate op:
  1966.  
  1967.  
  1968.  
  1969.                       | ?- op(_p_r_e_c_e_d_e_n_c_e,_t_y_p_e,_n_a_m_e).
  1970.  
  1971.  
  1972.  
  1973.  
  1974.           The argument _n_a_m_e can also be a list of names of operators of the  same  type  and  pre-
  1975.  
  1976.  
  1977.  
  1978.  
  1979.  
  1980.  
  1981.                                                      29
  1982.  
  1983.  
  1984.  
  1985.  
  1986.  
  1987.  
  1988.  
  1989.  
  1990.  
  1991.  
  1992.           cedence.
  1993.  
  1994.  
  1995.  
  1996.  
  1997.                It is possible to have more than one operator of the  same  name, so long  as  they
  1998.  
  1999.  
  2000.  
  2001.           are  of different kinds, i.e.  infix, prefix or postfix.  An operator of any kind may be
  2002.  
  2003.  
  2004.  
  2005.           redefined by a new declaration  of  the same  kind.   This  applies equally to operators
  2006.  
  2007.  
  2008.  
  2009.           which are provided as _s_t_a_n_d_a_r_d in SB-Prolog, namely:
  2010.  
  2011.  
  2012.  
  2013.  
  2014.               :- op(   1200,   xfx,   [ :-, --> ]).
  2015.               :- op(   1200,    fx,   [ :-, ?- ]).
  2016.               :- op(   1198,   xfx,   [ ::- ]).
  2017.               :- op(   1100,   xfy,   [ ; ]).
  2018.               :- op(   1050,   xfy,   [ -> ]).
  2019.               :- op(   1000,   xfy,   [ ',' ]).   /* See note below */
  2020.               :- op(    900,    fy,   [ not, \+, spy, nospy ]).
  2021.               :- op(    700,   xfx,   [ =, is, =.., ==, \==, <,>, =<,>=,
  2022.                                         =:=, =\=, <, >, =<, >= ]).
  2023.               :- op(    661,   xfy,   [ `.' ]).
  2024.               :- op(    500,   yfx,   [ +, -, /\, \/ ]).
  2025.               :- op(    500,    fx,   [ +, - ]).
  2026.               :- op(    400,   yfx,   [ *, /, //, <<, >> ]).
  2027.               :- op(    300,   xfx,   [ mod ]).
  2028.               :- op(    200,   xfy,   [ ^ ]).
  2029.  
  2030.  
  2031.  
  2032.  
  2033.  
  2034.  
  2035.                Operator declarations are most usefully placed in directives at  the  top  of  your
  2036.  
  2037.  
  2038.  
  2039.           program  files.  In  this case the directive should be a command as shown above. Another
  2040.  
  2041.  
  2042.  
  2043.           common method of organisation is to have one file just containing  commands  to  declare
  2044.  
  2045.  
  2046.  
  2047.                                                      30
  2048.  
  2049.  
  2050.  
  2051.  
  2052.  
  2053.  
  2054.  
  2055.  
  2056.  
  2057.  
  2058.           all the necessary operators. This file is then always consulted first.
  2059.  
  2060.  
  2061.  
  2062.  
  2063.                Note that a comma written literally as  a  punctuation  character can  be  used  as
  2064.  
  2065.  
  2066.  
  2067.           though it were an infix operator of precedence 1000 and type `xfy':
  2068.  
  2069.  
  2070.  
  2071.               X,Y    ','(X,Y)
  2072.  
  2073.  
  2074.  
  2075.  
  2076.           represent the same compound term.  But note that a comma written as  a  quoted  atom  is
  2077.  
  2078.  
  2079.  
  2080.           _n_o_t a standard operator.
  2081.  
  2082.  
  2083.  
  2084.  
  2085.                Note also that the  arguments  of  a  compound  term  written  in standard   syntax
  2086.  
  2087.  
  2088.  
  2089.           must  be  expressions of precedence _b_e_l_o_w 1000. Thus it is necessary to parenthesize the
  2090.  
  2091.  
  2092.  
  2093.           expression "P :- Q" in
  2094.  
  2095.  
  2096.  
  2097.               assert((P :- Q))
  2098.  
  2099.  
  2100.  
  2101.  
  2102.           The following syntax restrictions serve  to remove potential ambiguity  associated  with
  2103.  
  2104.  
  2105.  
  2106.           prefix operators.
  2107.  
  2108.  
  2109.  
  2110.  
  2111.  
  2112.  
  2113.                                                      31
  2114.  
  2115.  
  2116.  
  2117.  
  2118.  
  2119.  
  2120.  
  2121.  
  2122.  
  2123.  
  2124.           -    In a term written in standard syntax, the  principal  functor  and  its   following
  2125.  
  2126.  
  2127.  
  2128.                "("  must  _n_o_t be separated by any whitespace.  Thus
  2129.  
  2130.  
  2131.  
  2132.                    point (X,Y,Z)
  2133.  
  2134.  
  2135.  
  2136.  
  2137.                is invalid syntax (unless _p_o_i_n_t were declared as a prefix operator).
  2138.  
  2139.  
  2140.  
  2141.  
  2142.           -    If the argument of a prefix operator  starts  with  a  "(",   this   "("  must   be
  2143.  
  2144.  
  2145.  
  2146.                separated   from  the operator by at least one space or other non-printable charac-
  2147.  
  2148.  
  2149.  
  2150.                ter.  Thus
  2151.  
  2152.  
  2153.  
  2154.                    :-(p;q),r.
  2155.  
  2156.  
  2157.  
  2158.  
  2159.                (where `:-' is the prefix operator) is invalid syntax, and must be written as
  2160.  
  2161.  
  2162.  
  2163.                    :- (p;q),r.
  2164.  
  2165.  
  2166.  
  2167.  
  2168.  
  2169.           -    If a prefix  operator  is  written  without  an  argument,  as  an ordinary   atom,
  2170.  
  2171.  
  2172.  
  2173.                the   atom   is   treated   as   an expression of the same precedence as the prefix
  2174.  
  2175.  
  2176.  
  2177.  
  2178.  
  2179.                                                      32
  2180.  
  2181.  
  2182.  
  2183.  
  2184.  
  2185.  
  2186.  
  2187.  
  2188.  
  2189.  
  2190.                operator, and  must  therefore  be  bracketed where necessary.  Thus  the  brackets
  2191.  
  2192.  
  2193.  
  2194.                are necessary in
  2195.  
  2196.  
  2197.  
  2198.                    X = (?-)
  2199.  
  2200.  
  2201.  
  2202.  
  2203.  
  2204.           _4.  _S_B-_P_r_o_l_o_g: _O_p_e_r_a_t_i_o_n_a_l _S_e_m_a_n_t_i_c_s
  2205.  
  2206.  
  2207.  
  2208.  
  2209.  
  2210.  
  2211.           _4._1.  _S_t_a_n_d_a_r_d _E_x_e_c_u_t_i_o_n _B_e_h_a_v_i_o_u_r
  2212.  
  2213.  
  2214.  
  2215.  
  2216.  
  2217.  
  2218.                The normal execution behaviour of SB-Prolog follows the usual left to  right  order
  2219.  
  2220.  
  2221.  
  2222.           of literals within a clause, and the textual top to bottom order of clauses for a predi-
  2223.  
  2224.  
  2225.  
  2226.           cate.  This corresponds to a depth first search of the leftmost SLD-tree for the program
  2227.  
  2228.  
  2229.  
  2230.           and the given query.  Unification without occurs check is used, and execution backtracks
  2231.  
  2232.  
  2233.  
  2234.           to the most recent choice point when unification fails.
  2235.  
  2236.  
  2237.  
  2238.  
  2239.  
  2240.  
  2241.  
  2242.  
  2243.  
  2244.  
  2245.                                                      33
  2246.  
  2247.  
  2248.  
  2249.  
  2250.  
  2251.  
  2252.  
  2253.  
  2254.  
  2255.  
  2256.           _4._2.  _C_u_t_s _a_n_d _I_f-_T_h_e_n-_E_l_s_e
  2257.  
  2258.  
  2259.  
  2260.  
  2261.  
  2262.  
  2263.                This standard execution behaviour of SB-Prolog can be changed using constructs like
  2264.  
  2265.  
  2266.  
  2267.           _c_u_t (`!') and _i_f-_t_h_e_n-_e_l_s_e (`->').  In SB-Prolog, cuts are usually treated as _h_a_r_d, i.e.
  2268.  
  2269.  
  2270.  
  2271.           discard choice points of all the literals to the left of the cut in the clause  contain-
  2272.  
  2273.  
  2274.  
  2275.           ing  the  cut  being  executed, and also the choice point for the parent predicate, i.e.
  2276.  
  2277.  
  2278.  
  2279.           any remaining clauses for the predicate containing the cut being  executed.   There  are
  2280.  
  2281.  
  2282.  
  2283.           some  situations,  however,  where  the  scope of a cut is restricted to be smaller than
  2284.  
  2285.  
  2286.  
  2287.           this.  Restrictions apply under the following conditions:
  2288.  
  2289.  
  2290.  
  2291.  
  2292.           (1)  The cut occurs in a term which has been constructed at runtime and  called  through
  2293.  
  2294.  
  2295.  
  2296.                _c_a_l_l/1, e.g. in
  2297.  
  2298.  
  2299.  
  2300.                                    ..., X = (p(Y), !, q(Y)), ..., call(X), ...
  2301.  
  2302.  
  2303.  
  2304.  
  2305.                In this case, the scope of the cut is restricted to be within the _c_a_l_l, unless  one
  2306.  
  2307.  
  2308.  
  2309.  
  2310.  
  2311.                                                      34
  2312.  
  2313.  
  2314.  
  2315.  
  2316.  
  2317.  
  2318.  
  2319.  
  2320.  
  2321.  
  2322.                of the following cases also apply and serve to restrict its scope further.
  2323.  
  2324.  
  2325.  
  2326.  
  2327.           (2)  The cut occurs in a negated goal, or within the scope of an if-then-else.  In these
  2328.  
  2329.  
  2330.  
  2331.                cases,  the  scope  of  the  cut is restricted to be within the negation or the if-
  2332.  
  2333.  
  2334.  
  2335.                then-else.
  2336.  
  2337.  
  2338.  
  2339.  
  2340.           In cases involving nested occurrences of these situations, the scope of the cut is  res-
  2341.  
  2342.  
  2343.  
  2344.           tricted  to that for the deepest such nested construct, i.e. most restricted.  For exam-
  2345.  
  2346.  
  2347.  
  2348.           ple, in the construct
  2349.  
  2350.  
  2351.  
  2352.                  ..., not( (p(X) -> not( (q(X), (r(X) -> s(X) ; (t(X), !, u(X)))) )) ), ...
  2353.  
  2354.  
  2355.  
  2356.  
  2357.           the scope of the cut is restricted to the inner if-then-else, and does  not  affect  any
  2358.  
  2359.  
  2360.  
  2361.           choice point that may have been set up for q(X).
  2362.  
  2363.  
  2364.  
  2365.  
  2366.                The behaviour of the _i_f-_t_h_e_n-_e_l_s_e operator `->' is as follows: when  executing  the
  2367.  
  2368.  
  2369.  
  2370.           goal
  2371.  
  2372.  
  2373.  
  2374.  
  2375.  
  2376.  
  2377.                                                      35
  2378.  
  2379.  
  2380.  
  2381.  
  2382.  
  2383.  
  2384.  
  2385.  
  2386.  
  2387.                                                  _P -> _Q ; _R
  2388.  
  2389.  
  2390.  
  2391.  
  2392.           if _P succeeds, then any alternatives for _P are discarded and _Q  is  tried,  while  if  _P
  2393.  
  2394.  
  2395.  
  2396.           fails then _R is tried.  Thus, -> may be thought of as
  2397.  
  2398.  
  2399.  
  2400.  
  2401.  
  2402.                       _P -> _Q ; _R :- _P, !, _Q.
  2403.                       _P -> _Q ; _R :- _R.
  2404.  
  2405.  
  2406.  
  2407.  
  2408.  
  2409.  
  2410.           The scoping of cuts within if-then-elses is consistent with this conceptualization.
  2411.  
  2412.  
  2413.  
  2414.  
  2415.           _4._3.  _U_n_i_f_i_c_a_t_i_o_n _o_f _F_l_o_a_t_i_n_g _P_o_i_n_t _N_u_m_b_e_r_s
  2416.  
  2417.  
  2418.  
  2419.  
  2420.  
  2421.  
  2422.                As far as unification is concerned, no type distinction is  made  between  integers
  2423.  
  2424.  
  2425.  
  2426.           and  floating  point numbers, and no explicit type conversion is necessary when unifying
  2427.  
  2428.  
  2429.  
  2430.           an integer with a float.  However, due to the finite precision representation of  float-
  2431.  
  2432.  
  2433.  
  2434.           ing  point  numbers  and  cumulative round-off errors in floating point arithmetic, com-
  2435.  
  2436.  
  2437.  
  2438.           parisons involving floating point numbers may not always give the expected results.   An
  2439.  
  2440.  
  2441.  
  2442.  
  2443.                                                      36
  2444.  
  2445.  
  2446.  
  2447.  
  2448.  
  2449.  
  2450.  
  2451.  
  2452.  
  2453.  
  2454.           effort is made to minimize surprises by considering two numbers _x and _y (at least one of
  2455.  
  2456.  
  2457.  
  2458.           which is a float) to be unifiable if ||_x| - |_y||/_m_i_n(|_x|, |_y|) to be less  than  10[-5].
  2459.  
  2460.  
  2461.  
  2462.           However,  this  does not guarantee immunity against round-off errors.  For the same rea-
  2463.  
  2464.  
  2465.  
  2466.           son, users are warned that indexing on predicate arguments (see Section 13) may not give
  2467.  
  2468.  
  2469.  
  2470.           the expected results if floating point numbers are involved.
  2471.  
  2472.  
  2473.  
  2474.  
  2475.           _5.  _E_v_a_l_u_a_b_l_e _P_r_e_d_i_c_a_t_e_s
  2476.  
  2477.  
  2478.  
  2479.  
  2480.  
  2481.  
  2482.                This section describes (most of) the evaluable predicates  provided  by  SB-Prolog.
  2483.  
  2484.  
  2485.  
  2486.           These  can  be  divided  into  three  classes: _i_n_l_i_n_e predicates, _b_u_i_l_t_i_n predicates and
  2487.  
  2488.  
  2489.  
  2490.           _l_i_b_r_a_r_y predicates.
  2491.  
  2492.  
  2493.  
  2494.  
  2495.                Inline predicates represent ``primitive'' operations in the WAM.  Calls  to  inline
  2496.  
  2497.  
  2498.  
  2499.           predicates  are compiled into a sequence of WAM instructions in-line, i.e. without actu-
  2500.  
  2501.  
  2502.  
  2503.           ally making a call to the predicate.  Thus, for  example,  relational  predicates  (>/2,
  2504.  
  2505.  
  2506.  
  2507.  
  2508.  
  2509.                                                      37
  2510.  
  2511.  
  2512.  
  2513.  
  2514.  
  2515.  
  2516.  
  2517.  
  2518.  
  2519.  
  2520.           >=/2,  etc.)  compile  to,  essentially, a subtraction and a conditional branch.  Inline
  2521.  
  2522.  
  2523.  
  2524.           predicates cannot be redefined by the user.  Table 1 lists the SB-Prolog  inline  predi-
  2525.  
  2526.  
  2527.  
  2528.           cates.
  2529.  
  2530.  
  2531.  
  2532.  
  2533.                Unlike inline predicates, builtin predicates are implemented by C functions in  the
  2534.  
  2535.  
  2536.  
  2537.           simulator,  and  accessed  via  the  inline predicate `_$_b_u_i_l_t_i_n'/1.  Thus, if a builtin
  2538.  
  2539.  
  2540.  
  2541.           predicate _f_o_o/3 was defined as builtin number 38, there would be  a  definition  in  the
  2542.  
  2543.  
  2544.  
  2545.           system of the form
  2546.  
  2547.  
  2548.  
  2549.  
  2550.  
  2551.                       foo(X,Y,Z) :- '_$builtin'(38).
  2552.  
  2553.  
  2554.  
  2555.  
  2556.  
  2557.  
  2558.           =/2                     </2                       =</2                     >=/2
  2559.           >/2                     /\/2                      `\/'/2                   <</2
  2560.           >>/2                    =:=/2                     =\=/2                    is/2
  2561.           /1                      `_$savecp'/1              `_$cutto'/1              `_$builtin'/1
  2562.           `_$call'/1              nonvar/1                  var/1                    fail/0
  2563.           halt/0                  true/0
  2564.  
  2565.  
  2566.                                   Table 1: Inline Predicates of SB-Prolog
  2567.  
  2568.  
  2569.  
  2570.  
  2571.  
  2572.  
  2573.  
  2574.  
  2575.                                                      38
  2576.  
  2577.  
  2578.  
  2579.  
  2580.  
  2581.  
  2582.  
  2583.  
  2584.  
  2585.  
  2586.                In effect, a builtin is simply a segment of code in  a  large  case  (i.e.  _s_w_i_t_c_h)
  2587.  
  2588.  
  2589.  
  2590.           statement.   Each  builtin  is  identified  internally by an integer, referred to as its
  2591.  
  2592.  
  2593.  
  2594.           ``builtin number'', associated with it.  The code for a builtin with  builtin  number  _k
  2595.  
  2596.  
  2597.  
  2598.           corresponds  to  the  _k[_t_h.]  case  in the switch statement.  SB-Prolog limits the total
  2599.  
  2600.  
  2601.  
  2602.           number of builtins to 256.
  2603.  
  2604.  
  2605.  
  2606.  
  2607.                Builtins, unlike inline predicates,  can be redefined by the  user.   For  example,
  2608.  
  2609.  
  2610.  
  2611.           the predicate _f_o_o/3 above can be redefined simply by compiling the new definition into a
  2612.  
  2613.  
  2614.  
  2615.           directory such that during dynamic loading, the  new  definition  would  be  encountered
  2616.  
  2617.  
  2618.  
  2619.           first and loaded.
  2620.  
  2621.  
  2622.  
  2623.  
  2624.                A list of the builtins currently provided is listed in  Appendix  1.   Section  7.4
  2625.  
  2626.  
  2627.  
  2628.           describes the procedure to be followed in order to define new builtin predicates.
  2629.  
  2630.  
  2631.  
  2632.  
  2633.                Like builtin predicates, library predicates may also be redefined by the user.  The
  2634.  
  2635.  
  2636.  
  2637.           essential  difference  between builtin and library predicates is that whereas the former
  2638.  
  2639.  
  2640.  
  2641.                                                      39
  2642.  
  2643.  
  2644.  
  2645.  
  2646.  
  2647.  
  2648.  
  2649.  
  2650.  
  2651.  
  2652.           are coded into the simulator in C, the latter are written in Prolog.
  2653.  
  2654.  
  2655.  
  2656.  
  2657.           _5._1.  _I_n_p_u_t _a_n_d _O_u_t_p_u_t
  2658.  
  2659.  
  2660.  
  2661.  
  2662.  
  2663.  
  2664.                Input and output are done with respect to the current  input  and  output  streams.
  2665.  
  2666.  
  2667.  
  2668.           These  can  be set, reset or checked using the file handling predicates described below.
  2669.  
  2670.  
  2671.  
  2672.           The default input and output streams are denoted by user, and refer to the user's termi-
  2673.  
  2674.  
  2675.  
  2676.           nal.
  2677.  
  2678.  
  2679.  
  2680.  
  2681.           _5._1._1.  _F_i_l_e _H_a_n_d_l_i_n_g
  2682.  
  2683.  
  2684.  
  2685.  
  2686.  
  2687.  
  2688.           see(_F)
  2689.  
  2690.  
  2691.  
  2692.                _F becomes the current input stream.  _F must be instantiated to an atom at the  time
  2693.  
  2694.  
  2695.  
  2696.                of the call.
  2697.  
  2698.  
  2699.  
  2700.  
  2701.  
  2702.  
  2703.  
  2704.  
  2705.  
  2706.  
  2707.                                                      40
  2708.  
  2709.  
  2710.  
  2711.  
  2712.  
  2713.  
  2714.  
  2715.  
  2716.  
  2717.  
  2718.           seeing(_F)
  2719.  
  2720.  
  2721.  
  2722.                _F is unified with the name of the current input file.
  2723.  
  2724.  
  2725.  
  2726.  
  2727.           seen Closes the current input stream.
  2728.  
  2729.  
  2730.  
  2731.  
  2732.           tell(_F)
  2733.  
  2734.  
  2735.  
  2736.                _F becomes the current output stream.  _F must be instantiated to an atom at the time
  2737.  
  2738.  
  2739.  
  2740.                of the call.
  2741.  
  2742.  
  2743.  
  2744.  
  2745.           telling(_F)
  2746.  
  2747.  
  2748.  
  2749.                _F is unified with the name of the current output file.
  2750.  
  2751.  
  2752.  
  2753.  
  2754.           told
  2755.  
  2756.  
  2757.  
  2758.  
  2759.                Closes the current output stream.
  2760.  
  2761.  
  2762.  
  2763.  
  2764.           $exists(_F)
  2765.  
  2766.  
  2767.  
  2768.                Succeeds if file _F exists.
  2769.  
  2770.  
  2771.  
  2772.  
  2773.                                                      41
  2774.  
  2775.  
  2776.  
  2777.  
  2778.  
  2779.  
  2780.  
  2781.  
  2782.  
  2783.  
  2784.           _5._1._2.  _T_e_r_m _I/_O
  2785.  
  2786.  
  2787.  
  2788.  
  2789.  
  2790.  
  2791.           read(_X)
  2792.  
  2793.  
  2794.  
  2795.                The next term, delimited by a full stop (i.e.  a "." followed  by a carriage-return
  2796.  
  2797.  
  2798.  
  2799.                or   a   space),   is   read  from the current input stream and unified with _X. The
  2800.  
  2801.  
  2802.  
  2803.                syntax of the term must accord  with  current operator  declarations.   If  a  call
  2804.  
  2805.  
  2806.  
  2807.                read(_X)  causes  the  end  of  the current input stream to be reached, _X is unified
  2808.  
  2809.  
  2810.  
  2811.                with  the  atom `end_of_file'.  Further  calls  to read for the  same  stream  will
  2812.  
  2813.  
  2814.  
  2815.                then cause an error failure.
  2816.  
  2817.  
  2818.  
  2819.  
  2820.           write(_X)
  2821.  
  2822.  
  2823.  
  2824.                The term _X is written to the current output stream  according  to operator declara-
  2825.  
  2826.  
  2827.  
  2828.                tions in force.
  2829.  
  2830.  
  2831.  
  2832.  
  2833.  
  2834.  
  2835.  
  2836.  
  2837.  
  2838.  
  2839.                                                      42
  2840.  
  2841.  
  2842.  
  2843.  
  2844.  
  2845.  
  2846.  
  2847.  
  2848.  
  2849.  
  2850.           display(_X)
  2851.  
  2852.  
  2853.  
  2854.                The term _X is displayed on the terminal in standard parenthesised prefix notation.
  2855.  
  2856.  
  2857.  
  2858.  
  2859.           writeq(_T_e_r_m)
  2860.  
  2861.  
  2862.  
  2863.                Similar to write(_T_e_r_m), but the names of atoms and functors are quoted where neces-
  2864.  
  2865.  
  2866.  
  2867.                sary to make the result acceptable as input to read.
  2868.  
  2869.  
  2870.  
  2871.  
  2872.           print(_T_e_r_m)
  2873.  
  2874.  
  2875.  
  2876.                Prints out the term _T_e_r_m onto the user's terminal.
  2877.  
  2878.  
  2879.  
  2880.  
  2881.           writename(_T_e_r_m)
  2882.  
  2883.  
  2884.  
  2885.                If _T_e_r_m is an uninstantiated variable, its name, which looks a lot like an  address
  2886.  
  2887.  
  2888.  
  2889.                in memory, is written out; otherwise, the principal functor of _T_e_r_m is written out.
  2890.  
  2891.  
  2892.  
  2893.  
  2894.           writeqname(_T_e_r_m)
  2895.  
  2896.  
  2897.  
  2898.                As for writename, but the names are quoted where necessary.
  2899.  
  2900.  
  2901.  
  2902.  
  2903.  
  2904.  
  2905.                                                      43
  2906.  
  2907.  
  2908.  
  2909.  
  2910.  
  2911.  
  2912.  
  2913.  
  2914.  
  2915.  
  2916.           _5._1._3.  _C_h_a_r_a_c_t_e_r _I/_O
  2917.  
  2918.  
  2919.  
  2920.  
  2921.  
  2922.  
  2923.           nl   A new line is started on the current output stream.
  2924.  
  2925.  
  2926.  
  2927.  
  2928.           get0(_N)
  2929.  
  2930.  
  2931.  
  2932.                _N is the ASCII code of the next character from the current  input  stream.  If  the
  2933.  
  2934.  
  2935.  
  2936.                current  input stream reaches its end of file, a -1 is returned (however, unlike in
  2937.  
  2938.  
  2939.  
  2940.                C-Prolog, the input stream is not closed on encountering end-of-file).
  2941.  
  2942.  
  2943.  
  2944.  
  2945.           get(_N)
  2946.  
  2947.  
  2948.  
  2949.                _N is the ASCII code of the  next  non-blank  printable  character from the  current
  2950.  
  2951.  
  2952.  
  2953.                input stream. It has the same behaviour as get0 on end of file.
  2954.  
  2955.  
  2956.  
  2957.  
  2958.           put(_N)
  2959.  
  2960.  
  2961.  
  2962.                ASCII character code _N is output to the  current  output  stream.   _N  must  be  an
  2963.  
  2964.  
  2965.  
  2966.                integer.
  2967.  
  2968.  
  2969.  
  2970.  
  2971.                                                      44
  2972.  
  2973.  
  2974.  
  2975.  
  2976.  
  2977.  
  2978.  
  2979.  
  2980.  
  2981.  
  2982.           tab(_N)
  2983.  
  2984.  
  2985.  
  2986.                _N spaces are output to the current output stream.  _N must be an integer.
  2987.  
  2988.  
  2989.  
  2990.  
  2991.           _5._2.  _A_r_i_t_h_m_e_t_i_c
  2992.  
  2993.  
  2994.  
  2995.  
  2996.  
  2997.  
  2998.                Arithmetic is  performed  by   evaluable  predicates   which   take   as  arguments
  2999.  
  3000.  
  3001.  
  3002.           _a_r_i_t_h_m_e_t_i_c  _e_x_p_r_e_s_s_i_o_n_s    and   _e_v_a_l_u_a_t_e   them.   An   arithmetic expression is a term
  3003.  
  3004.  
  3005.  
  3006.           built  from  _e_v_a_l_u_a_b_l_e _f_u_n_c_t_o_r_s, numbers  and variables.  At  the  time  of  evaluation,
  3007.  
  3008.  
  3009.  
  3010.           each  variable  in  an arithmetic expression must be bound to a number or to  an  arith-
  3011.  
  3012.  
  3013.  
  3014.           metic  expression.  Each evaluable functor stands for an arithmetic  operation.
  3015.  
  3016.  
  3017.  
  3018.  
  3019.                The evaluable  functors  are  as   follows,   where   _X   and   _Y   are  arithmetic
  3020.  
  3021.  
  3022.  
  3023.           expressions.
  3024.  
  3025.  
  3026.  
  3027.  
  3028.           _X + _Y
  3029.  
  3030.  
  3031.  
  3032.                addition.
  3033.  
  3034.  
  3035.  
  3036.  
  3037.                                                      45
  3038.  
  3039.  
  3040.  
  3041.  
  3042.  
  3043.  
  3044.  
  3045.  
  3046.  
  3047.  
  3048.           _X - _Y
  3049.  
  3050.  
  3051.  
  3052.                subtraction.
  3053.  
  3054.  
  3055.  
  3056.  
  3057.           _X * _Y
  3058.  
  3059.  
  3060.  
  3061.                multiplication.
  3062.  
  3063.  
  3064.  
  3065.  
  3066.           _X / _Y
  3067.  
  3068.  
  3069.  
  3070.                division.[3]  Amounts to integer division if both _X and _Y are integers.
  3071.  
  3072.  
  3073.  
  3074.  
  3075.           _X mod _Y
  3076.  
  3077.  
  3078.  
  3079.                _X (integer) modulo _Y.
  3080.  
  3081.  
  3082.  
  3083.  
  3084.           -_X   unary minus.
  3085.  
  3086.  
  3087.  
  3088.  
  3089.           _X /\ _Y
  3090.  
  3091.  
  3092.  
  3093.                integer bitwise conjunction.
  3094.           ____________________
  3095.              [3] The ``integer division'' operator `//' of C-Prolog is not supported;  it  can  be
  3096.           faked using _f_l_o_o_r/2 if necessary.
  3097.  
  3098.  
  3099.  
  3100.  
  3101.  
  3102.  
  3103.                                                      46
  3104.  
  3105.  
  3106.  
  3107.  
  3108.  
  3109.  
  3110.  
  3111.  
  3112.  
  3113.  
  3114.           _X \/ _Y
  3115.  
  3116.  
  3117.  
  3118.                integer bitwise disjunction.
  3119.  
  3120.  
  3121.  
  3122.  
  3123.           _X << _Y
  3124.  
  3125.  
  3126.  
  3127.                integer bitwise left shift of _X by _Y places.
  3128.  
  3129.  
  3130.  
  3131.  
  3132.           _X >> _Y
  3133.  
  3134.  
  3135.  
  3136.                integer bitwise right shift of _X by _Y places.
  3137.  
  3138.  
  3139.  
  3140.  
  3141.           \_X   integer bitwise negation.
  3142.  
  3143.  
  3144.  
  3145.  
  3146.                As far as unification is concerned, no type distinction is  made  between  integers
  3147.  
  3148.  
  3149.  
  3150.           and  floating  point numbers, and no explicit type conversion is necessary when unifying
  3151.  
  3152.  
  3153.  
  3154.           an integer with a float.  However, due to the finite precision representation of  float-
  3155.  
  3156.  
  3157.  
  3158.           ing  point  numbers  and  cumulative round-off errors in floating point arithmetic, com-
  3159.  
  3160.  
  3161.  
  3162.           parisons involving floating point numbers may not always give the expected results.   An
  3163.  
  3164.  
  3165.  
  3166.  
  3167.  
  3168.  
  3169.                                                      47
  3170.  
  3171.  
  3172.  
  3173.  
  3174.  
  3175.  
  3176.  
  3177.  
  3178.  
  3179.  
  3180.           effort is made to minimize surprises by considering two numbers _x and _y (at least one of
  3181.  
  3182.  
  3183.  
  3184.           which is a float) to be unifiable if |_x - _y|/_m_i_n(|_x|, |_y|) to be less than 10[-5].   The
  3185.  
  3186.  
  3187.  
  3188.           user  should  note,  however,  that  this  does not guarantee immunity against round-off
  3189.  
  3190.  
  3191.  
  3192.           errors.
  3193.  
  3194.  
  3195.  
  3196.  
  3197.                The arithmetic evaluable predicates are as follows, where _X and _Y stand for  arith-
  3198.  
  3199.  
  3200.  
  3201.           metic expressions, and _Z for some term.  Note that this means that is only evaluates one
  3202.  
  3203.  
  3204.  
  3205.           of its arguments as an arithmetic expression (the right-hand side one), whereas all  the
  3206.  
  3207.  
  3208.  
  3209.           comparison predicates evaluate both their arguments.
  3210.  
  3211.  
  3212.  
  3213.  
  3214.           _Z is _X
  3215.  
  3216.  
  3217.  
  3218.  
  3219.                Arithmetic expression _X is evaluated and the result, is unified with _Z. Fails if  _X
  3220.  
  3221.  
  3222.  
  3223.                is  not  an arithmetic expression.  One point to note is that in compiled code, the
  3224.  
  3225.  
  3226.  
  3227.                current implementation of is/2 cannot handle compound expressions  created  dynami-
  3228.  
  3229.  
  3230.  
  3231.                cally: these have to be handled using eval/2.  Thus, for example, the goals
  3232.  
  3233.  
  3234.  
  3235.                                                      48
  3236.  
  3237.  
  3238.  
  3239.  
  3240.  
  3241.  
  3242.  
  3243.  
  3244.  
  3245.                                     ..., X = Y + Z, Y = 3, Z = 2, W is X, ...
  3246.  
  3247.  
  3248.  
  3249.  
  3250.                would fail, whereas
  3251.  
  3252.  
  3253.  
  3254.                                    ..., X = Y + Z, Y = 3, Z = 2, eval(X,W), ...
  3255.  
  3256.  
  3257.  
  3258.  
  3259.                could succeed.
  3260.  
  3261.  
  3262.  
  3263.  
  3264.           eval(_E, _X)
  3265.  
  3266.  
  3267.  
  3268.                Evaluates the arithmetic expression _E and unifies  the  result  with  the  term  _X.
  3269.  
  3270.  
  3271.  
  3272.                Fails  if  _E  is  not  an  arithmetic expression.  The principal difference between
  3273.  
  3274.  
  3275.  
  3276.                eval/2 and is/2 is that in compiled code, is/2 cannot handle arithmetic expressions
  3277.  
  3278.  
  3279.  
  3280.                that are compound terms constructed at runtime, but eval/2 can.  On the other hand,
  3281.  
  3282.  
  3283.  
  3284.                is/2 is more efficient for the evaluation of static arithmetic expressions (i.   e.
  3285.  
  3286.  
  3287.  
  3288.                expressions  that  do  not  have  compound  subterms  created at runtime).  Another
  3289.  
  3290.  
  3291.  
  3292.                difference is that while is/2 is an inline predicate, eval/2  is  not;  thus,  is/2
  3293.  
  3294.  
  3295.  
  3296.                cannot be redefined by the user, but eval/2 can.
  3297.  
  3298.  
  3299.  
  3300.  
  3301.                                                      49
  3302.  
  3303.  
  3304.  
  3305.  
  3306.  
  3307.  
  3308.  
  3309.  
  3310.  
  3311.  
  3312.           _X =:= _Y
  3313.  
  3314.  
  3315.  
  3316.  
  3317.                The values of _X and _Y are equal.  If either _X or _Y involve compound  subexpressions
  3318.  
  3319.  
  3320.  
  3321.                that are created at runtime, they should first be evaluated using eval/2.
  3322.  
  3323.  
  3324.  
  3325.  
  3326.           _X =\= _Y
  3327.  
  3328.  
  3329.  
  3330.  
  3331.                The values of _X and _Y are not equal.  If either _X or _Y involve compound  subexpres-
  3332.  
  3333.  
  3334.  
  3335.                sions that are created at runtime, they should first be evaluated using eval/2.
  3336.  
  3337.  
  3338.  
  3339.  
  3340.           _X < _Y
  3341.  
  3342.  
  3343.  
  3344.  
  3345.                The value of _X is less than the value of _Y.  If either  _X  or  _Y  involve  compound
  3346.  
  3347.  
  3348.  
  3349.                subexpressions  that  are  created at runtime, they should first be evaluated using
  3350.  
  3351.  
  3352.  
  3353.                eval/2.
  3354.  
  3355.  
  3356.  
  3357.  
  3358.           _X > _Y
  3359.  
  3360.  
  3361.  
  3362.  
  3363.  
  3364.  
  3365.  
  3366.  
  3367.                                                      50
  3368.  
  3369.  
  3370.  
  3371.  
  3372.  
  3373.  
  3374.  
  3375.  
  3376.  
  3377.  
  3378.                The value of _X is greater than the value of _Y.  If either _X or _Y  involve  compound
  3379.  
  3380.  
  3381.  
  3382.                subexpressions  that  are  created at runtime, they should first be evaluated using
  3383.  
  3384.  
  3385.  
  3386.                eval/2.
  3387.  
  3388.  
  3389.  
  3390.  
  3391.           _X =< _Y
  3392.  
  3393.  
  3394.  
  3395.  
  3396.                The value of _X is less than or equal to the value of _Y.  If either _X or  _Y  involve
  3397.  
  3398.  
  3399.  
  3400.                compound subexpressions that are created at runtime, they should first be evaluated
  3401.  
  3402.  
  3403.  
  3404.                using eval/2.
  3405.  
  3406.  
  3407.  
  3408.  
  3409.           _X >= _Y
  3410.  
  3411.  
  3412.  
  3413.  
  3414.                The value of _X is greater than or equal to the value  of  _Y.   If  either  _X  or  _Y
  3415.  
  3416.  
  3417.  
  3418.                involve  compound  subexpressions that are created at runtime, they should first be
  3419.  
  3420.  
  3421.  
  3422.                evaluated using eval/2.
  3423.  
  3424.  
  3425.  
  3426.  
  3427.  
  3428.  
  3429.  
  3430.  
  3431.  
  3432.  
  3433.                                                      51
  3434.  
  3435.  
  3436.  
  3437.  
  3438.  
  3439.  
  3440.  
  3441.  
  3442.  
  3443.  
  3444.           floor(_X, _Y)
  3445.  
  3446.  
  3447.  
  3448.  
  3449.                If _X is a floating point number in the call and _Y is free, then _Y  is  instantiated
  3450.  
  3451.  
  3452.  
  3453.                to  the largest integer whose absolute value is not greater than the absolute value
  3454.  
  3455.  
  3456.  
  3457.                of _X; if _X is uninstantiated in the call and _Y is an integer,  then  _X  is  instan-
  3458.  
  3459.  
  3460.  
  3461.                tiated to the smallest float not less than _Y.
  3462.  
  3463.  
  3464.  
  3465.  
  3466.           floatc(_F, _M, _E)
  3467.  
  3468.  
  3469.  
  3470.  
  3471.                If _F is a number while _M and _E are uninstantiated in the call, then  _M  is  instan-
  3472.  
  3473.  
  3474.  
  3475.                tiated to a float _m (of magnitude less than 1), and _E to an integer _n, such that
  3476.  
  3477.  
  3478.  
  3479.                                                   _F = _m * 2[_n].
  3480.  
  3481.  
  3482.  
  3483.  
  3484.                If _F is uninstantiated in the call while _M is a float and  _E  an  integer,  then  _F
  3485.  
  3486.  
  3487.  
  3488.                becomes instantiated to _M * 2[_E].
  3489.  
  3490.  
  3491.  
  3492.  
  3493.  
  3494.  
  3495.  
  3496.  
  3497.  
  3498.  
  3499.                                                      52
  3500.  
  3501.  
  3502.  
  3503.  
  3504.  
  3505.  
  3506.  
  3507.  
  3508.  
  3509.  
  3510.           exp(_X, _Y)
  3511.  
  3512.  
  3513.  
  3514.  
  3515.                If _X is instantiated to a number and _Y is uninstantiated in the  call,  then  _Y  is
  3516.  
  3517.  
  3518.  
  3519.                instantiated  to  _e[_X]  (where  _e = 2.71828...); if _X is uninstantiated in the call
  3520.  
  3521.  
  3522.  
  3523.                while _Y is instantiated to a positive number, then _X is instantiated to _l_o_g<_e>(_Y).
  3524.  
  3525.  
  3526.  
  3527.  
  3528.           square(_X, _Y)
  3529.  
  3530.  
  3531.  
  3532.  
  3533.                If _X is instantiated to a number while _Y is uninstantiated  in  the  call,  then  _Y
  3534.  
  3535.  
  3536.  
  3537.                becomes instantiated to _X[2]; if _X is uninstantiated in the call while _Y is instan-
  3538.  
  3539.  
  3540.  
  3541.                tiated to a positive number, then _X becomes instantiated  to  the  positive  square
  3542.  
  3543.  
  3544.  
  3545.                root of _Y (if _Y is negative in the call, _X becomes instantiated to 0.0).
  3546.  
  3547.  
  3548.  
  3549.  
  3550.           sin(_X, _Y)
  3551.  
  3552.  
  3553.  
  3554.  
  3555.                If _X is instantiated to a number (representing an angle in radians) and _Y is  unin-
  3556.  
  3557.  
  3558.  
  3559.                stantiated  in  the  call,  then  _Y becomes instantiated to _s_i_n(_X) (the user should
  3560.  
  3561.  
  3562.  
  3563.  
  3564.  
  3565.                                                      53
  3566.  
  3567.  
  3568.  
  3569.  
  3570.  
  3571.  
  3572.  
  3573.  
  3574.  
  3575.  
  3576.                check the magnitude of _X to make sure that the result  is  meaningful).   If  _Y  is
  3577.  
  3578.  
  3579.  
  3580.                instantiated  to  a  number  between  -i~i~/2 and i~i~/2 and _X is uninstantiated in the
  3581.  
  3582.  
  3583.  
  3584.                call, then _X becomes instantiated to _s_i_n[-1](_Y).
  3585.  
  3586.  
  3587.  
  3588.  
  3589.           _5._3.  _C_o_n_v_e_n_i_e_n_c_e
  3590.  
  3591.  
  3592.  
  3593.  
  3594.  
  3595.  
  3596.           _P , _Q
  3597.  
  3598.  
  3599.  
  3600.                _P and then _Q.
  3601.  
  3602.  
  3603.  
  3604.  
  3605.           _P ; _Q
  3606.  
  3607.  
  3608.  
  3609.                _P or _Q.
  3610.  
  3611.  
  3612.  
  3613.  
  3614.                Always  succeeds.
  3615.  
  3616.  
  3617.  
  3618.  
  3619.           _X = _Y
  3620.  
  3621.  
  3622.  
  3623.                Defined as if by the clause " Z=Z ", i.e. _X and _Y are unified.
  3624.  
  3625.  
  3626.  
  3627.  
  3628.  
  3629.  
  3630.  
  3631.                                                      54
  3632.  
  3633.  
  3634.  
  3635.  
  3636.  
  3637.  
  3638.  
  3639.  
  3640.  
  3641.  
  3642.           _5._4.  _E_x_t_r_a _C_o_n_t_r_o_l
  3643.  
  3644.  
  3645.  
  3646.  
  3647.  
  3648.  
  3649.           !    Cut (discard) all choice points made since the parent goal started execution.  (The
  3650.  
  3651.  
  3652.  
  3653.                scope of cut in different contexts is discussed in Section 4.2).
  3654.  
  3655.  
  3656.  
  3657.  
  3658.           not _P
  3659.  
  3660.  
  3661.  
  3662.                If the goal _P has a solution, fail,  otherwise  succeed.   It  is defined as if by
  3663.  
  3664.  
  3665.  
  3666.                    not(P) :- P, !, fail.
  3667.                    not(_).
  3668.  
  3669.  
  3670.  
  3671.  
  3672.  
  3673.           _P -> _Q ; _R
  3674.  
  3675.  
  3676.  
  3677.                Analogous to "if _P then _Q else _R" i.e.  defined as if by
  3678.  
  3679.  
  3680.  
  3681.                    P -> Q ; R :- P, !, Q.
  3682.                    P -> Q ; R :- R.
  3683.  
  3684.  
  3685.  
  3686.  
  3687.  
  3688.           _P -> _Q
  3689.  
  3690.  
  3691.  
  3692.                When occurring other  than  as  one  of  the  alternatives  of  a  disjunction,  is
  3693.  
  3694.  
  3695.  
  3696.  
  3697.                                                      55
  3698.  
  3699.  
  3700.  
  3701.  
  3702.  
  3703.  
  3704.  
  3705.  
  3706.  
  3707.  
  3708.                equivalent to
  3709.  
  3710.  
  3711.  
  3712.                    _P -> _Q ; fail.
  3713.  
  3714.  
  3715.  
  3716.  
  3717.  
  3718.           repeat
  3719.  
  3720.  
  3721.  
  3722.                Generates an  infinite  sequence  of  backtracking  choices.   It is defined by the
  3723.  
  3724.  
  3725.  
  3726.                clauses:
  3727.  
  3728.  
  3729.  
  3730.                    repeat.
  3731.                    repeat :- repeat.
  3732.  
  3733.  
  3734.  
  3735.  
  3736.  
  3737.           fail Always fails.
  3738.  
  3739.  
  3740.  
  3741.  
  3742.           _5._5.  _M_e_t_a-_L_o_g_i_c_a_l
  3743.  
  3744.  
  3745.  
  3746.  
  3747.  
  3748.  
  3749.           var(_X)
  3750.  
  3751.  
  3752.  
  3753.                Tests whether _X is currently instantiated to a variable.
  3754.  
  3755.  
  3756.  
  3757.  
  3758.  
  3759.  
  3760.  
  3761.  
  3762.  
  3763.                                                      56
  3764.  
  3765.  
  3766.  
  3767.  
  3768.  
  3769.  
  3770.  
  3771.  
  3772.  
  3773.  
  3774.           nonvar(_X)
  3775.  
  3776.  
  3777.  
  3778.                Tests whether _X is currently instantiated to a non-variable term.
  3779.  
  3780.  
  3781.  
  3782.  
  3783.           atom(_X)
  3784.  
  3785.  
  3786.  
  3787.                Checks that _X is  currently  instantiated  to  an   atom   (i.e.    a  non-variable
  3788.  
  3789.  
  3790.  
  3791.                term of arity 0, other than a number).
  3792.  
  3793.  
  3794.  
  3795.  
  3796.           integer(_X)
  3797.  
  3798.  
  3799.  
  3800.                Checks that _X is currently instantiated to an integer.
  3801.  
  3802.  
  3803.  
  3804.  
  3805.           real(_X)
  3806.  
  3807.  
  3808.  
  3809.  
  3810.                Checks that _X is currently instantiated to a floating point number..
  3811.  
  3812.  
  3813.  
  3814.  
  3815.           number(_X)
  3816.  
  3817.  
  3818.  
  3819.                Checks that _X is currently instantiated to a number, i.e.  that  it  is  either  an
  3820.  
  3821.  
  3822.  
  3823.                integer or a real.
  3824.  
  3825.  
  3826.  
  3827.  
  3828.  
  3829.                                                      57
  3830.  
  3831.  
  3832.  
  3833.  
  3834.  
  3835.  
  3836.  
  3837.  
  3838.  
  3839.  
  3840.           atomic(_X)
  3841.  
  3842.  
  3843.  
  3844.                Checks that _X is currently instantiated to an atom or number.
  3845.  
  3846.  
  3847.  
  3848.  
  3849.           structure(_X)
  3850.  
  3851.  
  3852.  
  3853.  
  3854.                Checks that _X is currently instantiated to a compound term, i.e. to  a  nonvariable
  3855.  
  3856.  
  3857.  
  3858.                term that is not atomic.
  3859.  
  3860.  
  3861.  
  3862.  
  3863.           functor(_T,_F,_N)
  3864.  
  3865.  
  3866.  
  3867.                The principal functor of term _T has name _F and arity _N,  where  _F  is   either   an
  3868.  
  3869.  
  3870.  
  3871.                atom  or,  provided _N is 0, a number. Initially, either _T must be instantiated to a
  3872.  
  3873.  
  3874.  
  3875.                non-variable, or _F and _N  must be   instantiated   to,   respectively,  either   an
  3876.  
  3877.  
  3878.  
  3879.                atom   and  a non-negative integer or an integer and 0. If these conditions are not
  3880.  
  3881.  
  3882.  
  3883.                satisfied, an error message is given.  In the case where  _T  is  initially  instan-
  3884.  
  3885.  
  3886.  
  3887.                tiated  to  a variable, the result of the  call  is to  instantiate  _T  to the most
  3888.  
  3889.  
  3890.  
  3891.                general term having the principal functor indicated.
  3892.  
  3893.  
  3894.  
  3895.                                                      58
  3896.  
  3897.  
  3898.  
  3899.  
  3900.  
  3901.  
  3902.  
  3903.  
  3904.  
  3905.  
  3906.           arg(_I,_T,_X)
  3907.  
  3908.  
  3909.  
  3910.                Initially, _I must be instantiated to a positive  integer  and  _T   to  a   compound
  3911.  
  3912.  
  3913.  
  3914.                term.  The result of the call is to unify _X with the _Ith argument of term  _T.  (The
  3915.  
  3916.  
  3917.  
  3918.                arguments  are  numbered from  1 upwards.) If the initial conditions are not satis-
  3919.  
  3920.  
  3921.  
  3922.                fied or _I is out of range, the call merely fails.
  3923.  
  3924.  
  3925.  
  3926.  
  3927.           _X =.. _Y
  3928.  
  3929.  
  3930.  
  3931.                _Y is a list whose head is the atom corresponding to the principal functor  of _X and
  3932.  
  3933.  
  3934.  
  3935.                whose tail is the argument list of that functor in _X. E.g.
  3936.  
  3937.  
  3938.  
  3939.                    product(0,N,N-1) =.. [product,0,N,N-1]
  3940.  
  3941.                    N-1 =.. [-,N,1]
  3942.  
  3943.                    product =.. [product]
  3944.  
  3945.  
  3946.  
  3947.  
  3948.                If _X is instantiated to a variable, then _Y must be instantiated either to a list of
  3949.  
  3950.  
  3951.  
  3952.                determinate  length whose head is an atom, or to a list of length 1 whose head is a
  3953.  
  3954.  
  3955.  
  3956.                number.
  3957.  
  3958.  
  3959.  
  3960.  
  3961.                                                      59
  3962.  
  3963.  
  3964.  
  3965.  
  3966.  
  3967.  
  3968.  
  3969.  
  3970.  
  3971.  
  3972.           name(_X,_L)
  3973.  
  3974.  
  3975.  
  3976.                If _X is an atom or a number then _L is a list of the ASCII codes of  the  characters
  3977.  
  3978.  
  3979.  
  3980.                comprising the name of _X. E.g.
  3981.  
  3982.  
  3983.  
  3984.                    name(product,[112,114,111,100,117,99,116])
  3985.  
  3986.  
  3987.  
  3988.  
  3989.                i.e.  name(product,"product").
  3990.  
  3991.  
  3992.  
  3993.  
  3994.           If _X is instantiated to a variable, _L must be instantiated to a list of ASCII  character
  3995.  
  3996.  
  3997.  
  3998.           codes.  E.g.
  3999.  
  4000.  
  4001.  
  4002.               | ?- name(X,[104,101,108,108,111])).
  4003.  
  4004.               X = hello
  4005.  
  4006.               | ?- name(X,"hello").
  4007.  
  4008.               X = hello
  4009.  
  4010.  
  4011.  
  4012.  
  4013.  
  4014.           call(_X)
  4015.  
  4016.  
  4017.  
  4018.                If _X is a nonvariable term in the program text, then it is executed exactly as if _X
  4019.  
  4020.  
  4021.  
  4022.                appeared in the program text instead of _c_a_l_l(_X), e.g.
  4023.  
  4024.  
  4025.  
  4026.  
  4027.                                                      60
  4028.  
  4029.  
  4030.  
  4031.  
  4032.  
  4033.  
  4034.  
  4035.  
  4036.  
  4037.                     ..., p(a), call( (q(X), r(Y)) ), s(X), ...
  4038.  
  4039.  
  4040.  
  4041.  
  4042.                is equivalent to
  4043.  
  4044.  
  4045.  
  4046.                     ..., p(a), q(X), r(Y), s(X), ...
  4047.  
  4048.  
  4049.  
  4050.  
  4051.                However, if X is a variable in the program text, then if at runtime  _X  is  instan-
  4052.  
  4053.  
  4054.  
  4055.                tiated  to  a  term  which  would  be  acceptable as the body of a clause, the goal
  4056.  
  4057.  
  4058.  
  4059.                call(_X) is executed as if that term appeared textually in  place  of  the  call(_X),
  4060.  
  4061.  
  4062.  
  4063.                _e_x_c_e_p_t  _t_h_a_t  any  cut (`!') occurring in _X will remove only those choice points in
  4064.  
  4065.  
  4066.  
  4067.                _X.  If _X is not  instantiated  as described above, an error message is printed  and
  4068.  
  4069.  
  4070.  
  4071.                call fails.
  4072.  
  4073.  
  4074.  
  4075.  
  4076.           _X    (where _X is a variable) Exactly the same as call(_X).  However, we prefer the expli-
  4077.  
  4078.  
  4079.  
  4080.                cit  usage of _c_a_l_l/1 as good programming practice, and the use of a top level vari-
  4081.  
  4082.  
  4083.  
  4084.                able subgoal elicits a warning from the compiler.
  4085.  
  4086.  
  4087.  
  4088.  
  4089.  
  4090.  
  4091.  
  4092.  
  4093.                                                      61
  4094.  
  4095.  
  4096.  
  4097.  
  4098.  
  4099.  
  4100.  
  4101.  
  4102.  
  4103.  
  4104.           conlength(_C,_L)
  4105.  
  4106.  
  4107.  
  4108.  
  4109.                Succeeds if the length of the print name of the constant _C (which can be  an  atom,
  4110.  
  4111.  
  4112.  
  4113.                buffer  or integer), in bytes, is _L.  If _C is a buffer (see Section 5.8), it is the
  4114.  
  4115.  
  4116.  
  4117.                length of the buffer; if _C is an integer, it is the length of the decimal represen-
  4118.  
  4119.  
  4120.  
  4121.                tation of that integer, i.e., the number of bytes that a $_w_r_i_t_e_n_a_m_e will use.
  4122.  
  4123.  
  4124.  
  4125.  
  4126.           _5._6.  _S_e_t_s
  4127.  
  4128.  
  4129.  
  4130.  
  4131.  
  4132.  
  4133.                When  there  are  many solutions to a problem, and when  all  those  solutions  are
  4134.  
  4135.  
  4136.  
  4137.           required   to   be   collected  together,  this  can  be  achieved  by  repeatedly back-
  4138.  
  4139.  
  4140.  
  4141.           tracking  and gradually building up a list of the solutions.   The  following  evaluable
  4142.  
  4143.  
  4144.  
  4145.           predicates are provided to automate this process.
  4146.  
  4147.  
  4148.  
  4149.  
  4150.           setof(_X,_P,_S)
  4151.  
  4152.  
  4153.  
  4154.                Read this as "_S is the set of all instances of _X  such  that _P  is provable''.   If
  4155.  
  4156.  
  4157.  
  4158.  
  4159.                                                      62
  4160.  
  4161.  
  4162.  
  4163.  
  4164.  
  4165.  
  4166.  
  4167.  
  4168.  
  4169.  
  4170.                _P  is not provable, setof(_X,_P,_S) succeeds with _S instantiated to the empty list [].
  4171.  
  4172.  
  4173.  
  4174.                The term _P specifies a goal or goals as in call(_P). _S is a set of terms represented
  4175.  
  4176.  
  4177.  
  4178.                as   a  list   of  those terms, without duplicates, in the standard order for terms
  4179.  
  4180.  
  4181.  
  4182.                (see Section 5.7). If there are uninstantiated variables in _P  which  do  not  also
  4183.  
  4184.  
  4185.  
  4186.                appear in _X, then a  call  to  this  evaluable  predicate  may  backtrack, generat-
  4187.  
  4188.  
  4189.  
  4190.                ing  alternative  values  for  _S  corresponding to different instantiations of  the
  4191.  
  4192.  
  4193.  
  4194.                free  variables of _P.  Variables occurring in _P will not be treated as free if they
  4195.  
  4196.  
  4197.  
  4198.                are explicitly bound within _P by an existential quantifier.  An  existential  quan-
  4199.  
  4200.  
  4201.  
  4202.                tification is written:
  4203.  
  4204.  
  4205.  
  4206.                    _Y^_Q
  4207.  
  4208.  
  4209.  
  4210.  
  4211.                meaning "there exists a _Y such that _Q is true", where  _Y is some Prolog term  (usu-
  4212.  
  4213.  
  4214.  
  4215.                ally, a variable, or tuple or list of variables).
  4216.  
  4217.  
  4218.  
  4219.  
  4220.  
  4221.  
  4222.  
  4223.  
  4224.  
  4225.                                                      63
  4226.  
  4227.  
  4228.  
  4229.  
  4230.  
  4231.  
  4232.  
  4233.  
  4234.  
  4235.  
  4236.           bagof(_X,_P,_B_a_g)
  4237.  
  4238.  
  4239.  
  4240.                This is the same as setof except that the list (or alternative lists) returned will
  4241.  
  4242.  
  4243.  
  4244.                not  be  ordered,   and   may   contain  duplicates.   If _P is unsatisfiable, _b_a_g_o_f
  4245.  
  4246.  
  4247.  
  4248.                succeeds binding _B_a_g to the empty list.  The effect of this relaxation is  to  save
  4249.  
  4250.  
  4251.  
  4252.                considerable time and space in execution.
  4253.  
  4254.  
  4255.  
  4256.  
  4257.           findall(_X,_P,_L)
  4258.  
  4259.  
  4260.  
  4261.                Similar to _b_a_g_o_f/3, except that variables in _P that do not occur in _X  are  treated
  4262.  
  4263.  
  4264.  
  4265.                as  local,  and  alternative  lists are not returned for different bindings of such
  4266.  
  4267.  
  4268.  
  4269.                variables.  The list _L is, in general, unordered, and may contain duplicates.  If _P
  4270.  
  4271.  
  4272.  
  4273.                is unsatisfiable, _f_i_n_d_a_l_l succeeds binding _S to the empty list.
  4274.  
  4275.  
  4276.  
  4277.  
  4278.           _X^_P
  4279.  
  4280.  
  4281.  
  4282.  
  4283.                The system recognises this as meaning "there exists an _X  such that  _P   is  true",
  4284.  
  4285.  
  4286.  
  4287.                and  treats  it  as  equivalent  to  call(_P).  The use of this explicit existential
  4288.  
  4289.  
  4290.  
  4291.                                                      64
  4292.  
  4293.  
  4294.  
  4295.  
  4296.  
  4297.  
  4298.  
  4299.  
  4300.  
  4301.  
  4302.                quantifier outside the setof and bagof constructs is superfluous.
  4303.  
  4304.  
  4305.  
  4306.  
  4307.           _5._7.  _C_o_m_p_a_r_i_s_o_n _o_f _T_e_r_m_s
  4308.  
  4309.  
  4310.  
  4311.  
  4312.  
  4313.  
  4314.                These  evaluable  predicates  are   meta-logical.     They   treat   uninstantiated
  4315.  
  4316.  
  4317.  
  4318.           variables  as objects  with  values  which  may  be  compared,  and  they  never instan-
  4319.  
  4320.  
  4321.  
  4322.           tiate those variables.  They should _n_o_t be used when what you really want is  arithmetic
  4323.  
  4324.  
  4325.  
  4326.           comparison (Section 5.2) or unification.
  4327.  
  4328.  
  4329.  
  4330.  
  4331.                The  predicates  make reference to a standard total ordering of terms, which is  as
  4332.  
  4333.  
  4334.  
  4335.           follows:
  4336.  
  4337.  
  4338.  
  4339.  
  4340.                variables, in a standard order (roughly, oldest first - the order  is  _n_o_t  related
  4341.  
  4342.  
  4343.  
  4344.                to the names of variables);
  4345.  
  4346.  
  4347.  
  4348.  
  4349.                numbers, from -"infinity" to +"infinity";
  4350.  
  4351.  
  4352.  
  4353.  
  4354.  
  4355.  
  4356.  
  4357.                                                      65
  4358.  
  4359.  
  4360.  
  4361.  
  4362.  
  4363.  
  4364.  
  4365.  
  4366.  
  4367.  
  4368.                atoms, in alphabetical (i.e. ASCII) order;
  4369.  
  4370.  
  4371.  
  4372.  
  4373.                complex  terms, ordered first by arity, then by the name of principal functor, then
  4374.  
  4375.  
  4376.  
  4377.                by the arguments (in left-to-right order).
  4378.  
  4379.  
  4380.  
  4381.  
  4382.                For example, here is a list of terms in the standard order:
  4383.  
  4384.  
  4385.  
  4386.               [ X, -9, 1, fie, foe, fum, X = Y, fie(0,2), fie(1,1) ]
  4387.  
  4388.  
  4389.  
  4390.  
  4391.           These are the basic predicates for comparison of arbitrary terms:
  4392.  
  4393.  
  4394.  
  4395.  
  4396.           _X == _Y
  4397.  
  4398.  
  4399.  
  4400.  
  4401.                Tests if the terms currently instantiating _X and  _Y are  literally  identical   (in
  4402.  
  4403.  
  4404.  
  4405.                particular,  variables in equivalent positions in the two terms must be identical).
  4406.  
  4407.  
  4408.  
  4409.                For example, the question
  4410.  
  4411.  
  4412.  
  4413.                    | ?- X == Y.
  4414.  
  4415.  
  4416.  
  4417.  
  4418.                fails (answers "no") because _X  and  _Y  are   distinct   uninstantiated  variables.
  4419.  
  4420.  
  4421.  
  4422.  
  4423.                                                      66
  4424.  
  4425.  
  4426.  
  4427.  
  4428.  
  4429.  
  4430.  
  4431.  
  4432.  
  4433.  
  4434.                However, the question
  4435.  
  4436.  
  4437.  
  4438.                    | ?- X = Y, X == Y.
  4439.  
  4440.  
  4441.  
  4442.  
  4443.                succeeds because the first goal unifies the two variables (see page 42).
  4444.  
  4445.  
  4446.  
  4447.  
  4448.           _X \== _Y
  4449.  
  4450.  
  4451.  
  4452.  
  4453.                Tests  if  the  terms  currently  instantiating   _X   and   _Y   are  not  literally
  4454.  
  4455.  
  4456.  
  4457.                identical.
  4458.  
  4459.  
  4460.  
  4461.  
  4462.           _T_1 @< _T_2
  4463.  
  4464.  
  4465.  
  4466.  
  4467.                Term _T_1 is before term _T_2 in the standard order.
  4468.  
  4469.  
  4470.  
  4471.  
  4472.           _T_1 @> _T_2
  4473.  
  4474.  
  4475.  
  4476.  
  4477.                Term _T_1 is after term _T_2 in the standard order.
  4478.  
  4479.  
  4480.  
  4481.  
  4482.  
  4483.  
  4484.  
  4485.  
  4486.  
  4487.  
  4488.  
  4489.                                                      67
  4490.  
  4491.  
  4492.  
  4493.  
  4494.  
  4495.  
  4496.  
  4497.  
  4498.  
  4499.  
  4500.           _T_1 @=< _T_2
  4501.  
  4502.  
  4503.  
  4504.  
  4505.                Term _T_1 is not after term _T_2 in the standard order.
  4506.  
  4507.  
  4508.  
  4509.  
  4510.           _T_1 @>= _T_2
  4511.  
  4512.  
  4513.  
  4514.  
  4515.                Term _T_1 is not before term _T_2 in the standard order.
  4516.  
  4517.  
  4518.  
  4519.  
  4520.  
  4521.  
  4522.           Some further predicates involving comparison of terms are:
  4523.  
  4524.  
  4525.  
  4526.  
  4527.           compare(_O_p,_T_1,_T_2)
  4528.  
  4529.  
  4530.  
  4531.  
  4532.                The  result  of comparing terms _T_1 and _T_2 is _O_p, where the possible values  for  _O_p
  4533.  
  4534.  
  4535.  
  4536.                are:
  4537.  
  4538.  
  4539.  
  4540.                    `='   if _T_1 is identical to _T_2,
  4541.  
  4542.                    `<'   if _T_1 is before _T_2 in the standard order,
  4543.  
  4544.                    `>'   if _T_1 is after _T_2 in the standard order.
  4545.  
  4546.  
  4547.  
  4548.  
  4549.                Thus compare(=,_T_1,_T_2) is equivalent to _T_1 == _T_2.
  4550.  
  4551.  
  4552.  
  4553.  
  4554.  
  4555.                                                      68
  4556.  
  4557.  
  4558.  
  4559.  
  4560.  
  4561.  
  4562.  
  4563.  
  4564.  
  4565.  
  4566.           sort(_L_1,_L_2)
  4567.  
  4568.  
  4569.  
  4570.  
  4571.                The elements of the list _L_1 are sorted into the standard order, and  any  identical
  4572.  
  4573.  
  4574.  
  4575.                (i.e. `==') elements are merged,  yielding  the  list _L_2.
  4576.  
  4577.  
  4578.  
  4579.  
  4580.           _5._8.  _B_u_f_f_e_r_s
  4581.  
  4582.  
  4583.  
  4584.  
  4585.  
  4586.  
  4587.                SB-Prolog supports the concept of buffers.  A buffer is actually a constant and the
  4588.  
  4589.  
  4590.  
  4591.           characters  that  make  up  the buffer is the name of the constant.  However, the symbol
  4592.  
  4593.  
  4594.  
  4595.           table entry for a buffer is not hashed and thus is not added to  the  obj-list,  so  two
  4596.  
  4597.  
  4598.  
  4599.           different  buffers will never unify.  Buffers can be allocated either in permanent space
  4600.  
  4601.  
  4602.  
  4603.           or on the heap.  Buffers in permanent space stay there forever; buffers on the heap  are
  4604.  
  4605.  
  4606.  
  4607.           deallocated when the ``allocate buffer'' goal is backtracked over.
  4608.  
  4609.  
  4610.  
  4611.  
  4612.                A buffer allocated on the heap can either be a simple buffer, or it  can  be  allo-
  4613.  
  4614.  
  4615.  
  4616.           cated  as  a  subbuffer  of  another  buffer  already  on the heap.  A subbuffer will be
  4617.  
  4618.  
  4619.  
  4620.  
  4621.                                                      69
  4622.  
  4623.  
  4624.  
  4625.  
  4626.  
  4627.  
  4628.  
  4629.  
  4630.  
  4631.  
  4632.           deallocated when its superbuffer is deallocated.
  4633.  
  4634.  
  4635.  
  4636.  
  4637.                There are occasions when it is not known, in advance, exactly how much  space  will
  4638.  
  4639.  
  4640.  
  4641.           be  required and so how big a buffer should be allocated.  Sometimes this problem can be
  4642.  
  4643.  
  4644.  
  4645.           overcome by allocating a large buffer and then,  after  using  as  much  as  is  needed,
  4646.  
  4647.  
  4648.  
  4649.           returning  the  rest  of the buffer to the system. This can be done, but only under _v_e_r_y
  4650.  
  4651.  
  4652.  
  4653.           limited circumstances: a buffer is allocated from the end of the  permanent  space,  the
  4654.  
  4655.  
  4656.  
  4657.           top  of  the heap, or from the next available space in the superbuffer; if no more space
  4658.  
  4659.  
  4660.  
  4661.           has been used beyond the end of the buffer, a tail portion of the buffer can be returned
  4662.  
  4663.  
  4664.  
  4665.           to the system. This operation is called ``trimming'' the buffer.
  4666.  
  4667.  
  4668.  
  4669.  
  4670.                The following is a list of library predicates  for  buffer  management  (predicates
  4671.  
  4672.  
  4673.  
  4674.           whose names begin with `$' are low-level routines intended only for serious hackers wil-
  4675.  
  4676.  
  4677.  
  4678.           ling to put up with little or no protection):
  4679.  
  4680.  
  4681.  
  4682.  
  4683.  
  4684.  
  4685.  
  4686.  
  4687.                                                      70
  4688.  
  4689.  
  4690.  
  4691.  
  4692.  
  4693.  
  4694.  
  4695.  
  4696.  
  4697.  
  4698.           alloc_perm(_S_i_z_e, _B_u_f_f)
  4699.  
  4700.  
  4701.  
  4702.                Allocates a buffer with a length _S_i_z_e in the permanent (i.e.  program)  area.  _S_i_z_e
  4703.  
  4704.  
  4705.  
  4706.                must  be  bound  to a number. On successful return, _B_u_f_f will be bound to the allo-
  4707.  
  4708.  
  4709.  
  4710.                cated buffer.  The buffer, being in the permanent area, is never de-allocated.
  4711.  
  4712.  
  4713.  
  4714.  
  4715.           alloc_heap(_S_i_z_e, _B_u_f_f)
  4716.  
  4717.  
  4718.  
  4719.                Allocates a buffer of size _S_i_z_e on the heap and binds _B_u_f_f to it. Since  it  is  on
  4720.  
  4721.  
  4722.  
  4723.                the heap, it will be deallocated on backtracking.
  4724.  
  4725.  
  4726.  
  4727.  
  4728.           $alloc_buff(_S_i_z_e,_B_u_f_f,_T_y_p_e,_S_u_p_b_u_f_f,_R_e_t_c_o_d_e)
  4729.  
  4730.  
  4731.  
  4732.  
  4733.                Allocates a buffer.  _S_i_z_e is the length (in bytes) of the buffer to allocate;  _B_u_f_f
  4734.  
  4735.  
  4736.  
  4737.                is  the buffer allocated, and should be unbound at the time of the call; _T_y_p_e indi-
  4738.  
  4739.  
  4740.  
  4741.                cates where to allocate the buffer: a value of 0 indicates that the buffer is to be
  4742.  
  4743.  
  4744.  
  4745.                allocated in permanent space, 1 that it should be on the heap, and 2 indicates that
  4746.  
  4747.  
  4748.  
  4749.                it should be allocated from a larger heap buffer; _S_u_p_b_u_f_f is the larger  buffer  to
  4750.  
  4751.  
  4752.  
  4753.                                                      71
  4754.  
  4755.  
  4756.  
  4757.  
  4758.  
  4759.  
  4760.  
  4761.  
  4762.  
  4763.  
  4764.                allocate  a  subbuffer  out  of,  and  is only looked at if the value of _T_y_p_e is 2;
  4765.  
  4766.  
  4767.  
  4768.                _R_e_t_c_o_d_e is the return code: a value of 0 indicates that the buffer has  been  allo-
  4769.  
  4770.  
  4771.  
  4772.                cated,  while  a value of 1 indicates that the buffer could not be allocated due to
  4773.  
  4774.  
  4775.  
  4776.                lack of space.  The arguments _S_i_z_e, _T_y_p_e, and _S_u_p_b_u_f_f (if _T_y_p_e = 2) are input argu-
  4777.  
  4778.  
  4779.  
  4780.                ments,  and  should  be  bound at the time of the call; _B_u_f_f and _R_e_t_c_o_d_e are output
  4781.  
  4782.  
  4783.  
  4784.                arguments, and should be unbound at the time of the call.
  4785.  
  4786.  
  4787.  
  4788.  
  4789.           trimbuff(_T_y_p_e, _B_u_f_f, _N_e_w_l_e_n)
  4790.  
  4791.  
  4792.  
  4793.                This allows (in some very restricted circumstances) the changing of the size  of  a
  4794.  
  4795.  
  4796.  
  4797.                buffer.  _T_y_p_e is 0 if the buffer is permanent, 1 if the buffer is on the heap. _B_u_f_f
  4798.  
  4799.  
  4800.  
  4801.                is the buffer.  _N_e_w_l_e_n is an integer: the size (which should be  smaller  than  the
  4802.  
  4803.  
  4804.  
  4805.                original  length  of the buffer) to make the buffer. If the buffer is at the top of
  4806.  
  4807.  
  4808.  
  4809.                the heap (if heap buffer) or the end of the program area (if  permanent)  then  the
  4810.  
  4811.  
  4812.  
  4813.                heap-top  (or  program area top) will be readjusted down.  The length of the buffer
  4814.  
  4815.  
  4816.  
  4817.  
  4818.  
  4819.                                                      72
  4820.  
  4821.  
  4822.  
  4823.  
  4824.  
  4825.  
  4826.  
  4827.  
  4828.  
  4829.  
  4830.                will be modified to _N_e_w_l_e_n.  This is (obviously) a very low-level primitive and  is
  4831.  
  4832.  
  4833.  
  4834.                for hackers only to implement grungy stuff.
  4835.  
  4836.  
  4837.  
  4838.  
  4839.           $trim_buff(_S_i_z_e,_B_u_f_f,_T_y_p_e,_S_u_p_b_u_f_f)
  4840.  
  4841.  
  4842.  
  4843.  
  4844.                Trims the buffer _B_u_f_f (possibly contained in superbuffer _S_u_p_b_u_f_f) of type  _T_y_p_e  to
  4845.  
  4846.  
  4847.  
  4848.                _S_i_z_e  bytes.  The value of _T_y_p_e indicates where the buffer is located: a value of 0
  4849.  
  4850.  
  4851.  
  4852.                denotes a buffer in permanent space, a value of 1 a buffer on the heap, and a value
  4853.  
  4854.  
  4855.  
  4856.                of 2 a buffer within another buffer (the superbuffer).  All the arguments are input
  4857.  
  4858.  
  4859.  
  4860.                arguments, and should be instantiated at the time of call (with the possible excep-
  4861.  
  4862.  
  4863.  
  4864.                tion of _S_u_p_b_u_f_f, which is looked at only if _T_y_p_e is 2).  The internal length of the
  4865.  
  4866.  
  4867.  
  4868.                buffer _B_u_f_f is changed to _S_i_z_e.
  4869.  
  4870.  
  4871.  
  4872.  
  4873.           conlength(_C_o_n_s_t_a_n_t,_L_e_n_g_t_h)
  4874.  
  4875.  
  4876.  
  4877.  
  4878.  
  4879.  
  4880.  
  4881.  
  4882.  
  4883.  
  4884.  
  4885.                                                      73
  4886.  
  4887.  
  4888.  
  4889.  
  4890.  
  4891.  
  4892.  
  4893.  
  4894.  
  4895.  
  4896.                Succeeds if the length of the print name of the constant _C_o_n_s_t_a_n_t (which can be  an
  4897.  
  4898.  
  4899.  
  4900.                atom,  buffer or integer), in bytes, is _L_e_n_g_t_h.  If _C_o_n_s_t_a_n_t is a buffer, it is the
  4901.  
  4902.  
  4903.  
  4904.                length of the buffer; if _C_o_n_s_t_a_n_t is an integer, it is the length  of  the  decimal
  4905.  
  4906.  
  4907.  
  4908.                representation  of  that  integer, i.e., the number of bytes that a $_w_r_i_t_e_n_a_m_e will
  4909.  
  4910.  
  4911.  
  4912.                use.
  4913.  
  4914.  
  4915.  
  4916.  
  4917.           _5._9.  _M_o_d_i_f_i_c_a_t_i_o_n _o_f _t_h_e _P_r_o_g_r_a_m
  4918.  
  4919.  
  4920.  
  4921.  
  4922.  
  4923.  
  4924.                The predicates defined in this section allow modification of the program as  it  is
  4925.  
  4926.  
  4927.  
  4928.           actually  running.   Clauses  can be added to the program (_a_s_s_e_r_t_e_d) or removed from the
  4929.  
  4930.  
  4931.  
  4932.           program (_r_e_t_r_a_c_t_e_d).  At the lowest level, the system supports the asserting of  clauses
  4933.  
  4934.  
  4935.  
  4936.           with  upto  one  literal in the body.  It does this by allocating a buffer and compiling
  4937.  
  4938.  
  4939.  
  4940.           code for the clause into that buffer.  Such a buffer is called  a  ``clause  reference''
  4941.  
  4942.  
  4943.  
  4944.           (_c_l_r_e_f).   The  clref  is  then  added  to a chain of clrefs.  The chain of clrefs has a
  4945.  
  4946.  
  4947.  
  4948.  
  4949.  
  4950.  
  4951.                                                      74
  4952.  
  4953.  
  4954.  
  4955.  
  4956.  
  4957.  
  4958.  
  4959.  
  4960.  
  4961.  
  4962.           header, which is a small buffer called a ``predicate reference'' (_p_r_r_e_f), which contains
  4963.  
  4964.  
  4965.  
  4966.           pointers  to  the beginning and end of its chain of clrefs.  Clause references are quite
  4967.  
  4968.  
  4969.  
  4970.           similar to ``database references'' of C-Prolog, and can be called.
  4971.  
  4972.  
  4973.  
  4974.  
  4975.                A prref is normally associated with a predicate symbol  and  contains  the  clauses
  4976.  
  4977.  
  4978.  
  4979.           that have been asserted for that symbol.  However, prrefs can  be constructed and clrefs
  4980.  
  4981.  
  4982.  
  4983.           added to them, and such clauses can be called, all without associating that  prref  with
  4984.  
  4985.  
  4986.  
  4987.           any  particular  predicate  symbol.  A predicate symbol that  has an associated prref is
  4988.  
  4989.  
  4990.  
  4991.           called a ``dynamic'' predicate (other  predicate  types  are  ``compiled''  and  ``unde-
  4992.  
  4993.  
  4994.  
  4995.           fined'').
  4996.  
  4997.  
  4998.  
  4999.  
  5000.                There are options as to where clrefs are  allocated and  how they are linked  to  a
  5001.  
  5002.  
  5003.  
  5004.           chain  of   clrefs  associated  with a  prref.   A clref can either be allocated in per-
  5005.  
  5006.  
  5007.  
  5008.           manent space (the normal arrangement) or as a sub-buffer of a superbuffer on  the  heap.
  5009.  
  5010.  
  5011.  
  5012.           When  adding  a   clref  to  a  prref chain, it can be be added at the beginning  of the
  5013.  
  5014.  
  5015.  
  5016.  
  5017.                                                      75
  5018.  
  5019.  
  5020.  
  5021.  
  5022.  
  5023.  
  5024.  
  5025.  
  5026.  
  5027.  
  5028.           chain or at the end of the chain.  In addition, one of the  argument  positions  can  be
  5029.  
  5030.  
  5031.  
  5032.           used to index, so that subsequent retrievals will try  to index on that position.
  5033.  
  5034.  
  5035.  
  5036.  
  5037.  
  5038.  
  5039.           assert(_C)
  5040.  
  5041.  
  5042.  
  5043.  
  5044.                The current instance of _C is interpreted as a clause and is added  to  the  program
  5045.  
  5046.  
  5047.  
  5048.                (with  new private variables replacing any uninstantiated variables), at the end of
  5049.  
  5050.  
  5051.  
  5052.                the list of clauses for that predicate.  _C must be instantiated to a non-variable.
  5053.  
  5054.  
  5055.  
  5056.  
  5057.           assert(_C, _O_p_t_s)
  5058.  
  5059.  
  5060.  
  5061.  
  5062.                As for _a_s_s_e_r_t/1, with _O_p_t_s a list of options.   The  options  currently  recognized
  5063.  
  5064.  
  5065.  
  5066.                are:
  5067.  
  5068.  
  5069.  
  5070.  
  5071.              first
  5072.  
  5073.  
  5074.  
  5075.  
  5076.  
  5077.  
  5078.  
  5079.  
  5080.  
  5081.  
  5082.  
  5083.                                                      76
  5084.  
  5085.  
  5086.  
  5087.  
  5088.  
  5089.  
  5090.  
  5091.  
  5092.  
  5093.  
  5094.                If on, causes the asserted clause to be added as the first clause of the  database.
  5095.  
  5096.  
  5097.  
  5098.                Default: off.
  5099.  
  5100.  
  5101.  
  5102.  
  5103.              index
  5104.  
  5105.  
  5106.  
  5107.  
  5108.                If on, causes an index to be  generated  on  the  first  argument  of  the  clause.
  5109.  
  5110.  
  5111.  
  5112.                Default: off.
  5113.  
  5114.  
  5115.  
  5116.  
  5117.              index(N)
  5118.  
  5119.  
  5120.  
  5121.  
  5122.                If on, causes an index to be  generated  on  the  N[th]  argument  of  the  clause.
  5123.  
  5124.  
  5125.  
  5126.                Default: off.
  5127.  
  5128.  
  5129.  
  5130.  
  5131.              q
  5132.  
  5133.  
  5134.  
  5135.  
  5136.                ``quick''.  If on, invokes an assertion algorithm that is simpler  and  more  effi-
  5137.  
  5138.  
  5139.  
  5140.                cient than the general one.  However, the code generated with the simpler algorithm
  5141.  
  5142.  
  5143.  
  5144.                may not be correct if there are repeated variables within  compound  terms  in  the
  5145.  
  5146.  
  5147.  
  5148.  
  5149.                                                      77
  5150.  
  5151.  
  5152.  
  5153.  
  5154.  
  5155.  
  5156.  
  5157.  
  5158.  
  5159.  
  5160.                body of the clause, e.g. in
  5161.  
  5162.  
  5163.  
  5164.                            p(X) :- q([X, X]).
  5165.  
  5166.  
  5167.  
  5168.  
  5169.                Default: off.
  5170.  
  5171.  
  5172.  
  5173.  
  5174.           asserti(_C,_N)
  5175.  
  5176.  
  5177.  
  5178.  
  5179.                The current instance of _C, interpreted as a clause, is asserted to the program with
  5180.  
  5181.  
  5182.  
  5183.                an index on its _N[_t_h] argument.  If _N is zero, no index is created.
  5184.  
  5185.  
  5186.  
  5187.  
  5188.           asserta(_C)
  5189.  
  5190.  
  5191.  
  5192.  
  5193.                Similar to assert(_C), except that the new clause becomes the _f_i_r_s_t  clause  of  the
  5194.  
  5195.  
  5196.  
  5197.                procedure concerned.
  5198.  
  5199.  
  5200.  
  5201.  
  5202.           asserta(_C, _R_e_f)
  5203.  
  5204.  
  5205.  
  5206.  
  5207.                Similar to asserta(_C), but also unifies _R_e_f with the clause reference of the clause
  5208.  
  5209.  
  5210.  
  5211.                asserted.
  5212.  
  5213.  
  5214.  
  5215.                                                      78
  5216.  
  5217.  
  5218.  
  5219.  
  5220.  
  5221.  
  5222.  
  5223.  
  5224.  
  5225.  
  5226.           assertz(_C)
  5227.  
  5228.  
  5229.  
  5230.  
  5231.                Similar to assert(_C), except that the new clause becomes the  _l_a_s_t  clause  of  the
  5232.  
  5233.  
  5234.  
  5235.                procedure concerned.
  5236.  
  5237.  
  5238.  
  5239.  
  5240.           assertz(_C, _R_e_f)
  5241.  
  5242.  
  5243.  
  5244.  
  5245.                Similar to assertz(_C), but also unifies _R_e_f with the clause reference of the clause
  5246.  
  5247.  
  5248.  
  5249.                asserted.
  5250.  
  5251.  
  5252.  
  5253.  
  5254.           assert_union(_P, _Q)
  5255.  
  5256.  
  5257.  
  5258.  
  5259.                The clauses for _Q are added to the clauses for _P.  For example, the call
  5260.  
  5261.  
  5262.  
  5263.                            | ?- assert_union(p(X,Y),q(X,Y)).
  5264.  
  5265.  
  5266.  
  5267.  
  5268.                has the effect of adding the rule
  5269.  
  5270.  
  5271.  
  5272.                    p(X,Y) :- q(X,Y).
  5273.  
  5274.  
  5275.  
  5276.  
  5277.                as the last rule defining _p/2.  If _P is not defined, it results in the  call  to  _Q
  5278.  
  5279.  
  5280.  
  5281.                                                      79
  5282.  
  5283.  
  5284.  
  5285.  
  5286.  
  5287.  
  5288.  
  5289.  
  5290.  
  5291.  
  5292.                being the only clause for _P.
  5293.  
  5294.  
  5295.  
  5296.  
  5297.  
  5298.                The variables in the arguments to _a_s_s_e_r_t__u_n_i_o_n/2  are  not  significant,  e.g.  the
  5299.  
  5300.  
  5301.  
  5302.                above would have been equivalent to
  5303.  
  5304.  
  5305.  
  5306.                            | ?- assert_union(p(Y,X),q(X,Y)).
  5307.                    or
  5308.                            | ?- assert_union(p(_,_),q(_,_)).
  5309.  
  5310.  
  5311.  
  5312.  
  5313.                However, the arities of the two predicates involved must match, e.g.   even  though
  5314.  
  5315.  
  5316.  
  5317.                the goal
  5318.  
  5319.  
  5320.  
  5321.                            | ?- assert_union(p(X,Y), r(X,Y,Z)).
  5322.  
  5323.  
  5324.  
  5325.  
  5326.                will succeed, the predicate _p/2 will not in any way depend on the clauses for _r/3.
  5327.  
  5328.  
  5329.  
  5330.  
  5331.           assert(_C_l_a_u_s_e,_A_Z,_I_n_d_e_x,_C_l_r_e_f)
  5332.  
  5333.  
  5334.  
  5335.  
  5336.                Asserts a clause to a predicate.  _C_l_a_u_s_e is the clause to  assert.   _A_Z  is  0  for
  5337.  
  5338.  
  5339.  
  5340.                insertion  as  the  first clause, 1 for insertion as the last clause.  _I_n_d_e_x is the
  5341.  
  5342.  
  5343.  
  5344.  
  5345.  
  5346.  
  5347.                                                      80
  5348.  
  5349.  
  5350.  
  5351.  
  5352.  
  5353.  
  5354.  
  5355.  
  5356.  
  5357.  
  5358.                number of the argument on which to  index (0 for no indexing).  _C_l_r_e_f  is  returned
  5359.  
  5360.  
  5361.  
  5362.                as the  clause reference of the fact newly asserted.  If the main functor symbol of
  5363.  
  5364.  
  5365.  
  5366.                _C_l_a_u_s_e has been declared (by $_a_s_s_e_r_t_f__a_l_l_o_c__t/2, see below) to have its clauses  on
  5367.  
  5368.  
  5369.  
  5370.                the  heap,  the clref will be allocated there. If the predicate symbol of _C_l_a_u_s_e is
  5371.  
  5372.  
  5373.  
  5374.                undefined, it will be initialized and _C_l_a_u_s_e added. If  the  predicate  symbol  has
  5375.  
  5376.  
  5377.  
  5378.                compiled clauses, it is first converted to be dynamic (see _s_y_m_t_y_p_e/2, Section 5.10)
  5379.  
  5380.  
  5381.  
  5382.                by adding a special clref that calls the compiled clauses.  _F_a_c_t, _A_Z and _I_n_d_e_x  are
  5383.  
  5384.  
  5385.  
  5386.                input arguments, and should be instantiated at the time of call; _C_l_r_e_f is an output
  5387.  
  5388.  
  5389.  
  5390.                argument, and should be uninstantiated at the time of call.
  5391.  
  5392.  
  5393.  
  5394.  
  5395.           retract(_H_e_a_d)
  5396.  
  5397.  
  5398.  
  5399.  
  5400.                The first clause in the program whose head matches _H_e_a_d is erased.  This  predicate
  5401.  
  5402.  
  5403.  
  5404.                may  be used in a non-deterministic fashion, i.e. it will successively backtrack to
  5405.  
  5406.  
  5407.  
  5408.                retract clauses whose heads match _H_e_a_d.  _H_e_a_d must be initially instantiated  to  a
  5409.  
  5410.  
  5411.  
  5412.  
  5413.                                                      81
  5414.  
  5415.  
  5416.  
  5417.  
  5418.  
  5419.  
  5420.  
  5421.  
  5422.  
  5423.  
  5424.                non-variable.
  5425.  
  5426.  
  5427.  
  5428.  
  5429.           abolish(_P)
  5430.  
  5431.  
  5432.  
  5433.  
  5434.                Completely remove all clauses for the procedure with head  _P  (which  should  be  a
  5435.  
  5436.  
  5437.  
  5438.                term). For example, the goal
  5439.  
  5440.  
  5441.  
  5442.                    | ?- abolish( p(_, _, _) ).
  5443.  
  5444.  
  5445.  
  5446.  
  5447.                removes all clauses for the predicate _p/3.
  5448.  
  5449.  
  5450.  
  5451.  
  5452.           abolish(_P, _N)
  5453.  
  5454.  
  5455.  
  5456.  
  5457.                Completely remove all clauses for the predicate _P (which should be  an  atom)  with
  5458.  
  5459.  
  5460.  
  5461.                arity _N (which should be an integer).
  5462.  
  5463.  
  5464.  
  5465.  
  5466.           call_ref(_C_a_l_l, _R_e_f)
  5467.  
  5468.  
  5469.  
  5470.  
  5471.                Calls the predicate whose database reference (prref) is _R_e_f, using the literal _C_a_l_l
  5472.  
  5473.  
  5474.  
  5475.                as the call.  This is similar to call_ref(_C_a_l_l, _R_e_f, 0).
  5476.  
  5477.  
  5478.  
  5479.                                                      82
  5480.  
  5481.  
  5482.  
  5483.  
  5484.  
  5485.  
  5486.  
  5487.  
  5488.  
  5489.  
  5490.           call_ref(_C_a_l_l, _R_e_f, _T_r)
  5491.  
  5492.  
  5493.  
  5494.  
  5495.                Calls the predicate whose database reference (prref) is _R_e_f, using the literal _C_a_l_l
  5496.  
  5497.  
  5498.  
  5499.                as  the  call.   _T_r  must  be  either 0 or 1: if _T_r is 0 then the call _C_a_l_l is made
  5500.  
  5501.  
  5502.  
  5503.                assuming the ``trust'' optimization will be made; if _T_r is  1  then  the  ``trust''
  5504.  
  5505.  
  5506.  
  5507.                optimization  is  not used, so that any new fact added before final failure will be
  5508.  
  5509.  
  5510.  
  5511.                seen by _C_a_l_l.  (Also, this currently does not take advantage of any  indexing  that
  5512.  
  5513.  
  5514.  
  5515.                might  have been constructed, while $_d_b__c_a_l_l__p_r_r_e_f does.)  _C_a_l_l, _R_e_f and _T_r are all
  5516.  
  5517.  
  5518.  
  5519.                input arguments, and should be instantiated at the time of call.
  5520.  
  5521.  
  5522.  
  5523.  
  5524.  
  5525.  
  5526.           The basic library predicates that support the manipulation of prrefs and clrefs  are  as
  5527.  
  5528.  
  5529.  
  5530.           follows:
  5531.  
  5532.  
  5533.  
  5534.  
  5535.           $db_new_prref(_P_r_r_e_f,_W_h_e_r_e,_S_u_p_b_u_f_f)
  5536.  
  5537.  
  5538.  
  5539.  
  5540.  
  5541.  
  5542.  
  5543.  
  5544.  
  5545.                                                      83
  5546.  
  5547.  
  5548.  
  5549.  
  5550.  
  5551.  
  5552.  
  5553.  
  5554.  
  5555.  
  5556.                Creates an empty Prref, i.e. one with no facts in it. If  called,  it  will  simply
  5557.  
  5558.  
  5559.  
  5560.                fail.  _W_h_e_r_e indicates where the prref should be allocated:  a value of 0 indicates
  5561.  
  5562.  
  5563.  
  5564.                the permanent area, while a value of 2 indicates that it is to be  allocated  as  a
  5565.  
  5566.  
  5567.  
  5568.                subbuffer.   _S_u_p_b_u_f_f is the superbuffer from which to allocate _P_r_r_e_f if _W_h_e_r_e is 2.
  5569.  
  5570.  
  5571.  
  5572.                _W_h_e_r_e should be instantiated at the time of call, while _P_r_r_e_f should  be  uninstan-
  5573.  
  5574.  
  5575.  
  5576.                tiated;  in  addition, if _W_h_e_r_e is 2, _S_u_p_b_u_f_f should be instantiated at the time of
  5577.  
  5578.  
  5579.  
  5580.                call.
  5581.  
  5582.  
  5583.  
  5584.  
  5585.           $db_assert_fact(_F_a_c_t,_P_r_r_e_f,_A_Z,_I_n_d_e_x,_C_l_r_e_f,_W_h_e_r_e,_S_u_p_b_u_f_f)
  5586.  
  5587.  
  5588.  
  5589.  
  5590.                _F_a_c_t is a fact to be asserted; _P_r_r_e_f is a predicate reference to which to  add  the
  5591.  
  5592.  
  5593.  
  5594.                asserted  fact; _A_Z is either 0, indicating the fact should be inserted as the first
  5595.  
  5596.  
  5597.  
  5598.                clause in _P_r_r_e_f, or 1, indicating it should be inserted as the last; _I_n_d_e_x is 0  if
  5599.  
  5600.  
  5601.  
  5602.                no  index  is to be built, or _n if an index on the _n[th] argument of the fact is to
  5603.  
  5604.  
  5605.  
  5606.                be used.  (Asserting at the beginning  of  the  chain  with  indexing  is  not  yet
  5607.  
  5608.  
  5609.  
  5610.  
  5611.                                                      84
  5612.  
  5613.  
  5614.  
  5615.  
  5616.  
  5617.  
  5618.  
  5619.  
  5620.  
  5621.  
  5622.                supported.)  _W_h_e_r_e indicates where the clref is to be allocated: a value of 0 indi-
  5623.  
  5624.  
  5625.  
  5626.                cates that it should be in the permanent area, while a value of 2 indicates that it
  5627.  
  5628.  
  5629.  
  5630.                should  be  allocated as a subbuffer of _S_u_p_b_u_f_f.  _C_l_r_e_f is returned and it  is  the
  5631.  
  5632.  
  5633.  
  5634.                clause reference  of the  asserted fact.   _F_a_c_t, _P_r_r_e_f, _A_Z,  _I_n_d_e_x  and  _W_h_e_r_e  are
  5635.  
  5636.  
  5637.  
  5638.                input  arguments,  and  should be instantiated at the time of call; in addition, if
  5639.  
  5640.  
  5641.  
  5642.                _W_h_e_r_e is 2, then _S_u_p_b_u_f_f should also be instantiated.  _C_l_r_e_f is an output argument,
  5643.  
  5644.  
  5645.  
  5646.                and should be uninstantiated at the time of call.
  5647.  
  5648.  
  5649.  
  5650.  
  5651.           $db_add_clref(_F_a_c_t,_P_r_r_e_f,_A_Z,_I_n_d_e_x,_C_l_r_e_f,_W_h_e_r_e,_S_u_p_b_u_f_f)
  5652.  
  5653.  
  5654.  
  5655.  
  5656.                Adds the clref _C_l_r_e_f to the prref _P_r_r_e_f. _F_a_c_t is the fact that  has  been  compiled
  5657.  
  5658.  
  5659.  
  5660.                into _C_l_r_e_f (used only to get the arity and for indexing).  The other parameters are
  5661.  
  5662.  
  5663.  
  5664.                as for $_d_b__a_s_s_e_r_t__f_a_c_t/7.
  5665.  
  5666.  
  5667.  
  5668.  
  5669.           $db_call_prref(_C_a_l_l,_P_r_r_e_f)
  5670.  
  5671.  
  5672.  
  5673.  
  5674.  
  5675.  
  5676.  
  5677.                                                      85
  5678.  
  5679.  
  5680.  
  5681.  
  5682.  
  5683.  
  5684.  
  5685.  
  5686.  
  5687.  
  5688.                Calls the prref _P_r_r_e_f using the literal _C_a_l_l as the call.  The call is done by sim-
  5689.  
  5690.  
  5691.  
  5692.                ply  branching  to  the first clause.  New facts added to _P_r_r_e_f after the last fact
  5693.  
  5694.  
  5695.  
  5696.                has been retrieved by _C_a_l_l, but before _C_a_l_l is failed through, will  _n_o_t  be  used.
  5697.  
  5698.  
  5699.  
  5700.                Both  _C_a_l_l and _P_r_r_e_f are input arguments, and should be instantiated at the time of
  5701.  
  5702.  
  5703.  
  5704.                call.
  5705.  
  5706.  
  5707.  
  5708.  
  5709.           $db_call_prref_s(_C_a_l_l,_P_r_r_e_f)
  5710.  
  5711.  
  5712.  
  5713.  
  5714.                This also  calls the prref _P_r_r_e_f using _C_a_l_l as  the  call.   The   difference  from
  5715.  
  5716.  
  5717.  
  5718.                $_d_b__c_a_l_l__p_r_r_e_f  is  that  this does not use the ``trust'' optimization, so that any
  5719.  
  5720.  
  5721.  
  5722.                new fact added before final failure will be seen by _C_a_l_l.   (Also,  this  currently
  5723.  
  5724.  
  5725.  
  5726.                does  not  take  advantage  of any indexing that might have been constructed, while
  5727.  
  5728.  
  5729.  
  5730.                $_d_b__c_a_l_l__p_r_r_e_f does.)  Both _C_a_l_l and _P_r_r_e_f  are  input  arguments,  and  should  be
  5731.  
  5732.  
  5733.  
  5734.                instantiated at the time of call.
  5735.  
  5736.  
  5737.  
  5738.  
  5739.  
  5740.  
  5741.  
  5742.  
  5743.                                                      86
  5744.  
  5745.  
  5746.  
  5747.  
  5748.  
  5749.  
  5750.  
  5751.  
  5752.  
  5753.  
  5754.           $db_get_clauses(_P_r_r_e_f,_C_l_r_e_f,_D_i_r)
  5755.  
  5756.  
  5757.  
  5758.  
  5759.                This returns, nondeterministically, all the clause  references  _C_l_r_e_f  for  clauses
  5760.  
  5761.  
  5762.  
  5763.                asserted to prref _P_r_r_e_f.  If _D_i_r is 0, then the first clref on the list is returned
  5764.  
  5765.  
  5766.  
  5767.                first; if _D_i_r is 1, then they are returned in reverse order.   _P_r_r_e_f  and  _D_i_r  are
  5768.  
  5769.  
  5770.  
  5771.                input arguments, and should be instantiated at the time of call; _C_l_r_e_f is an output
  5772.  
  5773.  
  5774.  
  5775.                argument, and should be uninstantiated at the time of call.
  5776.  
  5777.  
  5778.  
  5779.  
  5780.           $db_kill_clause(_C_l_r_e_f)
  5781.  
  5782.  
  5783.  
  5784.  
  5785.                This predicate retracts the fact referenced by clref _C_l_r_e_f.  It does this by simply
  5786.  
  5787.  
  5788.  
  5789.                making  the  first  instruction  of the clause a _f_a_i_l instruction.  This means that
  5790.  
  5791.  
  5792.  
  5793.                this clause will fail whenever it is called in the future,  no  matter  how  it  is
  5794.  
  5795.  
  5796.  
  5797.                accessed.  _C_l_r_e_f should be instantiated at the time of call.
  5798.  
  5799.  
  5800.  
  5801.  
  5802.  
  5803.  
  5804.  
  5805.  
  5806.  
  5807.  
  5808.  
  5809.                                                      87
  5810.  
  5811.  
  5812.  
  5813.  
  5814.  
  5815.  
  5816.  
  5817.  
  5818.  
  5819.  
  5820.           _5._1_0.  _E_n_v_i_r_o_n_m_e_n_t_a_l
  5821.  
  5822.  
  5823.  
  5824.  
  5825.  
  5826.  
  5827.           op(_p_r_i_o_r_i_t_y, _t_y_p_e, _n_a_m_e)
  5828.  
  5829.  
  5830.  
  5831.  
  5832.                Treat _n_a_m_e as an operator of the stated _t_y_p_e and _p_r_i_o_r_i_t_y (see Section 3.2).   _n_a_m_e
  5833.  
  5834.  
  5835.  
  5836.                may  also  be  a  list of names, in which all are to be treated as operators of the
  5837.  
  5838.  
  5839.  
  5840.                stated _t_y_p_e and _p_r_i_o_r_i_t_y.
  5841.  
  5842.  
  5843.  
  5844.  
  5845.           break
  5846.  
  5847.  
  5848.  
  5849.  
  5850.                Causes the current execution to be suspended at the next procedure call.  Then  the
  5851.  
  5852.  
  5853.  
  5854.                message  ``[  Break  (level  1) ]'' is displayed.  The interpreter is then ready to
  5855.  
  5856.  
  5857.  
  5858.                accept input as though it was at the top level (except that at break level _n  >  0,
  5859.  
  5860.  
  5861.  
  5862.                the prompt is ``_n: ?- '').  If another call of break is encountered, it moves up to
  5863.  
  5864.  
  5865.  
  5866.                level 2, and so on.  To  close  the  break  and  resume  the  execution  which  was
  5867.  
  5868.  
  5869.  
  5870.                suspended,  type  the  END-OF-INPUT  character.   Execution  will be resumed at the
  5871.  
  5872.  
  5873.  
  5874.  
  5875.                                                      88
  5876.  
  5877.  
  5878.  
  5879.  
  5880.  
  5881.  
  5882.  
  5883.  
  5884.  
  5885.  
  5886.                procedure call where it had been suspended.  Alternatively, the suspended execution
  5887.  
  5888.  
  5889.  
  5890.                can  be  aborted by calling the evaluable predicate abort, which causes a return to
  5891.  
  5892.  
  5893.  
  5894.                the top level.
  5895.  
  5896.  
  5897.  
  5898.  
  5899.           abort
  5900.  
  5901.  
  5902.  
  5903.  
  5904.                Aborts the current execution, taking you back to top level.
  5905.  
  5906.  
  5907.  
  5908.  
  5909.           save(_F)
  5910.  
  5911.  
  5912.  
  5913.  
  5914.                The system saves the current state of the system into file _F.
  5915.  
  5916.  
  5917.  
  5918.  
  5919.           restore(_F)
  5920.  
  5921.  
  5922.  
  5923.  
  5924.                The system restores the saved state in file _F to be the current  state.   One  res-
  5925.  
  5926.  
  5927.  
  5928.                triction  imposed  by  the  current  system is that various system parameters (e.g.
  5929.  
  5930.  
  5931.  
  5932.                stack sizes, permanent space, heap space, etc.) of the saved state have to  be  the
  5933.  
  5934.  
  5935.  
  5936.                same  as  that of the current invocation.  Thus, it is not possible to save a state
  5937.  
  5938.  
  5939.  
  5940.  
  5941.                                                      89
  5942.  
  5943.  
  5944.  
  5945.  
  5946.  
  5947.  
  5948.  
  5949.  
  5950.  
  5951.  
  5952.                from an invocation where 50000 words of permanent space  had  been  allocated,  and
  5953.  
  5954.  
  5955.  
  5956.                then restore the same state in an invocation with 100000 words of permanent space.
  5957.  
  5958.  
  5959.  
  5960.  
  5961.           cputime(_X)
  5962.  
  5963.  
  5964.  
  5965.  
  5966.                Unifies _X with the time elapsed, in milliseconds, since the system was started up.
  5967.  
  5968.  
  5969.  
  5970.  
  5971.           $getenv(_V_a_r,_V_a_l)
  5972.  
  5973.  
  5974.  
  5975.  
  5976.                _V_a_l is unified with the value of the Unix environment variable _V_a_r.
  5977.  
  5978.  
  5979.  
  5980.  
  5981.           statistics
  5982.  
  5983.  
  5984.  
  5985.  
  5986.                Prints out the current allocations and amounts of space used for each of  the  four
  5987.  
  5988.  
  5989.  
  5990.                main  areas:  the  permanent  area, the local stack, the global stack and the trail
  5991.  
  5992.  
  5993.  
  5994.                stack.  Does not work well unless the simulator has been called with the -s  option
  5995.  
  5996.  
  5997.  
  5998.                (see Section 7.2).
  5999.  
  6000.  
  6001.  
  6002.  
  6003.  
  6004.  
  6005.  
  6006.  
  6007.                                                      90
  6008.  
  6009.  
  6010.  
  6011.  
  6012.  
  6013.  
  6014.  
  6015.  
  6016.  
  6017.  
  6018.           symtype(_T,_N)
  6019.  
  6020.  
  6021.  
  6022.  
  6023.                Unifies _N with the ``internal type'' of the principal functor of the term _T,  which
  6024.  
  6025.  
  6026.  
  6027.                must  be  instantiated at the time of the call.  _N is bound to 0 if _T does not have
  6028.  
  6029.  
  6030.  
  6031.                an entry point defined (i.e. cannot be executed); to 1 if the principal functor  of
  6032.  
  6033.  
  6034.  
  6035.                _T  is ``dynamic'', i.e. has asserted code; to 2 if the principal functor for _T is a
  6036.  
  6037.  
  6038.  
  6039.                compiled predicate; and 3 if _T denotes a buffer.  Thus, for example, if the  predi-
  6040.  
  6041.  
  6042.  
  6043.                cate _p/2 is a compiled predicate which has been loaded into the system, the goal
  6044.  
  6045.  
  6046.  
  6047.                            | ?- symtype(p(_,_), X).
  6048.  
  6049.  
  6050.  
  6051.  
  6052.                will succeed binding X to 2; on the other hand, the goal
  6053.  
  6054.  
  6055.  
  6056.                            | ?- assert(q(a,b,c)), symtype(q(_,_,_), X).
  6057.  
  6058.  
  6059.  
  6060.  
  6061.                will succeed binding X to 1.
  6062.  
  6063.  
  6064.  
  6065.  
  6066.  
  6067.  
  6068.  
  6069.  
  6070.  
  6071.  
  6072.  
  6073.                                                      91
  6074.  
  6075.  
  6076.  
  6077.  
  6078.  
  6079.  
  6080.  
  6081.  
  6082.  
  6083.  
  6084.           system(_C_a_l_l)
  6085.  
  6086.  
  6087.  
  6088.  
  6089.                Calls the operating system with the atom _C_a_l_l as argument.  For example, the call
  6090.  
  6091.  
  6092.  
  6093.                    | ?- system('ls').
  6094.  
  6095.  
  6096.  
  6097.  
  6098.                will produce a directory listing.  Since _s_y_s_t_e_m/1 is  executed  by  forking  off  a
  6099.  
  6100.  
  6101.  
  6102.                shell  process,  it cannot be used, for example, to change the working directory of
  6103.  
  6104.  
  6105.  
  6106.                the simulator.
  6107.  
  6108.  
  6109.  
  6110.  
  6111.           syscall(_N,_A_r_g_s,_R_e_s)
  6112.  
  6113.  
  6114.  
  6115.  
  6116.                Executes the Unix system call number _N with arguments _A_r_g_s, and returns the  result
  6117.  
  6118.  
  6119.  
  6120.                in  _R_e_s.  _N  is  an  integer, and _A_r_g_s a Prolog list of the arguments to the system
  6121.  
  6122.  
  6123.  
  6124.                call.  For example, to execute the system call ``_c_r_e_a_t(_F_i_l_e,_M_o_d_e)'',  knowing  that
  6125.  
  6126.  
  6127.  
  6128.                the syscall number for the Unix command _c_r_e_a_t(2) is 8, we execute the goal
  6129.  
  6130.  
  6131.  
  6132.                    | ?- syscall(8, [_F_i_l_e, _M_o_d_e], _D_e_s).
  6133.  
  6134.  
  6135.  
  6136.  
  6137.  
  6138.  
  6139.                                                      92
  6140.  
  6141.  
  6142.  
  6143.  
  6144.  
  6145.  
  6146.  
  6147.  
  6148.  
  6149.  
  6150.                where _D_e_s is the file descriptor returned by _c_r_e_a_t.  The syscall numbers  for  some
  6151.  
  6152.  
  6153.  
  6154.                Unix system calls are given in Table 2.
  6155.  
  6156.  
  6157.  
  6158.  
  6159.           _5._1_1.  _G_l_o_b_a_l _V_a_l_u_e_s
  6160.  
  6161.  
  6162.  
  6163.  
  6164.  
  6165.  
  6166.                SB-Prolog has some primitives that  permit  the  programmer  to  manipulate  global
  6167.  
  6168.  
  6169.  
  6170.           values.  These are provided primarily as an efficiency hack, and needless to say, should
  6171.  
  6172.  
  6173.  
  6174.           be used with a great deal of care.
  6175.  
  6176.  
  6177.           ________________________________________________________________________________________
  6178.                exit                          1                fork                         2
  6179.                read                          3                write                        4
  6180.                open                          5                close                        6
  6181.                creat                         8                link                         9
  6182.                unlink                       10                chdir                       12
  6183.                chmod                        15                lseek                       19
  6184.                access                       33                kill                        37
  6185.                wait                         84                socket                      97
  6186.                connect                      98                accept                      99
  6187.                send                        101                recv                       102
  6188.                bind                        104                setsockopt                 105
  6189.                listen                      106                recvmsg                    113
  6190.                sendmsg                     114                getsockopt                 118
  6191.                recvfrom                    125                sendto                     133
  6192.                socketpair                  135                mkdir                      136
  6193.                rmdir                       137                getsockname                150
  6194.           ________________________________________________________________________________________
  6195.  
  6196.          |
  6197.          |
  6198.          |
  6199.          |
  6200.          |
  6201.          |
  6202.          |
  6203.          |
  6204.          |
  6205.          |
  6206.          |
  6207.          |
  6208.          |
  6209.          |
  6210.          |
  6211.          |
  6212.          |
  6213.  
  6214.  
  6215.  
  6216.  
  6217.  
  6218.  
  6219.  
  6220.  
  6221.  
  6222.  
  6223.  
  6224.  
  6225.  
  6226.  
  6227.  
  6228.                                                      |
  6229.                                                      |
  6230.                                                      |
  6231.                                                      |
  6232.                                                      |
  6233.                                                      |
  6234.                                                      |
  6235.                                                      |
  6236.                                                      |
  6237.                                                      |
  6238.                                                      |
  6239.                                                      |
  6240.                                                      |
  6241.                                                      |
  6242.                                                      |
  6243.                                                      |
  6244.                                                      |
  6245.  
  6246.  
  6247.  
  6248.  
  6249.  
  6250.  
  6251.  
  6252.  
  6253.  
  6254.  
  6255.  
  6256.  
  6257.  
  6258.  
  6259.  
  6260.                                                                                                  |
  6261.                                                                                                  |
  6262.                                                                                                  |
  6263.                                                                                                  |
  6264.                                                                                                  |
  6265.                                                                                                  |
  6266.                                                                                                  |
  6267.                                                                                                  |
  6268.                                                                                                  |
  6269.                                                                                                  |
  6270.                                                                                                  |
  6271.                                                                                                  |
  6272.                                                                                                  |
  6273.                                                                                                  |
  6274.                                                                                                  |
  6275.                                                                                                  |
  6276.                                                                                                  |
  6277.  
  6278.  
  6279.  
  6280.  
  6281.  
  6282.  
  6283.  
  6284.  
  6285.  
  6286.  
  6287.  
  6288.  
  6289.  
  6290.  
  6291.  
  6292.  
  6293.  
  6294.  
  6295.                             Table 2: Some Syscall Numbers for Unix Systems Calls
  6296.  
  6297.  
  6298.  
  6299.  
  6300.  
  6301.  
  6302.  
  6303.                                                      93
  6304.  
  6305.  
  6306.  
  6307.  
  6308.  
  6309.  
  6310.  
  6311.  
  6312.  
  6313.  
  6314.           globalset(_T_e_r_m)
  6315.  
  6316.  
  6317.  
  6318.  
  6319.                Allows the user to save a global value.  _T_e_r_m must be bound to a compound term, say
  6320.  
  6321.  
  6322.  
  6323.                _p(_V).  _V  must be a number or a constant or a variable.  If _V is a number or a con-
  6324.  
  6325.  
  6326.  
  6327.                stant, the effect of _g_l_o_b_a_l_s_e_t(_p(_V)) can be described as:
  6328.  
  6329.  
  6330.  
  6331.                    retract(p(_)), assert(p(V)).
  6332.  
  6333.  
  6334.  
  6335.  
  6336.                I.e., _p is a predicate that when called will, from now on (until some other  change
  6337.  
  6338.  
  6339.  
  6340.                by  _g_l_o_b_a_l_s_e_t/1), deterministically return _V.  If _V is a variable, the effect is to
  6341.  
  6342.  
  6343.  
  6344.                make _V a global variable whose value is accessible by calling _p. For example,  exe-
  6345.  
  6346.  
  6347.  
  6348.                cuting  ``_g_l_o_b_a_l_s_e_t(_p(_X))''  makes _X a global variable. _X can be set by unification
  6349.  
  6350.  
  6351.  
  6352.                with some other term.  On backtracking, _X will be restored to its earlier value.
  6353.  
  6354.  
  6355.  
  6356.  
  6357.           gennum(_N_e_w_n_u_m)
  6358.  
  6359.  
  6360.  
  6361.  
  6362.  
  6363.  
  6364.  
  6365.  
  6366.  
  6367.  
  6368.  
  6369.                                                      94
  6370.  
  6371.  
  6372.  
  6373.  
  6374.  
  6375.  
  6376.  
  6377.  
  6378.  
  6379.  
  6380.                gennum/1 sets its argument to a new integer every time it is invoked.
  6381.  
  6382.  
  6383.  
  6384.  
  6385.           gensym(_C,_N_e_w_s_y_m)
  6386.  
  6387.  
  6388.  
  6389.  
  6390.                gensym/2 sets its second argument to an atom whose name is  made  by  concatenating
  6391.  
  6392.  
  6393.  
  6394.                the  name of the atom _C to the current gennum number. This new constant is bound to
  6395.  
  6396.  
  6397.  
  6398.                _N_e_w_s_y_m.  For example, if the current _g_e_n_n_u_m number is 37, then the call
  6399.  
  6400.  
  6401.  
  6402.                    | ?- gensym(aaa,X)
  6403.  
  6404.  
  6405.  
  6406.  
  6407.                will succeed binding _X to the atom `aaa37'.
  6408.  
  6409.  
  6410.  
  6411.  
  6412.           _6.  _D_e_b_u_g_g_i_n_g
  6413.  
  6414.  
  6415.  
  6416.  
  6417.  
  6418.  
  6419.           _6._1.  _H_i_g_h _L_e_v_e_l _T_r_a_c_i_n_g
  6420.  
  6421.  
  6422.  
  6423.  
  6424.  
  6425.  
  6426.                The preferred method of tracing execution is through the predicate  _t_r_a_c_e/1.   This
  6427.  
  6428.  
  6429.  
  6430.           predicate takes as argument a term _P/_N, where _P is a predicate name and _N its arity, and
  6431.  
  6432.  
  6433.  
  6434.  
  6435.                                                      95
  6436.  
  6437.  
  6438.  
  6439.  
  6440.  
  6441.  
  6442.  
  6443.  
  6444.  
  6445.  
  6446.           sets a ``trace point'' on the corresponding predicate; it can also be given  a  list  of
  6447.  
  6448.  
  6449.  
  6450.           such terms, in which case a trace point is set on each member of the list.  For example,
  6451.  
  6452.  
  6453.  
  6454.           executing
  6455.  
  6456.  
  6457.  
  6458.                       | ?- trace(pred1/2), trace([pred2/3, pred3/2]).
  6459.  
  6460.  
  6461.  
  6462.  
  6463.           sets trace points on predicates _p_r_e_d_1/2, _p_r_e_d_2/3 and _p_r_e_d_3/2.  Only those predicates are
  6464.  
  6465.  
  6466.  
  6467.           traced that have trace points set on them.
  6468.  
  6469.  
  6470.  
  6471.  
  6472.                If all the predicates in a file are to be traced, it is usually convenient  to  use
  6473.  
  6474.  
  6475.  
  6476.           the _P_r_e_d_L_i_s_t parameter of _c_o_m_p_i_l_e/4 or _c_o_n_s_u_l_t/3, e.g.:
  6477.  
  6478.  
  6479.  
  6480.                       | ?- compile(foo, 'foo.out', [t,v], Preds), load('foo.out'), trace(Preds).
  6481.               or
  6482.                       | ?- consult(foo, [v], Preds), trace(Preds).
  6483.  
  6484.  
  6485.  
  6486.  
  6487.           Notice that in the first case, the t compiler option (see Section 8.3) should be  speci-
  6488.  
  6489.  
  6490.  
  6491.           fied  in  order  to turn off certain assembler optimizations and facilitate tracing.  In
  6492.  
  6493.  
  6494.  
  6495.           the second case, the same effect may be achieved by specifying the t option to _c_o_n_s_u_l_t.
  6496.  
  6497.  
  6498.  
  6499.  
  6500.  
  6501.                                                      96
  6502.  
  6503.  
  6504.  
  6505.  
  6506.  
  6507.  
  6508.  
  6509.  
  6510.  
  6511.  
  6512.                The trace points set on predicates may be overwritten by loading  byte  code  files
  6513.  
  6514.  
  6515.  
  6516.           via _l_o_a_d/1, and in this case it may be necessary to explicitly set trace points again on
  6517.  
  6518.  
  6519.  
  6520.           the loaded predicates.  This does not happen with _c_o_n_s_u_l_t: predicates  that  were  being
  6521.  
  6522.  
  6523.  
  6524.           traced continue to have trace points set after consulting.
  6525.  
  6526.  
  6527.  
  6528.  
  6529.                The tracing facilities of SB-Prolog are in many ways very similar to  those  of  C-
  6530.  
  6531.  
  6532.  
  6533.           Prolog.   However,  leashing  is  not supported, and only those predicates can be traced
  6534.  
  6535.  
  6536.  
  6537.           which have had trace points set on them through _t_r_a_c_e/1.  This makes _t_r_a_c_e/1  and  _s_p_y/1
  6538.  
  6539.  
  6540.  
  6541.           very  similar:  essentially,  trace  amounts to two levels of spy points.  In SB-Prolog,
  6542.  
  6543.  
  6544.  
  6545.           tracing occurs at _C_a_l_l (i.e. entry to a predicate), successful _E_x_i_t from a  clause,  and
  6546.  
  6547.  
  6548.  
  6549.           _F_a_i_l_u_r_e of the entire call.  The tracing options available during debugging are the fol-
  6550.  
  6551.  
  6552.  
  6553.           lowing:
  6554.  
  6555.  
  6556.  
  6557.  
  6558.           c, NEWLINE: Creep
  6559.  
  6560.  
  6561.  
  6562.                Causes the system to single-step to the next port  (i.e.  either  the  entry  to  a
  6563.  
  6564.  
  6565.  
  6566.  
  6567.                                                      97
  6568.  
  6569.  
  6570.  
  6571.  
  6572.  
  6573.  
  6574.  
  6575.  
  6576.  
  6577.  
  6578.                traced predicate called by the executed clause, or the success or failure exit from
  6579.  
  6580.  
  6581.  
  6582.                that clause).
  6583.  
  6584.  
  6585.  
  6586.  
  6587.           a: Abort
  6588.  
  6589.  
  6590.  
  6591.                Causes execution to abort and control to return to the top level interpreter.
  6592.  
  6593.  
  6594.  
  6595.  
  6596.           b: Break
  6597.  
  6598.  
  6599.  
  6600.                Calls the evaluable predicate _b_r_e_a_k, thus invoking recursively a new incarnation of
  6601.  
  6602.  
  6603.  
  6604.                the system interpreter.  The command prompt at break level _n is
  6605.  
  6606.  
  6607.  
  6608.                            _n: ?-
  6609.  
  6610.  
  6611.  
  6612.  
  6613.                The user may return to the previous break level by entering the system  end-of-file
  6614.  
  6615.  
  6616.  
  6617.                character  (e.g.  ctrl-D),  or  typing in the atom _e_n_d__o_f__f_i_l_e; or to the top level
  6618.  
  6619.  
  6620.  
  6621.                interpreter by typing in _a_b_o_r_t.
  6622.  
  6623.  
  6624.  
  6625.  
  6626.  
  6627.  
  6628.  
  6629.  
  6630.  
  6631.  
  6632.  
  6633.                                                      98
  6634.  
  6635.  
  6636.  
  6637.  
  6638.  
  6639.  
  6640.  
  6641.  
  6642.  
  6643.  
  6644.           f: Fail
  6645.  
  6646.  
  6647.  
  6648.                Causes execution to fail, thus transferring control to the Fail port of the current
  6649.  
  6650.  
  6651.  
  6652.                execution.
  6653.  
  6654.  
  6655.  
  6656.  
  6657.           h: Help
  6658.  
  6659.  
  6660.  
  6661.                Displays the table of debugging options.
  6662.  
  6663.  
  6664.  
  6665.  
  6666.           l: Leap
  6667.  
  6668.  
  6669.  
  6670.                Causes the system to resume running the program, only stopping when a spy-point  is
  6671.  
  6672.  
  6673.  
  6674.                reached or the program terminates.  This allows the user to follow the execution at
  6675.  
  6676.  
  6677.  
  6678.                a higher level than exhaustive tracing.
  6679.  
  6680.  
  6681.  
  6682.  
  6683.           n: Nodebug
  6684.  
  6685.  
  6686.  
  6687.                Turns off debug mode.
  6688.  
  6689.  
  6690.  
  6691.  
  6692.  
  6693.  
  6694.  
  6695.  
  6696.  
  6697.  
  6698.  
  6699.                                                      99
  6700.  
  6701.  
  6702.  
  6703.  
  6704.  
  6705.  
  6706.  
  6707.  
  6708.  
  6709.  
  6710.           q: Quasi-skip
  6711.  
  6712.  
  6713.  
  6714.                This is like Skip except that it does not mask out spy points.
  6715.  
  6716.  
  6717.  
  6718.  
  6719.           r: Retry (fail)
  6720.  
  6721.  
  6722.  
  6723.                Transfers to the Call port of the current goal.  Note, however, that side  effects,
  6724.  
  6725.  
  6726.  
  6727.                such as database modifications etc., are not undone.
  6728.  
  6729.  
  6730.  
  6731.  
  6732.           s: Skip
  6733.  
  6734.  
  6735.  
  6736.                Causes tracing to be turned off for the entire execution of the  procedure.   Thus,
  6737.  
  6738.  
  6739.  
  6740.                nothing  is  seen until control comes back to that procedure, either at the Success
  6741.  
  6742.  
  6743.  
  6744.                or the Failure port.
  6745.  
  6746.  
  6747.  
  6748.  
  6749.  
  6750.  
  6751.           Other predicates that are useful in debugging are:
  6752.  
  6753.  
  6754.  
  6755.  
  6756.  
  6757.  
  6758.  
  6759.  
  6760.  
  6761.  
  6762.  
  6763.  
  6764.  
  6765.                                                     100
  6766.  
  6767.  
  6768.  
  6769.  
  6770.  
  6771.  
  6772.  
  6773.  
  6774.  
  6775.  
  6776.           untrace(_P_r_e_d_s)
  6777.  
  6778.  
  6779.  
  6780.                where Preds is a term _P/_N, where _P is a predicate name and _N its arity, or  a  list
  6781.  
  6782.  
  6783.  
  6784.                of  such  terms.   Turns  off  tracing  on the specified predicates.  _P_r_e_d_s must be
  6785.  
  6786.  
  6787.  
  6788.                instantiated at the time of the call.
  6789.  
  6790.  
  6791.  
  6792.  
  6793.           spy(_P_r_e_d_s)
  6794.  
  6795.  
  6796.  
  6797.                where Preds is a term _P/_N, where _P is a predicate name and _N its arity, or  a  list
  6798.  
  6799.  
  6800.  
  6801.                of such terms.  Sets spy points on the specified predicates.  _P_r_e_d_s must be instan-
  6802.  
  6803.  
  6804.  
  6805.                tiated at the time of the call.
  6806.  
  6807.  
  6808.  
  6809.  
  6810.           nospy(Preds)
  6811.  
  6812.  
  6813.  
  6814.                where Preds is a term _P/_N, where _P is a predicate name and _N its arity, or  a  list
  6815.  
  6816.  
  6817.  
  6818.                of  such  terms.   Removes  spy  points on the specified predicates.  _P_r_e_d_s must be
  6819.  
  6820.  
  6821.  
  6822.                instantiated at the time of the call.
  6823.  
  6824.  
  6825.  
  6826.  
  6827.  
  6828.  
  6829.  
  6830.  
  6831.                                                     101
  6832.  
  6833.  
  6834.  
  6835.  
  6836.  
  6837.  
  6838.  
  6839.  
  6840.  
  6841.  
  6842.           debug
  6843.  
  6844.  
  6845.  
  6846.                Turns on debugging mode.  This causes subsequent execution of predicates with trace
  6847.  
  6848.  
  6849.  
  6850.                or  spy  points  to be traced, and is a no-op if there are no such predicates.  The
  6851.  
  6852.  
  6853.  
  6854.                predicates _t_r_a_c_e/1 and _s_p_y/1 cause debugging mode to be turned on automatically.
  6855.  
  6856.  
  6857.  
  6858.  
  6859.           nodebug
  6860.  
  6861.  
  6862.  
  6863.                Turns off debugging mode.  This causes trace and spy points to be ignored.
  6864.  
  6865.  
  6866.  
  6867.  
  6868.           debugging
  6869.  
  6870.  
  6871.  
  6872.                Displays information about whether debug mode is on or not,  and  lists  predicates
  6873.  
  6874.  
  6875.  
  6876.                that have trace points or spy points set on them.
  6877.  
  6878.  
  6879.  
  6880.  
  6881.           tracepreds(_L)
  6882.  
  6883.  
  6884.  
  6885.                Binds _L to a list of terms _P/_N where the predicate _P of arity _N has a  trace  point
  6886.  
  6887.  
  6888.  
  6889.                set on it.
  6890.  
  6891.  
  6892.  
  6893.  
  6894.  
  6895.  
  6896.  
  6897.                                                     102
  6898.  
  6899.  
  6900.  
  6901.  
  6902.  
  6903.  
  6904.  
  6905.  
  6906.  
  6907.  
  6908.           spypreds(_L)
  6909.  
  6910.  
  6911.  
  6912.                Binds _L to a list of terms _P/_N where the predicate _P of arity _N has a spy point set
  6913.  
  6914.  
  6915.  
  6916.                on it.
  6917.  
  6918.  
  6919.  
  6920.  
  6921.  
  6922.  
  6923.                There is one known bug in the package: attempts to set trace points,  via  _t_r_a_c_e/1,
  6924.  
  6925.  
  6926.  
  6927.           on  system  and  library predicates that are used by the trace package can cause bizarre
  6928.  
  6929.  
  6930.  
  6931.           behaviour.
  6932.  
  6933.  
  6934.  
  6935.  
  6936.           _6._2.  _L_o_w _L_e_v_e_l _T_r_a_c_i_n_g
  6937.  
  6938.  
  6939.  
  6940.  
  6941.  
  6942.  
  6943.                SB-Prolog also provides a facility for low level tracing of execution.  This can be
  6944.  
  6945.  
  6946.  
  6947.           activated  by  invoking  the  simulator  with  the  -T  option, or through the predicate
  6948.  
  6949.  
  6950.  
  6951.           _t_r_a_c_e/0.  It causes trace information to be printed out at every call  (including  those
  6952.  
  6953.  
  6954.  
  6955.           to  system  trap  handlers).  The volume of such trace information can very become large
  6956.  
  6957.  
  6958.  
  6959.           very quickly, so this method of tracing is not recommended in general.
  6960.  
  6961.  
  6962.  
  6963.                                                     103
  6964.  
  6965.  
  6966.  
  6967.  
  6968.  
  6969.  
  6970.  
  6971.  
  6972.  
  6973.  
  6974.                Low level tracing may be turned off using the predicate _u_n_t_r_a_c_e/0.
  6975.  
  6976.  
  6977.  
  6978.  
  6979.           _7.  _T_h_e _S_i_m_u_l_a_t_o_r
  6980.  
  6981.  
  6982.  
  6983.  
  6984.  
  6985.  
  6986.                The simulator resides in the SB-Prolog system directory _s_i_m.   The  following  sec-
  6987.  
  6988.  
  6989.  
  6990.           tions describe various aspects of the simulator.
  6991.  
  6992.  
  6993.  
  6994.  
  6995.           _7._1.  _I_n_v_o_k_i_n_g _t_h_e _S_i_m_u_l_a_t_o_r
  6996.  
  6997.  
  6998.  
  6999.  
  7000.  
  7001.  
  7002.                The simulator is invoked by the command
  7003.  
  7004.  
  7005.  
  7006.  
  7007.  
  7008.                       sbprolog _b_c__f_i_l_e
  7009.  
  7010.  
  7011.  
  7012.  
  7013.  
  7014.  
  7015.           where _b_c__f_i_l_e is a byte code file resulting from the compilation of  a  Prolog  program.
  7016.  
  7017.  
  7018.  
  7019.           In  almost all cases, the user will wish to interact with the SB-Prolog _q_u_e_r_y _e_v_a_l_u_a_t_o_r,
  7020.  
  7021.  
  7022.  
  7023.           in which case _b_c__f_i_l_e will be $_r_e_a_d_l_o_o_p, and the command will be
  7024.  
  7025.  
  7026.  
  7027.  
  7028.  
  7029.                                                     104
  7030.  
  7031.  
  7032.  
  7033.  
  7034.  
  7035.  
  7036.  
  7037.  
  7038.  
  7039.                       sbprolog /
  7040.                                _\ path \
  7041.                                       //\$readloop
  7042.  
  7043.  
  7044.  
  7045.  
  7046.  
  7047.  
  7048.           where /
  7049.                 _\_p_a_t_h\
  7050.                      / is the path to the directory containing the command interpreter  $_r_e_a_d_l_o_o_p.
  7051.  
  7052.  
  7053.  
  7054.           This directory, typically, is the system directory _m_o_d_l_i_b.
  7055.  
  7056.  
  7057.  
  7058.  
  7059.                The command interpreter reads in a query typed in by the  user,  evaluates  it  and
  7060.  
  7061.  
  7062.  
  7063.           prints  the  answer(s),  repeating this until it encounters an end-of-file (the standard
  7064.  
  7065.  
  7066.  
  7067.           end-of-file character on the system, e.g. ctrl-D), or the user types in  _e_n_d__o_f__f_i_l_e  or
  7068.  
  7069.  
  7070.  
  7071.           _h_a_l_t.
  7072.  
  7073.  
  7074.  
  7075.  
  7076.                The user should ensure that the the directory containing the  executable  file  _s_i_m
  7077.  
  7078.  
  7079.  
  7080.           (typically,  the  system  directory _s_i_m) is included in the shell variable _p_a_t_h; if not,
  7081.  
  7082.  
  7083.  
  7084.           the full path to the simulator will have to be specified.
  7085.  
  7086.  
  7087.  
  7088.  
  7089.                In general, the simulator may be invoked with a variety of options, as follows:
  7090.  
  7091.  
  7092.  
  7093.  
  7094.  
  7095.  
  7096.  
  7097.  
  7098.  
  7099.                                                     105
  7100.  
  7101.  
  7102.  
  7103.  
  7104.  
  7105.  
  7106.  
  7107.  
  7108.  
  7109.                       sbprolog -_o_p_t_i_o_n_s _b_c__f_i_l_e
  7110.               or
  7111.                       sbprolog -_o_p_t_i_o_n<1> -_o_p_t_i_o_n<2> ... -_o_p_t_i_o_n<_n> _b_c__f_i_l_e
  7112.  
  7113.  
  7114.  
  7115.  
  7116.  
  7117.  
  7118.           The options recognized by the simulator are described below.
  7119.  
  7120.  
  7121.  
  7122.  
  7123.                When called with a byte code file _b_c__f_i_l_e, the simulator begins execution with  the
  7124.  
  7125.  
  7126.  
  7127.           first  clause  in  that  file.   The first clause in such a file, therefore, should be a
  7128.  
  7129.  
  7130.  
  7131.           clause without any arguments in the head  (otherwise,  the  simulator  will  attempt  to
  7132.  
  7133.  
  7134.  
  7135.           dereference  argument pointers in the head that are really pointing into deep space, and
  7136.  
  7137.  
  7138.  
  7139.           usually come to a sad end).  If the user is executing a file in this manner rather  than
  7140.  
  7141.  
  7142.  
  7143.           using the command interpreter, he should also be careful to include the undefined predi-
  7144.  
  7145.  
  7146.  
  7147.           cate  handler  `_$_u_n_d_e_f_i_n_e_d__p_r_e_d'/1,   which   is   normally   defined   in   the   file
  7148.  
  7149.  
  7150.  
  7151.           _m_o_d_l_i_b/$_i_n_i_t__s_y_s._P.
  7152.  
  7153.  
  7154.  
  7155.  
  7156.  
  7157.  
  7158.  
  7159.  
  7160.  
  7161.  
  7162.  
  7163.  
  7164.  
  7165.                                                     106
  7166.  
  7167.  
  7168.  
  7169.  
  7170.  
  7171.  
  7172.  
  7173.  
  7174.  
  7175.  
  7176.           _7._2.  _S_i_m_u_l_a_t_o_r _O_p_t_i_o_n_s
  7177.  
  7178.  
  7179.  
  7180.  
  7181.  
  7182.  
  7183.           The following is a list of options recognized by the simulator.
  7184.  
  7185.  
  7186.  
  7187.  
  7188.           T    Generates a trace at entry to each called routine.
  7189.  
  7190.  
  7191.  
  7192.  
  7193.           d    Produces a disassembled dump of _b_c__f_i_l_e into a file named `dump.pil' and exits.
  7194.  
  7195.  
  7196.  
  7197.  
  7198.           n    Adds machine addresses when producing trace and dump.
  7199.  
  7200.  
  7201.  
  7202.  
  7203.           s    Maintains information for the builtin _s_t_a_t_i_s_t_i_c_s/0.  Default: off.
  7204.  
  7205.  
  7206.  
  7207.  
  7208.           m _s_i_z_e
  7209.  
  7210.  
  7211.  
  7212.                Allocates _s_i_z_e words (4 bytes) of space to  the  local  stack  and  heap  together.
  7213.  
  7214.  
  7215.  
  7216.                Default: 100000.
  7217.  
  7218.  
  7219.  
  7220.  
  7221.           p _s_i_z_e
  7222.  
  7223.  
  7224.  
  7225.                Allocates _s_i_z_e words of space to the program area.  Default: 100000.
  7226.  
  7227.  
  7228.  
  7229.  
  7230.  
  7231.                                                     107
  7232.  
  7233.  
  7234.  
  7235.  
  7236.  
  7237.  
  7238.  
  7239.  
  7240.  
  7241.  
  7242.           b _s_i_z_e
  7243.  
  7244.  
  7245.  
  7246.                Allocates _s_i_z_e words of space to the trail stack.  Default: _m/5,  where  _m  is  the
  7247.  
  7248.  
  7249.  
  7250.                amount of space allocated to the local stack and heap together.  This parameter, if
  7251.  
  7252.  
  7253.  
  7254.                specified, must follow the -m parameter.
  7255.  
  7256.  
  7257.  
  7258.  
  7259.  
  7260.  
  7261.           As an example, the command
  7262.  
  7263.  
  7264.  
  7265.                       sbprolog -s -p 60000 -m 150000 \$readloop
  7266.  
  7267.  
  7268.  
  7269.  
  7270.           starts the simulator executing the command  interpreter  with  60000  bytes  of  program
  7271.  
  7272.  
  7273.  
  7274.           space,  150000  bytes  of  local  and global stack space and (by default) 30000 bytes of
  7275.  
  7276.  
  7277.  
  7278.           trail stack space; the _s option also results in statistics information being maintained.
  7279.  
  7280.  
  7281.  
  7282.  
  7283.           _7._3.  _I_n_t_e_r_r_u_p_t _H_a_n_d_l_i_n_g
  7284.  
  7285.  
  7286.  
  7287.  
  7288.  
  7289.  
  7290.  
  7291.  
  7292.  
  7293.  
  7294.  
  7295.  
  7296.  
  7297.                                                     108
  7298.  
  7299.  
  7300.  
  7301.  
  7302.  
  7303.  
  7304.  
  7305.  
  7306.  
  7307.  
  7308.                SB-Prolog provides a facility for exception handling using user-definable interrupt
  7309.  
  7310.  
  7311.  
  7312.           handlers.   This can be used both for external interrupts, e.g. those generated from the
  7313.  
  7314.  
  7315.  
  7316.           keyboard by the user or from signals other processes;  or  internal  traps,  e.g.  those
  7317.  
  7318.  
  7319.  
  7320.           caused  by  stack  overflows,  encountering undefined predicates, etc.  For example, the
  7321.  
  7322.  
  7323.  
  7324.           ``undefined  predicate''  interrupt  is  handled,   by   default,   by   the   predicate
  7325.  
  7326.  
  7327.  
  7328.           `_$_u_n_d_e_f_i_n_e_d__p_r_e_d'/1,  which  is  defined  in  the file _m_o_d_l_i_b/$_i_n_i_t__s_y_s._P.  The default
  7329.  
  7330.  
  7331.  
  7332.           action on encountering an undefined predicate is to attempt to dynamically load  a  file
  7333.  
  7334.  
  7335.  
  7336.           whose  name matches that of the undefined predicate.  However, the user may easily alter
  7337.  
  7338.  
  7339.  
  7340.           this behaviour by redefining the undefined predicate handler.
  7341.  
  7342.  
  7343.  
  7344.  
  7345.           _8.  _T_h_e _C_o_m_p_i_l_e_r
  7346.  
  7347.  
  7348.  
  7349.  
  7350.  
  7351.  
  7352.           The compiler translates Prolog source files into byte-code object files.  It is  written
  7353.  
  7354.  
  7355.  
  7356.           entirely in Prolog.  The byte code for the compiler can be found in the SB-Prolog system
  7357.  
  7358.  
  7359.  
  7360.  
  7361.  
  7362.  
  7363.                                                     109
  7364.  
  7365.  
  7366.  
  7367.  
  7368.  
  7369.  
  7370.  
  7371.  
  7372.  
  7373.  
  7374.           directory _c_m_p_l_i_b, with the source code resident in _c_m_p_l_i_b/_s_r_c.
  7375.  
  7376.  
  7377.  
  7378.  
  7379.                Byte code files may be concatenated together to  produce  other  byte  code  files.
  7380.  
  7381.  
  7382.  
  7383.           Thus,  for  example, if _f_o_o_1 and _f_o_o_2 are byte code files resulting from the compilation
  7384.  
  7385.  
  7386.  
  7387.           of two Prolog source programs, then the file _f_o_o, obtained by executing the  shell  com-
  7388.  
  7389.  
  7390.  
  7391.           mand
  7392.  
  7393.  
  7394.  
  7395.                       cat foo1 foo2 > foo
  7396.  
  7397.  
  7398.  
  7399.  
  7400.           is a byte code file as well, and may be loaded and executed.  In this case, loading  and
  7401.  
  7402.  
  7403.  
  7404.           executing  the  file _f_o_o would give the same result as loading _f_o_o_1 and _f_o_o_2 separately,
  7405.  
  7406.  
  7407.  
  7408.           which in turn would be the same as concatenating the original source files and compiling
  7409.  
  7410.  
  7411.  
  7412.           this  larger  file.  This makes it easier to compile large programs: one need only break
  7413.  
  7414.  
  7415.  
  7416.           them into smaller pieces, compile the individual pieces, and concatenate the byte  files
  7417.  
  7418.  
  7419.  
  7420.           together.
  7421.  
  7422.  
  7423.  
  7424.  
  7425.  
  7426.  
  7427.  
  7428.  
  7429.                                                     110
  7430.  
  7431.  
  7432.  
  7433.  
  7434.  
  7435.  
  7436.  
  7437.  
  7438.  
  7439.  
  7440.                The following sections describe the various aspects of the compiler in more detail.
  7441.  
  7442.  
  7443.  
  7444.  
  7445.           _8._1.  _S_t_r_u_c_t_u_r_e _o_f _t_h_e _C_o_m_p_i_l_e_r
  7446.  
  7447.  
  7448.  
  7449.  
  7450.  
  7451.  
  7452.           The compilation of a Prolog program proceeds in four phases.  In the  first  phase,  the
  7453.  
  7454.  
  7455.  
  7456.           source program is read in and  passed through a preprocessor.  The output of the prepro-
  7457.  
  7458.  
  7459.  
  7460.           cessor is essentially another Prolog source program.  Next, this program is  transformed
  7461.  
  7462.  
  7463.  
  7464.           into  an  internal  representation (essentially an annotated abstract syntax tree).  The
  7465.  
  7466.  
  7467.  
  7468.           third phase  involves  generating  WAM  ``assembly''  code  and  peephole  optimization.
  7469.  
  7470.  
  7471.  
  7472.           Finally, the WAM assembly code is processed by an assembler which generates byte code.
  7473.  
  7474.  
  7475.  
  7476.  
  7477.           _8._2.  _I_n_v_o_k_i_n_g _t_h_e _C_o_m_p_i_l_e_r
  7478.  
  7479.  
  7480.  
  7481.  
  7482.  
  7483.  
  7484.           The compiler is invoked through the Prolog predicate _c_o_m_p_i_l_e:
  7485.  
  7486.  
  7487.  
  7488.                       | ?- compile(_I_n_F_i_l_e [, _O_u_t_F_i_l_e ] [, _O_p_t_i_o_n_s_L_i_s_t ]).
  7489.  
  7490.  
  7491.  
  7492.  
  7493.  
  7494.  
  7495.                                                     111
  7496.  
  7497.  
  7498.  
  7499.  
  7500.  
  7501.  
  7502.  
  7503.  
  7504.  
  7505.  
  7506.           where optional parameters are enclosed in brackets.  _I_n_F_i_l_e is the  name  of  the  input
  7507.  
  7508.  
  7509.  
  7510.           (i.e.  source)  file;  _O_u_t_F_i_l_e  is  the  name  of the output file (i.e. byte code) file;
  7511.  
  7512.  
  7513.  
  7514.           _O_p_t_i_o_n_s_L_i_s_t is a list of compiler options (see below).
  7515.  
  7516.  
  7517.  
  7518.  
  7519.                The input and output file names must be Prolog atoms,  i.e.  either  begin  with  a
  7520.  
  7521.  
  7522.  
  7523.           lower  case  letter  or dollar sign `$', and consist only of letters, digits, and under-
  7524.  
  7525.  
  7526.  
  7527.           scores; or, be enclosed within single quotes.  If the output file name is not specified,
  7528.  
  7529.  
  7530.  
  7531.           it  defaults to _I_n_F_i_l_e.out.  The list of options, if specified, is a Prolog list, i.e. a
  7532.  
  7533.  
  7534.  
  7535.           term of the form
  7536.  
  7537.  
  7538.  
  7539.                       [ _o_p_t_i_o_n<1>, _o_p_t_i_o_n<2>, ..., _o_p_t_i_o_n<_n> ].
  7540.  
  7541.  
  7542.  
  7543.  
  7544.           If left unspecified, it defaults to the empty list [].
  7545.  
  7546.  
  7547.  
  7548.  
  7549.                In fact, the output file name and the options list may be specified in  any  order.
  7550.  
  7551.  
  7552.  
  7553.           Thus, for example, the queries
  7554.  
  7555.  
  7556.  
  7557.  
  7558.  
  7559.  
  7560.  
  7561.                                                     112
  7562.  
  7563.  
  7564.  
  7565.  
  7566.  
  7567.  
  7568.  
  7569.  
  7570.  
  7571.                       | ?- compile('/usr/debray/foo', foo_out, [v]).
  7572.               and
  7573.                       | ?- compile('/usr/debray/foo', [v], foo_out).
  7574.  
  7575.  
  7576.  
  7577.  
  7578.           are equivalent, and specify that the Prolog source file `/_u_s_r/_d_e_b_r_a_y/_f_o_o' is to be  com-
  7579.  
  7580.  
  7581.  
  7582.           piled  in verbose mode (see ``Compiler Options'' below), and that the byte code is to be
  7583.  
  7584.  
  7585.  
  7586.           generated into the file _f_o_o__o_u_t.
  7587.  
  7588.  
  7589.  
  7590.  
  7591.                The _c_o_m_p_i_l_e predicate may also be called with a fourth parameter:
  7592.  
  7593.  
  7594.  
  7595.                       | ?- compile(_I_n_F_i_l_e, _O_u_t_F_i_l_e, _O_p_t_i_o_n_s_L_i_s_t, _P_r_e_d_L_i_s_t).
  7596.  
  7597.  
  7598.  
  7599.  
  7600.           where _I_n_F_i_l_e, _O_u_t_F_i_l_e and _O_p_t_i_o_n_s_L_i_s_t are as before; _c_o_m_p_i_l_e/4 unifies _P_r_e_d_L_i_s_t  with  a
  7601.  
  7602.  
  7603.  
  7604.           list of terms _P/_N denoting the predicates defined in _I_n_F_i_l_e, where _P is a predicate name
  7605.  
  7606.  
  7607.  
  7608.           and _N its arity. _P_r_e_d_L_i_s_t, if specified, is usually given as an uninstantiated variable;
  7609.  
  7610.  
  7611.  
  7612.           its principal use is for setting trace points on the predicates in the file (see Section
  7613.  
  7614.  
  7615.  
  7616.           6), e.g. by executing
  7617.  
  7618.  
  7619.  
  7620.                       | ?- compile('/usr/debray/foo', foo_out, [v], L), load(foo_out), trace(L).
  7621.  
  7622.  
  7623.  
  7624.  
  7625.  
  7626.  
  7627.                                                     113
  7628.  
  7629.  
  7630.  
  7631.  
  7632.  
  7633.  
  7634.  
  7635.  
  7636.  
  7637.  
  7638.           Notice that _P_r_e_d_L_i_s_t can only appear in _c_o_m_p_i_l_e/4.
  7639.  
  7640.  
  7641.  
  7642.  
  7643.           _8._3.  _C_o_m_p_i_l_e_r _O_p_t_i_o_n_s
  7644.  
  7645.  
  7646.  
  7647.  
  7648.  
  7649.  
  7650.           The following options are currently recognized by the compiler:
  7651.  
  7652.  
  7653.  
  7654.  
  7655.           a    Specifies that an ``assembler'' file is to be created.  The name of  the  assembler
  7656.  
  7657.  
  7658.  
  7659.                file  is obtained by appending ``.asl'' to the source file name.  While the writing
  7660.  
  7661.  
  7662.  
  7663.                out of assembly code slows down the compilation process to some extent,  it  allows
  7664.  
  7665.  
  7666.  
  7667.                the  assembler  to  do a better job of optimizing away indirect subroutine linkages
  7668.  
  7669.  
  7670.  
  7671.                (since in this case the assembler has assembly code for the entire program to  work
  7672.  
  7673.  
  7674.  
  7675.                with  at  once,  not just a single predicate).  This results in code that is faster
  7676.  
  7677.  
  7678.  
  7679.                and more compact.
  7680.  
  7681.  
  7682.  
  7683.  
  7684.           d    Dumps expanded macros to the user (see Section 10).
  7685.  
  7686.  
  7687.  
  7688.  
  7689.  
  7690.  
  7691.  
  7692.  
  7693.                                                     114
  7694.  
  7695.  
  7696.  
  7697.  
  7698.  
  7699.  
  7700.  
  7701.  
  7702.  
  7703.  
  7704.           e    Expand macros (see Section 10).
  7705.  
  7706.  
  7707.  
  7708.  
  7709.           i    Specifies that indexes are to be generated.   The  pragma  file  (see  Section  12)
  7710.  
  7711.  
  7712.  
  7713.                corresponding  to the source file is consulted for information regarding predicates
  7714.  
  7715.  
  7716.  
  7717.                which should have indexes constructed, and the arguments on which indexes are to be
  7718.  
  7719.  
  7720.  
  7721.                constructed.
  7722.  
  7723.  
  7724.  
  7725.  
  7726.           t    If specified, turns off assembler optimizations that  eliminate  indirect  branches
  7727.  
  7728.  
  7729.  
  7730.                through the symbol table in favour of direct branches.  This is useful in debugging
  7731.  
  7732.  
  7733.  
  7734.                compiled code.  It is _n_e_c_e_s_s_a_r_y if the extension table feature is to be used.
  7735.  
  7736.  
  7737.  
  7738.  
  7739.           v    If specified, compiles in ``verbose'' mode, which causes  messages  regarding  pro-
  7740.  
  7741.  
  7742.  
  7743.                gress of compilation to be printed out.
  7744.  
  7745.  
  7746.  
  7747.  
  7748.  
  7749.  
  7750.  
  7751.  
  7752.  
  7753.  
  7754.  
  7755.  
  7756.  
  7757.  
  7758.  
  7759.                                                     115
  7760.  
  7761.  
  7762.  
  7763.  
  7764.  
  7765.  
  7766.  
  7767.  
  7768.  
  7769.  
  7770.           _8._4.  _A _N_o_t_e _o_n _C_o_d_i_n_g _f_o_r _E_f_f_i_c_i_e_n_c_y
  7771.  
  7772.  
  7773.  
  7774.  
  7775.  
  7776.  
  7777.                The SB-Prolog system tends to favour programs that are relatively pure.  Thus,  for
  7778.  
  7779.  
  7780.  
  7781.           example,  _a_s_s_e_r_ts tend to be quite expensive, encouraging the user to avoid them if pos-
  7782.  
  7783.  
  7784.  
  7785.           sible.  This section points out some syntactic constructs that lead to the generation of
  7786.  
  7787.  
  7788.  
  7789.           efficient  code.   These involve (_i) avoiding the creation of backtrack points; and (_i_i)
  7790.  
  7791.  
  7792.  
  7793.           minimizing data movement between registers.  Optimization of logic programs is  an  area
  7794.  
  7795.  
  7796.  
  7797.           of  ongoing research, and we expect to enhance the capabilities of the system further in
  7798.  
  7799.  
  7800.  
  7801.           future versions.
  7802.  
  7803.  
  7804.  
  7805.  
  7806.           _8._4._1.  _A_v_o_i_d_i_n_g _C_r_e_a_t_i_o_n _o_f _B_a_c_k_t_r_a_c_k _P_o_i_n_t_s
  7807.  
  7808.  
  7809.  
  7810.  
  7811.  
  7812.  
  7813.                Since the creation of backtrack points is relatively expensive, program  efficiency
  7814.  
  7815.  
  7816.  
  7817.           may  be  improved substantially by using constructs that avoid the creation of backtrack
  7818.  
  7819.  
  7820.  
  7821.           points where possible.  The SB-Prolog compiler recognizes conditionals involving certain
  7822.  
  7823.  
  7824.  
  7825.                                                     116
  7826.  
  7827.  
  7828.  
  7829.  
  7830.  
  7831.  
  7832.  
  7833.  
  7834.  
  7835.  
  7836.           complementary  inline  tests,  and generates code that does not create choice points for
  7837.  
  7838.  
  7839.  
  7840.           such cases.  Two inline tests _p(_X~) and _q(_X~) are complementary if  and  only  if  _p(_X~)  =_
  7841.  
  7842.  
  7843.  
  7844.           _n_o_t(_q(_X~)).   For  example, the literals `_X > _Y' and `_X =< _Y' are complementary.  At this
  7845.  
  7846.  
  7847.  
  7848.           point, complementary tests are recognized as such only  if  their  argument  tuples  are
  7849.  
  7850.  
  7851.  
  7852.           identical.   The  inline  predicates  that  are  treated  in  this  manner,  with  their
  7853.  
  7854.  
  7855.  
  7856.           corresponding complementary literals, are shown in Table 3.   The  syntactic  constructs
  7857.  
  7858.  
  7859.  
  7860.  
  7861.  
  7862.                                  _________________________________________
  7863.                                      _I_n_l_i_n_e _T_e_s_t       _C_o_m_p_l_e_m_e_n_t_a_r_y _T_e_s_t
  7864.                                  _________________________________________
  7865.                                   >/2                  =</2
  7866.                                  _________________________________________
  7867.                                   =</2                 >/2
  7868.                                  _________________________________________
  7869.                                   >=/2                 </2
  7870.                                  _________________________________________
  7871.                                   </2                  >=/2
  7872.                                  _________________________________________
  7873.                                   =:=/2                =\=/2
  7874.                                  _________________________________________
  7875.                                   =\=/2                =:=/2
  7876.                                  _________________________________________
  7877.                                   var/1                nonvar/1
  7878.                                  _________________________________________
  7879.                                   nonvar/1             var/1
  7880.                                  _________________________________________
  7881.  
  7882.                                 |
  7883.                                 |
  7884.                                 |
  7885.                                 |
  7886.                                 |
  7887.                                 |
  7888.                                 |
  7889.                                 |
  7890.                                 |
  7891.                                 |
  7892.                                 |
  7893.                                 |
  7894.                                 |
  7895.                                 |
  7896.                                 |
  7897.                                 |
  7898.                                 |
  7899.                                 |
  7900.  
  7901.  
  7902.  
  7903.  
  7904.  
  7905.  
  7906.  
  7907.  
  7908.  
  7909.  
  7910.  
  7911.  
  7912.  
  7913.  
  7914.  
  7915.  
  7916.                                                     |
  7917.                                                     |
  7918.                                                     |
  7919.                                                     |
  7920.                                                     |
  7921.                                                     |
  7922.                                                     |
  7923.                                                     |
  7924.                                                     |
  7925.                                                     |
  7926.                                                     |
  7927.                                                     |
  7928.                                                     |
  7929.                                                     |
  7930.                                                     |
  7931.                                                     |
  7932.                                                     |
  7933.                                                     |
  7934.  
  7935.  
  7936.  
  7937.  
  7938.  
  7939.  
  7940.  
  7941.  
  7942.  
  7943.  
  7944.  
  7945.  
  7946.  
  7947.  
  7948.  
  7949.  
  7950.                                                                          |
  7951.                                                                          |
  7952.                                                                          |
  7953.                                                                          |
  7954.                                                                          |
  7955.                                                                          |
  7956.                                                                          |
  7957.                                                                          |
  7958.                                                                          |
  7959.                                                                          |
  7960.                                                                          |
  7961.                                                                          |
  7962.                                                                          |
  7963.                                                                          |
  7964.                                                                          |
  7965.                                                                          |
  7966.                                                                          |
  7967.                                                                          |
  7968.  
  7969.  
  7970.  
  7971.  
  7972.  
  7973.  
  7974.  
  7975.  
  7976.  
  7977.  
  7978.  
  7979.  
  7980.  
  7981.  
  7982.  
  7983.  
  7984.  
  7985.  
  7986.  
  7987.                           Table 3: Complementary tests recognized by the compiler
  7988.  
  7989.  
  7990.  
  7991.  
  7992.  
  7993.  
  7994.  
  7995.                                                     117
  7996.  
  7997.  
  7998.  
  7999.  
  8000.  
  8001.  
  8002.  
  8003.  
  8004.  
  8005.  
  8006.           recognized are:
  8007.  
  8008.  
  8009.  
  8010.  
  8011.           (_i)  Disjuncts of the form
  8012.  
  8013.  
  8014.  
  8015.                    _h_e_a_d :- (( _t_e_s_t(_X~), ... ) ; ( not(_t_e_s_t(_X~)), ...)).
  8016.  
  8017.  
  8018.  
  8019.  
  8020.                or
  8021.  
  8022.  
  8023.  
  8024.                    _h_e_a_d :- (( _t_e_s_t(_X~), ... ) ; ( _c_o_m_p__t_e_s_t(_X~), ...)).
  8025.  
  8026.  
  8027.  
  8028.  
  8029.                where _t_e_s_t is one of the inline  tests  in  the  table  above,  and  _c_o_m_p__t_e_s_t  the
  8030.  
  8031.  
  8032.  
  8033.                corresponding  complementary  test  (note  that the arguments to _t_e_s_t and _c_o_m_p__t_e_s_t
  8034.  
  8035.  
  8036.  
  8037.                have to be identical).
  8038.  
  8039.  
  8040.  
  8041.  
  8042.           (_i_i) Conditionals of the form
  8043.  
  8044.  
  8045.  
  8046.                    _h_e_a_d :- (_t_e_s_t<1>, ... , _t_e_s_t<_n>) -> _T_r_u_e__C_a_s_e ; _F_a_l_s_e__C_a_s_e.
  8047.  
  8048.  
  8049.  
  8050.  
  8051.                or
  8052.  
  8053.  
  8054.  
  8055.                    _h_e_a_d :- (_t_e_s_t<1>; ... ; _t_e_s_t<_n>) -> _T_r_u_e__C_a_s_e ; _F_a_l_s_e__C_a_s_e.
  8056.  
  8057.  
  8058.  
  8059.  
  8060.  
  8061.                                                     118
  8062.  
  8063.  
  8064.  
  8065.  
  8066.  
  8067.  
  8068.  
  8069.  
  8070.  
  8071.  
  8072.                where each _t_e_s_t<_i> is an inline test, as mentioned in the table above.
  8073.  
  8074.  
  8075.  
  8076.  
  8077.                The code generated for these cases involves a test and conditional branch,  and  no
  8078.  
  8079.  
  8080.  
  8081.           choice  point  is  created.   We expect future versions of the translator to recognize a
  8082.  
  8083.  
  8084.  
  8085.           wider class of complementary tests.
  8086.  
  8087.  
  8088.  
  8089.  
  8090.                Notice that this discourages the use of explicit  cuts.   For  example,  whereas  a
  8091.  
  8092.  
  8093.  
  8094.           choice point will be created for
  8095.  
  8096.  
  8097.  
  8098.               part(M,[E|L],U1,U2) :-
  8099.                  ((E =< M, !, U1 = [E|U1a], U2 = U2a) ;
  8100.                   (U1 = U1a, U2 = [E|U2a])),
  8101.                  part(M,L,U1a,U2a).
  8102.  
  8103.  
  8104.  
  8105.  
  8106.           no choice point will be created for either
  8107.  
  8108.  
  8109.  
  8110.               part(M,[E|L],U1,U2) :-
  8111.                  (E =< M ->
  8112.                     (U1 = [E|U1a], U2 = U2a) ;
  8113.                     (U1 = U1a, U2 = [E|U2a])),
  8114.                  part(M,L,U1a,U2a).
  8115.  
  8116.  
  8117.  
  8118.  
  8119.           or
  8120.  
  8121.  
  8122.  
  8123.  
  8124.  
  8125.  
  8126.  
  8127.                                                     119
  8128.  
  8129.  
  8130.  
  8131.  
  8132.  
  8133.  
  8134.  
  8135.  
  8136.  
  8137.               part(M,[E|L],U1,U2) :-
  8138.                  ((E =< M, U1 = [E|U1a], U2 = U2a) ;
  8139.                   (E > M,  U1 = U1a, U2 = [E|U2a])),
  8140.                  part(M,L,U1a,U2a).
  8141.  
  8142.  
  8143.  
  8144.  
  8145.           Thus, either of the two later versions will be more efficient than the version with  the
  8146.  
  8147.  
  8148.  
  8149.           explicit cut.
  8150.  
  8151.  
  8152.  
  8153.  
  8154.           _8._4._2.  _M_i_n_i_m_i_z_i_n_g _D_a_t_a _M_o_v_e_m_e_n_t _B_e_t_w_e_e_n _R_e_g_i_s_t_e_r_s
  8155.  
  8156.  
  8157.  
  8158.  
  8159.  
  8160.  
  8161.                Data movement between registers for parameter passing may be minimized  by  leaving
  8162.  
  8163.  
  8164.  
  8165.           variables in the same argument position wherever possible.  Thus, the clause
  8166.  
  8167.  
  8168.  
  8169.                       p(X,Y) :- p1(X,Y,0).
  8170.  
  8171.  
  8172.  
  8173.  
  8174.           is preferable to
  8175.  
  8176.  
  8177.  
  8178.                       p(X,Y) :- p1(0,X,Y).
  8179.  
  8180.  
  8181.  
  8182.  
  8183.           because the first definition leaves the variables _X and _Y in the same argument positions
  8184.  
  8185.  
  8186.  
  8187.           (first and second, respectively), while the second definition does not.
  8188.  
  8189.  
  8190.  
  8191.  
  8192.  
  8193.                                                     120
  8194.  
  8195.  
  8196.  
  8197.  
  8198.  
  8199.  
  8200.  
  8201.  
  8202.  
  8203.  
  8204.           _8._5.  _A_s_s_e_m_b_l_y
  8205.  
  8206.  
  8207.  
  8208.  
  8209.  
  8210.  
  8211.                The SB-Prolog assembler can be invoked by loading the compiler and using the predi-
  8212.  
  8213.  
  8214.  
  8215.           cate $_a_s_m/3:
  8216.  
  8217.  
  8218.  
  8219.                       | ?- $asm(_I_n_F_i_l_e, _O_u_t_F_i_l_e, _O_p_t_i_o_n_s_L_i_s_t).
  8220.  
  8221.  
  8222.  
  8223.  
  8224.           where _I_n_F_i_l_e is a Prolog atom which is the name of a WAM assembly source file (e.g.  the
  8225.  
  8226.  
  8227.  
  8228.           ``.asl''  file  generated when a Prolog program is compiled with the ``a'' option), _O_u_t_-
  8229.  
  8230.  
  8231.  
  8232.           _F_i_l_e is an atom which is the name of the intended byte code file, and _O_p_t_i_o_n_s_L_i_s_t  is  a
  8233.  
  8234.  
  8235.  
  8236.           list of options.  The options recognized by the assembler are:
  8237.  
  8238.  
  8239.  
  8240.  
  8241.           v    ``Verbose'' mode.  Prints out information regarding progress of assembly.
  8242.  
  8243.  
  8244.  
  8245.  
  8246.           t    ``Trace''.  If specified, the assembler generates code to force procedure calls  to
  8247.  
  8248.  
  8249.  
  8250.                branch  indirectly via the symbol table, instead of using a direct branch.  This is
  8251.  
  8252.  
  8253.  
  8254.                Useful for tracing compiled code.  It is _n_e_c_e_s_s_a_r_y if the extension  table  feature
  8255.  
  8256.  
  8257.  
  8258.  
  8259.                                                     121
  8260.  
  8261.  
  8262.  
  8263.  
  8264.  
  8265.  
  8266.  
  8267.  
  8268.  
  8269.  
  8270.                is to be used.
  8271.  
  8272.  
  8273.  
  8274.  
  8275.           The assembler is intended primarily to support the compiler, so  the  assembly  language
  8276.  
  8277.  
  8278.  
  8279.           syntax  is  quirky  in places.  The interested reader is advised to look at the assembly
  8280.  
  8281.  
  8282.  
  8283.           files resulting from compilation with the ``a'' option for more on  SB-Prolog  assembler
  8284.  
  8285.  
  8286.  
  8287.           syntax.
  8288.  
  8289.  
  8290.  
  8291.  
  8292.           _9.  _M_o_d_u_l_e_s
  8293.  
  8294.  
  8295.  
  8296.  
  8297.  
  8298.  
  8299.                To describe how modules (or maybe more properly, just libraries) are currently sup-
  8300.  
  8301.  
  8302.  
  8303.           ported  in  our  system, we must describe the _$_u_n_d_e_f_i_n_e_d__p_r_e_d/1 interrupt handler.  The
  8304.  
  8305.  
  8306.  
  8307.           system keeps a table of modules and routines that are needed from each.  When  a  predi-
  8308.  
  8309.  
  8310.  
  8311.           cate  is  found  to  be undefined, the table is searched to see if it is defined by some
  8312.  
  8313.  
  8314.  
  8315.           module.  If so, that module is loaded (if it hasn't  been  previously  loaded)  and  the
  8316.  
  8317.  
  8318.  
  8319.           association  is  made between the routine name as defined in the module, and the routine
  8320.  
  8321.  
  8322.  
  8323.  
  8324.  
  8325.                                                     122
  8326.  
  8327.  
  8328.  
  8329.  
  8330.  
  8331.  
  8332.  
  8333.  
  8334.  
  8335.  
  8336.           name as used by the invoker.
  8337.  
  8338.  
  8339.  
  8340.  
  8341.                The table of modules and needed routines is:
  8342.  
  8343.  
  8344.  
  8345.                      defined_mods(_M_o_d_n_a_m_e, [_p_r_e_d<1>/_a_r_i_t_y<1>, ..., _p_r_e_d<_n>/_a_r_i_t_y<_n>]).
  8346.  
  8347.  
  8348.  
  8349.  
  8350.           where _M_o_d_n_a_m_e is the name of the module.  The module exports  _n  predicate  definitions.
  8351.  
  8352.  
  8353.  
  8354.           The  first  exported  pred  is of arity _a_r_i_t_y<>1, and needs to be invoked by the name of
  8355.  
  8356.  
  8357.  
  8358.           _p_r_e_d<1>.
  8359.  
  8360.  
  8361.  
  8362.  
  8363.                The table of modules that have already been loaded is given by
  8364.  
  8365.  
  8366.  
  8367.                       loaded_mods(_M_o_d_n_a_m_e).
  8368.  
  8369.  
  8370.  
  8371.  
  8372.           A module is a file of predicate definitions, together with a fact  defining  a  list  of
  8373.  
  8374.  
  8375.  
  8376.           predicates  exported  by  that  module, and a set of facts, each of which specifies, for
  8377.  
  8378.  
  8379.  
  8380.           some other module, the predicates imported from that module.  For  example,  consider  a
  8381.  
  8382.  
  8383.  
  8384.           module  name `_p'. It contains a single fact, named _p__e_x_p_o_r_t, that is true of the list of
  8385.  
  8386.  
  8387.  
  8388.  
  8389.  
  8390.  
  8391.                                                     123
  8392.  
  8393.  
  8394.  
  8395.  
  8396.  
  8397.  
  8398.  
  8399.  
  8400.  
  8401.  
  8402.           predicate/arity's that are exported.  E.g.
  8403.  
  8404.  
  8405.  
  8406.                       p_export([p1/2, p2/4])
  8407.  
  8408.  
  8409.  
  8410.  
  8411.           indicates that the module _p exports the predicates _p_1/2 and _p_2/4.   For  each  module  _m
  8412.  
  8413.  
  8414.  
  8415.           which  contains predicates needed by the module _p, there is a fact for _p__u_s_e, describing
  8416.  
  8417.  
  8418.  
  8419.           what module is needed and the names of the predicates defined  there  that  are  needed.
  8420.  
  8421.  
  8422.  
  8423.           For example, if module _p needs to import predicates _i_p_1/2 and _i_p_2/3 from module _q, there
  8424.  
  8425.  
  8426.  
  8427.           would be a fact
  8428.  
  8429.  
  8430.  
  8431.                       p_use(q,[ip1/2, ip2/3])
  8432.  
  8433.  
  8434.  
  8435.  
  8436.           where _q is a module that exports two predicates: one 2-ary and  one  3-ary.   This  list
  8437.  
  8438.  
  8439.  
  8440.           corresponds to the export list of module _q.
  8441.  
  8442.  
  8443.  
  8444.  
  8445.                The correspondence between the predicates  in  the  export  list  of  an  exporting
  8446.  
  8447.  
  8448.  
  8449.           module,  and  those  in  the import or _u_s_e list of a module which imports one or more of
  8450.  
  8451.  
  8452.  
  8453.           them, is by position, i.e. the predicate names at the exporting and importing names  may
  8454.  
  8455.  
  8456.  
  8457.                                                     124
  8458.  
  8459.  
  8460.  
  8461.  
  8462.  
  8463.  
  8464.  
  8465.  
  8466.  
  8467.  
  8468.           be  different,  and the association between names in the two lists is by the position in
  8469.  
  8470.  
  8471.  
  8472.           the list.  If the importing module does not wish to import one or more of the predicates
  8473.  
  8474.  
  8475.  
  8476.           exported  by the exporting module, it may put an anonymous variable in the corresponding
  8477.  
  8478.  
  8479.  
  8480.           position in its _u_s_e list.  Thus, for example, if module _p above  had  wished  to  import
  8481.  
  8482.  
  8483.  
  8484.           only the predicate _i_p_2/3 from module _q, the corresponding use fact would be
  8485.  
  8486.  
  8487.  
  8488.                       p_use(q, [_, ip2/3]).
  8489.  
  8490.  
  8491.  
  8492.  
  8493.  
  8494.                The initial set of predicates and the modules from which they are to be  loaded  is
  8495.  
  8496.  
  8497.  
  8498.           set   up   by   an   initial   call   to   $_p_r_o_r_c/0   (see  the  SB-Prolog  system  file
  8499.  
  8500.  
  8501.  
  8502.           _m_o_d_l_i_b/_s_r_c/$_p_r_o_r_c._P).  This predicate makes initial calls to the  predicate  $define_mod
  8503.  
  8504.  
  8505.  
  8506.           which  set  up  the  tables  described above so that the use of standard predicates will
  8507.  
  8508.  
  8509.  
  8510.           cause the correct modules to be loaded in the _$_u_n_d_e_f_i_n_e_d__p_r_e_d routine, and the  correct
  8511.  
  8512.  
  8513.  
  8514.           names to be used.
  8515.  
  8516.  
  8517.  
  8518.  
  8519.  
  8520.  
  8521.  
  8522.  
  8523.                                                     125
  8524.  
  8525.  
  8526.  
  8527.  
  8528.  
  8529.  
  8530.  
  8531.  
  8532.  
  8533.  
  8534.           _1_0.  _M_a_c_r_o_s
  8535.  
  8536.  
  8537.  
  8538.  
  8539.  
  8540.  
  8541.                SB-Prolog features a facility for the definition and expansion of  macros  that  is
  8542.  
  8543.  
  8544.  
  8545.           fully  compatible  with  the  runtime  system.   Its basic mechanism is a simple partial
  8546.  
  8547.  
  8548.  
  8549.           evaluator.  It is called by both _c_o_n_s_u_l_t and _c_o_m_p_i_l_e, so  that  macro  expansion  occurs
  8550.  
  8551.  
  8552.  
  8553.           independently  of  whether  the code is interpreted or compiled (but not when asserted).
  8554.  
  8555.  
  8556.  
  8557.           Moreover, the macro definitions are retained as clauses at runtime, so  that  invocation
  8558.  
  8559.  
  8560.  
  8561.           of  macros  via  _c_a_l_l/1  at  runtime (or from asserted clauses) does not pose a problem.
  8562.  
  8563.  
  8564.  
  8565.           This means, however, that if the same macro is used in many different files, it will  be
  8566.  
  8567.  
  8568.  
  8569.           loaded more than once, thus leading to wasetd space.  This ought to be thought about and
  8570.  
  8571.  
  8572.  
  8573.           fixed.
  8574.  
  8575.  
  8576.  
  8577.  
  8578.                The  source  for  the  macro   expander   is   in   the   SB-Prolog   system   file
  8579.  
  8580.  
  8581.  
  8582.           _m_o_d_l_i_b/_s_r_c/$_m_a_c._P.
  8583.  
  8584.  
  8585.  
  8586.  
  8587.  
  8588.  
  8589.                                                     126
  8590.  
  8591.  
  8592.  
  8593.  
  8594.  
  8595.  
  8596.  
  8597.  
  8598.  
  8599.  
  8600.           _1_0._1.  _D_e_f_i_n_i_n_g _M_a_c_r_o_s
  8601.  
  8602.  
  8603.  
  8604.  
  8605.  
  8606.  
  8607.                `Macros', or predicates to be evaluated at compile-time, are defined by clauses  of
  8608.  
  8609.  
  8610.  
  8611.           the form
  8612.  
  8613.  
  8614.  
  8615.                       Head ::- Body
  8616.  
  8617.  
  8618.  
  8619.  
  8620.           where facts have `true' as their body.  The partial evaluator will expand any call to  a
  8621.  
  8622.  
  8623.  
  8624.           predicate  defined by ::-/2 that unifies with the head of only one clause in ::-/2. If a
  8625.  
  8626.  
  8627.  
  8628.           call unifies with the head of more than one clause in ::-/2, it  will  not  be  expanded
  8629.  
  8630.  
  8631.  
  8632.           Notice that this is not a fundamental restriction, since `;' is permitted in the body of
  8633.  
  8634.  
  8635.  
  8636.           a clause.  The partial evaluator also converts each definition of the form
  8637.  
  8638.  
  8639.  
  8640.                       Head ::- Body.
  8641.  
  8642.  
  8643.  
  8644.  
  8645.           to a clause of the form
  8646.  
  8647.  
  8648.  
  8649.                       Head :- Body.
  8650.  
  8651.  
  8652.  
  8653.  
  8654.  
  8655.                                                     127
  8656.  
  8657.  
  8658.  
  8659.  
  8660.  
  8661.  
  8662.  
  8663.  
  8664.  
  8665.  
  8666.           and adds this second clause to the other ``normal'' clauses  that  were  read  from  the
  8667.  
  8668.  
  8669.  
  8670.           file.   This  ensures  that  calls  to the macro at runtime, e.g. through _c_a_l_l/1 or from
  8671.  
  8672.  
  8673.  
  8674.           unexpanded calls in the program do not cause any problems.
  8675.  
  8676.  
  8677.  
  8678.  
  8679.                The partial evaluator is actually a Prolog interpreter written `purely' in  Prolog,
  8680.  
  8681.  
  8682.  
  8683.           i.e., variable assignments are explicitly handled.  This is necessary to be able to han-
  8684.  
  8685.  
  8686.  
  8687.           dle impure constructs such as `var(X), X=a'.  As a result this is  a  _v_e_r_y  _s_l_o_w  Prolog
  8688.  
  8689.  
  8690.  
  8691.           evaluator.
  8692.  
  8693.  
  8694.  
  8695.  
  8696.                Since naive partial evaluation can go into an infinite  loop,  SB-Prolog's  partial
  8697.  
  8698.  
  8699.  
  8700.           evaluator  maintains a depth-bound and will not expand recursive calls deeper than that.
  8701.  
  8702.  
  8703.  
  8704.           The depth is determined by the globalset predicate $_m_a_c__d_e_p_t_h.  The  default  value  for
  8705.  
  8706.  
  8707.  
  8708.           $_m_a_c__d_e_p_t_h is 50  This can be changed to some other value _n by executing
  8709.  
  8710.  
  8711.  
  8712.                       | ?- globalset($mac_depth(_n)).
  8713.  
  8714.  
  8715.  
  8716.  
  8717.  
  8718.  
  8719.  
  8720.  
  8721.                                                     128
  8722.  
  8723.  
  8724.  
  8725.  
  8726.  
  8727.  
  8728.  
  8729.  
  8730.  
  8731.  
  8732.           _1_0._2.  _M_a_c_r_o _E_x_p_a_n_d_e_r _O_p_t_i_o_n_s
  8733.  
  8734.  
  8735.  
  8736.  
  8737.  
  8738.  
  8739.           The following options are recognized by the macro expander:
  8740.  
  8741.  
  8742.  
  8743.  
  8744.           d    Dumps all clauses to the user after expansion.  Useful for debugging.
  8745.  
  8746.  
  8747.  
  8748.  
  8749.           e    Expand macros.  If omitted, the expander simply converts each  ::-/2  clause  to  a
  8750.  
  8751.  
  8752.  
  8753.                normal :-/2 clause.
  8754.  
  8755.  
  8756.  
  8757.  
  8758.           v    ``Verbose'' mode.  Prints macros that are/are not being expanded.
  8759.  
  8760.  
  8761.  
  8762.  
  8763.           _1_1.  _E_x_t_e_n_s_i_o_n _T_a_b_l_e_s
  8764.  
  8765.  
  8766.  
  8767.  
  8768.  
  8769.  
  8770.                Extension tables store the calls and answers for a predicate. If a  call  has  been
  8771.  
  8772.  
  8773.  
  8774.           made before, answers are retrieved from the extension table instead of being recomputed.
  8775.  
  8776.  
  8777.  
  8778.           Extension tables provide a caching mechanism for Prolog. In addition,  extension  tables
  8779.  
  8780.  
  8781.  
  8782.           affect  the  termination  characteristics  of  recursive programs. Some Prolog programs,
  8783.  
  8784.  
  8785.  
  8786.  
  8787.                                                     129
  8788.  
  8789.  
  8790.  
  8791.  
  8792.  
  8793.  
  8794.  
  8795.  
  8796.  
  8797.  
  8798.           which are logically correct, enter an infinite loop  due  to  recursive  predicates.  An
  8799.  
  8800.  
  8801.  
  8802.           extension  table saved on recursive predicates can find all answers (provided the set of
  8803.  
  8804.  
  8805.  
  8806.           such answers is finite) and terminate for some logic programs for which Prolog's evalua-
  8807.  
  8808.  
  8809.  
  8810.           tion  strategy  enters  an  infinite loop. Iterations over the extension table execution
  8811.  
  8812.  
  8813.  
  8814.           strategy provides complete evaluation of queries over  function-free  Horn  clause  pro-
  8815.  
  8816.  
  8817.  
  8818.           grams.
  8819.  
  8820.  
  8821.  
  8822.  
  8823.                To be able to use the simple extension table evaluation on a set of predicates, the
  8824.  
  8825.  
  8826.  
  8827.           source  file  should  either  be  consulted, or compiled with the t option (the t option
  8828.  
  8829.  
  8830.  
  8831.           keeps the assembler from optimizing subroutine linkage and allows  the  extension  table
  8832.  
  8833.  
  8834.  
  8835.           facility to intercept calls to predicates).
  8836.  
  8837.  
  8838.  
  8839.  
  8840.                To use extension table execution, all predicates that are to be saved in the exten-
  8841.  
  8842.  
  8843.  
  8844.           sion table must be passed to _e_t/1. For example,
  8845.  
  8846.  
  8847.  
  8848.                       | ?- et([pred1/1, pred2/2]), et(pred3/2)
  8849.  
  8850.  
  8851.  
  8852.  
  8853.                                                     130
  8854.  
  8855.  
  8856.  
  8857.  
  8858.  
  8859.  
  8860.  
  8861.  
  8862.  
  8863.  
  8864.           will set up ``ET-points'' for the for predicates _p_r_e_d_1/1,  _p_r_e_d_2/2  and  _p_r_e_d_3/2,  which
  8865.  
  8866.  
  8867.  
  8868.           will  cause  extension tables for these predicates to be maintained during execution. At
  8869.  
  8870.  
  8871.  
  8872.           the time of the call to _e_t/1, these predicates must be defined, either  by  having  been
  8873.  
  8874.  
  8875.  
  8876.           loaded, or through _c_o_n_s_u_l_t.
  8877.  
  8878.  
  8879.  
  8880.  
  8881.                The predicate _n_o_e_t/1 takes a list of  predicate/arity  pairs  for  which  ET-points
  8882.  
  8883.  
  8884.  
  8885.           should  be  deleted.   Notice  that once an ET-point has been set up for a predicate, it
  8886.  
  8887.  
  8888.  
  8889.           will be maintained unless explicitly deleted via _n_o_e_t/1.  If the definition of a  predi-
  8890.  
  8891.  
  8892.  
  8893.           cate  which has an ET-point defined is to be updated, the ET-point must first be deleted
  8894.  
  8895.  
  8896.  
  8897.           via _n_o_e_t/1.  The predicate can then be reloaded and a new ET-point established.  This is
  8898.  
  8899.  
  8900.  
  8901.           enforced  by  the  failure of the goal ``et(P/N)'' if an ET-point already exists for the
  8902.  
  8903.  
  8904.  
  8905.           argument predicate.  In this case, the following error message will be displayed:
  8906.  
  8907.  
  8908.  
  8909.                       *et* already defined for: P/N
  8910.  
  8911.  
  8912.  
  8913.  
  8914.  
  8915.  
  8916.  
  8917.  
  8918.  
  8919.                                                     131
  8920.  
  8921.  
  8922.  
  8923.  
  8924.  
  8925.  
  8926.  
  8927.  
  8928.  
  8929.  
  8930.                There are, in fact, two extension table algorithms:  a  simple  one,  which  simply
  8931.  
  8932.  
  8933.  
  8934.           caches  calls  to  predicates which have ET-points defined; and a complete ET algorithm,
  8935.  
  8936.  
  8937.  
  8938.           which iterates the simple extension table algorithm until no more answers can be  found.
  8939.  
  8940.  
  8941.  
  8942.           The  simple algorithm is more efficient than the complete one; however, the simple algo-
  8943.  
  8944.  
  8945.  
  8946.           rithm is not complete for certain especially nasty forms of mutual recursion, while  the
  8947.  
  8948.  
  8949.  
  8950.           complete algorithm is.  To use the simple extension table algorithm, predicates can sim-
  8951.  
  8952.  
  8953.  
  8954.           ply be called as usual.  The complete extension table algorithm  may  be  used  via  the
  8955.  
  8956.  
  8957.  
  8958.           query
  8959.  
  8960.  
  8961.  
  8962.                       | ?- et_star(Query).
  8963.  
  8964.  
  8965.  
  8966.  
  8967.  
  8968.           The extension table algorithm is intended for predicates that are ``essentially  pure'',
  8969.  
  8970.  
  8971.  
  8972.           and  results  are  not guaranteed for code using impure code.  The extension table algo-
  8973.  
  8974.  
  8975.  
  8976.           rithm saves only those answers which are not instances of what is already in the  table,
  8977.  
  8978.  
  8979.  
  8980.           and  uses  these answers if the current call is an instance of a call already made.  For
  8981.  
  8982.  
  8983.  
  8984.  
  8985.                                                     132
  8986.  
  8987.  
  8988.  
  8989.  
  8990.  
  8991.  
  8992.  
  8993.  
  8994.  
  8995.  
  8996.           example, if a call _p(_X, _Y), with _X and _Y uninstantiated,  is  encountered  and  inserted
  8997.  
  8998.  
  8999.  
  9000.           into  the  extension  table,  then  a subsequent call _p(_X, _b) will be computed using the
  9001.  
  9002.  
  9003.  
  9004.           answers for _p(_X, _Y) already in the extension table.  Notice that this might not work  if
  9005.  
  9006.  
  9007.  
  9008.           var/nonvar tests are used on the second argument in the evaluation of _p.
  9009.  
  9010.  
  9011.  
  9012.  
  9013.                Another problem with using impure code is that if an ET predicate is cut over, then
  9014.  
  9015.  
  9016.  
  9017.           the  saved call implies that all answers for that predicate were computed, but there are
  9018.  
  9019.  
  9020.  
  9021.           only partial results in the ET because of the cut.  So on a subsequent call  the  incom-
  9022.  
  9023.  
  9024.  
  9025.           plete extension table answers are used when all answers are expected.
  9026.  
  9027.  
  9028.  
  9029.               Example:
  9030.                       r(X,Y) :- p(X,Y),q(Y,Z),!,fail.
  9031.                       | ?-  r(X,Y) ; p(X,Y).
  9032.  
  9033.  
  9034.  
  9035.  
  9036.  
  9037.           Let p be an ET predicate whose evaluation yields many tuples.  In the evaluation of  the
  9038.  
  9039.  
  9040.  
  9041.           query,  r(X,Y)  makes a call to p(X,Y).  Assuming that there is a tuple such that q(Y,Z)
  9042.  
  9043.  
  9044.  
  9045.           succeeds with the first p tuple then the evaluation of p is cut over. The call to p(X,Y)
  9046.  
  9047.  
  9048.  
  9049.  
  9050.  
  9051.                                                     133
  9052.  
  9053.  
  9054.  
  9055.  
  9056.  
  9057.  
  9058.  
  9059.  
  9060.  
  9061.  
  9062.           in  the query uses the extension table because of the previous call in the evaluation of
  9063.  
  9064.  
  9065.  
  9066.           r(X,Y). Only one answer is found, whereas the relation p contains many  tuples,  so  the
  9067.  
  9068.  
  9069.  
  9070.           computation  is  not  complete.   Note  that  "cuts" used within the evaluation of an ET
  9071.  
  9072.  
  9073.  
  9074.           predicate are ok, as long as they don't cut over the evaluation of another ET predicate.
  9075.  
  9076.  
  9077.  
  9078.           The evaluation of the predicate that uses cuts does not cut over any et processing (such
  9079.  
  9080.  
  9081.  
  9082.           as storing or retrieving answers) so that the tuples that are computed are saved. In the
  9083.  
  9084.  
  9085.  
  9086.           following  example, the ET is used to generate prime numbers where an ET point is put on
  9087.  
  9088.  
  9089.  
  9090.           prime/1.
  9091.  
  9092.  
  9093.  
  9094.               Example:
  9095.  
  9096.               prime(I) :- globalset(globalgenint(2)),fail.                    /* Generating Primes */
  9097.               prime(I) :- genint(I), not(div(I)).
  9098.               div(I) :- prime(X), 0 is I mod X.
  9099.  
  9100.               genint(N) :-
  9101.                   repeat,
  9102.                       globalgenint(N),
  9103.                       N1 is N+1,
  9104.                       globalset(globalgenint(N1)).
  9105.  
  9106.  
  9107.  
  9108.  
  9109.  
  9110.  
  9111.  
  9112.  
  9113.  
  9114.  
  9115.  
  9116.  
  9117.                                                     134
  9118.  
  9119.  
  9120.  
  9121.  
  9122.  
  9123.  
  9124.  
  9125.  
  9126.  
  9127.  
  9128.           The following summarizes the library predicates supporting the extension table facility:
  9129.  
  9130.  
  9131.  
  9132.  
  9133.  
  9134.  
  9135.           et(_L)
  9136.  
  9137.  
  9138.  
  9139.  
  9140.                Sets up an ET-point on the predicates _L, which causes calls  and  anwers  to  these
  9141.  
  9142.  
  9143.  
  9144.                predicates  to  be saved in an ``extension table''.  _L is either a term _P_r_e_d/_A_r_i_t_y,
  9145.  
  9146.  
  9147.  
  9148.                where _P_r_e_d is a predicate symbol and _A_r_i_t_y its  arity,  or  a  set  of  such  terms
  9149.  
  9150.  
  9151.  
  9152.                represented  as a list.  _L must be instantiated, and the predicates specified in it
  9153.  
  9154.  
  9155.  
  9156.                defined, at the time of the call to _e_t/1.  Gives error messages and fails if any of
  9157.  
  9158.  
  9159.  
  9160.                the  predicates in _L is undefined, or if an ET-point already exists on any of them;
  9161.  
  9162.  
  9163.  
  9164.                in this case, no ET-point is set up on any of the predicates in _L.
  9165.  
  9166.  
  9167.  
  9168.  
  9169.           et_star(Goal)
  9170.  
  9171.  
  9172.  
  9173.  
  9174.                Invokes the complete extension table algorithm on the goal _G_o_a_l.
  9175.  
  9176.  
  9177.  
  9178.  
  9179.  
  9180.  
  9181.  
  9182.  
  9183.                                                     135
  9184.  
  9185.  
  9186.  
  9187.  
  9188.  
  9189.  
  9190.  
  9191.  
  9192.  
  9193.  
  9194.           _e_t__p_o_i_n_t_s(_L)
  9195.  
  9196.  
  9197.  
  9198.  
  9199.                Unifies _L with a list of predicates for which an ET-point is  defined.   _L  is  the
  9200.  
  9201.  
  9202.  
  9203.                empty list [] if there are no ET-points defined.
  9204.  
  9205.  
  9206.  
  9207.  
  9208.           noet(_L)
  9209.  
  9210.  
  9211.  
  9212.  
  9213.                Deletes ET-points on the predicates specified in _L.  _L is either a term _P/_N,  where
  9214.  
  9215.  
  9216.  
  9217.                _P is the name of a predicate and _N its arity, or a set of such terms represented as
  9218.  
  9219.  
  9220.  
  9221.                a list.  Gives error messages and fails if there is  no  ET-point  on  any  of  the
  9222.  
  9223.  
  9224.  
  9225.                predicates  specified  in _L.  Deleting an ET-point for a predicate also removes the
  9226.  
  9227.  
  9228.  
  9229.                calls and answers stored in the extension table for that predicate.  The  extension
  9230.  
  9231.  
  9232.  
  9233.                tables  for  all  predicates  for  which ET-points are defined may be deleted using
  9234.  
  9235.  
  9236.  
  9237.                _e_t__p_o_i_n_t_s/1 in cojnunction with _n_o_e_t/1.
  9238.  
  9239.  
  9240.  
  9241.  
  9242.  
  9243.                _L must be instantiated at the time of the call to _n_o_e_t/1.
  9244.  
  9245.  
  9246.  
  9247.  
  9248.  
  9249.                                                     136
  9250.  
  9251.  
  9252.  
  9253.  
  9254.  
  9255.  
  9256.  
  9257.  
  9258.  
  9259.  
  9260.           et_remove(_L)
  9261.  
  9262.  
  9263.  
  9264.  
  9265.                Removes both calls and answers for the predicates specified in _L.  In effect,  this
  9266.  
  9267.  
  9268.  
  9269.                results  in the extension table for these predicates to be set to empty.  _L must be
  9270.  
  9271.  
  9272.  
  9273.                instantiated at the time of the call to either a term _P/_N, where _P is  a  predicate
  9274.  
  9275.  
  9276.  
  9277.                with arity _N, or a list of such terms.  An error occurs if any of the predicates in
  9278.  
  9279.  
  9280.  
  9281.                _L does not have an ET-point set.
  9282.  
  9283.  
  9284.  
  9285.  
  9286.  
  9287.                All extension tables can be  emptied  by  using  _e_t__p_o_i_n_t_s/1  in  conjunction  with
  9288.  
  9289.  
  9290.  
  9291.                _e_t__r_e_m_o_v_e/1.
  9292.  
  9293.  
  9294.  
  9295.  
  9296.           et_answers(_P/_N, _T_e_r_m)
  9297.  
  9298.  
  9299.  
  9300.  
  9301.                Retrieves the answers stored in the extension table for the predicate _P/_N  in  _T_e_r_m
  9302.  
  9303.  
  9304.  
  9305.                one  at  a  time.   _T_e_r_m  is  of the form _P(_t<1>, ..., _t<_N>).  An error results and
  9306.  
  9307.  
  9308.  
  9309.                _e_t__a_n_s_w_e_r_s/2 fails if _P/_N is not fully specified (ground), or if _P/_N does not  have
  9310.  
  9311.  
  9312.  
  9313.  
  9314.  
  9315.                                                     137
  9316.  
  9317.  
  9318.  
  9319.  
  9320.  
  9321.  
  9322.  
  9323.  
  9324.  
  9325.  
  9326.                an ET-point set.
  9327.  
  9328.  
  9329.  
  9330.  
  9331.           et_calls(_P/_N, _T_e_r_m)
  9332.  
  9333.  
  9334.  
  9335.  
  9336.                Retrieves the calls stored in the extension table for the predicate _P/_N in _T_e_r_m one
  9337.  
  9338.  
  9339.  
  9340.                at  a  time.   _T_e_r_m  is  of  the  form  _P(_t<1>,  ...,  _t<_N>).  An error results and
  9341.  
  9342.  
  9343.  
  9344.                _e_t__c_a_l_l_s/2 fails if _P/_N is not fully specified (ground), or if _P/_N does not have an
  9345.  
  9346.  
  9347.  
  9348.                ET-point set.
  9349.  
  9350.  
  9351.  
  9352.  
  9353.           _1_2.  _O_t_h_e_r _L_i_b_r_a_r_y _U_t_i_l_i_t_i_e_s
  9354.  
  9355.  
  9356.  
  9357.  
  9358.  
  9359.  
  9360.                The SB-Prolog library contains various other utilities, some of  which  are  listed
  9361.  
  9362.  
  9363.  
  9364.           below.
  9365.  
  9366.  
  9367.  
  9368.  
  9369.           $append(_X, _Y, _Z)
  9370.  
  9371.  
  9372.  
  9373.  
  9374.  
  9375.  
  9376.  
  9377.  
  9378.  
  9379.  
  9380.  
  9381.                                                     138
  9382.  
  9383.  
  9384.  
  9385.  
  9386.  
  9387.  
  9388.  
  9389.  
  9390.  
  9391.  
  9392.                Succeeds if list _Z is the concatenation of lists _X and _Y.
  9393.  
  9394.  
  9395.  
  9396.  
  9397.           $member(_X, _L)
  9398.  
  9399.  
  9400.  
  9401.  
  9402.                Checks whether _X unifies with any element of list _L, succeeding more than  once  if
  9403.  
  9404.  
  9405.  
  9406.                there are multiple such elements.
  9407.  
  9408.  
  9409.  
  9410.  
  9411.           $memberchk(_X, _L)
  9412.  
  9413.  
  9414.  
  9415.  
  9416.                Similar to $_m_e_m_b_e_r/2, except that $_m_e_m_b_e_r_c_h_k/2  is  deterministic,  i.e.  does  not
  9417.  
  9418.  
  9419.  
  9420.                succeed more than once for any call.
  9421.  
  9422.  
  9423.  
  9424.  
  9425.           $reverse(_L, _R)
  9426.  
  9427.  
  9428.  
  9429.  
  9430.                Succeeds if _R is the reverse of list _L.  If _L is not a fully determined list,  i.e.
  9431.  
  9432.  
  9433.  
  9434.                if the tail of _L is a variable, this predicate can succeed arbitrarily many times.
  9435.  
  9436.  
  9437.  
  9438.  
  9439.           $merge(_X, _Y, _Z)
  9440.  
  9441.  
  9442.  
  9443.  
  9444.  
  9445.  
  9446.  
  9447.                                                     139
  9448.  
  9449.  
  9450.  
  9451.  
  9452.  
  9453.  
  9454.  
  9455.  
  9456.  
  9457.  
  9458.                Succeeds if _Z is the list resulting from ``merging'' lists _X and _Y, i.e.  the  ele-
  9459.  
  9460.  
  9461.  
  9462.                ments  of  _X  together with any element of _Y not occurring in _X.  If _X or _Y contain
  9463.  
  9464.  
  9465.  
  9466.                duplicates, _Z may also contain duplicates.
  9467.  
  9468.  
  9469.  
  9470.  
  9471.           $absmember(_X, _L)
  9472.  
  9473.  
  9474.  
  9475.  
  9476.                Similar to $_m_e_m_b_e_r/2, except that it checks for identity (through ==/2) rather than
  9477.  
  9478.  
  9479.  
  9480.                unifiability (through =/2) of _X with elements of _L.
  9481.  
  9482.  
  9483.  
  9484.  
  9485.           $nthmember(_X, _L, _N)
  9486.  
  9487.  
  9488.  
  9489.  
  9490.                Succeeds if the _N[th.] element of the list _L unifies with _X.  Fails if _N is greater
  9491.  
  9492.  
  9493.  
  9494.                than  the  length  of _L.  Either _X and _L, or _L and _N, should be instantiated at the
  9495.  
  9496.  
  9497.  
  9498.                time of the call.
  9499.  
  9500.  
  9501.  
  9502.  
  9503.           $member2(_X, _L)
  9504.  
  9505.  
  9506.  
  9507.  
  9508.  
  9509.  
  9510.  
  9511.  
  9512.  
  9513.                                                     140
  9514.  
  9515.  
  9516.  
  9517.  
  9518.  
  9519.  
  9520.  
  9521.  
  9522.  
  9523.  
  9524.                Checks whether _X unifies with any of the actual elements of _L.  The only difference
  9525.  
  9526.  
  9527.  
  9528.                between  this  and $_m_e_m_b_e_r/2 is on lists with a variable tail, e.g. [a, b, c | _ ]:
  9529.  
  9530.  
  9531.  
  9532.                while $_m_e_m_b_e_r/2 would insert _X at the end of such a list if it  did  not  find  it,
  9533.  
  9534.  
  9535.  
  9536.                $_m_e_m_b_e_r_2/2 only checks for membership but does not insert it into the list if it is
  9537.  
  9538.  
  9539.  
  9540.                not there.
  9541.  
  9542.  
  9543.  
  9544.  
  9545.           _1_3.  _P_r_a_g_m_a _F_i_l_e_s
  9546.  
  9547.  
  9548.  
  9549.  
  9550.  
  9551.  
  9552.                Users may specify pragma information about SB-Prolog programs in a file called  the
  9553.  
  9554.  
  9555.  
  9556.           _p_r_a_g_m_a  _f_i_l_e for the program.  The pragma file corresponding to a Prolog source file _f_o_o
  9557.  
  9558.  
  9559.  
  9560.           is a file _f_o_o._d_e_f which is either in the same directory as the source file, or is  in  a
  9561.  
  9562.  
  9563.  
  9564.           subdirectory _d_e_f_s in the directory containing the source file.  In other words, relative
  9565.  
  9566.  
  9567.  
  9568.           to the directory containing the source file _f_o_o, the pragma file can be  either  _f_o_o._d_e_f
  9569.  
  9570.  
  9571.  
  9572.           or _d_e_f_s/_f_o_o._d_e_f.
  9573.  
  9574.  
  9575.  
  9576.  
  9577.  
  9578.  
  9579.                                                     141
  9580.  
  9581.  
  9582.  
  9583.  
  9584.  
  9585.  
  9586.  
  9587.  
  9588.  
  9589.  
  9590.                Pragma information in such a file is specified as a set of facts _p_r_a_g/_3.  The first
  9591.  
  9592.  
  9593.  
  9594.           and  second  arguments  are, respectively, the name and arity of the predicate for which
  9595.  
  9596.  
  9597.  
  9598.           information is being specified.  The third argument is the pragma being  specified,  and
  9599.  
  9600.  
  9601.  
  9602.           can  be  either a term, or a list of terms.  Thus, for example, to specify that an index
  9603.  
  9604.  
  9605.  
  9606.           is to be created on the first argument position for a predicate _b_a_r/3 in the  file  _f_o_o,
  9607.  
  9608.  
  9609.  
  9610.           we  would  enter,  in a file _f_o_o._d_e_f, the fact ``prag(bar, 3, index)'' or ``prag(bar, 3,
  9611.  
  9612.  
  9613.  
  9614.           [index])''.
  9615.  
  9616.  
  9617.  
  9618.  
  9619.                The pragma information that may be specified is limited, at this point, to informa-
  9620.  
  9621.  
  9622.  
  9623.           tion  about  indexing.   If  an index is to be created on argument _n of a predicate, the
  9624.  
  9625.  
  9626.  
  9627.           corresponding pragma is a term ``index(_n)''.  In the special  case  where  _n  =  1,  the
  9628.  
  9629.  
  9630.  
  9631.           pragma ``_i_n_d_e_x(1)'' may be abbreviated to ``_i_n_d_e_x''.
  9632.  
  9633.  
  9634.  
  9635.  
  9636.  
  9637.  
  9638.  
  9639.  
  9640.  
  9641.  
  9642.  
  9643.  
  9644.  
  9645.                                                     142
  9646.  
  9647.  
  9648.  
  9649.  
  9650.  
  9651.  
  9652.  
  9653.  
  9654.  
  9655.  
  9656.           Acknowledgements
  9657.  
  9658.  
  9659.  
  9660.  
  9661.                A large number of people were involved, at some time or  another,  with  the  Logic
  9662.  
  9663.  
  9664.  
  9665.           Programming  group  at SUNY, Stony Brook, and  deserve credit for bringing the SB-Prolog
  9666.  
  9667.  
  9668.  
  9669.           system to its present form.  The following names, in particular, ought to be  mentioned:
  9670.  
  9671.  
  9672.  
  9673.           David  Scott  Warren,  Weidong  Chen,  Suzanne Dietrich, Sanjay Manchanda, Jiyang Xu and
  9674.  
  9675.  
  9676.  
  9677.           David Znidarsic.  The author is also grateful to Fernando Pereira for permission to  use
  9678.  
  9679.  
  9680.  
  9681.           material  from the C-Prolog manual for the descriptions of Prolog syntax and many of the
  9682.  
  9683.  
  9684.  
  9685.           builtins.  junk
  9686.  
  9687.  
  9688.  
  9689.  
  9690.  
  9691.  
  9692.  
  9693.  
  9694.  
  9695.  
  9696.  
  9697.  
  9698.  
  9699.  
  9700.  
  9701.  
  9702.  
  9703.  
  9704.  
  9705.  
  9706.  
  9707.  
  9708.  
  9709.  
  9710.  
  9711.                                                      53
  9712.  
  9713.  
  9714.  
  9715.  
  9716.  
  9717.  
  9718.  
  9719.  
  9720.  
  9721.  
  9722.           _A_p_p_e_n_d_i_x _1: _E_v_a_l_u_a_b_l_e _P_r_e_d_i_c_a_t_e_s _o_f _S_B-_P_r_o_l_o_g
  9723.  
  9724.  
  9725.  
  9726.  
  9727.  
  9728.  
  9729.  
  9730.  
  9731.                An entry of ``B'' indicates a builtin predicate, ``I''  an  inline  predicate,  and
  9732.  
  9733.  
  9734.  
  9735.           ``L''  a  library  predicate.   A  ``P''  indicates that the predicate is handled by the
  9736.  
  9737.  
  9738.  
  9739.           preprocessor during compilation and/or consulting.
  9740.  
  9741.  
  9742.  
  9743.  
  9744.           (L)      _$undefined_pred/1 .........   10
  9745.  
  9746.  
  9747.           (L)      compile/1 ..................   11
  9748.  
  9749.  
  9750.           (L)      compile/2 ..................   11
  9751.  
  9752.  
  9753.           (L)      compile/3 ..................   11
  9754.  
  9755.  
  9756.           (L)      compile/4 ..................   11
  9757.  
  9758.  
  9759.           (B)      load/1 .....................   13
  9760.  
  9761.  
  9762.           (L)      consult/1 ..................   15
  9763.  
  9764.  
  9765.           (L)      consult/2 ..................   15
  9766.  
  9767.  
  9768.           (P)      :-/1 .......................   18
  9769.  
  9770.  
  9771.           (L)      op/3 .......................   29
  9772.  
  9773.  
  9774.  
  9775.  
  9776.  
  9777.  
  9778.  
  9779.  
  9780.  
  9781.  
  9782.  
  9783.  
  9784.  
  9785.  
  9786.  
  9787.  
  9788.  
  9789.  
  9790.  
  9791.  
  9792.  
  9793.  
  9794.  
  9795.  
  9796.  
  9797.  
  9798.  
  9799.  
  9800.  
  9801.  
  9802.  
  9803.  
  9804.                                                         (B)      see/1 ......................   40
  9805.  
  9806.  
  9807.                                                         (B)      seen .......................   41
  9808.  
  9809.  
  9810.                                                         (B)      tell/1 .....................   41
  9811.  
  9812.  
  9813.                                                         (B)      telling/1 ..................   41
  9814.  
  9815.  
  9816.                                                         (B)      told/0 .....................   41
  9817.  
  9818.  
  9819.                                                         (B)      $exists/1 ..................   41
  9820.  
  9821.  
  9822.                                                         (L)      read/1 .....................   42
  9823.  
  9824.  
  9825.                                                         (L)      write/1 ....................   42
  9826.  
  9827.  
  9828.                                                         (L)      display/1 ..................   43
  9829.  
  9830.  
  9831.                                                         (L)      writeq/1 ...................   43
  9832.  
  9833.  
  9834.  
  9835.  
  9836.  
  9837.                                                      54
  9838.  
  9839.  
  9840.  
  9841.  
  9842.  
  9843.  
  9844.  
  9845.  
  9846.  
  9847.           (L)      print/1 ....................   43
  9848.  
  9849.  
  9850.           (B)      writename/1 ................   43
  9851.  
  9852.  
  9853.           (B)      writeqname/1 ...............   43
  9854.  
  9855.  
  9856.           (B)      nl/0 .......................   44
  9857.  
  9858.  
  9859.           (B)      get0/1 .....................   44
  9860.  
  9861.  
  9862.           (B)      get/1 ......................   44
  9863.  
  9864.  
  9865.           (B)      put/1 ......................   44
  9866.  
  9867.  
  9868.           (B)      tab/1 ......................   45
  9869.  
  9870.  
  9871.           (I)      is/2 .......................   48
  9872.  
  9873.  
  9874.           (L)      eval/2 .....................   49
  9875.  
  9876.  
  9877.           (I)      =:=/2 ......................   50
  9878.  
  9879.  
  9880.           (I)      =\=/2 ......................   50
  9881.  
  9882.  
  9883.           (I)      </2 ........................   50
  9884.  
  9885.  
  9886.           (I)      >/2 ........................   50
  9887.  
  9888.  
  9889.           (I)      =</2 .......................   51
  9890.  
  9891.  
  9892.           (I)      >=/2 .......................   51
  9893.  
  9894.  
  9895.           (B)     floor/2 .....................   52
  9896.  
  9897.  
  9898.           (B)     floatc/3 ....................   52
  9899.  
  9900.  
  9901.  
  9902.  
  9903.  
  9904.  
  9905.  
  9906.  
  9907.  
  9908.  
  9909.  
  9910.  
  9911.  
  9912.  
  9913.  
  9914.  
  9915.  
  9916.  
  9917.  
  9918.  
  9919.  
  9920.  
  9921.  
  9922.  
  9923.  
  9924.  
  9925.  
  9926.  
  9927.  
  9928.  
  9929.  
  9930.  
  9931.  
  9932.  
  9933.  
  9934.  
  9935.  
  9936.  
  9937.  
  9938.  
  9939.  
  9940.  
  9941.  
  9942.  
  9943.  
  9944.  
  9945.  
  9946.  
  9947.  
  9948.  
  9949.  
  9950.  
  9951.                                                         (B)     exp/2 .......................   53
  9952.  
  9953.  
  9954.                                                         (B)     square/2 ....................   53
  9955.  
  9956.  
  9957.                                                         (B)     sin/2 .......................   53
  9958.  
  9959.  
  9960.                                                         (I)      `,'/2 ......................   54
  9961.  
  9962.  
  9963.                                                         (I)      `;'/2 ......................   54
  9964.  
  9965.  
  9966.                                                         (I)      true/0 .....................   54
  9967.  
  9968.  
  9969.                                                         (I)      =/2 ........................   54
  9970.  
  9971.  
  9972.                                                         (P)      !/0 ........................   55
  9973.  
  9974.  
  9975.                                                         (P)      not/1 ......................   55
  9976.  
  9977.  
  9978.                                                         (P)      ->/2 .......................   56
  9979.  
  9980.  
  9981.                                                         (L)      repeat/0 ...................   56
  9982.  
  9983.  
  9984.                                                         (I)      fail/0 .....................   56
  9985.  
  9986.  
  9987.                                                         (I)      var/1 ......................   56
  9988.  
  9989.  
  9990.                                                         (I)      nonvar/1 ...................   57
  9991.  
  9992.  
  9993.                                                         (B)      atom/1 .....................   57
  9994.  
  9995.  
  9996.                                                         (B)      integer/1 ..................   57
  9997.  
  9998.  
  9999.                                                         (B)     real/1 ......................   57
  10000.  
  10001.  
  10002.                                                         (B)      number/1 ...................   57
  10003.  
  10004.  
  10005.  
  10006.  
  10007.                                                      55
  10008.  
  10009.  
  10010.  
  10011.  
  10012.  
  10013.  
  10014.  
  10015.  
  10016.  
  10017.           (B)      atomic/1 ...................   58
  10018.  
  10019.  
  10020.           (B)     structure/1 .................   58
  10021.  
  10022.  
  10023.           (L)      functor/3 ..................   58
  10024.  
  10025.  
  10026.           (B)      arg/3 ......................   59
  10027.  
  10028.  
  10029.           (L)      =../2 ......................   59
  10030.  
  10031.  
  10032.           (B)      name/2 .....................   60
  10033.  
  10034.  
  10035.           (P)      call/1 .....................   60
  10036.  
  10037.  
  10038.           (B)      conlength/2 ................   62
  10039.  
  10040.  
  10041.           (L)      setof/3 ....................   63
  10042.  
  10043.  
  10044.           (L)      bagof/3 ....................   64
  10045.  
  10046.  
  10047.           (B)      ==/2 .......................   66
  10048.  
  10049.  
  10050.           (B)      \==/2 ......................   67
  10051.  
  10052.  
  10053.           (B)      @</2 .......................   67
  10054.  
  10055.  
  10056.           (B)      @>/2 .......................   67
  10057.  
  10058.  
  10059.           (B)      @=</2 ......................   68
  10060.  
  10061.  
  10062.           (B)      @>=/2 ......................   68
  10063.  
  10064.  
  10065.           (B)      compare/3 ..................   68
  10066.  
  10067.  
  10068.           (L)     sort/2 ......................   69
  10069.  
  10070.  
  10071.  
  10072.  
  10073.  
  10074.  
  10075.  
  10076.  
  10077.  
  10078.  
  10079.  
  10080.  
  10081.  
  10082.  
  10083.  
  10084.  
  10085.  
  10086.  
  10087.  
  10088.  
  10089.  
  10090.  
  10091.  
  10092.  
  10093.  
  10094.  
  10095.  
  10096.  
  10097.  
  10098.  
  10099.  
  10100.  
  10101.  
  10102.  
  10103.  
  10104.  
  10105.  
  10106.  
  10107.  
  10108.  
  10109.  
  10110.  
  10111.  
  10112.  
  10113.  
  10114.  
  10115.  
  10116.  
  10117.  
  10118.  
  10119.  
  10120.  
  10121.                                                         (L)      alloc_perm/2 ...............   71
  10122.  
  10123.  
  10124.                                                         (L)      alloc_heap/2 ...............   71
  10125.  
  10126.  
  10127.                                                         (L)      $alloc_heap/5 ..............   71
  10128.  
  10129.  
  10130.                                                         (L)      trimbuff/3 .................   72
  10131.  
  10132.  
  10133.                                                         (L)      $trim_buff/4 ...............   73
  10134.  
  10135.  
  10136.                                                         (L)      assert/1 ...................   76
  10137.  
  10138.  
  10139.                                                         (L)      assert/2 ...................   76
  10140.  
  10141.  
  10142.                                                         (L)      asserti/2 ..................   78
  10143.  
  10144.  
  10145.                                                         (L)     asserta/1 ...................   78
  10146.  
  10147.  
  10148.                                                         (L)     asserta/2 ...................   78
  10149.  
  10150.  
  10151.                                                         (L)     assertz/1 ...................   79
  10152.  
  10153.  
  10154.                                                         (L)     assertz/2 ...................   79
  10155.  
  10156.  
  10157.                                                         (L)      assert_union/2 .............   79
  10158.  
  10159.  
  10160.                                                         (L)      assert/4 ...................   80
  10161.  
  10162.  
  10163.                                                         (L)      retract/1 ..................   81
  10164.  
  10165.  
  10166.                                                         (L)      abolish/1 ..................   82
  10167.  
  10168.  
  10169.                                                         (L)      abolish/2 ..................   82
  10170.  
  10171.  
  10172.                                                         (L)      call_ref/2 .................   82
  10173.  
  10174.  
  10175.  
  10176.  
  10177.                                                      56
  10178.  
  10179.  
  10180.  
  10181.  
  10182.  
  10183.  
  10184.  
  10185.  
  10186.  
  10187.           (L)      call_ref/3 .................   83
  10188.  
  10189.  
  10190.           (L)      $db_new_prref/3 ............   84
  10191.  
  10192.  
  10193.           (L)      $db_assert_fact/5 ..........   84
  10194.  
  10195.  
  10196.           (L)      $db_add_clref/7 ............   85
  10197.  
  10198.  
  10199.           (L)      $db_call_prref/2 ...........   86
  10200.  
  10201.  
  10202.           (L)      $db_call_prref_s/2 .........   86
  10203.  
  10204.  
  10205.           (L)      $db_get_clauses/3 ..........   87
  10206.  
  10207.  
  10208.           (L)      $db_kill_clause/1 ..........   87
  10209.  
  10210.  
  10211.           (L)     break/0 .....................   88
  10212.  
  10213.  
  10214.           (B)     abort/0 .....................   89
  10215.  
  10216.  
  10217.           (B)     save/1 ......................   89
  10218.  
  10219.  
  10220.           (B)     restore/1 ...................   89
  10221.  
  10222.  
  10223.           (B)      cputime/1 ..................   90
  10224.  
  10225.  
  10226.           (L)      $getenv/2 ..................   90
  10227.  
  10228.  
  10229.           (B)      statistics/0 ...............   90
  10230.  
  10231.  
  10232.           (B)      symtype/2 ..................   91
  10233.  
  10234.  
  10235.           (B)      system/1 ...................   92
  10236.  
  10237.  
  10238.           (B)      syscall/3 ..................   92
  10239.  
  10240.  
  10241.  
  10242.  
  10243.  
  10244.  
  10245.  
  10246.  
  10247.  
  10248.  
  10249.  
  10250.  
  10251.  
  10252.  
  10253.  
  10254.  
  10255.  
  10256.  
  10257.  
  10258.  
  10259.  
  10260.  
  10261.  
  10262.  
  10263.  
  10264.  
  10265.  
  10266.  
  10267.  
  10268.  
  10269.  
  10270.  
  10271.  
  10272.  
  10273.  
  10274.  
  10275.  
  10276.  
  10277.  
  10278.  
  10279.  
  10280.  
  10281.  
  10282.  
  10283.  
  10284.  
  10285.  
  10286.  
  10287.  
  10288.  
  10289.  
  10290.  
  10291.                                                         (L)      globalset/1 ................   94
  10292.  
  10293.  
  10294.                                                         (L)      gennum/1 ...................   95
  10295.  
  10296.  
  10297.                                                         (L)      gensym/2 ...................   95
  10298.  
  10299.  
  10300.                                                         (L)      trace/1 ....................   95
  10301.  
  10302.  
  10303.                                                         (L)      untrace/1 ..................  101
  10304.  
  10305.  
  10306.                                                         (L)      spy/1 ......................  101
  10307.  
  10308.  
  10309.                                                         (L)      nospy/1 ....................  101
  10310.  
  10311.  
  10312.                                                         (L)      debug/0 ....................  102
  10313.  
  10314.  
  10315.                                                         (L)      nodebug/0 ..................  102
  10316.  
  10317.  
  10318.                                                         (L)      debugging/0 ................  102
  10319.  
  10320.  
  10321.                                                         (L)      tracepreds/1 ...............  102
  10322.  
  10323.  
  10324.                                                         (L)      spypreds/1 .................  103
  10325.  
  10326.  
  10327.                                                         (L)      trace/0 ....................  103
  10328.  
  10329.  
  10330.                                                         (L)      untrace/0 ..................  104
  10331.  
  10332.  
  10333.                                                         (P)      ::-/2 ......................  127
  10334.  
  10335.  
  10336.                                                         (L)      et/1 .......................  135
  10337.  
  10338.  
  10339.                                                         (L)      et_star/1 ..................  135
  10340.  
  10341.  
  10342.                                                         (L)      et_points/1 ................  136
  10343.  
  10344.  
  10345.  
  10346.  
  10347.                                                      57
  10348.  
  10349.  
  10350.  
  10351.  
  10352.  
  10353.  
  10354.  
  10355.  
  10356.  
  10357.           (L)      noet/1 .....................  136
  10358.  
  10359.  
  10360.           (L)      et_remove/1 ................  137
  10361.  
  10362.  
  10363.           (L)      et_calls/2 .................  138
  10364.  
  10365.  
  10366.           (L)      $append/3 ..................  139
  10367.  
  10368.  
  10369.           (L)      $member/2 ..................  139
  10370.  
  10371.  
  10372.           (L)      $reverse/2 .................  139
  10373.  
  10374.  
  10375.           (L)      $merge/3 ...................  140
  10376.  
  10377.  
  10378.           (L)      $absmember/2 ...............  140
  10379.  
  10380.  
  10381.           (L)      $nthmember/3 ...............  140
  10382.  
  10383.  
  10384.  
  10385.  
  10386.  
  10387.  
  10388.           _A_p_p_e_n_d_i_x _2: _A_d_d_i_n_g _B_u_i_l_t_i_n_s _t_o _S_B-_P_r_o_l_o_g
  10389.  
  10390.  
  10391.  
  10392.  
  10393.  
  10394.  
  10395.                Adding a builtin involves writing the C code for the desired case and installing it
  10396.  
  10397.  
  10398.  
  10399.           into  the  simulator.   The files in the irectory _s_i_m/_b_u_i_l_t_i_n contain the C code for the
  10400.  
  10401.  
  10402.  
  10403.           builtin predicates supported by the system.  The following procedure is to  be  followed
  10404.  
  10405.  
  10406.  
  10407.           when adding a builtin to the system:
  10408.  
  10409.  
  10410.  
  10411.  
  10412.  
  10413.  
  10414.  
  10415.  
  10416.  
  10417.                                                      58
  10418.  
  10419.  
  10420.  
  10421.  
  10422.  
  10423.  
  10424.  
  10425.  
  10426.  
  10427.  
  10428.           _I. _I_n_s_t_a_l_l_i_n_g _C _C_o_d_e:
  10429.  
  10430.  
  10431.  
  10432.  
  10433.           (1)  Go to the directory _s_i_m/_b_u_i_l_t_i_n.
  10434.  
  10435.  
  10436.  
  10437.  
  10438.           (2)  Look at the #defines in the file _b_u_i_l_t_i_n._h, and choose a number _N_1 (between  0  and
  10439.  
  10440.  
  10441.  
  10442.                255) which is not in use to be the builtin number for the new builtin.
  10443.  
  10444.  
  10445.  
  10446.  
  10447.           (3)  Add to the file _b_u_i_l_t_i_n._h the line
  10448.  
  10449.  
  10450.  
  10451.                            #define NEWBUILTIN _N_1
  10452.  
  10453.  
  10454.  
  10455.  
  10456.  
  10457.           (4)  The convention is that the code for builtin will be in  a  parameterless  procedure
  10458.  
  10459.  
  10460.  
  10461.                named  _b__N_E_W_B_U_I_L_T_I_N.  Modify the file _i_n_i_t__b_r_a_n_c_h._c in the directory _s_i_m/_b_u_i_l_t_i_n by
  10462.  
  10463.  
  10464.  
  10465.                adding these lines:
  10466.  
  10467.  
  10468.  
  10469.                            extern int b_NEWBUILTIN();
  10470.                    and
  10471.                            set_b_inst ( NEWBUILTIN, b_NEWBUILTIN );
  10472.  
  10473.  
  10474.  
  10475.  
  10476.                in the appropriate places.
  10477.  
  10478.  
  10479.  
  10480.  
  10481.  
  10482.  
  10483.                                                      59
  10484.  
  10485.  
  10486.  
  10487.  
  10488.  
  10489.  
  10490.  
  10491.  
  10492.  
  10493.  
  10494.           (5)  The builtins are compiled together into one object file, _b_u_i_l_t_i_n.  Update the  file
  10495.  
  10496.  
  10497.  
  10498.                _M_a_k_e_f_i_l_e  by  appending  the  name  of your object code file at the end of the line
  10499.  
  10500.  
  10501.  
  10502.                ``OBJS = ... '' and insert the appropriate commands to compile your C source  file,
  10503.  
  10504.  
  10505.  
  10506.                e.g.:
  10507.  
  10508.  
  10509.  
  10510.                    OBJS = [ ... _o_t_h_e_r _f_i_l_e _n_a_m_e_s ... ] newbuiltin.o
  10511.  
  10512.                     ...
  10513.  
  10514.                    newbuiltin.o: $(HS)
  10515.                            cc $(CFLAGS) newbuiltin.c
  10516.  
  10517.  
  10518.  
  10519.  
  10520.  
  10521.           (6)  Execute the updated make file to create an updated object file _b_u_i_l_t_i_n.
  10522.  
  10523.  
  10524.  
  10525.  
  10526.           (7)  Go to the directory _s_i_m and execute _m_a_k_e to install the new file _b_u_i_l_t_i_n.
  10527.  
  10528.  
  10529.  
  10530.  
  10531.  
  10532.  
  10533.           _I_I. _I_n_s_t_a_l_l_i_n_g _P_r_o_l_o_g _C_o_d_e:
  10534.  
  10535.  
  10536.  
  10537.  
  10538.                Assume that the builtin predicate to be added is _n_e_w_b_u_i_l_t_i_n/4.  The  procedure  for
  10539.  
  10540.  
  10541.  
  10542.           installing the Prolog code for this is as follows:
  10543.  
  10544.  
  10545.  
  10546.  
  10547.  
  10548.  
  10549.                                                      60
  10550.  
  10551.  
  10552.  
  10553.  
  10554.  
  10555.  
  10556.  
  10557.  
  10558.  
  10559.  
  10560.           (1)  Go to the SB-Prolog system directory _l_i_b/_s_r_c,  where  the  Prolog  source  for  the
  10561.  
  10562.  
  10563.  
  10564.                library routines is kept.
  10565.  
  10566.  
  10567.  
  10568.  
  10569.           (2)  Each builtin definition is of the form
  10570.  
  10571.  
  10572.  
  10573.  
  10574.  
  10575.                            pred( ... ) :- '_$builtin'(_N).
  10576.  
  10577.  
  10578.  
  10579.  
  10580.  
  10581.  
  10582.                where _N is an integer, the builtin number of _p_r_e_d.
  10583.  
  10584.  
  10585.  
  10586.  
  10587.           (3)  Create a Prolog source file _n_e_w_b_u_i_l_t_i_n._P (notice correspondence with  the  name  of
  10588.  
  10589.  
  10590.  
  10591.                the predicate being defined) containing the definition
  10592.  
  10593.  
  10594.  
  10595.  
  10596.  
  10597.                            newbuiltin(A,B,C,D) :- '_$builtin'(_N_1).
  10598.  
  10599.  
  10600.  
  10601.  
  10602.  
  10603.  
  10604.                where _N_1 is the builtin number of the predicate _n_e_w_b_u_i_l_t_i_n, obtained  when  instal-
  10605.  
  10606.  
  10607.  
  10608.                ling the C code for the builtin (see above).
  10609.  
  10610.  
  10611.  
  10612.  
  10613.  
  10614.  
  10615.                                                      61
  10616.  
  10617.  
  10618.  
  10619.  
  10620.  
  10621.  
  10622.  
  10623.  
  10624.  
  10625.  
  10626.           (4)  Compile this Prolog predicate, using the simulator and the _c_o_m_p_i_l_e predicate,  into
  10627.  
  10628.  
  10629.  
  10630.                a  file  _n_e_w_b_u_i_l_t_i_n  (notice  correspondence  with  the name of the predicate being
  10631.  
  10632.  
  10633.  
  10634.                defined) in the SB-Prolog directory _l_i_b.
  10635.  
  10636.  
  10637.  
  10638.  
  10639.  
  10640.  
  10641.  
  10642.  
  10643.  
  10644.  
  10645.  
  10646.  
  10647.  
  10648.  
  10649.  
  10650.  
  10651.  
  10652.  
  10653.  
  10654.  
  10655.  
  10656.  
  10657.  
  10658.  
  10659.  
  10660.  
  10661.  
  10662.  
  10663.  
  10664.  
  10665.  
  10666.  
  10667.  
  10668.  
  10669.  
  10670.  
  10671.  
  10672.  
  10673.  
  10674.  
  10675.  
  10676.  
  10677.  
  10678.  
  10679.  
  10680.  
  10681.                                                      62
  10682.  
  10683.  
  10684.  
  10685.  
  10686.  
  10687.  
  10688.  
  10689.  
  10690.  
  10691.  
  10692.                                                 TABLE OF CONTENTS
  10693.  
  10694.  
  10695.  
  10696.  
  10697.                  1 :  Introduction ..........................................................    2
  10698.  
  10699.  
  10700.                  2 :  Getting Started .......................................................    5
  10701.  
  10702.  
  10703.                    2.1 :  The Dynamic Loader Search Path ....................................    5
  10704.  
  10705.  
  10706.                    2.2 :  System Directories ................................................    7
  10707.  
  10708.  
  10709.                    2.3 :  Invoking the Simulator ............................................    8
  10710.  
  10711.  
  10712.                    2.4 :  Executing Programs ................................................   11
  10713.  
  10714.  
  10715.                       2.4.1 :  Compiling Programs ...........................................   11
  10716.  
  10717.  
  10718.                       2.4.2 :  Loading Byte Code Files ......................................   13
  10719.  
  10720.  
  10721.                       2.4.3 :  Consulting Programs ..........................................   15
  10722.  
  10723.  
  10724.                    2.5 :  Execution Directives ..............................................   18
  10725.  
  10726.  
  10727.                  3 :  Syntax ................................................................   20
  10728.  
  10729.  
  10730.                    3.1 :  Terms .............................................................   20
  10731.  
  10732.  
  10733.                    3.2 :  Operators .........................................................   25
  10734.  
  10735.  
  10736.                  4 :  SB-Prolog: Operational Semantics ......................................   33
  10737.  
  10738.  
  10739.                    4.1 :  Standard Execution Behaviour ......................................   33
  10740.  
  10741.  
  10742.                    4.2 :  Cuts and If-Then-Else .............................................   34
  10743.  
  10744.  
  10745.  
  10746.  
  10747.  
  10748.  
  10749.  
  10750.  
  10751.  
  10752.  
  10753.  
  10754.  
  10755.  
  10756.  
  10757.                    4.3 :  Unification of Floating Point Numbers .............................   36
  10758.  
  10759.  
  10760.                  5 :  Evaluable Predicates ..................................................   37
  10761.  
  10762.  
  10763.                    5.1 :  Input and Output ..................................................   40
  10764.  
  10765.  
  10766.                       5.1.1 :  File Handling ................................................   40
  10767.  
  10768.  
  10769.                       5.1.2 :  Term I/O .....................................................   42
  10770.  
  10771.  
  10772.                       5.1.3 :  Character I/O ................................................   44
  10773.  
  10774.  
  10775.                    5.2 :  Arithmetic ........................................................   45
  10776.  
  10777.  
  10778.                    5.3 :  Convenience .......................................................   54
  10779.  
  10780.  
  10781.                    5.4 :  Extra Control .....................................................   55
  10782.  
  10783.  
  10784.                    5.5 :  Meta-Logical ......................................................   56
  10785.  
  10786.  
  10787.                    5.6 :  Sets ..............................................................   62
  10788.  
  10789.  
  10790.                    5.7 :  Comparison of Terms ...............................................   65
  10791.  
  10792.  
  10793.                    5.8 :  Buffers ...........................................................   69
  10794.  
  10795.  
  10796.                    5.9 :  Modification of the Program .......................................   74
  10797.  
  10798.  
  10799.                    5.10 :  Environmental ....................................................   88
  10800.  
  10801.  
  10802.                    5.11 :  Global Values ....................................................   93
  10803.  
  10804.  
  10805.                  6 :  Debugging .............................................................   95
  10806.  
  10807.  
  10808.                    6.1 :  High Level Tracing ................................................   95
  10809.  
  10810.  
  10811.  
  10812.  
  10813.  
  10814.  
  10815.  
  10816.  
  10817.  
  10818.  
  10819.  
  10820.  
  10821.  
  10822.  
  10823.                    6.2 :  Low Level Tracing .................................................  103
  10824.  
  10825.  
  10826.                  7 :  The Simulator .........................................................  104
  10827.  
  10828.  
  10829.                    7.1 :  Invoking the Simulator ............................................  104
  10830.  
  10831.  
  10832.                    7.2 :  Simulator Options .................................................  107
  10833.  
  10834.  
  10835.                    7.3 :  Interrupt Handling ................................................  108
  10836.  
  10837.  
  10838.                  8 :  The Compiler ..........................................................  109
  10839.  
  10840.  
  10841.                    8.1 :  Structure of the Compiler .........................................  111
  10842.  
  10843.  
  10844.                    8.2 :  Invoking the Compiler .............................................  111
  10845.  
  10846.  
  10847.                    8.3 :  Compiler Options ..................................................  114
  10848.  
  10849.  
  10850.                    8.4 :  A Note on Coding for Efficiency ...................................  116
  10851.  
  10852.  
  10853.                       8.4.1 :  Avoiding Creation of Backtrack Points ........................  116
  10854.  
  10855.  
  10856.                       8.4.2 :  Minimizing Data Movement Between Registers ...................  120
  10857.  
  10858.  
  10859.                    8.5 :  Assembly ..........................................................  121
  10860.  
  10861.  
  10862.                  9 :  Modules ...............................................................  122
  10863.  
  10864.  
  10865.                  10 :  Macros ...............................................................  126
  10866.  
  10867.  
  10868.                    10.1 :  Defining Macros ..................................................  127
  10869.  
  10870.  
  10871.                    10.2 :  Macro Expander Options ...........................................  129
  10872.  
  10873.  
  10874.                  11 :  Extension Tables .....................................................  129
  10875.  
  10876.  
  10877.  
  10878.  
  10879.  
  10880.  
  10881.  
  10882.  
  10883.  
  10884.  
  10885.  
  10886.  
  10887.  
  10888.  
  10889.                  12 :  Other Library Utilities ..............................................  138
  10890.  
  10891.  
  10892.                  13 :  Pragma Files .........................................................  141
  10893.  
  10894.  
  10895.  
  10896.                Appendix 1: Evaluable Predicates of SB-Prolog ................................   54
  10897.  
  10898.  
  10899.  
  10900.                Appendix 2: Adding Builtins to SB-Prolog .....................................   58
  10901.  
  10902.  
  10903.  
  10904.  
  10905.  
  10906.  
  10907.  
  10908.  
  10909.  
  10910.  
  10911.  
  10912.  
  10913.  
  10914.  
  10915.  
  10916.  
  10917.  
  10918.  
  10919.  
  10920.  
  10921.  
  10922.  
  10923.  
  10924.  
  10925.  
  10926.  
  10927.  
  10928.  
  10929.  
  10930.  
  10931.  
  10932.  
  10933.  
  10934.  
  10935.  
  10936.  
  10937.  
  10938.  
  10939.  
  10940.  
  10941.  
  10942.  
  10943.  
  10944.  
  10945.  
  10946.  
  10947.  
  10948.  
  10949.