Embedding Finite Sets in a Logic Programming Language
Total Page:16
File Type:pdf, Size:1020Kb
Embedding Finite Sets in a Logic Programming Language Agostino Dovier Eugenio G. Omodeo Dipartimento di Informatica Dip. di Informatica e Sistemistica Università di Pisa Università di Roma La Sapienza C.so Italia 40, 56100 PISA Via Salaria 113, 00198 ROMA [email protected] [email protected] Enrico Pontelli Gianfranco Rossi New Mexico State Univ. Dip. di Matematica Dept. of Computer Science Università di Bologna Las Cruces, NM 88003 P.zza di Porta S.Donato 5, [email protected] 40127 BOLOGNA [email protected] Abstract. A way of introducing simple (finite) set designations and operations as first-class objects of an (unrestricted) logic programming language is discussed from both the declarative and the operational semantics viewpoint. First, special set terms are added to definite Horn clause logic and an extended Herbrand Universe based on an axiomatic characterization of the kind of sets we are dealing with is defined accordingly. Moreover, distinguished predicates representing set membership and equality are added to the base language along with their negative counterparts (π and ). A new unification algorithm which can cope with set terms is developed and proved to terminate. Usual SLD resolution is modified so as to incorporate the new unification algorithm and to properly manage the distinguished predicates for set operations (in particular, conjunctions of atoms containing π and are dealt with as constraints, first reduced to a canonical form through a suitable canonization algorithm). Finally, the application of the resulting language to the definition of Restricted Universal Quantifiers is discussed. 1 Introduction General agreement exists about the usefulness of sets as very high-level representations of complex data structures (cf. [2]) and of set abstractions in code specifications. In particular, sets and set abstractions can be conveniently used in rapid software prototyping, where efficiency is not a primary requirement while the availability of high level data and operation abstractions are key features of the prototype implementation language. Only relatively few programming languages embody sets as primitive objects. Among them, the (non-executable) specification language Z [26], the procedural language SETL [5, 24] and the functional languages MIRANDA [25] and ME TOO [18]. A number of recent proposals have preferred, however, a more declarative programming framework. In particular, logic programming languages have been advocated by many as a suitable setting for declarative programming with sets. Among them, the proposals of the logic database languages LDL [3] and LPS [11]. Also the recent work by Legeard and Legros [13] introduces sets in a Prolog-like language dealing with set expressions as constraints, while [23] analyzes the problem of logic programming with sets in its generality (including infinite sets). A similar framework but stressing the notion of equational programming is also advocated in [9]. Actual Prolog systems already provide some facilities to support sets in the form of the built-in predicates setof and bagof. However, it is widely recognized that the definition of these facilities is quite unsatisfactory. In fact, no precise logical semantics can be attributed to them. Sets are simply represented as lists and therefore are dealt with as ordinary terms (e.g. by an implicit order among their elements). On the contrary, a logic language embodying sets ought to supply special set former terms along with a few basic operations on them as part of the language, and allow other more complex operations on sets (possibly including the setof operation), to become definable within the language itself, without having to further extend its semantics. Aim of this paper is to define such an extended logic language, called {log} (read set-log). Unlike some of the above cited proposals, however, we do not restrict ourselves to any specific application domain. So our starting point is a pure logic programming language, that is Definite Horn clauses with no extra-logical constructs. Then simple set constructs are added to this language (namely, enumerated set terms, set membership and equality and their negative counterparts) and suitable operational and declarative semantics are defined for them. It is shown that the usual set operations, such as union, intersection, etc., can be defined in this extended language. Also Restricted Universal Quantifiers are shown to be definable within the language itself; so they can be added to it as a simple syntactic extension (dealt with by a suitable preprocessor). The paper is organized as follows. Section 2 briefly describes a few extensions to ordinary logic programming language syntax that can support the introduction of set terms. Declarative semantics of the resulting language is presented in section 3 by defining an extended Herbrand Universe based on an axiomatic characterization of the kind of sets we are dealing with. The problem of unification of set terms is then addressed in section 4 and a new unification algorithm which can deal with set terms is described in detail. Section 5 presents the extended SLD procedure incorporating the new unification algorithm and the management of , =, and π, based on the so called constraint canonization algorithm. Section 6 shows how Restricted Universal Quantifiers can be defined in our language. Finally, a comparison with some other related proposals is carried out in section 7. 2 {log} syntax To begin with, we introduce in this section the extensional representation of finite sets. All that is presupposed is the availability of: - an interpreted constant, {}, for the empty set Ø; - a binary function symbol, with (used as an infix left associative operator), to be interpreted as follows: s with t stands for the set that results from adding t as a new element to the set s. Apart from these two symbols, {log} contains the usual equipment of clausal Horn logic (cf. [14]) along with distinguished predicates for set membership and equality ( and =) and their negative counterparts (π and ). The extensional representation for sets referred to above is provided by a collection G of ground terms: this is the smallest collection such that - the constant {} belongs to G ; - s with t, s, t ground terms (not necessarily in G), belongs to G . In view of the intended interpretation, any s in G will be called a set term. A non-ground term t is called a set term if there exists an instantiation σ of the variables in t such that tσ belongs to G; in particular a variable is a set term. Set terms of the form t with tn with ... with t1 wherein t is not a set term are intended to designate sets based on a kernel t other than {} (also called colored sets). For the sake of simplicity we introduce special syntactic forms to designate set terms: {t1,...,tn|s} stands for s with tn with ... with t1 and {t1,...,tn} stands for {} with tn with ... with t1 where n ≥ 1 and s, t1,...,tn are terms. For example: - f(a,{5}), i.e. f(a,{} with 5), is a term, but not a set term; - {2,g(3),a}, i.e. {} with a with g(3) with 2, is a ground set term; - {}, {1,X,Y,2}, {1,1,{2,{}},f(a,{b})} and any term {t1,...,tn|R} with a 'tail' variable R, are set terms - {a|f({b})} is a colored set term based on the kernel f({b}). For the rest of this paper, we will freely exploit the syntactic features available in Edinburgh Prolog in addition to the constructs discussed above. Three sample {log} clauses are: - q(X) :- X {a,b,c,d} & p(X) - singleton(X) :- X = {Y}. - in_difference(X,Set1,Set2) :- X Set1 & X Set2. Colored set terms (i.e. terms of the form {t1,...,tn|t} wherein t is not a set term) do not designate sets of any conventional kind; one might therefore contend that such terms ought to be forbidden as they are unlikely to serve any serious purpose in programming. Nevertheless, we deem it convenient to always regard {t1,...,tn|t} as a legal set term when t1,...,tn, t are legal, to make the language structure absolutely uniform and the inference mechanisms (e.g. unification) more straightforward. 3 {log} declarative semantics To convey the meaning of our language, we are to formally characterize the legal interpretations of {log}. We begin by providing the axioms of a suitable first-order set theory with equality: the legal interpretations of {log} will be the models of these axioms (cf. [8]). Next we will focus on a privileged interpretation domain formed by terms, UH, resulting from suitable modifications to the classical Herbrand universe [14]; moreover, we will designate a fixed binary relation over UH as the privileged interpretation of . (The predicate = will be interpreted as syntactic equality). The interpretation of the predicate symbols of {log} other than or = is left totally unspecified for the time being, as it must take into account a {log} program, viz. a set of application-specific clausal axioms enriching the initial axiom endowment we are presenting now. 3.1 Set axioms Here are our basic axioms (X, Y, V, S and the Xis distinct variables ranging over the whole interpretation domain): (Z) V {}; (W1) V π Y ∅ (V X with Y × V X); (W2) Y X with Y; (L) Y X ∅ ∃z (Y z & X = z with Y); (K0) (∀z z X) ∅ (X = ker(X)); (K1) V ker(X); (K2) ker(V with Y) = ker(V); (E) (ker(X) = ker(Y) & ∀z (z X × z Y)) ∅ X = Y; (R) ∃z ∀w (w X ∅ (z X & w z)); (U) f(X1,...,Xn) π {} & V f(X1,...,Xn), where n is the arity of f and f/n {{}/0, ker/1, with/2}. Although rather conventional in the overall, and somewhat narrow in comparison to well-established set theories such as Zermelo-Fraenkel or von Neumann-Bernays-Gödel, our theory slightly deviates from the classical ones under two respects: - Presence of urelements.