Chapter 9: Constraint Logic Programming

Similar documents
INTRODUCTION TO PROLOG

Part I Logic programming paradigm

Functional Logic Programming. Kristjan Vedel

3rd CHR Summer School Topics: Introduction to Constraint Programming

Constructive negation with the well-founded semantics for CLP

Implementação de Linguagens 2016/2017

Conjunctive queries. Many computational problems are much easier for conjunctive queries than for general first-order queries.

Range Restriction for General Formulas

Knowledge Representation and Reasoning Logics for Artificial Intelligence

A second life for Prolog

Knowledge Representation and Reasoning Logics for Artificial Intelligence

Automated Reasoning. Natural Deduction in First-Order Logic

An Experimental CLP Platform for Integrity Constraints and Abduction

SAT solver of Howe & King as a logic program

Logic Languages. Hwansoo Han

Lecture 4: January 12, 2015

Multi-event IDS Categories. Introduction to Misuse Intrusion Detection Systems (IDS) Formal Specification of Intrusion Signatures and Detection Rules

Chapter 5. Pure PROLOG. Foundations of Logic Programming

COMP4418 Knowledge Representation and Reasoning

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

A Pearl on SAT Solving in Prolog (extended abstract)

Overview. CS389L: Automated Logical Reasoning. Lecture 6: First Order Logic Syntax and Semantics. Constants in First-Order Logic.

Computational Logic Introduction to Logic Programming

Syntax: Terms Arity: is the number of arguments of a structure. Constructors are represented

Constraint Solving. Systems and Internet Infrastructure Security

IDP : conga/minisat(id)

Modelling with Constraints

Logic Programming and Resolution Lecture notes for INF3170/4171

Topics in Constraint Programming: Exercises

The Logic Paradigm. Joseph Spring. 7COM1023 Programming Paradigms

CS 321 Programming Languages and Compilers. Prolog

Chapter 16. Logic Programming. Topics. Unification. Resolution. Prolog s Search Strategy. Prolog s Search Strategy

Multi-paradigm Declarative Languages

COMP2411 Lecture 20: Logic Programming Examples. (This material not in the book)

Notes for Chapter 12 Logic Programming. The AI War Basic Concepts of Logic Programming Prolog Review questions

An Improvement on Sub-Herbrand Universe Computation

Logic: TD as search, Datalog (variables)

X-KIF New Knowledge Modeling Language

arxiv: v1 [cs.pl] 15 Sep 2018

CSL105: Discrete Mathematical Structures. Ragesh Jaiswal, CSE, IIT Delhi

Lecture 1: Conjunctive Queries

Bliksem 1.10 User Manual

Artificial Intelligence. Chapters Reviews. Readings: Chapters 3-8 of Russell & Norvig.

Prolog-2 nd Lecture. Prolog Predicate - Box Model

Recursion and Induction: Haskell; Primitive Data Types; Writing Function Definitions

Formal Specification: Z Notation. CITS5501 Software Testing and Quality Assurance

Name: CSCI E-220: Artificial Intelligence Midterm Exam, Fall Term 2001

Decision Procedures in the Theory of Bit-Vectors

Introduction to Logic Programming in Prolog 1 / 39

CS558 Programming Languages

Finite State Verification. CSCE Lecture 14-02/25/2016

Constraint (Logic) Programming

A Simple and Efficient Boolean Solver for Constraint Logic Programming

Logic Programming Languages

Logic, Programming and Prolog (Supplement)

List of lectures. Lecture content. Symbolic Programming. TDDA69 Data and Program Structure Symbolic and Logic Programming Cyrille Berger

Virtual World Development

Course Summary! What have we learned and what are we expected to know?

1. true / false By a compiler we mean a program that translates to code that will run natively on some machine.

PROPOSITIONAL LOGIC (2)

Unification Algorithms

Foundations of AI. 9. Predicate Logic. Syntax and Semantics, Normal Forms, Herbrand Expansion, Resolution

Introduction to Lexical Analysis

Computing Fundamentals 2 Introduction to CafeOBJ

Integrity Constraints (Chapter 7.3) Overview. Bottom-Up. Top-Down. Integrity Constraint. Disjunctive & Negative Knowledge. Proof by Refutation

Constraint Programming. Global Constraints. Amira Zaki Prof. Dr. Thom Frühwirth. University of Ulm WS 2012/2013

Inconsistency-tolerant logics

Lee Pike. June 3, 2005

Adding Constraint Handling Rules to Curry Extended Abstract

Data Types. (with Examples In Haskell) COMP 524: Programming Languages Srinivas Krishnan March 22, 2011

Finding and Fixing Bugs in Liquid Haskell. Anish Tondwalkar

Constraints and Logic Programming

CS558 Programming Languages

arxiv:cs/ v1 [cs.lo] 14 Sep 2004

Lecture Notes on Real-world SMT

International Journal on Artificial Intelligence Tools Mode Analysis During Program Development

Some notes about Event-B and Rodin

Full Clausal Logic - Syntax: clauses

CHAPTER 8. Copyright Cengage Learning. All rights reserved.

LOGIC AND DISCRETE MATHEMATICS

Introduction to Lexical Analysis

Chapter 10 Part 1: Reduction

Software Verification : Introduction

Constraint Programming

Introduction to predicate calculus

Prolog. Artificial Intelligence. Lecture 2 Karim Bouzoubaa

Module 6. Knowledge Representation and Logic (First Order Logic) Version 2 CSE IIT, Kharagpur

1 Inference for Boolean theories

Course on Artificial Intelligence and Intelligent Systems. A short introduction to CHR and its application for rule-based expert systems

HANDBOOK OF LOGIC IN ARTIFICIAL INTELLIGENCE AND LOGIC PROGRAMMING

Chapter 16. Logic Programming Languages ISBN

Chapter 3: Pure Prolog

Discrete Mathematics Lecture 4. Harper Langston New York University

BITCOIN MINING IN A SAT FRAMEWORK

CODE ANALYSIS CARPENTRY

Programming Languages Third Edition

Concepts of programming languages

Minimum Satisfying Assignments for SMT. Işıl Dillig, Tom Dillig Ken McMillan Alex Aiken College of William & Mary Microsoft Research Stanford U.

CMSC 331 Final Exam Section 0201 December 18, 2000

Prolog Cut: Summary. CSC324 Logic & Relational Programming Illustrated in Prolog. Prolog Cut: Summary. Non-deterministic Programming

Transcription:

9. Constraint Logic Programming 9-1 Deductive Databases and Logic Programming (Winter 2007/2008) Chapter 9: Constraint Logic Programming Introduction, Examples Basic Query Evaluation Finite Domain Constraint Solver

9. Constraint Logic Programming 9-2 Introduction (1) Constraint logic programming (CLP) extends standard logic programming by constraints, which can in principle be any kind of logical formulae. A concrete system permits only a subset of formulae, because it needs a constraint solver that can check a set of constraints for consistency, Sometimes incomplete constraint solvers are used, which are not able to detect all inconsistencies. simplify a set of constraints, so that it can be presented to the user as an answer. Optimal would be Variable = Value, but this is not always possible.

9. Constraint Logic Programming 9-3 Introduction (2) Normally, the constraints are simply literals with special predicates. The semantics of these predicates is not defined by rules, but by a logical constraint theory. The semantics is implemented or approximated by the constraint solver in the system. Constraint Handling Rules (CHR) is an approach to define constraint solvers by special rules. However, these look quite different from the usual logic programming rules.

9. Constraint Logic Programming 9-4 Introduction (3) The function symbols that can be used for term construction in constraint literals is limited: It must be evaluable functions which the constraint solver knows (e.g., +, -, *, /). Variables in constraints have a specific domain (e.g., integers). They cannot be bound to arbitrary terms. Note the difference between The value of variable X is the term Y+3. The value of variable X is an integer, at the moment it is known that X = Y+3.

9. Constraint Logic Programming 9-5 Introduction (4) For instance, the following logic program would work in the Ciao System with the package CLP(R) for constraint programming over reals: :- use_package(clpr). p(x,y) :- X.=. Y * 3, q(x,y). q(x,y) :- X - 2.=. Y. Ciao can be downloaded from [http://www.ciaohome.org/]. The operator.=. means simply =, but that symbol is used for unification already. For the query p(x,y), one gets the answer Y.=.1.0, X.=.3.0 (system of linear equations was solved).

9. Constraint Logic Programming 9-6 Introduction (5) In SWI-Prolog, the same example looks as follows: :- use_module(library(clpr)). p(x,y) :- {X = Y * 3}, q(x,y). q(x,y) :- {X - 2 = Y}. In the Debian/Ubuntu Linux distribution, this constraint solver is missing (probably a bug). The Windows distribution contains it. Under Linux, one can put the files to /usr/lib/swi-prolog/library/clp. Here, constraints are marked with {...}. This shows that working with constraints is not very portable. In ECLiPSe, e.g. the operator $= is used and type declarations for the variables are important.

9. Constraint Logic Programming 9-7 Introduction (6) For comparison, consider the logic program p(x,y) :- X = Y * 3, q(x,y). q(x,y) :- X - 2 = Y. Here, the answer is no : The call to q is q(y*3,y). Then it is required to unify (Y*3)-2 and Y. This only fails if the system uses an occur check. If the second equation were written X = Y+2, it would fail in any Prolog system. If one uses is instead of =, one would get an instantiation fault. is requires that all variables occurring on the right hand side are bound to an arithmetic expression.

9. Constraint Logic Programming 9-8 Basic Query Evaluation (1) Query evaluation works as SLD-resolution, however besides the current goal, there is also a current set of constraints. If the first/selected literal in the current goal is a constraint, it is moved to the current set of constraints. The current set of constaints is then checked for consistency (with the constraint solver). If it is inconsistent, the current derivation fails. Since one typically searches the derivation tree depth first, backtracking to the last untried choice point will then occur.

9. Constraint Logic Programming 9-9 Basic Query Evaluation (2) If the first/selected literal is a normal literal (not a constraint), a usual SLD-step is done, however Instead of unifying the head p(t 1,..., t n ) of a program clause (with fresh variables) with the selected literal p(u 1,..., u n ), one adds the constraints u 1 = t 1,..., u n = t n to the constraint store. Furthermore, as usual, the body of the clause is inserted into the goal (replacing the selected/calling literal). In this way, unification can be seen as a very specific constraint solver.

9. Constraint Logic Programming 9-10 Basic Query Evaluation (3) The constraint solver may also transform the current set of constraints into an equivalent one (simplify them, e.g. solve equations). This will typically occur when a constraint is added. A derivation is successful when it ends in an empty goal and a satisfiable constraint store. Instead of an answer substitution, the contents of the constraint store will then be printed. Possibly restricted to the variables that occurred in the query.

9. Constraint Logic Programming 9-11 Delaying Conditions (1) One specific feature of CLP is that constraints are usually not immediately evaluated, but delayed until more information becomes available about the variables occurring in them. For instance, consider the program: p(x) :- X.<. 5, q(x). q(x) :- r(x,y). r(7,y) :- very_complex_calculation(y). The condition X.<. 5 cannot be evaluated at the point where it is called.

9. Constraint Logic Programming 9-12 Delaying Conditions (2) The constraint X.<. 5 waits in the background and as soon as X is bound to 7, it causes the failure. In Prolog (with < instead of.<.) one would get an instantiation fault. One would have to move X < 5 after the call q(x), but then the very_complex_calculation is done, and the failure is detected only afterwards. This gives logic programming a kind of coroutining.

9. Constraint Logic Programming 9-13 Unification vs. Equations (1) The above example also explains why unification is conceptually replaced by solving special equations: The variable X gets the value 7 by a unification step, and the constraint solver must react. This does not necessarily mean that when a CLP system executes a standard logic program, the implementation is different. Actually, most modern Prolog systems have CLP libraries.

9. Constraint Logic Programming 9-14 Unification vs. Equations (2) However, the following example does not work: p(x,y) :- X.=. Y * 3, q(x,y). q(z+2,z). Formally, one gets the system of equations X = Y * 3, X = Z + 2, Y = Z. This could be solved with solution X=3, Y=1, Z=1. But in SWI-Prolog and Ciao, one gets a kind of instantiation fault. Unification introduces equations with =, while the constraint solver mainly works with.=.. A problem might also be to determine whether Z is a standard Prolog variable or a numerical one.

9. Constraint Logic Programming 9-15 Finite Domain Constraints (1) A finite domain solver keeps for each variable a finite domain of values, usually an interval of integers (or union of such intervals). For the CLP-FD Solver in SWI-Prolog, one declares a domain for a variable e.g. with X in 0..9 It is also possible to do this for a list of variables, e.g. [X, Y, Z] ins 0..100

9. Constraint Logic Programming 9-16 Finite Domain Constraints (2) Equality for the CLP-FD solver is written #=. Note that the constraints are not written in {..}. Besides #=, the solver knows e.g. about #<, #<=, #>=, #>, #\=. Boolean values are treated as 0 and 1, and then boolean connections #\ (not), #\/ (or), #/\ (and), #<==> (iff), #==> (then), #<== (if) can be used. The expressions can contain +, -, *, /, mod, rem, abs, min, max, ^. One can ask the solver to try all possible values for a variable (or list of variables) one by one with label(x) When this variable has a concrete value, often the constraints entail values for other variables.

9. Constraint Logic Programming 9-17 Finite Domain Constraints (3) A well-known puzzle is S E N D + M O R E M O N E Y The task is to find digits for the letters (all different, S and M not zero) such that this addition gives the correct result. A solution can easily be found with a finite domain solver. One only needs a formal specification of the problem as shown on the next slide.

9. Constraint Logic Programming 9-18 Finite Domain Constraints (4) Puzzle in SWI-Prolog: :- use_module(library(clpfd)). puzzle([s,e,n,d],[m,o,r,e],[m,o,n,e,y]) :- Vars = [S,E,N,D,M,O,R,Y], Vars ins 0..9, all_different(vars), S * 1000 + E * 100 + N * 10 + D + M * 1000 + O * 100 + R * 10 + E #= M * 10000 + O * 1000 + N * 100 + E * 10 + Y, M #\= 0, S #\= 0. Example from [http://www.swi-prolog.org/man/clpfd.html] (with one small modification in the head).

9. Constraint Logic Programming 9-19 Finite Domain Constraints (5) To solve the puzzle, call puzzle(a, B, C), label(a). Of course, one could call label also for the other variable lists, but that is not necessary. The constraint solver can uniquely determine their values once it has assigned concrete values to the variables in A. CLP can also be used to make functions with arithmetic invertible: :- use_module(library(clpfd)). fac(0, 1). fac(n, F) :- N #> 0, N1 #= N-1, F #= N*F1, fac(n1, F1).