COEN 171 Logic Programming Logic programming and predicate
- Slides: 30
COEN 171 - Logic Programming · · · · · Logic programming and predicate calculus Prolog statements Facts and rules Matching Subgoals and backtracking List operations Arithmetic Controlling backtracking Running Prolog N queens hints (9. 1)
Logic Programming (9. 2) · Logic programming – uses a form of symbolic (mathematical) logic as programming language » declarative language – imperative and functional languages allow programs that describe how to compute a solution – declarative languages allow programs that describe facts about the problem domain and the form of the result (what, not how) » how to achieve that result is left up to the system
Predicate Calculus · Predicate calculus is one form of symbolic logic · Propositions consist of – atomic propositions consist of compound terms – compound terms have a functor (name or relation) and arguments » professor (ron) » nieces (heidi, sasha, anna) » professor (Z), where Z is a variable – compound propositions consist of 2 or more atomic propositions joined by logical connectors (9. 3)
Predicate Calculus (continued) (9. 4) · A clausal form is a standard way of writing propositions
Predicate Calculus (continued) · One wants to infer things from propositions · Can do so using resolution techniques (9. 5)
Predicate Calculus (continued) (9. 6) · Unification is the process of selecting values for variables in clauses – “instantiating” variables · With resolution, can use a restricted kind of clause called Horn clauses – nothing on the left side (no implication, either) » niece (anna, ron) % state facts – single atomic proposition on the left side (“headed” Horn clause
(9. 7) Prolog · Prolog is the most prominent example of a declarative language · Prolog uses resolution and unification on Horn clauses · Prolog statements consist of terms – constants » atom (any string of letters, digits, and _ starting with a lower case letter, or any printable string in single quotes) • anna • miss_JONES • ‘South America’ » numbers (integers or reals, usually integers) – variables » any string of letters, digits, and _ starting with an upper case letter or an _ • X • List_2 • _
(9. 8) Prolog (continued) · Prolog statements (continued) – structures » functor and arguments, which may also be structures • professor(ron) • date(4, may, 1995) • date (Day, may, 1995) » defined by a name (functor) and arity (number of arguments) » so date (4, may, 1995) and date (124, 1995) are different » think of these as trees date 4 may 1995
(9. 9) Facts and Rules · Prolog programs consist of facts and rules – facts are always true. they are “unheaded” Horn clauses » they need not be actually true in real life, but are in the Prolog universe – big (bear). % NOTICE “. ” – small (cat). – big (mouse). – mother (ron, eva). – rules are full (“headed”) Horn clauses » head is a single term » antecedent is a single term or a series of terms joined by and – – – brother (X, Y) : - male (X), parents (X, M, F), parents (Y, M, F). » can also join with “; ” meaning or % “, ” means % and
Matching (9. 10) · One of the most important operations to do on terms is matching – two terms match if » they are identical » the variables in both terms can be instantiated to objects so that the terms become identical – so date (D, M, 1981) and date (D 1, july, Y 1) match with the instantiations » D = D 1 » M = july » Y 1 - 1981 – we say matching succeeds if two terms match, otherwise it fails
(9. 11) Matching (continued) · The formal matching rules are – if S and T are constants, then they match only if they’re the same object – if S is a variable and T is anything, they match and S is instantiated to T. Conversely, if T is a variable, then T is instantiated to S. – if S and T are structures, they match only if » S and T have the same principal functor » all their corresponding components match – triangle(point(1, 1), A, point(2, 3)) – triangle(X, point(4, Y), point(2, Z)) triangle point 1 1 triangle A point 2 X 3 point 4 point Y 2 Z
Subgoals and Backtracking (9. 12) · Prolog operates by starting with a database of facts and rules · Then the interpreter is given a goal – Prolog searches the database trying to match the goal against a fact or the LHS of a rule – if match fact » done – if match LHS of rule » RHS of rule becomes set of subgoals, try to match subgoals in turn – if fail, back up to immediately preceding match, try to satisfy the goal that matched with a different match – subgoals are always satisfied in left-to-right order – database is always searched beginning to end » physical layout of facts and rules determines order of processing
Subgoals and Backtracking (continued) · Example (9. 13)
Subgoals and Backtracking (continued) Control Flow call model fail Goal 1 succeed call redo fail Goal 2 succeed redo · Failure causes control to return to previous goal (redo) · Success causes invocation of next goal (9. 14)
Backtracking Performance · Reordering the clauses and goals in the database can have a significant impact on the performance of a program – consider the following examples (9. 15)
Backtracking Performance (continued) The same logical program, with 4 different orderings of statements (9. 16)
Backtracking Performance (continued) (9. 17)
Backtracking Performance (continued) (9. 18)
Backtracking Performance (continued) (9. 19)
Backtracking Performance (continued) (9. 20)
(9. 21) List Operations · Lists are one of the basic data structures of Prolog – the other is structures, which are equivalent to records · Lists are represented in programs, and printed out, as a sequence of items in [ ] – [ ann, tom, tennis, skiing ] head tail ann tom tennis skiing []
List Operations (continued) (9. 22) · Prolog provides a notation to separate head and tail of a list – [ head | tail ] » [ ann | [ tom, tennis, skiing] ] · Some useful list operations » not built in, but some are in libraries distributed with various Prologs – member (X, L) succeeds if X is in L » member (X, [X | Tail] ). » member (X, [ Head | Tail ] ) : - member (X, Tail). – conc (L 1, L 2, L 3) succeeds if L 3 = L 1 concatenated with L 2 » and returns L 3 = L 1 cat L 2 » conc ([], L, L). » conc ( [X | L 1], L 2, [X | L 3] ) : - conc (L 1, L 2, L 3). • conc ( [a, b, c], [1, 2, 3], L). • L = [a, b, c, 1, 2, 3]
List Operations (continued) (9. 23) · Useful operations (continued) – add (X, L, [X | L] ). % or just [X | L] – del (X, L, L 1) deletes one occurrence of X from L giving L 1 » del (X, [X | Tail], Tail ). » del (X, [Y | Tail], [Y | Tail 1] ) : - del (X, Tail 1). input goal interpreter response • ? - del (a, [a, b, a, a], L). • L = [b, a, a] ; • L = [a, b, a] ; • no ; typed by user causes rematch as if goal had failed
Arithmetic (9. 24) · In Prolog, arithmetic is performed by built-in predicates that take arithmetic expressions as arguments and evaluate them · arithmetic expressions (ae) consist of numbers, variables, and arithmetic functors – arithmetic functors are » X + Y, X - Y, X * Y, X / Y (real division), X // Y (integer division), X mod Y, -X » variable must be bound to a non-variable expression at evaluation time · To evaluate an ae, pass as argument to one of these predicates – Z is X (assignment statement), X =: = Y (numeric equality test), X == Y (not equal), X < Y, X > Y, X =< Y, X >= Y
Arithmetic (continued) (9. 25) · Examples – suppose database with facts born (name, yearborn). Can retrieve anyone born between 1950 and 1960 by » ? - born (Name, Year), Year >= 1950, Year =< 1960. – find the length (number of top level nodes) of a list » length ( [], 0). » length ( [X | Tail], N ) : - length (Tail, N 1), N is N 1 + 1. » ? - length ( [a, b, [ c, d ], e ], N). » N=4 · It’s important to differentiate between X = Y (which tries to match X and Y) and X =: = Y, which tests numeric equality » » » ? - 1+2 =: = 2 + 1. yes ? - 1 + 2 = 2 + 1 no ? - 1 + A = B + 2 %matches and A set to 2, B set to 1
Controlling Backtracking (9. 26) · Another way to control efficiency in a Prolog program is to directly control the backtracking mechanism · Means to do this called the cut (denoted !) – cut is a subgoal that is always satisfied, but backtracking can’t pass through » freezes choices made to that point – C : - P, Q, R, !, S, T, U. – C : - V. – Suppose try to satisfy rule A : - B, C, D. » satisfy B, try to satisfy C, match first LHS, try to satisfy P, Q, R, S, T, U » backtracking works freely when trying to satisfy P, Q or R » once past !, backtracking works freely trying to satisfy S, T or U » if S eventually fails, won’t backtrack to try other alternatives for P, Q, R, which means C fails. Also won’t try rule C : - V.
Controlling Backtracking (continued) (9. 27) · Example – max (first, second, larger) » max (X, Y, X) : - X >= Y. » max (X, Y, Y). – if first rule succeeds, never have to check second, so add cut » max (X, Y, X) : - X >= Y, !. » max (X, Y, Y). – important if max is one of several subgoals » foo (A, B) : - max (A, B, Large), » bar (Large). – if max is satisfied by A >= B and bar fails, no point in trying to satisfy max again.
N Queens Hints · One way to start this is to pass in a list of the row positions, with variables for each of the columns – variables get instantiated while executing – add rule » board ([1/C 1, 2/C 2, . . . , 8/C 8]). – then to invoke your program » |? - board(Soln), nqueens(Soln). » Soln = [1/4, 2/2, . . . , 8/1] » type ; to find more than one solution (9. 28)
(9. 29) Running Prolog · Log onto workstation in design center and type Prolog at system prompt. · The interpreter provides a top level prompt |? – load a file » |? - [‘filename’]. – allow clauses to be entered directly from terminal » » |? - [user] | | ^D |? - %no way to save - for play only %control D returns to top level – to interrupt execution » |? - foo (X). » ^C » Prolog interrupt - press h for help – to exit » |? - ^D %gets menu of choices
Running Prolog (continued) (9. 30) · debugging – |? - trace. » starts extensive tracing mode » Enter key single steps » |? - notrace. turns off trace – |? - spy (rulename). » traces only rule » |? - spy (member). » |? - nospy. turns of spy · Use Unix script command to capture screen output to hand in
- Predicate adjective and predicate nominative
- Predicate nouns and predicate adjectives
- Predicate nominative and adjective
- Diagram predicate adjective
- The gardener predicate
- Predicate nominatives and predicate adjectives
- Simple subject definition
- Coen311
- Coen 311 concordia
- Lab 3: smtp lab
- Coen 352
- Coen 6501
- Coen 445
- Coen 445
- Pg 171
- Lei 171 de 2007
- The verbs salir decir and venir p.155
- Nonlinear pricing example
- Ai 171
- Ai 171
- 171 meaning
- 171 nomreli mekteb
- 171 nomreli mekteb
- Decret 171/2015
- Dot
- Cs 171 uci
- 171 in binary
- 171 sayli mekteb
- 171 nomreli mekteb
- Predicate logic rules of inference
- Current king of france