1 / 30

Symbolic Programming and LISP CS570 Lecture Notes by Jin Hyung Kim Computer Science Department

Symbolic Programming and LISP CS570 Lecture Notes by Jin Hyung Kim Computer Science Department KAIST. Programming Language. A Tool of Representing Knowledge Any Language for Any Task Language Selection driven by Convenience Efficiency man power machine power.

edric
Télécharger la présentation

Symbolic Programming and LISP CS570 Lecture Notes by Jin Hyung Kim Computer Science Department

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Symbolic Programming and LISP CS570 Lecture Notes by Jin Hyung Kim Computer Science Department KAIST

  2. Programming Language • A Tool of Representing Knowledge • Any Language for Any Task • Language Selection driven by • Convenience • Efficiency • man power • machine power

  3. Required Characteristics of AI Languages • Fast prototyping and Refinement • Different Software Engineering Concept • Emphasis of (Heuristic) Knowledge • Knowledge representation • Symbolic Computation • Separation of Domain Knowledge and programming knowledge

  4. Special Features Required for AI Programming • To Support Symbolic Programming • Variety of data types • List, set, tree, graph, etc. • Rich Control Structures • Hierarchical Control, Recursion, backtracking, demons • Pattern Matching • Pattern-directed data retrieval • Pattern-directed control mechanism • Deductive mechanism

  5. LISP is the Standard AI Language • Invented in 1958 by John McCarthy • Considerable Evolution since then • Used in virtually all AI laboratories • LISP is • LISt Processing Language • Functional Language • High-level machine language • Interpreter Language with compiler support

  6. LISP Language Features • Simple, Uniform Syntax • Run-type type and Boundary checking • Uniform representation of program and data • Powerful MACRO Facility • Machine level access • Object-oriented extensions • Symbol and properties • Flexible control structure

  7. LISP/ Software Engineering • permit modular programming • incremental development • Good for even • General purpose • system programming - LISP machine

  8. LISP has Conventional Features as well • Structured Programming • Numeric Processing • Strings and Arrays • Input/Output with Stream and Formatting • Bit manipulation

  9. LISP is Flexible • Uniform Access to System, Library and User-defined Facilities • New data type and control structures can be created by MACRO • Language-embedding language • OPS5, PLANNER, CONNIVER, MACSYMA, FLAVOR, PROLOG • Easy to accommodate new Developments in Programming Language • Will survive for a long time

  10. LISP system features • Tracer • Debugger • Garbage Collector • Built-in Editor • Structured • Screen Oriented • Spelling Corrector • Object System (CLOS) Support Powerful Development and Test Environment Good for Prototyping

  11. LISP Implementation • Many dialects ==> Common Lisp Standard • KAIST has CommonLisp on • adam.kaist.ac.kr • /usr/local/lib/cmucl/bin/lisp • Consult with

  12. LISP Object SQRT LONG_NAME_OK -17.5 NIL T (John Mary SoonHee) (PLUS 2 3) (TIMES (PLUS 2 3) 6) ATOM LIST S- Expression

  13. READ-EVAL-PRINT Loop • Basic User Interface Read an Expression Evaluate the Expression Print the Evaluate result

  14. Evaluation of S-Expression • Number ====> Number • Symbol ====> content of value cell • T ====> T • NIL ====> NIL (false - empty list) • List ====> as a function call (function arg1 arg2 ...)

  15. Value Property function Package Print-Name Internal Representation of Symbol Symbol#3241

  16. Evaluation Examples > (setf (get ‘apple ‘color) ‘red) red > (get ‘apple ‘color) red > (defun apple () (print apple ??) apple > (apple) apple ? apple ? > (defun hypotenuse (a b) (sqrt (+ (square a) (square b)))) > (defun square (x) (* x x)) > 12 12 > (+ 2 3) 5 > (+ (* 2 3)(1+ 9)) 16 > apple unbound variable >(setq apple ‘(an apple)) (an apple) >apple (an apple)

  17. Quote / Back Quote • Quote ’(a b c) => (a b c) • Back Quote (setq a ’(1 2 3)) `(a ,a b) => (a (1 2 3) b) `(a @,a b) => (a 1 2 3 b) (defmacro pushf (item stack) `(setq ,stack (cons ,item ,stack))

  18. Property List • Collection of Property-value pairs associated with a symbol • Symbol: john • age 45 • height 5.7 • weight 180 • complexion black • education BS-in-engineering • Operation (setf (get ’john ’address) ’(hanbit Apt 101)) (get ’john ’age) ==> 45 (get ’john ’wife) ==> nil

  19. Condition Clauses • (iftest conditional alternate) • (cond ((test-1 action-1) (test-2 action-2) . . . (test-n action-n)) • (or (oddp 2)(and (oddp 4)(evenp 6)))

  20. Iterative Construct >(mapcar #’+ ’(1 2 3) ’(4 5 6)) (5 7 9) >(do ((i 1 (+ j 1)) (j 1 (* j 1))) ; var, init, incr ((= i 10) j) ; end test (princ i)) ; body 123456789 362880 >(dolist (x ‘(a b c d))(princ x)) ABCD >(dotimes (I 10 I)(princ I)) 0123456789 10 do - Paralle assigment do* - sequential assignment

  21. List Handling with Recursion (defun insert (new pairs) (cond ((null pairs)(cons new ())) ((= (first new) (first (first pairs))) pairs) ((< (first new)(first (first pairs))) (cons new pairs)) (t (cons (first pairs) (insert new (rest pairs))))))

  22. DEFUN (defun foo (first &optional (second 20)) (+ first second)) (foo 3) => 23 (foo 3 4) = 7 (foo 3 4 5) => error (defun bar (first &rest args) (* first (apply #’+ args)) (bar 2 3 4) => 14

  23. SCOPING >(setq x 2) >(defun local (x) (setq x (+ x 1))(* x x)) LOCAL >(local (+ x 1)) 16 >x 2 >(defun global (x) (setq sym (+ x 1)(* sym sym)) GLOBAL >(global 1) 4 >sym 2 (let ((local-1 initial-1) … (local-n initial-n)) (action-1) . . . )

  24. Macros • Substitute chunk of code into program • Special form of function which evaluates substituted code • Example >(defmacro pushf (item stack) (list ’setq stack (list ’cons item stack))) > (setq bag ’(orange grape milk sandwich)) > (pushf apple bag) (setq bag (cons ’apple bag)) > bag (apple orange grape milk sandwich)

  25. Lambda and Functions as Arguments >(defun decreasingp (x y f) (if (> (funcall f x)(funcall f y)) t nil)) >(decreasingp 1 2 #’(lambda (x)(* x x))) NIL >(setq reciprocal #’(lambda (x)(/ 1 x))) >(decreasingp 1 2 reciprocal) T >(let ((x 0)) (setq counter #’(lambda ()(setq x (+ x 1))))) >(funcall counter) 1 >(funcall counter) 2 > x error: unbound variable - X

  26. Trace >(defun fact (n) (cond ((= n 1) 1) (t (* n (fact (1- n))))) >(trace fact) >(fact 4) Entering: FACT, Argument list: (4) Entering: FACT, Argument list: (3) Entering: FACT, Argument list: (2) Entering: FACT, Argument list: (1) Exiting: FACT, Value: 1 Exiting: FACT, Value: 2 Exiting: FACT, Value: 6 Exiting: FACT, Value: 24

  27. Package • Partition of LISP world • Interface print names typed by user and internal representation of symbols • Current Package : *package* • <package USER> <package LISP> <package KETWORD • refer symbol in other package by package quailifier • foo:bar - external symbol • foo::bar - internal symbol • :test - keyword package, constant

  28. Internal and External Symbols >(make-package ‘old) #<package OLD> >*package* #<package USER> >(setq old::foo 123) 123 >foo error: unbound variable - FOO >old:foo error: external symbol not found - “FOO” >old::foo 123 Import export use-package

  29. Input and Formatted Output >(read file) >(read-char file) >(format T “~4,2f is a real number”, 1.23456) 1.23 is a real number NIL >(format nil “~D is integer;~%~A is symbol”, 4,’foo) “4 is integer;FOO is symbol” >(setq file (open “file-name” :direction :output)) >(format file “this is a file io”)

  30. Manual • COMMON LISP • by Guy L/ Steele Jr. • Digital Press • Online version http://www.cs.cmu.edu/Web/Groups/AI/html/cltl/cltl2.html

More Related