CS 342 Lecture -*- Outline -*- * Pure Prolog ** Syntax (8.2.1, p. 354) *** Case sensitive: variables begin with captial letter *** clauses are either facts or rules ---------- ;rules: (infer (good X) from (logical X) (simple X)) (infer (logical Y) from (scientific Y)) ;facts: (infer (good prolog)) (infer (huge jupiter)) ----------- **** goals, either names or expressions ------------ constant Var or (Var) (less 3 Z) ------------ names beginning with a capital are variables. Var MeToo names beginning with lower case letter are constants (symbols) constant aSym compare with ' in LISP **** compound terms (expression): functor + arguments functor like a noun, these are the data structures of Prolog -------------- (tree Subtree1 Root Subtree2) (cons a (cons b nil)) -------------- could also use other notation: e.g., (. a (. b [])) ---------------- (sentence (np (n ron)) (vp (v gave) (np (art a) (n paper)) (compls (prep to) (np (n sue))))) ------------------ Prolog doesn't understand ths (p. 355) **** scope of variables limited to a clause ---------------- (infer (understands L X) from (groks terms L X) (groks syntax L X) (groks semantics L X)) ---------------- scope of L and X is just this clause! ** Examples *** assertions ------------- ; a semantic network (infer (object event1 paper)) (infer (recipeint event1 sue)) (infer (actor event1 ron)) (infer (action event1 gave)) ------------- *** queries -------------- ;was the object of event1 a paper? -> (infer? (object event1 paper)) Satisfied ; what was given in event1 -> (infer? (object event1 What) > (print What)) paper Satisfied -> (infer? (actor Event Who) > (action Event gave) > (print Who Event)) ron event1 Satisfied -> (infer? (actor X Y) > (action X Z) (print X Y Z)) event1 ron gave Satisfied ------------- Get them to read the last 2 ** Interpretations of clauses (8.2.2) *** logical (declarative) p. 355 -------------- (infer (g X Y) from (h1 Y) (h2 X)) means for all X, Y: if (h1 Y) and (h2 X) then (g X Y) -------------- to solve problem of form (g X Y), need to solve {h1,h2}. logically it's an implication for a knowlege-based system, it's an if-then rule Special case of this is facts: -------------- (infer (f X)) means for all X: (f X) is true -------------- logically, an axiom knowledge-based system, a fact ; program for optimists (infer (good X)) **** example: member on p. 354 ------------ (infer (member X (cons X L))) (infer (member X (cons Y M)) from (member X M)) ------------ (infer? (member 3 (cons 3 nil))) Satisfied ; by first rule (infer? (member 3 (cons 7 (cons 3 nil)))) Satisfied ; by second rule ***** running code backwards (infer? (member 3 M) (print M)) (cons 3 L1) Satisfied used the first rule ***** closed world assumption (infer? (member 3 nil)) Not satisfied give a "def" **** other examples, p 365 when they seem to be asking "how does it work?", go on to the next section, perhaps coming back stress the logical interpretation on page 365ff... e.g., addtoend, append show backwards, be sure to do queries of each. can define lists! ---------------- (infer (list nil)) (infer (list (cons X L)) from (list L)) (infer? (list (cons 1 (cons 2 nil)))) ---------------- variation on addtoend ---------------- (infer (equals (addtoend nil X) (cons X nil))) (infer (equals (addtoend (cons Y L) X) (cons Y M)) from (equals (addtoend L X) M)) (infer? (equals (addtoend (cons 3 nil) 4) L) (print L)) ---------------- *** Procedural reading (programming language) p. 357 first version, for ground rules (no variables) -------------- C1 C2 C3 (infer? goal) means try to satisfy goal by C1, then by C2, ... (infer g from h1 h2) means to satisfy g, first do h1, then h2 -------------- goal g identifies form of call it can answer, body "h1 h2" is an ordered list of procedure calls. must satisfy them all (recursively) example from page 357 -------------- (infer imokay from youreokay hesokay) ; C1 (infer yourokay from theyreokay) ; C2 (infer hesokay) ; C3 (infer theyreokay) ; C4 (infer? imokay) -------------- draw time vs. stack diagram always start from the top of the program -------------- (infer hesnotokay from imnotokay) ; C5 (infer shesokay from hesnotokay) ; C6 (infer shesokay from theyreokay) ; C7 (infer? shesokay) -------------- for this one, the first procedural rule isn't sufficient so use 2nd rule, p. 358 (backtracking) Note that order matters, whereas logically it shouldn't -------------- (infer hesnotokay from shesokay) ; C8 (infer hesnotokay from imokay) ; C9 (infer? shesokay) -------------- it was true before... this is how Prolog really works, so logic is the intent, but not quite achieved *** both at once is great insight of logic programming