Lutz, Charles D wrote: > To be honest, the outputs of both of these variants are not totally > clear to me. First off, > > what does it “mean” to have “f(a, X)” inside of the head clause? Apart > from saying p1 > > or p2 is a clause of arity 1, whose argument is another clause of arity > 2, what is the > > “practical” meaning of it? We have to be careful with the terminology: - we have here two _predicates_ (p1/1 and p2/1), i.e. things that can be true of false. - each predicate is defined by (one or more) _clauses_. Here, we only have one clause each for p1/1 and p2/1. Since there are no clauses for f/2, there is no predicate f/2, i.e. it doesn't make sense to ask whether f/2 is true or false. f(a,X) is therefore simply a data structure, more precisely a compound term with two subterms. Now, a clause like p1(f(a,X)) :- ... means that it provides a definition for the truth of p1/1, but it applies only to the case that the argument of p1/1 is a data structure of the form f(a,X). Incidentally, this is equivalent to having written p1(Arg) :- Arg = f(a,X), ... To understand what happens when you ask the query (your example 24) ?- p1(f(a,b)). in the presence of the definition: p1(Arg) :- Arg = f(a,X), writeln(X). you can simply "macro-expand" the definition, by substituting in your query the left-hand side of the defining clause with the right hand side, and filling in the actual value for Arg. This gives ?- f(a,b) = f(a,X), writeln(X). and this should explain why it writes "b". Now look at your query 23, whose expansion yields ?- f(A,b) = f(a,X) , writeln(X). This still writes "b", but since unification (=) works both ways, it also binds the variable A to a. The "matching clauses" work the same, except that they use a one-way variant of unification, which only allows variables on the right hand side to be bound (and fails otherwise). Apart from the matching clauses (which are ECLiPSe-specific), this is standard Prolog mechanism which you can find in quite compact form in e.g. the text book like Bratko, or in the ECLiPSe book by Apt/Wallace, or have a look at some of the online resources at www.eclipse-clp.org/links.html It would be great to beef up the ECLiPSe tutorial and make it more self contained. If someone feels inspired, the sources are available at SourceForge! Best regards, JoachimReceived on Wed May 09 2007 - 12:42:48 CEST
This archive was generated by hypermail 2.3.0 : Wed Sep 25 2024 - 15:13:20 CEST