AI1 Prolog exercise 2 20.11.84 Check that you have a copy of emnotes (notes on the editor em). Switch the terminal on; call host (2972); log on. Look at your files - make sure you still have a copy of the file called 'family'. Copy it from me if you haven't (ecmi04.famtree). Run prolog - use ai1prolog or p Command: ai1prolog Consult the file 'family' ?- [family]. Try matching a few goals to remind yourself how it works. ?- parent(Who,cecil). ?- daughter(tom,D). ?- son(mary,P). ?- son(P,mary). Quit from prolog ?- quit. Last week we added extra clauses to our family tree program by consulting a special file - the user file. Each clause was read in from the terminal, instead of from a stored file in your file area. All the clauses added were lost when you 'quitted' prolog. If we put the clauses in a file in your file area (instead of in the user file) we could consult it (as we consulted 'family') and use the clauses, and save them to be used again. We can also change them easily. We are going to add a grandparent clause and more family. The editor 'em' allows us to add/delete/change/move text in a file. Type the following: Command: em family Use your emnotes and try a few commands to the editor. >1p >5p >1,10p >10 The last line altered or printed becomes 'the current line', indicated by . (a single full stop). The last line in the file is indicated by $. Try >p999 >1,$p Try adding some more people to the database. >1p >a father(alan,tom). father(alan,jane). father(alan,fred). mother(zoe,jim). mother(zoe,sue). . > Remember a single . on its own indicates that you have finished adding text. Look out for the > prompt to continue with edit commands. We could also add the grandparent rule. After the male/female clauses might be a good place to put it, before the parent clauses. >/parent/ Find the first line with 'parent' in it. Insert the new rule before it. >i grandparent(Gp,Gch):- parent(Gp,Pa), parent(Pa,Gch). . > 1,$p The whole database will be listed. We need to 'write it back' to the file (ie update the file to include what we have changed in this session). >w Get back to emas command level (use q not quit) >q Get into prolog. Consult your new version of 'family'. List it. Try it. If you spot any errors you will need to edit the file again to alter it. Command: list family Command: p ?- [family]. ?- listing. There is a way to edit the file without having to go out of prolog and back in again. You can give emas commands from inside prolog. ?- emas(em,family). will have the same effect as Command: em family except that when you finish editing you will be back in Prolog again automatically (if that is where you went into the editor from). >w >q ?- [-family]. You have to consult the file again BUT if you just do this as before [family] you will get two copies of some clauses in your database. To prevent this you reconsult the file, and all clauses in the new (reconsulted) file replace any in the old file with the same name. To indicate to the prolog interpreter that you wish to reconsult you put a - in front of the filename. Try adding brother/sister and aunt/uncle clauses: write them out on paper first get into the editor add/change the text get back to prolog reconsult the file list it test it look any changes needed start again! If you get this far, and are quite happy, then try something harder! How would you add facts and rules (ie clauses) to the database to give information about people's ages, and to be able to answer questions like 'Is fred older than Jim?' or 'How old is Sue?' ? Some hints: You could have a clause called age with two arguments (persons_name,persons_age) eg age(fred,30). age(mary,72). You can compare ages using > which means 'is greater than' ie X > Y is true (the goal succeeds) if whatever matches to X is greater that whatever matches to Y eg if X becomes instantiated to 72 and y to 30 then the goal X > Y would succeed. try ?- age(cecil,X),age(mary,Y), Y>X. [you have to add some ages to the database first] There might be a rule is_older(Oldperson,Youngperson) which is true when the Oldperson is older than the Youngperson. The whole rule, if we were comparing mary and cecil's ages, would be mary is older than cecil if we know mary's age and we now cecil's age and mary's age is greater than cecil's age. We could different rules for cases where we don't know everyone's age, but we know that if fred is the parent of cecil then fred must be older than cecil. How about this mary is older than cecil if mary is older than someone and that someone is older than cecil. [we are getting into more hairy ground here with clauses that have subgoals with the same name as themselves - if a clause calls a clause of the same name (ie it has itself as a subgoal) we say that it is recursive - more of this later in the course].