home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / modprolg / mod-prol.lha / Prolog / modlib / src / $consult.P < prev    next >
Encoding:
Text File  |  1992-06-01  |  58.2 KB  |  1,757 lines

  1. /****************************************************************************
  2.  *                                                                          *
  3.  * This file has been changed by to include Modules Extensions              *
  4.  * Changes by : Brian Paxton 1991/92                                        *
  5.  * Last update : June 1992                                                  *
  6.  *                                                                          *
  7.  * Organisation : University of Edinburgh.                                  *
  8.  * For : Departments of Computer Science and Artificial Intelligence        * 
  9.  *       Fourth Year Project.                                               *
  10.  *                                                                          *
  11.  ****************************************************************************/
  12.  
  13. /* $consult.P */
  14.  
  15. /* consult(Filename) consults the named file for interpretation.
  16.    The second (optional) parameter to consult is a list of options.
  17.    The third (optional) parameter is used to return a list of
  18.    predicate/arity pairs that were defined by the consult. This list
  19.    can be passed to trace/1 so that they can be traced.
  20. */
  21.  
  22. $consult_export([$consult/1,$consult/2,$consult/3,$consult_list/1]).
  23.  
  24. /* Details of the module environment database structures are given here :
  25.  
  26.    $module_structure(Name,Tag,Substrs,Preds,Funs)
  27.  
  28.      Stores the the signatures of the top structures declared at the
  29.      top level only. Substrs is a list of substructure maps of the form
  30.      'Name ---> Tag', and Preds and Funs are lists of items of the form
  31.      'Name ---> Internalname'.  Name is a module environment name of the
  32.      form 'A : B : ... : D', tags are simply integers and Internalname is a
  33.      tagged atom.
  34.  
  35.    $module_signature(Name,Tag,Substrs,Preds,Funs)
  36.  
  37.      Stores the names of any signatures defined at the top level. As
  38.      above.
  39.  
  40.    $module_functor(Name,Tag,Atids,Substrs,Preds,Funs,Strexpr,P,Q,R)
  41.  
  42.      Stores the names of any functors defined at the top level.
  43.      Substrs, Preds and Funs as above, Strexpr is the body of the functor,
  44.      and P, Q and R are the structure, signature and functor environments
  45.      respectively, at the time the functor declaration was made. These are
  46.      simply a list of integer tags referring to the tag entries in these
  47.      predicates.
  48.  
  49.    $mapped_function(X,Arity,Y,Str)
  50.  
  51.      Any declaration of the form 'fun X = Y' is stored as a clause in
  52.      this predicate. Str is the tag of the structure in which the
  53.      declaration was made.
  54.  
  55.    $declared_function(X)
  56.  
  57.      In order to preserve the function/predicate distinction in this
  58.      Prolog, any function defined as 'fun X' or 'fun X = ...' is stored in
  59.      this predicate.  It is later used only by assert/retract to ensure
  60.      that no attempt is made to assert a declared function into the
  61.      database (which has the effect of turning the function into a
  62.      predicate). This preserves functions as functions. Preserving
  63.      predicates as predicates is easier. If a predicate is defined by a
  64.      series of clauses, when those clauses are asserted by the consult
  65.      code, the predicate becomes a predicate. If it is defined by a 'pred
  66.      X' declaration, the consult code calls $assert_abolish_i(X), which has
  67.      the effect of turning X into a predicate by creating it as an 'empty'
  68.      predicate which simply fails when calls.
  69.  
  70.  
  71.      Note that throughout, P is the structure environment, Q is the
  72.      signature environment and R is the functor environment, each stored
  73.      as a list of Name ---> X mappings, where X is either a tag or a
  74.      datatype describing the construct.
  75. */
  76.  
  77. % $consult(File) 
  78. %
  79.  
  80. $consult(File) :-
  81.         $consult(File, [v], _).
  82.  
  83. % $consult(File, Opts)
  84. %
  85.  
  86. $consult(File, Opts) :-
  87.         $consult(File, Opts, _).
  88.  
  89. % $consult_list(Filelist)
  90. %
  91.  
  92. $consult_list([]).
  93. $consult_list([H|T]) :- $consult_list1(H), $consult_list(T).
  94.  
  95. % $consult_list1(File)
  96. %
  97.  
  98. $consult_list1('-'(File)) :- !, $consult(File).
  99. $consult_list1(File) :- $consult(File).
  100.  
  101. % $consult(File, Opts, Preds)
  102. %
  103. % Opts is a list containing :
  104. %  'v'  for 'verbose' loading.
  105. %  't'  to enable tracing on all loaded predicates.
  106.  
  107. $consult(File, Opts, Preds) :-
  108.     ( $mod_exists(File) ->
  109.         ( $see(File),
  110.           ( $memberchk(v, Opts) ->
  111.           ( $writename('[Opening '),
  112.             $writename(File),
  113.             $writename(']'),$nl ) ;
  114.           true ),
  115.           $mod_read_data(Data),
  116.           $seen,
  117.           $mod_convert_to_internal(Data, Opts, Preds),
  118.           ( $memberchk(v, Opts) ->
  119.           ( $writename('[Closing '),
  120.             $writename(File),
  121.             $writename(']'),$nl ) ;
  122.             true ) ) ;
  123.         $mod_error(['** Consult Error : File',File,'not found']) ), !.
  124.  
  125. % $mod_exists(File)
  126. %
  127.  
  128. $mod_exists(user).
  129. $mod_exists(File) :- exists(File).
  130.  
  131. % $mod_read_data(Input)
  132. %
  133.  
  134. $mod_read_data(List) :-
  135.     $read_module(Input),
  136.     ( Input == end_of_file ->
  137.           List = [] ;
  138.           ( $mod_read_data(Rest),
  139.             List = [Input|Rest] ) ).
  140.  
  141. % $mod_convert_to_internal(Data, Opts, Preds)
  142. %
  143.  
  144. $mod_convert_to_internal(Data, Opts, AllPreds) :-
  145.     $mod_explode_database(Sig, P, Q, R),
  146.         $mod_dec(Data, Sig, P, Q, R, sig(Substrs, Preds, Funs),
  147.                   P1, Q1, R1, Code, Opts, 0, yes, [], Funmap2), !,
  148.         $mod_translate_code(Code, Preds, Funs, Code1, 0), !,
  149.     ( $memberchk(v, Opts) ->
  150.         ( $writename('Updating database ...'), $nl ) ;
  151.         true ),
  152.     $mod_assert_code(Code1, [], AllPreds),
  153.     $mod_assert_functions(Funmap2),
  154.     $retract(($module_structure(root, 0, _, _, _) :- _)),
  155.     $mod_assert_structure(root, 0, Substrs, Preds, Funs),
  156.     ( $memberchk(t, Opts) -> $mod_trace_all(AllPreds) ;
  157.                              true ), !.
  158.  
  159. % $mod_assert_code(Code, Partial, Preds)
  160. %
  161.  
  162. $mod_assert_code([], Preds, Preds).
  163. $mod_assert_code(['_$done'(Head)|List], Preds, Result) :-
  164.     $assert(Head),
  165.     $mod_get_name(Head, Name),
  166.     ( $memberchk(Name, Preds) ->
  167.         $mod_assert_code(List, Preds, Result) ;
  168.         $mod_assert_code(List, [Name|Preds], Result) ).
  169. $mod_assert_code(['_$blank'(Name/Arity)|List], Preds, Result) :-
  170.     $bldstr(Name,Arity,Head),
  171.     $assert_abolish_i(Head), /* Create 'empty' predicate which will fail*/
  172.     ( $memberchk(Name/Arity, Preds) ->
  173.         $mod_assert_code(List, Preds, Result) ;
  174.         $mod_assert_code(List, [Name/Arity|Preds], Result) ).
  175.  
  176. % $mod_get_name(Clause, Name/Arity)
  177. %
  178.  
  179. $mod_get_name((X :- _), Name) :- !,
  180.     $mod_get_name(X, Name).
  181. $mod_get_name(Head, Name/Arity) :-
  182.     $functor0(Head, Name),
  183.     $arity(Head, Arity).
  184.  
  185. % $mod_assert_functions(Code)
  186. %
  187.  
  188. $mod_assert_functions(Code) :-
  189.     Code == [] -> true ;
  190.                   ( Code = [Head|List],
  191.             $mod_assert_functions(List),
  192.                 $asserta(Head) ).
  193.  
  194. % $mod_trace_all(Predlist)
  195. %
  196.  
  197. $mod_trace_all([]).
  198. $mod_trace_all([Pred|Rest]) :-
  199.     $symtype('_$traced_preds'(_), Type),
  200.     Type > 0, !,
  201.     ( '_$traced_preds'(Pred) -> true ;
  202.                                $trace(Pred) ),
  203.     $mod_trace_all(Rest).
  204. $mod_trace_all([Pred|Rest]) :-
  205.     $trace(Pred),
  206.     $mod_trace_all(Rest).
  207.                 
  208.  
  209. % $mod_explode_database(Sig, P, Q, R).
  210. %
  211. % P is the structure environment.
  212. % Q is the signature environment.
  213. % R is the functor environment.
  214. %
  215. % Explode grabs condensed form in database and expands it to full 
  216. % environment datastructures.
  217.  
  218. $mod_explode_database(sig(Substrs, Preds, Funs), P, Q, R) :-
  219.     $bagof(Name ---> Tag, Name^Tag^Substrs^Preds^Funs^
  220.                $module_structure(Name, Tag, Substrs, Preds, Funs), P1),
  221.     $mod_remove_root(P1, P),
  222.     $bagof(Name ---> Tag, Name^Tag^Substrs^Preds^Funs^
  223.                $module_signature(Name, Tag, Substrs, Preds, Funs), Q),
  224.     $bagof(Name ---> Tag,
  225.            Name^Tag^Atids^Substrs^Preds^Funs^Strexpr^P0^Q0^R0^
  226.                 $module_functor(Name, Tag, Atids, Substrs, Preds, Funs, 
  227.                                 Strexpr, P0, Q0, R0), R),
  228.     $module_structure(root, 0, Substrs, Preds, Funs), !.
  229.  
  230. % $mod_remove_root(Environment, Result)
  231. %
  232.  
  233. $mod_remove_root([root ---> 0|Rest], Rest).
  234. $mod_remove_root([], []).
  235. $mod_remove_root([Head|Tail], [Head|Result]) :-
  236.     $mod_remove_root(Tail, Result).
  237.  
  238. %****************************************************************************
  239.  
  240. % $mod_fit(Arg, Sig1, Str, Namestr, Namesig)
  241. % Fitting a structure to a signature.
  242.  
  243. $mod_fit(str(Tag, Sig), Sig1, str(Tag, Newsig), Namestr, Namesig) :- !,
  244.         $mod_fit0(Sig, Sig1, Newsig, Namestr, Namesig).
  245.  
  246. $mod_fit(Sig, Sig1, str(Tag, Newsig), Namestr, Namesig) :-
  247.     gennum(Tag),
  248.         $mod_fit0(Sig, Sig1, Newsig, Namestr, Namesig).
  249.  
  250. % $mod_fit0(Sig1, Sig2, Sig3, Namestr, Namesig)
  251. %
  252.  
  253. $mod_fit0(sig(Substrs, Preds, Funs), sig(Substrs1, Preds1, Funs1),
  254.                       sig(Substrs2, Preds2, Funs2), _, _) :-
  255.     $mod_domain_restrict(Substrs, Substrs1, Substrs2),
  256.     $mod_domain_restrict(Preds, Preds1, Preds2),
  257.     $mod_domain_restrict(Funs, Funs1, Funs2),
  258.     $mod_check_pairs(Substrs1, Substrs),
  259.     $mod_check_pairs(Preds1, Preds),
  260.     $mod_check_pairs(Funs1, Funs), !.
  261.  
  262. $mod_fit0(Str, Sig, _, Namestr, Namesig) :-
  263.     $mod_error(['** Consult Error : Fitting', Namestr,'to',Namesig]).
  264.  
  265. % $mod_check_pairs(One, Two)
  266. %
  267.  
  268. $mod_check_pairs([],_).
  269. $mod_check_pairs([A ---> Tag|Tail], Two) :-
  270.     $memberchk(B ---> Tag,Tail) ->
  271.             ( $memberchk(A ---> Tag2, Two),
  272.               $memberchk(B ---> Tag2, Two),
  273.                   $mod_check_pairs(Tail, Two) ) ;
  274.         $mod_check_pairs(Tail, Two).
  275.  
  276. %$mod_check_pairs(One, Two) :-
  277. %    $member(A ---> X, One),
  278. %    $member(B ---> X, One),
  279. %    A \= B,
  280. %    '_$savecp'(Cp),
  281. %    $mod_check_pair(A, B, Two, Cp),
  282. %    fail.
  283. %$mod_check_pairs(_, _).
  284. %
  285. %% $mod_check_pair(A, B, Two, Cp)
  286. %%
  287. %% 
  288. %
  289. %$mod_check_pair(A, B, Two, Cp) :-
  290. %    $member(A ---> X, Two),
  291. %    $member(B ---> X, Two).
  292. %$mod_check_pair(_, _, _, Cp) :-
  293. %    '_$cutto'(Cp),
  294. %    fail.
  295.  
  296. %----------------------------------------------------------------------------
  297.  
  298. % $mod_tag(Sig1, Sig2)
  299. % Generating new internal names for constants.
  300. % Note that this predicate is only ever called to handle signatures. If
  301. % the signatures were those of actual structures, then the maps would be
  302. % atoms. When we are dealing with signatures on their own, we only
  303. % require a datatype that satisfies sharing constraints - ie. distinct
  304. % values for distinct items, equal values for 'shared' items. We therefore
  305. % simplify and use only integers.
  306.  
  307. $mod_tag(sig(Substrs, Preds, Funs), sig(Substrs1, Preds1, Funs1)) :-
  308.     $mod_tag(Substrs, Substrs1),
  309.     $mod_tag(Preds, Preds1),
  310.     $mod_tag(Funs, Funs1).
  311.  
  312. % $mod_tag(Map1, Map2)
  313. %
  314.  
  315. $mod_tag(Old,New) :-
  316.     $setof(Map, X^Map^( member(X ---> Map,Old),
  317.                         not($pervasive(X)) ), Maps),
  318.     $mod_tag_pairs(Maps,Newmaps),
  319.     $mod_retag(Old,Newmaps,New).
  320.  
  321. $mod_tag_pairs([],[]).
  322. $mod_tag_pairs([Map|Tail],[(Map,New)|Ntail]) :-
  323.     $gennum(New),
  324.     $mod_tag_pairs(Tail,Ntail).
  325.  
  326. $mod_retag([],_,[]).
  327. $mod_retag([X ---> Old|Tail], Pairs, [X ---> New|Ntail]) :-
  328.     ( $memberchk((Old,New),Pairs) -> true ;
  329.                                          New = Old ),
  330.     $mod_retag(Tail,Pairs,Ntail).
  331.  
  332. %$mod_tag([], []).
  333. %$mod_tag([X ---> Y|Rest], [X ---> Z|New]) :-
  334. %    ( $pervasive(X) -> X = Z ; $gennum(Z) ),
  335. %    $mod_tag(Rest, New).
  336.  
  337. %----------------------------------------------------------------------------
  338.  
  339. % $mod_identify(Idlist, Sig, Newsig)
  340. % Identifying substructures in a signature.
  341.  
  342. $mod_identify([], Sig, Sig).
  343. $mod_identify([[Id, Id1]|Rest], Sig, Newsig) :-
  344.     $mod_identify(Rest, Sig, Sig1),
  345.     $mod_identify(Id, Id1, Sig1, Newsig).
  346.  
  347. % $mod_identify(Id, Id1, Sig1, Sig2)
  348. %
  349.  
  350. $mod_identify(Id, Id1, sig(Substrs,Preds,Funs), sig(Substrs1,Preds1,Funs1)) :-
  351.     $mod_identify_pairs(Preds, Preds, Ppairs, Id, Id1),
  352.     $mod_identify_pairs(Funs, Funs, Fpairs, Id, Id1),
  353.     $gennum(Tag),
  354.     $mod_replace_mappings([Id ---> Tag, Id1 ---> Tag], Substrs, Substrs1),
  355.     $mod_identify_items(Ppairs, Preds, Preds1),
  356.     $mod_identify_items(Fpairs, Funs, Funs1), !.
  357.  
  358. $mod_identify(Id, Id1, _, _) :-
  359.     $mod_error(['** Consult Error : Sharing violation with',Id and Id1]).
  360.  
  361. % $mod_identify_pairs(Preds, Other, Result, Id, Id1)
  362. %
  363.  
  364. $mod_identify_pairs([], _, [], _, _).
  365. $mod_identify_pairs([Pred/Arity ---> _|Rest], Other, 
  366.                 [[Pred/Arity, Pred1/Arity]|Result], Id, Id1) :-
  367.     $mod_prefix_path(Id, Pred, Extra), !,
  368.     $member(Pred1/Arity ---> _, Other),
  369.     $mod_prefix_path(Id1, Pred1, Extra), !,
  370.     $mod_identify_pairs(Rest, Other, Result, Id, Id1).
  371. $mod_identify_pairs([_|Rest], Other, Result, Id, Id1) :- !,
  372.     $mod_identify_pairs(Rest, Other, Result, Id, Id1).
  373.  
  374. % $mod_identify_items(Pairs, Items, Result)
  375. %
  376.  
  377. $mod_identify_items([], Items, Items).
  378. $mod_identify_items([[Item1, Item2]|Rest], Items, Result) :-
  379.     $mod_identify_items(Rest, Items, Part),
  380.     $gennum(Tag),
  381.     $mod_replace_mappings([Item1 ---> Tag, Item2 ---> Tag], Part, Result).
  382.  
  383. % $mod_prefix_path(Id, Path, Extra)
  384. %
  385.  
  386. $mod_prefix_path(X:Id, X:Path, Extra) :-
  387.     $mod_prefix_path(Id, Path, Extra).
  388. $mod_prefix_path(X, X:Path, Path) :-
  389.     $atom(X).
  390.  
  391. %----------------------------------------------------------------------------
  392.  
  393. % $mod_substructure(Id, Sig, Str)
  394. % Extracting a substructure from a signature/structure.
  395.  
  396. $mod_substructure(Id, sig(Substrs, Preds, Funs),
  397.               str(Tag, sig(Substrs1, Preds1, Funs1))) :-
  398.     $memberchk(Id ---> Tag, Substrs),
  399.     $bagof(N/A ---> Tag0, $member(Id:N/A ---> Tag0, Substrs), Substrs1),
  400.     $bagof(N/A ---> Tag0, $member(Id:N/A ---> Tag0, Preds), Preds1),
  401.     $bagof(N/A ---> Tag0, $member(Id:N/A ---> Tag0, Funs), Funs1).
  402.  
  403. %----------------------------------------------------------------------------
  404.  
  405. % $mod_addsubstrs(List, Sig, Sig1)
  406. % Adding new substructures to a structure/signature.
  407.  
  408. $mod_addsubstrs(List, Sig, Sig1) :-
  409.     $mod_add_tags(List, New),
  410.     $mod_addsubstrs0(New, Sig, Sig1).
  411.  
  412. %  $mod_addsubstrs0(List, Sig1, Sig2)
  413. %
  414.  
  415. $mod_addsubstrs0(List, sig(Substrs, Preds, Funs), 
  416.              sig(Substrs2, Preds2, Funs2)) :-
  417.     $mod_addsubstrs1(List, sig(Substrs1, Preds1, Funs1)),
  418.         $mod_addsubstrs_union(Substrs, Substrs1, Substrs2),
  419.         $mod_addsubstrs_union(Preds, Preds1, Preds2),
  420.         $mod_addsubstrs_union(Funs, Funs1, Funs2).
  421.  
  422. % $mod_addsubstrs1(Substrs, Sig)
  423. %
  424.  
  425. $mod_addsubstrs1([], sig([],[],[])).
  426. $mod_addsubstrs1([[Atid1, str(Tag1, sig(Substrs1, Preds1, Funs1))]|Rest],
  427.                  sig([Atid1 ---> Tag1|Nsubstrs], Npreds, Nfuns)) :-
  428.     $bagof(Atid1:Id ---> Tag, $member(Id ---> Tag, Substrs1), Substrs2),
  429.     $bagof((Atid1:Id)/Arity ---> Tag, $member(Id/Arity ---> Tag, Preds1),
  430.                 Preds2),
  431.     $bagof((Atid1:Id)/Arity ---> Tag, $member(Id/Arity ---> Tag, Funs1),
  432.                 Funs2),
  433.     $mod_addsubstrs1(Rest, sig(Substrs3, Preds3, Funs3)),
  434.     $mod_addsubstrs_union(Substrs2, Substrs3, Nsubstrs),
  435.     $mod_addsubstrs_union(Preds2, Preds3, Npreds),
  436.     $mod_addsubstrs_union(Funs2, Funs3, Nfuns), !.
  437.  
  438. % $mod_add_tags(List, Taggedlist)
  439. %
  440.  
  441. $mod_add_tags([],[]).
  442. $mod_add_tags([[Atid,Sig]|Rest],[[Atid,str(Tag,Sig)]|New]) :-
  443.     $gennum(Tag),
  444.     $mod_add_tags(Rest,New).
  445.  
  446. % $mod_addsubstrs_union(X, Y, Union)
  447. %
  448.  
  449. $mod_addsubstrs_union([], X, X).
  450. $mod_addsubstrs_union([Name ---> Map|Tail], List, Nlist) :-
  451.     $memberchk(Name ---> _, List), !,
  452.     $mod_addsubstrs_union(Tail, List, Nlist).
  453. $mod_addsubstrs_union([Map|Tail], List, [Map|Nlist]) :-
  454.     $mod_addsubstrs_union(Tail, List, Nlist).
  455.  
  456. %****************************************************************************
  457.  
  458. % sigb ::= atid = sigexpr
  459. % $mod_sigb(Sigexpr, Q, Atid, Result, Opts ,Addcode)
  460.  
  461. $mod_sigb(Atid = Sigexpr, Q, Atid, Result, Opts ,Addcode) :- !,
  462.     $mod_valid_name(Atid, signature),
  463.     $mod_sigexpr(Sigexpr, Q, sig(Substrs, Preds, Funs)),
  464.     $gennum(Tag),
  465.     ( Addcode == yes ->
  466.           ( Result = Tag,
  467.             $mod_assert_signature(Atid, Tag, Substrs, Preds, Funs) ) ;
  468.           Result = sig(Substrs, Preds, Funs) ),
  469.     $mod_print_signature(Opts, Atid, sig(Substrs, Preds, Funs)).
  470.  
  471. $mod_sigb(Sigb, _, _, _, _, _) :-
  472.     $mod_error(['** Consult Error : Bad signature binding',Sigb]).
  473.  
  474. % $mod_print_signature(Opts, Atid, Sig)
  475. %
  476.  
  477. $mod_print_signature(Opts, Atid, sig(Substrs, Preds, Funs)) :-
  478.     $memberchk(v, Opts) ->
  479.       ( $writename('signature '),
  480.         $write(Atid), $nl,
  481.         $writename('  sig'), $nl,
  482.         $mod_write_lists('    structure ', Substrs),
  483.         $mod_write_lists('    pred ', Preds),
  484.         $mod_write_lists('    fun ', Funs),
  485.         $writename('  end.'), $nl ) ;
  486.       true.
  487.  
  488. % $mod_write_lists(Text, List)
  489. %
  490.  
  491. $mod_write_lists(_, []).
  492. $mod_write_lists(Text, [Head ---> _|Tail]) :-
  493.     $writename(Text),
  494.     $write(Head), $nl,
  495.     $mod_write_lists(Text, Tail).
  496.  
  497. %----------------------------------------------------------------------------
  498.  
  499. % funb ::= atid(plist) = strexpr
  500. % $mod_funb(Funb, P, Q, R, Atid, Fun, Opts ,Addcode
  501.  
  502. :- mode $mod_funb(+,++,++,+,-,-,+,++).
  503.  
  504. $mod_funb(Name/Sig = Body/Sig0, P, Q, R, Atid, Fun, Opts ,Addcode) :- !,
  505.     (Sig = Sig0 ->
  506.           $mod_funb(Name = Body/Sig, P, Q, R, Atid, Fun, Opts ,Addcode) ;
  507.           ($mod_warning(['** Consult Warning : Functor', Name, 
  508.                           ': signature mismatch -', Sig0 and Sig,
  509.                   '- using', Sig]),
  510.            $mod_funb(Name = Body/Sig, P, Q, R, Atid, Fun, Opts ,Addcode))).
  511.  
  512. $mod_funb(Name/Sig = Body, P, Q, R, Atid, Fun, Opts ,Addcode) :- !,
  513.         (var(Sig) ->
  514.               ( $mod_warning(['** Consult Warning : Functor',Name,
  515.                               ': signature is a variable - ignoring']),
  516.         $mod_funb(Name = Body, P, Q, R, Atid, Fun, Opts ,Addcode) ) ;
  517.           $mod_funb(Name = Body/Sig, P, Q, R, Atid, Fun, Opts ,Addcode)).
  518.  
  519. $mod_funb(Name = Body/Sig, P, Q, R, Atid, Fun, Opts ,Addcode) :-
  520.         var(Sig), !,
  521.         $mod_warning(['** Consult Warning : Functor',Name,
  522.                       ': signature is a variable - ignoring']),
  523.     $mod_funb(Name = Body, P, Q, R, Atid, Fun, Opts ,Addcode).
  524.  
  525. $mod_funb(Head = Strexpr, P, Q, R, Atid, Result, Opts ,Addcode) :-
  526.         $structure(Head), !,
  527.     Head \= [_|_],
  528.     $univ(Head, [Atid|Plist]),
  529.     $mod_valid_name(Atid, functor), !,
  530.     $mod_plist(Plist, Q, Atids, sig(Substrs, Preds, Funs)),
  531.     $mod_replace_mappings(Atids, sig(Substrs, Preds, Funs), P, P1),
  532.     $mod_remove(v, Opts, Nopts),
  533.     $mod_strexpr(Strexpr, P1, Q, R, _, _, Nopts, no, [], _),
  534.     $gennum(Tag),
  535.     ( Addcode == yes ->
  536.            ( Result = Tag,
  537.              $mod_assert_functor(Atid, Tag, Atids, Substrs, Preds, Funs,
  538.                                  Strexpr, P, Q, R) ) ;
  539.            Result = fun(Atids, Substrs, Preds, Funs, Strexpr, P, Q, R) ),
  540.     ( $memberchk(v,Opts) -> ( $writename('functor '),
  541.                               $write(Head), $nl ) ;
  542.                     true ).
  543.  
  544. $mod_funb(Funb, _, _, _, _, _, _, _) :-
  545.     $mod_error(['** Consult Error : Bad functor binding', Funb]).
  546.  
  547. %----------------------------------------------------------------------------
  548.  
  549. % plist ::= atid1/sigexpr1, ... , atidn/sigexprn
  550. %                [sharing patheq1 and ... and patheqn]   n => 0, m => 1.
  551. % $mod_plist(Paramlist, Q, Params, Sig)
  552.  
  553. $mod_plist(Paramlist, Q, Params, Sig) :-
  554.      $mod_pre_process_plist(Paramlist, Args, Sharing),
  555.     $mod_convert_to_ands0(Args, NewParams), !,
  556.     $mod_spec(structure(NewParams), sig([], [], []), Q, Sig0),
  557.         ( Sharing = [] -> Sig = Sig0 ;
  558.                           $mod_spec(sharing(Sharing), Sig0, Q, Sig) ),
  559.     $bagof(Param, Param^Dummy^$member(Param/Dummy, Args), Params), !.
  560.  
  561. % $mod_pre_process_plist(Paramlist, Args, Sharing)
  562. %
  563.  
  564. $mod_pre_process_plist([sharing(Arg, Sharing)], [Arg], Sharing).
  565. $mod_pre_process_plist([], [], []).
  566. $mod_pre_process_plist([Head|Tail], [Head|Args], Sharing) :-
  567.     $mod_pre_process_plist(Tail, Args, Sharing).
  568.  
  569. % $mod_convert_to_ands0(List, Anded)
  570. %
  571.  
  572. $mod_convert_to_ands0([Arg], Arg).
  573. $mod_convert_to_ands0([Arg|Rest], Arg and New) :-
  574.     $mod_convert_to_ands0(Rest, New).
  575.  
  576. %----------------------------------------------------------------------------
  577.  
  578. % patheq ::= id1 = ... = idn                                   n => 1
  579. % $mod_patheq(Patheq, Sig, Result)
  580.  
  581. $mod_patheq(Id1 = Idi, sig(Substrs, _, _), Result) :- !,
  582.     (var(Id1) -> 
  583.         $mod_error(['** Consult Error : Variable in sharing constraint',
  584.                     Id1 = Idi]) ;
  585.         ( $memberchk(Id1 ---> _, Substrs) ->
  586.              $mod_patheq2(Id1, Idi, Substrs, Result) ;
  587.              ( $mod_error([
  588.            '** Consult Error : Unknown substructure in sharing constraint',
  589.                                    Patheq]) ) )).
  590.  
  591. $mod_patheq(Path, _, _) :-
  592.     $mod_error(['** Consult Error : Bad path equation',Path]).
  593.  
  594. % $mod_patheq2(Id1, Idi, Substrs, Pairs) 
  595. %
  596.  
  597. $mod_patheq2(Id1, Idi = Idj, Substrs, [[Id1, Idi]|Rest]) :- !,
  598.     ( var(Idi) -> $mod_error([
  599.                            '** Consult Error : Variable in sharing constraint',
  600.                        Id1 = Idi]) ;
  601.                 ( $memberchk(Idi ---> _, Substrs),
  602.               $mod_patheq2(Id1, Idj, Substrs, Rest) ) ).
  603. $mod_patheq2(Id1, Idi, Substrs, [[Id1, Idi]]) :-
  604.     var(Idi) -> $mod_error([
  605.                         '** Consult Error : Variable in sharing constraint']) ;
  606.                 $memberchk(Idi ---> _, Substrs).
  607.  
  608. %----------------------------------------------------------------------------
  609.  
  610. % strb ::= atid = strexpr
  611. % $mod_strb(Strb, Sig1, P, Q, R, Atid, Str, Code, Opts, Addcode,
  612. %           Funmap1, Funmap2)
  613.  
  614. :- mode $mod_strb(+,+,++,++,+,-,-,-,+,++,++,-).
  615.  
  616. $mod_strb(Name/Sig = Body/Sig0, Sig1, P, Q, R, Atid, Str, Code, Opts, Addcode,
  617.       Funmap1, Funmap2) :- !,
  618.     (Sig = Sig0 ->
  619.             $mod_strb(Name = Body/Sig, Sig1, P, Q, R, Atid, Str, Code, 
  620.                       Opts, Addcode, Funmap1, Funmap2) ;
  621.         ( $mod_warning(['** Consult Warning : Structure', Name, 
  622.                         ': signature mismatch -',
  623.                                 Sig0 and Sig, '- will use', Sig]),
  624.           $mod_strb(Name = Body/Sig, Sig1, P, Q, R, Atid, Str, Code, 
  625.                     Opts, Addcode, Funmap1, Funmap2) ) ).
  626.  
  627. $mod_strb(Name/Sig0 = Body, Sig, P, Q, R, Atid, Str, Code, Opts ,Addcode, 
  628.       Funmap1, Funmap2) :- !,
  629.         (var(Sig0) ->
  630.                 ( $mod_warning(['** Consult Warning : Structure', Name,
  631.                         ': signature is a variable - ignoring']),
  632.           $mod_strb(Name = Body, Sig, P, Q, R, Atid, Str, Code, Opts, 
  633.                         Addcode, Funmap1, Funmap2) ) ;
  634.         $mod_strb(Name = Body/Sig0, Sig, P, Q, R, Atid, Str, Code, 
  635.                   Opts, Addcode, Funmap1, Funmap2)).
  636.  
  637. $mod_strb(Name = Body/Sig0, Sig, P, Q, R, Atid, Str, Code, Opts ,Addcode, 
  638.       Funmap1, Funmap2) :-
  639.         var(Sig0), !,
  640.         $mod_warning(['** Consult Warning : Structure', Name,
  641.                   ': signature is a variable - ignoring']),
  642.     $mod_strb(Name = Body, Sig, P, Q, R, Atid, Str, Code, Opts ,Addcode, 
  643.               Funmap1, Funmap2).
  644.  
  645. $mod_strb(Atid = Strexpr, Sig, P, Q, R, Atid, str(Tag, sig(Substrs, Preds,
  646.       Funs)), Code, Opts, Addcode, Funmap1, Funmap2) :- !,
  647.     $mod_valid_name(Atid, structure), 
  648.     $mod_remove(v, Opts, Nopts),
  649.     $mod_strexpr(Strexpr, P, Q, R, str(Tag, sig(Substrs, Preds, Funs)), 
  650.                  Code, Nopts, no, Funmap1, Funmap2),
  651.     ( Addcode == yes -> 
  652.              $mod_assert_structure(Atid, Tag, Substrs, Preds, Funs) ;
  653.          true ),
  654.     ( $memberchk(v,Opts) -> ( $writename('structure '),
  655.                               $write(Atid), $nl ) ;
  656.                     true ).
  657.  
  658. $mod_strb(Strb, _, _, _, _, _, _, _, _, _, _, _) :-
  659.     $mod_error(['** Consult Error : Bad structure binding', Strb]).
  660.  
  661. %----------------------------------------------------------------------------
  662.  
  663. % sigexpr ::= SIG dec END
  664. % $mod_sigexpr(Sigexpr, Q, Sig)
  665.  
  666. :- mode $mod_sigexpr(+,++,-).
  667.  
  668. $mod_sigexpr(X, _, _) :-
  669.     var(X), !,
  670.     $mod_error(['** Consult Error : Signature expression is a variable']).
  671.  
  672. $mod_sigexpr([A|B], Q, Sig) :- !,
  673.     $mod_spec([A|B], sig([], [], []), Q, Sig).
  674.  
  675. %----------------------------------------------------------------------------
  676.  
  677. % sigexpr ::= atid
  678.  
  679. $mod_sigexpr(Atid, Q, sig(Substrs, Preds, Funs)) :-    
  680.     $atom(Atid), !,
  681.     ($memberchk(Atid ---> sig(Substrs, Preds, Funs), Q) -> true ;
  682.      ( ( $memberchk(Atid ---> Tag, Q),
  683.        $module_signature(_, Tag, Substrs, Preds, Funs) ) -> true ;
  684.        $mod_error(['** Consult Error : Unknown signature', Atid]) )), !.
  685.  
  686. $mod_sigexpr(Sigexpr, _, _) :-
  687.     $mod_error(['** Error : Bad signature expression']).
  688.  
  689. %----------------------------------------------------------------------------
  690.  
  691. % spec ::= PRED atid/nat
  692. % $mod_spec(Spec, Sig, Q, Sig2)
  693.  
  694. :- mode $mod_spec(+,++,++,-).
  695.  
  696. $mod_spec(X, _, _, _) :-
  697.     var(X), !,
  698.     $mod_error(['** Consult Error : Specification is a variable']).
  699.  
  700. $mod_spec(pred Pred and Preds ,Sig, Q, Sig2) :- !,
  701.     $mod_spec(pred Pred ,Sig, Q, Sig1),
  702.     $mod_spec(pred Preds ,Sig1, Q, Sig2).
  703.  
  704. $mod_spec(pred Atid/Nat, _, _, _) :-
  705.     (var(Atid) ; var(Nat)), !,
  706.         $mod_error(['** Consult Error : Variable in predicate specification']).
  707.  
  708. $mod_spec(pred Atid/Nat, sig(Substrs, Preds, Funs), _,
  709.           sig(Substrs, [Atid/Nat ---> Tag|Preds], Funs)) :-
  710.     $atom(Atid),
  711.     integer(Nat),
  712.         not($mod_member_or_pervasive(Atid/Nat ---> _, Preds)), !,
  713.     ( ( $memberchk(Atid/Nat ---> _, Funs) ;
  714.         $pervasive_function(Atid/Nat) ) ->
  715.            $mod_error(['** Consult Error : Trying to redefine function',
  716.                        Atid/Nat, 'as a predicate in signature']) ;
  717.            $gennum(Tag) ).
  718.  
  719. $mod_spec(pred Atid/Nat, Sig, _, Sig) :-
  720.     $atom(Atid),
  721.     integer(Nat), !,
  722.         ( $pervasive_predicate(Atid/Nat) ->
  723.               $mod_warning([
  724.                 '** Consult Warning : Cannot redefine pervasive predicate',
  725.                     Atid/Nat, 'in signature']) ;
  726.               $mod_warning(['** Consult Warning : Predicate', Atid/Nat,
  727.                             'defined twice in signature']) ).
  728.  
  729. $mod_spec(pred Pred, _, _, _) :- !,
  730.     $mod_error(['** Consult Error : Bad predicate specification', 
  731.                 pred Pred]).
  732.  
  733. %----------------------------------------------------------------------------
  734.  
  735. % spec ::= FUN atid/nat
  736.  
  737. $mod_spec(fun Fun and Funs ,Sig, Q, Sig2) :- !,
  738.     $mod_spec(fun Fun ,Sig, Q, Sig1),
  739.     $mod_spec(fun Funs ,Sig1, Q, Sig2).
  740.  
  741. $mod_spec(fun Atid/Nat, _, _, _) :-
  742.     (var(Atid) ; var(Nat)), !,
  743.     $mod_error(['** Consult Error : Variable in function specification']).
  744.  
  745. $mod_spec(fun Atid/Nat, sig(Substrs, Preds, Funs), _,
  746.           sig(Substrs, Preds, [Atid/Nat ---> Tag|Funs])) :-
  747.     $atom(Atid),
  748.     integer(Nat),
  749.     not($mod_member_or_pervasive(Atid/Nat ---> _, Funs)), !,
  750.     ( ( $memberchk(Atid/Nat ---> _, Preds) ;
  751.         $pervasive_predicate(Atid/Nat) ) ->
  752.            $mod_error(['** Consult Error : Trying to redefine predicate',
  753.                        Atid/Nat, 'as a function in signature']) ;
  754.            $gennum(Tag) ).
  755.  
  756. $mod_spec(fun Atid/Nat, Sig, _, Sig) :-
  757.     $atom(Atid),
  758.     integer(Nat), !,
  759.         ( $pervasive_function(Atid/Nat) ->
  760.              $mod_warning([
  761.                 '** Consult Warning : Cannot redefine pervasive function', 
  762.                     Atid/Nat, 'in signature']) ;
  763.              $mod_warning(['** Consult Warning : Function', Atid/Nat,
  764.                            'defined twice in signature']) ).
  765.  
  766. $mod_spec(fun Fun, _, _, _) :- !,
  767.     $mod_error(['** Consult Error : Bad function specification', fun Fun]).
  768.  
  769. %----------------------------------------------------------------------------
  770.  
  771. % spec ::= sharing patheq1 and ... and patheqn           n => 1.
  772.  
  773. $mod_spec(sharing(Shared), Sig, _, Newsig) :- !,
  774.     $mod_patheq_all(Shared, Sig, Union),
  775.     $mod_identify(Union, Sig, Newsig).
  776.  
  777. % $mod_patheq_all(Patheq, Sig1, Result)
  778. %
  779.  
  780. $mod_patheq_all(Patheq and Rest, Sig1, Result) :- !,
  781.     $mod_patheq_all(Rest, Sig1, Bit),
  782.     $mod_patheq(Patheq, Sig1, Bit2),
  783.     $append(Bit, Bit2, Result).
  784. $mod_patheq_all(Patheq, Sig1, Result) :-
  785.     $mod_patheq(Patheq, Sig1, Result).
  786.  
  787. %----------------------------------------------------------------------------
  788.  
  789. % spec ::= structure specstrb1 and ... and specstrbn          n => 1.
  790.  
  791. $mod_spec(structure Specstrbs, sig(Substrs, Preds, Funs), Q, Newsig) :- !,
  792.     $mod_spec_all(Specstrbs, sig(Substrs, Preds, Funs), Q, Results),
  793.     ( $mod_check_spec(Results) -> 
  794.         $mod_addsubstrs(Results, sig(Substrs, Preds, Funs), Newsig) ;
  795.         $mod_error(['** Consult Error : Bad structure spec', Specstrbs,
  796.           ': Either duplicate structure names or structure already defined'])),
  797.     !.
  798.  
  799. % $mod_spec_all(Specstrb, Sig, Q, Result)
  800. %
  801.  
  802. $mod_spec_all(Specstrb and Rest, Sig, Q, [[Atid, Sig1]|Tail]) :- !,
  803.     $mod_specstrb(Specstrb, Sig, Q, Atid, Sig1),
  804.     $mod_spec_all(Rest, Sig, Q, Tail).
  805. $mod_spec_all(Specstrb, Sig, Q, [[Atid, Sig1]]) :-
  806.     $mod_specstrb(Specstrb, Sig, Q, Atid, Sig1).
  807.  
  808. % $mod_check_spec(List)
  809. %
  810.  
  811. $mod_check_spec([]).
  812. $mod_check_spec([[Atid, _]|After]) :-
  813.     $not_memberchk([Atid, _], After),
  814.     $mod_check_spec(After).
  815.  
  816. %----------------------------------------------------------------------------
  817.  
  818. % spec ::= spec spec'
  819.  
  820. $mod_spec([Spec|Rest], Sig, Q, Result) :- !,
  821.     $mod_spec(Spec, Sig, Q, Sig1),
  822.     $mod_spec(Rest, Sig1, Q, Result).
  823.  
  824. $mod_spec([], Sig, Q, Sig) :- !.
  825.  
  826. %----------------------------------------------------------------------------
  827.  
  828. $mod_spec(Spec, _, _, _) :-
  829.     $mod_error(['** Consult Error : Bad specification',Spec]).
  830.  
  831. %----------------------------------------------------------------------------
  832.  
  833. % specstrb ::= atid/sigexpr
  834. % $mod_specstrb(Specstrb, Sig, Q, Atid, Newsig)
  835.  
  836. :- mode $mod_specstrb(+,++,++,-,-).
  837.  
  838. $mod_specstrb(Atid/Sigexpr, _, _, _, _) :-
  839.     (var(Atid) ; var(Sigexpr)), !,
  840.     $mod_error(['** Consult Error : Variable in structure specification']).
  841.  
  842. $mod_specstrb(Atid/Sigexpr, sig(Substr, _, _), Q, Atid, Newsig) :- !,
  843.     ( $memberchk(Atid ---> _, Substr) ->
  844.               $mod_error(['** Consult Error : Structure', Atid,
  845.                       'already defined in signature']) ;
  846.           ( $mod_sigexpr(Sigexpr, Q, Sig),
  847.             $mod_tag(Sig, Newsig) ) ).
  848.  
  849. $mod_specstrb(Specstrb, _, _, _, _) :-
  850.     $mod_error(['** Consult Error : Bad structure specification',
  851.                 Specstrb]).
  852.  
  853. %----------------------------------------------------------------------------
  854.  
  855. % strexpr ::= STRUCT dec END
  856. % $mod_strexpr(Strexpr, P, Q, R, Str, Code, Opts ,Addcode, Funmap1, Funmap2)
  857.  
  858. :- mode $mod_strexpr(+,++,++,+,-,-,+,+,+,-).
  859.  
  860. $mod_strexpr(X, _, _, _, _, _, _, _, _, _) :-
  861.     var(X), !,
  862.     $mod_error(['** Consult Error : Structure expression is a variable']).
  863.  
  864. $mod_strexpr([A|B], P, Q, R, str(Tag, sig(Substrs, Preds, Funs)), Code1, 
  865.          Opts ,Addcode, Funmap1, Funmap2) :- !,
  866.     $gennum(Tag),
  867.     $mod_dec([A|B], sig([],[],[]), P, Q, R, sig(Substrs, Preds, Funs),
  868.              P1, Q1, R1, Code, Opts, Tag ,Addcode, Funmap1, Funmap2),
  869.     $mod_translate_code(Code, Preds, Funs, Code1, Tag).
  870.  
  871. % After a clause has been translated into its new internal form, the
  872. % clause is passed around as '_$done'(Clause) which forms a mark
  873. % to ensure that a second translation is not attempted later.
  874. % '_$blank'(Name/Arity) is used to specify predicates which should be
  875. % created but are empty (they have been declared using a 'pred Name/Arity'
  876. % declaration.
  877.  
  878. % $mod_translate_code(Code, Preds, Funs, Translated, Opts)
  879. %
  880.  
  881. :- mode $mod_translate_code(+,++,++,-,++).
  882.  
  883. $mod_translate_code([], _, _, [],_).
  884. $mod_translate_code(['_$blank'(Clause)|Rest], Preds, Funs,
  885.                 ['_$blank'(Clause)|Tail], Tag) :- !,
  886.     $mod_translate_code(Rest, Preds, Funs, Tail, Tag).
  887. $mod_translate_code(['_$done'(Clause)|Rest], Preds, Funs,
  888.                 ['_$done'(Clause)|Tail], Tag) :- !,
  889.     $mod_translate_code(Rest, Preds, Funs, Tail, Tag).
  890. $mod_translate_code([Clause|Rest], Preds, Funs,
  891.                 ['_$done'(NClause1)|Tail], Tag) :-
  892.     ( $mod_translate(Clause, Preds, Funs, NClause, Tag),
  893.       $fun_rel(NClause, NClause1, Tag) ) ->
  894.            $mod_translate_code(Rest, Preds, Funs, Tail, Tag) ;
  895.            $mod_error(['** Consult Error : Cannot translate', Clause]).
  896.  
  897. %----------------------------------------------------------------------------
  898.  
  899. % strexpr ::= strexpr/sigexpr
  900.  
  901. $mod_strexpr(Strexpr/Sigexpr, P, Q, R, Str, Code, _, Addcode, 
  902.          Funmap1, Funmap2) :- !,
  903.     $mod_strexpr(Strexpr, P, Q, R, Str1, Code, [], Addcode, 
  904.                  Funmap1, Funmap2),
  905.     $mod_sigexpr(Sigexpr, Q, Sig),
  906.         ( Strexpr = [_|_] -> Namestr = ('structure') ; Namestr = Strexpr ),
  907.         ( Sigexpr = [_|_] -> Namesig = 'its signature' ; Namesig = Sigexpr ),
  908.     $mod_fit(Str1, Sig, Str, Namestr, Namesig).
  909.  
  910. %----------------------------------------------------------------------------
  911.  
  912. % strexpr ::= id
  913.  
  914. $mod_strexpr(Atid, P, Q, R, str(Tag, sig(Substrs, Preds, Funs)), [], _, _, 
  915.          Funmap, Funmap) :-
  916.     $atom(Atid), !,
  917.     ( $memberchk(Atid ---> str(Tag, sig(Substrs, Preds, Funs)), P) -> true;
  918.      (( $memberchk(Atid ---> Tag, P),
  919.         $module_structure(_, Tag, Substrs, Preds, Funs) ) -> true ;
  920.       $mod_error(['** Consult Error : Structure', Atid, 'is unknown']))),!.
  921.  
  922. %----------------------------------------------------------------------------
  923.  
  924. % strexpr ::= atid:id
  925.  
  926. $mod_strexpr(Atid:Id, P, Q, R, Str, [], _, _, Funmap, Funmap) :-
  927.     $atom(Atid), !,
  928.     nonvar(Id), 
  929.     ( ( $memberchk(Atid ---> str(Tag, sig(Substrs, Preds, Funs)), P) ;
  930.         ( $memberchk(Atid ---> Tag, P),
  931.           $module_structure(_, Tag, Substrs, Preds, Funs) ) ) ->
  932.              $mod_substructure(Id, sig(Substrs, Preds, Funs), Str) ;
  933.          $mod_error(['** Consult Error : Structure', Atid,
  934.                      'is unknown']) ).
  935.  
  936. %----------------------------------------------------------------------------
  937.  
  938. % strexpr ::= atid(strexpr1,...,strexprn)              n => 0.
  939.  
  940. $mod_strexpr(Compound, P, Q, R, Str1, Code, _, _, Funmap1, Funmap3) :-
  941.     $structure(Compound), !,
  942.     $univ(Compound, [Atid|Strexprs]),
  943.     ( $memberchk(Atid ---> fun(Atids, sig(Substrs, Preds, Funs), Strexpr, 
  944.                                P1, Q1, R1), R) -> true ;
  945.       ( ( $memberchk(Atid ---> Tag, R),
  946.               $module_functor(_, Tag, Atids, Substrs, Preds, Funs, 
  947.                           Strexpr, P1, Q1, R1) ) -> true ;
  948.         $mod_error(['** Consult Error : Functor', Atid, 'is unknown']))),
  949.     $length(Strexprs, Len1),
  950.      $length(Atids, Len2),
  951.     ( Len1 == Len2 -> true ;
  952.       $mod_error(['** Consult Error :', Len1, 'arguments to functor', Atid,
  953.                       'instead of', Len2]) ),
  954.     $mod_apply_strexpr(Strexprs, Atids, P, Q, R, Pairs, Code1, [], 
  955.                        Funmap1, Funmap2),
  956.     $mod_addsubstrs0(Pairs, sig([], [], []), Result),
  957.     $mod_fit(Result, sig(Substrs, Preds, Funs), str(_, Strsig), 
  958.              Compound, 'its signature'),
  959.     $mod_replace_mappings(Atids, Strsig, P1, P2),
  960.     $mod_strexpr(Strexpr, P2, Q1, R1, Str1, Code2, [], no, 
  961.                  Funmap2, Funmap3),
  962.     $append(Code1, Code2, Code).
  963.  
  964. $mod_strexpr(Strexpr, _, _, _, _, _, _, _, _, _) :-
  965.     $mod_error(['** Consult Error : Bad structure expression', Strexpr]).
  966.  
  967. % $mod_apply_strexpr(Strexprs, Atids, P, Q, R, Pairs, Code, Opts,
  968. %                    Funmap1, Funmap3) 
  969. %
  970.  
  971. $mod_apply_strexpr([], [], _, _, _, [], [], _, Funmap, Funmap).
  972. $mod_apply_strexpr([Strexpr|Strexprs], [Atid|Atids], P, Q, R, 
  973.                    [[Atid, Str]|Rest], Code, Opts, Funmap1, Funmap3) :-
  974.     $mod_strexpr(Strexpr, P, Q, R, Str, Code1, Opts, no, Funmap1, Funmap2),
  975.     $mod_apply_strexpr(Strexprs, Atids, P, Q, R, Rest, Code2, Opts, 
  976.                        Funmap2, Funmap3),
  977.     $append(Code1, Code2, Code).
  978.  
  979. %----------------------------------------------------------------------------
  980.  
  981. % $mod_dec(Dec, Sig, P, Q, R, Sig1, P1, Q1, R1, Code, Opts)
  982. %
  983.  
  984. :- mode $mod_dec(+,++,++,++,+,-,-,-,-,-,+,++,++,++,-).
  985.  
  986. $mod_dec(X, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :-
  987.     var(X), !,
  988.     $mod_error(['** Consult Error : Declaration is a variable']).
  989.  
  990. $mod_dec(inherit Atid ,Sig, P, Q, R, Sig1, P1, Q1, R1, Code, Opts, Strtag, 
  991.      Addcode, Funmap1, Funmap2) :- !,
  992.     $mod_dec(structure Atid = Atid ,Sig, P, Q, R, Sig1, P1, Q1, R1, Code, 
  993.              Opts, Strtag, Addcode, Funmap1, Funmap2).
  994.  
  995. $mod_dec(fun Fun and Funs ,Sig, P, Q, R, Sig2, P2, Q2, R2, [], Opts, Strtag, 
  996.      Addcode, Funmap1, Funmap3) :- !,
  997.     $mod_dec(fun Fun ,Sig, P, Q, R, Sig1, P1, Q1, R1, _, Opts, Strtag, 
  998.              Addcode, Funmap1, Funmap2),
  999.     $mod_dec(fun Funs ,Sig1, P1, Q1, R1, Sig2, P2, Q2, R2, _, Opts,
  1000.              Strtag, Addcode, Funmap2, Funmap3).
  1001.  
  1002. $mod_dec(pred Pred and Preds ,Sig, P, Q, R, Sig2, P2, Q2, R2, Code3, Opts, 
  1003.          Strtag, Addcode, Funmap1, Funmap3) :- !,
  1004.     $mod_dec(pred Pred ,Sig, P, Q, R, Sig1, P1, Q1, R1, Code1, Opts, 
  1005.              Strtag, Addcode, Funmap1, Funmap2),
  1006.     $mod_dec(pred Preds ,Sig1, P1, Q1, R1, Sig2, P2, Q2, R2, Code2, Opts, 
  1007.              Strtag, Addcode, Funmap2, Funmap3),
  1008.     $append(Code1, Code2, Code3).
  1009.  
  1010. %----------------------------------------------------------------------------
  1011.  
  1012. % dec ::= PRED atid/nat.
  1013.  
  1014. $mod_dec(pred Atid/Nat, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :-
  1015.     (var(Atid) ; var(Nat)), !,
  1016.     $mod_error(['** Consult Error : Variable in predicate declaration']).
  1017.  
  1018. $mod_dec(pred Atid/Nat, sig(Substrs, Preds, Funs), P, Q, R, 
  1019.          sig(Substrs, [Atid/Nat ---> Map|Preds], Funs), P, Q, R, 
  1020.      ['_$blank'(Map/Nat)], _, Strtag, _, Funmap, Funmap) :-
  1021.     $atom(Atid),
  1022.     integer(Nat),
  1023.     not($mod_member_or_pervasive(Atid/Nat ---> _, Preds)), !,
  1024.     ( ( $memberchk(Atid/Nat ---> _, Funs) ;
  1025.         $pervasive_function(Atid/Nat) ) ->
  1026.            $mod_error(['** Consult Error : Trying to redefine function',
  1027.                        Atid/Nat, 'as a predicate']) ;
  1028.            $dismantle_name(Map, Atid, Strtag) ).
  1029.  
  1030. $mod_dec(pred Atid/Nat, Sig, P, Q, R, Sig, P, Q, R, [], _, _, _, 
  1031.      Funmap, Funmap) :-
  1032.     $atom(Atid),
  1033.     integer(Nat), !,
  1034.         ( $pervasive_predicate(Atid/Nat) ->
  1035.               $mod_warning([
  1036.                 '** Consult Warning : Cannot redefine pervasive predicate',
  1037.                     Atid/Nat, 'in structure']) ;
  1038.           $mod_warning(['** Consult Warning : Predicate', Atid/Nat,
  1039.                             'defined twice in structure']) ).
  1040.  
  1041. $mod_dec(pred Pred, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :- !,
  1042.     $mod_error(['** Consult Error : Bad predicate declaration',pred Pred]).
  1043.  
  1044. %----------------------------------------------------------------------------
  1045.  
  1046. % dec ::= FUN atid/nat = id
  1047.  
  1048. $mod_dec(fun Atid/Nat = Id, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :-
  1049.     (var(Atid) ; var(Nat) ; var(Id)), !,
  1050.     $mod_error(['** Consult Error : Variable in function declaration']).
  1051.  
  1052. $mod_dec(fun Atid/Nat = Id, sig(Substrs, Preds, Funs), P, Q, R, 
  1053.          sig(Substrs, Preds, [Atid/Nat ---> Map|Funs]),  P, Q, R, [], _,
  1054.          Strtag, Addcode, Funmap, 
  1055.      [$mapped_function(Name, Nat, Map, Strtag),
  1056.       $declared_function(Compound)|Funmap]) :-
  1057.     $atom(Atid),
  1058.     integer(Nat),
  1059.     not($mod_member_or_pervasive(Atid/Nat ---> _, Funs)), !,
  1060.     ( ( $memberchk(Atid/Nat ---> _, Preds) ;
  1061.         $pervasive_predicate(Atid/Nat) ) ->
  1062.            $mod_error(['** Consult Error : Trying to redefine predicate',
  1063.                        Atid/Nat, 'as a function']) ;
  1064.            ( $mod_fun(Id, Nat, sig(Substrs, Preds, Funs), P, Map),
  1065.          $dismantle_name(Name, Atid, Strtag),
  1066.          $bldstr(Name, Nat, Compound) ) ).
  1067.  
  1068. $mod_dec(fun Atid/Nat = _, Sig, P, Q, R, Sig, P, Q, R, [], _, _, _, 
  1069.      Funmap, Funmap) :-
  1070.     $atom(Atid),
  1071.     integer(Nat), !,
  1072.     ( $pervasive_function(Atid/Nat) ->
  1073.              $mod_warning([
  1074.                 '** Consult Warning : Cannot redefine pervasive function',
  1075.                     Atid/Nat, 'in structure']) ;
  1076.          $mod_warning(['** Consult Warning : Function', Atid/Nat,
  1077.                             'defined twice in structure']) ).
  1078.  
  1079. %----------------------------------------------------------------------------
  1080.  
  1081. $mod_dec(fun Atid = Id, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :-
  1082.     var(Atid), !,
  1083.     $mod_error(['** Consult Error : Variable in function declaration']).
  1084.  
  1085. $mod_dec(fun Atid = Id, sig(Substrs, Preds, Funs), P, Q, R, Sig, NewP, 
  1086.          NewQ, NewR, Code, Opts, Strtag, Addcode, Funmap1, Funmap2) :-
  1087.     $atom(Atid), !,
  1088.     ( Id = Structure:Name ->
  1089.         $mod_strexpr(Structure, P, Q, R, str(_, sig(_, _, Otherfuns)), _,
  1090.                          _, _, _, _) ;
  1091.         ( Otherfuns = Funs, Name = Id ) ),
  1092.     $mod_member_chop(Name/Nat ---> _, Otherfuns, Rest), !,
  1093.     ( $memberchk(Name/_ ---> _, Rest) -> 
  1094.                 $mod_error(['** Consult Error : Ambiguous function', Id,
  1095.                             'when defining function', Atid]) ;
  1096.             $mod_dec(fun Atid/Nat = Id, sig(Substrs, Preds, Funs), P, Q,R,
  1097.                          Sig, NewP, NewQ, NewR, Code, Opts, Strtag, Addcode, 
  1098.              Funmap1, Funmap2) ).
  1099.  
  1100. $mod_dec(fun Atid = Id, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :- !,
  1101.     $mod_error(['** Consult Error : Bad function declaration',
  1102.                  fun Atid = Id]).
  1103.  
  1104. %----------------------------------------------------------------------------
  1105.  
  1106. % dec ::= FUN atid/nat
  1107.  
  1108. $mod_dec(fun Atid/Nat, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :-
  1109.     (var(Atid) ; var(Nat) ), !,
  1110.     $mod_error(['** Consult Error : Variable in function declaration']).
  1111.  
  1112. $mod_dec(fun Atid/Nat, sig(Substrs, Preds, Funs), P, Q, R, 
  1113.          sig(Substrs, Preds, [Atid/Nat ---> Map|Funs]), P, Q, R, [], _,
  1114.      Strtag, Addcode, Funmap, [$declared_function(Compound)|Funmap]) :-
  1115.     $atom(Atid),
  1116.     integer(Nat),
  1117.     not($mod_member_or_pervasive(Atid/Nat ---> _, Funs)), !,
  1118.     ( ( $memberchk(Atid/Nat ---> _, Preds) ;
  1119.         $pervasive_predicate(Atid/Nat) ) ->
  1120.            $mod_error(['** Consult Error : Trying to redefine predicate',
  1121.                        Atid/Nat, 'as a function']) ;
  1122.            ( $dismantle_name(Map,Atid,Strtag),
  1123.              $bldstr(Map,Nat,Compound) ) ).
  1124.  
  1125. $mod_dec(fun Atid/Nat, Sig, P, Q, R, Sig, P, Q, R, [], _, _, _, Funmap, 
  1126.      Funmap) :-
  1127.     $atom(Atid),
  1128.     integer(Nat), !,
  1129.         ( $pervasive_function(Atid/Nat) ->
  1130.               $mod_warning([
  1131.                  '** Consult Warning : Cannot redefine pervasive function',
  1132.                  Atid/Nat, 'in structure']) ;
  1133.           $mod_warning(['** Consult Warning : Function', Atid/Nat,
  1134.                             'defined twice in structure']) ).
  1135.  
  1136. $mod_dec(fun Fun, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :- !,
  1137.     $mod_error(['** Consult Error : Bad function declaration', fun Fun]).
  1138.  
  1139. %----------------------------------------------------------------------------
  1140.  
  1141. % dec ::= STRUCTURE strb
  1142.  
  1143. $mod_dec(structure Strb, Sig, P, Q, R, Sig1, [Atid ---> Result|P], Q, R, Code, 
  1144.      Opts, _, Addcode, Funmap1, Funmap2) :- !,
  1145.     $mod_strb(Strb, Sig, P, Q, R, Atid, str(Tag, Strsig), Code, Opts, 
  1146.               Addcode, Funmap1, Funmap2),
  1147.     $mod_addsubstrs0([[Atid, str(Tag, Strsig)]], Sig, Sig1),
  1148.     ( Addcode == yes -> Result = Tag ;
  1149.                         Result = str(Tag, Strsig) ).
  1150.  
  1151. %----------------------------------------------------------------------------
  1152.  
  1153. % dec ::= SIGNATURE sigb
  1154.  
  1155. $mod_dec(signature Sigb, Sig, P, Q, R, Sig, P, [Atid ---> Sig1|Q], R, [], Opts,
  1156.          _, Addcode, Funmap, Funmap) :- !,
  1157.     $mod_sigb(Sigb, Q, Atid, Sig1, Opts, Addcode).
  1158.  
  1159. %----------------------------------------------------------------------------
  1160.  
  1161. % dec ::= FUNCTOR funb
  1162.  
  1163. $mod_dec(functor Funb, Sig, P, Q, R, Sig, P, Q, [Atid ---> Fun|R], [], Opts,
  1164.          _, Addcode, Funmap, Funmap) :- !,
  1165.     $mod_funb(Funb, P, Q, R, Atid, Fun, Opts, Addcode).
  1166.  
  1167. %----------------------------------------------------------------------------
  1168.  
  1169. % dec ::= dec dec'
  1170.  
  1171. $mod_dec([Dec|Restdec], Sig, P, Q, R, Sig2, P2, Q2, R2, Code3, Opts, Strtag, 
  1172.      Addcode, Funmap1, Funmap3) :- !,
  1173.       $mod_dec(Dec, Sig, P, Q, R, Sig1, P1, Q1, R1, Code1, Opts, Strtag, 
  1174.            Addcode, Funmap1, Funmap2),
  1175.       $mod_dec(Restdec, Sig1, P1, Q1, R1, Sig2, P2, Q2, R2, Code2, Opts, 
  1176.            Strtag, Addcode, Funmap2, Funmap3),
  1177.       $append(Code1, Code2, Code3).
  1178.  
  1179. $mod_dec([], Sig, P, Q, R, Sig, P, Q, R, [], _, _, _, Funmap, Funmap) :- !.
  1180.  
  1181. %----------------------------------------------------------------------------
  1182.  
  1183. /* Note the following pecularity with this implementation :
  1184.         structure test =
  1185.             struct
  1186.                 fun f/2.
  1187.                 fun g = f.
  1188.                 :- op(500,xfx,g).
  1189.             end.
  1190.    The infix operator status will be placed on f/2 and not g/2!!
  1191. */
  1192.  
  1193.  
  1194. $mod_dec(':-'(Call), sig(Substrs, Preds, Funs), P, Q, R, 
  1195.      sig(Substrs, Preds, Funs), P, Q, R, 
  1196.      [], _, Strtag, _, Funmap, Funmap) :- !,
  1197.     ( var(Call) -> 
  1198.          $mod_error(['** Consult Error : Argument to :-/1 is a variable']);
  1199.          true ),
  1200.     $mod_translate_body(Call, Preds, Funs, Ncall ,Strtag),
  1201.     $expand_body(Ncall, Final, Strtag),
  1202.     ( call(Final) ; true ), !.
  1203.  
  1204. %----------------------------------------------------------------------------
  1205.  
  1206. % dec ::= atid(term1,...,termn) [:- atom1,...,atomm]     (Prolog clause)
  1207.  
  1208. $mod_dec((X --> Y), Sig, P, Q, R, Sig1, P1, Q1, R1, Code, Opts, Strtag,
  1209.      Addcode, Funmap1, Funmap2) :- !,
  1210.     ( $dcg((X --> Y), Clause) ->
  1211.             $mod_dec(Clause, Sig, P, Q, R, Sig1, P1, Q1, R1, Code, Opts,
  1212.                  Strtag, Addcode, Funmap1, Funmap2) ;
  1213.         $mod_error(['** Consult Error : Bad DCG clause ', (X --> Y)])).
  1214.  
  1215. $mod_dec((Head :- Body), sig(Substrs, Preds, Funs), P, Q, R, 
  1216.          sig(Substrs, Preds, Funs), P, Q, R, [(Head :- Body)], _, _, _, 
  1217.      Funmap, Funmap) :-
  1218.     nonvar(Head),
  1219.     $functor(Head, Atid, N),
  1220.     $memberchk(Atid/N ---> _, Preds), !.
  1221.  
  1222. $mod_dec((Head :- Body), sig(Substrs, Preds, Funs), P, Q, R, 
  1223.          sig(Substrs, [Atid/N ---> Tag|Preds], Funs), P, Q, R, 
  1224.      [(Head :- Body)], _, Strtag, _, Funmap, Funmap) :- !,
  1225.     nonvar(Head),
  1226.     $functor(Head, Atid, N), 
  1227.     ( $pervasive_predicate(Atid/N) ->
  1228.            $mod_error([
  1229.              '** Consult Error : Trying to redefine pervasive predicate',
  1230.                            Atid/N]) ;
  1231.                true ),
  1232.     ( ( $memberchk(Atid/N ---> _, Funs) ;
  1233.         $pervasive_function(Atid/N) ) ->
  1234.            $mod_error(['** Consult Error : Trying to redefine function',
  1235.                        Atid/N, 'as a predicate']) ;
  1236.            $dismantle_name(Tag, Atid, Strtag) ).
  1237.  
  1238. $mod_dec(Head, sig(Substrs, Preds, Funs), P, Q, R, sig(Substrs, Preds, Funs), 
  1239.      P, Q, R, [Head], _, _, _, Funmap, Funmap) :-
  1240.     nonvar(Head),
  1241.     $functor(Head, Atid, N),
  1242.     $memberchk(Atid/N ---> _, Preds), !.
  1243.  
  1244. $mod_dec(Head, sig(Substrs, Preds, Funs), P, Q, R, 
  1245.          sig(Substrs, [Atid/N ---> Tag|Preds], Funs), P, Q, R, [Head], _, 
  1246.      Strtag, _, Funmap, Funmap) :- !,
  1247.     nonvar(Head),
  1248.     $functor(Head, Atid, N), 
  1249.     ( $pervasive_predicate(Atid/N) ->
  1250.            $mod_error([
  1251.              '** Consult Error : Trying to redefine pervasive predicate',
  1252.                            Atid/N]) ;
  1253.                true ),
  1254.     ( ( $memberchk(Atid/N ---> _, Funs) ;
  1255.         $pervasive_function(Atid/N) ) ->
  1256.            $mod_error(['** Consult Error : Trying to redefine function',
  1257.                        Atid/N, 'as a predicate']) ;
  1258.            $dismantle_name(Tag, Atid, Strtag) ).
  1259.  
  1260. %----------------------------------------------------------------------------
  1261.  
  1262. $mod_dec(Dec, _, _, _, _, _, _, _, _, _, _, _, _, _, _) :-
  1263.     $mod_error(['** Consult Error : Bad declaration', Dec,'in structure']).
  1264.  
  1265. %----------------------------------------------------------------------------
  1266.  
  1267. % $mod_fun(Atid, Nat, Sig, P, Map)
  1268. %
  1269.  
  1270. $mod_fun(Atid:Id, Nat, Sig, P, Map) :- !,
  1271.     ( ( var(Atid) ; var(Id) ) ->
  1272.           $mod_error(['** Consult Error : Variable in function name']);
  1273.           true ),
  1274.     $mod_strexpr(Atid, P, _, _, str(_, sig(_, _, Funs)), _,
  1275.                      _, _, _, _),
  1276.     ( $mod_member_or_pervasive(Id/Nat ---> Map, Funs) -> true ;
  1277.           $mod_error(['** Consult Error : Unknown function', Atid:Id/Nat])).
  1278.  
  1279. $mod_fun(Atid, Nat, sig(_, _, Funs), _, Map) :-
  1280.     $mod_member_or_pervasive(Atid/Nat ---> Map, Funs) -> true ;
  1281.         $mod_error(['** Consult Error : Function', Atid/Nat, 'is unknown']).
  1282.  
  1283. %****************************************************************************
  1284.  
  1285. % $mod_translate(Clause, Preds, Funs, NClause, Tag)
  1286. %
  1287.  
  1288. $mod_translate((Head :- Body), Preds, Funs, (Nhead :- Nbody), Strtag) :-
  1289.     !, $mod_translate(Head, Preds, Funs, Nhead, Strtag),
  1290.     $mod_translate_body(Body, Preds, Funs, Nbody, Strtag).
  1291.  
  1292. $mod_translate(Head, Preds, Funs, Nhead, Strtag) :-
  1293.     $univ(Head, [Name|Args]),
  1294.     $length(Args, Arity),
  1295.     ( Name == ':' ->
  1296.             $mod_error(['** Consult Error : Illegal predicate',Head]) ;
  1297.                 ( ( $memberchk(Name/Arity ---> Internal, Preds) ;
  1298.             ( $pervasive(Name/Arity),
  1299.               Internal = Name) ;
  1300.             $mod_translate_local(Name, Arity, Strtag, Internal) ) ->
  1301.             ( $mod_translate_args(Args, Preds, Funs, Nargs, 
  1302.                                   Strtag),
  1303.               $mod_univ(Nhead, [Internal|Nargs]) ) ;
  1304.             $mod_error(['** Consult Error : Unknown predicate',
  1305.                         Name/Arity, in, Head]) ) ).
  1306.  
  1307. % $mod_translate_body(Call, Preds, Funs, NCall)
  1308. %
  1309.  
  1310. :- mode $mod_translate_body(+,++,++,-,++).
  1311.  
  1312. $mod_translate_body((A, B), Preds, Funs, (NA, NB), Strtag) :- !,
  1313.     $mod_translate_body(A, Preds, Funs, NA, Strtag),
  1314.     $mod_translate_body(B, Preds, Funs, NB, Strtag).
  1315.  
  1316. $mod_translate_body((A; B), Preds, Funs, (NA; NB), Strtag) :- !,
  1317.     $mod_translate_body(A, Preds, Funs, NA, Strtag),
  1318.     $mod_translate_body(B, Preds, Funs, NB, Strtag).
  1319.  
  1320. $mod_translate_body((A -> B), Preds, Funs, (NA -> NB), Strtag) :- !,
  1321.     $mod_translate_body(A, Preds, Funs, NA, Strtag),
  1322.     $mod_translate_body(B, Preds, Funs, NB, Strtag).
  1323.  
  1324. $mod_translate_body(Call, Preds, Funs, NCall, Strtag) :- 
  1325.     ( Call = ':'(_,_) -> $mod_translate_colon(Call, Name, Args) ;
  1326.                          $univ(Call, [Name|Args]) ),
  1327.     $length(Args, Arity),
  1328.     ( ( $memberchk(Name/Arity ---> Tag, Preds) ;
  1329.             ( $pervasive(Name/Arity), Tag = Name ) ;
  1330.         $mod_translate_local(Name, Arity, Strtag, Tag) ) ->
  1331.           ( $mod_translate_args(Args, Preds, Funs, Nargs, Strtag),
  1332.             $mod_univ(NCall, [Tag|Nargs]) ) ;
  1333.           $mod_error(['** Consult Error : Unknown/hidden predicate',
  1334.                       Name/Arity])).
  1335.  
  1336. % $mod_translate_args(Args, Preds, Funs, NArgs)
  1337. %
  1338.  
  1339. :- mode $mod_translate_args(+,++,++,-,++).
  1340.  
  1341. $mod_translate_args(Item, _, _, Item,_) :-
  1342.     ( var(Item) ; number(Item) ; $is_buffer(Item) ), !.
  1343.  
  1344. $mod_translate_args([], _, _, [],_) :- !.
  1345.  
  1346. $mod_translate_args([Head|Tail], Preds, Funs, [NHead|NTail], Strtag) :- 
  1347.     !, $mod_translate_args(Head, Preds, Funs, NHead, Strtag),
  1348.     $mod_translate_args(Tail, Preds, Funs, NTail, Strtag).
  1349.  
  1350. $mod_translate_args(Item, Preds, Funs, Nstruct, Strtag) :-
  1351.         ( Item = ':'(_,_) -> $mod_translate_colon(Item, Name, Args) ;
  1352.                          $univ(Item, [Name|Args]) ),
  1353.     $length(Args, Arity),
  1354.     ( ( $memberchk(Name/Arity ---> Tag, Preds) ;
  1355.         $memberchk(Name/Arity ---> Tag, Funs) ;
  1356.             ( $pervasive(Name/Arity), Tag = Name ) ;
  1357.         $mod_translate_local(Name, Arity, Strtag, Tag) ) ->
  1358.           ( $mod_translate_args(Args, Preds, Funs, Nargs, Strtag),
  1359.             $mod_univ(Nstruct, [Tag|Nargs]) ) ;
  1360.           $mod_error(['** Consult Error : Predicate/function', Name/Arity,
  1361.                       'unknown in translation phase']) ).
  1362.  
  1363. % $mod_translate_local(Name, Arity, Tag, Local)
  1364. %
  1365. %
  1366.  
  1367. $mod_translate_local(Name, Arity, Tag, Local) :-
  1368.     Name \= (_:_),
  1369.     $dismantle_name(Local, Name, Tag),
  1370.     $mod_warning(['** Consult Warning : Undeclared predicate/function',
  1371.                   Name/Arity,'in code']).
  1372.  
  1373. % $mod_translate_colon(Struct, Name, Args)
  1374. %
  1375.  
  1376. $mod_translate_colon(Atom:Rest, Atom:Name, Args) :- !,
  1377.         $mod_translate_colon(Rest, Name, Args).
  1378. $mod_translate_colon(Struct, Name, Args) :-
  1379.     ( $structure(Struct) -> true ;
  1380.         $atom(Struct) ),
  1381.         $univ(Struct, [Name|Args]).
  1382.  
  1383. % $mod_univ(Term, List)
  1384. %
  1385.  
  1386. $mod_univ(X, [Head|Tail]) :-
  1387.     number(Head) -> X = anything ;         % <--- (1)
  1388.                     $univ(X, [Head|Tail]).
  1389.  
  1390.  
  1391. % Line (1). We encounter problems if we try to create a structure with
  1392. % an integer as its name, instead of an atom as its name. However, this case
  1393. % only arises when we check that a functor body is a valid one. This code 
  1394. % is discarded anyway, so we forget about wasting time processing an integer, 
  1395. % and simply return a dummy value here.
  1396.  
  1397. %****************************************************************************
  1398.  
  1399. % UTILITY PREDICATES
  1400.  
  1401. % $mod_valid_name(Name, Type)
  1402. %
  1403.  
  1404. $mod_valid_name(Name, Type) :-
  1405.     ( $atom(Name),
  1406.       $name(Name, Xname),
  1407.       not( (Xname = [0'$ | _] ;
  1408.             Xname = [0'_, 0'$|_]) ) ) 
  1409.         -> true ;
  1410.            $mod_error(['** Consult Error : Illegal name', Name, for,Type]).
  1411.  
  1412. % $mod_replace_mappings(Itemlist, List, Result)
  1413. %
  1414.  
  1415. $mod_replace_mappings(Itemlist, List, Result) :-
  1416.     Itemlist == [] ->
  1417.              Result = List ;
  1418.          ( Itemlist = [Item ---> Tag|Rest],
  1419.            $mod_replace(Item ---> _, Item ---> Tag, List, Newlist),
  1420.            $mod_replace_mappings(Rest, Newlist, Result) ).
  1421.  
  1422. % $mod_replace_mappings(Atidlist, Sig, P, NewP)
  1423. %
  1424.  
  1425. $mod_replace_mappings(Atidlist, Sig, P, NewP) :-
  1426.     Atidlist == [] -> 
  1427.              NewP = P ;
  1428.          ( Atidlist = [Atid|Atids],
  1429.            $mod_substructure(Atid, Sig, Map),
  1430.            $mod_replace(Atid ---> _, Atid ---> Map, P, P1),
  1431.            $mod_replace_mappings(Atids, Sig, P1, NewP) ).
  1432.  
  1433. % $mod_replace(Map, Map2, List, Newlist)
  1434. %
  1435.  
  1436. $mod_replace(_, Map2, [], [Map2]).
  1437. $mod_replace(Map, Map2, [Map|Rest], [Map2|Rest]).
  1438. $mod_replace(Map, Map2, [Head|Rest], [Head|Newtail]) :-
  1439.     $mod_replace(Map, Map2, Rest, Newtail).
  1440.  
  1441. % $mod_domain_restrict(X, Y, Result)
  1442. %
  1443.  
  1444. $mod_domain_restrict(_, [], []).
  1445. $mod_domain_restrict(List, [X ---> _|Tail], [X ---> Z|New]) :-
  1446.     $memberchk(X ---> Z, List),
  1447.     $mod_domain_restrict(List, Tail, New).
  1448.  
  1449. % $mod_error(List)
  1450. %
  1451.  
  1452. $mod_error([]) :-
  1453.     $nl,
  1454.     fail.
  1455. $mod_error([Head|Tail]) :-
  1456.     $write(Head),
  1457.     $writename(' '),
  1458.     $mod_error(Tail).
  1459.  
  1460. % $mod_warning(List)
  1461. %
  1462.  
  1463. $mod_warning([]) :-
  1464.     $nl.
  1465. $mod_warning([Head|Tail]) :-
  1466.     $write(Head),
  1467.     $writename(' '),
  1468.     $mod_warning(Tail).
  1469.  
  1470. % $mod_member_or_pervasive(Map, List)
  1471. %
  1472.  
  1473. $mod_member_or_pervasive(Atid/Nat ---> Tag, Y) :-
  1474.     $memberchk(Atid/Nat ---> Tag, Y), !.
  1475. $mod_member_or_pervasive(Atid/Nat ---> Atid, _) :-
  1476.     $pervasive(Atid/Nat).
  1477.  
  1478. % $mod_member_chop(Item, List, Leftover)
  1479. %
  1480.  
  1481. $mod_member_chop(X, [Head|Tail], Rest) :-
  1482.     Head = X -> Rest = Tail ;
  1483.                  $mod_member_chop(X, Tail, Rest).
  1484.  
  1485. % $mod_remove(Item, List, Newlist)
  1486. %
  1487. %
  1488.  
  1489. $mod_remove(_, [], []).
  1490. $mod_remove(X, [X|Y], Y) :- !.
  1491. $mod_remove(X, [Head|Rest], [Head|New]) :-
  1492.     $mod_remove(X, Rest, New).
  1493.  
  1494. %****************************************************************************
  1495.  
  1496. % An interesting problem with SB-Prolog appeared when writing the next
  1497. % set of predicates. They do a simple job - assert the module
  1498. % environments into the database. However, SB-Prolog is restricted to
  1499. % very small clauses when asserting, and module environments are very
  1500. % large (the root structure for instance very rapidly runs into hundreds
  1501. % of individually mapped predicates and functions). These predicates
  1502. % were therefore written to hack the original clause to bits, and assert
  1503. % the parts into the database one at a time. This is how is works :
  1504.  
  1505. % Basically, instead of asserting the following into the database (for 
  1506. % example) :
  1507. %   $module_structure(root,0,
  1508. %                     [btreedata1 ---> 34,btreedata2 ---> 36, ...],
  1509. %                     [btreedata1 : rightchild / 2 ---> '34__rightchild', ...],
  1510. %                     [test2 : a / 1 ---> '122__a', ...]).
  1511. % Where the lists are very long, we assert the following :
  1512. %   $module_structure(root,0,V0,V1,V2) :-
  1513. %         $module_data135(V0),
  1514. %         ($module_data138(V3,V1),
  1515. %         $module_data137(V4,V3),
  1516. %         $module_data136(V4)
  1517. %         ),
  1518. %         $module_data139(V2).
  1519. %   $module_data135([btreedata1 ---> 34,btreedata2 ---> 36, ... ]).
  1520. %   $module_data138(V0,[btreedata1 : rightchild / 2 ---> '34__rightchild',
  1521. %                       ... |V0]).
  1522. %   $module_data137(V0,[btreeeq : c : isnode / 1 ---> '36__isnode',
  1523. %                       ... |V0]).
  1524. %   $module_data136([test1 : pp / 1 ---> '132__pp']).
  1525. %   $module_data139([test2 : a / 1 ---> '122__a', ... ]).
  1526. % Each clause is now guaranteed to be within the restrictive clause size
  1527. % limits imposed by SB-Prolog.
  1528. % Note that
  1529. %                  $module_structure(a,1,[],[],[]).
  1530. % is stored as
  1531. %                  $module_structure(a,1,V0,V1,V2) :-
  1532. %                       V0 = [],
  1533. %                       V1 = [],
  1534. %                       V2 = [].
  1535. % Like the business about cutting back to almost the top level when an
  1536. % error occurs, this technique is messy, but it works.
  1537.  
  1538. % $mod_assert_structure(Module, Tag, Substrs, Preds, Funs)
  1539. %
  1540.  
  1541. $mod_assert_structure(Module, Tag, Substrs, Preds, Funs) :-
  1542.         $mod_split(Substrs, Nsubstrs),
  1543.         $mod_split(Preds, Npreds),
  1544.         $mod_split(Funs, Nfuns),
  1545.         $mod_make_split(Nsubstrs, Subterm, Subvar),
  1546.         $mod_make_split(Npreds, Predterm, Predvar),
  1547.         $mod_make_split(Nfuns, Funterm, Funvar), !,
  1548.         $asserta(($module_structure(Module, Tag, Subvar, Predvar, Funvar) :-
  1549.                            Subterm, Predterm, Funterm)).
  1550.  
  1551. $mod_assert_structure(Module, _, _, _, _) :-
  1552.     $mod_error(['** Consult Error : Problem asserting structure', Module,
  1553.                     'into database - signature too large?']).
  1554.  
  1555. % $mod_assert_functor(Name, Tag, Atids, Substrs, Preds, Funs, Strexpr, P, Q, R)
  1556. %
  1557.  
  1558. $mod_assert_functor(Name, Tag, Atids, Substrs, Preds, Funs, Strexpr, P, Q, 
  1559.                     R) :-
  1560.         $mod_split(Substrs, Nsubstrs),
  1561.         $mod_split(Preds, Npreds),
  1562.         $mod_split(Funs, Nfuns),
  1563.         $mod_split(Strexpr, NStrexpr),
  1564.     $mod_split(P, NP),
  1565.     $mod_split(Q, NQ),
  1566.     $mod_split(R, NR),
  1567.         $mod_make_split(Nsubstrs, Subterm, Subvar),
  1568.         $mod_make_split(Npreds, Predterm, Predvar),
  1569.         $mod_make_split(Nfuns, Funterm, Funvar),
  1570.         $mod_make_split(NStrexpr, Strterm, Strvar),
  1571.     $mod_make_split(NP, Pterm, Pvar),
  1572.     $mod_make_split(NQ, Qterm, Qvar),
  1573.     $mod_make_split(NR, Rterm, Rvar), !,
  1574.         $asserta(($module_functor(Name, Tag, Atids, Subvar, Predvar, Funvar,
  1575.                                  Strvar, Pvar, Qvar, Rvar) :-
  1576.                            Subterm, Predterm, Funterm, Strterm,
  1577.                Pterm, Qterm, Rterm)).
  1578.  
  1579. $mod_assert_functor(Module, _, _, _, _, _, _, _, _, _) :-
  1580.     $mod_error(['** Consult Error : Problem asserting functor', Module,
  1581.                     'into database - signature/code/environments too large?']).
  1582.  
  1583. % $mod_assert_signature(Name, Tag, Substrs, Preds, Funs)
  1584. %
  1585.  
  1586. $mod_assert_signature(Name, Tag, Substrs, Preds, Funs) :-
  1587.         $mod_split(Substrs, Nsubstrs),
  1588.         $mod_split(Preds, Npreds),
  1589.         $mod_split(Funs, Nfuns),
  1590.         $mod_make_split(Nsubstrs, Subterm, Subvar),
  1591.         $mod_make_split(Npreds, Predterm, Predvar),
  1592.         $mod_make_split(Nfuns, Funterm, Funvar), !,
  1593.         $asserta(($module_signature(Name, Tag, Subvar, Predvar, Funvar) :-
  1594.                            Subterm, Predterm, Funterm)).
  1595.  
  1596. $mod_assert_signature(Module, _, _, _, _) :-
  1597.     $mod_error(['** Consult Error : Problem asserting signature', Module,
  1598.                     'into database - too large?']).
  1599.  
  1600. % $mod_make_split(List, Call, Var)
  1601. %
  1602.  
  1603. $mod_make_split([], (Var = []), Var).
  1604. $mod_make_split([List], Call, Var) :-
  1605.     $gensym($module_data ,Name),
  1606.     $univ(Clause, [Name, List]),
  1607.     $univ(Call, [Name, Var]),
  1608.     $assert(Clause).
  1609. $mod_make_split([List|Tail], (Call, Term), Newvar) :-
  1610.         $mod_make_split(Tail, Term, Var),
  1611.     $gensym($module_data, Name),
  1612.         $mod_open_end(List, Nlist, Nvar),
  1613.     $univ(Clause,[Name, Nvar, Nlist]),
  1614.     $univ(Call, [Name, Var, Newvar]),
  1615.     $assert(Clause).
  1616.  
  1617. % $mod_open_end(List, Newlist, Var)
  1618. %
  1619.  
  1620. $mod_open_end([], Var, Var).
  1621. $mod_open_end([Head|Tail], [Head|New], Var) :-
  1622.         $mod_open_end(Tail, New, Var).
  1623.  
  1624. % $mod_split(List, Result
  1625. %
  1626.  
  1627. $mod_split([], []).
  1628. $mod_split(List, Result) :-
  1629.         $mod_split(List, 250, Bit, Leftover),
  1630.     ( Bit = [] -> Result = [Leftover] ;
  1631.                   ( $mod_split(Leftover, Rest),
  1632.                 Result = [Bit|Rest] ) ).
  1633.  
  1634. % $mod_split(List, Count, Result, Leftover)
  1635. %
  1636.  
  1637. $mod_split([], _, [], []).
  1638. $mod_split([Item|Tail], Count, [Item|Bit], Leftover) :-
  1639.         $mod_term_length(Item, Len),
  1640.         Ncount is Count - Len - 1,
  1641.         Ncount > 0,
  1642.         $mod_split(Tail, Ncount, Bit, Leftover).
  1643. $mod_split(List, _, [], List).
  1644.  
  1645. % $mod_term_length(X, Length)
  1646. %
  1647.  
  1648. $mod_term_length(X, 1) :-
  1649.         ( number(X) ; var(X) ; $is_buffer(X) ),!.
  1650. $mod_term_length([], 1).
  1651. $mod_term_length(Str, Total) :-
  1652.         $mod_length_args(1, Str, Bit),
  1653.         Total is Bit + 1.
  1654.  
  1655. % $mod_length_args(No, Str, Total
  1656. %
  1657.  
  1658. $mod_length_args(No, Str, Total) :-
  1659.         $arg(No, Str, Arg),
  1660.         $mod_term_length(Arg, Len),
  1661.         Newno is No + 1,
  1662.         $mod_length_args(Newno, Str, Sofar),
  1663.         Total is Sofar + Len.
  1664. $mod_length_args(_, _, 0).
  1665.  
  1666. %****************************************************************************
  1667.