Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prolog or: How I Learned to Stop Worrying and Love the Search.

Similar presentations


Presentation on theme: "Prolog or: How I Learned to Stop Worrying and Love the Search."— Presentation transcript:

1 Prolog or: How I Learned to Stop Worrying and Love the Search

2 Hello. I’m Zach, one of Sorin’s students. ztatlock@cs.ucsd.edu

3 Search Any cycles?

4 Search How about now?

5 Search And now?

6 Search Reach from stopping by ?

7 Search is Fundamental Natural way to phrase problems: Is there an X such that Y? Ubiquitous in Computer Science:

8 Prolog: Interface to Search Search is declarative say what you want not how to get it Often radical simplification shorter, clearer programs less development effort fewer bugs SEARCH

9 Prolog Anatomy 101 Prolog programs do three things: 1.Declare Facts known points in the search space 2. Declare Rules add new points to search space based on old ones 3. Query over Search Space search for a point

10 Prolog Anatomy 101 Facts : known points Rules : add new points from old points Query: is reachable? Rules Facts Query

11 Prolog 1.Basic Syntax 2.Backtracking Search 3.Examples in the toplevel: A.Variables in Queries B.Negation C.Search Order

12 Basic Syntax Declare Facts: parent(homer, bart). parent(marge, bart). parent(mona, homer). Known points in search space Basis of all we can conclude

13 Basic Syntax Simple Query: ?- parent(homer, bart). true. ?- parent(mona, bart). false. Ask if a particular point is in search space

14 Basic Syntax Declare Rules: grandparent(GP, GC) :- parent(GP, P), parent(P, GC). ADD THIS NEW POINT IF YOU FIND THESE OLD POINTS VARIABLES

15 Atoms vs. Variables Atom starts with lowercase letter particular individual object only equal to self Variable starts with uppercase letter “hole” replaceable by atom

16 grandparent(GP, GC) can be added to search space If you find 3 atoms GP, P, GC Such that both parent(GP, P) and parent(P, GC) Basic Syntax grandparent(GP, GC) :- parent(GP, P), parent(P, GC).

17 Basic Syntax Query that requires Rule: ?- grandparent(mona, bart). true. ?- grandparent(homer, bart). false. Q: Which query takes longer?

18 Prolog 1.Basic Syntax 2.Backtracking Search 3.Examples in the toplevel: A.Variables in Queries B.Negation C.Search Order

19 Backtracking Search Query : is in search space? Prolog searches for backward: Start at Look for path back to Facts using Rules Top-down approach since Bottom-up is too inefficient

20 Backtracking Search Search( ) 1. in search space? Success! 2. For each rule R that could add : For each old node R requires : If Search( ) fails, try next rule. All old nodes found. Success! 3. Not in space, no rule can add. Fail.

21 Prolog 1.Basic Syntax 2.Backtracking Search 3.Examples in the toplevel: A.Variables in Queries B.Negation C.Search Order

22


Download ppt "Prolog or: How I Learned to Stop Worrying and Love the Search."

Similar presentations


Ads by Google