adding a loop construct to prolog - ijcta a loop construct to prolog keehang kwon dept. of computer...

3
Adding a Loop Construct to Prolog Keehang Kwon Dept. of Computer Engineering, DongA University Busan 604-714, Korea [email protected] Abstract: Prolog have traditionally lacked de- vices for the looping tasks. This limitation can be addressed by using a fragment of Japaridze’s computability logic. We propose SeqProlog, an extension to Prolog with sequential recurrence goal formulas. SeqProlog extends Prolog by al- lowing goals of the form T G where G is a goal. These goals allow us to specify the looping tasks. keywords: Prolog, loop, computability logic 1 Introduction The while-loop constructs have been popular in imperative languages. Despite its wide use, the while-loop constructs cannot be specified in logic programming based on classical logic or linear logic. For example, it is very awkward to specify the following task in Prolog: repeatedly read a number N , output the factorial of N until some negative number is read. Dealing with the looping tasks in Prolog is typically done via the use of recursion in the in- struction set. One major drawback with these attempts is that the semantics of the instruction set becomes awkward and complicated. This limitation can be overcome only by a new seman- tics for logical formulas, i.e., using Japaridze’s Computability Logic(CL)[2]. CL is a beauti- ful semantic platform for reinterpreting logic as a theory of tasks. Formulas in CL stand for instructions that can carry out some computa- tional tasks. This paper proposes SeqProlog, an extension of Prolog with sequential recurrence operators in goal formulas. Traditionally, Prolog has the problem of expressing the while-loop tasks. The class of sequential recurrence operators enables the programmer to express these looping tasks. To be specific, a sequential-conjunctive- recurrence goal is of the form T G where G is a goal. Executing this goal has the following in- tended semantics: execute G repeatedly. All ex- ecutions must succeed for executing T G to suc- ceed. On the other hand, a sequential-conjunctive goal, introduced in [3], is of the form G 1 G 2 where G 1 ,G 2 are goals. Executing this goal has the following intended semantics: execute both G 1 and G 2 in sequence. Both executions must succeed for executing G 1 G 2 to succeed. In this paper we present the syntax and se- mantics of this extended language, show some examples of its use. The remainder of this paper is structured as follows. We describe SeqProlog based on a first- order sequential Horn clauses in the next sec- tion. In Section 3, we present some examples of SeqProlog. Section 4 concludes the paper. 2 The Language The language we use is an expanded version of Horn clauses with sequential-conjunctive- recurrence goals. It is described by G- and D- formulas given by the syntax rules below: G ::= A | G G | G G |∃xG | G G | T G D ::= A | G A |∀xD | D D In the rules above, A represents an atomic formula. A D-formula is called a sequential Horn clause. 1 Keehang Kwon, Int. J. Comp. Tech. Appl., Vol 2 (1), 121-123 ISSN : 2229-6093 121

Upload: dinhquynh

Post on 29-Apr-2018

221 views

Category:

Documents


1 download

TRANSCRIPT

Adding a Loop Construct to Prolog

Keehang KwonDept. of Computer Engineering, DongA University

Busan 604-714, [email protected]

Abstract: Prolog have traditionally lacked de-vices for the looping tasks. This limitation canbe addressed by using a fragment of Japaridze’scomputability logic. We propose SeqProlog, anextension to Prolog with sequential recurrencegoal formulas. SeqProlog extends Prolog by al-lowing goals of the form

⋂G where G is a goal.

These goals allow us to specify the looping tasks.keywords: Prolog, loop, computability logic

1 Introduction

The while-loop constructs have been popular inimperative languages. Despite its wide use, thewhile-loop constructs cannot be specified in logicprogramming based on classical logic or linearlogic. For example, it is very awkward to specifythe following task in Prolog: repeatedly read anumber N , output the factorial of N until somenegative number is read.

Dealing with the looping tasks in Prolog istypically done via the use of recursion in the in-struction set. One major drawback with theseattempts is that the semantics of the instructionset becomes awkward and complicated. Thislimitation can be overcome only by a new seman-tics for logical formulas, i.e., using Japaridze’sComputability Logic(CL)[2]. CL is a beauti-ful semantic platform for reinterpreting logic asa theory of tasks. Formulas in CL stand forinstructions that can carry out some computa-tional tasks.

This paper proposes SeqProlog, an extensionof Prolog with sequential recurrence operatorsin goal formulas. Traditionally, Prolog has theproblem of expressing the while-loop tasks. Theclass of sequential recurrence operators enables

the programmer to express these looping tasks.To be specific, a sequential-conjunctive-

recurrence goal is of the form⋂

G where G isa goal. Executing this goal has the following in-tended semantics: execute G repeatedly. All ex-ecutions must succeed for executing

⋂G to suc-

ceed.On the other hand, a sequential-conjunctive

goal, introduced in [3], is of the form G1 ∩ G2

where G1, G2 are goals. Executing this goal hasthe following intended semantics: execute bothG1 and G2 in sequence. Both executions mustsucceed for executing G1 ∩G2 to succeed.

In this paper we present the syntax and se-mantics of this extended language, show someexamples of its use.

The remainder of this paper is structured asfollows. We describe SeqProlog based on a first-order sequential Horn clauses in the next sec-tion. In Section 3, we present some examples ofSeqProlog. Section 4 concludes the paper.

2 The Language

The language we use is an expanded versionof Horn clauses with sequential-conjunctive-recurrence goals. It is described by G- and D-formulas given by the syntax rules below:

G ::= A | G ∧G | G ∨G | ∃x G | G ∩G |⋂G

D ::= A | G ⊃ A | ∀x D | D ∧D

In the rules above, A represents an atomicformula. A D-formula is called a sequential Hornclause.

1

Keehang Kwon, Int. J. Comp. Tech. Appl., Vol 2 (1), 121-123 ISSN : 2229-6093

121

In the transition system to be considered, G-formulas will function as queries and a set of D-formulas will constitute a set of available instruc-tions. For this reason, we refer to a G-formula asa query, to a set of D-formula as an instructionset.

We will present an operational semantics forthis language. These rules in fact depend on thetop-level constructor in the expression, a prop-erty known as uniform provability[5]. It is worthnoting that this property is preserved by disal-lowing the disjunctive formulas, existential for-mulas in the instruction set. We disallow thesequential conjunctive formulas as well in the in-struction set for simplicity.

It appears that uniform provability is re-quired only for noninteractive algorithms.

Definition 2.1. Let G be a goal and let P be afinite set of available instructions. Then the no-tion of executing 〈P, G〉 – executing G relativeto P – is defined as follows:

(1) If G is an atom and is identical to an in-stance of a program clause in P, then thecurrent execution terminates with a suc-cess.

(2) If G is an atom and an instance of a pro-gram clause in P is of the form G1 ⊃ G,execute 〈P, G1〉. This execution must suc-ceed for the current execution to succeed.

(3) If G is G1 ∧G2, then execute both 〈P, G1〉and 〈P, G2〉 in parallel. Both executionsmust succeed for the current execution tosucceed.

(4) If G is G1 ∨G2, then execute both 〈P, G1〉and 〈P, G2〉 in parallel. At least one ofthese two executions must succeed for thecurrent execution to succeed.

(5) If G is ∃xG1, then execute 〈P, [t/x]G1〉where t is a term. This execution mustsucceed for the current execution to suc-ceed.

(6) If G is G1 ∩G2, then execute both 〈P, G1〉and 〈P, G2〉 in sequence. Both executionsmust succeed for the current execution tosucceed.

(7) If G is⋂

G, then execute both 〈P, G〉and 〈P,

⋂G〉 in sequence. Both executions

must succeed for the current execution tosucceed.

In the above rules, the symbols⋂

provides loop-ing executions of instructions: they allow for therepeated sequential conjunctive execution of theinstructions. We conjecture the completeness ofthe above procedure with respect to the CL se-mantics at this stage.

3 Examples

An example of the use of this construct is pro-vided by the following “factorial” program whichcontains some basic factorial-handling instruc-tions written in conventional Prolog. Here + isused for the arithmetic addition and nat(X) istrue if X is a natural number.

fact(0, 1). % base casefact(X + 1, XY + Y ) : − fact(X, Y ) ∧ nat(X).

Our language in Section 2 permits sequential-conjunctive-recurrence operators in goals. Thisallows for repeated sequential interactions be-tween the user and the system. An exampleof the use of this construct is provided by theprogram which repeatedly does the following se-quential tasks: read a number from the user, andthen output its factorial number:

query :.⋂% while-loop begin

(read(N)∩fact(N, O)∩write(′fact :′)∩write(O)) % while-loop end

2

Keehang Kwon, Int. J. Comp. Tech. Appl., Vol 2 (1), 121-123 ISSN : 2229-6093

122

For example, consider a goal query. Solving thisgoal has the effect of executing query with re-spect to the instructions in the factorial program.

4 Conclusion

In this paper, we have considered an extensionto Prolog with sequential Horn clauses. This ex-tension allows goals of the form

⋂G where G is

a goal. These goals are particularly useful forthe looping executions of instructions. Our fu-ture interest is in a language which extends se-quential hereditary Harrop formulas with thesefeatures[1].

5 Acknowledgements

This work was supported by Dong-A UniversityResearch Fund.

References

[1] S. Abramsky, “Sequentiality vs concurrencyin Games and Logic”, Mathematical Struc-tures in Computer Science, vol.13, pp.531–565, 2003.

[2] G. Japaridze, “Introduction to computabil-ity logic”, Annals of Pure and Applied Logic,vol.123, pp.1–99, 2003.

[3] K. Kwon and S. Hur, “Adding sequentialconjunctions to Prolog”, IJCTA, vol.1, No.1,pp.1-3, 2010.

[4] D. Miller, “A logical analysis of modules inlogic programming”, Journal of Logic Pro-gramming, vol.6, pp.79–108, 1989.

[5] D. Miller, G. Nadathur, F. Pfenning, andA. Scedrov, “Uniform proofs as a foundationfor logic programming”, Annals of Pure andApplied Logic, vol.51, pp.125–157, 1991.

3

Keehang Kwon, Int. J. Comp. Tech. Appl., Vol 2 (1), 121-123 ISSN : 2229-6093

123