(penguin-rule (if (animal-x is bird) (animal-x can swim)) (then (animal-x is penguin))) (?x is bird)

Similar documents
Associative Database Managment WIlensky Chapter 22

Associative Database Managment

Lisp Basic Example Test Questions

Announcements. Today s Menu

Recursion & Iteration

Functions, Conditionals & Predicates

(defun fill-nodes (nodes texts name) (mapcar #'fill-node (replicate-nodes nodes (length texts) name) texts))

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 4. LISP: PROMĚNNÉ, DALŠÍ VLASTNOSTI FUNKCÍ, BLOKY, MAPOVACÍ FUNKCIONÁLY, ITERAČNÍ CYKLY,

Midterm Examination (Sample Solutions), Cmput 325

Common Lisp. Blake McBride

Pattern Matching WIlensky Chapter 21

Imperative languages

Lecture Notes on Lisp A Brief Introduction

Review of Functional Programming

Vectors in Scheme. Data Structures in Scheme

15 Unification and Embedded Languages in Lisp

Modern Programming Languages. Lecture LISP Programming Language An Introduction

A little bit of Lisp

Lisp. Versions of LISP

CSCI 2210: Programming in Lisp. Progn. Block. CSCI Programming in Lisp; Instructor: Alok Mehta 1. ANSI Common Lisp, Chapters 5-10

Functional Programming. Pure Functional Programming

Department of Computer and information Science Norwegian University of Science and Technology

19 Machine Learning in Lisp

MIDTERM EXAMINATION - CS130 - Spring 2005

Symbols are more than variables. Symbols and Property Lists. Symbols are more than variables. Symbols are more than variables.

Building a system for symbolic differentiation

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 5. LISP: MAKRA, DATOVÁ STRUKTURA ZÁZNAM

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Lisp: Question 1. Dr. Zoran Duric () Midterm Review 1 1/ 13 September 23, / 13

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

A Genetic Algorithm Implementation

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

ALISP interpreter in Awk

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Documentation for LISP in BASIC

CSc 520 Principles of Programming Languages

;; definition of function, fun, that adds 7 to the input (define fun (lambda (x) (+ x 7)))

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Imperative, OO and Functional Languages A C program is

Simplifying Logical Formulas

MORE SCHEME. 1 What Would Scheme Print? COMPUTER SCIENCE MENTORS 61A. October 30 to November 3, Solution: Solutions begin on the following page.

UMBC CMSC 331 Final Exam

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

Homework 1. Reading. Problems. Handout 3 CSCI 334: Spring, 2012

Common LISP-Introduction

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Streams and Lazy Evaluation in Lisp

Homework #2. Source code with description. Tzewen Wang ECE578 Winter 2005

Heap storage. Dynamic allocation and stacks are generally incompatible.

(defvar *state* nil "The current state: a list of conditions.")

Emacs Lisp Introduction

Robot Programming with Lisp

(defmacro while (condition &body body) `(iterate loop () (if,condition (loop)))))

Building a system for symbolic differentiation

Introduction to Functional Programming and basic Lisp

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Using Symbols in Expressions (1) evaluate sub-expressions... Number. ( ) machine code to add

Functional Programming. Pure Functional Languages

INF4820. Common Lisp: Closures and Macros

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Turtles All The Way Down

UMBC CMSC 331 Final Exam

regsim.scm ~/umb/cs450/ch5.base/ 1 11/11/13

CS61A Midterm 2 Review (v1.1)

Sample Final Exam Questions

An Explicit-Continuation Metacircular Evaluator

Scheme: Strings Scheme: I/O

Data abstraction, revisited

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Problems 3-1, 3.3, 3.4 and 3.5 in Chapter 3 of Winston and Horn's LISP (see Below)

User-defined Functions. Conditional Expressions in Scheme

(4) UNIX 1 UNIX MAGAZINE Makefile ` ,000. / / Makefile. Makefile UNIX

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Functional Programming. Pure Functional Languages

CS 394P Automatic Programming Class Notes. Gordon S. Novak Jr. Department of Computer Sciences University of Texas at Austin

Introduction to Lisp

Functional programming with Common Lisp

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

6.001: Structure and Interpretation of Computer Programs

John McCarthy IBM 704

Introduction to Scheme

Course Title: Mastering the Visual LISP Integrated Development Environment (IDE)

CMPUT325 Extensions to Pure Lisp. Extensions to Pure Lisp. Bob Price and Russ Greiner. 5th October 2004

Speaker Name: Bill Kramer. Course: CP33-2 A Visual LISP Wizard's Intro to Object Magic. Course Description

Artificial Intelligence Programming

Homework 1. Notes. What To Turn In. Unix Accounts. Reading. Handout 3 CSCI 334: Spring, 2017

CSCI337 Organisation of Programming Languages LISP

CMSC 331 Final Exam Fall 2013

Intro. Scheme Basics. scm> 5 5. scm>

Berkeley Scheme s OOP

Putting the fun in functional programming

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

CS 275 Name Final Exam Solutions December 16, 2016

Discussion 12 The MCE (solutions)

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

ACSL All-Star Practice

Transcription:

93 10 1993.11.15 1 1.1 (penguin-rule (if (animal-x is bird) (animal-x can swim)) (then (animal-x is penguin))) (animal-x) 3 animal-y animal-z 1 (penguin-rule (if (?x is bird) (?x can swim)) (then (?x is penguin)))?x animal-x animal-y (?x is bird)?x (mago-rule (if (?x is a child-of?y) (?y is a child-of?z)) (then (?x is a grandchild-of?z))) 1.2 Lisp?x (symbol)?? (defun varp (x) (and (symbolp x) (char= (char (symbol-name x) 0) #\?))) 1

x (symbol-name) ((char 0))?(#\?) 1.3 37 2 pat exp exp pat match1 pat ( ) 38 2 pat exp exp pat match 1.4 t >(match (?x is a?y) (this is a pen) ((nil))) ((?X. THIS) (?Y. PEN) (NIL)) 3 matchv (defun matchv (var exp alist &aux a) (setq a (assoc var alist)) (cond ((null a) (cons (cons var exp) alist)) ((equal (cdr a) exp) alist) (t nil))) 39 match1 3 matchv >(match1?x abc ((?x. abc) (nil))) ((?X. ABC) (NIL)) >(match1?x def ((?x. abc) (nil))) NIL >(match1?x def ((nil))) ((?X. DEF) (NIL)) >(match1 def def ((nil))) ((NIL)) 40 match 3 2

1.5 >(expand (?x is a?y) ((?x. this) (?y. pen))) (THIS IS A PEN) 41 var alist expandv (expandv?x ((?x. baka))) BAKA) 42 pat alist expand1 (expand1?x ((?x. baka))) BAKA (expand1 abc ((?x. baka))) ABC 43 pat alist S expand 2 2.1 (setq *facts* ((onna mary) (fuufu bob mary) (jane kodomo-of mary) (jack kodomo-of bob) (otoko jack) (suzanne kodomo-of jack) (alice kodomo-of jack))) /ua/kuno/work/pat.lsp ( )? >(recall+ (?x kodomo-of jack) ((nil))) (((?X. SUZANNE) (NIL)) ((?X. ALICE) (NIL))) jack 44 recall+ 1 recall+ 3

(defun recall-list (patlist &aux alset r) (setq alset (((nil)))) (dolist (pat patlist) (setq alset (mapcan # (lambda (a) (recall+ pat a)) alset)) (if (null alset) (return-from recall-list nil))) alset) >(recall-list ((?x kodomo-of?y) (otoko?x))) (((?Y. BOB) (?X. JACK) (NIL)) ((?Y. JANE) (?X. TOM) (NIL))) 2.2 recall-list then testif usethen tryrule (defun testif (rule &aux alset) (recall-list (cdadr rule))) (defun usethen (rule alset &aux result efact) (setq result nil) (dolist (fact (cdaddr rule)) (dolist (alist alset) (setq efact (expand fact alist)) (cond ((remember efact) (format t "Rule ~A Deduces ~A~%" (car rule) efact) (setq result t))))) result) (defun tryrule (rule &aux alset) (if (setq alset (testif rule)) (usethen rule alset))) (setq *rules* ((fuufu-rule-1 (if (fuufu?x?y)) (then (fuufu?y?x))) (fuufu-rule-2 (if (fuufu?x?y) (otoko?x)) (then (onna?y))) (fuufu-rule-3 (if (fuufu?x?y) (onna?x)) (then (otoko?y))) (kyoudai-rule-1 (if (?x kodomo-of?y) (?z kodomo-of?y)) (then (kyoudai?x?z))) (kyoudai-rule-2 (if (kyoudai?x?y)) (then (kyoudai?y?x))))) >(deduce) Rule FUUFU-RULE-1 Deduces (FUUFU MARY BOB) Rule FUUFU-RULE-2 Deduces (ONNA MARY) Rule KYOUDAI-RULE-1 Deduces (KYOUDAI ALICE ALICE) 4

Rule KYOUDAI-RULE-1 Deduces (KYOUDAI ALICE SUZANNE) Rule KYOUDAI-RULE-1 Deduces (KYOUDAI SUZANNE ALICE) Rule KYOUDAI-RULE-1 Deduces (KYOUDAI SUZANNE SUZANNE) Rule KYOUDAI-RULE-1 Deduces (KYOUDAI JACK JACK) Rule KYOUDAI-RULE-1 Deduces (KYOUDAI JANE JANE) T Lisp ( ) 7 7-A. (!) 7-B. 7-C. A4 (1993 6) ( ) 12 6 (defun varp (x) (and (symbolp x) (char= (char (symbol-name x) 0) #\?))) ;(defun match1 (pat exp) ; (cond ((varp pat) t) ; (t (equal pat exp)))) 5

;(defun match (pat exp) ; (cond ((atom pat) (match1 pat exp)) ; ((atom exp) nil) ; (t (and (match (car pat) (car exp)) ; (match (cdr pat) (cdr exp)))))) (defun matchv (var exp alist &aux a) (setq a (assoc var alist)) (cond ((null a) (cons (cons var exp) alist)) ((equal (cdr a) exp) alist) (t nil))) (defun match1 (pat exp alist) (cond ((varp pat) (matchv pat exp alist)) ((equal pat exp) alist) (t nil))) (defun match (pat exp alist &aux a) (cond ((atom pat) (match1 pat exp alist)) ((atom exp) nil) (t (setq a (match (car pat) (car exp) alist)) (if a (match (cdr pat) (cdr exp) a) nil)))) (defun expandv (var alist &aux a) (setq a (assoc var alist)) (cond ((null a) nil) (t (cdr a)))) (defun expand1 (pat alist) (cond ((varp pat) (expandv pat alist)) (t pat))) (defun expand (pat alist &aux a) (cond ((atom pat) (expand1 pat alist)) (t (cons (expand (car pat) alist) (expand (cdr pat) alist))))) (defun remember (new) (dolist (old *facts*) (if (equal old new) (return-from remember nil))) (push new *facts*) t) (defun recall+ (pat alist &aux a r) (dolist (f *facts*) (if (setq a (match pat f alist)) (push a r))) r) (defun recall-list (patlist &aux alset r) (setq alset (((nil)))) (dolist (pat patlist) (setq alset (mapcan # (lambda (a) (recall+ pat a)) alset)) (if (null alset) (return-from recall-list nil))) alset) (defun testif (rule &aux alset) (recall-list (cdadr rule))) (defun usethen (rule alset &aux result efact) 6

(setq result nil) (dolist (fact (cdaddr rule)) (dolist (alist alset) (setq efact (expand fact alist)) (cond ((remember efact) (format t "Rule ~A Deduces ~A~%" (car rule) efact) (setq result t))))) result) (defun tryrule (rule &aux alset) (if (setq alset (testif rule)) (usethen rule alset))) (defun stepforward () (dolist (rule *rules*) (if (tryrule rule) (return-from stepforward t))) nil) (defun deduce (&aux progress) (setq progress nil) (loop (if (stepforward) (setq progress t) (return-from deduce progress)))) 7

93 : : 8

: Lisp : : : 9