meljun cortes programming languages logic programming
TRANSCRIPT
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
1/14
Logic Programming
Programming Languages
* Property of STI
Page 1 of 14
Logic Programming
The term logic programming refers looselyto:
The use of facts and rules to representinformation, and
The use of deduction to answer queries.
These two aspects reflect a division of labor between programmers and the language for
logic programming.
Programmers supply the facts and rules,and the language uses deduction tocomputer answers and queries.
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
2/14
Logic Programming
Programming Languages
* Property of STI
Page 2 of 14
Logic Programming
Predicate calculus includes a wider rangeof entities.
It permits the description of relations andthe use of variables.
Formal logic is presented using
propositions and symbolic logic. Propositions is defined as a logical statement
that may or may not be true.
Symbolic logic is used to expresspropositions, relationships betweenpropositions, and how new propositions can
be inferred from existing ones that are true.
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
3/14
Logic Programming
Programming Languages
* Property of STI
Page 3 of 14
Propositions Two basic elements of logic programming
propositions:
A constant is an object that retains its valuethroughout the execution of a program
A variable is an object used to representdata that can be changed while the programor procedure is running.• Different from variables in imperative
languages
tomic propositionsconsist of compound
terms Compound term is one element of a
mathematical relation, written like amathematical function• Functor is a function symbol that names the
relationship
• Ordered list of parameters (tuple)
Example:
male (francis)
like (john,jane)
man (ian)
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
4/14
Logic Programming
Programming Languages
* Property of STI
Page 4 of 14
Propositions
List of connectors used in predicatecalculus
Precedence proceeds top-down
Name Symbol Example Meaning
negation a not a
conjunction a b a and b
disjunctiona b
a or bequivalence = a b a is
equivalentto b
implication a b a implies b
implication a b b implies a
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
5/14
Logic Programming
Programming Languages
* Property of STI
Page 5 of 14
Propositions
Clausal Form
Too many ways to state the same thing
Use a standard form for propositions
B1 B2 … Bn A 1 A 2 … A m• means if all the As are true, then at least one
B is true
• Antecedent: right side
• Consequent: left side
Example:
if it is raining and it is windy, then there is atyphoon
if there is a typhoon, then it is raining and itis windy
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
6/14
Logic Programming
Programming Languages
* Property of STI
Page 6 of 14
Basic Elements of
PROLOG
Terms
collection of statements that can be a
constant, a variable, or a structure Example:
terms_s
‘term’
12
‘number_12’
Variables are strings of letters, digits andunderscores that begin with an uppercaseletter
• Example:
Number_17
Variable
Structures are PROLOG equivalent of atomicpropositions in predicate calculus
• Example:
female (anna).
taste (lemon, sour).
predecessor (mary, shelley) :-
mother(mary, shelley.).
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
7/14
Logic Programming
Programming Languages
* Property of STI
Page 7 of 14
Basic Elements of
PROLOG
Fact Statements
building blocks of logic programming
the bases to which a given hypothesis isderived
Example:
female (anna).
taste (lemon, sour).
Rule Statements
commonly known as headed Horn clauses
syntax:
consequence_1:- antecedent_expression
Goal Statements
syntactic form of Prolog that is identical toheadless Horn clauses
Example:
man(jose)
father(X, juan)
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
8/14
Logic Programming
Programming Languages
* Property of STI
Page 8 of 14
Basic Elements of
PROLOG
Data types are set of values and someoperations that can be performed on that
set of values.
Simple types are implementationsdependent in PROLOG – theseimplementations are summarized in thefollowing table.
Composite types are composed of basictypes.
• In Prolog, a list is designated by square brackets [ ]
•
Example: [dog,act,mouse]
TYPE V LUES
boolean true, fail
integer integers
real floating point numbers
variable variables
atom character sequences
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
9/14
Logic Programming
Programming Languages
* Property of STI
Page 9 of 14
Basic Elements of
PROLOG
Type Predicates
• The following built-in predicates are used to
determine the type of a parameter:
Predicate CHECKS IF
var (V) V is a variable
nonvar (NV) NV is not a variable
atom (A) A is an atominteger (I) I is an integer
real (R) R is a floating point number
number (N) N is an integer or real
atomic (A) A is an atom or a number
functor (T,F,A) T is a term with functor F andarity A
T = ..L T is a term, L is a list
clause(H,T) H :- T is a rule in the program
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
10/14
Logic Programming
Programming Languages
* Property of STI
Page 10 of 14
Basic Elements of
PROLOG
rithmetic Expressions
evaluated using the built-in predicate “is”
that is used as an infix operator in thefollowing form:
variable is expression
rithmetic Operations
PROLOG provides standard arithmeticexpressions as summarized in the table:
SYMBOL OPER TION
+ addition
- subtraction
* multiplication
/ real division
// integer division
Mod modulus
** power
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
11/14
Logic Programming
Programming Languages
* Property of STI
Page 11 of 14
Basic Elements of
PROLOG
Boolean Predicates
Prolog provides more general comparison
operators that compare terms andpredicates to test for unifiability and
whether terms are identical.
SYMBOL OPER TION CTION COMMENTS
A ?= B Unifiable A and B are
unifiable but
Does not
unify A andB
A = B Unify Unify A andB if possible
A \+= B Not unifiable
A == B Identical Does notunify A and
B
A \+== B Not identical
A =:= B Equal (value) Evaluates Aand B to
Determine ifequal
A =\+= B less than(numeric)
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
12/14
Logic Programming
Programming Languages
* Property of STI
Page 12 of 14
Basic Elements of
PROLOG
SYMBOL OPER TION CTION COMMENTS
A < B Less or equal(numeric)
A =< B Greater than(numeric)
A > B Greater orequal(numeric)
A >= B Less than(terms)
A @< B Less or equal(terms)
A @=< B Less or equal(terms)
A @> B Greater than(terms)
A @>= B Greater orequal (terms)
Continuation..
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
13/14
Logic Programming
Programming Languages
* Property of STI
Page 13 of 14
Basic Elements of
PROLOG
Logical Operators
Predicates are functions that return a
Boolean value. Thus, the logical operatorsare built into the language.
The table below summarizes the logicaloperators:
SYMBOL OPER TION
not negation
\+ not provable
, logical conjunction
; logical disjunction
:- logical implication
-> if-then-else
-
8/8/2019 MELJUN CORTES Programming Languages Logic Programming
14/14
Logic Programming
Programming Languages
* Property of STI
Page 14 of 14
Applications of Logic
Programming
Relational Database Management Systems
simple tables of information can be
described by Prolog rules
Expert Systems
consist of a database of facts, an inferencingprocess, some heuristics about the domain,and some friendly human interface
Natural Language Processing
natural language itself is a grammar in itselfalbeit possesses com inherent redundanciesand ambiguity