Concepts+Techniques+and+Models+of+Computer+Programming_Part24

# Concepts+Techniques+and+Models+of+Computer+Programming_Part24

This preview shows pages 1–3. Sign up to view the full content.

648 Relational Programming correct deductions from the axioms. That is, the results of all predicates are valid tuples in the predicates’ relations. We call this deterministic logic programming . Table 9.2 defines a translation scheme T which translates any statement s in the relational kernel language into a logical formula T ( s ). Procedure definitions are translated into predicate definitions. Note that exceptions are not translated. Raising an exception signals that the normal, logical execution is no longer valid. The logical sentence therefore does not hold in that case. Proving the correctness of this table is beyond the scope of this chapter. We leave it as an interesting exercise for mathematically-minded readers. A given logical semantics can correspond to many operational semantics. For example, the following three statements: 1. X=Y s 2. s X=Y 3. if X==Y then s else fail end all have the exactly same logical semantics, namely: x = y T ( s ) But their operational semantics are very different! The first statement binds X and Y and then executes s . The second statement executes s and then binds X and Y . The third statement waits until it can determine whether or not X and Y are equal. It then executes s , if it determines that they are equal. Writing a logic program consists of two parts: writing the logical semantics and then choosing an operational semantics for it. The art of logic program- ming consists in balancing two conflicting tensions: the logical semantics should be simple and the operational semantics should be efficient. All the declarative programs of Chapters 3 and 4 can be seen in this light. They are all logic pro- grams. In the Prolog language, this has given rise to a beautiful programming style [182, 21, 139]. Deterministic append Let us write a simple logic program to append two lists. We have already seen the Append function: fun {Append A B} case A of nil then B [] X|As then X|{Append As B} end end Let us expand it into a procedure: Copyright c 2001-3 by P. Van Roy and S. Haridi. All rights reserved.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
9.3 Relation to logic programming 649 Relational statement Logical formula skip true fail false s 1 s 2 T ( s 1 ) T ( s 2 ) local X in s end x.T ( s ) X=Y x = y X=f(l1:X1 ... ln:Xn) x = f ( l 1 : x 1 , ..., l n : x n ) if X then s 1 else s 2 end ( x = true T ( s 1 )) ( x = false T ( s 2 )) case X of f(l1:X1 ... ln:Xn) ( x 1 , ..., x n .x = f ( l 1 : x 1 , ..., l n : x n ) T ( s 1 )) then s 1 else s 2 end ( ¬∃ x 1 , ..., x n .x = f ( l 1 : x 1 , ..., l n : x n ) T ( s 2 )) proc {P X1 ... Xn} s end x 1 , ..., x n .p ( x 1 , ..., x n ) T ( s ) {P Y1 ... Yn} p ( y 1 , ..., y n ) choice s 1 [] ... [] s n end T ( s 1 ) ... T ( s n ) Table 9.2: Translating a relational program to logic proc {Append A B ?C} case A of nil then C=B [] X|As then Cs in C=X|Cs {Append As B Cs} end end According to Table 9.2, this procedure has the following logical semantics: a, b, c. append ( a, b, c ) ( a = nil c = b ) ( x, a , c .a = x | a c = x | c append ( a , b, c )) The procedure also has an operational semantics, given by the semantics of the declarative model. The call: {Append [1 2 3] [4 5] X} executes successfully and returns X=[1 2 3 4 5] . The call’s logical meaning is the tuple
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern