Ch. 13 Ch. 131 jcmt CSE 3302 CSE@UTA Programming Languages CSE3302 Programming Languages (notes?)...

23
CSE 3302 CSE@UTA Programming Languages Ch. 13 Ch. 13 1 jcmt CSE3302 CSE3302 Programming Languages Programming Languages (notes?) (notes?) Dr. Carter Tiernan

Transcript of Ch. 13 Ch. 131 jcmt CSE 3302 CSE@UTA Programming Languages CSE3302 Programming Languages (notes?)...

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 1 jcmt

CSE3302CSE3302Programming LanguagesProgramming Languages

(notes?)(notes?)

Dr. Carter Tiernan

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 2 jcmt

Logic ProgrammingLogic Programming

• Nonprocedural programming• Higher-level language allows one

to express the same program with less detail

• Language does more automatically• Programmer focuses more on what

to do not on how to do it

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 3 jcmt

Automatic deductionAutomatic deduction• Automatic theorem proving

– The development of programs that can construct formal proofs of propositions stated in symbolic language

– Side effect of proof is construction of a result which demonstrates the proof

• Programs expressed in the form of propositions that assert the existence of a desired result

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 4 jcmt

PrologProlog

• Programs are structured like theorems– Clauses that define the problem domain– Facts (database of relationships among

atomic individuals)– Goals

• Is a fact provable?• Is there an individual satisfying the goal?• Is there a solution satisfying the goal?

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 5 jcmt

Prolog ClausesProlog Clauses• Clauses define relationships or

“predicates”

• Facts or hypothesesparent (charles, harry).

• Goals:- grandparent (elizabeth, X).

• Conditions or rulesgrandparent(Y, Z) :- parent (Y,W), parent(W,Z).

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 6 jcmt

PredicatesPredicates• <head> :- <body>• Predicates

– Relationship applied to terms– Relationships

• Properties of• Relations among

– Terms• Atoms• Variables• Compound terms

• Horn clause form

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 7 jcmt

GoalsGoals

• Executing a goal– Match clauses in predicate by finding an

assignment of values to the variables that makes the goal identical to the head of one of the clauses (unification)

– Variables are bound (instantiated) to create subgoal

– Recursive application and pattern matching

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 8 jcmt

Compound termsCompound terms• Compound term allows us to

describe individuals without naming them

• Functor with atoms or variablesd(X, plus(U,V), plus(DU,DV)) ord(X, U + V, DU + DV)

• Similar to LISP list structure• Acts somewhat like a function but is

not a function call

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 9 jcmt

Data structuresData structures

• No constructors• Data structures are implicitly

defined by their properties• Few primitives• Compound terms can provide

logical description of structure– Some Prologs allow infix notation for

functors

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 10 jcmt

Complex structuresComplex structures• Compound terms represent themselves

– Symbolic notations can be defined directly– Good for mathematical relationships

• Predicates can define structures– Predicates can replace compound terms– Expressions matched to clauses must exist

within the Prolog “closed world”– Good for object-oriented relationships

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 11 jcmt

More data structuresMore data structures

• Abstract data types - so abstract they’re only described

• Infinite terms - “occurs check”• Representation of ‘infinite’ list is

finite– Circular structure

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 12 jcmt

Control StructuresControl Structures

• Separation of logic and control– Independent analysis

• Order of clauses has no effect on meaning or logic

• Control affects generation and unification of subgoals– Efficiency is an issue but not meaning

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 13 jcmt

Subgoal GenerationSubgoal Generation

• Top-down– Start from goal; try to reach

hypotheses– Recursive approach

• Bottom-up– Start with hypotheses; try to reach

goal– Iterative approach

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 14 jcmt

BacktrackingBacktracking

• Multiple matching clauses may be available

• If a failure occurs after a choice point, execution backtracks to the last choice point

• Another match is made and execution continues.

• Implementation of efficient backtracking is crucial in logic programming

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 15 jcmt

Input / Output ParametersInput / Output Parameters

• Goals attempt to satisfy subgoals with whatever value is unified

• Parameters are neither inherently input nor output. Which ever is supplied is used as input.

• When no parameter values are given, the systems attempts to search for any solution that satisfies the pattern

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 16 jcmt

Searching in PrologSearching in Prolog

• Depth-first search is specified– Not pure logic programming

• Attempts to satisfy goals in the order written

• Will try matching clauses in the order in which they were entered into the DB

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 17 jcmt

Prolog vs. Logic ProgrammingProlog vs. Logic Programming

• Interpretation of arithmetic jumps beyond the bounds of strict logic programming unless handled as succ()

• Efficiency requires the use underlying hardware support

• ‘is’ gives an assignment• Binding forces ordering

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 18 jcmt

Search rationaleSearch rationale

• Breadth-first – searches paths in parallel – needs exponentially more space than

depth-first

• Depth-first– Can get caught in infinitely deep search– Programmer is required to order clauses

to prevent endless search

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 19 jcmt

Nonmonotonic reasoningNonmonotonic reasoning

• Updateable database– Assert– Retract

• Does not match logic• Does match world state changes

over time

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 20 jcmt

CutsCuts

• “You have found all the solutions there are; do not bother trying to find any others”

• Predicate that always succeeds, but past which you can never backtrack

• Used with repeat to provide looping

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 21 jcmt

Higher-order rulesHigher-order rules

• Parameters must be terms not predicates

• Logic programming is generally restricted to first-order logic

• Resolution algorithm is complete only for first-order logic

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 22 jcmt

NegationNegation• Unsatisfiability - cannot be proved

true– Absence of data– Closed world assumption

• Conclusions can be drawn about relationships that DO hold

• NO conclusions can be made about relationships that do NOT hold

• not( -- ) predicate succeeds if -- fails

CSE 3302 CSE@UTA Programming Languages Ch. 13Ch. 13 23 jcmt

EquivalenceEquivalence

• Term equality• Other types of equivalence cannot

be defined– In terms of logical properties

• Term inequality requires complete binding for correct interpretation