home *** CD-ROM | disk | FTP | other *** search
/ Programmer 7500 / MAX_PROGRAMMERS.iso / CLIPPER / MISC / AIE8901.ZIP / TOOLBOX.CDE < prev   
Encoding:
Text File  |  1988-05-04  |  8.1 KB  |  282 lines

  1. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  2.  
  3.            Tax Records as Executable Data
  4.  
  5.  
  6. furniture(X) :-
  7.       X is 0
  8. +68.90  % work table , capitol office furn, 17 mar
  9. +91.11  % bookcase jacob gardner 4/20 receipt
  10.  
  11.  
  12. transportation(X) :-
  13.         X is 0
  14. +2.75   % 4/30 trip to icarus re. demo disk for prolog tools
  15. +1.60   % metro to bethesda to pick up arity manuals
  16. +3.00   % bus and metro to rockville to ai sig 5/5
  17. +2.40   % bus and metro to get copies for CDC AIT intro to AI
  18. +4*2.65 % bus and metro to twinbrook for CDC AIT intro to AI 5/16-17
  19.  
  20. stamps(X) :-
  21.       X is 0
  22. +0.75  % 4/28 mailing prolog tools info
  23. +0.5   % 5/4 stamps
  24.  
  25. supplies(X) :-
  26.    X is
  27. +97.85-91.11   % book ends jacob gardner 4/20 receipt
  28.                % staples jacob gardner 4/20 receipt
  29. + 4.29         % packing tape cash higgers 5/8 CR receipt
  30.  
  31. services(X) :-
  32.    X is 0
  33. +11.70    % copies for ai sig lecture 5/5
  34. +6.40     % copies of PC monitor article for CDC AIT intro to AI
  35.  
  36.  
  37.  
  38.  
  39.            Total Expenses from Executable Data
  40.  
  41. total(X) :-
  42.     furniture(Furniture ),
  43.     transportation(Transportation),
  44.     stamps(Stamps),
  45.     services(Services),
  46.     supplies(Supplies),
  47.   X is  0
  48.           + Furniture
  49.           + Stamps
  50.           + Services
  51.           + Transportation
  52.           + Supplies
  53.  
  54.  
  55.  
  56.            A  Report Definition
  57.  
  58. report_skeleton([
  59.       note $ The following are business expenses$,
  60.       nl                     ,
  61.       furniture              ,
  62.       hardware               ,
  63.       publications           ,
  64.       rent                   ,
  65.       services               ,
  66.       software               ,
  67.       supplies               ,
  68.       transportation         ,
  69.       utilities              ,
  70.       nl                     ,
  71.       expenses                               ]).
  72.  
  73.  
  74.  
  75.                  A Simpler E-Data Format
  76.  
  77. furniture are
  78. +68.90 % work table , capitol office furn, 17 mar (on may citi visa stmt)
  79. +91.11 % bookcase jacob gardner 4/20 receipt  dc tax inc. on order here
  80.  
  81.  
  82. transportation are
  83. +2.75 % 4/30 trip to icarus re. demo disk for prolog tools
  84. +1.60 % metro to bethesda to pick up arity manuals
  85. +3.00 % bus and metro to rockville to ai sig 5/5
  86. +2.40 % bus and metro to get copies for CDC AIT intro to AI
  87. +4*2.65 % bus and metro to twinbrook for CDC AIT intro to AI 5/16-17
  88.  
  89. /********* similarly for stamps, supplies, services, etc. **********/
  90.  
  91. expenses are
  92.     furniture
  93.    + transportation
  94.    + stamps
  95.    + supplies
  96.    + services
  97.  
  98.  
  99.              Internally Stored E-Data
  100.  
  101. furniture are + 68.9 + 91.11.
  102. transportation are +2.75 + 1.6 + 3.0 + 2.4 + 4 * 2.65.
  103. expenses are furniture + transportation + stamps
  104.              + supplies + services.
  105.  
  106.  
  107.           Finding Category Values
  108.  
  109.        /* get_value returns the Value of Category */
  110. get_value( Category, Value) :-
  111.          % get definition of category
  112.          % as an arithmetic expression
  113.               tax_trace([$b get_definition$]),
  114.      get_definition(Category, Expr),
  115.          % evaluate expression
  116.               tax_trace([$b get_value_hlpr$]),
  117.      get_value_hlpr(Category, Expr, Value),!.
  118.  
  119.      % default value is 0
  120. get_value( _, 0).
  121.  
  122.       % get the Expr(ession) which defines Category
  123. get_definition(Category, Expr) :-
  124.        are(Category, Expr).
  125.  
  126.  
  127.      % get_value_hlpr gets the value of an expense category,
  128.      % given its name and definition as an arithmetic expression
  129.            % first, look for the value stored in the database
  130.  get_value_hlpr(Category, _, Value) :-
  131.            % if it's there, use it.
  132.      has_stored_value(Category, Value),!,
  133.               tax_trace([Category, $stored = $, Value])
  134.                 .
  135.  
  136.     % otherwise, compute the value and save it
  137.  get_value_hlpr(Category, Expr, Value) :-
  138.            % compute the value
  139.               tax_trace([$b compute_value$]),
  140.      compute_value( Expr, Value),
  141.               tax_trace([Category, $computed = $, Value]),
  142.            % and save it
  143.      save_value_into_db(Category, Value),
  144.               tax_trace([$x get_value_hlpr$])
  145.                   .
  146.  
  147. % compute_value computes the value of an arithmetic experssion
  148.  
  149.       % This rule is just for tracing purposes
  150. compute_value(Expr, _) :-
  151.       tax_trace([$e compute_value, input = $,Expr]), fail.
  152.  
  153.            % numbers have themselves as values.
  154. compute_value( Expr, Expr)  :-
  155.      number(Expr),!.
  156.  
  157.            % atoms are assumed to name categories
  158.            % we get their values using get_value
  159. compute_value( Expr, Value)  :-
  160.       atom(Expr),!,
  161.        get_value( Expr, Value).
  162.  
  163.            % if the expression contains an arithmetic operator,
  164.            % apply that operator to its arguments
  165. compute_value(Expr, Value) :-
  166.            Expr =.. [Op, Arg1, Arg2],
  167.            compute_value(Arg1, Val1),
  168.            compute_value(Arg2, Val2),
  169.            apply_op(Op, Val1, Val2, Value).
  170.  
  171.            % evaluate unary sign operator
  172. compute_value(Expr, Value) :-
  173.            Expr =.. [Sign, Arg],
  174.            compute_value(Arg, Val),
  175.            apply_sign(Sign, Val, Value).
  176.  
  177.  
  178.         % apply an arithmetic operator to its arguments
  179. apply_op('+', Arg1, Arg2, Val) :- Val is Arg1 + Arg2.
  180. apply_op('-', Arg1, Arg2, Val) :- Val is Arg1 - Arg2.
  181. apply_op('/', Arg1, Arg2, Val) :- Val is Arg1 / Arg2.
  182. apply_op('*', Arg1, Arg2, Val) :- Val is Arg1 * Arg2.
  183.  
  184.  
  185.         % apply a unar;y sign operator to its argument
  186. apply_sign('+', Val, Val) :- !.
  187. apply_sign('-', Val, Value) :- Value is -Val.
  188.  
  189.  
  190.  
  191. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  192.            The Top Level
  193.  
  194.  
  195. compute  :-
  196.               % remove any old facts from the database
  197.         clean_up_database,
  198.               % read category definitions
  199.         consult_data,
  200.               % look up report definition
  201.         report_skeleton(L),
  202.               % do report
  203.         summarize(L).
  204.  
  205.  
  206.            The Report Definition Interpreter
  207.  
  208.         % definition of not as a prefix operator
  209. :- current_op( Prec, _, ':-'), op( Prec, fx, note).
  210.  
  211.         % top level of report generation
  212. summarize( []) :-  !.
  213. summarize( [H | T]) :-
  214.      summarize1(H),
  215.      summarize(T).
  216.  
  217.          % If the item in the report skeleton is a request for
  218.          % a new line, write out the new line.
  219. summarize1( nl )  :- nl,!.
  220.  
  221.          % If the item in the report skeleton is an atom that
  222.          % has a category definition, then get its value and
  223.          % report it
  224. summarize1( Category )  :-
  225.        atom( Category),
  226.        defined( Category),   !,
  227.        summarize_category( Category).
  228.  
  229.          % do any of the special forms you want, e.g. note
  230. summarize1( note(Arg) ):- note(Arg),!.
  231.  
  232.          % This rule gives the user access to the Prolog interpreter.
  233.          % Sometimes we may want to access by omitting this rule.
  234. summarize1( Form ):- call(Arg),!.
  235.  
  236.          % Prevent failures but write warning
  237. summarize1( Form ):- write_message([$*** Unable to process $, Form]),!.
  238.  
  239.          % succeeds when a Category is defined
  240. defined( Category) :- are( Category , _).
  241.  
  242.          % reports value of a category
  243. summarize_category( Category) :-
  244.        get_value( Category, Value ), !,
  245.        report( Category, Value).
  246.          % write warning if computation fails
  247. summarize_category( Category) :-
  248.       write_message([$*** Unable to compute $, Category ]),!.
  249.  
  250.            % report value of a category
  251. report( Category, Value) :-
  252.            % convert returned value to 2-decimal place form
  253.       float_text(Value, Value_as_string, fixed(2)),
  254.            % write out Category and Value in dollars and cents
  255.         write_message([ Category , $ = $, Value_as_string]).
  256.  
  257.            % write out user-supplied comment
  258. note(Arg) :- write_message([$Note: $, Arg]).
  259.  
  260.  
  261.  
  262.  
  263.            Robust Database Management in Prolog
  264.  
  265. has_stored_value(Category, Value) :-
  266.            call(zzz_stored_value(Category, Value)).
  267.  
  268. save_value_into_db(Category, Value)  :-
  269.            asserta(zzz_stored_value(Category, Value)).
  270.  
  271. clean_up_database :- retractall(zzz_stored_value( _, _)).
  272.  
  273.  
  274. retractall(Form) :-
  275.       retract(Form),
  276.       fail.
  277. retractall(_).
  278.  
  279. atabase :- retractall(zzz_stored_value( _, _)).
  280.  
  281.  
  282. retractall(Form) :