9
CSE 130 : Spring 2010 Programming Languages Lecture 16: Prolog Lecture 16: Prolog Ranjit Jhala UC San Diego Declarative Programming Declarative Programming Declare problem runtime finds solution Declare problem, runtime finds solution Given a graph of n cities, Fi d ih i i l h Find tour with minimum length Philosophy Writing constraints is easy (human) Writing constraints is easy (human) Writing search heuristic is hard (PL) History: Classical AI History: Classical AI Program = Expert DatabasesProgram = Expert Databases Humans describe world as facts and rules System answers questions about world Example: Facts/rules about symptoms and diseases Questions about condition of some patient

Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

CSE 130 : Spring 2010

Programming Languages

Lecture 16: Prolog Lecture 16: Prolog

Ranjit JhalaUC San Diego

Declarative ProgrammingDeclarative Programming

Declare problem runtime finds solutionDeclare problem, runtime finds solutionGiven a graph of n cities, Fi d i h i i l hFind tour with minimum length

Philosophy Writing constraints is easy (human)Writing constraints is easy (human)Writing search heuristic is hard (PL)

History: Classical AIHistory: Classical AI

Program = “Expert Databases”Program = Expert DatabasesHumans describe world as facts and rulesSystem answers questions about world

Example:Facts/rules about symptoms and diseasesQuestions about condition of some patientQ p

Page 2: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

Modern ApplicationsModern Applications

Scheduling Flights NBA Scheduling Flights, NBA, …

Database Queries SQL < Prolog

Config. Management Makefile

Distributed Systems ???Distributed Systems ???

H ll P lHello Prolog

Program Facts+Rules+QueriesProgram = Facts+Rules+Queries

No functions, statements, assignments…

Facts Rules QueriesFacts, Rules, Queries

are all built from

Terms

Page 3: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

Constants Variables CompoundAtoms

TermsTerms

Constants Variables CompoundAtoms

TermsTerms

Constants Variables CompoundAtoms

TermsTerms

Constants Variables CompoundAtoms

TermsTerms

Page 4: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

Constants Variables CompoundAtoms

TermsTerms

Terms Structured DataTerms = Structured Data

parent factorial

alice 5

alice bob 5

alice 5 parent(alice, bob) factorial(5)

Don’t “mean” anythingDon t mean anything

Facts Rules QueriesFacts, Rules, Queries

are all built from

Terms

Facts = Terms without VariablesFacts Terms without Variables

parent(alice, bob).parent(alice, bob).parent(margaret, kim).

t(f li j h )parent(felix, john).

Page 5: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

Facts = Terms without VariablesFacts Terms without Variables

parent(felix, john).

Predicate with 2 argumentsPredicate with 2 arguments“boolean-valued function”

Facts Rules QueriesFacts, Rules, Queries

are all built from

Terms

Queries = Terms with VariablesQueries Terms with Variables

parent(margaret, X).

Fi d X h th t Find X such that: parent(margaret X) parent(margaret,X)

Queries = Terms with VariablesQueries Terms with Variables

parent(margaret, X).

Find X such that Find X such that parent(margaret, X)p ( g , )

is a known fact.

Page 6: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

Queries = Terms with VariablesQueries Terms with Variables

parent(X, X).

Find X such that Find X such that parent(X, X)p ( , )

is a known fact.

How does Prolog Solve Queries?

UnificationUnification

UnificationUnification UnificationUnification

Page 7: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

Conjunctive QueriesConjunctive Queries

Facts Rules QueriesFacts, Rules, Queries

are all built from

TTerms

Rules = New facts from OldRules New facts from Old Rules = New facts from OldRules New facts from Old

Lets write a “grandparent” relationshipLets write a grandparent relationship…

grandparent(GP,GC) :-

Page 8: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

P F R lProgram = Facts + Rules

Disjunctive RulesDisjunctive Rules

Lets write a “has family” relationshipLets write a has_family relationship…

Recursive RulesRecursive Rules

Lets write a “ancestor” relationshipLets write a ancestor relationship…

Backtracking SearchBacktracking Search

Page 9: Prolog CSE 130 : Spring 2010 16: Ranjit Jhala UC San Diego

Numeric ComputationNumeric Computation