Download presentation

Presentation is loading. Please wait.

Published byLandon Webster Modified over 3 years ago

1
CICLOPS'061 AR (Action Rules) The Language, Implementation, and Applications A tutorial given at CICLOPS06 Neng-Fa Zhou CUNY Brooklyn College and Graduate Center

2
CICLOPS'062 Evolution from freeze to AR 4 Early delay constructs –freeze in Prolog-II freeze(X,p(X,Y)) –When declarations in NU-Prolog :-p(X,Y) when X. –Block and when in Sicstus Prolog :-block p(-,?). when(nonvar(X),p(X,Y))

3
CICLOPS'063 Evolution from freeze to AR (Cont.) Delay clauses in Sepia (Eclipse) delay and(X,Y,Z) if var(X),var(Y),X\==Y,Z\==1 Action rules (B-Prolog) p(X,Y),var(X),{ins(X)} => q(X,Y). Allows for the description of not only delay conditions but also activating events and actions

4
CICLOPS'064 Sources of this tutorial 4 N.-F. Zhou: Programming Finite-Domain Constraint Propagators in Action Rules, Theory and Practice of Logic Programming, accepted 2005. 4 N.-F. Zhou, M.Wallace, and P.J. Stuckey: The dom Event and its Use in Implementing Constraint Propagators, Technical Report, CUNY Computer Science, 2006. 4 T. Schrijvers, N.-F. Zhou, and B. Demoen: Translating Constraint Handling Rules into Action Rules, CHR'06. 4 N.-F. Zhou: A Constraint-based Graphics Library for B- Prolog, Software - Practice and Experience, 2003.

5
CICLOPS'065 Outline 4 The AR language –Syntax and semantics of action rules –Events 4 Applications –Co-routining and concurrency –Constraint propagation –Compiling CHR –Interactive graphical user interfaces 4 The implementation 4 Conclusion

6
CICLOPS'066 Agent, Condition, {EventSet} => Action The AR language Syntax 4 Action rules –Agent p(X1,…,Xn) –Condition Inline tests (e.g., var(X),nonvar(X),X==Y,X>Y) –EventSet event(X,O) -- a general form event ins(X) -- X is instantiated –Action Same as a clause body 4 A predicate can contain multiple action rules

7
CICLOPS'067 The AR language Syntax (Cont.) 4 Commitment rules –An action rule degenerates into a commitment rule if no event is specified 4 Example append([],Ys,Zs) => Ys=Zs. append([X|Xs],Ys,Zs) => Zs=[X|Zs1], append(Xs,Ys,Zs1).

8
CICLOPS'068 4 An agent (subgoal) A is suspended if: –A matches Agent, and –Condition is true 4 A is activated when an event in EventSet is posted 4 Action is executed when A is activated and Condition is true 4 A is suspended again after Action is executed 4 The next rule is tried if Condition fails 4 A fails if Action fails Agent, Condition, {EventSet} => Action The AR language Operational semantics

9
CICLOPS'069 Events 4 General form events –event(X,O) X – channel variable O – event object –Example echo(X),{event(X,O)}=>writeln(O). 4 ins(X) –X is instantiated –Example ( freeze(X,q(X,Y) ) ) p(X,Y),var(X),{ins(X)}=>true. P(X,Y)=>q(X,Y).

10
CICLOPS'0610 Posting events 4 A channel expression is –A channel variable X –A conjunction of variables: X1 /\ X2 … /\ Xn –A disjunction of variables: X1 \/ X2 … \/ Xn 4 Posting events –post_event(C,O) C is a channel expression –post_ins(X) Post an ins(X) event

11
CICLOPS'0611 Example An echoing agent echo(X),{event(X,O)}=> writeln(O). ?-echo(X),post_event(X,hello). hello ?-echo(X),repeat,post_event(X,hello),fail. hello …

12
CICLOPS'0612 Killing agents 4 An agent vanishes after a commitment rule is applied to it echo(Flag,X),var(Flag), {event(X,O),ins(Flag)} => writeln(O). echo(Flag,X) => true. ?-echo(Flag,X),post_event(X,hello),Flag=1. hello

13
CICLOPS'0613 Outline 4 The AR language –Syntax and semantics of action rules –Events 4 Applications –Co-routining and concurrency –Constraint propagation –Compiling CHR –Interactive graphical user interfaces 4 The implementation 4 Conclusion

14
CICLOPS'0614 Co-routining and concurrency freeze(X,G) freeze(X,G), var(X), {ins(X)} => true. freeze(X,G) => call(G). ?-freeze(X,writeln(X)),X=f(a). f(a)

15
CICLOPS'0615 Co-routining and concurrency Delay clauses Delay clause delay and(X,Y,Z) if var(X),var(Y),X\==Y,Z\==1 AR and(X,Y,Z), var(X),var(Y),X\==Y,Z\==1, {ins(X),ins(Y),ins(Z)} => true.

16
CICLOPS'0616 Co-routining and concurrency Compiling flat GHC 4 Flat GHC 4 AR append([],Ys,Zs):-true | Ys=Zs. append([X|Xs],Ys,Zs):-true | Zs=[X|Zs1], append(Xs,Ys,Zs1). append(Xs,Ys,Zs),var(Xs),{ins(Xs)} => true. append([],Ys,Zs) => Ys=Zs. append([X|Xs],Ys,Zs) => Zs=[X|Zs1], append(Xs,Ys,Zs1).

17
CICLOPS'0617 Outline 4 The AR language –Syntax and semantics of action rules –Events 4 Applications –Co-routining and concurrency –Constraint propagation –Compiling CHR –Interactive graphical user interfaces 4 The implementation 4 Conclusion

18
CICLOPS'0618 Events for programming constraint propagation –generated : When suspended for the first time –ins(X): X is instantiated –bound(X) A bound of X s domain is updated –dom(X,E) An inner value E is excluded from X s domain –dom(X) Some inner value is excluded from X s domain –dom_any(X,E) An arbitrary value E is excluded from X s domain –dom_any(X) Some value is excluded from X s domain

19
CICLOPS'0619 Posting events on domain variables 4 X#\=2 –Posts dom(X,2) and dom_any(X,2) 4 X#\=4 –Posts bound(X) and dom_any(X,4) 4 X#\=1 –Posts ins(X) X :: 1..4, X#\=2, X#\=4, X#\=1.

20
CICLOPS'0620 Propagators for aX=bY+c 4 Forward checking 'aX=bY+c_forward'(A,X,B,Y,C),var(X),var(Y), {ins(X),ins(Y)} => true. 'aX=bY+c_forward'(A,X,B,Y,C),var(X) => T is B*Y+C, X is T//A, A*X=:=T. 'aX=bY+c_forward'(A,X,B,Y,C) => T is A*X-C, Y is T//B, B*Y=:=T. When either X or Y is instantiated, instantiate the other variable.

21
CICLOPS'0621 Propagators for aX=bY+c 4 Interval consistency 'aX in bY+c_interval'(A,X,B,Y,C),var(X),var(Y), {generated,bound(Y)} => 'aX in bY+c_reduce_domain'(A,X,B,Y,C). 'aX in bY+c_interval'(A,X,B,Y,C) => true. Whenever a bound of Y s domain is updated, reduce X s domain to achieve interval consistency.

22
CICLOPS'0622 Propagators for aX=bY+c 4 Arc consistency 'aX in bY+c_arc'(A,X,B,Y,C),var(X),var(Y), {dom(Y,Ey)} => T is B*Ey+C, Ex is T//A, (A*Ex=:=T -> X #\=Ex;true). 'aX in bY+c_arc'(A,X,B,Y,C) => true. Whenever an element Ey is excluded from Y s domain, exclude Ey s counterpart Ex from X s domain.

23
CICLOPS'0623 Propagator for A1*X1+...+An*Xn+C = 0 'A1*X1+...+An*Xn+C=0'(C,A1,A2,...,An,X1,X2,..,Xn), n_vars_gt(n,2), {generated,ins(X1),bound(X1),...,ins(Xn),bound(Xn)} => reduce domains of X1,..,Xn to achieve ic. 'A1*X1+...+An*Xn+C=0'(C,A1,A2,...,An,X1,X2,..,Xn) => nary_to_binary(NewC,B1,B2,Y1,Y2), call_binary_propagator(NewC,B1,Y1,B2,Y2). When the constraint contains more than 2 variables, achieve interval consistency. When the constraint becomes binary archive arc consistency.

24
CICLOPS'0624 The use of the dom and dom_any events 4 The AC-4 algorithm for general support constraints 4 Channeling constraints in dual CSPs 4 Set constraints

25
CICLOPS'0625 The AC-4 algorithm for general support constraints ac4(BinaryRelation,X,Y), var(X),var(Y), {dom_any(X,Ex)} => decrement_counters(BinaryRelation,Ex,Y). ac4(BinaryRelation,X,Y) => true. Whenever a value Ex is excluded from the domain of X, the counters of those values in the domain of Y supported by Ex are decremented.

26
CICLOPS'0626 Channeling constraints in dual CSPs 4 Dual CSPs all_distinct([X 1,…,X N ]),X i in 1..N all_distinct([Y 1,…,Y N ]) X i #= j # Y j #= i X i #\= j # Y j #\= I 4 Relating primal and dual variables primal_dual(Xi,I,DualVarVector),var(Xi), {dom_any(Xi,J)} => arg(J,DualVarVector,Yj), Yj #\= I. primal_dual(Xi,I,DualVarVector) => true.

27
CICLOPS'0627 Set constraints 4 Representing finite-set domain variables using FD variables –V l – The lower bound, complement of definite elements –V u – The upper bound, possible elements –V c – The cardinality 4 Example –V :: {1}..{1,2,3} V l :: [0,2..4] the complement of {1} including dummies V u :: [0..4] {1,2,3} including dummies V c :: [1..3]

28
CICLOPS'0628 Propagation for set constraints 4 Propagators for R S subset_from_R_to_S(set(Rl,_Ru,_Rc),S), {dom(Rl,E)} => clpset_add(S,E). subset_from_S_to_R(R,set(_Sl,Su,_Sc)), {dom(Su,E)} => clpset_exclude(R,E).

29
CICLOPS'0629 Benchmarking CLP(FD) systems (As of Aug. 14, 2006) BP: B-Prolog 6.9 EP: Eclipse 5.8 #107 GP: Gnu-Prolog 1.2.16 SP: Sicstus-Prolog 3.12.5 CPU time, Windows XP Benchmarks: www.probp.com/bench.tar.gz

30
CICLOPS'0630 Benchmarking CLP(FD) systems (Cont.) 4 Benchmarks: www.di.univaq.it/~formisano/CLPASP/ www.probp.com/bench.tar.gz www.di.univaq.it/~formisano/CLPASP/ www.probp.com/bench.tar.gz CPU time, Windows XP

31
CICLOPS'0631 Outline 4 The AR language –Syntax and semantics of action rules –Events 4 Applications –Co-routining and concurrency –Constraint propagation –Compiling CHR –Interactive graphical user interfaces 4 The implementation 4 Conclusion

32
CICLOPS'0632 Compiling CHR 4 A comparison of CHR and AR –Common features Rule-based Matching –Differences Multi-headed rules are allowed in CHR Implicit delay in CHR vs. explicit delay in AR

33
CICLOPS'0633 An example CHR program reflexivity @ leq(X,X) true. antisymmetry @ leq(X,Y), leq(Y,X) X = Y. idempotence @ leq(X,Y) \ leq(X,Y) true. transitivity @ leq(X,Y), leq(Y,Z) ==> leq(X,Z). simplification propagation simpagation

34
CICLOPS'0634 Translating CHR into AR General ideas 4 All rules are single or double-headed When a constraint p(X) is added into the store, an event of the the following form is posted 4 For each occurrence of a constraint symbol and each matching constraint in the store, there is an agent watching the arrival of its partner constraint P Body.P, Q Body. P ==> Body.P \ Q Body. P, Q ==> Body. constr(Cno,Alive,History,X)

35
CICLOPS'0635 Translating CHR into AR Example p(X):- gen_constr_num(Cno), Constr=constr(Cno,AliveP,HistoryP,X), get_channel(p_1_1,ChP), get_channel(q_1_1,ChQ), agent_p_1_1(ChP,AliveP,X), post_p_1(ChQ,Constr,AliveP,X). agent_p_1_1(ChP,AliveP,X),var(AliveP), {event(ChP,Q),ins(AliveP)} => Q=constr(_,AliveQ,HistoryQ,Y), (var(AliveQ)->AliveP=0,AliveQ=0,r(X,Y);true). agent_p_1_1(ChP,AliveP,X) => true. p(X),q(Y) r(X,Y).

36
CICLOPS'0636 CHR to AR Example (Cont.) post_p_1(ChQ,Constr,AliveP,X),var(AliveP), {generated,ins(X),ins(AliveP)} => post_event(ChQ,Constr). post_p_1(ChQ,Constr,AliveP,X) => true. p(X),q(Y) r(X,Y).

37
CICLOPS'0637 Benchmarking CHR compilers ProgramLeuven (SWI) Leuven (B-Prolog) AR (by hand) fib3,250938109 leq6,4062,313360 primes6,5321,125640 zebra6,8431,765453 CPU time: milliseconds, Windows XP

38
CICLOPS'0638 Outline 4 The AR language –Syntax and semantics of action rules –Events 4 Applications –Co-routining and concurrency –Constraint propagation –Compiling CHR –Interactive graphical user interfaces 4 The implementation 4 Conclusion

39
CICLOPS'0639 CGLIB 4 Motivation –Implement an application with a GUI in one language 4 Features –Use constraints to specify the layouts of objects –Use action rules to specify interactions 4 Implementation –Implemented in B-Prolog, Java, JIPL, and C 4 Applications –Interactive user interfaces, animation, information visualization, intelligent agents, and games.

40
CICLOPS'0640 An example go:- cgButton(B,Hello World!), handleButtonClick(B), cgShow(B). handleButtonClick(B), {actionPerformed(B)} => halt.

41
CICLOPS'0641 Events for programming GUI 4 actionPerformed(O) 4 focusGained(O) 4 focusLost(O) 4 keyPressed(O,E) 4 keyReleased(O,E) 4 keyTyped(O,E ) 4 mousePressed(O,E) 4 mouseReleased(O,E) 4 mouseEntered(O,E) 4 mouseExited(O,E) 4 mouseClicked(O,E) 4 mouseDragged(O,E) 4 mouseMoved(O,E) windowClosing(O) windowOpened(O) windowIconified(O) windowDeiconified(O) windowClosed(O) windowActivated(O) windowDeactivated(O) componentResized(O,E) componentMoved(O,E) textValueChanged(O) itemStateChanged(O,E) adjustmentValueChanged(O,E) time(T)

42
CICLOPS'0642 Timers and time events go:- timer(T1,100), timer(T2,1000), ping(T1), pong(T2), repeat,fail. ping(T),{time(T)} => writeln(ping). pong(T),{time(T)} => writeln(pong).

43
CICLOPS'0643 Demo of CGLIB 4 Graphical user interfaces 4 Animation 4 Information visualization 4 Constraint satisfaction problems 4 Games

44
CICLOPS'0644 Outline 4 The AR language –Syntax and semantics of action rules –Events 4 Applications –Co-routining and concurrency –Constraint propagation –Compiling CHR –Interactive graphical user interfaces 4 The implementation 4 Conclusion

45
CICLOPS'0645 The implementation of AR (The ATOAM architecture) P code area X1 X2... Xn registers s AR stackheap trail HT TOP

46
CICLOPS'0646 The frame structure for deterministic predicates Arguments AR CPS BTM TOP Local vars 4 Frame slots –AR: Parents frame –CPS: Continuation PC –BTM: Bottom of the stack frame –TOP: Top of the stack

47
CICLOPS'0647 The frame structure for agents (Suspension frames) Arguments AR CPS BTM TOP PREV STATE REEP EVENT Local vars 4 Frame slots –PREV: Previous suspension frame –STATE: State of the frame (start, sleep, woken, end) –REEP: Reentrance program pointer –EVENT: Activating event

48
CICLOPS'0648 The frame structure for non- deterministic predicates Arguments AR CPS BTM TOP B CPF H T SF Local vars 4 Frame slots –B: Parent choice point frame –CPF: Continuation PC on failure –H: Top of the heap –T: Top of the trail stack –SF: Suspension frame

49
CICLOPS'0649 Spaghetti stack 4 Context switching is light 4 Activation frames are not in chronological order 4 Run-time testing is needed to de-allocate a frame 4 The BTM slot is needed for de-allocation of frames 4 Stack needs be garbage collected

50
CICLOPS'0650 Conclusion 4 Conclusion –AR is a simple but powerful language which has a variety of applications 4 Further work –Multi-threaded AR –Even faster implementation –Debugging –Fast implementation of AR on WAM [B. Demoen] –New applications (Multi-agents) 4 More information –www.probp.comwww.probp.com –www.bprolog.comwww.bprolog.com

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google