|
I just recreated my own solution to this problem. Here is some advice, not perfectly organized:
- Think of
instequivclasses as a weird sort of printout function. Don't use it in your definitions. Once you've applied instequivclasses, you can't do any more manipulation of the equivalence class with things like
join.
- You only need to add one more relation to the definition of
trivequiv. The subtle part of the construction of the trivial equivalence class is the way that it generates N different variables with a finite program text. At first, I wondered if this was possible, since I can only write a fixed number of variable names in my program, and perhaps N is larger than that number. But it turns out to be quite easy, beacuse of the scoping rules. All of the variables are local to each use of each clause (a clause is one line in a definition). So, the same variable name naturally generates a new variable each time the clause is used in a computation. After all this thinking, the solution is disturbingly trivial. (The scoping rules in Prolog come directly from the way that variables work in mathematical logic. They are not some goofy invention by the language designers. The power of conventional logic derives partly from having the right scoping rules for variables.)
insert is a piece of perfectly conventional recursive programming on lists. You can test it on lists of numbers or letters or any sort of normal data objects. You just have to translate your Scheme/ML intuition for inserting into a given location in a list into the weirder Prolog notation. Steal the trick for decrementing I from the last clause of trivequ, where I decremented N for you. After decrementing I, there's one more relation to establish the recursion.
unitequiv is the most complicated, but it still just involves 4 more relations (after the ones that I already wrote for you, assuming I<J). Create a trivial equivalence of the right size (that takes two relations, since you have to reduce the size from N). Then insert the same variable into the two equivalent positions. This is a tiny bit confusing, since each insertion changes the numbering of items in the list, but it's no big deal. The assumption I<J saves you another couple of relations using max and min.
join. Hmm. There's not much that you can do with no right-hand side to the clause. There's only one thing that makes any sense. Just do it.
- I very strongly recommend that you test each new relation as you program it. The comment symbol for Gnu Prolog is
%, so just comment out the parts that you haven't done yet.
- It's possible to update information about a relation during a single interactive session in Gnu Prolog. But it's subtle. If you just reread the program (
[equivclass].), you add new information about the relations, but you don't cancel the old information. Cancelling information is straightforward in procedural and functional programs, but nobody has figured out the logically sensible way to do it in logic programs. Gnu Prolog has a somewhat goofy thing, called retract. I strongly recommend that you ignore retract. Whenever you change your program, kill the interpreter, run a fresh new session of gprolog, and read it in again. Otherwise, you'll get very confused.
Mike O'D.
|