<<

CS 332 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 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

¾ , 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 , 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 ++ 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 (E.g. GNU , 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)) 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 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 – Here, here, here Try Examples for Scheme in PLP and here, here, http://racket-lang.org/

>(defun fib (N) >(fib 3) "Compute the N'th Fibonacci number." (if (or (zerop N) (= N 1)) 1> (FIB 3) 1 Simplify with let expressions 2> (FIB 2) (let 3> (FIB 1) ((F1 (fib (- N 1))) <3 (FIB 1) (F2 (fib (- N 2)))) 3> (FIB 0) (+ F1 F2)))) <3 (FIB 1) Note: use trace for debug <2 (FIB 2) FIB 2> (FIB 1) <2 (FIB 1) >(trace fib) <1 (FIB 3) 3 (FIB)  Sam Siewert 23 Lambda Calculus Two Major Theoretical Models of Computability (1930’s)

– Turing Machine (Abstract Machine – PDA with Tape) Computable Functions Non-Computable Functions (Busy beaver)

– Alonzo Church’s Hypothesis and Lambda Calculus Any Sufficient Formalism (Turing Machine, Lambda Calculus) is Complete, Equally Powerful and Anything Computed in One can Be Computed in the Other

Both Pre-date Modern Von Neumann Digital Computers (1930’s – Prior to WWII during time of Enigma)

Age of Computational Machines – E.g. Difference Engine

 Sam Siewert 24 Functional PL Features PLP Page 507 – Lisp vs. Scheme 1. First-class function values and higher order functions 2. Polymorphism (dynamic bindings) – Scheme Deep Binding 3. List types and operators 4. Structured functions returns (compared to single value) 5. Constructors for aggregates 6. Garbage collection (automatic memory management)

Common Lisp Scheme Only (Why not Common Lisp?)

>((lambda (x) (* x x)) 3) > (let ((a 3) > (b 4) 9 > (square (lambda (x) (* x x))) > (plus +)) >(sqrt 25) > (sqrt (plus (square a) (square b)))) 5.0 5.0

 Sam Siewert 25 Common Lisp vs. Scheme Scheme Only (see note on p. 511, “A word of caution…”) > (let ((a 3) > (b 4) > (square (lambda (x) (* x x))) > (plus +)) > (sqrt (plus (square a) (square b)))) 5.0 Common Lisp (But, this is different, so how do we do the same?) >(defun square (x) (* x x)) SQUARE Note: namespace square and plus >(defun plus (a b) (+ a b)) PLUS > Note: square and plus ok in let, but not used (let ((a 3) (b 4) (square (lambda (x) (* x x))) (plus +)) (sqrt (plus (square a) (square b))))

5.0  Sam Siewert 26 Scheme Tools – SCM SCM on is Simple Command Line ssiewert@ssiewert-VirtualBox:~/a331/lisp$ scm SCM version 5e5, Copyright (C) 1990-2006 Foundation. ;loading /usr/share//require ;done loading /usr/share/slib/require.scm ;loading /usr/share/slib/require ;done loading /usr/share/slib/require.scm ;loading /usr/lib/scm/Link ;done loading /usr/lib/scm/Link.scm ;loading /usr/lib/scm/Transcen ;done loading /usr/lib/scm/Transcen.scm > (load "map.scm") ;loading map.scm ;done loading map.scm # > (let ((a 3) > (b 4) > (square (lambda (x) (* x x))) > (plus +)) > (sqrt (plus (square a) (square b)))) 5.0 >  Sam Siewert 27

Build your Own Dialect of Functional Programming Nice GUI and IDE for Scheme Development and Test http://racket-lang.org/download/ (Build your own interpreted PL) – Scheme option (determine language from source)

 Sam Siewert 28 GNU Scheme https://www.gnu.org/software/mit-scheme/ Windows, OS-X, (Linux)

 Sam Siewert 29 Lisp Tools - Linux CLISP with Emacs Editor or Simple Shell Interpreter

 Sam Siewert 30 Lisp Equivalent Let* with funcall ;(defun square (x) (* x x)) ;(defun plus (a b) (+ a b))

(let* ((a 3) (b 4) (square #'(lambda (x) (* x x))) (plus #'+)) (sqrt (funcall plus (funcall square a) (funcall square b))))

Common Lisp Can Be Functional, But Not So Easy 1. Let* causes sequential bindings (not required, but interesting) 2. #’ refers to the function object and defers evaluation (required) 3. funcall actually invokes the function object (required)

Now the Let expression is much more like Scheme

 Sam Siewert 31 Lisp IDE and Editors Many Options …

GNU Common Lisp for ftp://ftp.cs.utexas.edu/pub/novak/gcl/gcl.exe Windows (per lab instructions) CLISP – sudo apt-get install – sudo apt-get install clisp-doc – sudo apt-get install – In Emacs, do M-x run-ilisp, Dialect: clisp

Can Use Emacs, Vi, etc. and add Syntax Features – sudo apt-get install ilisp – Tricky to set up if you’re new to Emacs

Can Use Eclipse – Tricky to use if you’re new to Eclipse – I have never used – Cusp and Dendelion

Can Just use Nano, Vi, etc. and Tough it Out!

 Sam Siewert 32 Practical Considerations

Use (quit) to exit the interpreter Use (load “pathandfilename”) to load a file with Lisp expressions in it

 Sam Siewert 33 Lisp and Scheme on Linux - Summary

PLP Prefers to Use Scheme Examples

For PLP book, use SCM or DrScheme instead of GCM/CLISP

Both are Fine, But Have +/- (Winston and Horn Examples)

A Point of Much Discussion & Many Prefer Scheme

Efficiency Issues – E.g. Lazy Evaluation of Expressions

Common Lisp Though is Portable, Comprehensive and Lots of Code to Re-Use Including CLOS

Modify /etc/default/gcl for profiling and ANSI support – DEFAULT_GCL_ANSI="y" – DEFAULT_GCL_PROF="y"

 Sam Siewert 34 Declarative Programming Language Concepts Program Axioms – Runtime derives Constructive Proof for Inputs (Query) Based on Logic – Rules and Facts Query Rule and Fact Base with a Predicate Forward Chaining – Derives conclusions from assertions Backward Chaining – Find Assertions that Support a Hypothesis The Cut! – Controls Backtracking

Semantic Web Automated Theorem Proving Science and Medicine – Hypothesis, Facts, Rules

 Sam Siewert 35 Prolog on PRClab and Linux

Like LISP, Numerous Prolog Interpreters Exist

PRClab has “gprolog”, /usr/local/gprolog-1.4.4/

On Ubuntu Linux, sudo apt-get install gprolog

 Sam Siewert 36 Prolog Introduction - Declarative

RULES FACTS %% Demo coming from %% http://www.math.unipd.it/~sperduti/AI11/likes.pl likes(sam,Food) :- indian(curry). indian(Food), indian(dahl). mild(Food). indian(tandoori). likes(sam,Food) :- indian(kurma). chinese(Food). mild(dahl). likes(sam,Food) :- mild(tandoori). italian(Food). mild(kurma). likes(sam,chips). chinese(chow_mein). chinese(chop_suey). chinese(sweet_and_sour).

italian(pizza). italian(spaghetti).

 Sam Siewert 37 Prolog Interactive Session Load a file Hypothesis ?- [likes]. ?- likes(sam,pizza). % likes compiled 0.01 sec, 4,792 bytes true . true. Simple Forward Chaining ?- likes(sam,dahl). true . ?- likes(sam,X). X = dahl . ?- likes(sam,cauliflower). List all rules for Food Sam Likes false. ?- listing(likes). ?- likes(sam,curry). likes(sam, A) :- false. indian(A), mild(A). likes(sam, A) :- ?- listing(indian). chinese(A). indian(curry). likes(sam, A) :- indian(dahl). italian(A). indian(tandoori). likes(sam, chips). indian(kurma). true. true.  Sam Siewert 38 Prolog Interactive Session Turn on tracing Predicate & Hypothesis ?- trace. [trace] ?- likes(sam,dahl). [trace] ?- Call: (6) likes(sam, dahl) ? creep Call: (7) indian(dahl) ? creep Simple Forward Chaining Exit: (7) indian(dahl) ? creep Call: (7) mild(dahl) ? creep [trace] ?- likes(sam,X). Exit: (7) mild(dahl) ? creep Call: (6) likes(sam, _G387) ? creep Exit: (6) likes(sam, dahl) ? creep Call: (7) indian(_G387) ? creep true Exit: (7) indian(curry) ? creep

Call: (7) mild(curry) ? creep [trace] ?- likes(sam,beet). Fail: (7) mild(curry) ? creep Call: (6) likes(sam, beet) ? creep Redo: (7) indian(_G387) ? creep Call: (7) indian(beet) ? creep Exit: (7) indian(dahl) ? creep Fail: (7) indian(beet) ? creep Call: (7) mild(dahl) ? creep Redo: (6) likes(sam, beet) ? creep Exit: (7) mild(dahl) ? creep Call: (7) chinese(beet) ? creep Exit: (6) likes(sam, dahl) ? creep Fail: (7) chinese(beet) ? creep X = dahl . Redo: (6) likes(sam, beet) ? creep Call: (7) italian(beet) ? creep Fail: (7) italian(beet) ? creep Redo: (6) likes(sam, beet) ? creep Fail: (6) likes(sam, beet) ? creep  Sam Siewert false . 39 More Install Notes – Functional & Logic PLs

SWI-Prolog (Windows download - http://www.swi- prolog.org/download/stable ) – for Linux, swi-prolog (sudo apt-get install swi-prolog)

C-Lisp – Linux (sudo apt-get install clisp) LispWorks – Download (Linux, Windows, Mac)

Scheme / Racket - https://racket-lang.org/download/ - for Linux, scm (sudo apt-get install scm)

PyCharm (https://www.jetbrains.com/pycharm/download - OS-X, Windows, Linux), python (sudo apt-get install python)

 Sam Siewert 40 What about Lisp and Prolog Today?

Less often used than imperative procedural programming, but not dead

GitHub activity – https://github.com/trending/prolog – https://github.com/trending/common-lisp

Lisp User Groups – http://planet.lisp.org/ – http://www.cliki.net/ – https://groups.google.com/forum/#!forum/comp.lang.lisp

Prolog User Groups and Activity – http://www.jiprolog.com/ – http://www.logicprogramming.org/ – https://groups.google.com/forum/#!forum/comp.lang.prolog

 Sam Siewert 41