”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree.

Slides:



Advertisements
Similar presentations
Basics of Database Programming with VB6
Advertisements

Singly linked lists Doubly linked lists
Formal Models of Computation Part II The Logic Model
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 10: Cuts and Negation Theory –Explain how to control Prolog`s backtracking behaviour with.
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
1 Knowledge Based Systems (CM0377) Lecture 11 (Last modified 26th March 2001)
XS - Platform What is XS – Manager ?
Modern Programming Languages
Prolog.
§3 Dynamic Programming Use a table instead of recursion 1. Fibonacci Numbers: F(N) = F(N – 1) + F(N – 2) int Fib( int N ) { if ( N
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
PROLOG Ivan Bratko University of Ljubljana Faculty of Computer and Info. Sc. Ljubljana, Slovenia.
Prolog Search. Implementing Search in Prolog How to represent the problem Uninformed Search –depth first –breadth first –iterative deepening search Informed.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
”assert” and ”retract”
Csci136 Computer Architecture II Lab#4. - Stack and Nested Procedures
1Lecture 12 Introduction to Prolog SWI-Prolog. 2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) University.
MIS 2000 Class 20 System Development Process Updated 2014.
Prolog OR (disjunction) “;” is same as a logical OR “;” is same as a logical OR It is also equivalent to using separate clauses... It is also equivalent.
Dynamic Programming Fibonacci numbers-example- Defined by Recursion F 0 = 0 F 1 = 1 F n = F n-1 + F n-2 n >= 2 F 2 = 0+1; F 3 = 1+1 =2; F 4 = 1+2 = 3 F.
1 Chapter 17: Amortized Analysis III. 2 Dynamic Table Sometimes, we may not know in advance the #objects to be stored in a table We may allocate space.
TEST-DRIVEN DEVELOPMENT Lecture 3. Definition Test-driven development (development through testing) is a technique of programming, in which the unit tests.
© Patrick Blackburn, Johan Bos & Kristina Striegnitz Lecture 11: Database Manipulation and Collecting Solutions Theory –Discuss database manipulation in.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
Database Manipulation Section 7.4. Prolog Knowledge Base n Knowledge base = database –set of facts/rules in the program n Can add/subtract facts and rules.
META-INTERPRETERS AND META-PROGRAMMING Ivan Bratko Faculty of Computer and Info. Sc. Univ. of Ljubljana.
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
For Monday Take home exam due Exam 1. For Wednesday Read chapter 10, sections 1-2 Prolog Handout 4.
CPSC 311, Fall 2009: Dynamic Programming 1 CPSC 311 Analysis of Algorithms Dynamic Programming Prof. Jennifer Welch Fall 2009.
A Guide to SQL, Seventh Edition. Objectives Create a new table from an existing table Change data using the UPDATE command Add new data using the INSERT.
Dynamic Programming Technique. D.P.2 The term Dynamic Programming comes from Control Theory, not computer science. Programming refers to the use of tables.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering More Built-in Predicates Notes for Ch.7 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Original Tree:
Image Processing Using Cilk 1 Parallel Processing – Final Project Image Processing Using Cilk Tomer Y & Tuval A (pp25)
CPSC 411 Design and Analysis of Algorithms Set 5: Dynamic Programming Prof. Jennifer Welch Spring 2011 CPSC 411, Spring 2011: Set 5 1.
LING 388 Language and Computers Lecture 18 10/30/03 Sandiway FONG.
COMP3221: Microprocessors and Embedded Systems Lecture 13: Functions II Lecturer: Hui Wu Session 2, 2005.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
Tail Recursion. Problems with Recursion Recursion is generally favored over iteration in Scheme and many other languages – It’s elegant, minimal, can.
Introduction Session 4. Generate the first N Fibonacci #s 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,... The nth Fibonacci number is the sum of the.
A Computer Science Tapestry 1 Recursion (Tapestry 10.1, 10.3) l Recursion is an indispensable technique in a programming language ä Allows many complex.
ADA: 7. Dynamic Prog.1 Objective o introduce DP, its two hallmarks, and two major programming techniques o look at two examples: the fibonacci.
Data Structures and Algorithms Stacks. Stacks are a special form of collection with LIFO semantics Two methods int push( Stack s, void *item ); - add.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Introduction To PROLOG World view of imperative languages. World view of relational languages. A PROLOG program. Running a PROLOG program. A PROLOG.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
1 Knowledge Based Systems (CM0377) Lecture 3 (Last modified 5th February 2001)
603 Database Systems Senior Lecturer: Laurie Webster II, M.S.S.E.,M.S.E.E., M.S.BME, Ph.D., P.E. Lecture 17 A First Course in Database Systems.
® Microsoft Access 2010 Tutorial 10 Automating Tasks with Macros.
Review: computing list results Many programs require list results to be computed, built and returned Many programs require list results to be computed,
Prolog Cut to Control Search Abstract Data Types Practical Examples Summary.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
The AI War LISP and Prolog Basic Concepts of Logic Programming
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, assert/retract, collecting solutions.
LANDESK SOFTWARE CONFIDENTIAL Tips and Tricks with Filters Jenny Lardh.
Scalatest. 2 Test-Driven Development (TDD) TDD is a technique in which you write the tests before you write the code you want to test This seems backward,
Tirgul 10. What We Will See Today:  Linked lists  Graphs  Trees  Iterators and Generators.
Dynamic Programming & Memoization. When to use? Problem has a recursive formulation Solutions are “ordered” –Earlier vs. later recursions.
Ch. 3 – Search Supplemental slides for CSE 327 Prof. Jeff Heflin.
1 Knowledge Based Systems (CM0377) Lecture 9 (Last modified 11th April 2002)
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
A Brief Introduction to Recursion. Recursion Recursive methods … –methods that call themselves! –They can only solve a base case –So, you divide a problem.
NAMI 360 – Screenshot Guide 1 Organization Profile.
1 CS 430 Database Theory Winter 2005 Lecture 13: SQL DML - Modifying Data.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Cs7120 (Prasad)L26-ProgTech1 Programming Techniques
5.13 Recursion Recursive functions Functions that call themselves
CSE 341 Lecture 5 efficiency issues; tail recursion; print
Presentation transcript:

”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree

Built-ins to Modify Prolog Programs The following built-ins allow modification of the loaded program as it is actually running. assert(C) - adds a clause C to program asserta(C) - C is added to front assertz(C) = assert(C) - C is added to end They always succeed. retract(C) - deletes a clause that matches C It fails if there is nothing to delete

Examples of using assert/retract ?-nice. ?-disgusting. ?-retract(fog). ?-disgusting. ?-assert(sunshine). ?-funny. ?-retract(raining). ?-nice. nice:- sunshine, \+ raining. % “\+ “ means not funny:- sunshine, raining. disgusting:- raining, fog. raining. fog.

Examples of using assert/retract Fibonacci numbers: 1,1,2,3,5,8,13,21,34,… fib(N,F) - F is the N'th Fibonacci number. fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, N2 is N-2, fib(N1,F1), fib(N2,F2), F is F1 + F2.

Execution Tree to Compute fib(6,F) fib(6,F) fib(5, F1)fib(4,F2) F is F1+F2

To improve the above program, we can use “assert” to save intermediate results :- dynamic myfib/2. myfib(1,1). myfib(2,1). myfib(N,F) :- N > 2, N1 is N-1, N2 is N-2, myfib(N1,F1), myfib(N2,F2), F is F1 + F2, % remember n-th Fibonacci number asserta( myfib(N,F) ).

assert/retract plays two roles Adding new information to the database (ref. previous examples) Communicating between or-branches An example: ?- b(X),b(Y). b(0). b(1).

the following program collects a list of boy names make_name_list(NameList):- % initialise the name list as an empty list assert(tmp([])), boy_names(X, _, _), retract(tmp(CurrentList)), assert(tmp([X|CurrentList])), fail. make_name_list(NameList):- retract(tmp(NameList)).

program collects all solutions of cango(X, Y, Path) cango_all(X, Y, _AllRoutes):- % initialise assert(tmp([])), cango(X, Y, Path), retract(tmp(CurrentList)), assert(tmp([(X,Y,Path)|CurrentList])), fail. cango_all(_, _, AllRoutes):- retract(tmp(AllRoutes)).

How to use “findall” Prolog has provided a built-in predicate findall(Vars, Goal, S) which collects all solutions of Goal into S. Here is an example: test(X,Y, AllRoutes):- findall( (P,L), cango(X,Y,P,L), AllRoutes). This returns AllRoutes = [(path1,line1), (path2,line2),…] where path i and line i are lists.