CS 332 Concepts

Lecture 10 – Alternative Programming Languages (Functional – LISP Declarative - PROLOG)

March 21, 2016  Sam Siewert 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 2 Research vs. Development Vision, Objectives, Goals NSF (OMB) – 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 3 Simple View of Research [&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 4 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

Haskell – Purely Functional

 Sam Siewert 5 Using Scheme on PRClab or PC

LISP Interpreter (E.g. GNU , Steel Bank)

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 6 Lisp Basics – Winston & Horn (setf friends '(dick jane sally)) Tested in friends nil CLISP (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) 7 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 8 CSL Lisp REDUCE Algebraic Simplification, Derivation, Etc. Simplifying Equations Compared to Numerical Solutions

 Sam Siewert 9 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 10 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 11 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 12 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 13 Digging Deeper …

Typically LISP is Taught in Introduction to AI (Symbolic) 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

 Sam Siewert 14 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 15 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 16 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 17 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 18 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 19 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 20 Scheme Tools – SCM SCM on is Simple Command Line ssiewert@ssiewert-VirtualBox:~/a331/lisp$ scm SCM version 5e5, Copyright (C) 1990-2006 Free 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 21 Scheme Tools – DrScheme (DrRacket)

Nice GUI and IDE for Scheme Development and Test http://racket-lang.org/download/ (No LISP support)

 Sam Siewert 22 Lisp Tools - Linux CLISP with Emacs Editor or Simple Shell Interpreter

 Sam Siewert 23 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 24 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 25 Practical Considerations

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

 Sam Siewert 26 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 27 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 28 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 29 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 30 Prolog Interactive Session Load a file Hypothesis ?- [likes]. ?- likes(sam,pizza). % likes compiled 0.01 sec, 4,792 bytes true . true. ?- likes(sam,dahl). Simple Forward Chaining true . ?- likes(sam,X). X = dahl . ?- likes(sam,cauliflower). List all rules for Food Sam Likes false. ?- listing(likes). likes(sam, A) :- ?- likes(sam,curry). indian(A), false. 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 31 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. 32 More Install Notes

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

C-Lisp – Only runs on Linux (sudo apt-get install clisp)

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 33