=Paper= {{Paper |id=Vol-487/paper-3 |storemode=property |title=Towards a Logic Language and Framework for Web Programming |pdfUrl=https://ceur-ws.org/Vol-487/paper3.pdf |volume=Vol-487 |dblpUrl=https://dblp.org/rec/conf/birthday/PiancastelliOD08 }} ==Towards a Logic Language and Framework for Web Programming== https://ceur-ws.org/Vol-487/paper3.pdf
                 Il Milione: A Journey in the Computational Logic in Italy




       Verso un framework e un linguaggio logico per la
                    programmazione Web
Towards a Logic Language and Framework for Web Programming
           Giulio Piancastelli     Andrea Omicini          Enrico Denti




                                            15
                           Il Milione: A Journey in the Computational Logic in Italy




SOMMARIO/ABSTRACT                                                   system aimed at being respectful of the Web architecture
                                                                    and principles. Among logic technologies, tuProlog has
Nonostante la popolarità del World Wide Web come                   been explicitly designed to be one of the enabling com-
piattaforma di sviluppo, una adeguata descrizione dei               ponents of Internet-based infrastructures: its engineer-
suoi principi architetturali e criteri di progettazione è          ing properties make it suitable for use on the Web, where
stata ottenuta solo recentemente, grazie alla introduzione          logic programming allows modification of resource be-
dello stile architetturale REST (Representational State             haviour at runtime. Accordingly, in this paper we present a
Transfer), che definisce la risorsa come la fondamentale            Prolog-based logic model for programming Web resources,
astrazione della informazione. Difatti, i linguaggi e               and outline a framework for developing Web applications
gli strumenti correntemente usati per programmare il                grounded on that model.
Web soffrono in genere della mancanza di una corretta
comprensione dei suoi vincoli architetturali e progettuali,         Keywords: World Wide Web, REST, Contextual Logic
e di una difformità tra le astrazioni di programmazione che        Programming, tuProlog, Prolog.
rende difficile sfruttare appieno le potenzialità del Web.
I linguaggi dichiarativi sono particolarmente adatti alla           1   Motivation and Background
costruzione di sistemi di programmazione rispettosi della
architettura e dei principi del Web. Tra le tecnologie              Despite the World Wide Web steadily gaining popularity
di programmazione logica, tuProlog è espressamente                 as the platform of choice for the development and fruition
progettato per essere uno dei componenti abilitanti di              of many kinds of Internet-based systems, a proper descrip-
infrastrutture basate su Internet: le sue proprietà in-            tion of the Web architectural principles and design criteria
gegneristiche lo rendono peraltro adatto per il Web,                has been achieved only recently, by the introduction of the
dove la programmazione logica permette la modifica                  Representational State Transfer (REST) architectural style
del comportamento delle risorse a tempo di esecuzione.              for distributed hypermedia systems [4]. REST defines the
Di conseguenza, questo articolo presenta un modello di              resource as the key abstraction of information, and pre-
programmazione logica per risorse Web basato su Prolog              scribes communication and interaction among resources to
e delinea un framework per sviluppare applicazioni Web              occur through a uniform interface by transferring a repre-
fondato su quel modello.                                            sentation of a resource current state.
                                                                       Yet, from the early years of procedural CGI scripts to
 Despite the popularity of the World Wide Web as a de-              the modern days of object-oriented frameworks, Web ap-
velopment platform, a proper description of its architec-           plication programming has always focussed on different
tural principles and design criteria has been achieved only         abstractions, such as page [5], controller [11], and more
recently, by the introduction of the Representational State         recently service [7], thus suffering from a mismatch that
Transfer (REST) architectural style which defines the re-           has made it difficult to exploit the full potential of the Web
source as the key abstraction of information. In fact, lan-         architectural properties. In fact, a page is just the result of
guages and tools currently used for Web programming gen-            a computation involving one or more resources, and deals
erally suffer from a lack of proper understanding of its ar-        only with representation issues on the client side. A con-
chitecture and design constraints, and from an abstraction          troller happens to be a programming framework abstrac-
mismatch that makes it hard to exploit the Web potential.           tion, sharing almost nothing with the underlying Web plat-
Declarative languages are well-suited for a programming             form. Finally, services disregard Web standards such as

                                                               16
                                  Il Milione: A Journey in the Computational Logic in Italy




URI and HTTP, so they do not get the benefits of the REST        and have a definite structure varying in predictable ways
architecture in terms of cacheability, connectedness, ad-        [10], they feature an interesting property on their own:
dressability, uniformity, and interoperability [10].             any path can be interpreted as including a set of resource
   Declarative programming has never been accepted into          names. More precisely, we say that resource names such
the Web mainstream, even though logic languages have             as the following:
shown they could effectively handle both communication
and co-ordination in a network-based context [1], and logic      http://example.com/sales/2004/Q4
technologies have been successfully used to engineer in-         encompass the names of other resources, and ultimately
telligent components at the core of Internet-based infras-       the name of the resource associated with the domain at the
tructures [2]. However, the REST focus on resource rep-          root of the URI:
resentations as the main driver of interaction, and the cor-
responding Web computation model, suggest that declara-          http://example.com/sales/2004
tive languages could play a significant role in the construc-    http://example.com/sales
tion of resource-oriented applications. The advantage of         http://example.com
using elements from logic programming languages such
                                                                 This naming structure suggests that each resource does not
as Prolog lies in the representational foundations of the
                                                                 exist in isolation, but lives in an information context com-
Web computation model: a declarative representation of
                                                                 posed by the resources associated to the names encom-
resources may be manipulated and, given the procedural
                                                                 passed by the name of that resource.
interpretation of Prolog clauses, directly executed by an
                                                                    To account for the possible complexity of Web computa-
interpreter when a resource is involved in a computation.
                                                                 tions that may involve more information than it is enclosed
   Accordingly, we define a resource programming model
                                                                 in a single isolated resource, the context C(R) is intro-
(called Web Logic Programming [9]), which exploits ele-
                                                                 duced as the locus of computation associated with each re-
ments of the logic paradigm and suitable logic technolo-
                                                                 source. The context of a resource is defined by the compo-
gies (i.e. the tuProlog engine [2]) so as to build a Web
                                                                 sition of the theories associated with the resources linked
application framework aimed at easing rapid prototyping,
                                                                 to names which are encompassed by that resource name,
and allowing the prototype to evolve while supporting Web
                                                                 including the theory associated with the resource itself.
architectural properties such as scalability or modifiability.
                                                                 Given a resource R with a name N (R) so that:

2     Web Logic Programming                                                  N (R) ⊆ N (R1 ) ⊆ . . . ⊆ N (Rn )

Web Logic Programming (WebLP) [9] is a Prolog-based              the associated context C(R) is generated by composition:
logic model to program resources and their interaction in
                                                                           C(R) = T (R) · T (R1 ) · . . . · T (Rn )
application systems following the constraints of the World
Wide Web architecture. To describe WebLP, we need both           where any theory T (Ri ), containing the knowledge base
to characterise its main data type abstraction and to define     associated to the resource Ri , can be empty – for instance
its underlying computation model.                                when there is no entity associated to the name N (Ri ).

2.1    Resources                                                 2.2    Computation Model
REST defines a resource as any conceptual target of an hy-       According to REST, the Web computation model revolves
pertext reference. Any information that can be named can         around transactions in the HTTP protocol. Each transac-
be a resource, including virtual (e.g. a document) and non-      tion starts with a request, containing the two key elements
virtual (e.g. a person) objects. Starting from this abstract     of Web computations: the method information, that indi-
definition, the main properties of resources can be easily       cates how the sender expects the receiver to process the
determined: a name (in the form of an URI); data, repre-         request, and the scope information, that indicates on which
senting the resource state; and behaviour, to be used, for       part of the data set the receiver should apply the method
instance, to change state or manage the interaction with         [10]. On the Web, the method information is contained in
other resources. The defining elements of resources can          the HTTP request method (e.g. GET, POST), and the scope
be easily mapped onto elements of well-known logic pro-          information is the URI of the resource to which the request
gramming languages such as Prolog. For each resource R           is directed. The result of a Web computation is a response,
we specify its name N (R) as the single quoted atom con-         telling whether the request has been successful or not, and
taining the resource URI identifier; data and behaviour can      optionally containing the representation of the new state of
be further recognised as facts and rules, respectively, in a     the target resource.
logic theory T (R) containing the knowledge base associ-            Adopting a logic programming view of the Web compu-
ated to the resource.                                            tation model, for each HTTP transaction the request can be
   In particular, if adopted resource names are descriptive      translated to represent a deduction by retaining the scope

                                                                   17
                           Il Milione: A Journey in the Computational Logic in Italy




information to indicate the target theory, and by mapping
the method information onto a proper logic goal. Then, the
computation takes place on the server side of the HTTP
transaction, in the context associated to the resource target
of the request. Finally, the information resulting from goal
solution is translated again into a suitable representation
and sent back in the HTTP response.
   A computation invoked by a goal G on a resource R trig-
gers the deduction of G on the context C(R). The com-
position of theories forming C(R) is then traversed in a
very similar way as units in Contextual Logic Program-
ming (CtxLP) [6]. The goal G is asked in turn to each the-
ory: the goal fails if no solution is found in any theory, or        Figure 2: The logic theory of a resource representing sales
succeeds as soon as it is solved using the knowledge base            for the fourth quarter of 2004 can be identified by two dif-
in a theory T (Ri ). Furthermore, when the goal G is substi-         ferent names and therefore live in two different contexts.
tuted by the subgoals of the matching rule in the theory, by
default the computation proceeds from C(Ri ) rather than
being restarted from the original context.                           computation has been invoked) to the innermost, passing
   As an example, consider the user jdoe in a book-                  through the theories belonging to each of the composing
shelf sharing application, where her shelf is repre-                 resources, until the host resource is finally involved.
sented by the resource S, identified by the URI
http://example.com/jdoe/shelf. Suppose that,                         2.3   Dynamic Resource Behaviour
according to a proper naming scheme, the resource B for
biology books lives at /jdoe/shelf/biology. When                     The behaviour of a resource can be regarded as dynamic
a GET request is issued for that resource, a predicate               under two independent aspects. First, two or more URIs
pick biology books/1 is ultimately invoked on B,                     can be associated to the same resource at any time: that
depending on a pick books/3 predicate that is neither                is, the names N1 (R), . . . , Nm (R) may identify the same
defined in B nor in S. The theory chain in C(B) is then              resource R, thus the same knowledge base contained in
traversed backwards up to the http://example.com                     the theory T (R) associated to the resource. Each different
resource, as depicted in Figure 1, where a suitable defini-          name Ni (R) also identifies a different context Ci (R) that
tion for pick books/3 is finally found. Definitions for              the same resource R may live within, (see Figure 2); there-
other predicates invoked by it are then searched starting            fore, predicates that are used in T (R), but are not defined
from the context of the root resource, rather than C(B)              there, may behave in different ways based on the definition
where the computation originally started.                            given by the context where the resource is called.
   The fixed structure of URIs as resource identifiers makes            The second dynamic aspect of a resource comes from
the composition of theories forming a context static, dif-           the ability to express behavioural rules as first-class ab-
ferently from CtxLP, where it was possible to push or pop            stractions in a logic programming language: on the one
units from the context stack at runtime. The structure of            hand, well-known logic mechanisms for state manipula-
identifiers and resources in the Web architecture also dic-          tion (the assertz/1 and retract/1 predicates) can
tates a unique direction in which the theories associated            be exploited to change the knowledge base associated to
to resources composing a context can be traversed: from              a resource; on the other, the HTTP protocol itself allows
the outermost (associated with the resource on which the             changing a resource by means of a PUT request, whose
                                                                     content should be considered as a modified version of the
                                                                     target resource that has to replace (or be merged with) the
                                                                     original version residing on the server.
                                                                        As an example, imagine a reading wish list in the previ-
                                                                     ous bookshelf application. Usually, when a book is added,
                                                                     the resource representing the wish list could check local
                                                                     libraries for book availability, and possibly borrow it on
                                                                     user’s behalf; if no book can be found, the resource could
                                                                     check its availability in online bookstores, reporting its
Figure 1: The /jdoe/shelf/biology resource re-                       price to the user for future purchase. During the period of
sponds to a HTTP GET request by eventually invoking the              time when an online bookstore offers discounts, the wish
pick biology book/1 predicate, which in turn calls                   list resource should react to the insertion of new books so
pick books/3. The context is traversed until a proper                as to check that store first instead of libraries.
definition for it is found in the / resource.                           The Web application could then be instructed to change

                                                                18
                                 Il Milione: A Journey in the Computational Logic in Italy




the behaviour of wish list resources by issuing HTTP PUT        4    Future Work
requests that modify the computational representation of
those resources. The PUT requests would carry the new           The development of tuProlog as a server-side Web technol-
rules in the content, so that wish list resources would ac-     ogy and of the WebLP framework will be the main focus
cordingly modify their knowledge base. The application          of our activity in the near future. Afterwards, we also plan
could programmatically restore the old behaviour at the         to explore possible extensions of the programming model,
end of the discount period, by sending another PUT request      mostly based on experience in building a variety of appli-
containing the previous rule set for each wish list resource.   cations on the framework.


3 tuProlog: Logic Technology for the Web                        REFERENCES
                                                                 [1] E. Denti and A. Omicini. Engineering multi-agent
tuProlog is a minimal Java-based system explicitly de-
                                                                     systems in LuCe. In International Workshop on
signed to integrate configurable and scalable Prolog com-
                                                                     Multi-Agent Systems in Logic Programming (MAS
ponents into standard Internet applications, and to be used
                                                                     ’99), Las Cruces, NM, USA, 30 November 1999.
as the core enabling technology for the provision of basic
coordination capabilities into complex Internet-based in-        [2] E. Denti, A. Omicini, and A. Ricci. tuProlog: A
frastructures [2]. Alongside configurability and scalability,        light-weight Prolog for Internet applications and in-
tuProlog has been designed to offer additional engineer-             frastructures. In I.V. Ramakrishnan, editor, Practical
ing properties suitable for distributed systems and archi-           Aspects of Declarative Languages, volume 1990 of
tectures: ease of deployment, lightness, and interoperabil-          LNCS, pages 184–198. Springer, 2001.
ity in accordance with standard protocols (RMI, CORBA,
TCP/IP). Those properties are a good match for the archi-        [3] E. Denti, A. Omicini, and A. Ricci. Multi-paradigm
tectural properties described by REST, so that tuProlog can          Java-Prolog integration in tuProlog. Science of Com-
be reasonably employed as the core inference engine tak-             puter Programming, 57(2):217–250, August 2005.
ing care of resource computations and interactions.
                                                                 [4] R. T. Fielding. Architectural Styles and the Design of
   With the aim of sketching a WebLP framework, a min-
                                                                     Network-based Software Architectures. PhD thesis,
imal Prolog engine such as tuProlog would need to be
                                                                     University of California, Irvine, 2000.
augmented with a construct very similar to logic contexts,
for which various implementation techniques exist, rang-         [5] R. Lerdorf and K. Tatroe.        Programming PHP.
ing from the least intrusive meta-interpretation to the most         O’Reilly, April 2002.
effective virtual machine enhancing. With a similar in-
tent, the architecture of tuProlog has been recently re-         [6] L. Monteiro and A. Porto. A Language for Con-
engineered to feature the malleability property [8], espe-           textual Logic Programming. In Logic Programming
cially important in allowing a light-weight Prolog technol-          Languages: Constraints, Functions, and Objects.
ogy to be extended with similar ease as the Prolog basic ex-         The MIT Press, 1993.
ecution model can be extended on the pure linguistic side.       [7] E. Newcomer and G. Lomow. Understanding SOA
   The pervasive integration with Java featured by tuProlog          with Web Services. Addison-Wesley, 2005.
[3] is so much important as we consider how much an es-
tablished platform for Web development Java has become           [8] G. Piancastelli, A. Benini, A. Omicini, and A. Ricci.
in the latest years. In order to build a WebLP framework,            The architecture and design of a malleable object-
some Java technology which has proven itself effective               oriented Prolog engine. In 23th ACM Symposium
for some parts of the Web computation model can be ex-               on Applied Computing (SAC 2008), Fortaleza, Ceará,
ploited, provided that the abstractions underlying the tech-         Brazil, 16–20 March 2008.
nology are sound within the Web architectural style. As
a first example of such technology, the existing Apache          [9] G. Piancastelli and A. Omicini. A Logic Program-
Tomcat web server/container can be considered a multi-               ming model for Web resources. In 4th International
threaded efficient environment where tuProlog can be in-             Conference on Web Information Systems and Tech-
tegrated, exploiting component life-cycle management and             nologies (WEBIST 2008), Funchal, Madeira, Portu-
an HTTP uniform interface implementation. JavaServer                 gal, 4–7 May 2008.
Pages are a further example, as an extensible technology        [10] L. Richardson and S. Ruby. RESTful Web Services.
to produce resource representations to be consumed on the            O’Reilly, May 2007.
client side of Web applications. Where instead abstractions
suffer from mismatch with respect to the REST architec-         [11] D. Thomas, D. Heinemeier Hansson, L. Breedt,
tural style, as the case is for Java servlets used as appli-         M. Clark, T. Fuchs, and A. Schwarz. Agile Web De-
cation controllers, they can be dismissed or re-used with a          velopment with Rails. Pragmatic Bookshelf, 2005.
different purpose, for instance as mere HTTP dispatchers.

                                                                    19