”assert” and ”retract”

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Support.ebsco.com Using the Search History Feature Tutorial.
Singly linked lists Doubly linked lists
Formal Models of Computation Part II The Logic Model
Here’s an interesting conversation. It’s a little lengthy
Higher Order Predicates Higher order predicates in PROLOG. Higher order declarative predicates : – findall – bagof – setof – =.. Higher order non-declarative.
Unit 9 Stay in Touch ——Listening and Speaking. How do they stay in touch with friends? send text messages write letters send s make phone calls.
Prolog.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
CLINIC MANAGEMENT SYSTEM APPOINTMENT SCHEDULING. How will this start? Any schedules already built will not be carried over to the CMS Portal You will.
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.
Lesson 4: Formatting Input Data for Arithmetic
Logic Programming Lecture 5: Nonlogical features, continued: negation-as-failure, collecting solutions, assert/retract.
WELCOME HI GOOD MORNING GOOD MORNING GOOD MORNING GOOD MORNING.
Network Terminology … Remember: Knowledge is Power!
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.
”assert” and ”retract”  Modifying Programs Dynamically  Prolog’s Execution Tree.
SOME IMPORTANT PHRASES FOR BASIC ENGLISH USERS
An Introduction to ASP.NET Web Pages 2 Module 1: Webmatrix Installation and Your First Web Site Tom Perkins.
Kids Computer Club House
1 ENG236: ENG236: C++ Programming Environment (2) Rocky K. C. Chang THE HONG KONG POLYTECHNIC UNIVERSITY.
UNIX command line. In this module you will learn: What is the computer shell What is the command line interface (or Terminal) What is the filesystem tree.
Welcome to My Class Family Name zhang Given Name lei Chinese Name 张雷
Introduction to Python
Introduction to the WebBoard Terry Dennis. The WebBoard - Our Connection The WebBoard URL is
CS Tutorial 1 Getting Started with Visual Studio 2012 (Visual Studio 2010 are no longer available on MSDNAA, please choose Visual Studio 2012 which.
11 A First Game Program Session Session Overview  Begin the creation of an arcade game  Learn software design techniques that apply to any form.
IST 210: PHP BASICS IST 210: Organization of Data IST210 1.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Nonvisual Arrays and Recursion by Chris Brown under Prof. Susan Rodger Duke University June 2012.
Duty Log and Chat Setup SSG Frese, Jerome S. Sensor Manager Cell 12 MDD.
Natural Language Processing 1.‘append’ program - two lists to one or split one list into two 2.How to write a parser in Prolog, how it works (ref. Paul’s.
Unit 1 A time to remember Please refer to page 2 Conversation.
CHAPTER 3 GC Java Fundamentals. 2 BASICS OF JAVA ENVIRONMENT  The environment  The language  Java applications programming Interface API  Various.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Project Deployment IT [211 CAP] How to convert your project to a full application.
Managing SX.e and TWL with MARC and Scripts Jeremiah Curtis
Lesson 4 Using Variables in Python – Creating a Simple ChatBot Program.
Revision at the Beginning of the 2 nd Semester (repeated slides) Small Coursework (40%) – Please complete it asap Go to:
Q and A for Sections 2.9, 4.1 Victor Norman CS106 Fall 2015.
Function Design in LISP. Program Files n LISP programs are plain text –DOS extensions vary; use.lsp for this course n (load “filename.lsp”) n Can use.
1 C++ Programming Basics Chapter 1 Lecture CSIS 10A.
What is Web Site Administration Tool ? WAT Allow you to Configure Web Site With Simple Interface –Manage Users –Manage Roles –Manage Access Rules.
Question : How many different ways are there to climb a staircase with n steps (for example, 100 steps) if you are allowed to skip steps, but not more.
1 / 61 Using the Customer Support Web Site © 2006, Universal Tax Systems, Inc. All Rights Reserved. Customer Support Site Objectives –In this chapter you.
When I want to work with SQL, I start off as if I am doing a regular query.
Review: computing list results Many programs require list results to be computed, built and returned Many programs require list results to be computed,
Computer Programming and Basic Software Engineering 9 Building Graphical User Interface Creating a Multiple-Form Interface.
F28PL1 Programming Languages Lecture 18: Prolog 3.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
More Python!. Lists, Variables with more than one value Variables can point to more than one value at a time. The simplest way to do this is with a List.
Sega 500 Scripted events and Sequences Jeff “Ezeikeil” Giles
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
1) the value you want to look up Vlookups are really easy......just remember you need 4 things: 2) The table range you want to look up to 3) The column.
Kindergarten Sight Words. Yes I can! Come here Look at me.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Dayu Zhang 9/10/2014 Lab03. Outline Brief Review of the 4 Steps in Hello.cpp Example Understand endl and \n Understand Comment Programming Exercise -
1 TP #2: How does Prolog answer questions? n Miscellaneous info; n Last TP exercises solved; n How does Prolog answer questions? n Recursive Prolog programs;
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
Conversing With a Foreigner. How NOT to Start a Conversation Excuse me, may I be your friend? Excuse me, could we chat? Excuse me, would you mind chatting.
OCR A Level F453: The function and purpose of translators Translators a. describe the need for, and use of, translators to convert source code.
IST 210: PHP Basics IST 210: Organization of Data IST2101.
Database application MySQL Database and PhpMyAdmin
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
NextGen Purchasing Calendar Year End 1099 Process
1. Open Visual Studio 2008.
Teaching London Computing
AP Magpie Chatbox "If a computer could think, how could we tell?"
Lesson 09: Lists Class Chat: Attendance: Participation
The Generic List<> Collection class
Presentation transcript:

”assert” and ”retract” Lecture 2 11/04/2017 ”assert” and ”retract” A Simple Chatbot Modifying Programs Dynamically Prolog’s Execution Tree ~ryang/prolog/top.html

A Simple Chatbot – only producing random responses gen_reply(S, R):- % totally random respones_db(random, Res), % create a random number length(Res, Length), Upper is Length+1, random(1, Upper, Rand), nth_item(Res, Rand, R). respones_db(random, [ [hello, '!'], [hi, there , '.', this, is, not, a, hello_world, program, '!'], [it, is, a, nice, day, '.'], [have, you, talked, to, me, before, '?'], ['.', '.', '.', what, do, you, mean, '?'] ]). chatbot:- print_welcome, conversations. conversations:- repeat, % prolog built-in print_prompt(you), readin(S), % defined in file % readin.pl gen_reply(S,R), print_prompt(me), write_list(R), is_quit(S). is_quit(S):- member(bye, S).

(cont. ) | bot1 : sorry i cannot remember everything you said (cont.) | bot1 : sorry i cannot remember everything you said . user |: ok. bot1 : sorry , i am only a single minded chatbot . user |: you said that. bot1 : sorry i cannot remember everything you said . user |: no problem. bot1 : oh ...... ok user |: good. bot1 : can you say it again ? user |: good!!!! bot1 : have you talked to me before ? user |: getting boring now, bye. bot1 : bye ! | ?- chat. Welcome! I am a chatbot Please finish your line with a full stop, ?, or ! user Hi. bot1 : hello ! user |: what is your name? bot1 : have you talked to me before ? user |: no, I don't think so. bot1 : hi there . this is not a hello_world program ! user |: cool. bot1 : sorry , i am only a single minded chatbot . user |: ok, i don't mind. bot1 : . . . what do you mean ? user |: I mean I like you.

A simple way to link chatbot to ‘cango’ program In get_reply(S,R) check if S has a pattern ‘to ... from ...’ if yes, call your ‘cango’, then generate the reply. pattern_tofm([to, X, from, Y|_], X, Y). % yes! pattern_tofm([_|T], X, Y):- % carry on checking pattern_tofm(T, X, Y).

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 prolog code (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 prolog code that matches C It fails if there is nothing to delete

Examples of using assert/retract ?-nice. ?-disgusting. ?-retract(fog). ?-assert(sunshine). ?-funny. ?-retract(raining). nice:- sunshine, \+ raining. % “\+ “ means not funny:- sunshine, raining. disgusting:- raining, fog. 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(5,F) fib(4, F1) fib(3, F2) F is F1+F2 fib(3, F1’) fib(2,F2’) F’ is F1’+F2’ fib(2, F1’’) fib(1,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) We will use this in our chatbot for collecting user’s information Communicating between or-branches An example: ?- binary(X),binary(Y). binary(0). binary(1).

the following program collects a list of boy names :- dynamic tmp/1. find_all_boy_names(NameList):- % initialise the name list as an empty list assert(tmp([])), boy_names(X, _, _), % return solution one by one retract(tmp(CurrentList)), assert(tmp([X|CurrentList])), % add new solution fail. find_all_boy_names(NameList):- retract(tmp(NameList)).

findall( P, cango3(X,Y,P), 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, cango3(X,Y,P), AllRoutes). This returns AllRoutes = [path1,path2,…] where each pathi is a solution

Back to chatbot If you want to remember some lines from user, you can have the following initially respones_db(echo, []). In the program, after reading a sentence S from user, you can do retract(respones_db(echo, CurrentList)), assert(respones(echo,([S|CurrentList])), If your user says, I am from London,.... assert(user_info(from, london)),