34
Lisp Tijs van der Storm Thursday, September 6, 12

Learn a language : LISP

Embed Size (px)

DESCRIPTION

Presentation introducing LISP, looking at the history and concepts behind this powerfull programming language. Presentation by Tijs van der Storm for the sept 2012 Devnology meetup at the Mirabeau offices in Amsterdam

Citation preview

Page 1: Learn a language : LISP

LispTijs van der Storm

Thursday, September 6, 12

Page 2: Learn a language : LISP

About me...• Work at Centrum Wiskunde & Informatica

• Teach at Universiteit van Amsterdam in the Master Software Engineering

• According to @jvandenbos “typical esoteric programming language dude” :)

• Contact: [email protected], @tvdstorm

Thursday, September 6, 12

Page 3: Learn a language : LISP

Interests and projects

• DSLs, MDD, programming, languages

• Co-designer of the Rascal metaprogramming language

• Co-designer of the Ensō model-based programming environment

Thursday, September 6, 12

Page 4: Learn a language : LISP

Atze van der Ploeg

Thursday, September 6, 12

Page 5: Learn a language : LISP

Today

• About Lisp

• Programming Clojure

• ... meta-programming

Thursday, September 6, 12

Page 6: Learn a language : LISP

http://lambda.bugyo.tk/cdr/mwl/

Thursday, September 6, 12

Page 7: Learn a language : LISP

What is Lisp?

• A programming language?

• For LIst Processing?

• The most intelligent way to misuse a computer?

• Lots of Irritating Superfluous Parentheses?

• Secret alien technology?

• Oatmeal with fingernail clippings mixed in?

• A programmer amplifier?

http://lisperati.com/

Thursday, September 6, 12

Page 8: Learn a language : LISP

What is Lisp?

• A PL for building organisms (Perlis)

• Building material (Kay)

• Opposite of a Blub language (Graham)

• Maxwell’s equations of software (Kay)

• The greatest language ever invented (Kay)

Thursday, September 6, 12

Page 9: Learn a language : LISP

John McCarthy

(September 4, 1927 – October 24, 2011)

Thursday, September 6, 12

Page 10: Learn a language : LISP

http://dx.doi.org/10.1145/367177.367199

Recursive Functions of Symbolic Expressions Their Computation by Machine, Part I

and

JOHX MCCAaTItY, Massachusetts Institute of Technology, Cambridge, Mass.

1 . I n t r o d u c t i o n A programming system called LISP (for lASt Processor)

has been developed for the IBM 704 computer by the Artificial Intelligence group at M.I.T. The system was designed to facilitate experiments with a proposed system called the Advice Taker, whereby a machine could be instructed to handle declarative as well as imperative sentences and could exhibit "common sense" in carrying out its instructions. The original proposal It] for the Advice Taker was made in November 1958. The main require- ment was a programming system for manipulating ex- pressions representing formalized declarative and irnpera- live sentences so that the Advice Taker system could make deductions.

In the course of its development the Lisp system went through several stages of simplification and eventually came to be based on a scheme for representing the partial recursive functions of a certain class of symbolic expres- sions. This representation is independent of the IBM 704 computer, or of any other electronic computer, and it now seems expedient to expound the system by starting with the class of expressions called S-expressions and the func- tions called S-functions.

In this article, we first describe a formalism for defining functions reeursively. We believe this formalism has ad- vantages both as a programming language and as vehicle for developing a theory of computation. Next, we describe S-expressions and S-functions, give some examples, and then describe the universM S-function apply which plays the theoretical role of a universal Turing machine and the practical role of an interpreter. Then we describe the representation of S-expressions in the memmT of the IBM 704 by list structures similar to those used by Newell, Shaw and Simon [2], and the representation of S-functions by program. Then we mention the main features of the Lisp programming system for the IBM 704. Next comes another way of describing computations with symbolic expressions, and finally we give a recursive function in- terpretation of flow charts.

We hope to describe some of the sylnbolie computations for which LISP has been used in another paper, and also to give elsewhere some applications of our reeursive function formalism to mathematical logic and to the problem of mechanical theorem proving.

184 C o m m u n i c a t i o n s o f t h e ACM

2. F u n c t i o n s an d F u n c t i o n Def in i t ions We shMl need a number of mathematical ideas ar:d

notations concerning functions in general. Most of the ideas are well known, but the notion of conditional e,~pre~'- sion is believed to be new, and ihe use of conditional expressions permits functions to be defined recursively in a new and convenient way.

a. Partial Functions. A partial function is a funct on that is defined only on part of its domain. Partial funetio:~s necessarily arise when functions are defined by eomputa~ tions because for some values of the arguments t:he Pomp:> ration defining the value of the function may not ter- minate. However, some of our elementary functions wilt be defined as partial functions.

b. Propositional Expres.s'ions and Predicates. A t)ropo~i- tionM expression is an expression whose possible values are T (for truth) and F (for falsity). We shall assume that the reader is fanfiliar with the propositionM eom~ee- lives A ("and"), V ("or" ) , and ~ ("not") , Typieai propositional expressions are:

x < y

(x < y) A (b = e)

x is prime A predicate is a function whose range consists of ih{: t ruth values T and F.

e. Conditional Expressions. The dependence of truth values on the vahtes of quantities of other kinds is ex- pressed in mathematics by predicates, and the depende~ee of t ruth values on other t ruth values by logical comxee- ~ives. However, the notations for expressing symbol (alE" the dependence of quantities of other kinds on trutt~ vMues is inadequate, so that English words and phrases are generMly used for expressing these depende~tces i:~ texts that, describe other dependences symbolically. I!'<~r example, the function Ix I is ustmlly defined in words.

Conditional expressions are a deviee for expressing the dependence of quantities on propositional quantities. :\ conditional expression has the form

( p : - + e l , - . - , p ~ --+ e , , )

where the p's are propositionM expressions and the e's are expressions of any kind. I t may be read, "If p~ thexx <,

(

Communications of the ACM, vol 3, issue 4, April 1960

Thursday, September 6, 12

Page 11: Learn a language : LISP

Last 17th of August: 50 years

ago (!)

http://www.softwarepreservation.org/projects/LISP/book/LISP%201.5%20Programmers%20Manual.pdf

Thursday, September 6, 12

Page 12: Learn a language : LISP

The famous page 13

http://xkcd.com/917/

Thursday, September 6, 12

Page 13: Learn a language : LISP

Guy L. Steele, Richard P. Gabriel, “The evolution of Lisp”, in: History of programming languages II, ACM 1996, p. 311

Thursday, September 6, 12

Page 14: Learn a language : LISP

Revised5 Report on the Algorithmic LanguageScheme

RICHARD KELSEY, WILLIAM CLINGER, AND JONATHAN REES (Editors)H. ABELSON R. K. DYBVIG C. T. HAYNES G. J. ROZAS

N. I. ADAMS IV D. P. FRIEDMAN E. KOHLBECKER G. L. STEELE JR.D. H. BARTLEY R. HALSTEAD D. OXLEY G. J. SUSSMAN

G. BROOKS C. HANSON K. M. PITMAN M. WAND

Dedicated to the Memory of Robert Hieb

20 February 1998

SUMMARY

The report gives a defining description of the program-ming language Scheme. Scheme is a statically scoped andproperly tail-recursive dialect of the Lisp programminglanguage invented by Guy Lewis Steele Jr. and GeraldJay Sussman. It was designed to have an exceptionallyclear and simple semantics and few di↵erent ways to formexpressions. A wide variety of programming paradigms, in-cluding imperative, functional, and message passing styles,find convenient expression in Scheme.

The introduction o↵ers a brief history of the language andof the report.

The first three chapters present the fundamental ideas ofthe language and describe the notational conventions usedfor describing the language and for writing programs in thelanguage.

Chapters 4 and 5 describe the syntax and semantics ofexpressions, programs, and definitions.

Chapter 6 describes Scheme’s built-in procedures, whichinclude all of the language’s data manipulation and in-put/output primitives.

Chapter 7 provides a formal syntax for Scheme written inextended BNF, along with a formal denotational semantics.An example of the use of the language follows the formalsyntax and semantics.

The report concludes with a list of references and an al-phabetic index.

CONTENTSIntroduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1 Overview of Scheme . . . . . . . . . . . . . . . . . . . . . . . 3

1.1 Semantics . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.2 Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . 3

1.3 Notation and terminology . . . . . . . . . . . . . . . . 3

2 Lexical conventions . . . . . . . . . . . . . . . . . . . . . . . 5

2.1 Identifiers . . . . . . . . . . . . . . . . . . . . . . . . . 5

2.2 Whitespace and comments . . . . . . . . . . . . . . . . 5

2.3 Other notations . . . . . . . . . . . . . . . . . . . . . . 5

3 Basic concepts . . . . . . . . . . . . . . . . . . . . . . . . . . 6

3.1 Variables, syntactic keywords, and regions . . . . . . . 6

3.2 Disjointness of types . . . . . . . . . . . . . . . . . . . 6

3.3 External representations . . . . . . . . . . . . . . . . . 6

3.4 Storage model . . . . . . . . . . . . . . . . . . . . . . . 7

3.5 Proper tail recursion . . . . . . . . . . . . . . . . . . . 7

4 Expressions . . . . . . . . . . . . . . . . . . . . . . . . . . . 8

4.1 Primitive expression types . . . . . . . . . . . . . . . . 8

4.2 Derived expression types . . . . . . . . . . . . . . . . . 10

4.3 Macros . . . . . . . . . . . . . . . . . . . . . . . . . . . 13

5 Program structure . . . . . . . . . . . . . . . . . . . . . . . . 16

5.1 Programs . . . . . . . . . . . . . . . . . . . . . . . . . 16

5.2 Definitions . . . . . . . . . . . . . . . . . . . . . . . . . 16

5.3 Syntax definitions . . . . . . . . . . . . . . . . . . . . 17

6 Standard procedures . . . . . . . . . . . . . . . . . . . . . . 17

6.1 Equivalence predicates . . . . . . . . . . . . . . . . . . 17

6.2 Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . 19

6.3 Other data types . . . . . . . . . . . . . . . . . . . . . 25

6.4 Control features . . . . . . . . . . . . . . . . . . . . . . 31

6.5 Eval . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

6.6 Input and output . . . . . . . . . . . . . . . . . . . . . 35

7 Formal syntax and semantics . . . . . . . . . . . . . . . . . . 38

7.1 Formal syntax . . . . . . . . . . . . . . . . . . . . . . . 38

7.2 Formal semantics . . . . . . . . . . . . . . . . . . . . . 40

7.3 Derived expression types . . . . . . . . . . . . . . . . . 43

Notes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

Additional material . . . . . . . . . . . . . . . . . . . . . . . . 45

Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

Alphabetic index of definitions of concepts,keywords, and procedures . . . . . . . . . . . . . . . . 48

50 pages: pure, small, “academic”

Thursday, September 6, 12

Page 15: Learn a language : LISP

1029 pages: comprehensive, practical,

messy, “industrial”

Thursday, September 6, 12

Page 16: Learn a language : LISP

What made Lisp different?

http://paulgraham.com/diff.html

Thursday, September 6, 12

Page 17: Learn a language : LISP

How is Lisp still different?

• Homoiconic syntax

• aka: there is no syntax

• Macros

• aka: compile-time code transformers

• Code is data, data can be code

• Program into the language

Thursday, September 6, 12

Page 18: Learn a language : LISP

http://xkcd.com/224/

Thursday, September 6, 12

Page 19: Learn a language : LISP

Thursday, September 6, 12

Page 20: Learn a language : LISP

Fun resources

Thursday, September 6, 12

Page 21: Learn a language : LISP

http://xkcd.com/297/

Thursday, September 6, 12

Page 22: Learn a language : LISP

Rich Hickey

Thursday, September 6, 12

Page 23: Learn a language : LISP

• Lisp syntax, macros, code as data etc.

• Functional programming, immutable data

• Data structures: map, set, vector, list

• Concurrency: transactional memory

• Compiles to JVM, intergrates with Java

• [and much more]

Thursday, September 6, 12

Page 24: Learn a language : LISP

(def basic-data-types '{:booleans [true, false] :numbers [1, 2, 3.0, 4/5] :strings ["this is a string"] :symbols [a, empty?, +, user/foo] :keywords [:a-key-word]})

NB: commas, are whitespace (!)

Thursday, September 6, 12

Page 25: Learn a language : LISP

(def collection-types '{:vectors [1,2,3,4] :maps {:x 3, :y 4} :sets #{a set of symbols} :lists (a list of symbols)})

Thursday, September 6, 12

Page 26: Learn a language : LISP

• Expressed using lists (Polish notation):

• Head is applied to the arguments in tail:

(operator arg1 arg2 ...)

(+ 1 2)

Thursday, September 6, 12

Page 27: Learn a language : LISP

Special forms(def x 3)

(if (> x 1) 'then 'else)

(do (print "hello") (print "world!"))

(let [x 1] (+ x 1))

(quote (this returns a list with seven symbols))'(this returns a list with seven symbols)

(fn [x n] (+ x n))

define

conditional

sequencing

local vars

quotation

closures

Thursday, September 6, 12

Page 28: Learn a language : LISP

Convenience macros(defn power [x n] (if (= n 0) 1 (* x (power x (- n 1)))))

define a function

(defmacro unless [cond then else] `(if (not ~cond) ~then ~else))

define a macro

Thursday, September 6, 12

Page 29: Learn a language : LISP

Macros!

• Functions that transform code trees

• aka: code that writes code

(defmacro unless [cond then else] `(if (not ~cond) ~then ~else))

quasi quote ` unquote ~

template

Thursday, September 6, 12

Page 30: Learn a language : LISP

Trying it in the REPL=> (defmacro unless [cond then else] `(if (not ~cond) ~then ~else))#'user/unless

=> (unless (> 2 3) 'yes 'no)yes

=> (macroexpand '(unless (> 2 3) 'yes 'no))(if (clojure.core/not (> 2 3)) (quote yes) (quote no))

=> (macroexpand '(unless (> 2 3) (+ 1 2) (* 2 3)))(if (clojure.core/not (> 2 3)) (+ 1 2) (* 2 3))

Thursday, September 6, 12

Page 31: Learn a language : LISP

Cascading conditionals

(defmacro cond' [case & cases] (if (empty? cases) `(when ~(first case) ~(second case)) `(if ~(first case) ~(second case) (cond' ~(first cases) ~@(rest cases)))))

rest params

splicing unquote ~@

macro recursion

(cond' [(> x y) 1] [(< x y) -1] [(= x y) 0]))

Thursday, September 6, 12

Page 32: Learn a language : LISP

Testing it out=> ((fn [x y] (cond' [(> x y) 1] [(< x y) -1] [(= x y) 0])) 1 2)-1

=> (macroexpand-all '(cond' [(> x y) 1] [(< x y) -1] [(= x y) 0]))(if (> x y) 1 (if (< x y) -1 (if (= x y) (do 0))))

Thursday, September 6, 12

Page 33: Learn a language : LISP

Why is this cool?

• Extend the language with new abstractions

• control-flow

• state machine

• GUI builders

• grammars, ... etc.

• Reuse Lisp syntax / compile with macros

Thursday, September 6, 12

Page 34: Learn a language : LISP

http://www.cwi.nl/~storm/devclj.html

s

Thursday, September 6, 12