Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic...

21
Programming Programming Languages Languages

Transcript of Programming Languages. A Logic Programming Language: Prolog This lesson describes the basic...

Programming Programming LanguagesLanguages

A Logic Programming A Logic Programming Language: PrologLanguage: Prolog

This lesson describes the basic structures This lesson describes the basic structures and functions of the logic programming and functions of the logic programming language: Prolog. language: Prolog.

OverviewOverview• Prolog, which stands for PROgramming in LOGic, is Prolog, which stands for PROgramming in LOGic, is

the most widely available language in the logic the most widely available language in the logic programming paradigm.programming paradigm.

• Prolog is based the mathematical notions of Prolog is based the mathematical notions of relations and logical inference.relations and logical inference.

• Prolog is a declarative language meaning Prolog is a declarative language meaning that rather than describing how to compute that rather than describing how to compute a solution, a program consists of a data base a solution, a program consists of a data base of facts and logical relationships (rules) of facts and logical relationships (rules) which describe the relationships which hold which describe the relationships which hold for the given application. for the given application.

Overview (2)Overview (2)• Rather then running a program to obtain a Rather then running a program to obtain a

solution, the user asks a question. solution, the user asks a question.

• When asked a question, the run time system searches When asked a question, the run time system searches through the data base of facts and rules to determine (by through the data base of facts and rules to determine (by logical deduction) the answer. logical deduction) the answer.

• Among the features of Prolog are `logical Among the features of Prolog are `logical variables' meaning that they behave like variables' meaning that they behave like mathematical variables, a powerful pattern-mathematical variables, a powerful pattern-matching facility (unification), a backtracking matching facility (unification), a backtracking strategy to search for proofs, uniform data strategy to search for proofs, uniform data structures, and input and output are structures, and input and output are interchangeable. interchangeable.

Overview(3)Overview(3)

• Often there will be more than one way to Often there will be more than one way to deduce the answer or there will be more deduce the answer or there will be more than one solution,than one solution,

• in such cases the run time system may be in such cases the run time system may be asked find other solutions. backtracking to asked find other solutions. backtracking to generate alternative solutions.generate alternative solutions.

• Prolog is a weakly typed language with Prolog is a weakly typed language with dynamic type checking and static scope rules. dynamic type checking and static scope rules.

• Prolog is used in artificial intelligence applications Prolog is used in artificial intelligence applications such as natural language interfaces, automated such as natural language interfaces, automated reasoning systems and expert systems. reasoning systems and expert systems.

The Structure of Prolog The Structure of Prolog ProgramsPrograms

• A Prolog program consists of a database of A Prolog program consists of a database of facts and rules, and queries (questions). facts and rules, and queries (questions).

• Fact: ... . Fact: ... .

• Rule: ... :- ... . Rule: ... :- ... .

• Query: ?- ... . Query: ?- ... . • Variables: must begin with an upper case letter. Variables: must begin with an upper case letter.

• Constants: numbers, begin with lowercase Constants: numbers, begin with lowercase letter, or enclosed in single quotes. letter, or enclosed in single quotes.

Clauses: Facts, rules, and Clauses: Facts, rules, and queries queries

• A Prolog program consists of a data base of A Prolog program consists of a data base of facts and rules. facts and rules.

• There is no structure imposed on a Prolog There is no structure imposed on a Prolog program, there is no main procedure, and program, there is no main procedure, and there is no nesting of definitions.there is no nesting of definitions.

• All facts and rules are global in scope and All facts and rules are global in scope and the scope of a variable is the fact or rule in the scope of a variable is the fact or rule in which it appears.which it appears.

• A Prolog program is executed by asking a A Prolog program is executed by asking a question. The question is called a query. question. The question is called a query. Facts, rules, and queries are called clauses.Facts, rules, and queries are called clauses.

SyntaxSyntax• Facts: Facts:

• 'It is sunny'.'It is sunny'.'It is summer'.'It is summer'.

• Queries:Queries:

• ?-?-?- 'It is sunny'. ?- 'It is sunny'.

YesYes?-?-?- 'It is cold'.?- 'It is cold'.NoNo?-?-

Rules: propositional logicRules: propositional logic• Rules extend the capabilities of a logic program.Rules extend the capabilities of a logic program.

• They are what give Prolog the ability to They are what give Prolog the ability to pursue its decision-making process. pursue its decision-making process.

• 'It is sunny'.'It is sunny'.'It is summer'.'It is summer'.'It is hot' :- 'It is summer', 'It is sunny'.'It is hot' :- 'It is summer', 'It is sunny'.'It is cold' :- 'It is winter', 'It is snowing'.'It is cold' :- 'It is winter', 'It is snowing'.

• ?- 'It is hot'.?- 'It is hot'.YesYes ?-?-

Rules: Predicate LogicRules: Predicate Logic• Facts and rules may be parameterized to produce programs in predicate logic. Facts and rules may be parameterized to produce programs in predicate logic.

• The parameters may be variables, atoms, The parameters may be variables, atoms, numbers, or terms. numbers, or terms.

• Parameterization permits the definition of Parameterization permits the definition of more complex relationships. more complex relationships.

A family's genelogical A family's genelogical relationships. relationships.

• female(amy).female(amy).female(johnette).female(johnette).male(anthony).male(anthony).male(bruce).male(bruce).male(ogden).male(ogden).parentof(amy,johnette).parentof(amy,johnette).parentof(amy,anthony).parentof(amy,anthony).parentof(amy,bruce).parentof(amy,bruce).parentof(ogden,johnette). parentof(ogden,johnette). parentof(ogden,anthony). parentof(ogden,anthony). parentof(ogden,bruce).parentof(ogden,bruce).

A family's genelogical A family's genelogical relationships. relationships.

• siblingof(X,Y) :- parentof(Z,X), siblingof(X,Y) :- parentof(Z,X), parentof(Z,Y), parentof(Z,Y),

X <> Y. X <> Y.

• brotherof(X,Y) :- parentof(Z,X),brotherof(X,Y) :- parentof(Z,X), male(X), male(X),

parentof(Z,Y), parentof(Z,Y), X <> Y. X <> Y.

Composite TypesComposite Types• Prolog does not provide an array type.Prolog does not provide an array type.

• Arrays may be represented as a list and Arrays may be represented as a list and multidimensional arrays as a list(s) of lists. multidimensional arrays as a list(s) of lists.

• An alternate representation is to represent An alternate representation is to represent an array as a set of facts in a the data base. an array as a set of facts in a the data base.

• A list is designated by square brackets ([ ]A list is designated by square brackets ([ ]+). +). [dog,cat,mouse][dog,cat,mouse]

• Elements in a Prolog list are ordered, even Elements in a Prolog list are ordered, even though there are no indexes.though there are no indexes.

Records or tuplesRecords or tuples• Records or tuples are represented as patterns. Records or tuples are represented as patterns.

• book( author( aaby, anthony ), book( author( aaby, anthony ), title( labmanual ), data( 1991 ))title( labmanual ), data( 1991 ))

• The elements of a tuple are accessed by The elements of a tuple are accessed by pattern matching. pattern matching.

• book(Title,Author,Publisher,Date). book(Title,Author,Publisher,Date). author(LastName,FirstName,MI). author(LastName,FirstName,MI).

publisher(Company,City).publisher(Company,City).

• book(T,A,publisher(C,rome),Date)book(T,A,publisher(C,rome),Date)

Type PredicatesType Predicates• Since Prolog is a weakly typed language, it is Since Prolog is a weakly typed language, it is

important for the user to be able to important for the user to be able to determine the type of a parameter. determine the type of a parameter.

• The following built in predicates are used to The following built in predicates are used to determine the type of a parameter. determine the type of a parameter.

• var(V), nonvar(NV), atom(A), integer(I) var(V), nonvar(NV), atom(A), integer(I) real(R), number(N), atomic(A), real(R), number(N), atomic(A), functor(T,F,A) T is a term with functor F functor(T,F,A) T is a term with functor F

and arity A and arity A clause(H,T) H :- T is a rule in the clause(H,T) H :- T is a rule in the program T =..L T is a term, L is a program T =..L T is a term, L is a list list

functor functor • The predicate, functor is used as follows. The predicate, functor is used as follows.

• functor(T,F,A) functor(T,F,A) T is a term, F is its functor, and A is its arity.T is a term, F is its functor, and A is its arity.

• ?- functor(t(a,b,c),F,A).?- functor(t(a,b,c),F,A).F = tF = t

A = 3A = 3yesyes

• t is the functor of the term t(a,b,c), and 3 is t is the functor of the term t(a,b,c), and 3 is the arity (number of arguments) of thethe arity (number of arguments) of theterm. term.

Unification and Pattern Unification and Pattern MatchingMatching

• The arguments in a query are matched (or The arguments in a query are matched (or unified in Prolog terminology) to select the unified in Prolog terminology) to select the appropriate rule. appropriate rule.

• Prolog code is often bidirectional.Prolog code is often bidirectional.

• In bidirectional code, the arguments may be In bidirectional code, the arguments may be use either for input or output.use either for input or output.

FunctionsFunctions

• That is, both the arguments to the function That is, both the arguments to the function and the result of the function must be and the result of the function must be parameters to the relation.parameters to the relation.

• This means that composition of two This means that composition of two functions cannot be constructed.functions cannot be constructed.

• Prolog does not provide for a function type therefore, Prolog does not provide for a function type therefore, functions must be defined as relations. functions must be defined as relations.

ListsLists

• Lists are a recursive data structure so Lists are a recursive data structure so recursion occurs naturally in the definitions recursion occurs naturally in the definitions of various list operations. of various list operations.

• Operations on lists must consider the empty Operations on lists must consider the empty list as a case.list as a case.

• Lists are the basic data structure used in Lists are the basic data structure used in logic (and functional) programming.logic (and functional) programming.

• Predicates on lists are often written using Predicates on lists are often written using multiple rules. One rule for the empty list (the multiple rules. One rule for the empty list (the base case) and a second rule for non empty base case) and a second rule for non empty lists.lists.

IterationIteration

• Recursion is the only iterative method Recursion is the only iterative method available in Prolog. available in Prolog.

Extra-Logical PredicatesExtra-Logical Predicates

• Program Access and Manipulation: Program Access and Manipulation: assert(Clause),assert(Clause),

retract(Clause_Head) ... retract(Clause_Head) ...

• System Access: System Access: system(Command) Execute Command system(Command) Execute Command

in the operating system in the operating system

• Input/Output: Input/Output: Most Prolog implementations provide Most Prolog implementations provide

the predicates read and write...the predicates read and write...