Title: AR (Action Rules) The Language, Implementation, and Applications A tutorial given at CICLOPS
1AR (Action Rules)The Language, Implementation,
and ApplicationsA tutorial given at CICLOPS06
- Neng-Fa Zhou
- CUNY Brooklyn College and Graduate Center
2Evolution from freeze to AR
- 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))
3Evolution 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)
gt q(X,Y). - Allows for the description of not only delay
conditions but also activating events and
actions
4Sources of this tutorial
- N.-F. Zhou Programming Finite-Domain Constraint
Propagators in Action Rules, Theory and Practice
of Logic Programming, accepted 2005. - 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. - T. Schrijvers, N.-F. Zhou, and B. Demoen
Translating Constraint Handling Rules into Action
Rules, CHR'06. - N.-F. Zhou A Constraint-based Graphics Library
for B-Prolog, Software - Practice and Experience,
2003.
5Outline
- The AR language
- Syntax and semantics of action rules
- Events
- Applications
- Co-routining and concurrency
- Constraint propagation
- Compiling CHR
- Interactive graphical user interfaces
- The implementation
- Conclusion
6The AR language Syntax
- Action rules
- Agent p(X1,,Xn)
- Condition
- Inline tests (e.g., var(X),nonvar(X),XY,XgtY)
- EventSet
- event(X,O) -- a general form event
- ins(X) -- X is instantiated
- Action
- Same as a clause body
- A predicate can contain multiple action rules
Agent, Condition, EventSet gt Action
7The AR language Syntax (Cont.)
- Commitment rules
- An action rule degenerates into a commitment rule
if no event is specified - Example
append(,Ys,Zs) gt YsZs. append(XXs,Ys,Zs)
gt ZsXZs1, append(Xs,Ys,Zs1).
8The AR languageOperational semantics
Agent, Condition, EventSet gt Action
- An agent (subgoal) A is suspended if
- A matches Agent, and
- Condition is true
- A is activated when an event in EventSet is
posted - Action is executed when A is activated and
Condition is true - A is suspended again after Action is executed
- The next rule is tried if Condition fails
- A fails if Action fails
9Events
- General form events
- event(X,O)
- X channel variable
- O event object
- Example
- echo(X),event(X,O)gtwriteln(O).
- ins(X)
- X is instantiated
- Example (freeze(X,q(X,Y)) ) p(X,Y),var(X),ins(
X)gttrue. P(X,Y)gtq(X,Y).
10Posting events
- A channel expression is
- A channel variable X
- A conjunction of variables X1 /\ X2 /\ Xn
- A disjunction of variables X1 \/ X2 \/ Xn
- Posting events
- post_event(C,O)
- C is a channel expression
- post_ins(X)
- Post an ins(X) event
11Example An echoing agent
echo(X),event(X,O) gt writeln(O).
?-echo(X),post_event(X,hello). hello ?-echo(X),re
peat,post_event(X,hello),fail. hello hello hello
12Killing agents
- An agent vanishes after a commitment rule is
applied to it
echo(Flag,X),var(Flag), event(X,O),ins(Flag)
gt writeln(O). echo(Flag,X) gt true.
?-echo(Flag,X),post_event(X,hello),Flag1. hello
13Outline
- The AR language
- Syntax and semantics of action rules
- Events
- Applications
- Co-routining and concurrency
- Constraint propagation
- Compiling CHR
- Interactive graphical user interfaces
- The implementation
- Conclusion
14Co-routining and concurrency freeze(X,G)
freeze(X,G), var(X), ins(X) gt
true. freeze(X,G) gt call(G).
?-freeze(X,writeln(X)),Xf(a). f(a)
15Co-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, in
s(X),ins(Y),ins(Z) gt true.
16Co-routining and concurrencyCompiling flat GHC
append(,Ys,Zs)-true YsZs. append(XXs,Ys,Z
s)-true ZsXZs1, append(Xs,Ys,Zs1).
append(Xs,Ys,Zs),var(Xs),ins(Xs) gt
true. append(,Ys,Zs) gt YsZs. append(XXs,Ys,
Zs) gt ZsXZs1, append(Xs,Ys,Zs1).
17Outline
- The AR language
- Syntax and semantics of action rules
- Events
- Applications
- Co-routining and concurrency
- Constraint propagation
- Compiling CHR
- Interactive graphical user interfaces
- The implementation
- Conclusion
18Events for programming constraint propagation
- generated When suspended for the first time
- ins(X) X is instantiated
- bound(X)
- A bound of Xs domain is updated
- dom(X,E)
- An inner value E is excluded from Xs domain
- dom(X)
- Some inner value is excluded from Xs domain
- dom_any(X,E)
- An arbitrary value E is excluded from Xs domain
- dom_any(X)
- Some value is excluded from Xs domain
19Posting events on domain variables
X 1..4, X\2, X\4, X\1.
- X\2
- Posts dom(X,2) and dom_any(X,2)
- X\4
- Posts bound(X) and dom_any(X,4)
- X\1
- Posts ins(X)
20Propagators for aXbYc
'aXbYc_forward'(A,X,B,Y,C),var(X),var(Y), ins(
X),ins(Y) gt true. 'aXbYc_forward'(A,X,B,Y,
C),var(X) gt T is BYC, X is T//A,
AXT. 'aXbYc_forward'(A,X,B,Y,C) gt
T is AX-C, Y is T//B, BYT.
When either X or Y is instantiated, instantiate
the other variable.
21Propagators for aXbYc
'aX in bYc_interval'(A,X,B,Y,C),var(X),var(Y),
generated,bound(Y) gt 'aX in
bYc_reduce_domain'(A,X,B,Y,C). 'aX in
bYc_interval'(A,X,B,Y,C) gt true.
Whenever a bound of Ys domain is updated, reduce
Xs domain to achieve interval consistency.
22Propagators for aXbYc
'aX in bYc_arc'(A,X,B,Y,C),var(X),var(Y), dom(
Y,Ey) gt T is BEyC,
Ex is T//A, (AExT -gt X
\Extrue). 'aX in bYc_arc'(A,X,B,Y,C) gt
true.
Whenever an element Ey is excluded from Ys
domain, exclude Eys counterpart Ex from Xs
domain.
23Propagator for A1X1...AnXnC 0
'A1X1...AnXnC0'(C,A1,A2,...,An,X1,X2,..,Xn)
, n_vars_gt(n,2), generated,ins(X1),bound(X1),.
..,ins(Xn),bound(Xn) gt reduce
domains of X1,..,Xn to achieve ic.
'A1X1...AnXnC0'(C,A1,A2,...,An,X1,X2,..,Xn)
gt 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.
24The use of the dom and dom_any events
- The AC-4 algorithm for general support
constraints - Channeling constraints in dual CSPs
- Set constraints
25The AC-4 algorithm for general support
constraints
ac4(BinaryRelation,X,Y), var(X),var(Y),
dom_any(X,Ex) gt decrement_counters(Binar
yRelation,Ex,Y). ac4(BinaryRelation,X,Y) gt 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.
26Channeling constraints in dual CSPs
- Dual CSPs
- all_distinct(X1,,XN),Xi in 1..N
- all_distinct(Y1,,YN) Xi j ltgt Yj i
Xi \ j ltgt Yj \ I - Relating primal and dual variables
primal_dual(Xi,I,DualVarVector),var(Xi),
dom_any(Xi,J) gt arg(J,DualVarVector,
Yj), Yj \ I. primal_dual(Xi,I,DualVarVecto
r) gt true.
27Set constraints
- Representing finite-set domain variables using FD
variables - Vl The lower bound, complement of definite
elements - Vu The upper bound, possible elements
- Vc The cardinality
- Example
- V 1..1,2,3
- Vl 0,2..4 the complement of 1
including dummies - Vu 0..4 1,2,3 including dummies
- Vc 1..3
28Propagation for set constraints
- subset_from_R_to_S(set(Rl,_Ru,_Rc),S),
dom(Rl,E) gt clpset_add(S,E). - subset_from_S_to_R(R,set(_Sl,Su,_Sc)),
- dom(Su,E)
- gt clpset_exclude(R,E).
29Benchmarking CLP(FD) systems(As of Aug. 14, 2006)
CPU time, Windows XP
- Benchmarks
- www.probp.com/bench.tar.gz
BP B-Prolog 6.9 EP Eclipse 5.8 107 GP
Gnu-Prolog 1.2.16 SP Sicstus-Prolog 3.12.5
30Benchmarking CLP(FD) systems(Cont.)
CPU time, Windows XP
- Benchmarkswww.di.univaq.it/formisano/CLPASP/
www.probp.com/bench.tar.gz
31Outline
- The AR language
- Syntax and semantics of action rules
- Events
- Applications
- Co-routining and concurrency
- Constraint propagation
- Compiling CHR
- Interactive graphical user interfaces
- The implementation
- Conclusion
32Compiling CHR
- 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
33An example CHR program
reflexivity _at_ leq(X,X) ltgt true. antisymmetry
_at_ leq(X,Y), leq(Y,X) ltgt X Y. idempotence _at_
leq(X,Y) \ leq(X,Y) ltgt true. transitivity _at_
leq(X,Y), leq(Y,Z) gt leq(X,Z).
simplification
simpagation
propagation
34Translating CHR into ARGeneral ideas
- 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 - 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 ltgt Body. P, Q ltgt Body. P gt Body. P \ Q
ltgt Body. P, Q gt Body.
constr(Cno,Alive,History,X)
35Translating CHR into ARExample
p(X),q(Y) ltgt r(X,Y).
p(X)- gen_constr_num(Cno), Constrconstr(Cno,Al
iveP,HistoryP,X), get_channel(p_1_1,ChP), get_ch
annel(q_1_1,ChQ), agent_p_1_1(ChP,AliveP,X), pos
t_p_1(ChQ,Constr,AliveP,X). agent_p_1_1(ChP,Alive
P,X),var(AliveP), event(ChP,Q),ins(AliveP)
gt Qconstr(_,AliveQ,HistoryQ,Y), (var(Al
iveQ)-gtAliveP0,AliveQ0,r(X,Y)true). agent_p_1_1
(ChP,AliveP,X) gt true.
36CHR to ARExample (Cont.)
p(X),q(Y) ltgt r(X,Y).
post_p_1(ChQ,Constr,AliveP,X),var(AliveP), gener
ated,ins(X),ins(AliveP) gt post_event(ChQ,Constr
). post_p_1(ChQ,Constr,AliveP,X) gt true.
37Benchmarking CHR compilers
CPU time milliseconds, Windows XP
Program Leuven (SWI) Leuven(B-Prolog) AR (by hand)
fib 3,250 938 109
leq 6,406 2,313 360
primes 6,532 1,125 640
zebra 6,843 1,765 453
38Outline
- The AR language
- Syntax and semantics of action rules
- Events
- Applications
- Co-routining and concurrency
- Constraint propagation
- Compiling CHR
- Interactive graphical user interfaces
- The implementation
- Conclusion
39CGLIB
- Motivation
- Implement an application with a GUI in one
language - Features
- Use constraints to specify the layouts of objects
- Use action rules to specify interactions
- Implementation
- Implemented in B-Prolog, Java, JIPL, and C
- Applications
- Interactive user interfaces, animation,
information visualization, intelligent agents,
and games.
40An example
go- cgButton(B,Hello World!),
handleButtonClick(B), cgShow(B). hand
leButtonClick(B), actionPerformed(B)
gt halt.
41Events for programming GUI
- actionPerformed(O)
- focusGained(O)
- focusLost(O)
- keyPressed(O,E)
- keyReleased(O,E)
- keyTyped(O,E )
- mousePressed(O,E)
- mouseReleased(O,E)
- mouseEntered(O,E)
- mouseExited(O,E)
- mouseClicked(O,E)
- mouseDragged(O,E)
- 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)
42Timers and time events
go- timer(T1,100), timer(T2,1000),
ping(T1), pong(T2), repeat,fail. pi
ng(T),time(T) gt writeln(ping). pong(T),time(T
) gt writeln(pong).
43Demo of CGLIB
- Graphical user interfaces
- Animation
- Information visualization
- Constraint satisfaction problems
- Games
44Outline
- The AR language
- Syntax and semantics of action rules
- Events
- Applications
- Co-routining and concurrency
- Constraint propagation
- Compiling CHR
- Interactive graphical user interfaces
- The implementation
- Conclusion
45The implementation of AR(The ATOAM architecture)
registers
code area
X1 X2 ... Xn
s
P
AR
stack
heap
H
T
trail
TOP
46The frame structure for deterministic predicates
- Frame slots
- AR Parents frame
- CPS Continuation PC
- BTM Bottom of the stack frame
- TOP Top of the stack
Arguments AR CPS BTM TOP Local vars
47The frame structure for agents(Suspension frames)
Arguments AR CPS BTM TOP PREV STATE REEP EVENT Loc
al vars
- Frame slots
- PREV Previous suspension frame
- STATE State of the frame (start, sleep, woken,
end) - REEP Reentrance program pointer
- EVENT Activating event
48The frame structure for non-deterministic
predicates
Arguments AR CPS BTM TOP B CPF H T SF Local vars
- 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
49Spaghetti stack
- Context switching is light
- Activation frames are not in chronological order
- Run-time testing is needed to de-allocate a frame
- The BTM slot is needed for de-allocation of
frames - Stack needs be garbage collected
50Conclusion
- Conclusion
- AR is a simple but powerful language which has a
variety of applications - Further work
- Multi-threaded AR
- Even faster implementation
- Debugging
- Fast implementation of AR on WAM B. Demoen
- New applications (Multi-agents)
- More information
- www.probp.com
- www.bprolog.com