10-prolog-part1

# 10-prolog-part1 - CSE 452 Programming Languages Logical...

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

1 CSE 452: Programming Languages Logical Programming Languages Part 1 2 Organization of Programming Languages-Cheng Outline ? Another programming paradigm: ? Logic Programming ? Prolog ? We’ll be using GNU prolog (http://www.gnu.org/software/gprolog/gprolog.ht ml ) 3 Organization of Programming Languages-Cheng Another Paradigm QUESTION: "What is a computer program?" ANSWER: "It is an executable representation of some algorithm designed to solve some real world problem." ? Kowalski (CACM, 1979): There are two key elements to a computer program ? Logic – what we want the program to achieve ? Control – how we are going to achieve it ALGORITHM = LOGIC + CONTROL ? Difference between imperative and declarative programming

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

View Full Document
2 4 Organization of Programming Languages-Cheng Example: Computing Factorial Imperative (Ada) implementation with CS_IO ; use CS_IO ; procedure FACTORIAL is N: integer; T: integer:= 1; begin put("Input ");get(N); for K in 2. .N loop T:= T*K; end loop; put("Factorial "); put(N); put("is "); put(T); newline; end FACTORIAL; ? For imperative language programmer needs to concentrate on both the logic and control Declarative (PROLOG) implementation factorial(0,1):- !. factorial(N1,T2):- N2 is N1-1, factorial(N2,T1), T2 is N1*T1. ? For declarative language, we define the logic (the desired goal or result) but not the control (how we achieve the desired goal) 5 Organization of Programming Languages-Cheng Declarative Languages ? Declarative languages make extensive use of relationships between objects ? There are two principal styles of defining relationships: 1. Functional Programming ? Relationships are expressed using functions. (define (square n) (* n n)) ? The square function expresses the relationship between the input n and the output value n*n 2. Logic programming ? Relationships are declared using expressions known as clauses. square(N, M):- M is N*N. ? Clauses can be used to express both facts and rules 6 Organization of Programming Languages-Cheng What is logic? ? Encyclopedia Brittanica: ? Logic is the study of propositions and their use in argumentation ? Encarta Encyclopedia: ? Logic is the science dealing with the principles of valid reasoning and argument ? Factasia Logic: ? Logic is the study of necessary truths and of systematic methods for expressing and demonstrating such truths
3 7 Organization of Programming Languages-Cheng Logic Programming ? Logic programming ? expresses programs in the form of symbolic logic ? uses a logical inferencing process for reasoning ? Logic programs are declarative rather than procedural ? Programs do not state exactly how a result is to be computed but rather describe the form of the result ? It is assumed that the computer can determine how the result is to be obtained ? One needs to provide the computer with the relevant information and a method of inference for computing desirable results ? Programming languages based on symbolic logic are called logic programming languages ? Prolog is the most widely used logic programming language

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 15

10-prolog-part1 - CSE 452 Programming Languages Logical...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online