41
March 26, 2018 Sam Siewert CS 332 Programming Language Concepts Lecture 11 – Alternative Programming Languages (Functional – LISP Declarative - PROLOG)

CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

  • Upload
    others

  • View
    19

  • Download
    0

Embed Size (px)

Citation preview

Page 1: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

March 26, 2018 Sam Siewert

CS 332Programming Language Concepts

Lecture 11 – Alternative Programming Languages

(Functional – LISPDeclarative - PROLOG)

Page 2: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

RemindersExercise #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 projectExercise #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

Page 3: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Extra Credit – Dijkstra’s FlagCorrect 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

Sam Siewert 3

¾ R, W3/8 Red3/8 White¼ Blue

By inspection

Page 4: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Quick Note on Lab #4, Part 3What I asked for is Not Natural!Sort, Add/Delete Like a Linked List, Dump Like MemoryEncapsulate Linked-List and Array in a Single Object and/or Instantiate with Either?How Does One Do this? OO?

Sam Siewert 4http://www.popsci.com/article/cars/will-helicopter-truck-fly

Many Options, Here’s a Few, Take an Approach

1. One Class with Methods the Encapsulates Both An Array 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. …?

Hybrid Concept

Hybrid Prototype - Outcome

Page 5: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Research vs. DevelopmentVision, Objectives, Goals– Acquisition of Knowledge– Requirements Less Emphasized, To Be

Determined– Proof-of-Concept and Prototypes

Emphasized– Repeatability of Results, Both Expected

and Unexpected

Experimental Research– Emphasis on Validation & Verification– Design of Experiments (PL Compare)– Data Analysis

Theoretical Research– Models – Mathematical, Logical,

Statistical, Probability, Simulation– Analysis of Problems and Domains

Sam Siewert 5

NSF (OMB)

"Research" is defined as a systematic studydirected toward fuller scientific knowledge orunderstanding of the subject studied. Researchis classified as either basic or applied, accordingto the objectives of the investigator.

"Development" is the systematic use ofknowledge and understanding gained fromresearch directed toward the production ofuseful materials, devices, systems, ormethods, including design and developmentof prototypes and processes.

DoD

https://www.rand.org/content/dam/rand/pubs/monograph_reports/MR1194/MR1194.appb.pdf

Page 6: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Simple View of Research [R&D]Acquisition of Knowledge – Theory or ExperimentDefined by Existing Knowledge and Expectations

Sam Siewert 6

Spectrum

Basic• Nobody knows• No application

Development• Well-known (standard)• Specified application

Innovation / Adv. Tech• Recombination of well-known• Specified application

Applied• Expectations

• Misconceptions• Assumptions

• Specific Problem or Class

Page 7: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Functional PL ConceptsBased 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

Page 8: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Using Scheme on PRClab or PCLISP 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

Page 9: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp Basics – Winston & Horn

Sam Siewert 9

(setf friends '(dick jane sally))friends(setf enemies '(troll grinch ghost))(setf enemies (remove 'ghost enemies))enemies(setf friends (cons 'ghost friends))friends(defun newfriend (name)(setf enemies (remove name enemies))(setf friends (cons name friends)))

(first '(a b c))(rest '(a b c))(rest '(c))(first (rest '(a b c)))(first '(rest (a b c)))(car '(a b c))(cdr '(a b c))(cadr '(a b c))(second '(a b c))(setf alist '(1 2 3 4 5 6))(setf alist '(a b c d e)

blist '(x y z))

tnilpi(append alist blist)(length alist)(reverse alist)(setf sam '((height 6.1)

(weight 210)))(assoc 'height sam)(setf nlist '(1 2 3 4 5 6 7 120))(max 1 2 3 4 5 6 122); parallel let(setf x 'outside)(let ((x 'inside)

(y x))(list x y))

; sequential let(setf x 'outside)(let* ((x 'inside)

(y x))(list x y))

()(expt 2 3)(cos 3)

Tested in C-Lisp

Page 10: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Modern C-Lisp IDELispWorks -Download

Graphical Debugger

Interactive

Windows

Mac

Linux

Sam Siewert 10

Page 11: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Everything is Really a List in Lisphttp://www.gigamonkeys.com/book/they-called-it-lisp-for-a-reason-list-processing.htmlLists always Have Parens around themTo get to an Atom, Use Car on List of ONE

Sam Siewert 11

Page 12: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

CSL Lisp REDUCEAlgebraic Simplification, Derivation, Etc.Simplifying Equations Compared to Numerical Solutions

Sam Siewert 12

Page 13: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp Applications and ActivityDS-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/

Sam Siewert 13

http://norvig.com/paip.html

Page 14: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Brief Lisp HistoryLisp 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-complete language for algorithms.

Sam Siewert 14

John McCarthy

Page 15: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Other Uses of LISP – “Latin Roots for Modern Multi-paradigm and Functional”

Automated Theorem ProvingGame Theory and AI GamesNatural Language ProcessingAutomated Planning and SchedulingOptimization SearchesSemantic Web and Search

Largely Being Replaced by Functional Features in Python, C++, C# and Newer Lisp Dialects such as Schemeand Racket as well as multi-paradigm Caml and Haskell, Swift

Sam Siewert 15

https://chessprogramming.wikispaces.com/Herbert+Simon

Page 16: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

What is Status of LispPython new/current AI teaching programming language

Python is often used for Intro to Programming with Java

C++, Java, Swift, Python, C#, etc. are used for production AI products

Lisp is a great way to learn functional programming based on simplicity, long history, and pure functional subset Sam Siewert 16

Pascal

Smalltalk

C#

Eiffel

C++

Lisp

Scheme

Common Lisp

CLOS

ML

Caml

OCaml

F#

Miranda

Haskell

Python

Ruby

Ada

Page 17: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

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

Page 18: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

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

Sam Siewert 18

Lambda expressions

Two lists …

Page 19: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Note on ANSI Common LISPSteel Bank Common LISP is ANSI StrictRequires Definition Prior to Elaboration (ANSI Addition)Many LISP Interpreters Simply Create the Definition Automatically (E.g. CLISP)

Sam Siewert 19

CLISP Has No WarningResult is the SAME, just WARNING

Page 20: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Digging Deeper …Typically Lisp is Taught in Introduction to AI (Symbolic)Python often used instead of Lisp Todayhttp://people.csail.mit.edu/phw/Books/#LispCLISP -https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/clm.html

Sam Siewert 20

Often a First Year Graduateor Senior Year UndergraduateComputer Science Class

Patrick H. Winston - Books

Page 21: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp Predicates and Functions

Sam Siewert 21

; 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)

Page 22: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp RecursionDesigned for Recursion, Nesting and Mapping Functions to Data of Most any Type

Sam Siewert 22

; 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)

((atom list) 1)(t (+ (count-atoms (first list))

(count-atoms (rest list))))))(count-atoms '(sqrt (expt x 2) (expt y 2)))

Call

Page 23: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp Introduction - Functional

Sam Siewert 23

>(defun fib (N)"Compute the N'th Fibonacci number."(if (or (zerop N) (= N 1))

1(let

((F1 (fib (- N 1)))(F2 (fib (- N 2))))

(+ F1 F2))))

FIB

>(trace fib)

(FIB)

Simplify with let expressions

Note: use trace for debug

>(fib 3)

1> (FIB 3)2> (FIB 2)3> (FIB 1)<3 (FIB 1)3> (FIB 0)<3 (FIB 1)

<2 (FIB 2)2> (FIB 1)<2 (FIB 1)

<1 (FIB 3)3

Learn by Example from Many Excellent Tutorials– Here, here, here

Try Examples for Scheme in PLP and here, here, http://racket-lang.org/

Page 24: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lambda CalculusTwo Major Theoretical Models of Computability (1930’s)

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

– Alonzo Church’s Hypothesis and Lambda CalculusAny 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

Page 25: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Functional PL FeaturesPLP Page 507 – Lisp vs. Scheme1. First-class function values and higher order functions2. Polymorphism (dynamic bindings) – Scheme Deep Binding3. List types and operators4. Structured functions returns (compared to single value)5. Constructors for aggregates6. Garbage collection (automatic memory management)

Sam Siewert 25

>((lambda (x) (* x x)) 3)

9

>(sqrt 25)

5.0

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

Common Lisp Scheme Only (Why not Common Lisp?)

Page 26: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Common Lisp vs. Scheme

Sam Siewert 26

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

Scheme Only (see note on p. 511, “A word of caution…”)

>(defun square (x) (* x x))SQUARE>(defun plus (a b) (+ a b))PLUS>(let ((a 3)

(b 4)(square (lambda (x) (* x x)))(plus +))

(sqrt (plus (square a) (square b))))

5.0

Note: square and plus ok in let, but not used

Note: namespace square and plus

Common Lisp (But, this is different, so how do we do the same?)

Page 27: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Scheme Tools – SCMSCM on Linux is Simple Command Line

ssiewert@ssiewert-VirtualBox:~/a331/lisp$ scmSCM version 5e5, Copyright (C) 1990-2006 Free Software Foundation.;loading /usr/share/slib/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#<unspecified>> (let ((a 3)> (b 4)> (square (lambda (x) (* x x)))> (plus +))> (sqrt (plus (square a) (square b))))5.0>

Sam Siewert 27

Page 28: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Build your Own Dialect of Functional Programming

Nice GUI and IDE for Scheme Development and Testhttp://racket-lang.org/download/ (Build your own interpreted PL) – Scheme option (determine language from source)

Sam Siewert 28

Page 29: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

GNU Schemehttps://www.gnu.org/software/mit-scheme/Windows, OS-X, Unix (Linux)

Sam Siewert 29

Page 30: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp Tools - LinuxCLISP with Emacs Editor or Simple Shell Interpreter

Sam Siewert 30

Page 31: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp Equivalent Let* with funcall

Sam Siewert 31

;(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 Easy1. 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

Page 32: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp IDE and EditorsMany Options …

GNU Common Lisp for Windows (per lab instructions)CLISP

– sudo apt-get install clisp– sudo apt-get install clisp-doc– sudo apt-get install slime– 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

ftp://ftp.cs.utexas.edu/pub/novak/gcl/gcl.exe

Page 33: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Practical ConsiderationsUse (quit) to exit the interpreterUse (load “pathandfilename”) to load a file with Lisp expressions in it

Sam Siewert 33

Page 34: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Lisp and Scheme on Linux - SummaryPLP 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

Page 35: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Declarative Programming Language Concepts

Program Axioms – Runtime derives Constructive Proof for Inputs (Query)Based on Logic – Rules and FactsQuery Rule and Fact Base with a PredicateForward Chaining – Derives conclusions from assertionsBackward Chaining – Find Assertions that Support a HypothesisThe Cut! – Controls Backtracking

Semantic WebAutomated Theorem ProvingScience and Medicine – Hypothesis, Facts, Rules

Sam Siewert 35

Page 36: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Prolog on PRClab and LinuxLike 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

Page 37: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Prolog Introduction - Declarative

Sam Siewert 37

%% Demo coming from%% http://www.math.unipd.it/~sperduti/AI11/likes.pl

likes(sam,Food) :-indian(Food),mild(Food).

likes(sam,Food) :-chinese(Food).

likes(sam,Food) :-italian(Food).

likes(sam,chips).

indian(curry).indian(dahl).indian(tandoori).indian(kurma).mild(dahl).mild(tandoori).mild(kurma).

chinese(chow_mein).chinese(chop_suey).chinese(sweet_and_sour).

italian(pizza).italian(spaghetti).

RULES FACTS

Page 38: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Prolog Interactive SessionLoad a file?- [likes].% likes compiled 0.01 sec, 4,792 bytestrue.

Simple Forward Chaining?- likes(sam,X).X = dahl .

List all rules for Food Sam Likes?- listing(likes).likes(sam, A) :-

indian(A),mild(A).

likes(sam, A) :-chinese(A).

likes(sam, A) :-italian(A).

likes(sam, chips).true.

Sam Siewert 38

Hypothesis?- likes(sam,pizza).true .

?- likes(sam,dahl).true .

?- likes(sam,cauliflower).false.

?- likes(sam,curry).false.

?- listing(indian).indian(curry).indian(dahl).indian(tandoori).indian(kurma).true.

Page 39: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

Prolog Interactive SessionTurn on tracing

?- trace.[trace] ?-

Simple Forward Chaining[trace] ?- likes(sam,X).

Call: (6) likes(sam, _G387) ? creepCall: (7) indian(_G387) ? creepExit: (7) indian(curry) ? creepCall: (7) mild(curry) ? creepFail: (7) mild(curry) ? creepRedo: (7) indian(_G387) ? creepExit: (7) indian(dahl) ? creepCall: (7) mild(dahl) ? creepExit: (7) mild(dahl) ? creepExit: (6) likes(sam, dahl) ? creep

X = dahl .

Sam Siewert 39

Predicate & Hypothesis[trace] ?- likes(sam,dahl).

Call: (6) likes(sam, dahl) ? creepCall: (7) indian(dahl) ? creepExit: (7) indian(dahl) ? creepCall: (7) mild(dahl) ? creepExit: (7) mild(dahl) ? creepExit: (6) likes(sam, dahl) ? creep

true

[trace] ?- likes(sam,beet).Call: (6) likes(sam, beet) ? creepCall: (7) indian(beet) ? creepFail: (7) indian(beet) ? creepRedo: (6) likes(sam, beet) ? creepCall: (7) chinese(beet) ? creepFail: (7) chinese(beet) ? creepRedo: (6) likes(sam, beet) ? creepCall: (7) italian(beet) ? creepFail: (7) italian(beet) ? creepRedo: (6) likes(sam, beet) ? creepFail: (6) likes(sam, beet) ? creep

false.

Page 40: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

More Install Notes – Functional & Logic PLsSWI-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

Page 41: CS 332 Programming Language Conceptsmercury.pr.erau.edu/~siewerts/cs332/documents/... · Using Scheme on PRClab or PC LISP Interpreter (E.g. GNU Common LISP, Steel Bank) C-Lisp Most

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