% File: ECMI01.MINIDB % Updated: 15 October 84 11:10 % Author: Peter Ross % Purpose: a mini database system, needing improvement in every way % Level: introductory. % This is extremely feeble. Learning exercise: improve it. % % It uses the following built-in predicates: % assert(Term) - to add to the database % retract(Term) - to remove from the database % read(Term) - to read data (in the form of a term) or % replies (also in the form of a term) from % the user. % abolish(Term,Arity) - to flush out the database % bagof(X, Term, List) - to collect up all the Xs fitting the Term % % NOTES: (1) all database items are held as terms of the form dbitem(Item). % (2) all your input must be valid Prolog terms. % (3) the program is atypical, because backtracking NEVER happens in it. % It can bomb out, if you attempt to query an empty database. go :- abolish(dbitem,1), db. db :- write('Type add. to add to the database,'), nl, write(' delete. to delete items,'), nl, write(' query. to ask a question.'), nl, read(Reply), process(Reply), db. process(add) :- write('Enter what you want to add.'), nl, read(Term), assert(dbitem(Term)), recheck(add). process(delete) :- write('Enter what you want to delete.'), nl, read(Term), retract(dbitem(Term)), recheck(delete). process(query) :- write('What do you want to look up?'), nl, read(Term), bagof(Term, dbitem(Term), List), write(List), nl, recheck(query). process(SomethingElse) :- write('I didn''t understand that.'), nl. recheck(Operation) :- write('Another (yes/no)?'), nl, read(Reply), reaction(Reply, Operation). reaction(yes, Operation) :- process(Operation). reaction(no, _). reaction(_, Operation) :- write('Answer yes or no, clot! '), recheck(Operation).