Logic Programming with Monads and Comprehensions Yves Bekkers ? Paul Tarau y Abstract We give a logical reconstruction of all-solution predicates in terms of list comprehensions in > = Prolog 's and we describe a variety of logic programming constructs in terms of monads and monad morphisms . Novel monad structures are described for lazy function lists , clause unfoldings and a monad morphism based embedding of Prolog in > = Prolog is given . 1 Introduction Monads and comprehensions , have been successfully used in functional programming as a convenient generalization of various structurally similar programming constructs starting with simple ones like list processing and ending with intricate ones like CPS transformations and state transformers . We refer to the work of Philip Wadler -LSB- 16 , 17 -RSB- for a long list of powerful examples and to Moggi -LSB- 10 -RSB- for the categorist sources of the concept of monad . All-solution predicates are the logic programming siblings of list comprehensions in functional programming languages . Let us point out an interesting -LRB- although possibly episodical -RRB- parallel between these two programming paradigms . When doubts have been raised by Goguen 's thesis -LSB- 6 -RSB- on the usefulness of higher order constructs for functional programming , this seemed d?ej?a vu for logic programmers familiar with Warren 's paper -LSB- 18 -RSB- . After emulating some basic > = - calculus constructs in terms of first-order Horn-clause logic -LRB- i.e. what is also known as pure Prolog -RRB- Warren has shown that most of the expressiveness of higher-order constructs can be emulated in first order logic programming . All-solution predicates have been recognized by Warren as a notable exception and have been present in logic programming ever since . However , in the ? Universit?e de Rennes I and IRISA/INRIA Rennes , bekkers@irisa.fr yD?epartement d'Informatique , Universit?e de Moncton , tarau@info.umoncton.ca following years they have been quite often disregarded -LRB- see comp.lang.prolog 1983-1994 -RRB- as ` non-logical ' and ` non-declarative ' by the high -LRB- or tight ? -RRB- standards of the logic programming community . Interestingly enough , the idea of the intrinsic usefulness of higher-order constructs seems to be back again in the functional programming community after the proof of expressiveness of some essentially high-order monad constructs -LRB- used for instance in describing state transformers and constant time array operations -RRB- in the papers of Philip Wadler . Post-Prolog generation logic programming languages like > = Prolog make essential use of high-order unification and quantification to help the programmer specify the intended meaning of complex operations . We will propose in this paper a ` monad-based ' approach to the definition of all-solution predicates in > = Prolog -LSB- 12 -RSB- . As a follow-up , we obtain a clarification of Prolog 's high-order operators and their similarities and differences with monad comprehensions in functional languages . More generally , we will see various program transformations as morphisms between suitably defined monads and hint to some of their practical uses in compilation . Novel monad structures are described for lazy function-lists , clause unfoldings and a monad morphism based embedding of an Prolog in > = Prolog is given . In the examples that follow we will not limit ourself to Prolog but refer most of the time to its more powerful superset > = Prolog -LSB- 7 , 8 , 14 , 13 -RSB- -LRB- in its > = Prolog-Mali incarnation -LSB- 5 , 2 -RSB- , which has been used to run all the programming examples -RRB- . The paper is organized as follows : we start by describing some monads for elementary data structures the -LRB- lists , lazy function-lists etc. -RRB- and monad morphisms . After that , we describe how to implement list comprehensions with monads , how to express monad operations in terms of all-solution predicates and the reverse process of defining list comprehensions in terms of bagof . Finally we deal with metaprogramming with monads ; after describing programs as monads of clauses , we give an embedding of Prolog in > = Prolog . We conclude with the usual future work and conclusion sections . 2 Monads for elementary data structures 2.1 The list-monad in > = Prolog We will start with a definition of the list monad in > = Prolog . Note that > = Prolog . is a language with polymorphic types , and with lambda terms manipulated through high-order unification . Let us stress from the start that terms are mostly used as data structures and that > = Prolog is not a mixture of orthogonal functional and logic programming languages but a natural extension of Horn clause logic based on the concept of uniform proof -LSB- 9 -RSB- . However we will not use in this case any of the quantification related features of > = Prolog so that the code will run with minor modifications on any Prolog system having the call/N primitive introduced by -LSB- 11 -RSB- . % primitive operations % maps a single element of A to an object of the monad type unitList A - > -LRB- list A -RRB- - > o. unitList X -LSB- X -RSB- . % applies to a list an operation from elements to lists % to obtain a new list type bindList -LRB- list A -RRB- - > -LRB- A - > -LRB- list B -RRB- - > o -RRB- - > -LRB- list B -RRB- - > o. bindList -LSB- -RSB- _ K -LSB- -RSB- . bindList -LSB- X | Xs -RSB- K R : - K X Ys , bindList Xs K Zs , append Ys Zs R. % derived operations % concatenates the elements of a list of lists type joinList -LRB- list -LRB- list A -RRB- -RRB- - > -LRB- list A -RRB- - > o. joinList Xss Xs : - bindList Xss id Xs . % applies an operation to each element of a list type mapList -LRB- A - > B - > o -RRB- - > -LRB- list A -RRB- - > -LRB- list B -RRB- - > o. mapList F Xs Ys : - bindList Xs -LRB- compose F unitList -RRB- Ys . % tools type id A - > A - > o. id X X. % composes two predicates which implement functions type compose -LRB- A - > B - > o -RRB- - > -LRB- B - > C - > o -RRB- - > -LRB- A - > C - > o -RRB- . compose F G X Y : - F X Z , G Z Y. type dupList A - > -LRB- list A -RRB- - > o. dupList X -LSB- X , X -RSB- . For instance , the goal bindList -LSB- 1,2,3 -RSB- dupList R. will instantiate R to -LSB- 1,1,2,2,3,3 -RSB- . It is easy to verify -LRB- and actually programmable in > = Prolog -RRB- that all the monad axioms and properties of -LSB- 16 , 17 -RSB- hold for our definitions . 2.2 The monad of lazy function-lists in > = Prolog Lazy function-lists -LSB- 4 -RSB- are a surprising but natural ` difference ' data-structure which among other properties allows ` linear ' performance on apparently quadratic predicates like the well known ` naive reverse ' Prolog benchmark -LSB- 4 -RSB- . Basically this is achieved by a lazy implementation of reduction . We refer to -LSB- 3 , 4 -RSB- for the details of their operational semantics . A simple example -LRB- which can be seen as a unit operator -RRB- is x \ z \ -LSB- x | z -RSB- which applied to 1 gives \ z -LSB- 1 | z -RSB- . The following program1 implements the monad of lazy function-lists . % tools #define flistT -LRB- A -RRB- -LRB- -LRB- list A -RRB- - > -LRB- list A -RRB- -RRB- #define NIL x \ x #define CONS x \ l \ u \ -LSB- x | -LRB- l u -RRB- -RSB- #define CONC l1 \ l2 \ x \ -LRB- l1 -LRB- l2 x -RRB- -RRB- % primitive monad operations type unitFlist A - > flistT -LRB- A -RRB- - > o. unitFlist X -LRB- CONS X NIL -RRB- . type bindFlist flistT -LRB- A -RRB- - > -LRB- A - > flistT -LRB- B -RRB- - > o -RRB- - > flistT -LRB- B -RRB- - > o. bindFlist NIL _ K NIL . bindFlist -LRB- CONS X Xs -RRB- K -LRB- CONC Ys Zs -RRB- : - K X Ys , bindFlist Xs K Zs . % derived monad operations type joinFlist flistT -LRB- flistT -LRB- A -RRB- -RRB- - > flistT -LRB- A -RRB- - > o. joinFlist Xss Xs : - bindFlist Xss id Xs . type mapFlist -LRB- A - > B - > o -RRB- - > flistT -LRB- A -RRB- - > flistT -LRB- B -RRB- - > o. mapFlist F Xs Ys : - bindFlist Xs -LRB- compose F unitFlist -RRB- Ys . 1using the concrete syntax of > = Prolog-Mali , with parametric types obtained through macroexpansion 2.3 Other elementary monads 2.3.1 Constructor sequence monads By replacing the list constructor ' . ' / 2 and terminator -LSB- -RSB- / 0 by another pair we can generalize the list monad to a more general ` constructor sequence ' monad . Two widely used instances are : ffl the conjunction monad for < ' , ' , true > ffl the disjunction monad for < ' ; ' , fail > 2.3.2 The monad of difference lists The implementation of this monad is similar to that of the monads of lists and lazy function-lists . However , its correctness in the absence of occur check and the need for extra logical tests to detect ` the end ' of such lists make it less interesting especially when a concept of lazy function-list is available as it is the case with > = Prolog-Mali . 2.4 Monad morphisms A monad morphism -LSB- 16 -RSB- is an application which preserves the monad operations . Intuitively they correspond to programs which exhibit a similar structure inside different syntactic wrappers . Morphisms of monads are a convenient way to implement equivalence preserving program transformations . Due to space constraints we will point out here only a less obvious morphism pair between the monad of lists and the monad of lazy function lists . type list2flist -LRB- list A -RRB- - > flistT -LRB- A -RRB- - > o. list2flist L FL : - pi nil \ -LRB- append L nil -LRB- FL nil -RRB- -RRB- . type flist2list flistT -LRB- A -RRB- - > -LRB- list A -RRB- - > o. flist2list F -LRB- F -LSB- -RSB- -RRB- . List2flist/2 is similar to a list-to-difference list converter except for the use of the universal quantifier pi to end the lazy function list . Notice that flist2list/2 means simply applying the lazy function list to the list terminator -LSB- -RSB- . Let us just hint to an application : the programmer who works with monads is free to move from one representation to another . For instance he can debug a simpler ` plain ' list program and then chose a more efficient representation following a monad morphism . 3 List comprehensions with monads 3.1 Concrete notation for list comprehension in > = Prolog Let us consider two examples of set comprehensions : B1 = fxjx 2 L & odd xg B2 = f -LSB- x ; y -RSB- jx 2 L1 & y 2 L2g B1 is the set of odd elements in the set L , and B2 is the set of pairs of values taken from the sets L1 for the first value in the pair and L2 for the second value in the pair . The corresponding list comprehensions in > = Prolog are defined as follows : B1 < == -LRB- all x \ -LRB- x : -LRB- x < -- L , 1 is x mod 2 -RRB- -RRB- -RRB- B2 < == -LRB- all y \ -LRB- all x \ -LRB- -LSB- x , y -RSB- : -LRB- x < -- L1 , y < -- L2 -RRB- -RRB- -RRB- -RRB- The syntax for list comprehension is : lcTo : : = -LRB- all variablen lcTo -RRB- j -LRB- termo : qualifiers -RRB- qualifiers : : = qualifier j qualifier ; qualifiers j -LRB- qualifiers -RRB- qualifier : : = generator j filter generator : : = variable < -- list A filter is a Prolog goal like 1 is x mod 2 . Infix operators are used in > = Prolog with the following correspondence : Set comp . List comp . 2 < -- j : & , = < == Variables in list comprehension are explicitly quantified with the quantifier all . The constructors all and : define the List comprehension data type which we call lcT . Data Type for list comprehensions Here are the > = Prolog type declarations for implementing list comprehensions : kind lcT type - > type . type all -LRB- _ B - > -LRB- lcT A -RRB- -RRB- - > -LRB- lcT A -RRB- . type : A - > o - > -LRB- lcT A -RRB- . lcT is a type constructor , and all and : and data constructors for that type . The generator < -- is a 2-argument predicate , its type in > = Prolog is : % - X < -- + Xs : X is an element of the list Xs type < -- X - > -LRB- list X -RRB- - > o . The translation from list comprehensions to lists is made with the predicate < == . Its type is : type < == -LRB- list A -RRB- - > -LRB- lcT A -RRB- - > o. All operators have been declared infix -LRB- xfx -RRB- with priority 700 . Translating list comprehensions to lists with the list-monad P. Wadler -LSB- 16 -RSB- gives a translation of list comprehensions into list-monad functions as follows : -LSB- t | x < -- L -RSB- = -LRB- map > = x ! t L -RRB- -LSB- t | -LRB- p , q -RRB- -RSB- = -LRB- join -LSB- -LSB- tjq -RSB- jp -RSB- -RRB- -LSB- t | true -RSB- = -LRB- unit t -RRB- -LSB- t | b -RSB- = -LRB- if b then -LSB- t -RSB- else -LSB- -RSB- -RRB- Due to space limitations we will not give translation of the two last equivalence rules into the two last clauses in figure 1 . Note that if is expresed with conditional -LRB- P ? A ; B -RRB- of > = Prolog . The rule for composing qualifiers is also easily translated . First , a list of list comprehensions is built and stored in Xs . Then the list comprehensions are translated applying the predicate < == to Xs with the map predicate . The resulting list is finally flattened with the join predicate . Using quantifications and higher order unification The first rule in figure 1 interprets the quantifier all . A quantified variable is represented with the > = - variable of an abstraction such as F. For each quantified variable , a new universal constant x is created -LRB- using > = Prolog logical connector pi -RRB- . The > = - variable is then substituted by a simple application -LRB- F x -RRB- . > = Prolog - reduction insures the -LRB- lazy -RRB- copying of the term . Each new universal constant x is associated with a freshly created existentially quantified logical variable _ X . This association is memorized with > = Prolog logical connector = > . The dynamic assertion variable x _ X extends the program context . Notice that , according to > = Prolog quantification rules , the quantifiers on the two variables x and _ X are ordered as : 9 _ X 8 x The intuitionistic interpretation of such a declaration means that the signature of the logical variable _ X does not contain the universal constant x. Hence , > = Prolog system , subsequently enforces that the substitution values for _ X do not contain the universal constant x . This is fundamental for the rest of the interpretation , see further the interpretation of the generators < -- . % X < == Lc : X is the simple list corresponding to % the comprehension list Lc Ys < == -LRB- all F -RRB- : - ! , pi x \ -LRB- variable x _ X = > -LRB- Ys < == -LRB- F x -RRB- -RRB- -RRB- . Ys < == -LRB- -LRB- F U -RRB- : -LRB- U < -- Xs -RRB- -RRB- : - \ + -LRB- \ + -LRB- variable U F -RRB- -RRB- , ! , mapList x \ y \ -LRB- y = -LRB- F x -RRB- -RRB- Xs Ys . Zs < == -LRB- T : -LRB- P , Q -RRB- -RRB- : - ! , Xs < == -LRB- -LRB- T : Q -RRB- : P -RRB- , mapList x \ y \ -LRB- x < == y -RRB- Ys Xs , joinList Ys Zs . Ys < == -LRB- T : true -RRB- : - ! , unitList T Ys . Ys < == -LRB- T : P -RRB- : - -LRB- P ? unitList T Ys ; Ys = -LSB- -RSB- -RRB- . Figure 1 : From list comprehensions to lists The predicate variable is a dynamic predicate declared as follows : type variable A - > -LRB- A - > B -RRB- - > o. dynamic variable . > = Prolog higher order unification is used for interpreting the rule for the generators < -- . Higher order unification unifies the left term of the comprehension list with the term -LRB- F U -RRB- , and finds substitution values for the logical variable F . There may be several such substitutions , including some containing the universal constant U . The solutions containing the universal constant U are discarded with the interpretation of the goal \ + -LRB- \ + -LRB- variable U F -RRB- -RRB- . The substitution values for F will be filtered because the second argument for variable is a logical variable which does not contain the universal constant U in its signature . 3.1.1 Using list comprehensions Here are four examples of the use of list comprehensions : 1 . Generating the odd numbers of a list L 2 . Generating the pairs -LSB- x , y -RSB- with the values of x taken from a list L1 , and the values of y taken from a list L2 . 3 . Generating the pairs -LSB- x , y -RSB- with the values of x taken from a list L1 , and the values of y taken from a list L2 , such that y < > x. 4 . Generating the values -LSB- a , b , c -RSB- such that a2 + b2 = c2 . Odds < == -LRB- all x \ -LRB- x : -LRB- x < -- L , 1 is x mod 2 -RRB- -RRB- -RRB- Pairs < == -LRB- all y \ -LRB- v x \ -LRB- -LSB- x , y -RSB- : -LRB- x < -- L1 , y < -- L2 -RRB- -RRB- -RRB- -RRB- Pairs < == -LRB- all y \ -LRB- v x \ -LRB- -LSB- x , y -RSB- : -LRB- x < -- L1 , y < -- L2 , y =\ = x -RRB- -RRB- -RRB- -RRB- R < == -LRB- all c \ -LRB- all b \ -LRB- all a \ -LRB- -LSB- a , b , c -RSB- : -LRB- a < -- L , b < -- L , a < b , c < -- L ; a * a + b * b = : = c * c -RRB- -RRB- -RRB- -RRB- -RRB- Here is a quick sort program using list comprehensions . % sort X Y : list Y is a sorted version of list X type sort -LRB- list A -RRB- - > -LRB- list A -RRB- - > o. sort -LSB- -RSB- -LSB- -RSB- . sort -LSB- X | L -RSB- S : - Low < == -LRB- all y \ -LRB- y : -LRB- y < -- L , y < X -RRB- -RRB- -RRB- , sort Low SLow , Up < == -LRB- all y \ -LRB- y : -LRB- y < -- L , y > X -RRB- -RRB- -RRB- , sort Up SUp , append SLow -LSB- X | SUp -RSB- S. 4 Monad-based comprehensions vs. all-solution predicates 4.1 Expressing monad operations in terms of all-solution predicates The differences between comprehensions if functional languages and logic programming come mostly from the presence of nondeterminism , which will tend to return instances with renamed logical variables unless instructed otherwise with appropriate quantifiers . In > = Prolog-Mali , bagof is typed as follows : type bagof -LRB- A - > o -RRB- - > -LRB- list A -RRB- - > o. mode -LRB- setof + AbstractGoal ? SetOfSols -RRB- When executed it picks am unknown -LRB- logical variable -RRB- U and unifies the nonempty list of solutions for U in -LRB- AbstractGoal U -RRB- with SetOfSols . As in any Prolog , if there are unknowns in AbstractGoal -LRB- global unknowns -RRB- , bagof will backtrack on alternative solutions corresponding to different instantiations for global unknowns . With appropriate quantification -LRB- as available in > = Prolog -RRB- bagof can be used therefore as a practical way to implement for instance join and map as folows : type join -LRB- list -LRB- list T -RRB- -RRB- - > -LRB- list T -RRB- - > o. join Xss Ys : - bagof X \ -LRB- sigma Xs \ -LRB- member Xs Xss , member X Xs -RRB- -RRB- Ys . type map -LRB- A - > B - > o -RRB- - > -LRB- list A -RRB- - > -LRB- list B -RRB- - > o. map F Xs Ys : - bagof Y \ -LRB- sigma X \ -LRB- member X Xs , F X Y -RRB- -RRB- Ys . Except for reversibility and behaviour on non-ground F map will fonction as if defined by : map _ -LSB- -RSB- -LSB- -RSB- . map F -LSB- X | Xs -RSB- -LSB- Y | Ys -RSB- : - F X Y , map F Xs Y i.e. a goal like Xs = -LSB- A , B , B , A -RSB- , map unit Xs Zs . will return Zs =[ -LSB- A -RSB- , -LSB- B -RSB- , -LSB- B -RSB- , -LSB- A -RSB- -RSB- . Notice that in > = Prolog-Mali bagof -LRB- and also findall -RRB- allows to specify its intended use through explicit quantification . However , by replacing bagof with findall sharing is not preserved , i.e. something like Zs =[ -LSB- _ A -RSB- , -LSB- _ B -RSB- , -LSB- _ C -RSB- , -LSB- _ D -RSB- -RSB- . is returned . 4.2 Defining list comprehensions with bagof The following is a translation of our list comprehensions with the builtin predicate bagof . This translation first converts the list comprehension into an abstraction wich is given as an argument to the predicate bagof . The conversion is made with the predicate get_list . Notice the terminal case which builds the abstraction by merely tranforming a list comprehension -LRB- T : P -RRB- into the abstraction x \ -LRB- x = T , P -RRB- . Notice also the definition of the predicate < -- which is directly mapped onto the non-deterministic predicate member . In our previous definition of list comprehension , the predicate < -- did not need to be defined as it was meta-interpreted . % get_list + Lc - Goal type get_list -LRB- lcT A -RRB- - > -LRB- A - > o -RRB- - > o. get_list -LRB- all F -RRB- y \ -LRB- sigma x \ -LRB- R x y -RRB- -RRB- : - ! , pi x \ -LRB- get_list -LRB- F x -RRB- y \ -LRB- R x y -RRB- -RRB- . get_list -LRB- T : P -RRB- x \ -LRB- x = T , P -RRB- . X < -- Xs : - member X Xs . Ys < == P : - get_list P G , bagof G Ys . 5 Metaprogramming with monads 5.1 Programs as monads of clauses Let us start from simple -LRB- algebraic -RRB- clause unfolding operation : Definition 1 Let A0 : - A1 , A2 , : : : , An and B0 : - B1 , : : : , Bm be two clauses -LRB- suppose n > ; m > = -RRB- . We define -LRB- A0 : - A1 , A2 , : : : , An -RRB- ? -LRB- B0 : - B1 , : : : , Bm -RRB- = -LRB- A0 : - B1 , : : : , Bm , A2 , : : : , An -RRB- ? with ? = mgu -LRB- A1 , B0 -RRB- . If the atoms A1 and B0 do not unify , the result of the composition is denoted as ? . Furthermore , as usual , we consider A0 : - true , A2 , : : : , An to be equivalent to A0 : - A2 , : : : , An , and for any clause C , ?? C = C ? ? = ? . We suppose that at least one operand has been renamed apart to a variant with fresh variables . Prolog programs and their evaluation is expressed naturally in terms of the monad of clause unfoldings , as described by the folowing Prolog meta-program . unitClause -LRB- Cs , G , -LSB- A -RSB- -RRB- : - A = -LRB- G : - -LSB- G -RSB- -RRB- . joinClause -LRB- Cs , Gs , NewGs -RRB- : - findall -LRB- NewG , expandClause -LRB- Cs , Gs , NewG -RRB- , NewGs -RRB- . expandClause -LRB- Cs , Gs , NewG -RRB- : - member -LRB- G , Gs -RRB- , member -LRB- C , Cs -RRB- , composeClause -LRB- G , C , NewG -RRB- . composeClause -LRB- -LRB- H :-[ -RSB- -RRB- , _ , -LRB- H :-[ -RSB- -RRB- -RRB- . composeClause -LRB- -LRB- H : - -LSB- B | Bs -RSB- -RRB- , -LRB- B : - NewBs -RRB- , -LRB- H : - Gs -RRB- -RRB- : - append -LRB- NewBs , Bs , Gs -RRB- . Notice that monad operations are parameterized with respect to a fixed set of clauses Cs -LRB- i.e. a given ` program ' -RRB- . A Prolog resolution step -LRB- depth first search with leftmost selection rule -RRB- can be conveniently described as expandClause . Multiple parallel goal rewriting is naturally expressed as joinClause . A more efficient version is obtained by following the morphism from the monad of lists to the monad of difference lists . 5.2 An embedding of Prolog in > = Prolog Using the natural monad structure induced by the Prolog style clause unfolding operation ? we will give a straightforward embedding of Prolog in > = Prolog . We can map an arbitrary -LRB- untyped -RRB- Prolog term to a universal type in > = Prolog as follows : kind prologT type . type pINT int - > prologT . type pFLOAT float - > prologT . type pSTRUCT -LRB- list int -RRB- - > -LRB- list prologT -RRB- - > prologT . It is easy to verify that by mapping variables to variables on both sides , the mapping will commute with unfoldings . Therefore it will also commute with finite sequences of resolution steps . This allows us to write down a basic Prolog to > = Prolog compiler in a few dozen lines . With this straightforward scheme , the compilation of app -LRB- -LSB- -RSB- , Ys , Ys -RRB- . app -LRB- -LSB- A | Xs -RSB- , Ys , -LSB- A | Zs -RSB- -RRB- : - app -LRB- Xs , Ys , Zs -RRB- . becomes : type demo prologT - > o. demo -LRB- pSTRUCT `` app '' -LSB- -LRB- pSTRUCT '' -LSB- -RSB- '' -LSB- -RSB- -RRB- , A , A -RSB- -RRB- . demo -LRB- pSTRUCT `` app '' -LSB- -LRB- pSTRUCT '' . '' -LSB- A , B -RSB- -RRB- , C , -LRB- pSTRUCT '' . '' -LSB- A , D -RSB- -RRB- -RSB- -RRB- : - demo -LRB- pSTRUCT `` app '' -LSB- B , C , D -RSB- -RRB- . In practice , the scheme can be refined by having the mapping act as id on the set of builtins and Prolog 's CUT operation . By mapping predicates to predicates and generating trivial type declarations for the arguments we obtain a practical embedding of Prolog in > = Prolog which is useful to run existing untyped Prolog applications . 6 Related work Despite the proven expressiveness of monadic style in functional programming -LSB- 16 , 17 -RSB- and denotational semantics -LSB- 10 -RSB- no systematic exploration of the concept has been done for logic programming languages . However , ideas alike in substance are present in Peter VanRoy 's Extended DCGs -LSB- 15 -RSB- and their WildLife -LSB- 1 -RSB- counterparts , which encapsulate a concept of state similar to the state transformers in functional programming -LSB- 16 -RSB- and some of the morfisms we have described are ` implicitely ' known to good Prolog programmers . This situation largely motivates our effort as both major declarative programming paradigms share the reasons for adopting monads in every-day programming . 7 Future work The following are some of the most attempting follow-ups to the experiments described in this paper : ffl state transformers in logic programming ffl an implementation of arbitrary monad comprehensions ffl the monad of the answers of a -LRB- nondeterministic -RRB- logic program ffl Fold/Unfold transformations and monad operations ffl transforming meta-interpreters with monad operations ffl a monadic view of continuation passing and binarization in logic programming For instance , meta-interpreters for logic programming languages can be described in terms of the monad of clause unfoldings . All-solution predicates can be seen as morphisms from a suitably organized monad of answers to the monad of lists . Knowing their monad properties allows to transform -LRB- i.e. ` partially evaluate ' -RRB- all-solution predicates in a rigorous way to their more efficient first-order equivalents . 8 Conclusion We have shown that monads are as useful to describe the basic data structures of logic programming languages as they are in functional programming . In particular we have organized function lists as a monad and studied some interesting monad morphisms . We have introduced a monad-based concept of lists comprehensions for > = Prolog . We have described Prolog 's unfolding operation in a monadic style , with a practical compilation scheme from untyped Prolog to > = Prolog . The use of an intrinsically high order logic programming language -LRB- > = Prolog -RRB- has been shown particularly useful , although most of our techniques will also work in ordinary Prolog systems . Although transposing an elegant declarative concept from one declarative programming paradigm to another is not difficult , their elegant implementation was not always obvious . Moreover , the monadic view of lazy function lists and meta-programming is novel and it looks like a good starting point for a uniform description for the semantics of logic programs and for program transformations . Acknowledgment Paul Tarau thanks for support from the Canadian National Science and Engineering Research Council -LRB- grant OGP0107411 -RRB- , the FESR of the Universit?e de Moncton and for the fellowships from Universit?e de Rennes and the I.R.I.S.A Rennes . Discussions with members of the LANDE group and especially Pascal Brisset , Pascal Fradet , Daniel LeMetayer and Olivier Ridoux have been very helpful in clarifying our ideas on the subject . References -LSB- 1 -RSB- H . A ?? t-Kaci and A. Podelski . Towards a meaning of LIFE . In J. Maluszy?nski and M. Wirsing , editors , Proceedings of the 3rd Int . Symposium on Programming Language Implementation and Logic Programming , PLILP91 , Passau , Germany , number 528 . Springer Verlag , Aug. 1991 . -LSB- 2 -RSB- Y. Bekkers , B. Canet , O. Ridoux , and L. Ungaro . MALI : A memory with a real-time garbage collector for implementing logic programming languages . In 3rd Symp . Logic Programming , Salt Lake City , UT , USA , 1986 . IEEE . -LSB- 3 -RSB- P. Brisset . Compilation de > = Prolog . Th?ese , Universit?e de Rennes I , 1992 . -LSB- 4 -RSB- P. Brisset and O. Ridoux . Na ?? ve reverse can be linear . In K. Furukawa , editor , 8th Int . Conf . Logic Programming , pages 857 -LCB- 870 , Paris , France , 1991 . MIT Press . ftp : / / ftp.irisa.fr / local/lande . -LSB- 5 -RSB- P. Brisset and O. Ridoux . The architecture of an implementation of > = Prolog : Prolog/Mali . In Workshop on > = Prolog , Philadelphia , PA , USA , 1992 . ftp : / / ftp.irisa.fr / local/lande . -LSB- 6 -RSB- J. Goguen . Higher order functions considered unnecessary for higher order programming . Technical report , SRI International , Jan. . 1989 . Technical report SRI-CSL-88-1 . -LSB- 7 -RSB- D. Miller . A logic programminglanguage with lambda-abstraction , function variables , and simple unification . J. Logic and Computation , 1 -LRB- 4 -RRB- :497 -LCB- 536 , 1991 . -LSB- 8 -RSB- D. Miller . Unification of simply typed lambda-terms as logic programming . In K. Furukawa , editor , 8th Int . Conf . Logic Programming , pages 255 -LCB- 269 , Paris , France , 1991 . MIT Press . -LSB- 9 -RSB- D. Miller , G. Nadathur , and A. Scedrov . Hereditary Harrop formulas and uniform proof systems . In D. Gries , editor , 2nd Symp . Logic in Computer Science , pages 98 -LCB- 105 , Ithaca , New York , USA , 1987 . -LSB- 10 -RSB- E. Moggi . Notions of computation and monads . Information and Computation , 93:55 -LCB- 92 , 1991 . -LSB- 11 -RSB- A. Mycroft and R. A. O'Keefe . A polimorphic type system for prolog . Artificial Intelligence , -LRB- 23 -RRB- :295 -LCB- 307 , 1984 . -LSB- 12 -RSB- G. Nadathur and D. Miller . An overview of > = Prolog . In K. Bowen and R. Kowalski , editors , Symp . Logic Programming , pages 810 -LCB- 827 , Seattle , Washington , USA , 1988 . -LSB- 13 -RSB- G. Nadathur and D. Miller . Higher-order Horn clauses . JACM , 37 -LRB- 4 -RRB- :777 -LCB- 814 , 1990 . -LSB- 14 -RSB- G. Nadathur and D. Wilson . A representation of lambda terms suitable for operations on their intensions . In ACM Conf . Lisp and Functional Programming , pages 341 -LCB- 348 , Nice , France , 1990 . ACM Press . -LSB- 15 -RSB- P. V. Roy . A useful extension to Prolog 's Definite Clause Grammar notation . SIGPLAN notices , 24 -LRB- 11 -RRB- :132 -LCB- 134 , Nov. 1989 . -LSB- 16 -RSB- P. Wadler . Comprehending monads . In ACM Conf . Lisp and Functional Programming , pages 61 -LCB- 78 , Nice , France , 1990 . ACM Press . -LSB- 17 -RSB- P. Wadler . The essence of functional programming . In ACM Symposium POPL ' 92 , pages 1 -LCB- 15 . ACM Press , 1992 . -LSB- 18 -RSB- D. H. D. Warren . Higher-order extensions to Prolog -LCB- are they needed ? In D. Michie , J. Hayes , and Y. H. Pao , editors , Machine Intelligence 10 . Ellis Horwood , 1981 . Word count : dvi2tty - l - w132 art | wc 943 4969 55597

B I O B O O O O O O O O O O O O O O O O O O O O B I O O O B O O O O O O O B I O O O O B O O O O O O O O O O B I I O O O O O O O O O O B O O O B O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O B O O O O O O O O O O O O O O B O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O B I O B O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O B O O O O O B O O O O O O O O B O O O B O O O O O O O O O O O O O B O O O O O O O O O O B O O O O O O O O O O O O O B O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O B O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O B O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O B I O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O B O O O O O O O O B O O O O O O O O O O O O B O O O B O O O O O O O O O B O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O B O O O B O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O B O O O O O O O O O O O O O O O B O O O B O O O O O O O O B I O O O O B O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B I O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O B I O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O B O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O O

lazy function lists
list comprehensions
logic programming
monads
prolog

all solution predicates
computing paradigms
higher-order unification