B-Prolog - Action Rules

Action Rules

The lack of a facility for programming "active" sub-goals that can be reactive to the environment has been considered one of the weaknesses of logic programming. To overcome this, B-Prolog provides a simple and yet powerful language, called Action Rules (AR), for programming agents. An agent is a subgoal that can be delayed and can later be activated by events. Each time an agent is activated, some action may be executed. Agents are a more general notion than delay constructs in early Prolog systems and processes in concurrent logic programming languages in the sense that agents can be responsive to various kinds of events including instantiation, domain, time, and user-defined events.

An action rule takes the following

H, G, {E} => B

where H is a pattern for agents, G is a sequence of conditions on the agents, E is a set of patterns for events that can activate the agents, and B is a sequence of actions performed by the agents when they are activated. When the event pattern E together with the enclosing braces is missing, an action rule degenerates into a matching clause.

A set of built-in events is provided for programming constraint propagators and interactive graphical user interfaces. For example, ins(X) is an event that is posted when the variable X is instantiated. A user program can create and post its own events and define agents to handle them. A user-defined event takes the form of event(X,O) where X is a variable, called a suspension variable, that connects the event with its handling agents, and O is a Prolog term that contains the information to be transmitted to the agents. The built-in post(E) posts the event E.

Consider the following examples:

echo(X),{event(X,Mes)}=>writeln(Mes). ping(T),{time(T)} => writeln(ping).

The agent echo(X) echoes whatever message it receives. For example,

?-echo(X),post(event(X,hello)),post(event(X,world)).

outputs the message hello followed by world. The agent ping(T) responds to time events from the timer T. Each time it receives a time event, it prints the message ping. For example,

?-timer(T,1000),ping(T),repeat,fail.

creates a timer that posts a time event every second and creates an agent ping(T) to respond to the events. The loop after the agent is needed to make the agent perpetual.

AR has been found useful for programming simple concurrency, implementing constraint propagators, and developing interactive graphical user interfaces. It has served as an intermediate language for compiling Constraint Handling Rules (CHR) and Answer Set Programs (ASP).

Read more about this topic:  B-Prolog

Famous quotes containing the words action and/or rules:

    Suit the action to the word, the word to the action, with this special observance, that you o’erstep not the modesty of nature.
    William Shakespeare (1564–1616)

    Can rules or tutors educate
    The semigod whom we await?
    He must be musical,
    Tremulous, impressional,
    Alive to gentle influence
    Of landscape and of sky
    And tender to the spirit-touch
    Of man’s or maiden’s eye.
    Ralph Waldo Emerson (1803–1882)