1 Course progression Parallels between languages, including –sequencing, selection and repetition –abstraction mechanisms behavioral abstraction (e.g.

Slides:



Advertisements
Similar presentations
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Advertisements

1 Programming Languages (CS 550) Mini Language Interpreter Jeremy R. Johnson.
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Scheme in Scheme. Why implement Scheme in Scheme  Implementing a language is a good way to learn more about programming languages  Interpreters are.
CSE 3341/655; Part 4 55 A functional program: Collection of functions A function just computes and returns a value No side-effects In fact: No program.
1 Programming Languages (CS 550) Lecture Summary Functional Programming and Operational Semantics for Scheme Jeremy R. Johnson.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Chapter 3 Functional Programming. Outline Introduction to functional programming Scheme: an untyped functional programming language.
CS 355 – PROGRAMMING LANGUAGES Dr. X. Apply-to-all A functional form that takes a single function as a parameter and yields a list of values obtained.
1-1 An Introduction to Scheme March Introduction A mid-1970s dialect of LISP, designed to be a cleaner, more modern, and simpler version than.
1 Functional programming Languages And a brief introduction to Lisp and Scheme.
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
Chapter 15 Functional Programming Languages. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Introduction Design of imperative languages is.
ISBN Chapter 15 Functional Programming Languages Mathematical Functions Fundamentals of Functional Programming Languages Introduction to.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
1 Functional languages (e.g. Scheme, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Structuring data So far we’ve seen two types of values that we can bind to names: –numbers –Functions Today we will see two more: –symbols –pairs.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
ISBN Chapter 15 Functional Programming Languages.
Cs7100(Prasad)L8Proc1 Procedures. cs7100(Prasad)L8Proc2 Primitive procedures  etc User-defined procedures –Naming a sequence of operations.
Functional Programming in Scheme and Lisp. Overview In a functional programming language, functions are first class objects. You can create them, put.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Patterns in OCaml functions. Formal vs. actual parameters Here's a function definition (in C): –int add (int x, int y) { return x + y; } –x and y are.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Functional Programming and Lisp. Overview In a functional programming language, functions are first class objects. In a functional programming language,
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Formal Semantics Chapter Twenty-ThreeModern Programming Languages, 2nd ed.1.
COP4020 Programming Languages Functional Programming Prof. Xin Yuan.
A Third Look At ML Chapter NineModern Programming Languages, 2nd ed.1.
ISBN Chapter 15 Functional Programming Languages.
Principles of Programming Languages Lecture 1 Slides by Yaron Gonen, based on slides by Daniel Deutch and lecture notes by Prof. Mira Balaban.
Milos Hauskrecht (PDF) Hieu D. Vu (PPT) LISP PROGARMMING LANGUAGE.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
1 Programming Languages (CS 550) Lecture 2 Summary Mini Language Interpreter Jeremy R. Johnson.
1 FP Foundations, Scheme In Text: Chapter Chapter 14: FP Foundations, Scheme Mathematical Functions Def: A mathematical function is a mapping of.
CSE 425: Functional Programming I Programs as Functions Some programs act like mathematical functions –Associate a set of input values from the function’s.
Ch Ch jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (n-n-n-notes) Summer 2003 Dr. Carter Tiernan.
Functional Programming
Functional Programming
History of Computing – Lisp
6.001 SICP Variations on a Scheme
6.001 SICP Object Oriented Programming
Java Primer 1: Types, Classes and Operators
Introduction to Scheme
6.001: Structure and Interpretation of Computer Programs
Original material by Eric Grimson
Mini Language Interpreter Programming Languages (CS 550)
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Functional Programming
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
CSE 341 Section 5 Winter 2018.
The Metacircular Evaluator (Continued)
6.001 SICP Further Variations on a Scheme
6.001 SICP Variations on a Scheme
Peter Seibel Practical Common Lisp Peter Seibel
6.001 SICP Interpretation Parts of an interpreter
topics interpreters meta-linguistic abstraction eval and apply
Lisp.
Presentation transcript:

1 Course progression Parallels between languages, including –sequencing, selection and repetition –abstraction mechanisms behavioral abstraction (e.g. functions) data abstraction (e.g. user-defined types) Differences between languages, including –variables –typing –extensibility of language (cf. libraries) Specific languages –Scheme, ML, Prolog, Pascal, C, Java, C#, Fortress, etc.

2 Functional languages (e.g. Scheme, Lisp, ML) Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined using the keyword lambda: (lambda ( ) ) Syntax of Scheme is very simple: primitive expressions numbers, such as 17 symbols, such as ’fred names, such as fred complex expressions function applications, such as (+ 3 2) or (addOne x), consist of a function and arguments for the function. special forms, such as (define x y), are evaluated in special ways. ML is a functional language. ML is based on lambda calculus. lambda abstraction = function definition In ML, a function is (typically) defined using the fun keyword: fun = Syntax of ML is familiar: block-structured syntax infix operators for +, *, etc function names appear before argument lists, not inside parentheses

3 Evaluation (Scheme and ML share model – focus on Scheme) Numbers are interpreted in base 10. Names are looked up in an environment, starting with the current environment, following static links, until the name is found. If it is not found, an error occurs. (define ) is evaluated by first evaluating, and binding, in the current environment, the name to that value. (lambda …) is evaluated by creating an appropriate closure (sometimes called a procedure). The closure’s environment link refers to the environment which was active when the closure was created. All members of an application are evaluated, after which the first is applied to the rest. A procedure application is evaluated by creating an environment to bind the parameters of the procedure to its arguments, and then evaluating the body relative to this new environment. The static link of the environment refers to the same environment as the closure, whereas the dynamic link refers to the environment that was active when the procedure was applied.

4 Functional languages on CSE UNIX systems (e.g. pollux) Installed in /projects/alphonce/bin –Scheme text-based interpreter: mzscheme graphical ide: drscheme –must add /util/gnu/lib to LD_LIBRARY_PATH –Erlang: erl Installed in /util/bin –Lisp (Allegro Common Lisp): standalone: acl from within Emacs: run-acl –ML Standard ML of NJ: sml

5 Examples SCHEME > > (define x 12) > x 12 > (define addOne (lambda (x) (+ x 1))) > (addOne x) 13 > (define add (lambda (x y) (+ x y))) > (add 3 x) 15 > (define adder (lambda (x) (lambda (y) (+ x y)))) > (define add3 (adder 3)) > (add3 x) 15 > (define add7 (adder 7)) > (add7 x) 19 ML - 12 val it=12:int - val x=12; val x=12:int - x val it=12:int - fun addOne x = x+1; val addOne=fn:int->int - addOne x val it=13:int - fun add (x,y) = x+y; val add=fn:int*int->int; - add(3,x); val it=15:int - fun adder x y = x+y; val adder=fn:int->int->int - val add3 = adder 3; val add3=fn:int->int - add3 x; val it=15:int; - val add7 = adder 7; val add7=fn:int->int - add7 x; val it=19:int;

6 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing problems. Many ideas in design patterns have their roots in functional programming (e.g. strategy allows us to treat methods as first-class, a decorator acts like a function which maps a function to a function – think of the stream decorators in Java). We describe something as first-class if it can be created at runtime, created anonymously, named, stored in data structures, passed as an argument to a function, returned as a value from a function. With mutation (the ability to change a name-value binding in an environment) sophisticated systems can be built quite compactly.

7 Rules of expression evaluation Numbers evaluate to “themselves” –a character string representing a number evaluates to its base 10 numeric representation (unless a different base is explicitly indicated) For example: –Scheme > 100 if you ask Scheme to evaluate a string of (decimal) digits 100 you get the corresponding decimal number as a result > #o100 if you ask Scheme to evaluate a string of octal digits 64 you get the corresponding decimal number as a result –This character string to numeric representation happens in other languages as well; consider Java: int x = 100; int y = 0100; System.out.println("x is "+x+", but y is "+y); –prints: x is 100, but y is 64

8 Evaluating names A name is evaluated by looking it up. Lookup starts in the current environment, and continues along the chain of statically-linked environments until either a binding is found (in which case the corresponding value is returned) or it isn’t (in which case an error occurs). For example (assuming no binding for x exists yet): > x Error: reference to undefined identifier: x > (define x 12) > x 12 >

9 lambda forms A lambda form (lambda abstraction) defines a function in Scheme. Informally, the syntax is: (lambda ( ) ) When a lambda form is evaluated a closure results (which is printed as # ). –Aside - comparison of proposals for adding closures to Java: For example: > (define addOne (lambda (p) (+ p 1))) > addOne # >

10 Primitives Primitives, such as + and -, are ordinary names with name-value bindings established at start-up in the primitive environment, the base environment for evaluation (base in the sense that its static link is null). We can use + wherever we need a function. For example (defining a function which takes a function as argument): > (define applyOperator (lambda (op) (op 3 4))) > (applyOperator +) 7 > (applyOperator -) We can also rebind the names of primitives (not a good idea in general, but this shows that primitives are not treated differently from other names in the language). > (+ 3 4) name “+” refers to addition function 7 > (define + -) name “+” now refers to subtraction function > (+ 3 4)

11 Function applications A function application is evaluated by first evaluating each expression inside the application, then applying the function to its arguments. This is accomplished by first creating a new environment in which the function’s parameters are bound to its arguments, and then evaluating the function’s body with respect to the new environment (which is now the current environment).

12 Evaluating (addOne 4) + addOne primitive env user env - cons … p (+ p 1) p4

13 Local bindings In a language like C or Java we can create local bindings inside a function/method by defining local variables: int mystery(int x, int y, int z) { int a = func1(x,y); int b = func2(y,z); } Local bindings can be created using a let-form: (define mystery (lambda (x y z) (let ((a (func1 x y)) (b (func2 y z))) ) Let-form is just syntactic sugar for a procedure application; the above let-form is equivalent to: ((lambda (a b) ) (func1 x y) (func2 y z))

14 Evaluating (define foo (let ((a 1) (b 2)) (lambda (c) (+ a b c)))) + addOne foo primitive env user env - cons … a b (lambda (c) (+ a b c)) a1 b2 (lambda (c) (+ a b c)) c (+ a b c)

15 Mutation set! allows an existing name-value binding to be changed.

16 Structuring data So far we’ve seen two types of values that we can bind to names: –numbers –functions two others are: –symbols –pairs

17 Symbols A symbol is an unevaluated name. Consider the following interaction: > (define x 12) > x 12 The x in the define is not evaluated. It is a symbol. The x in the second line is evaluated by looking up the value it is bound to in the current environment.

18 Using symbols We can bind a name to a symbolic value by quoting the symbol – quoting prevents evaluation: > (define fifi 'poodle) > fifi poodle The quote mark (') is syntactic sugar for the form (quote …); the above is equivalent to: > (define fifi (quote poodle))

19 Pairs cons is a primitive function which builds a pair. A pair has two members. (cons 'a 'b) builds a pair –whose first member is the symbol a –whose second member is the symbol b cons is therefore a pair constructor. Graphically: ba

20 accessors car returns the first member of a pair cdr returns the second member of a pair > (define myPair (cons 'a 'b)) > (car myPair) a > (cdr myPair) b

21 How is a pair displayed? > (define myPair (cons 'a 'b)) > myPair (a. b) Pairs are printed with a dot (.) separating the first (left) and second (right) components.

22 The REPL When interacting with a Scheme system, a Read-Eval-Print Loop (REPL) reads what you type, evaluates it, prints the resulting value, and loops back to repeat. The reader and printer parts of the REPL handle syntactic sugar (like converting 'a into (quote a)) and also printing things like lists.

23 Lists What is a list? A recursive definition: –the empty list () is a list –a pair whose cdr is a list is a list We can build a list using cons and (): > (define myList (cons 'a '())) > myList (a)

24 Wait a minute!! > (define myPair (cons 'a 'b)) > myPair (a. b) > (define myList (cons 'a '()) > myList (a)

25 What's going on? Why did myList print as (a) rather than as (a. ())? In fact, they are entirely equivalent: the REPL strikes again. The printer formats pairs which cdr is a pair in a special way to make lists easier to read.

26 Printing lists (a. (b. (c. (d. ())))) is printed as (a b c d) Graphically the structure is: This structure can be built in a variety of ways, some of which are shown on the next slide. abcd

27 Choices…choices (cons 'a (cons 'b (cons 'c (cons 'd '())))) (list 'a 'b 'c 'd) '(a b c d) '(a. (b c d)) (cons 'a (list 'b 'c 'd)) and so on!

28 Association lists An association list is a list of pairs. Used to implement a look-up table of key- value pairs. The primitive assoc is used to search an association list, given a key. Example on next slide.

29 Association list example > (define aList '((fifi. poodle) (fido. bulldog) (clifford. bigRed) (lassie. collie))) > (assoc 'fido aList) (fido. bulldog) > (assoc 'rufus aList) #f

30 Mutating pair components set-car! mutates the value of the car of a pair set-cdr! mutates the value of the cdr of a pair > (define myPair (cons 'a 'b)) > myPair (a. b) > (set-car! myPair 'fred) > myPair (fred. b) > (set-cdr! myPair 'wilma) > myPair (fred. wilma)

31 Lists in other languages A list is a recursive data structure –base case: an empty list is a list (called null, or (), in Scheme) –recursive case: a pair whose tail is a list is also a list (cons is pair op) Lists are fundamental also in Lisp, ML, Prolog, Erlang, etc. Lists in ML –list type is defined by cases: –[] is empty list constructor (a literal of type ’a list ) –:: is non-empty list constructor (a function of type ’a * ’a list -> ’a list ) –Example: 1 :: 2 :: [] produces list printed as [1,2] can write list directly as [1,2] Lists in Prolog (not entire truth, but close enough for now) –same basic structure as in Scheme –[] is empty list constructor –[_|_] is non-empty list constructor –Example: [ 1 | [ 2 | [] ] ] produces list printed as [1,2] can write list directly as [1,2]

32 Homework issues What is basic algorithm?

33 Homework issues What is basic algorithm? –(UPDATE THIS SLIDE)