Download presentation

Presentation is loading. Please wait.

Published byChasity Tindle Modified about 1 year ago

1
1 Chapter 4:Constraint Logic Programs Where we learn about the only programming concept rules, and how programs execute

2
2 Constraint Logic Programs u User-Defined Constraints u Programming with Rules u Evaluation u Derivation Trees and Finite Failure u Goal Evaluation u Simplified Derivation Trees u The CLP Scheme

3
3 User-Defined Constraints u Many examples of modelling can be partitioned into two parts u a general description of the object or process u and specific information about the situation at hand u The programmer should be able to define their own problem specific constraints u Rules enable this

4
4 Rules A user defined constraint to define the model of the simple circuit: parallel_resistors(V,I,R1,R2) parallel_resistors(V,I,R1,R2) :- V = I1 * R1, V = I2 * R2, I1 + I2 = I. And the rule defining it

5
5 Using Rules parallel_resistors(V,I,R1,R2) :- V = I1 * R1, V = I2 * R2, I1 + I2 = I. Behaviour with resistors of 10 and 5 Ohms Behaviour with 10V battery where resistors are the same It represents the constraint (macro replacement)

6
6 User-Defined Constraints u user-defined constraint: p(t1,...,tn) where p is an n-ary predicate and t1,...,tn are expressions u literal: a prim. or user-defined constraint u goal: a sequence of literals L1,...,Lm u rule: A :- B where A is a user-defined constraint and B a goal u program: a sequence of rules

7
7 Its not macro replacement! parallel_resistors(VA,IA,10,5), parallel_resistors(VB,IB,8,3), VA + VB = V, I = IB, I = IA Imagine two uses of parallel resistors After macro replacement (converting comma to conj) Confused the two sets of local variables I1, I2

8
8 Renamings u A renaming r is a bijective (invertable) mapping of variables to variables u A syntactic object is a constraint, user- defined constraint, goal or rule u Applying a renaming to a syntactic object gives the object with each variable x replaced by r(x) u variant o’ of object o has renaming r(o’)=o

9
9 Rewriting User-Defined Cons. u goal G of the form (or empty m=0 []) u L 1,..., L i-1, L i, L i+1,..., L m u L i is of the form p(t 1,...,t n ) u R is of the form p(s 1,...,s n ) :- B u r is a renaming s.t. vars in r(R) not in G u The rewriting of G at Li by R using renaming r is u L 1,...,L i-1,t 1 =r(s 1 ),...,t n =r(s n ),r(B),L i+1,...,L m

10
10 Rewriting Example parallel_resistors(VA,IA,10,5), parallel_resistors(VB,IB,8,3), VA + VB = V, I = IB, I = IA Rewrite the first literal with rule parallel_resistors(V,I,R1,R2) :- V = I1 * R1, V = I2 * R2, I1 + I2 = I. Renaming: parallel_resistors(V’,I’,R1’,R2’) :- V’ = I1’*R1’, V’ = I2’*R2’, I1’+I2’ = I’. VA=V’, IA=I’, 10=R1’, 5=R2’, V’ = I1’*R1’, V’ = I2’*R2’, I1’+I2’ = I’, parallel_resistors(VB,IB,8,3), VA + VB = V, I = IB, I = IA Rewrite the 8th literal Renaming: parallel_resistors(V’’,I’’,R1’’,R2’’) :- V’’=I1’’*R1’’, V’’=I2’’*R2’’, I1’’+I2’’=I’’. VA=V’, IA=I’, 10=R1’, 5=R2’, V’ = I1’*R1’, V’ = I2’*R2’, I1’+I2’ = I’, VB=V’’, IB=I’’, 8=R1’’, 3=R2’’, V’’=I1’’*R1’’, V’’=I2’’*R2’’, I1’’+I2’’=I’’ VA + VB = V, I = IB, I = IA Simplifying onto the variables of interest V and I

11
11 Programming with Rules A voltage divider circuit, where cell must be 9 or 12V resistors 5,9 or 14 voltage_divider(V,I,R1,R2,VD,ID) :- V1 = I*R1, VD= I2*R2, V = V1+VD, I = I2+ID. cell(9). (shorthand for cell(9) :- [].) cell(12). resistor(5). resistor(9). resistor(14).

12
12 Programming with Rules Aim: find component values such that the divider voltage VD is between 5.4 and 5.5 V when the divider current ID is 0.1A voltage_divider(V,I,R1,R2,VD,ID), 5.4 <= VD, VD <= 5.5, ID = 0.1, cell(V), resistor(R1), resistor(R2). Note: when rewriting cell and resistor literals there is a choice of which rule to use (V=9,R1=5,R2=5) unsatisfiable constraint (V=9,R1=5,R2=9) satisfiable constraint

13
13 Programming with Rules Consider the factorial function, how do we write rules for a predicate fac(N,F) where F = N! (R1) fac(0,1). (R2) fac(N,N*F) :- N >= 1, fac(N-1, F). Note how the definition is recursive (in terms of itself) and mimics the mathematical definition

14
14 Programming with Rules (R1) fac(0,1). (R2) fac(N,N*F) :- N >= 1, fac(N-1, F). Rewriting the goal fac(2,X) (i.e. what is 2!) Simplified onto variable X, then answer X = 2 Different rewriting: the constraints are unsatisfiable

15
15 Evaluation u In each rewriting step we should check that the conjunction of primitive constraints is satisfiable u derivation does this u in each step a literal is handled u primitive constraints: added to constraint store u user-defined constraints: rewritten

16
16 Evaluation u derivation for : u where each to is a derivation step u derivation for G is a derivation for the state

17
17 Evaluation u state: where G 1 is a goal and C 1 is a constraint u derivation step: G 1 is L 1, L 2,..., L m u L 1 is a primitive constraint, C 2 is C 1 /\ L 1 u if solv(C /\ L 1 ) = false then G 2 = [] u else G 2 = L 2,..., L m u L 1 is a user-defined constraint, C 2 is C 1 and G 2 is the rewriting of G 1 at L 1 using some rule and renaming

18
18 Derivation for Derivation for fac(1,Y) Corresponding answer simplified to Y is Y = 1

19
19 Derivation for Derivation for fac(1,Y) A failed derivation for fac(1,Y)

20
20 Derivations u For derivation beginning at u success state: where solv(C) != false u successful derivation: last state is success u answer: simpl(C, vars( )) u fail state: where solv(C) = false u failed derivation: last state is fail state

21
21 Derivation Trees u derivation tree for goal G u root is u the children of each state are the states reachable in one derivation step u Encodes all possible derivations u when leftmost literal is prim. constraint only one child u otherwise children ordered like rule order

22
22 Derivation Tree Example failed derivation answer: Y = 1

23
23 Derivation Trees u The previous example shows three derivations, 2 failed and one successful u finitely failed: if a derivation tree is finite and all derivations are failed u next slide a finitely failed derivation tree u infinite derivation tree: some derivations are infinite

24
24 Finitely Failed Example

25
25 Infinite Derivation Tree (S1) stupid(X) :- stupid(X). (S2) stupid(1). Answer: X=1 Infinite derivation

26
26 Goal Evaluation u Evaluation of a goal performs an in-order depth-first search of the derivation tree u when a success state is encountered the system returns an answer u the user can ask for more answers in which case the search continues u execution halts when the users requests no more answers or the entire tree is explored

27
27 Goal Evaluation Example Return answer: Y = 1 more? Return no more

28
28 Goal Evaluation Example 2 The evaluation never finds an answer, even though infinitely many exist

29
29 Simplified Derivation Trees u Derivation trees are very large u A simplified form which has the most useful information u constraints in simplified form (variables in the initial goal and goal part of state) u uninteresting states removed

30
30 Simplified State u simplified state: in derivation for G u replace C 0 with C 1 =simpl(C 0, vars(G,G 0 )) u if x=t in C 1 replace x by t in G 0 giving G 1 u replace C 1 with C 2 =simpl(C 1, vars(G,G 1 )) u Example

31
31 Simplified Derivation u A state is critical if it is the first or last state of a derivation or the first literal is a user- defined constraint u A simplified derivation for goal G contains all the critical states in simplified form u similarly for a simplified derivation tree

32
32 Example Simplified Tree Note: fail states are and success states contain answers

33
33 The CLP Scheme u The scheme defines a family of programming languages u A language CLP(X) is defined by u constraint domain X u solver for the constraint domain X u simplifier for the constraint domain X u Example we have used CLP(Real) u Another example CLP(Tree)

34
34 CLP(R) u Example domain for chapters 5,6,7 u Elements are trees containing real constants u Constraints are for trees u and for arithmetic

35
35 Constraint Logic Programs Summary u rules: for user-defined constraints u multiple rules for one predicate u can be recursive u derivation: evaluates a goal u successful: gives an answer (constraint) u failed: can go no further u infinite u scheme: defines a CLP language

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google