Programming Languages: Design, Specification, and Implementation G22.2210-001 Rob Strom September 28, 2006.

Slides:



Advertisements
Similar presentations
1 Copyright © 1998 by Addison Wesley Longman, Inc. Chapter 14 Functional Programming Languages - The design of the imperative languages is based directly.
Advertisements

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.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
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.
LISP Programming. LISP – simple and powerful mid-1950’s by John McCarthy at M.I.T. “ LIS t P rocessing language” Artificial Intelligence programs LISP.
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.
Programming Languages: Design, Specification, and Implementation G Rob Strom September 14, 2006.
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.
Programming Languages: Design, Specification, and Implementation G Rob Strom October 5, 2006.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
CS 152: Programming Language Paradigms February 24 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Functional Programming Universitatea Politehnica Bucuresti Adina Magda Florea
1 Lisp Functions –Built-in functions –Defining functions –Function Evaluation and Special Forms defun, if Control statements –Conditional if, cond –Repetition.
ISBN Chapter 15 Functional Programming Languages.
331 Final Spring Details 6-8 pm next Monday Comprehensive with more emphasis on material since the midterm Study example finals and midterm exams.
CS 330 Programming Languages 11 / 21 / 2006 Instructor: Michael Eckmann.
CSE 341, S. Tanimoto Lisp Defining Functions with DEFUN Functions are the primary abstraction mechanism available in Lisp. (Others are structures.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
Introduction to Scheme CS 480/680 – Comparative Languages “And now for something completely different…” – Monty Python “And now for something completely.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
ISBN Chapter 15 Functional Programming Languages.
CS535 Programming Languages Chapter - 10 Functional Programming With Lists.
CSE S. Tanimoto Macros 1 Defining Macros in Scheme Extensibility: A language is extensible if the language can be extended. New Scheme control structures.
Programming Languages: Design, Specification, and Implementation G Rob Strom September 21, 2006.
PPL CPS. Moed A 2007 Solution (define scale-tree (λ (tree factor) (map (λ (sub-tree) (if (list? sub-tree) (scale-tree sub-tree factor) (* sub-tree.
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.
Operational Semantics of Scheme
Functional Programming
Abstract Syntax cs7100 (Prasad) L7AST.
CS314 – Section 5 Recitation 10
Functional Programming Languages
Functional Programming
CS 326 Programming Languages, Concepts and Implementation
6.001 Jeopardy.
6.001 SICP Variations on a Scheme
The interpreter.
Introduction to Scheme
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
COP4020 Programming Languages
Original material by Eric Grimson
Introduction to Functional Programming in Racket
The Metacircular Evaluator
FP Foundations, Scheme In Text: Chapter 14.
Dynamic Scoping Lazy Evaluation
The Metacircular Evaluator
6.001 SICP Further Variations on a Scheme
Streams, Delayed Evaluation and a Normal Order Interpreter
Abstract Syntax cs7100 (Prasad) L7AST.
Lecture 12: Message passing The Environment Model
Lecture 14 - Environment Model (cont.) - Mutation - Stacks and Queues
6.001 SICP Variations on a Scheme
Introduction to Functional Programming in Racket
Lecture 14: The environment model (cont
Defining Functions with DEFUN
Abstraction and Repetition
topics interpreters meta-linguistic abstraction eval and apply
Common Lisp II.
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
More Scheme CS 331.
Defining Macros in Scheme
Presentation transcript:

Programming Languages: Design, Specification, and Implementation G Rob Strom September 28, 2006

Administrative Alternative mailing address for me: Everyone should subscribe to the class mailing list: /g22_2110_001_fa06 Homework due by class 5 (Scheme): Convert list of atoms to concrete tree according to the simple grammar Convert concrete tree to abstract tree expr ::= expr “+” term | expr “–” term | term term ::= term “*” factor | term “/” factor | factor factor ::= number | identifier | “(“ expr “)”

Closures A closure is a function whose execution has access to an external environment LISP was the earliest language to do closures, and it did them the other way (dynamic) Static generally considered better; Scheme is basically LISP with closures done “right”

Essential features in Scheme 1 st class procedures Dynamically created procedures Based on lambda calculus over atoms and pairs; by convention, lists are pairs Continuations Automatic garbage collection Applicative style: binding, no update, no side effects (but there are exceptions to this, the “set!” operator) Static scoping, but no static typing! Simple syntax (afunction arg1 arg2 …) ; function application Special forms, e.g. (if … ) (quote (x y)) (lambda (x) (* x x))

Scheme: Applicative example (define isort ( lambda (l) (letrec ( ; defines a list of bindings (here just 1) (insert ( ; inserts item x in sorted order to list l lambda (x l) (if (null? l) (list x) (if (<= x (car l)) (cons x l) (cons (car l) (insert x (cdr l)))) ; means this insert )))) ; the below is executed in the context of the bindings (if (null? l) nil (insert (car l) (isort (cdr l)))) ) )) (isort ( ))

Continuations A procedure with parameters to tell you what to do with an answer after computing it In Continuation Passing Style (CPS), instead of returning a result, you pass the result to the continuation that was passed to you (define (mysqrt x) (sqrt x)) ; normal definition of mysqrt (display (mysqrt 4)) ; normal call to display mysqrt of 4 (+ (mysqrt 4) 2) ; normal call to compute sqrt(4) + 2 (define (mysqrt x k) (k (sqrt x))) ; CPS definition of mysqrt (mysqrt 4 display) ; CPS call to display mysqrt of 4 (mysqrt 4 (lambda (x) (+ x 2))) ; CPS call to compute sqrt(4) + 2 (define (factorial n) (if (<= n 1) 1 (* n (factorial (- n 1))))) ; normal definition of factorial (+ (factorial 4) 2) ; normal call to compute 4! + 2 (define (factorial n k) (if (<= n 1) (k 1) (factorial (- n 1) (lambda (ret) (k (* n ret)))))) (factorial 4 (lambda (x) (+ x 2))) ; CPS call to compute 4! + 2

Using CPS and closures To return multiple values: x, y, z, instead of packing it into a single object like (x.(y.z)), and later unpacking, simply call the continuation passing x, y, z Instead of alternate exits (e.g. succeed and return x, y, z vs. fail and return nothing), use multiple continuations

Example: CPS plus closures (define get-expr-prefix (lambda ( lst ; a list of unparsed tokens success ; continuation(parsedexpr unparsed) parsedexpr : expr-tree of prefix of lst, unparsed : rest failure ; continuation(unparsed) ) (letrec (make-closure (lambda (parsedexpr ; parsedexpr : expr parsed so far cont ; a continuation(handle-has-suffix, handle-has-no-suffix) ; handle-has-suffix: lambda(operator unparsed) ; handle-has-no-suffix : lambda(unparsed) ) (letrec ((expr parsedexpr) (handle-has-suffix-method (lambda (operator unparsed …) (handle-has-no-suffix-method(lambda (unparsed) (success parsedexpr unparsed)) (cont handle-has-suffix-method handle-has-no-suffix-method) )) ( (term-found (lambda (parsedterm unparsed) ; parsedterm :term-tree, unparsed: rest of lst (make-closure (cons ‘expr (list parsedterm)) (lambda (has-suffix-method has-no-suffix-method) (get-additive-operator unparsed has-suffix-method has-no-suffix-method))) (term-not-found (lambda (unparsed) (failure unparsed))) (get-term-prefix lst term-found term-not-found) ) ))) (define get-expr (lambda lst) (get-expr-prefix lst (lambda (parsed, unparsed) (if (empty? unparsed) (display parsed) (display ‘error)) (lambda(unparsed) (display ‘error)) ; Find the term following the +/- operator ; If there is one, extend the expression, save it in the closure, ; and once again try to find a suffix ; If there isn’t one, this is a bad expression, e.g. A * B + ) letrec (got-term (lambda (parsedterm rest) (make-closure (cons ‘expr (list parsedexpr operator parsedterm)) (lambda (has-suffix has-no-suffix) (get-additive-operator rest has-suffix has-no-suffix)))) (no-term (lambda rest) (failure rest))) (get-term-prefix rest got-term no-term))

Call/cc – by example (call-with-current-continuation (lambda (exit) (for-each (lambda (x) (if (negative? x) (exit x))) '( )) #t)) => -3 (define list-length (lambda (obj) (call-with-current-continuation (lambda (return) (letrec ((r (lambda (obj) (cond ((null? obj) 0) ((pair? obj) (+ (r (cdr obj)) 1)) (else (return #f)))))) (r obj)))))) (list-length '( )) => 4 (list-length '(a b. c)) => #f

Macros These are ways to extend Scheme They are invoked on static expressions rather than on values Scheme introduced “hygienic macros” Hygiene: If a macro transformer inserts a binding for an identifier, the new binding will not capture other identifiers of the same name introduced elsewhere. Referential Transparency: If a macro transformer inserts a free reference to an identifier, the reference refers to the binding that was visible where the transformer was specified, regardless of any local bindings that may surround the use of the macro.

Eval This allows you to create an arbitrary expression representing a Scheme program as a data object, and then execute it There is an interpreter that one can write in scheme that emulates eval: what is surprising is how short the program is – a Scheme interpreter in Scheme is a shorter program that your homework program!