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 / MODLIB / MODLIB_SRC / $assert.P < prev    next >
Text File  |  1991-08-10  |  10KB  |  274 lines

  1. /************************************************************************
  2. *                                    *
  3. *    The SB-Prolog System                        *
  4. *    Copyright SUNY at Stony Brook, 1986                *
  5. *                                    *
  6. ************************************************************************/
  7.  
  8. /*-----------------------------------------------------------------
  9. SB-Prolog is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY.  No author or distributor
  11. accepts responsibility to anyone for the consequences of using it
  12. or for whether it serves any particular purpose or works at all,
  13. unless he says so in writing.  Refer to the SB-Prolog General Public
  14. License for full details.
  15.  
  16. Everyone is granted permission to copy, modify and redistribute
  17. SB-Prolog, but only under the conditions described in the
  18. SB-Prolog General Public License.   A copy of this license is
  19. supposed to have been given to you along with SB-Prolog so you
  20. can know your rights and responsibilities.  It should be in a
  21. file named COPYING.  Among other things, the copyright notice
  22. and this notice must be preserved on all copies. 
  23. ------------------------------------------------------------------ */
  24.  
  25.  
  26. /* new assert using assert_fact, $db  and rules */
  27.  
  28. $assert_export([$assert/1,$asserta/1,$asserta/2,$assertz/1,$assertz/2,
  29.         $assert/2,$asserti/2,$assert/4,$assert_union/2,$assert_call_s/1,
  30.         $assert_get_prref/2,$assert_put_prref/2,$assert_abolish_i/1]).
  31.  
  32. /* $assert_use($meta,[$functor/3,$univ/2,$length/2]).
  33.    $assert_use($blist,[$append/3,$member/2,$memberchk/2]).
  34.    $assert_use($buff,
  35.     [$alloc_perm/2,$alloc_heap/2,$trimbuff/3,$buff_code/4,$symtype/2,
  36.         $substring/6,$subnumber/6,$subdelim/6,$conlength/2,
  37.         $pred_undefined/1, $hashval/3]).
  38.    $assert_use($bio,[$writename/1,$writeqname/1,$put/1,$nl/0,$tab/1,
  39.     $tell/1,$tell/2,$telling/1,$told/0,$get/1,$get0/1,$see/1,$seeing/1,
  40.     $seen/0]).
  41.    $assert_use($db,[$db_new_prref/1,$db_assert_fact/5, $db_assert_fact/6,$db_assert_fact/7,
  42.            $db_assert_fact/8, $db_add_clref/6,
  43.         $db_call_prref/2,$db_call_prref_s/2,$db_call_prref_s/3,
  44.         $db_call_clref/2,$db_get_clauses/3,$db_kill_clause/1]).
  45. */
  46.  
  47. $assert_exp_cut((Head:-Body),(Nhead:-Nbody)) :- !,
  48.     $univ(Head,Hlist),$append(Hlist,[Cutpoint],Nhlist),
  49.     $univ(Nhead,Nhlist),
  50.     $assert_exp_cutb(Body,Nbody,Cutpoint).
  51.  
  52. $assert_exp_cut(Head,Head). /* leave unchanged, Arity is one less */
  53.  
  54. $assert_exp_cutb(X,X,_) :- var(X),!.
  55. $assert_exp_cutb(!,'_$cutto'(Cutpoint),Cutpoint) :- !.
  56. $assert_exp_cutb((A,B,C,D),','(Na,Nb,Nc,Nd),Cutpoint) :- !, /* opt */
  57.     $assert_exp_cutb(A,Na,Cutpoint),
  58.     $assert_exp_cutb(B,Nb,Cutpoint),
  59.     $assert_exp_cutb(C,Nc,Cutpoint),
  60.     $assert_exp_cutb(D,Nd,Cutpoint).
  61. $assert_exp_cutb((A,B),(Na,Nb),Cutpoint) :- !,
  62.     $assert_exp_cutb(A,Na,Cutpoint),
  63.     $assert_exp_cutb(B,Nb,Cutpoint).
  64. $assert_exp_cutb((A;B),(Na;Nb),Cutpoint) :- !,
  65.     $assert_exp_cutb(A,Na,Cutpoint),
  66.     $assert_exp_cutb(B,Nb,Cutpoint).
  67. $assert_exp_cutb((A->B),(A->Nb),Cutpoint) :- !,
  68.     $assert_exp_cutb(B,Nb,Cutpoint).
  69. $assert_exp_cutb(X,X,_).
  70.  
  71. $assert(Clause) :- $assert(Clause,1,0,_).
  72.  
  73. $asserta(Clause) :- $assert(Clause,0,0,_).
  74. $asserta(Clause,Ref) :- $assert(Clause,0,0,Ref).
  75.  
  76. $assertz(Clause) :- $assert(Clause,1,0,_).
  77. $assertz(Clause,Ref) :- $assert(Clause,1,0,Ref).
  78.  
  79. $assert(Clause,Options) :-
  80.     ($memberchk(index,Options) -> Index = 1 ;
  81.      $memberchk(index(Index),Options) ;
  82.      Index = 0
  83.     ),
  84.     ($memberchk(q,Options) -> Flatten = 1 ; Flatten = 0),
  85.     ($memberchk(first,Options) -> AZ = 0 ; AZ = 1),
  86.     $assert(Clause,AZ,Index,Clref,Flatten).
  87.  
  88. $asserti(Clause,Index) :- $assert(Clause,1,Index,_).
  89.  
  90. $assert(Clause, AZ, Index, Clref) :-
  91.     $assert(Clause, AZ, Index, Clref, 0).
  92.  
  93. $assert(Clause, AZ, Index, Clref,Flatten) :-
  94.     $assert_exp_cut(Clause,Nclause), /* write(Nclause),nl, */
  95.     $assert_cvt_dyn(Clause,Prref,Where,Supbuff),
  96.     $db_assert_fact(Nclause,Prref,AZ,Index,Clref,Flatten,Where,Supbuff).
  97.  
  98.  
  99. /* this is a translator for facts. It takes a term that represents 
  100.    a predicate call (a fact) and generates and writes the code 
  101.    corresponding to the fact into a buffer. It then asserts the fact 
  102.    by adding it to the end of the tryme-retryme-trustme sequence for
  103.    the main predicate of the fact.
  104. */
  105.  
  106.  
  107. /* $assert(Fact,AZ,Index,Clref):  asserts a fact to a fact-defined 
  108. predicate. Fact is the fact to assert. AZ is 0 for insertion as the
  109. first clause; 1 for insertion as the last clause. Index is the number of 
  110. the argument on which to index; 0 for no indexing. Clref is returned as
  111. the clause reference of the fact newly asserted. */
  112.  
  113.  
  114. $assert_cvt_dyn(Clause,Prref,Where,Supbuff) :-
  115.     (Clause = (Fact:-B),! ; Clause=Fact),
  116.     $symtype(Fact, SYMTYPE),
  117.     (SYMTYPE =:= 1 ->        /* already dynamic */
  118.       $assert_get_prref(Fact,Prref,Where,Supbuff)
  119.       ;
  120.       Where = 0,
  121.       (SYMTYPE =:= 0 ->        /* undefined, this is first clause */
  122.         $db_new_prref(Prref),
  123.         $assert_put_prref(Fact,Prref)
  124.         ;
  125.         (SYMTYPE =:= 2 ->        /* compiled, so convert */
  126.           $assert_cvt_buff(Fact,Ccls),
  127.           $db_new_prref(Prref),
  128.           $assert_put_prref(Fact,Prref),
  129.           $arity(Fact,Arity1),Arity is Arity1+1,
  130.           $db_add_clref(Fact,Arity,Prref,1,0,Ccls)
  131.           ;
  132.           $writename('Error, cannot assert into Buffer'),$nl,fail
  133.         )
  134.       )
  135.     ).
  136.  
  137.  
  138. /* return a buffer with a branch to the clauses for Fact */
  139. $assert_cvt_buff(Fact,Tbuff) :-
  140.     $alloc_perm(16,Tbuff),   /* buff to convert to dynamic */
  141.     $buff_code(Tbuff,0,14 /*ptv*/ ,Tbuff),    /* back ptr */
  142.     $buff_code(Tbuff,10,3 /*pb*/ ,240 /*jump*/ ),
  143.     $buff_code(Tbuff,11,3 /*pb*/ ,0),
  144.     $buff_code(Tbuff,12,20 /*pepb*/ ,Fact).
  145.  
  146.  
  147. /* assert_union adds the clauses of the second predicate
  148.    to the first predicate. E.g., given p(X,Y) and q(X,Y), it adds the rule
  149.    p(X,Y) :- q(X,Y) as the last rule defining p. If p is not defined, then
  150.    it results in the call of q being the only clause for p */
  151.  
  152. $assert_union(P,Q) :- 
  153.     $assert_cvt_buff(Q,Qclref),
  154.     $assert_cvt_dyn(P,Prref,0,0),
  155.     $arity(P,Arity1),Arity is Arity1+1,
  156.     $db_add_clref(P,Arity,Prref,1,0,Qclref).
  157.     
  158.  
  159.  
  160.  
  161. /* This defines routines that can be used to assert facts onto the heap.
  162. */
  163.  
  164. /* We have introduced a new simulator instruction similar  to the one
  165. used to translate variables in globalset.  It is a branch
  166. instruction, called executev.  It  derefs its  argument and  if it is
  167. not a variable, does an execute to main functor symbol.  (Execute has
  168. been modified so that when a buffer is called, it branches  to disp 4
  169. in the name.)  If it  is a  variable, it  gives an  error message and
  170. fails.  */ 
  171.  
  172. /* $assert_new_t_prref(Call,Prref,Supbuff):  Call must be
  173. instantiated to a term (just used for getting psc).  If  that psc has
  174. no e.p.  then this creates a permanent buffer  containing an executev
  175. instruction, and the constant  for the  Supbuff, and  points the e.p.
  176. of Call to it.  A Prref is allocated and  the target  of the executev
  177. is set to that.  If the psc already has an e.p., the predicate fails.
  178. */ 
  179.  
  180. $assert_new_t_prref(Call,Prref,Supbuff) :-
  181.     $symtype(Call,Type),
  182.     (Type =:= 1,    /* dynamic */
  183.      $buff_code(Call,0,7 /*gepb*/ ,Vbuff),
  184.      $buff_code(Vbuff,4,6 /*gb*/ ,249 /*noop*/ ),
  185.      $buff_code(Vbuff,5,6,0),
  186.      $buff_code(Vbuff,6,6,238 /* executev */ ),
  187.      $buff_code(Vbuff,8,18 /*ubv*/ ,Prref),
  188.      $db_new_prref(Prref,2,Supbuff),
  189.      $buff_code(Vbuff,12,18 /*ubv*/ ,Supbuff),
  190.      !
  191.     ;
  192.      $buff_code(Call,0,11,0), /* this overrides everything!! */
  193.      /* allocate new executev instruction, and supbuff ptr */
  194.      $alloc_perm(16,Vbuff), /* must make permanent */
  195.      $buff_code(Vbuff,0,14,Vbuff), /* set back ptr */
  196.      $buff_code(Call,0,9 /*pep*/ ,Vbuff),
  197.      $buff_code(Vbuff,4,3 /*pb*/ ,249 /*noop*/ ),
  198.      $buff_code(Vbuff,5,3,0),
  199.      $buff_code(Vbuff,6,3,238 /* executev */ ),
  200.      $buff_code(Vbuff,7,3,0),
  201.      $buff_code(Vbuff,8,12 /*fv*/ ,0),
  202.      $buff_code(Vbuff,12,12 /*fv*/ ,0),
  203.      $db_new_prref(Prref,2,Supbuff),
  204.      $buff_code(Vbuff,8,18 /*ubv*/ ,Prref),
  205.      $buff_code(Vbuff,12,18 /*ubv*/ ,Supbuff)
  206.     ).
  207.  
  208.  
  209. /* $assert_alloc_t must be called first to declare that a predicate (or set
  210. of predicates) are to have facts asserted into them on the  heap.  It
  211. is given a list of Pred/Arity pairs and a size.  That  amount of heap
  212. space is reserved for facts to  be asserted  to these  predicates.  A
  213. temporary prref buffer is created.  */ 
  214.  
  215. $assert_alloc_t(Palist,Size) :- 
  216.     $alloc_heap(Size,Sbuff),
  217.     $assert_alloc_t1(Palist,Sbuff).
  218.  
  219. $assert_alloc_t1([],_).
  220. $assert_alloc_t1([F|R],Supbuff) :- 
  221.     $assert_alloc_t1(F,Supbuff),$assert_alloc_t1(R,Supbuff).
  222. $assert_alloc_t1(P/A,Supbuff) :-
  223.     $bldstr(P,A,Term),
  224.     $assert_new_t_prref(Term,Prref,Supbuff).
  225.  
  226.  
  227.  
  228. $assert_call_s(Goal) :- 
  229.     $assert_get_prref(Goal,Prref,_,_),$db_call_prref_s(Goal,Prref).
  230.  
  231.  
  232. /* $assert_get_prref(Fact,Prref,Where,Supbuff):  where Fact is a
  233. literal, which should be dynamic. The e.p. field of the main functor
  234. symbol of Fact points to either a permanent prref, or a execv buffer
  235. that points to a temporary prref. If it is a permanent prref, Where
  236. is returned as 0; if a temporary, Where is set to 2, and Supbuff is
  237. bound to the superbuffer containing the clauses. */
  238.  
  239. $assert_get_prref(Fact,Prref) :- $assert_get_prref(Fact,Prref,_,_).
  240. $assert_get_prref(Fact,Prref,Where,Supbuff) :-
  241.     $symtype(Fact,Type),
  242.     (Type =:= 1 ->    /*DYNA: must be dynamic */
  243.         $buff_code(Fact,0,7 /*gepb*/ ,Vbuff),
  244.          ($buff_code(Vbuff,4,6 /*pb*/ ,249 /*noop*/ ),
  245.           $buff_code(Vbuff,5,6,0),
  246.           $buff_code(Vbuff,6,6,238 /* executev */ ),
  247.           Where=2,
  248.           $buff_code(Vbuff,8,18 /*ubv*/ ,Prref),
  249.           $buff_code(Vbuff,12,18 /*ubv*/ ,Supbuff),
  250.           !
  251.          ;
  252.           Prref=Vbuff,Where=0
  253.          )
  254.         ;
  255.          Type =\= 0, /* if undefined, just fail */
  256.          $writename('Error, Illegal Predicate ref: '),
  257.          $write(Fact),$nl,fail
  258.     ).
  259.  
  260. /* $assert_put_prref(Fact,Prref):  where Fact is a literal and Prref
  261. is an prref.  Prref must  be bound  to an  existing prref.   The e.p.
  262. field of the psc entry for the main functor symbol of Fact  is set to
  263. point to the Prref.  */ 
  264.  
  265. $assert_put_prref(Fact,Prref) :-
  266.     $buff_code(Fact,0,9 /*pep*/ ,Prref).
  267.  
  268. /* $assert_abolish_i(Fact): initializes the predicate that is the main 
  269. functor symbol of Fact to be empty, by allocating a new empty Prref and 
  270. assigning it. */
  271.  
  272. $assert_abolish_i(Fact) :- 
  273.     $db_new_prref(Prref),$assert_put_prref(Fact,Prref).
  274.