Zhou ciclops floc06

315 views

Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

Zhou ciclops floc06

  1. 1. AR (Action Rules) The Language, Implementation, and Applications A tutorial given at CICLOPS’06 Neng-Fa Zhou CUNY Brooklyn College and Graduate Center
  2. 2. Evolution from freeze to AR <ul><li>Early delay constructs </li></ul><ul><ul><li>freeze in Prolog-II freeze(X,p(X,Y)) </li></ul></ul><ul><ul><li>When declarations in NU-Prolog :-p(X,Y) when X . </li></ul></ul><ul><ul><li>Block and when in Sicstus Prolog </li></ul></ul><ul><ul><ul><li>:-block p(-,?) . when(nonvar(X),p(X,Y)) </li></ul></ul></ul>
  3. 3. Evolution from freeze to AR (Cont.) <ul><li>Delay clauses in Sepia (Eclipse) delay and(X,Y,Z) if var(X),var(Y),X==Y,Z==1 </li></ul><ul><li>Action rules (B-Prolog) p(X,Y),var(X),{ins(X)} => q(X,Y). </li></ul><ul><li>Allows for the description of not only delay conditions but also activating events and actions </li></ul>
  4. 4. Sources of this tutorial <ul><li>N.-F. Zhou: Programming Finite-Domain Constraint Propagators in Action Rules, Theory and Practice of Logic Programming , accepted 2005. </li></ul><ul><li>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. </li></ul><ul><li>T. Schrijvers, N.-F. Zhou, and B. Demoen: Translating Constraint Handling Rules into Action Rules, CHR'06 . </li></ul><ul><li>N.-F. Zhou: A Constraint-based Graphics Library for B-Prolog, Software - Practice and Experience , 2003. </li></ul>
  5. 5. Outline <ul><li>The AR language </li></ul><ul><ul><li>Syntax and semantics of action rules </li></ul></ul><ul><ul><li>Events </li></ul></ul><ul><li>Applications </li></ul><ul><ul><li>Co-routining and concurrency </li></ul></ul><ul><ul><li>Constraint propagation </li></ul></ul><ul><ul><li>Compiling CHR </li></ul></ul><ul><ul><li>Interactive graphical user interfaces </li></ul></ul><ul><li>The implementation </li></ul><ul><li>Conclusion </li></ul>
  6. 6. The AR language Syntax <ul><li>Action rules </li></ul><ul><ul><li>Agent p(X1,…,Xn) </li></ul></ul><ul><ul><li>Condition </li></ul></ul><ul><ul><ul><li>Inline tests (e.g., var(X),nonvar(X),X==Y,X>Y) </li></ul></ul></ul><ul><ul><li>EventSet </li></ul></ul><ul><ul><ul><li>event(X,O) -- a general form event </li></ul></ul></ul><ul><ul><ul><li>ins(X) -- X is instantiated </li></ul></ul></ul><ul><ul><li>Action </li></ul></ul><ul><ul><ul><li>Same as a clause body </li></ul></ul></ul><ul><li>A predicate can contain multiple action rules </li></ul>Agent, Condition, {EventSet} => Action
  7. 7. The AR language Syntax (Cont.) <ul><li>Commitment rules </li></ul><ul><ul><li>An action rule degenerates into a commitment rule if no event is specified </li></ul></ul><ul><li>Example </li></ul>append([],Ys,Zs) => Ys=Zs. append([X|Xs],Ys,Zs) => Zs=[X|Zs1], append(Xs,Ys,Zs1).
  8. 8. The AR language Operational semantics <ul><li>An agent (subgoal) A is suspended if: </li></ul><ul><ul><li>A matches Agent, and </li></ul></ul><ul><ul><li>Condition is true </li></ul></ul><ul><li>A is activated when an event in EventSet is posted </li></ul><ul><li>Action is executed when A is activated and Condition is true </li></ul><ul><li>A is suspended again after Action is executed </li></ul><ul><li>The next rule is tried if Condition fails </li></ul><ul><li>A fails if Action fails </li></ul>Agent, Condition, {EventSet} => Action
  9. 9. Events <ul><li>General form events </li></ul><ul><ul><li>event(X,O) </li></ul></ul><ul><ul><ul><li>X – channel variable </li></ul></ul></ul><ul><ul><ul><li>O – event object </li></ul></ul></ul><ul><ul><li>Example </li></ul></ul><ul><ul><li>echo(X),{event(X,O)}=>writeln(O). </li></ul></ul><ul><li>ins(X) </li></ul><ul><ul><li>X is instantiated </li></ul></ul><ul><ul><li>Example ( freeze(X,q(X,Y) ) ) p(X,Y),var(X),{ins(X)}=>true. P(X,Y)=>q(X,Y). </li></ul></ul>
  10. 10. Posting events <ul><li>A channel expression is </li></ul><ul><ul><li>A channel variable X </li></ul></ul><ul><ul><li>A conjunction of variables: X1 / X2 … / Xn </li></ul></ul><ul><ul><li>A disjunction of variables: X1 / X2 … / Xn </li></ul></ul><ul><li>Posting events </li></ul><ul><ul><li>post_event(C,O) </li></ul></ul><ul><ul><ul><li>C is a channel expression </li></ul></ul></ul><ul><ul><li>post_ins(X) </li></ul></ul><ul><ul><ul><li>Post an ins(X) event </li></ul></ul></ul>
  11. 11. 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 hello hello …
  12. 12. Killing agents <ul><li>An agent vanishes after a commitment rule is applied to it </li></ul>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. 13. Outline <ul><li>The AR language </li></ul><ul><ul><li>Syntax and semantics of action rules </li></ul></ul><ul><ul><li>Events </li></ul></ul><ul><li>Applications </li></ul><ul><ul><li>Co-routining and concurrency </li></ul></ul><ul><ul><li>Constraint propagation </li></ul></ul><ul><ul><li>Compiling CHR </li></ul></ul><ul><ul><li>Interactive graphical user interfaces </li></ul></ul><ul><li>The implementation </li></ul><ul><li>Conclusion </li></ul>
  14. 14. 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. 15. Co-routining and concurrency Delay clauses <ul><li>Delay clause delay and(X,Y,Z) if var(X),var(Y),X==Y,Z==1 </li></ul><ul><li>AR and(X,Y,Z), var(X),var(Y),X==Y,Z==1, {ins(X),ins(Y),ins(Z)} => true. </li></ul>
  16. 16. Co-routining and concurrency Compiling flat GHC <ul><li>Flat GHC </li></ul><ul><li>AR </li></ul>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. 17. Outline <ul><li>The AR language </li></ul><ul><ul><li>Syntax and semantics of action rules </li></ul></ul><ul><ul><li>Events </li></ul></ul><ul><li>Applications </li></ul><ul><ul><li>Co-routining and concurrency </li></ul></ul><ul><ul><li>Constraint propagation </li></ul></ul><ul><ul><li>Compiling CHR </li></ul></ul><ul><ul><li>Interactive graphical user interfaces </li></ul></ul><ul><li>The implementation </li></ul><ul><li>Conclusion </li></ul>
  18. 18. Events for programming constraint propagation <ul><ul><li>generated : When suspended for the first time </li></ul></ul><ul><ul><li>ins(X): X is instantiated </li></ul></ul><ul><ul><li>bound(X) </li></ul></ul><ul><ul><ul><li>A bound of X ’s domain is updated </li></ul></ul></ul><ul><ul><li>dom(X,E) </li></ul></ul><ul><ul><ul><li>An inner value E is excluded from X ’s domain </li></ul></ul></ul><ul><ul><li>dom(X) </li></ul></ul><ul><ul><ul><li>Some inner value is excluded from X ’s domain </li></ul></ul></ul><ul><ul><li>dom_any(X,E) </li></ul></ul><ul><ul><ul><li>An arbitrary value E is excluded from X ’s domain </li></ul></ul></ul><ul><ul><li>dom_any(X) </li></ul></ul><ul><ul><ul><li>Some value is excluded from X ’s domain </li></ul></ul></ul>
  19. 19. Posting events on domain variables <ul><li>X#=2 </li></ul><ul><ul><li>Posts dom(X,2) and dom_any(X,2) </li></ul></ul><ul><li>X#=4 </li></ul><ul><ul><li>Posts bound(X) and dom_any(X,4) </li></ul></ul><ul><li>X#=1 </li></ul><ul><ul><li>Posts ins(X) </li></ul></ul>X :: 1..4, X#=2, X#=4, X#=1.
  20. 20. Propagators for aX=bY+c <ul><li>Forward checking </li></ul>'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. 21. Propagators for aX=bY+c <ul><li>Interval consistency </li></ul>'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. 22. Propagators for aX=bY+c <ul><li>Arc consistency </li></ul>'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. 23. 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. 24. The use of the dom and dom_any events <ul><li>The AC-4 algorithm for general support constraints </li></ul><ul><li>Channeling constraints in dual CSPs </li></ul><ul><li>Set constraints </li></ul>
  25. 25. 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. <ul><ul><li>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. </li></ul></ul>
  26. 26. Channeling constraints in dual CSPs <ul><li>Dual CSPs </li></ul><ul><ul><ul><li>all_distinct([X 1 ,…,X N ]),X i in 1..N </li></ul></ul></ul><ul><ul><ul><li>all_distinct([Y 1 ,…,Y N ]) X i #= j #<=> Y j #= i X i #= j #<=> Y j #= I </li></ul></ul></ul><ul><li>Relating primal and dual variables </li></ul>primal_dual(Xi,I,DualVarVector),var(Xi), {dom_any(Xi,J)} => arg(J,DualVarVector,Yj), Yj #= I. primal_dual(Xi,I,DualVarVector) => true.
  27. 27. Set constraints <ul><li>Representing finite-set domain variables using FD variables </li></ul><ul><ul><li>V l – The lower bound, complement of definite elements </li></ul></ul><ul><ul><li>V u – The upper bound, possible elements </li></ul></ul><ul><ul><li>V c – The cardinality </li></ul></ul><ul><li>Example </li></ul><ul><ul><li>V :: {1}..{1,2,3} </li></ul></ul><ul><ul><ul><li>V l :: [0,2..4] the complement of {1} including dummies </li></ul></ul></ul><ul><ul><ul><li>V u :: [0..4] {1,2,3} including dummies </li></ul></ul></ul><ul><ul><ul><li>V c :: [1..3] </li></ul></ul></ul>
  28. 28. Propagation for set constraints <ul><li>Propagators for R  S </li></ul>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. 29. 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. 30. Benchmarking CLP(FD) systems (Cont.) <ul><li>Benchmarks: www.di.univaq.it/~formisano/CLPASP/ www.probp.com/bench.tar.gz </li></ul>CPU time, Windows XP
  31. 31. Outline <ul><li>The AR language </li></ul><ul><ul><li>Syntax and semantics of action rules </li></ul></ul><ul><ul><li>Events </li></ul></ul><ul><li>Applications </li></ul><ul><ul><li>Co-routining and concurrency </li></ul></ul><ul><ul><li>Constraint propagation </li></ul></ul><ul><ul><li>Compiling CHR </li></ul></ul><ul><ul><li>Interactive graphical user interfaces </li></ul></ul><ul><li>The implementation </li></ul><ul><li>Conclusion </li></ul>
  32. 32. Compiling CHR <ul><li>A comparison of CHR and AR </li></ul><ul><ul><li>Common features </li></ul></ul><ul><ul><ul><li>Rule-based </li></ul></ul></ul><ul><ul><ul><li>Matching </li></ul></ul></ul><ul><ul><li>Differences </li></ul></ul><ul><ul><ul><li>Multi-headed rules are allowed in CHR </li></ul></ul></ul><ul><ul><ul><li>Implicit delay in CHR vs. explicit delay in AR </li></ul></ul></ul>
  33. 33. 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. 34. Translating CHR into AR General ideas <ul><li>All rules are single or double-headed </li></ul><ul><li>When a constraint p(X) is added into the store, an event of the the following form is posted </li></ul><ul><li>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 </li></ul>P <=> Body. P, Q <=> Body. P ==> Body. P Q <=> Body. P, Q ==> Body. constr(Cno,Alive,History,X)
  35. 35. 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. 36. 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. 37. Benchmarking CHR compilers CPU time: milliseconds, Windows XP 453 1,765 6,843 zebra 640 1,125 6,532 primes 360 2,313 6,406 leq 3,250 Leuven (SWI) 109 938 fib AR (by hand) Leuven (B-Prolog) Program
  38. 38. Outline <ul><li>The AR language </li></ul><ul><ul><li>Syntax and semantics of action rules </li></ul></ul><ul><ul><li>Events </li></ul></ul><ul><li>Applications </li></ul><ul><ul><li>Co-routining and concurrency </li></ul></ul><ul><ul><li>Constraint propagation </li></ul></ul><ul><ul><li>Compiling CHR </li></ul></ul><ul><ul><li>Interactive graphical user interfaces </li></ul></ul><ul><li>The implementation </li></ul><ul><li>Conclusion </li></ul>
  39. 39. CGLIB <ul><li>Motivation </li></ul><ul><ul><li>Implement an application with a GUI in one language </li></ul></ul><ul><li>Features </li></ul><ul><ul><li>Use constraints to specify the layouts of objects </li></ul></ul><ul><ul><li>Use action rules to specify interactions </li></ul></ul><ul><li>Implementation </li></ul><ul><ul><li>Implemented in B-Prolog, Java, JIPL, and C </li></ul></ul><ul><li>Applications </li></ul><ul><ul><li>Interactive user interfaces, animation, information visualization, intelligent agents, and games. </li></ul></ul>
  40. 40. An example go:- cgButton(B,“Hello World!”), handleButtonClick(B), cgShow(B).   handleButtonClick(B), {actionPerformed(B)} => halt.
  41. 41. Events for programming GUI <ul><li>actionPerformed(O) </li></ul><ul><li>focusGained(O) </li></ul><ul><li>focusLost(O) </li></ul><ul><li>keyPressed(O,E) </li></ul><ul><li>keyReleased(O,E) </li></ul><ul><li>keyTyped(O,E ) </li></ul><ul><li>mousePressed(O,E) </li></ul><ul><li>mouseReleased(O,E) </li></ul><ul><li>mouseEntered(O,E) </li></ul><ul><li>mouseExited(O,E) </li></ul><ul><li>mouseClicked(O,E) </li></ul><ul><li>mouseDragged(O,E) </li></ul><ul><li>mouseMoved(O,E) </li></ul><ul><li>windowClosing(O) </li></ul><ul><li>windowOpened(O) </li></ul><ul><li>windowIconified(O) </li></ul><ul><li>windowDeiconified(O) </li></ul><ul><li>windowClosed(O) </li></ul><ul><li>windowActivated(O) </li></ul><ul><li>windowDeactivated(O) </li></ul><ul><li>componentResized(O,E) </li></ul><ul><li>componentMoved(O,E) </li></ul><ul><li>textValueChanged(O) </li></ul><ul><li>itemStateChanged(O,E) </li></ul><ul><li>adjustmentValueChanged(O,E) </li></ul><ul><li>time(T) </li></ul>
  42. 42. 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. 43. Demo of CGLIB <ul><li>Graphical user interfaces </li></ul><ul><li>Animation </li></ul><ul><li>Information visualization </li></ul><ul><li>Constraint satisfaction problems </li></ul><ul><li>Games </li></ul>
  44. 44. Outline <ul><li>The AR language </li></ul><ul><ul><li>Syntax and semantics of action rules </li></ul></ul><ul><ul><li>Events </li></ul></ul><ul><li>Applications </li></ul><ul><ul><li>Co-routining and concurrency </li></ul></ul><ul><ul><li>Constraint propagation </li></ul></ul><ul><ul><li>Compiling CHR </li></ul></ul><ul><ul><li>Interactive graphical user interfaces </li></ul></ul><ul><li>The implementation </li></ul><ul><li>Conclusion </li></ul>
  45. 45. The implementation of AR (The ATOAM architecture) P code area X1 X2 ... Xn registers s AR stack heap trail H T TOP
  46. 46. The frame structure for deterministic predicates Arguments AR CPS BTM TOP Local vars <ul><li>Frame slots </li></ul><ul><ul><li>AR: Parent’s frame </li></ul></ul><ul><ul><li>CPS: Continuation PC </li></ul></ul><ul><ul><li>BTM: Bottom of the stack frame </li></ul></ul><ul><ul><li>TOP: Top of the stack </li></ul></ul>
  47. 47. The frame structure for agents (Suspension frames) Arguments AR CPS BTM TOP PREV STATE REEP EVENT Local vars <ul><li>Frame slots </li></ul><ul><ul><li>PREV: Previous suspension frame </li></ul></ul><ul><ul><li>STATE: State of the frame (start, sleep, woken, end) </li></ul></ul><ul><ul><li>REEP: Reentrance program pointer </li></ul></ul><ul><ul><li>EVENT: Activating event </li></ul></ul>
  48. 48. The frame structure for non-deterministic predicates Arguments AR CPS BTM TOP B CPF H T SF Local vars <ul><li>Frame slots </li></ul><ul><ul><li>B: Parent choice point frame </li></ul></ul><ul><ul><li>CPF: Continuation PC on failure </li></ul></ul><ul><ul><li>H: Top of the heap </li></ul></ul><ul><ul><li>T: Top of the trail stack </li></ul></ul><ul><ul><li>SF: Suspension frame </li></ul></ul>
  49. 49. Spaghetti stack <ul><li>Context switching is light </li></ul><ul><li>Activation frames are not in chronological order </li></ul><ul><li>Run-time testing is needed to de-allocate a frame </li></ul><ul><li>The BTM slot is needed for de-allocation of frames </li></ul><ul><li>Stack needs be garbage collected </li></ul>
  50. 50. Conclusion <ul><li>Conclusion </li></ul><ul><ul><li>AR is a simple but powerful language which has a variety of applications </li></ul></ul><ul><li>Further work </li></ul><ul><ul><li>Multi-threaded AR </li></ul></ul><ul><ul><li>Even faster implementation </li></ul></ul><ul><ul><li>Debugging </li></ul></ul><ul><ul><li>Fast implementation of AR on WAM [B. Demoen] </li></ul></ul><ul><ul><li>New applications (Multi-agents) </li></ul></ul><ul><li>More information </li></ul><ul><ul><li>www.probp.com </li></ul></ul><ul><ul><li>www.bprolog.com </li></ul></ul>

×