B-PrologB-Prolog was a high-performance implementation of the standard Prolog language with several extended features including matching clauses, action rules for event handling, finite-domain constraint solving, arrays and hash tables, declarative loops, and tabling. First released in 1994, B-Prolog is now a widely used CLP system. The constraint solver of B-Prolog was ranked top in two categories in the Second International Solvers Competition,[1] and it also took the second place in P class in the second ASP solver competition [2] and the second place overall in the third ASP solver competition.[3] B-Prolog underpins the PRISM system, a logic-based probabilistic reasoning and learning system. B-Prolog is a commercial product, but it can be used for learning and non-profit research purposes free of charge (since version 7.8 for individual users, including commercial individual users, B-Prolog is free of charge [4]). B-Prolog is not anymore actively developed, but it forms the basis for the Picat programming language. Matching clausesA matching clause is a form of a clause where the determinacy and input/output unifications are denoted explicitly. The compiler translates matching clauses into matching trees and generates indexes for all input arguments. The compilation of matching clauses is much simpler than that of normal Prolog clauses because no complex program analysis or specialization is necessary; and the generated code tends to be more compact and faster. The B-Prolog compiler and most of the library predicates are written in matching clauses. A matching clause takes the following form: H, G => B
where merge([],Ys,Zs) => Zs=Ys.
merge(Xs,[],Zs) => Zs=Xs.
merge([X|Xs],[Y|Ys],Zs),X<Y => Zs=[X|ZsT],merge(Xs,[Y|Ys],ZsT).
merge(Xs,[Y|Ys],Zs) => Zs=[Y|ZsT],merge(Xs,Ys,ZsT).
The cons merge([X|Xs],Ys,Zs),Ys=[Y|_],X<Y => Zs=[X|ZsT],merge(Xs,Ys,ZsT).
The call Action rulesThe 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 A set of built-in events is provided for programming constraint propagators and interactive graphical user interfaces. For example, Consider the following examples: echo(X),{event(X,Mes)}=>writeln(Mes).
ping(T),{time(T)} => writeln(ping).
The agent ?-echo(X),post(event(X,hello)),post(event(X,world)).
outputs the message ?-timer(T,1000),ping(T),repeat,fail.
creates a timer that posts a time event every second and creates an agent 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). CLP(FD)Like many Prolog-based finite-domain constraint solvers, B-Prolog's finite-domain solver was heavily influenced by the CHIP system. The first fully-fledged solver was released with B-Prolog version 2.1 in March 1997. That solver was implemented in an early version of AR, called delay clauses. During the past decade, the implementation language AR has been extended to support a rich class of domain events ( Thanks to the employment of AR as the implementation language, the constraint solving part of B-Prolog is relatively small (3800 lines of Prolog code and 6000 lines of C code, including comments and spaces) but its performance is very competitive. The AR language is open to the user for implementing problem-specific propagators. For example, the following defines a propagator for maintaining arc consistency for the constraint 'X_in_C_Y_ac'(X,Y,C),var(X),var(Y),
{dom(Y,Ey)}
=>
Ex is C-Ey,
domain_set_false(X,Ex).
'X_in_C_Y_ac'(X,Y,C) => true.
Arrays and the array subscript notationIn B-Prolog, the maximum arity of a structure is 65535. This entails that a structure can be used as a one-dimensional array, and a multi-dimensional array can be represented as a structure of structures. To facilitate creating arrays, B-Prolog provides a built-in, called The built-in predicate nth(I,L,E) :- E @= L[I].
Loops with foreach and list comprehensionProlog relies on recursion to describe loops. The lack of powerful loop constructs has arguably made Prolog less acceptable to beginners and less productive to experienced programmers because it is often tedious to define small auxiliary recursive predicates for loops. The emergence of constraint programming constructs such as CLP(FD) has further revealed this weakness of Prolog as a modeling language. B-Prolog provides a built-in, called The foreach(A in [a,b], I in 1..2, write((A,I)))
outputs four tuples X @= [(A,I) : A in [a,b], I in 1..2]
binds Calls to The loop constructs considerably enhance the modeling power of CLP(FD). The following gives a program for the N-queens problem in B-Prolog: queens(N):-
length(Qs,N),
Qs :: 1..N,
foreach(I in 1..N-1, J in I+1..N,
(Qs[I] #\= Qs[J],
abs(Qs[I]-Qs[J]) #\= J-I)),
labeling([ff],Qs),
writeln(Qs).
The array notation on lists helps shorten the description. Without it, the foreach(I in 1..N-1, J in I+1..N,[Qi,Qj],
(nth(Qs,I,Qi),
nth(Qs,J,Qj),
Qi #\= Qj,
abs(Qi-Qj) #\= J-I)),
where bool_queens(N):-
new_array(Qs,[N,N]),
Vars @= [Qs[I,J] : I in 1..N, J in 1..N],
Vars :: 0..1,
foreach(I in 1..N, % one queen in each row
sum([Qs[I,J] : J in 1..N]) #= 1),
foreach(J in 1..N, % one queen in each column
sum([Qs[I,J] : I in 1..N]) #= 1),
foreach(K in 1-N..N-1, % at most one queen in each left-down diag
sum([Qs[I,J] : I in 1..N, J in 1..N, I-J=:=K]) #=< 1),
foreach(K in 2..2*N, % at most one queen in each left-up diag
sum([Qs[I,J] : I in 1..N, J in 1..N, I+J=:=K]) #=< 1),
labeling(Vars),
foreach(I in 1..N,[Row],
(Row @= [Qs[I,J] : J in 1..N], writeln(Row))).
TablingTabling has been found increasingly important for not only helping beginners write workable declarative programs but also developing real-world applications such as natural language processing, model checking, and machine learning applications. B-Prolog implements a tabling mechanism, called linear tabling, which is based on iterative computation of looping subgoals rather than suspension of them to compute the fixed points. The PRISM system, which heavily relies on tabling, has been the main driving force for the design and implementation of B-Prolog's tabling system. The idea of tabling is to memorize the answers to tabled calls and use the answers to resolve subsequent variant calls. In B-Prolog, as in XSB, tabled predicates are declared explicitly by declarations in the following form: :-table P1/N1,...,Pk/Nk.
For example, the following tabled predicate defines the transitive closure of a relation as given by :-table path/2.
path(X,Y):-edge(X,Y).
path(X,Y):-path(X,Z),edge(Z,Y).
With tabling, any query to the program is guaranteed to terminate as long as the term sizes are bounded. By default, all the arguments of a tabled call are used in variant checking and all answers are tabled for a tabled predicate. B-Prolog supports table modes, which allow the system to use only input arguments in variant checking and table answers selectively. The table mode declaration :-table p(M1,...,Mn):C.
instructs the system how to do tabling on Table modes are very useful for declarative description of dynamic programming problems. For example, the following program encodes the Dijkstra's algorithm for finding a path with the minimum weight between a pair of nodes. :-table sp(+,+,-,min).
sp(X,Y,[(X,Y)],W) :-
edge(X,Y,W).
sp(X,Y,[(X,Z)|Path],W) :-
edge(X,Z,W1),
sp(Z,Y,Path,W2),
W is W1+W2.
The table mode states that only one path with the minimum weight is tabled for each pair of nodes. See alsoReferences
External links |
Portal di Ensiklopedia Dunia