SWI-Prolog (Windows Download - Prolog.Org/Download/Stable ) – for Linux, Swi-Prolog (Sudo Apt-Get Install Swi-Prolog)
Total Page:16
File Type:pdf, Size:1020Kb
CS 332 Programming Language Concepts Lecture 11 – Alternative Programming Languages (Functional – LISP Declarative - PROLOG) March 26, 2018 Sam Siewert Reminders Exercise #4 - Your proposals!! - Pair Up if you Like – Williams, Laurie, et al. "Strengthening the case for pair programming." IEEE software 17.4 (2000): 19-25. [Canvas] – 4 Objective Measure, 4 Subjective vs. 3 Objective, 3 Subjective Exercise #5 - work on project Exercise #6 - is your Final Report Exam #2, Last Day of Class, April 26, Review Tues Before Final Exam – Your Presentation – Team or Individual Analysis or Mini-Alternate Programming Language – 12:30-2:30pm, Sat, Apr 28 – Ideally No more than 6 presentations (consider pairing up) – Or, Overflow session (and I bring food!) Sam Siewert 2 Extra Credit – Dijkstra’s Flag Correct spelling of Dijkstra! (please) Dutch, French, and Czech flag require sorting of equal numbers of Red, Blue and White pixels in a PPM (flags) See Assignment #3 for Extra Credit - Turn in Anytime ¾ R, W 3/8 Red 3/8 White ¼ Blue By inspection Sam Siewert 3 Quick Note on Lab #4, Part 3 Hybrid Concept What I asked for is Not Natural! Sort, Add/Delete Like a Linked List, Dump Like Memory Encapsulate Linked-List and Array in a Single Object and/or Instantiate with Either? How Does One Do this? OO? Many Options, Here’s a Few, Take an Approach 1. One Class with Methods the Encapsulates Both An Array Hybrid Prototype - Outcome and Linked List (to provide both behaviors) 2. Make your Linked-List Object Work like an Array (Best Done with C++ where you can Overload “[]”) and Use Methods In Place of Array 3. Hybrid Data Structure – Array with Linked List of Pointers to It! – So Called Write-Anywhere Scheme with Data Map 4. …? http://www.popsci.com/article/cars/will-helicopter-truck-fly Sam Siewert 4 Research vs. Development NSF (OMB) Vision, Objectives, Goals – Acquisition of Knowledge "Research" is defined as a systematic study – Requirements Less Emphasized, To Be directed toward fuller scientific knowledge or Determined understanding of the subject studied. Research is classified as either basic or applied, according – Proof-of-Concept and Prototypes to the objectives of the investigator. Emphasized – Repeatability of Results, Both Expected "Development" is the systematic use of and Unexpected knowledge and understanding gained from research directed toward the production of useful materials, devices, systems, or Experimental Research methods, including design and development – Emphasis on Validation & Verification of prototypes and processes. – Design of Experiments (PL Compare) DoD – Data Analysis Theoretical Research – Models – Mathematical, Logical, Statistical, Probability, Simulation – Analysis of Problems and Domains Sam Siewert https://www.rand.org/content/dam/rand/pubs/monograph_reports/MR1194/MR1194.appb.pdf 5 Simple View of Research [R&D] Acquisition of Knowledge – Theory or Experiment Defined by Existing Knowledge and Expectations Development Innovation / Adv. Tech Applied Basic • Well-known (standard) • Recombination of well-known • Expectations • Nobody knows • Specified application • Specified application • Misconceptions • No application • Assumptions • Specific Problem or Class Spectrum Sam Siewert 6 Functional PL Concepts Based on Lambda Calculus Output is Math Function of Inputs with No Internal State, No Side Effects – Lambda Calculus Also Used to Specify PL Semantics (Denotational Semantics) – Nested Functions and Recursion – Everything is a Function, Including Data (Much like Everything is and Object in OOP) Common Lisp – Symbolic AI, Functional Core – Online C-Lisp Book/Manual – Syntax Overview Haskell – Purely Functional Sam Siewert 7 Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most Widely Used Numerous Lisp Variants – E.g. Scheme – sudo apt-get install scm – Good so you can follow along in Chapter 10, PLP On PRClab, Use Gambit Scheme Interpreter Sam Siewert 8 Lisp Basics – Winston & Horn (setf friends '(dick jane sally)) t Tested in friends nil C-Lisp (setf enemies '(troll grinch ghost)) pi (setf enemies (remove 'ghost enemies)) (append alist blist) enemies (length alist) (setf friends (cons 'ghost friends)) (reverse alist) friends (setf sam '((height 6.1) (defun newfriend (name) (weight 210))) (setf enemies (remove name enemies)) (assoc 'height sam) (setf friends (cons name friends))) (setf nlist '(1 2 3 4 5 6 7 120)) (first '(a b c)) (max 1 2 3 4 5 6 122) (rest '(a b c)) ; parallel let (rest '(c)) (setf x 'outside) (first (rest '(a b c))) (let ((x 'inside) (first '(rest (a b c))) (y x)) (car '(a b c)) (list x y)) (cdr '(a b c)) ; sequential let (cadr '(a b c)) (setf x 'outside) (second '(a b c)) (let* ((x 'inside) (setf alist '(1 2 3 4 5 6)) (y x)) (setf alist '(a b c d e) (list x y)) blist '(x y z)) () (expt 2 3) Sam Siewert (cos 3) 9 Modern C-Lisp IDE LispWorks - Download Graphical Debugger Interactive Windows Mac Linux Sam Siewert 10 Everything is Really a List in Lisp http://www.gigamonkeys.com/book/they-called-it-lisp-for- a-reason-list-processing.html Lists always Have Parens around them To get to an Atom, Use Car on List of ONE Sam Siewert 11 CSL Lisp REDUCE Algebraic Simplification, Derivation, Etc. Simplifying Equations Compared to Numerical Solutions Sam Siewert 12 Lisp Applications and Activity DS-1, Deep-Space 1 – RAX – Remote Agent – Flew Common Lisp Interpreter (Lisp Works) ROS Lisp - http://wiki.ros.org/roslisp Planet Lisp - http://planet.lisp.org/ MIT course – Lisp Intro to Programming https://common-lisp.net/ http://norvig.com/paip.html Sam Siewert 13 Brief Lisp History Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT). McCarthy published its design in a paper in Communications of the ACM in 1960, entitled "Recursive Functions of Symbolic Expressions and Their Computation by Machine, Part I".[9] He showed that with a few simple operators and a notation for functions, one can build a Turing- John McCarthy complete language for algorithms. Sam Siewert 14 Other Uses of LISP – “Latin Roots for Modern Multi-paradigm and Functional” Automated Theorem Proving Game Theory and AI Games Natural Language Processing Automated Planning and Scheduling Optimization Searches Semantic Web and Search Largely Being Replaced by Functional Features in Python, C++, C# and Newer Lisp Dialects such as Scheme and Racket as well as multi-paradigm Caml and Haskell, Swift https://chessprogramming.wikispaces.com/Herbert+Simon Sam Siewert 15 What is Status of Lisp Lisp Python new/current AI teaching programming Pascal language Scheme ML Python is often used for Intro to Programming with Java Ada Smalltalk C++, Java, Swift, Python, C#, Common Lisp etc. are used for production Miranda C++ AI products CLOS Caml Eiffel Haskell Lisp is a great way to learn functional programming Python OCaml based on simplicity, long Ruby history, and pure functional C# subset F# Sam Siewert 16 Shallow to Deep – Structural or Mathematical Equivalence Testing Equality and Equivalence Gets Interesting (Equality Predicates) – Structurally the Same – String Comparison – Real Valued Comparison – Same Function, Same Evaluation of a Function … Most PLs Have Simple Evaluation and Compare Sam Siewert 17 Some Examples … Using A Simple List of Numbers … Most Imperative Procedural PLs have Shallow Copy (I Can’t Assign a Struct to a Struct, Array to an Array, List to a List, etc., Just Single Values) Likewise Comparison is Typically Numerical Only Lambda expressions Two lists … Sam Siewert 18 Note on ANSI Common LISP Steel Bank Common LISP is ANSI Strict Requires Definition Prior to Elaboration (ANSI Addition) Many LISP Interpreters Simply Create the Definition Automatically (E.g. CLISP) Result is the SAME, just WARNING CLISP Has No Warning Sam Siewert 19 Digging Deeper … Typically Lisp is Taught in Introduction to AI (Symbolic) Python often used instead of Lisp Today http://people.csail.mit.edu/phw/Books/#Lisp CLISP - https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/clm.html Often a First Year Graduate or Senior Year Undergraduate Computer Science Class Patrick H. Winston - Books Sam Siewert 20 Lisp Predicates and Functions ; Conditional expression (defun prob-term (p) (cond ((> p 0.75) 'very-likely) ((> p 0.5) 'likely) ((> p 0.25) 'unlikely) (t 'very-unlikely))) ; Predicates and conditionals (equal '(1 2 3 5 4 7 9 11) '(1 2 3 5 4 7 9 11)) (equal alist blist) (member 'e alist) (equal (lambda (x) (* x x)) (lambda (x) (* x x))) (eql (lambda (x) (* x x)) (lambda (x) (* x x))) (equal (lambda (x) (* x x)) (lambda (y) (* y y))) (= 4 4.0) (defun square (n) (* n n)) (mapcar #'square '(1 2 3)) (mapcar #'= '(1 2 3) '(3 2 1)) (funcall #'(lambda (param) (first param)) '(a b c d e f)) (setf trees '((maple shade) (apple fruit))) (member '(maple shade) trees) (member '(maple shade) trees :test #'equal) Sam Siewert 21 Lisp Recursion Designed for Recursion, Nesting and Mapping Functions to Data of Most any Type ; Recursion (defun fibonacci (N) "Compute the N'th Fibonacci number." (if (or (zerop N) (= N 1)) 1 (+ (fibonacci (- N 1)) (fibonacci (- N 2))))) (defun count-elements (list) (if (endp list) 0 (+ 1 (count-elements (rest list))))) (trace count-elements) (count-elements '(1 2 3 4 5 6 7 8 9 10)) (defun count-atoms (list) (cond ((null list) 0) Call ((atom list) 1) (t (+ (count-atoms (first list)) (count-atoms (rest list)))))) (count-atoms '(sqrt (expt x 2) (expt y 2))) Sam Siewert 22 Lisp Introduction - Functional Learn by Example from Many Excellent Tutorials