Prolog Programming in Logic ===================== Software: SWI-Prolog Windows: www.cs.fit.edu/~pkc/classes/ai/ Linux/unix: pl Loading source files: .pl []. or consult(). Exit: halt. ===================== Syntax: - Predicates/functions and constants start with a lowercase letter - Variables start with an uppercase letter - Universally qunatified Horn sentences. - "Facts" and "Rules" - "Facts" before "Rules" in the source file - Must end with a period (.) - Facts: single predicate (no atecendents in Horn sentences), no variables. - Rules: consequent :- antecedent1, antecedent2, ..., antecedent_n. - "Head:" consequent; "Body:" antecedents - Queries return "yes" or "no" - If the queries have variables, queries return the variable bindings - One variable binding is displayed, addtional ones, if present, are displayed one by one when the user types a semicolon (;) - comments start with a percent sign (%), or are delimited by /* */ ===================== Sample source file % facts parent(mary, tom). parent(mary, liz). parent(tom, bob). % rules % Parent(x,z) ^ Parent(z,y) => Grandparent(x,y) grandparent(X, Y) :- parent(X, Z), parent(Z, Y). % Parent(x,y) => Ancestor(x,y) % Parent(x,z) ^ Ancestor(z,y) => Ancestor(x,y) ancestor(X, Y) :- parent(X, Y). ancestor(X, Y) :- parent(X, Z), ancestor(Z, Y). ===================== Sample queries ?- parent(mary, tom). yes ?- parent(mary, X). X=tom; X=liz ?- grandparent(mary, bob). yes ?- grandparent(mary, X). X=bob ?- ancestor(mary, X). yes ======================== - Starting from the query, perform depth first search - search ordering in the body (antecedents): left to right - Generally, start with the most restrictive predicate in the body