INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. Common Lisp Fundamentals

Similar documents
INF4820: Algorithms for Artificial Intelligence and Natural Language Processing. More Common Lisp

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Common LISP Tutorial 1 (Basic)

Imperative, OO and Functional Languages A C program is

Artificial Intelligence Programming

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89

Functional Programming. Pure Functional Programming

Gene Kim 9/9/2016 CSC 2/444 Lisp Tutorial

A little bit of Lisp

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define

Functional Programming. Pure Functional Languages

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE,

CSCI337 Organisation of Programming Languages LISP

Functional Programming. Big Picture. Design of Programming Languages

CS 314 Principles of Programming Languages

Functional programming with Common Lisp

Functional Programming. Pure Functional Languages

CS 480. Lisp J. Kosecka George Mason University. Lisp Slides

Symbolic Computation and Common Lisp

SCHEME AND CALCULATOR 5b

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015

Allegro CL Certification Program

Principles of Programming Languages Topic: Functional Programming Professor L. Thorne McCarty Spring 2003

Robot Programming with Lisp

Announcement. Overview. LISP: A Quick Overview. Outline of Writing and Running Lisp.

INF4820. Common Lisp: Closures and Macros

Lisp. Versions of LISP

Allegro CL Certification Program

Department of Computer and information Science Norwegian University of Science and Technology

Functional Programming

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7

A Small Interpreted Language

Lecture Notes on Lisp A Brief Introduction

Chapter 1. Fundamentals of Higher Order Programming

Algorithms for AI and NLP (INF4820 Lisp & FSAs)

15 Unification and Embedded Languages in Lisp

FP Foundations, Scheme

Introduction to Lisp

Look at the outermost list first, evaluate each of its arguments, and use the results as arguments to the outermost operator.

Common Lisp. Blake McBride

Organization of Programming Languages CS3200/5200N. Lecture 11

A Brief Introduction to Scheme (I)

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives

LISP. Everything in a computer is a string of binary digits, ones and zeros, which everyone calls bits.

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky.

Robot Programming with Lisp

Documentation for LISP in BASIC

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University

Lecture #2 Kenneth W. Flynn RPI CS

CSCC24 Functional Programming Scheme Part 2

Scheme: Expressions & Procedures

Section 10: LISP to Scheme. Evolution of Software Languages

Debugging in LISP. trace causes a trace to be printed for a function when it is called

SCHEME The Scheme Interpreter. 2 Primitives COMPUTER SCIENCE 61A. October 29th, 2012

Intro. Scheme Basics. scm> 5 5. scm>

Reasoning About Programs Panagiotis Manolios

Common LISP-Introduction

Lambda Calculus. Gunnar Gotshalks LC-1

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Functional Programming Lecture 1: Introduction

Racket. CSE341: Programming Languages Lecture 14 Introduction to Racket. Getting started. Racket vs. Scheme. Example.

TAIL RECURSION, SCOPE, AND PROJECT 4 11

LECTURE 16. Functional Programming

Introduction 2 Lisp Part I

Ruby: Introduction, Basics

CS A331 Programming Language Concepts

Introduction to Scheme

University of Maine School of Computing and Information Science COS 470/570

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 3. LISP: ZÁKLADNÍ FUNKCE, POUŽÍVÁNÍ REKURZE,

Lisp Basic Example Test Questions

6.037 Lecture 4. Interpretation. What is an interpreter? Why do we need an interpreter? Stages of an interpreter. Role of each part of the interpreter

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives

Lambda Calculus LC-1

CIS4/681 { Articial Intelligence 2 > (insert-sort '( )) ( ) 2 More Complicated Recursion So far everything we have dened requires

Functional Programming Languages (FPL)

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003.

Lecture #24: Programming Languages and Programs

Introduction to lambda calculus Part 3

User-defined Functions. Conditional Expressions in Scheme

CPS 506 Comparative Programming Languages. Programming Language Paradigm

Functional programming in LISP

Introduction to ACL2. CS 680 Formal Methods for Computer Verification. Jeremy Johnson Drexel University

Chapter 15. Functional Programming Languages

COP4020 Programming Assignment 1 - Spring 2011

Functional Programming with Common Lisp

Introduction to Functional Programming and basic Lisp

NST: A Unit Test Framework for Common Lisp

Functional Languages. Hwansoo Han

A Brief Introduction to Common Lisp

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am.

An Introduction to Scheme

CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College. Scheme

Transcription:

INF4820: Algorithms for Artificial Intelligence and Natural Language Processing Common Lisp Fundamentals Stephan Oepen & Murhaf Fares Language Technology Group (LTG) August 30, 2017

Last Week: What is AI? 2 Since the 1950s: Chatbots, theorem proving, blocks world, expert and dialogue systems, game playing,... Moving target: Whatever requires intelligent decisions, but seems out of reach, technologically, at the time? Recently: Conversational user interfaces, self-driving cars, talking robots, AlphaGo. But also (fuzzily) business intelligence, (big) data analytics,... Toolkit of ( clever ) methods for representation and problem solving.

Topic of the Day 3 Lisp

Why Common Lisp? 4 Eric S. Raymond (2001), How to Become a Hacker: Lisp is worth learning for the profound enlightenment experience you will have when you finally get it; that experience will make you a better programmer for the rest of your days, even if you never actually use Lisp itself a lot. High-level and efficient language with especially strong support for symbolic and functional programming. Rich language: multitude of built-in data types and operations. Easy to learn: trivial syntax and straightforward semantics. Incremental and interactive development. ANSI-standardized and stable. Several very strong compilers available.

Lisp 5 Conceived in the late 1950s by John McCarthy one of the founding fathers of AI. Originally intended as a mathematical formalism. A family of high-level languages. Several dialects, e.g. Scheme, Clojure, Emacs Lisp, and Common Lisp. Although a multi-paradigm language, functional style prevalent.

Basic Common Lisp in a Couple of Minutes Testing a few expressions at the REPL; the read eval print loop. (= the interactive Lisp-environment) Examples? "this is a string" "this is a string"? 42 42? represents the REPL prompt and what an expression evaluates to. Atomic data types like numbers, booleans, and strings are self evaluating. Symbols evaluate to whatever value they are bound to.? t t? nil nil? pi 3.141592653589793d0? foo error; unbound 6

A Note on Terminology 7 Lisp manipulates so-called symbolic expressions. AKA s-expressions or sexps. Two fundamental types of sexps; 1. atoms (e.g., nil, t, numbers, strings, symbols) 2. lists containing other sexps. Sexps are used to represent both data and code.

Function Calls 8 Parenthesized prefix notation First element (prefix) = operator (i.e. the procedure or function). The rest of the list is the operands (i.e. the arguments or parameters). Use nesting (of lists) to build compound expressions. Expressions can span multiple lines; indentation for readability. Examples? (+ 1 2) 3? (+ 1 2 10 7 5) 25? (/ (+ 10 20) 2) 15? (* (+ 42 58) (- (/ 8 2) 2)) 200

The Syntax and Semantics of CL 9? (expt (- 8 4) 2) 16 You now know (almost) all there is to know about (the rules of) CL. The first element of a list names a function that is invoked with the values of all remaining elements as its arguments. A few exceptions, called special forms, with their own evaluation rules.

Creating our own functions 10 The special form defun associates a function definition with a symbol: General form (defun name (parameter 1... parameter n ) body) Example? (defun average (x y) (/ (+ x y) 2))? (average 10 20) 15

The Hello World! of Functional Programming 11 Classic example: the factorial function. A recursive procedure; calls itself, directly or indirectly. May seem circular, but is well-defined as long as there s a base case terminating the recursion. For comparison: a non-recursive implementation (in Python). n! = { 1 if n = 0 n (n 1)! if n > 0 (defun! (n) (if (= n 0) 1 (* n (! (- n 1))))) def fac(n): r = 1 while (n > 0): r = r * n n = n - 1 return r

12 A Special Case of Recursion: Tail Recursion A more efficient way to define n! recursively. Use a helper procedure with an accumulator variable to collect the product along the way. The recursive call is in tail position; (defun! (n) (!-aux 1 1 n)) (defun!-aux (r i n) (if (> i n) r (!-aux (* i r) (+ i 1) n))) no work remains to be done in the calling function. Once we reach the base case, the return value is ready. Most CL compilers do tail call optimization (TCO), so that the recursion is executed as an iterative loop. (The next lecture will cover CL s built-in loop construct.)

Tracing the processes 13 Recursive (defun! (n) (if (= n 0) 1 (* n (! (- n 1)))))? (! 7) (* 7 (! 6)) (* 7 (* 6 (! 5))) (* 7 (* 6 (* 5 (! 4)))) (* 7 (* 6 (* 5 (* 4 (! 3))))) (* 7 (* 6 (* 5 (* 4 (* 3 (! 2)))))) (* 7 (* 6 (* 5 (* 4 (* 3 (* 2 (! 1))))))) (* 7 (* 6 (* 5 (* 4 (* 3 (* 2 1)))))) (* 7 (* 6 (* 5 (* 4 (* 3 2))))) (* 7 (* 6 (* 5 (* 4 6)))) (* 7 (* 6 (* 5 24))) (* 7 (* 6 120)) (* 7 720) 5040 Tail-Recursive (defun! (n) (!-aux 1 1 n)) (defun!-aux (r i n) (if (> i n) r (!-aux (* r i) (+ i 1) n)))? (! 7) (!-aux 1 1 7) (!-aux 1 2 7) (!-aux 2 3 7) (!-aux 6 4 7) (!-aux 24 5 7) (!-aux 120 6 7) (!-aux 720 7 7) (!-aux 5040 8 7) 5040

The quote Operator A special form making expressions self-evaluating. The quote operator (or simply ) suppresses evaluation.? pi 3.141592653589793d0? (quote pi) pi? pi pi? foobar error; unbound variable? foobar foobar? (* 2 pi) 6.283185307179586d0? (* 2 pi) (* 2 pi)? () error; missing procedure? () () 14

Both Code and Data are S-Expressions 15 We ve mentioned how sexps are used to represent both data and code. Note the double role of lists: Lists are function calls:? (* 10 (+ 2 3)) 50? (bar 1 2) error; function bar undefined But, lists can also be data:? (foo bar) (foo bar)? (list foo bar) (foo bar)

LISP = LISt Processing 16 cons builds up new lists; first and rest destructure them.? (cons 1 (cons 2 (cons 3 nil))) (1 2 3)? (cons 0 (1 2 3)) (0 1 2 3)? (first (1 2 3)) 1? (rest (1 2 3)) (2 3)? (first (rest (1 2 3))) 2? (rest (rest (rest (1 2 3)))) nil Many additional list operations (derivable from the above), e.g.? (list 1 2 3) (1 2 3)? (append (1 2) (3) (4 5 6)) (1 2 3 4 5 6)? (length (1 2 3)) 3? (reverse (1 2 3)) (3 2 1)? (nth 2 (1 2 3)) 3? (last (1 2 3)) (3) Wait, why not 3?

Lists are Really Chained cons Cells 17 (1 2 3) ((1 2) 3) 1 2 1 3 nil 3 nil (cons 1 (cons 2 (cons 3 nil))) 2 nil (cons (cons 1 (cons 2 nil)) (cons 3 nil))

Assigning Values: Generalized Variables defparameter declares a global variable and assigns a value:? (defparameter *foo* 42) *FOO*? *foo* 42 setf provides a uniform way of assigning values to variables. General form: (setf place value)... where place can either be a variable named by a symbol or some other storage location:? (setf *foo* (+ *foo* 1))? *foo* 43? (setf *foo* (2 2 3))? (setf (first *foo*) 1)? *foo* (1 2 3) 18

Some Other Macros for Assignment 19 Example Type of x Effect (incf x y) number (setf x (+ x y)) (incf x) number (incf x 1) (decf x y) number (setf x (- x y)) (decf x) number (decf x 1) (push y x) list (setf x (cons y x)) (pop x) list (let ((y (first x))) (setf x (rest x)) y) (pushnew y x) list (if (member y x) x (push y x)) Shall we write our own push and pop? Just a second!

Local Variables 20 Sometimes we want to store intermediate results. let and let* create temporary value bindings for symbols.? (defparameter *foo* 42) *FOO*? (defparameter *bar* 100) *BAR*? (let ((*bar* 7) (baz 1)) (+ baz *bar* *foo*)) 50? *bar* 100? baz error; unbound variable Bindings valid only in the body of let. Previously existing bindings are shadowed within the lexical scope. let* is like let but binds sequentially.

Predicates 21 A predicate tests some condition. Evaluates to a boolean truth value: nil (the empty list) means false. Anything non-nil (including t) means true.? (listp (1 2 3)) t? (null (rest (1 2 3))) nil? (evenp 2) t? (defparameter foo 42)? (or (not (numberp foo)) (and (>= foo 0) (<= foo 42))) t Plethora of equality tests: eq, eql, equal, and equalp.

Equality for One and All 22 eq tests object identity; not applicable for numbers or characters. eql is like eq, but well-defined on numbers and characters. equal tests structural equivalence (recursively for lists and strings). equalp is like equal but insensitive to case and numeric type.? (eq (list 1 2 3) (1 2 3)) nil? (equal (list 1 2 3) (1 2 3)) t? (eq 42 42)? [implementation-dependent]? (eql 42 42) t? (eql 42 42.0) nil? (equalp 42 42.0) t? (equal "foo" "foo") t? (equalp "FOO" "foo") t Also many type-specialized tests like =, string=, etc.

In Conclusion 23 http://xkcd.com/297/

Programming in INF4820 24 In the IFI Linux environment, we have available Allegro Common Lisp, a commercial Lisp interpreter and compiler. We provide a pre-configured, integrated setup with emacs and the SLIME Lisp interaction mode. Several open-source Lisp implementation exist, e.g. Clozure or SBCL, compatible with SLIME, so feel free to experiment (at some later point). First-time users, please spend some time studying basic keyboard commands, for example: C-h t and M-x doctor RET. We have posted a Getting Started guide and Emacs Cheat Sheet on the course web pages.

Next Week 25 More Common Lisp. Higher-order functions. More on argument lists (optional arguments, keywords, defaults). More data types: Hash-tables, a-lists, arrays, sequences, and structures Iteration (loop) and mapping.