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

Post on 02-Feb-2022

2 views 0 download

transcript

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

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

Constants Variables CompoundAtoms

TermsTerms

Constants Variables CompoundAtoms

TermsTerms

Constants Variables CompoundAtoms

TermsTerms

Constants Variables CompoundAtoms

TermsTerms

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).

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.

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

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) :-

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

Numeric ComputationNumeric Computation