Upcoming SlideShare
×

# 16 logical programming

• 90 views

More in: Technology
• Comment goes here.
Are you sure you want to
Be the first to comment

Total Views
90
On Slideshare
0
From Embeds
0
Number of Embeds
0

Shares
0
0
Likes
1

No embeds

### Report content

No notes for slide

### Transcript

• 1. ICS 313 - Fundamentals of Programming Languages 116. Logical ProgrammingLogic ProgrammingNeither imperative nor functionalDeals with relations, not functionsArguments and results are treated uniformlydaughter(sue,john)lessthan(3,10)Separate logic from controlProgrammer declares what facts and relations are trueSystem determines how to use facts to solve problems
• 2. ICS 313 - Fundamentals of Programming Languages 2Relations (Predicates)A table with n ≥ 0 columns and a possibly infinite setof rowsA tuple (a1,a2,…,an) is in a relation if ai appears incolumn i, 1 ≤ i ≤ nExample: Relation append is a set of tuples of theform (X,Y,Z), where Z consists of the elements of Xfollowed by the elements of YRelations can be specified using Horn clauses(rules)Horn Clauses (Rules)A Horn clause:P if Q1 and Q2 and … and Qk, k ≥ 0can be interpreted as:The consequent, P, is true if the antecedents Q1,Q2,…, Qk , k ≥ 0 are all trueAntecedents: conjunction of zero or more conditions that are atomicformulae in predicate logicConsequent: an atomic formula in predicate logic
• 3. ICS 313 - Fundamentals of Programming Languages 3Horn Clause TerminologyHorn Clause ClauseConsequent Goal HeadAntecedents Subgoals TailHorn clause with no tail FactHorn clause with tail RuleP if Q1 and Q2 and … and Qk p :- q1 , …, qnExample: Links Between LanguagesFortranAlgol 60CPLBCPLCC++Simula 67Smalltalk-80
• 4. ICS 313 - Fundamentals of Programming Languages 4Example Prolog Facts and RulesA prolog program starts with a collection of factslink(fortran, algol60)link(algol60, cpl)link(cpl, bcpl)link(bcpl, c)link(c, cplusplus)link(algol60, simula67)link(simula67, cplusplus)link(simula67, smalltalk80)path(L, L)path(L,M) :- link(L,X), path(X,M)In PrologA simple term is a number, variable or an atom thatstands for itselfAll variables start with capital lettersAll constants are in lower caseA compound term consists of an atom followed by aparenthesized sequence of subterms.The atom is called a functorSubterms are called argumentsAll predicates are in lower case
• 5. ICS 313 - Fundamentals of Programming Languages 5Horn Clauses with VariablesVariables may appear in the antecedents and theconsequent of a Horn clause:p(X1,X2,…,Xn) :- h(X1,X2,…,Xm)p(X1,X2,…,Xn) :- h(X1,X2,…,Xm,Y1,Y2,…,Yk)QueriesSimplest form: Does a particular tuple belong to arelationpath(algol60,smalltalk80).Can’t ask: Does a particular tuple not belong to a relationYes/Fail rather then Yes/No answers, where Fail indicates afailure to deduce an answer.More interesting: Is there an X such that a specificclause containing X evaluates to yespath(X,cplusplus)
• 6. ICS 313 - Fundamentals of Programming Languages 6Examples1 ?- consult(myrules).myrules compiled, 0.00 sec, 1,640 bytes.Yes2 ?- path(X,cplusplus).X = cplusplus ;X = fortran ;X = fortran ;X = algol60 ;X = cpl ;X = bcpl ;X = c ;X = algol60 ;X = simula67 ;No3 ?- path(c,Y).Y = c ;Y = cplusplus ;No4 ?- path(algol60,smalltalk80).YesMore Examples?- link(N,M), link(L,M).N = fortranM = algol60L = fortranYes?- link(N,M), link(L,M), not(L=N).N = cM = cplusplusL = simula67Yes