home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 9 / 09.iso / l / l210 / 1.ddi / PIE.ARC / PIE.PRO < prev    next >
Encoding:
Text File  |  1988-06-21  |  4.9 KB  |  174 lines

  1. /*****************************************************************************
  2.             Prolog Inference Engine
  3.             =======================
  4.  
  5.   Copyright (c) 1986, 88 by Borland International, Inc
  6.  
  7.   Module PIE.PRO: The declarations and main program
  8.  
  9.   This program implements the Prolog inference engine as described in
  10.   appendix K (META-PROGRAMMING) in the Turbo Prolog manual.
  11. *****************************************************************************/
  12.  
  13. config "pie.sys"
  14. check_determ code=3600 trail=1000 errorlevel=0
  15.  
  16. CONSTANTS
  17. % implement_trace = true
  18.  
  19. DOMAINS
  20.   TERM  = reference
  21.         var(VID);
  22.         cmp(REFSYMB,TERML);list(TERM,TERM); nill;
  23.         atom(REFSYMB); int(REFINT);
  24.         str(REFSTR); char(REFCHAR)
  25.  
  26.   TERML = reference TERM*
  27.  
  28.   REFINT    = reference INTEGER
  29.   REFSYMB    = reference STRING
  30.   REFSTR    = reference STRING
  31.   REFCHAR    = reference char
  32.   VID        = reference STRING
  33.  
  34. /*---------- INTERPRETER ENVIRONMENT ------------------*/
  35.   E    = e(VID,TERM)
  36.   ENV    = reference E*
  37.  
  38. /*---------- MISC HANDY DOMAIN DECLARATIONS ------------------*/
  39.   OP    = STRING    % Storing of operators
  40.   XFY    = STRING    % xfy; yfx; xfx; yfy; fx; fy; xf; yf
  41.   ASSOC = x; y        % The associavity to the left or the right
  42.   PRIOR = INTEGER    % Priority of operators
  43.   DISPLAY = SYMBOL    % Display mode for term writer
  44.  
  45.   FILE = temp; seeing; telling
  46.  
  47. /*---------- SCANNER DOMAINS ------------------*/
  48.   TOK        = lbrack; rbrack; lpar; rpar; lcurly; rcurly;
  49.           var(STRING);      atom(STRING);
  50.           int(INTEGER); str(STRING); char(CHAR);
  51.           comma; bar; dot
  52.  
  53.   CURSORTOK    = t(TOK,CURSOR)
  54.   CURSOR    = INTEGER
  55.   TOKL        = CURSORTOK*
  56.  
  57. /*---------- PARSER DOMAINS ------------------*/
  58.   STERM            = var(STRING);
  59.              cmp(STRING,STERML);
  60.              list(STERM,STERM); nill;
  61.              atom(STRING);
  62.              int(INTEGER);
  63.              str(STRING);
  64.              char(CHAR)
  65.  
  66.   STERML           = STERM*
  67.  
  68. PREDICATES % Need to be compiles first due to memory limitations
  69.   nondeterm call(STRING,TERML)
  70.  
  71. /* ---------- The clause and operator database ------------------- */
  72. DATABASE
  73.    determ traceflag
  74.    op(PRIOR,XFY,OP)
  75.    clause(STERM,STERM)
  76.  
  77. CLAUSES
  78.   op(1200,xfx,":-").    op(1100,xfy,";").    op(1000,xfy,",").
  79.   op(900,fy,"not").    op(700,xfx,"=").    op(700,xfx,"\\=").
  80.   op(700,xfx,"is").    op(700,xfx,"<").    op(700,xfx,"=<").
  81.   op(700,xfx,">").    op(700,xfx,">=").    op(700,xfx,"==").
  82.   op(700,xfx,"\\==").    op(700,xfx,"=..").    op(500,yfx,"+").
  83.   op(500,fx,"+").    op(500,yfx,"-").    op(500,fx,"-").
  84.   op(400,yfx,"*").    op(400,yfx,"/").    op(300,xfx,"mod").
  85.  
  86. /*****************************************************************************
  87.     Include files    
  88. *****************************************************************************/
  89.  
  90. include "pie.sca"    % The scanner
  91. include "pie.par"    % The operator precedence parser
  92. include "pie.out"    % The term
  93. include "pie.inf"    % The inference engine
  94.  
  95. /*****************************************************************************
  96.     USER INTERFACE
  97. *****************************************************************************/
  98.  
  99. DATABASE - counter
  100.   determ counter(INTEGER)
  101.  
  102. PREDICATES
  103.   run
  104.   init_counter
  105.   count
  106.   wsol(INTEGER)
  107.   wenv(ENV)
  108.   handle_usergoal(STERM)
  109.   errorhnd(INTEGER)
  110.   nondeterm repread(STRING,STRING)
  111.  
  112. CLAUSES
  113.   run:- textmode(ROWS,COLS),
  114.       RR=ROWS-1,
  115.       makewindow(3,7,7,"Prolog Inference Engine Trace",2,20,20,60),
  116.       makewindow(1,112,0,"",RR,0,1,COLS),
  117.       write(" Esc: Aborts     help: help information     list:list clauses     edit:editor"),
  118.       makewindow(2,7,7,"Prolog Inference Engine Dialog",0,0,RR,COLS),
  119.       write("\nGoal: "),
  120.     readln(FIRSTL),
  121.     repread(FIRSTL,L),
  122.     init_counter,
  123.       tokl(0,L,TOKL),    s_term(TOKL,_,TERM),
  124.     trap(handle_usergoal(TERM),E,errorhnd(E)),
  125. ifdef implement_trace
  126.     traceflag, % If trace is turned on show the trace window
  127.     retractall(traceflag),
  128.     shiftwindow(3),
  129.     write("Press any key\n"),
  130.     readchar(CH),
  131.     shiftwindow(2),
  132. enddef
  133.     fail.
  134.  
  135.   handle_usergoal(cmp(":-",[HEAD,BODY])):-!,
  136.     convhead(HEAD,HEAD1),
  137.     assert(clause(HEAD1,BODY)),
  138.     write("Asserted").
  139.   handle_usergoal(TERM):-
  140.       free(ENV),
  141.     getbacktrack(BTOP),
  142.     unify_body(TERM,ENV,BTOP),
  143.     wenv(ENV),nl,
  144.     count,
  145.     free(ENV), % Give only one solution when there are no variables
  146.     cutbacktrack(BTOP),
  147.     fail.
  148.   handle_usergoal(_):-
  149.     closefile(seeing),closefile(telling),
  150.     counter(X),wsol(X).
  151.  
  152.   errorhnd(0):-!.
  153.   errorhnd(N):-
  154.     errormsg("prolog.err",N,A,_),
  155.     write(A).
  156.  
  157.   repread(L,L).
  158.   repread(_,L):-write("\nGoal: "),readln(LL),repread(LL,L).
  159.  
  160.   init_counter:-
  161.     retractall(_,counter),assert(counter(0)).
  162.  
  163.   count:-retract(counter(N)),N1=N+1,assert(counter(N1)).
  164.  
  165.   wsol(0):-!,write("No solutions").
  166.   wsol(1):-!,write("1 Solution").
  167.   wsol(N):-write(N," Solutions").
  168.  
  169.   wenv(L):-free(L),!,write("True").
  170.   wenv([e(VAR,TERM)|T]):-free(T),!,write(VAR,'='),wterm("write",TERM).
  171.   wenv([e(VAR,TERM)|T]):-write(VAR,'='),wterm("write",TERM),write(", "),wenv(T).
  172.  
  173. GOAL run.
  174.