A Linguistic Symbiosis Approach to Bring the Declarative Power of Prolog to Java
Total Page:16
File Type:pdf, Size:1020Kb
LogicObjects : A Linguistic Symbiosis Approach to Bring the Declarative Power of Prolog to Java Sergio Castro Kim Mens Paulo Moura RELEASeD lab RELEASeD lab Dept. of Computer Science ICTEAM Institute ICTEAM Institute University of Beira Interior Université catholique de Université catholique de & CRACS, INESC–TEC Louvain, Belgium Louvain, Belgium Portugal [email protected] [email protected] [email protected] ABSTRACT Although there exists some other work that focusses on Logic programming is well suited for declaratively solving facilitating the interaction from an object-oriented language computational problems that require knowledge representa- to a logic language, to the best of our knowledge this is the tion and reasoning. Object-oriented languages, on the other first approach that provides a truly transparent, automatic hand, are well suited for modeling real-world concepts and and customizable integration from the perspective of the profit from rich ecosystems developed around them, which object-oriented language. are often missing from logic languages. For applications that This paper is structured as follows: Section 2 presents a require both the declarative power of logic programming and problem of declarative nature and its implementation in a the rich modeling expressiveness and development environ- logic language. This will be referenced in the next sections. ments offered by object-oriented languages, there is a need Section 3 presents our framework and shows how it enables for reconciling both worlds. LogicObjects is our linguistic a transparent access from Java to our implementation in symbiosis framework for integrating Prolog within the Java logic. Section 4 discuss relevant related work and section 5 language. It extends Java with annotations that allow Java presents our conclusions and future work. programs to interact transparently and automatically with Prolog programs. 2. CASE STUDY: THE LONDON UNDERGROUND This case addresses a typical problem that can be imple- Keywords mented easily with a logic programming language: a query- ing system about subway lines and stations in a big city Linguistic Symbiosis, Object-Oriented Programming, Logic (e.g., to query the number of intermediate stations from Programming, Multi-paradigm programming one station to another). Since most public transport sys- tems require a user-friendly interface, which can be devel- 1. INTRODUCTION oped more easily with an object-oriented language, a good Object-oriented programming languages have demonstra- solution would be to implement the declarative part of the ted their usefulness for modeling real-world concepts. In application in Prolog and the user interface in Java. addition, the availability of continuously growing software The first stage of the problem consists in expressing our ecosystems around them, including advanced IDEs and ex- knowledge about the London Underground in terms of logic tensive libraries, has contributed to their success. Declara- statements. Most of the code in this section has been taken tive languages like Prolog, however, are more convenient for \as is" from [7]. However, we did introduce an interesting expressing problems of declarative nature, such as rule-based variation in the original code. Instead of implementing it systems [6, 15]. in plain Prolog, we used Logtalk [12], a portable object- Linguistic symbiosis [11] has been used in the past to solve oriented layer on top of Prolog, thus allowing us to benefit the problem of integrating programs written in different lan- from the symbiosis features it offers while at the same time guages. In particular, the SOUL [9] language implements ad- facilitating the mapping that needs to be made between ob- vanced language symbiosis features between Smalltalk and jects belonging to the two worlds. Prolog [6, 10]. Some limits and issues with SOUL have been In our universe of discourse, stations are connected to discussed in [9]. This work is a first step towards a frame- other stations through underground lines. A station is nearby work that overcomes most of these limitations. another one if there is at most one station in between them. Finally, a station A is reachable from another station B if there exists a list of stations L that form a path going from B to A. Permission to make digital or hard copies of all or part of this work for Listing 1 shows the Logtalk definition of the metro object personal or classroom use is granted without fee provided that copies are (we omitted some predicate clauses to save space). Note not made or distributed for profit or commercial advantage and that copies that, although Logtalk also supports classes, we are not us- bear this notice and the full citation on the first page. To copy otherwise, to ing the word class here, since we are just defining a prototype republish, to post on servers or to redistribute to lists, requires prior specific object and to avoid confusion with Java classes. The metro permission and/or a fee. RAM-SE’12, June 13, 2012, Beijing, China object encapsulates the knowledge about how stations are Copyright 2012 ACM 978-1-4503-1277-6/12/06 ...$10.00. connected (lines 5{8), plus the rules for the logic predicates nearby/2 (lines 10{11) and reachable/3 (lines 13{14). The messages (queries) that the metro object can answer are specified using the public/1 directive (line 2). 1 :− object ( metro ) . 2 3: − public ([connected/3, nearby/2, reachable/3]). 4 5 connected(station(bond s t r e e t ) , station(oxford circus),line(central)). 6 connected(station(oxford c i r c u s ) , station(tottenham c o u r t road),line(central)). 7 connected(station(bond s t r e e t ) , station(green park),line(jubilee)). 8... 9 10 nearby(X,Y):− connected(X,Y,L) . 11 nearby(X,Y):− connected(X,Z,L) ,connected(Z,Y,L) . 12 Figure 1: Objects from two different worlds living 13 reachable(X,Y,[]):− connected(X,Y,L) . 14 reachable(X,Y,[Z jR]):− connected(X,Z,L) ,reachable(Z,Y,R) . in symbiosis. 15 16 :− end object . 11) answers if the station is nearby another station received Listing 1: The metro object in Logtalk as a parameter. The method reachable/1 (line 13) answers Messages are sent in Logtalk using the ::/2 operator. For if the station received as parameter is reachable from the example, to find which stations are connected to the station receiver station object (or answers the reachable stations if Bond Street we can use the query shown in listing 2. the parameter is an unbound variable). The method reach- able/2 (line 15) does the same, but includes in its second metro :: connected(station(bond street), Station , Line). argument a list with all the intermediate stations. All these methods are delegated to the metro object. Listing 2: Invoking a Logtalk method 1 :− object ( s t a t i o n ( Name ) ) . 2 Logtalk and Prolog both support non-deterministic queries, 3: − public ([name/1, connected/1, connected/2, nearby/1, reachable/1, reachable/2]). which allows retrieving all existing solutions for a query us- 4 5 name(Name) :− parameter(1, Name). ing backtracking, and meta-programming, which allows e.g. 6 to construct a list with all solutions to a query. For exam- 7 connected(Station) :− connected(Station , ). 8 ple, to get a list of all stations connected to Bond Street we 9 connected(Station, L) :− self(Self), metro :: connected(Self , Station , L). could write the query shown in listing 3. 10 11 nearby(Station) :− self(Self), metro :: nearby(Self , f i n d a l l ( S t a t i o n ) . S t a t i o n , 12 metro :: connected(station(bond street), Station , Line), 13 reachable(Station) :− reachable(Station , ). S t a t i o n s 14 ). 15 reachable(Station , IntermediateStations) :− self(Self), metro :: reachable(Self , Station , IntermediateStations). 16 Listing 3: Invoking a Logtalk method using the 17 :− end object . findall/3 meta-predicate Listing 5: The station object in Logtalk Listing 4 shows the definition of a parametric object [13] line/1. Finally, we show the loader file of our library. A loader file 1 :− object ( l i n e ( Name ) ) . defines the collection of objects that should be loaded (line 2 2) when requiring a Logtalk library. We further discuss these 3: − public ([name/1, connects/2]) . 4 objects in the next section. 5 name(Name) :− parameter(1, Name). 6 7 connects(Station1, Station2) :− self(Self), 1 :− initialization ( metro :: connected(Station1 , Station2 , Self). 2 logtalk load([metro, station , line]) 8 3). 9 :− end object . Listing 6: The load all.lgt loader file Listing 4: The line object in Logtalk The object's sole parameter (line 1) can be retrieved with the method name/1 (line 5). This object also defines a con- 3. LOGICOBJECTS nects/2 method (line 7) that answers stations directly con- In the previous section we have shown the implementa- nected by the line represented by the receiver object. This tion of the declarative part of our case study using Logtalk method implementation is delegated to the metro object. objects. In this section, we add Java counterparts to these Our last object is the station object (Listing 5). As for objects and show how objects from the Java world can sym- the line object, it is also a parametric object having as sole biotically interact with objects in Logtalk. All these objects parameter the name of a station (line 1). It defines a method are illustrated in figure 1. connected/1 (line 7) that answers if the station is connected In the remainder of this section we describe the linguistic with another station (or answers the stations that are con- symbiosis techniques employed by our LogicObjects frame- nected if the parameter is an unbound variable).