CSE 5326 - HW8 Due: November 27, 1991 at midnight (electronically) Instructions: The following four questions require you to write some simple Prolog programs to test your understanding of Prolog's built-in predicates. Put the programs in a file, and submit the file using the handin-hw8 program. Be sure to comment and test your programs. Also, be sure to put your name and userid in a comment at the top og the file. Separate your solutions to the four questions with appropriate comments. Question 1: Type Checking Predicates Write a Prolog predicate sum(L,N) that takes a list as the first argument and sums the elements of the list according to the following rules: If element is an integer, then just add it to the sum. If the element is an atom, then add 1 to the sum. If the element is a floating-point number, subtract 1 from the sum. Otherwise, add 0 to the sum. For example, ?- sum([],N). N = 0 ; no ?- sum([1,2,3],N). N = 6 ; no ?- sum([2,foo,1.3],2). /* 2 + 1 - 1 */ yes ?- sum([foo(a),foo(b)],N). /* 0 + 0 */ N = 0 ; no You may assume that the first argument is a list, and the second argument is a variable or an integer. Your solution will probably require an auxiliary predicate for returning the increment according to the above rules, and will probably require the use of some cuts. Question 2: File I/O Write a Prolog predicate dump(File) that dumps the current database to File, as per one student's request in class. The predicate should first open File for writing, set the current output stream to the File's stream, do a listing, and then close the stream. Note that the rules and facts written to the file have the A, B, C, ... variables, not the original variables, and that anonymous variables have been replaced by the Prolog variables as well. Question 3: Asserting Rules Write a Prolog program add_foo(Rule) that adds the Rule to the database after augmenting the list of antecedents with the predicate foo(X), where X is the first argument to the given Rule. For example, ?- add_foo((mortal(X):-man(X))). X = _42 ; no will have the side effect of not defining the given rule, but the augmented rule: mortal(A) :- foo(A), man(A). To do this, you will need a new predicate arg(N,T,A), which puts the Nth argument of term T in A. For example, ?- arg(2,likes(john,mary),A). A = mary; no ?- arg(1,mortal(X),A). X = A = _48 ; no Another piece of information you will need to solve this problem is that clause(H,B,R) does not require H to be of the form functor(_,_,...), when R is instantiated to a specific reference. This is contrary to what I told you in class (yes, I blew it again). For example, ?- assert((mortal(X):-man(X)),R), clause(H,B,R). X = _48, R = '$ref'(580712,1), H = mortal(_448), B = man(_448); no The add_foo(Rule) program should first assert the Rule and remember its reference, so that the program can then use clause(Head,Body,Ref) to get the head and body of the rule. Next, erase the Rule, determine the first argument (Arg1) of the Head of the Rule using the arg predicate, and then assert the Rule again, only the body of the new rule should be (foo(Arg1),Body). Question 4: Formatting Write a Prolog program enumerate(List) to enumerate the elements of a list of atoms. For example: ?- enumerate([a,b,c]). 1. a 2. b 3. c yes ?- enumerate([]). yes You may assume the list is either empty, or contains only atoms. You will probably have to define an auxiliary predicate that keeps track of a counter and uses format to output the elements of the list. --- End of File: HW8 ---