View stunning SlideShares in full-screen with the new iOS app!Introducing SlideShare for AndroidExplore all your favorite topics in the SlideShare appGet the SlideShare app to Save for Later — even offline
View stunning SlideShares in full-screen with the new Android app!View stunning SlideShares in full-screen with the new iOS app!
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
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
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)