home *** CD-ROM | disk | FTP | other *** search
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-
- Tax Records as Executable Data
-
-
- furniture(X) :-
- X is 0
- +68.90 % work table , capitol office furn, 17 mar
- +91.11 % bookcase jacob gardner 4/20 receipt
-
-
- transportation(X) :-
- X is 0
- +2.75 % 4/30 trip to icarus re. demo disk for prolog tools
- +1.60 % metro to bethesda to pick up arity manuals
- +3.00 % bus and metro to rockville to ai sig 5/5
- +2.40 % bus and metro to get copies for CDC AIT intro to AI
- +4*2.65 % bus and metro to twinbrook for CDC AIT intro to AI 5/16-17
-
- stamps(X) :-
- X is 0
- +0.75 % 4/28 mailing prolog tools info
- +0.5 % 5/4 stamps
-
- supplies(X) :-
- X is
- +97.85-91.11 % book ends jacob gardner 4/20 receipt
- % staples jacob gardner 4/20 receipt
- + 4.29 % packing tape cash higgers 5/8 CR receipt
-
- services(X) :-
- X is 0
- +11.70 % copies for ai sig lecture 5/5
- +6.40 % copies of PC monitor article for CDC AIT intro to AI
-
-
-
-
- Total Expenses from Executable Data
-
- total(X) :-
- furniture(Furniture ),
- transportation(Transportation),
- stamps(Stamps),
- services(Services),
- supplies(Supplies),
- X is 0
- + Furniture
- + Stamps
- + Services
- + Transportation
- + Supplies
-
-
-
- A Report Definition
-
- report_skeleton([
- note $ The following are business expenses$,
- nl ,
- furniture ,
- hardware ,
- publications ,
- rent ,
- services ,
- software ,
- supplies ,
- transportation ,
- utilities ,
- nl ,
- expenses ]).
-
-
-
- A Simpler E-Data Format
-
- furniture are
- +68.90 % work table , capitol office furn, 17 mar (on may citi visa stmt)
- +91.11 % bookcase jacob gardner 4/20 receipt dc tax inc. on order here
-
-
- transportation are
- +2.75 % 4/30 trip to icarus re. demo disk for prolog tools
- +1.60 % metro to bethesda to pick up arity manuals
- +3.00 % bus and metro to rockville to ai sig 5/5
- +2.40 % bus and metro to get copies for CDC AIT intro to AI
- +4*2.65 % bus and metro to twinbrook for CDC AIT intro to AI 5/16-17
-
- /********* similarly for stamps, supplies, services, etc. **********/
-
- expenses are
- furniture
- + transportation
- + stamps
- + supplies
- + services
-
-
- Internally Stored E-Data
-
- furniture are + 68.9 + 91.11.
- transportation are +2.75 + 1.6 + 3.0 + 2.4 + 4 * 2.65.
- expenses are furniture + transportation + stamps
- + supplies + services.
-
-
- Finding Category Values
-
- /* get_value returns the Value of Category */
- get_value( Category, Value) :-
- % get definition of category
- % as an arithmetic expression
- tax_trace([$b get_definition$]),
- get_definition(Category, Expr),
- % evaluate expression
- tax_trace([$b get_value_hlpr$]),
- get_value_hlpr(Category, Expr, Value),!.
-
- % default value is 0
- get_value( _, 0).
-
- % get the Expr(ession) which defines Category
- get_definition(Category, Expr) :-
- are(Category, Expr).
-
-
- % get_value_hlpr gets the value of an expense category,
- % given its name and definition as an arithmetic expression
- % first, look for the value stored in the database
- get_value_hlpr(Category, _, Value) :-
- % if it's there, use it.
- has_stored_value(Category, Value),!,
- tax_trace([Category, $stored = $, Value])
- .
-
- % otherwise, compute the value and save it
- get_value_hlpr(Category, Expr, Value) :-
- % compute the value
- tax_trace([$b compute_value$]),
- compute_value( Expr, Value),
- tax_trace([Category, $computed = $, Value]),
- % and save it
- save_value_into_db(Category, Value),
- tax_trace([$x get_value_hlpr$])
- .
-
- % compute_value computes the value of an arithmetic experssion
-
- % This rule is just for tracing purposes
- compute_value(Expr, _) :-
- tax_trace([$e compute_value, input = $,Expr]), fail.
-
- % numbers have themselves as values.
- compute_value( Expr, Expr) :-
- number(Expr),!.
-
- % atoms are assumed to name categories
- % we get their values using get_value
- compute_value( Expr, Value) :-
- atom(Expr),!,
- get_value( Expr, Value).
-
- % if the expression contains an arithmetic operator,
- % apply that operator to its arguments
- compute_value(Expr, Value) :-
- Expr =.. [Op, Arg1, Arg2],
- compute_value(Arg1, Val1),
- compute_value(Arg2, Val2),
- apply_op(Op, Val1, Val2, Value).
-
- % evaluate unary sign operator
- compute_value(Expr, Value) :-
- Expr =.. [Sign, Arg],
- compute_value(Arg, Val),
- apply_sign(Sign, Val, Value).
-
-
- % apply an arithmetic operator to its arguments
- apply_op('+', Arg1, Arg2, Val) :- Val is Arg1 + Arg2.
- apply_op('-', Arg1, Arg2, Val) :- Val is Arg1 - Arg2.
- apply_op('/', Arg1, Arg2, Val) :- Val is Arg1 / Arg2.
- apply_op('*', Arg1, Arg2, Val) :- Val is Arg1 * Arg2.
-
-
- % apply a unar;y sign operator to its argument
- apply_sign('+', Val, Val) :- !.
- apply_sign('-', Val, Value) :- Value is -Val.
-
-
-
- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- The Top Level
-
-
- compute :-
- % remove any old facts from the database
- clean_up_database,
- % read category definitions
- consult_data,
- % look up report definition
- report_skeleton(L),
- % do report
- summarize(L).
-
-
- The Report Definition Interpreter
-
- % definition of not as a prefix operator
- :- current_op( Prec, _, ':-'), op( Prec, fx, note).
-
- % top level of report generation
- summarize( []) :- !.
- summarize( [H | T]) :-
- summarize1(H),
- summarize(T).
-
- % If the item in the report skeleton is a request for
- % a new line, write out the new line.
- summarize1( nl ) :- nl,!.
-
- % If the item in the report skeleton is an atom that
- % has a category definition, then get its value and
- % report it
- summarize1( Category ) :-
- atom( Category),
- defined( Category), !,
- summarize_category( Category).
-
- % do any of the special forms you want, e.g. note
- summarize1( note(Arg) ):- note(Arg),!.
-
- % This rule gives the user access to the Prolog interpreter.
- % Sometimes we may want to access by omitting this rule.
- summarize1( Form ):- call(Arg),!.
-
- % Prevent failures but write warning
- summarize1( Form ):- write_message([$*** Unable to process $, Form]),!.
-
- % succeeds when a Category is defined
- defined( Category) :- are( Category , _).
-
- % reports value of a category
- summarize_category( Category) :-
- get_value( Category, Value ), !,
- report( Category, Value).
- % write warning if computation fails
- summarize_category( Category) :-
- write_message([$*** Unable to compute $, Category ]),!.
-
- % report value of a category
- report( Category, Value) :-
- % convert returned value to 2-decimal place form
- float_text(Value, Value_as_string, fixed(2)),
- % write out Category and Value in dollars and cents
- write_message([ Category , $ = $, Value_as_string]).
-
- % write out user-supplied comment
- note(Arg) :- write_message([$Note: $, Arg]).
-
-
-
-
- Robust Database Management in Prolog
-
- has_stored_value(Category, Value) :-
- call(zzz_stored_value(Category, Value)).
-
- save_value_into_db(Category, Value) :-
- asserta(zzz_stored_value(Category, Value)).
-
- clean_up_database :- retractall(zzz_stored_value( _, _)).
-
-
- retractall(Form) :-
- retract(Form),
- fail.
- retractall(_).
-
- atabase :- retractall(zzz_stored_value( _, _)).
-
-
- retractall(Form) :