• Save
16 logical programming
Upcoming SlideShare
Loading in...5
×
 

16 logical programming

on

  • 230 views

 

Statistics

Views

Total Views
230
Views on SlideShare
230
Embed Views
0

Actions

Likes
1
Downloads
0
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

16 logical programming 16 logical programming Document Transcript

  • 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
  • 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
  • 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 View slide
  • 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 View slide
  • 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)
  • 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