INTRODUCTORY PROLOG MUSINGS Intro: SWI Prolog listserv stupid tradition of single-char names... FACTS likes(joe, sue). -- lower case for objects, individuals -- syntax: relnship (the predicate) looks like function, objs like args -- . is important -- no space after likes -- write in a file that must be read -- 'consulted'. ?- consult(facts) ?- [facts, morefacts] % facts.pl in directory ?- ['quoted_name'] % same as w/o quotes. ?- ["dbl_quote"] % does NOT work TOP LEVEL: Questions with loves(john,fred). ?- loves(john, fred). true. ?- lovs(john, fred). Correct to: "loves(john, fred)"? Please answer 'y' or 'n'? yes true. ?- likes(john, fred) % oops! need to end with . ?- likes(john, fred)? % oops again! need to end with . | [forced] Action (h for help) ? Options: a: abort b: break c: continue e: exit g: goals t: trace h (?): help [forced] Action (h for help) ? abort ERROR: Execution Aborted % Execution Aborted ?- likes(john,fred). likes(john,fred) . ERROR: toplevel: Undefined procedure: likes/2 (DWIM could not correct goal) VARIABLES: start with capital. instantiated or not. ?- loves(X, fred). X = john. Multiple answers, type ; after an answer to see if any more. Note searching going on Conjunctions: ?- loves(john, X), loves(X,ava). does john love anyone who loves ava? body goals satisfied in order left to right. Implies backtracking search. *** RULES: also must be consulted Head :- body (of goals) ; in a rule means "or". , in a rule means "and". X is a sister of Y if: X female and X, Y have same parents. sister_of(X,Y) :- female(X), parents(X,M,F), parents(Y,M,F). (can break lines if like). STRUCTURES: like structs, records... an object consisting of collection of other objects, its components. functor(components) name (like a datatype or a typedef) occur inside facts: owns(john, book(cujo,author(king, steven))). look like facts: in fact the predicate IS the functor of a structure., and the args of fact or rule are components of a strucure. So prolog programs are structures. Prolog basic objects are are constants, variables, and structures. OPERATORS: can be infix versions of predicates or in general functors (the name for the predicate, structure name, whatever goes before (. can be defined by user. Example is = (which is NOT assignment). UNIFICATION and EQUALITY 2-way matching. Instantiates variables, and then the same variable has same value in entire body and head. So in search, if find ask (sister_of(sue, joe), then X gets sue and Y gets joe: then if parents(sue, emma, fred) is found, then M gets emma and F gets fred. meaning the program is now looking for parents(joe,emma,fred) to satisfy 2nd goal in body. ?- X = Y. is a goal satisfied by a match between X and Y. It's defined by the fact X=X. X uninstantiated and Y is to some term, X and Y are equal and X is now instantiated to what Y is. integers and atoms are = themselves abc = abc succeeds abc=xyz fails. structures are = if same functor and number of components and the corresponding components are equal. ?- a(b,C,d(e,F,g(h,i,J))) = a(B,c,d(E,f,g(H,i,j))). C = c, F = f, J = j, B = b, E = e, H = h. ARITHMETIC predicates X=:=Y, =\=, <,>,=<, >= is density(X,Y) :- pop(X,P), area(X,A), Y is P / A.