Outline for today Logic Programming • Infix operators/declarations • Logic programming with constraints • Finite domain constraints Lecture 9: Constraint logic programming • Real/rational constraints • Course review outline James Cheney Logic Programming November 20, 2014 Defining your own Infix operators operators • Syntax of Prolog has many built-in infix • :- op(Prec, Fixity, Op). • Prec is precedence - higher is weaker binding operators • Fixity is + - * / = is =.. • xfx , xfy , yfx - infix (non, right, left assoc) • You can also define your own prefix, infix, • fx, fy - prefix or postfix operators • xf, yf - postfix • Syntax and meaning are defined • x,y indicate associativity ( x needs explicit parentheses) independently • Op can be an atom or list of atoms James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014
Looking under the hood Remember (bonnet?) • Prolog supports arithmetic, but it's not very "logical" • Standard Prolog ops declared as: :- op(1200, xfx, [ :-, --> ]). ?- 2+2 = 4. :- op(1100, xfy, [ ; ]). no :- op(1000, xfy, [ ',' ]). ?- X is 1+2. :- op( 700, xfx, [ =, is, ...]). X = 3 :- op( 500, yfx, [ +, - ]). ?- 1+2 is X. :- op( 500, fx, [ +, - ]). Instantiation error... ... James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014 Example More problems with this • Using is/2 for arithmetic, sometimes we between(Low,_,Low). have to commit to ground values too early between(Low,High,N) :- • Leads to higher branching factor Low < High, • Also imposes order of evaluation on Next is Low + 1, programs that use arithmetic between(Next, High, N). • making programs less readable or reusable ?- between(1,1000,N), N > 999. ?- N > 999, between(1,1000,N). James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014
Constraint Logic Constraint Programming Programming • Why can't we just say things like • Constraint Programming is powerful and declarative ?- X + 1 = 5 * Y, Y = 1. • But it can be a pain to use • and have the system "solve for X "? • Have to put problem in a specific syntactic form X = 4 • Wouldn't it be nicer to specify constraint • Constraint Programming is a well- problems using Prolog? studied framework that lets us do this • That's Constraint Logic Programming • (Example: Linear Programming) James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014 Basic idea Finite domain constraints • Expand the program "state" to include special predicates • N in i..j called constraints • says that N has one of finitely many values i..j • Program can generate constraints at any time • t #= u • Note: Equations t = u are a form of constraint. • equality constraint • Reduce new constraint goals to normal form • t #< u, t #> u , etc. • e.g. unification for = • • Backtrack if collection of all constraints becomes inequality constraint • These predicates constrain but don't generate inconsistent • Enumerate solutions on request or require values James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014
between revisited indomain/1 Generates solutions to constraints ?- N in 1..100, N #> 99. N in 99..100 ?- X in 1..5, Y #= 2*X+1, indomain(Y). X = 1, Y = 3 ? ; ?- N in 1..100, N #> 99, X = 2, Y = 5 ? ; indomain(N). X = 3, Y = 7 ? ; N = 100. X = 4, Y = 9 ? ; X = 5, Y = 11 ? ; James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014 minimize/2, labeling/2 maximize/2 • First argument a list of options ( [] for now) • Given a goal G , find min or max value of constrained var Y after running G • Second argument a list of constrained variables ?- X in 1..100, • Enumerates all solutions, using options to Y #= (X - 50)*X, control search. minimize(indomain(Y), Y). ?- X in 0..3, Y in 0..3, X = 25, Y = -625 X #< Y, labeling([],[X,Y]). James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014
Distinctness A cryptarithmetic puzzle • We also have inequality constraints: Goal: SEND Find distinct numbers • X #\= Y + MORE S,E,N,D,M,O,R,Y • says X and Y have to be different (both may be between 0 and 9 ------ nonground) such that MONEY • and distinctness constraints: the numbers formed by SEND and MORE • all_different([X 1 ,...,X n ]) add up to MONEY • forces all elements of list to be different James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014 Traditional solution Traditional solution solve_money( [S,E,N,D], add_carry([],[],[],0). [M,O,R,E], add_carry([A|As],[B|Bs],[C|Cs],Carry) :- [M,O,N,E,Y]) :- add_carry(As,Bs,Cs,NextCarry), between(0,9,S), ..., between(0,9,Y), C is (A + B + NextCarry) mod 10, distinct([S,E,N,D,M,O,R,Y]), Carry is (A + B + NextCarry) / 10. add_carry([0,S,E,N,D], distinct([]). [0,M,O,R,E], distinct([X|Xs]) :- \+(member(X,Xs)), [M,O,N,E,Y], 0). distinct(Xs). James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014
CLP(FD) solution CLP(FD) solution solve_money2( [S,E,N,D], add_carry2([],[],[],0). [M,O,R,E], add_carry2([A|As],[B|Bs],[C|Cs],Carry) :- [M,O,N,E,Y]) :- add_carry2(As,Bs,Cs,NextCarry), � S in 0..9, ... , Y in 0..9, C #= (A + B + NextCarry) mod 10, all_different([S,E,N,D,M,O,R,Y]), Carry #= (A + B + NextCarry) / 10. add_carry2([0,S,E,N,D], [0,M,O,R,E], Note: Almost the same except for use of [M,O,N,E,Y], 0), constraints. labeling([],[S,E,N,D,M,O,R,Y]). James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014 Other constraint Using CLP domains • Real numbers: CLP(R) • Provided as SICSTUS libraries • [library(clpfd)]. ?- { 2*X+Y =< 16, X+2*Y =< 11, • [library(clpr)]. X+3*Y =< 15, Z = 30*X+50*Y }, • [library(clpq)]. maximize(Z). X = 7.0, Y = 2.0, Z = 310.0 • Rational numbers: CLP(Q) James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014
Note: Weird SICSTUS- Review ism • Material covered in LPN, ch. 1-6: ?- X is 3/2. % exact division • Terms, variables, unification (+/- occurs check) X = 1.5 • Arithmetic expressions/evaluation ?- X is 3//2. % integer division X = 1 • Recursion, avoiding nontermination ?- X #= 3/2. % FD-constraint integer division • Programming with lists and terms X = 1 • Expect ability to solve problems similar to those ?- X #= 3//2. % error! in tutorial programming exercises (or textbook Domain error.... exercises) James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014 Review Review • Material covered in LPN, ch. 7-11: • Advanced topics (Bratko ch. 11-12, 14, 23) • Definite clause grammars • Search techniques (DFS, BFS) • Difference lists • Symbolic programming & meta-programming • Nonlogical features ("is", cut, negation, assert/retract) • Constraint logic programming • Collecting solutions (findall, bagof, setof) • Expect understanding of basic ideas • Term manipulation (var, =.., functor, arg, call) • not ability to write large programs from • Expect ability to explain concepts & use in simple scratch under time pressure Prolog programs James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014
Some exam info Learning more • Programming exam: 2 hours • There is a lot more to logic programming • Books: "The Art of Prolog", Sterling & Shapiro, MIT Press • DICE machine with SICSTUS Prolog available • Online: comp.lang.prolog • • (Documentation won't be, but exam will not Association for Logic Programming • Main journal: Theory and Practice of Logic Programming (CUP) - main rely on memorizing obscure details) journal before 2001 was Journal of Logic Programming • • Sample exams on course web page Main conferences: • International Conference on Logic Programming (ICLP) - main annual conference. • Exams from >1 year ago are on ITO web • Principles and Practice of Declarative Programming (PPDP) - covers LP and other "declarative" paradigms page; questions similar but different format. • Honors/MSc projects? Let me know James Cheney Logic Programming November 20, 2014 James Cheney Logic Programming November 20, 2014
Recommend
More recommend