Lab 2 notes: 1. Continue the family program from Lab1 ------------------------------- 2. Operators in Prolog: -------------------- We talked about: =, ==, \=, \==, is, =:=, =\= number/1, integer/1,float/1 Try the following queries: ?- X =a. ?- X ==a. ?- X \=a. ?- b \= a. ?- X \== a. ?- b \== a. ?- X is 1+2. ?- X = 1+2. ?- 3*5 =:= 3+12. Notation: number/1 (for example) means that the predicate's name (the functor) is 'number' and it has 1 argument 3. Arithmentic example ------------------------- Here are the formulae for converting between degrees Fahrenheit and degrees Celsius: DegreesF = (9 / 5 * DegreesC) + 32 DegreesC = 5 / 9 * (DegreesF - 32) Write two Prolog rules: one called c2f(C, F) to convert from Celsius to Fahrenheit and one called f2c(F, C) to convert from Fahrenheit to Celsius. Make sure your rules work exactly as shown for the following queries: ?- c2f(20, F). F = 68 ?- f2c(98.6, C). C = 37 Write a Prolog rule called convert(C, F) that first checks which temperature (C or F) is given, and then does the appropriate conversion. That is, when C is given, convert(C, F) uses c2f(C, F). When F is given it uses f2c(F, C). Make sure your rule works exactly as shown for the following queries: ?- convert(20, F). F = 68 ?- convert(C, 98.6). C = 37 3. Debugging in Prolog. Traces: let you see every call one by one. Turning trace on: ?- trace. Turning trace off: ?- notrace. Note: in SWI Prolog, the trace stays on only for the next predicate you run. Spy point: those let you enter trace mode when a particular predicate is called. Adding a spy point for predicate pred with arity n (i.e., pred takes n arguments): ?- spy(pred/n). Adding a spy point for predicate pred, tracing every instance of the predicate, regardless of arity: ?- spy(pred). Removing a spy point: ?- nospy(pred/n). ?- nospy(pred). Useful commands while in trace mode: : move one step forward l: leap causes the program to resume (i.e., to leave trace mode) until the next spy point, or until the program is done. s: skip the current subprogram: the current subprogram is executed with trace off, and the trace resumes when that subprogram is done. Useful to quickly go through long subprograms that you've already debugged and you don't need to go through the details. Note that the behaviour of these commands may not be intuitively obvious when you work with them, so you should first get accustomed to them with simple programs. Dealing with misbehaving code: If your code gets into infinite recursion, you can hit Ctrl-C to stop it. The interpreter will put you in break mode, which you can leave by typing "a" (from "abort").