What is a model METAPROGRAMMING. ! Descrip5on/abstrac5on of real world things. ! Abstract representa5on that can be manipulated by a program 11/22/13

Size: px
Start display at page:

Download "What is a model METAPROGRAMMING. ! Descrip5on/abstrac5on of real world things. ! Abstract representa5on that can be manipulated by a program 11/22/13"

Transcription

1 METAPROGRAMMING What is a model! Descrip5on/abstrac5on of real world things o Something with a descrip5on of how it should be structured o Objects & rela5onships (a graph?)! Abstract representa5on that can be manipulated by a program o Can be easier to modify or work with o Simula5on (cost) o Abstrac5on of a process o Abstrac5on of something that does not exist yet 1

2 Models and metamodels Reflec5on, Introspec5on! Reflec/on is the ability of a program to manipulate as data something represen5ng the state of the program during its own execu5on.! Introspec/on is the ability for a program to observe and therefore reason about its own state.! Intercession (adap/on) is the ability for a program to modify its own execu5on state or alter its own interpreta6on or meaning.! All aspects require a mechanism for encoding execu5on state as data: this is called reifica'on. 2

3 Models and Metamodels Metaprogramming! The term metaprogramming covers almost any situa5on where a program manipulates code, either its own or that of some other program. This may happen in many ways. o Textual manipula5on of code as strings (Javascript eval) o Code genera5on at compile 5me or run 5me (JIT compilers) o Self- modifying code (Autonomic Compu5ng) o Staged computa5on (Par5al Evalua5on)! Strictly speaking, any compiler or interpreter would qualify as a metaprogram.! Here, the no5on of metaprogramming will refer to specific language features 3

4 metaprogramming Metaprogramming Examples #define MULT(x, y) x * y MACRO in C int z = MULT(3 + 2, 4 + 2); What value do you expect z to end up with? The obvious answer is 30 int z = * 4 + 2; // 2 * 4 will be evaluated first! Code inlining 4

5 MACRO in C #define MULT(x, y) (x) * (y) MULT(3 + 2, 4 + 2) will expand to (3 + 2) * (4 + 2) Metaprogramming Examples for(i = 0; i < n; i++)... The non- syntac5c UpTo macro Builds code at compile 5me #define UpTo(i, n) for((i) = 0; (i) < (n); (i)++) UpTo(i, 10)... 5

6 Metaprogramming Examples C++ Templates Template<int n> Vector<n> add(vector<n> lhs, Vector<n> rhs) { Vector<n> result = new Vector<n>; for (int i = 0; i < n; ++i) result.value[i] = lhs.value[i] + rhs.value[i]; return(result) } Template: general rou5ne for adding vectors of arbitrary length C++ STL does a lot of compile 5me specializa5on import java.io.* import java.lang.reflect.*; Class c = Class.forName("java.lang.System"); // Fetch System class Field f = c.getfield("out"); // Get sta5c field Object p = f.get(null); // Extract output stream Class cc = p.getclass(); // Get its class Class types[] =newclass[] { String.class}; // Iden5fy argument types Method m = cc.getmethod("println", types); // Get desired method Object a[] = new Object[] { "Hello, world" }; // Build argument array m.invoke(p,a); //Invoke method J A V A 6

7 Java Class Loading public class MainClass { } public sta5c void main(string[] args){ ClassLoader classloader = MainClass.class.getClassLoader(); try { Class aclass = classloader.loadclass("com.giangi.myclass"); System.out.println("aClass.getName() = " + aclass.getname()); } catch (ClassNotFoundExcep5on e) { e.printstacktrace(); } INTERMEZZO: LISP 7

8 Lisp Milestones! Late 1950's: LISP (LISt Processor) invented by John McCarthy et al. at MIT for the purpose of AI research.! 1960: landmark paper introducing LISP to the wider world: "Recursive Func5ons of Symbolic Expressions and Their Computa5on by Machine! 1974: Pisa MagmaLisp! 1975: Scheme invented by Guy Steele and Gerald Sussman.! Mid- 1980's: Non- Scheme Lisp dialects (principally Maclisp and Interlisp) standardized into "Common Lisp".! Late- 1980's: Common Lisp Object System adds OOP constructs to Common Lisp.! 1998: R 5 RS (5th revised report on Scheme) defines the Scheme standard s5ll in use today. 8

9 Lisp Big Ideas! Many of the major good ideas we've seen in programming languages originated in dialects of Lisp: o garbage collec5on, uniform by- reference data model, strong typing, lists as a pervasive data structure, heavy use of higher- order, anonymous, and recursive func5ons.! Programs are data: Lisp programs are themselves lists, and Lisp has special syntaxes for represen5ng "unevaluated list data". It is therefore easy to represent Lisp programs as Lisp data, and so Lisp lends itself to metaprogramming (wri5ng programs that manipulate other programs) easy.! Simplicity and regularity above all: Scheme demonstrates how small a language can be while remaining powerful and expressive.! Hygienic syntax macros: The Scheme syntax is extensible through a macro system that (unlike, for example, C macros) is rela5vely safe and has a clean seman5cs that fits in well with the rest of the language. Syntax program ::= S* S ::= atom list atom ::= IDENTIFIER NUMBER SYMBOL STRING... list ::= ( S* ) An s- expression is either an atom or a list. Atoms include variable names and literal values. A list is a sequence of whitespace- separated s- expressions inside round parens 9

10 (+ 1 2) ; sum of 1 and 2 ( ) ; sum of 1, 2, and 3; (cons 1 ()) ; 1 consed onto nil () (list 1 2 3) ; The list (cons 1 (cons 2 (cons 3 ()))) ;; Lists can be heterogeneous (list 1 "hi" 'a #f) (list 0 (list 1 2)) ;; (car (list 1 2 3)) ; car is the equivalent of "hd" (cdr (list 1 2 3)) ; cdr is the equivalent of "tl" (lambda (x) (+ x 1)) ; anonymous func5ons ((lambda (x) (x + 1)) 2) ; applies func5on to 2 (define add (lambda (x y) (+ x y))) ; binds name add to func5on map (define (mymap f alist) (if (null? alist) () (cons (f (car alist)) mymap f (cdr alist))))) 10

11 Car? Cadr? Historical note: car and cdr come from the names of the assembly language instruc5ons used to implement them in 1959 on the IBM 704: CAR was the opcode for "contents of the address register", CDR was the opcode for "contents of the decrement register". The terminology is horrible but has stayed with us to this day. Set and ref SCHEME ML (define x 5) ; val x = ref 5 (define y x) ; val y = ref (!x) (set! x 4) ; x := 4 x ;!x; y ;!y; 11

12 QUOTE Scheme has a convenient syntax for represen5ng data literals: prefix any expression with ' (single quote) and the expression, rather than being evaluated, will be returned as data S- Expression ; => Evaluated result '3 ; => 3 (a number) '"hi" ; => "hi" (a string) 'a ; => a (a symbol) '(+ 3 4) ; => (list '+ '3 '4) (a list) '(a b c) ; => (list 'a 'b 'c) (a list) QUOTE '(lambda (x) (+ x 3)) (a list) ; => (list 'lambda (list 'x) (list '+ 'x '3)) ; => (list 'lambda (list 'x) (list '+ 'x 3)) "quoted" data remains unevaluated, and provides a convenient way of represen5ng programs. Since programs themselves are lists, it is extremely simple to represent Lisp programs as data. 12

13 QUOTE names in Lisp programs are translated into symbols in quoted Lisp expressions. quoted names can be dis5nguished from quoted strings; : '(define x 10) ; => (list 'define 'x 10) '("define" x 10) ; => (list "define" 'x 10) QUASIQUOTE the ` (backquote) operator, also called quasiquote with the, (comma) operator, also called unquote. Quasiquote behaves like quote, except that unquoted expressions are evaluated: `(1 2,(+ 3 4)) ; => '(1 2 7) 13

14 Special forms (quote (1 2 (+ 3 4))) ; => '(1 2 (+ 3 4)) (quasiquote (1 2 (unquote (+ 3 4)))) ; => '(1 2 7) EVAL Since we have a method for represen5ng programs as data, it is convenient to have a func5on that evaluates that data as if it were a part of the currently running program Eval in DrScheme: (define x 5) (define y '(+ x 10)) (eval y) ; => 15 (eval '((lambda (x y) (string- concat x y)) "foo" "bar")) ; => "foobar" 14

15 The Scheme Interpreter in Scheme (define (eval exp env) (cond ((self- evalua5ng? exp) exp) ((quoted? exp) (text- of- quota5on exp)) ((variable? exp) (lookup- variable- value exp env)) ((defini5on? exp) (eval- defini5on exp env)) ((assignment? exp) (eval- assignment exp env)) ((lambda? exp) (make- procedure exp env)) ((condi5onal? exp) (eval- cond (clauses exp) env)) ((applica5on? exp) (apply (eval (operator exp) env) (list- of- values (operands exp) env))) (else (error "Unknown expression type - - EVAL" exp)))) (define (apply procedure arguments) (cond ((primi5ve- procedure? procedure (apply- primi5ve- procedure procedure arguments))) ((compound- procedure? procedure) (eval- sequence (procedure- body procedure) (extend- environment (parameters procedure) arguments (procedure- environment procedure)))) (else (error "Unknown procedure type - - APPLY" procedure)))) 15

16 END INTERMEZZO (LISP) JS EVAL eval("3+4"); // Returns 7 a= "5 ";b="2"; eval(a+b); // Returns 3, result of 5 2 eval(b+a); // Run5me syntax error a= "5 "; b = "1"; c = "a+a+b"; eval(c); // Returns the string "5-5- 1" eval(eval(c)) //Returns the number

17 JS Eval var foo = 1; func5on test() { var foo = 2; eval('foo = 3'); return foo; } test(); // 3 foo; // 1 JS Eval var strfun = "func5on myfun(arg) { return arg * 3; }"; eval(strfun); func5on usefun(x) { var res = myfun(x); alert(res); } 17

18 ML+++MACRO let fun pow n x = if n = 0 the 1 else x * (pow (n- 1) x) in pow (2*3) (5+6) let mac pow n x = if n = 0 the 1 else x * (pow (n- 1) x) pow (2*3) (5+6) espands into if (2*3) = 0 the 1 else (5+6) * (pow ((2*3)- 1) (5+6))... metaml let mac pow ~n x = ~(if n = 0 the <1> else <x * ~(pow (n- 1) x)>) In pow ~(2*3) (5+6) Escape ~e escape from macro expansion to evalua5on mode Bracket <e> interrupts evalua5on and return to the macro expansion mode (5+6) * (5+6) * (5+6) * (5+6) * (5+6) * (5+6) * 1 18

19 MetaOCaml # let x =.<4 + 2>. ;; val x: int code =.<4 + 2>. # let y =.<.~x +.~x>. ;; val y int code = <(4 + 2) + (4 + 2)>. #let z =.! Y;; val z : int = 12 Quote.<e>. An5quote.~ Execute.! Staged Programming Languages! MetaMl (MetaOCaml) are examples of mul5 stage programming languages provoding suitable abstrac5ons and control over exactly what parts are evaluated when in the chain from source to execu5on 19

20 F# IN PILLOLE F#! F# is a func5onal (and object oriented) language for the.net framework. let rec fact n = match n with 0 - > 1 n - > n * fact (n- 1) Approxima5on: F# is Ocml with.net libraries and CLR. MSIL,.. 20

21 F# at MSR! F# brings you type safe, succinct, efficient and expressive func5onal programming language on the.net pla~orm. It is a simple and pragma5c language, and has par5cular strengths in data- oriented programming, parallel I/O programming, parallel CPU programming, scrip5ng and algorithmic development. It lets you access a huge.net library and tools base and comes with a strong set of Visual Studio development tools. F# combines the advantages of typed func5onal programming with a high- quality, well- supported modern run5me system. F#! Interoperability with the.net framework and other.net languages is central to F#.! Core syntax is OCaml: with higher- order func5ons, lists, tuples, arrays, records,...! Objects are as in C#: with classes, inheritance, dot nota5on for field and method selec5on,...!.net toys (aka powerful tools): extensive libraries, concurrent garbage collector, install- 5me/run- 5me (JIT) compila5on, debuggers, profilers,...! Creates and consumes.net/c# types and values; can call and be called from other.net languages.! Generates and consumes.net code: can exchange first- class func5ons with other languages. 21

22 Metaprogramming in F#! D. Syme Leveraging.NET meta- programming components from F#: Integrated queries and interoperable heterogeneous execu5on. In ML 06: Proceedings of the ACM SIGPLAN 2006 Workshop on ML, pages ACM Press, September Quota5on in F# > open Microso.Fsharp.Quota5ons let q = <@ * val q : Quota5ons.Expr<int> = Call (None, op_addi5on, [Value (2), Call (None, op_mul5ply, [Value (2), Value (3)])]) F# provides explicit quota5on markers (as in LISP, MetaML and MetaOCaml) 22

23 > let w = <@ while true do prin~n "It never let f = <@ fun x y - > x + val w : Quota5ons.Expr<unit> = WhileLoop (Value (true), Call (None, PrintFormatLine, [Coerce (NewObject (Prin~Format`5, Value ("It never ends...")), Prin~Format`4)])) val f : Quota5ons.Expr<(int - > int - > int)> = Lambda (x, Lambda (y, Call (None, op_addi5on, [x, y]))) > open Microso.FSharp.Quota5ons open Microso.FSharp.Quota5ons.Paƒerns open Microso.FSharp.Quota5ons.DerivedPaƒerns let rec evalexpr q = match q with SpecificCall <@ (_, _, [a; b]) - > let l, r = evalexpr a, evalexpr b l + r SpecificCall <@ (_, _, [a; b]) - > let l, r = evalexpr a, evalexpr b l * r Value (v, t) - > if t = typeof<int> then v :?> int else failwith "Only integer values supported" _ - > failwith "Unsupported expression" 23

24 MORE INFO ON 24

Advances in Programming Languages

Advances in Programming Languages T O Y H Advances in Programming Languages APL19: Heterogeneous Metaprogramming in F# Ian Stark School of Informatics The University of Edinburgh Monday 15 March 2010 Semester 2 Week 10 E H U N I V E R

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 16: Functional Programming Zheng (Eddy Zhang Rutgers University April 2, 2018 Review: Computation Paradigms Functional: Composition of operations on data.

More information

Ways to implement a language

Ways to implement a language Interpreters Implemen+ng PLs Most of the course is learning fundamental concepts for using PLs Syntax vs. seman+cs vs. idioms Powerful constructs like closures, first- class objects, iterators (streams),

More information

Functional Programming. Pure Functional Programming

Functional Programming. Pure Functional Programming Functional Programming Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends only on the values of its sub-expressions (if any).

More information

LISP: LISt Processing

LISP: LISt Processing Introduc)on to Racket, a dialect of LISP: Expressions and Declara)ons LISP: designed by John McCarthy, 1958 published 1960 CS251 Programming Languages Spring 2017, Lyn Turbak Department of Computer Science

More information

(Func&onal (Programming (in (Scheme)))) Jianguo Lu

(Func&onal (Programming (in (Scheme)))) Jianguo Lu (Func&onal (Programming (in (Scheme)))) Jianguo Lu 1 Programming paradigms Func&onal No assignment statement No side effect Use recursion Logic OOP AOP 2 What is func&onal programming It is NOT what you

More information

Func%onal Programming in Scheme and Lisp

Func%onal Programming in Scheme and Lisp Func%onal Programming in Scheme and Lisp http://www.lisperati.com/landoflisp/ Overview In a func(onal programming language, func(ons are first class objects You can create them, put them in data structures,

More information

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

Scheme. Functional Programming. Lambda Calculus. CSC 4101: Programming Languages 1. Textbook, Sections , 13.7 Scheme Textbook, Sections 13.1 13.3, 13.7 1 Functional Programming Based on mathematical functions Take argument, return value Only function call, no assignment Functions are first-class values E.g., functions

More information

Discussion 12 The MCE (solutions)

Discussion 12 The MCE (solutions) Discussion 12 The MCE (solutions) ;;;;METACIRCULAR EVALUATOR FROM CHAPTER 4 (SECTIONS 4.1.1-4.1.4) of ;;;; STRUCTURE AND INTERPRETATION OF COMPUTER PROGRAMS ;;;from section 4.1.4 -- must precede def of

More information

Func%onal Programming in Scheme and Lisp

Func%onal Programming in Scheme and Lisp Func%onal Programming in Scheme and Lisp http://www.lisperati.com/landoflisp/ Overview In a func(onal programming language, func(ons are first class objects You can create them, put them in data structures,

More information

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

6.184 Lecture 4. Interpretation. Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 6.184 Lecture 4 Interpretation Tweaked by Ben Vandiver Compiled by Mike Phillips Original material by Eric Grimson 1 Interpretation Parts of an interpreter Arithmetic calculator

More information

Scheme in Scheme: The Metacircular Evaluator Eval and Apply

Scheme in Scheme: The Metacircular Evaluator Eval and Apply Scheme in Scheme: The Metacircular Evaluator Eval and Apply CS21b: Structure and Interpretation of Computer Programs Brandeis University Spring Term, 2015 The metacircular evaluator is A rendition of Scheme,

More information

Functional programming in LISP

Functional programming in LISP Programming Languages Week 4 Functional programming in LISP College of Information Science and Engineering Ritsumeikan University review of part 3 enumeration of dictionaries you receive a sequence of

More information

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen

COP4020 Programming Languages. Functional Programming Prof. Robert van Engelen COP4020 Programming Languages Functional Programming Prof. Robert van Engelen Overview What is functional programming? Historical origins of functional programming Functional programming today Concepts

More information

Functional Programming

Functional Programming Functional Programming CS331 Chapter 14 Functional Programming Original functional language is LISP LISt Processing The list is the fundamental data structure Developed by John McCarthy in the 60 s Used

More information

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

Fall 2018 Discussion 8: October 24, 2018 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 208 Discussion 8: October 24, 208 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of

Fall Semester, The Metacircular Evaluator. Today we shift perspective from that of a user of computer langugaes to that of a designer of 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs Fall Semester, 1996 Lecture Notes { October 31,

More information

CS 360 Programming Languages Interpreters

CS 360 Programming Languages Interpreters CS 360 Programming Languages Interpreters Implementing PLs Most of the course is learning fundamental concepts for using and understanding PLs. Syntax vs. semantics vs. idioms. Powerful constructs like

More information

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V.

Introduction to LISP. York University Department of Computer Science and Engineering. York University- CSE V. Introduction to LISP York University Department of Computer Science and Engineering York University- CSE 3401- V. Movahedi 11_LISP 1 Introduction to LISP Evaluation and arguments S- expressions Lists Numbers

More information

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

Summer 2017 Discussion 10: July 25, Introduction. 2 Primitives and Define CS 6A Scheme Summer 207 Discussion 0: July 25, 207 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

LECTURE 16. Functional Programming

LECTURE 16. Functional Programming LECTURE 16 Functional Programming WHAT IS FUNCTIONAL PROGRAMMING? Functional programming defines the outputs of a program as a mathematical function of the inputs. Functional programming is a declarative

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Pairs and Lists in Racket. Box-and-pointer diagrams for cons trees The Pros of cons: in Racket CS251 Programming Languages Fall 2018, Lyn Turbak Department of Computer Science Wellesley College booleans: #t, #f numbers: integers: 42, 0, -273 ra3onals: 2/3, -251/17 floa3ng

More information

Functional Programming. Big Picture. Design of Programming Languages

Functional Programming. Big Picture. Design of Programming Languages Functional Programming Big Picture What we ve learned so far: Imperative Programming Languages Variables, binding, scoping, reference environment, etc What s next: Functional Programming Languages Semantics

More information

Control in Sequential Languages

Control in Sequential Languages CS 242 2012 Control in Sequential Languages Reading: Chapter 8, Sections 8.1 8.3 (only) Section 7.3 of The Haskell 98 Report, Exception Handling in the I/O Monad, http://www.haskell.org/onlinelibrary/io-13.html

More information

A Small Interpreted Language

A Small Interpreted Language A Small Interpreted Language What would you need to build a small computing language based on mathematical principles? The language should be simple, Turing equivalent (i.e.: it can compute anything that

More information

Functional Programming. Pure Functional Languages

Functional Programming. Pure Functional Languages Functional Programming Pure functional PLs S-expressions cons, car, cdr Defining functions read-eval-print loop of Lisp interpreter Examples of recursive functions Shallow, deep Equality testing 1 Pure

More information

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

SCHEME 8. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. March 23, 2017 SCHEME 8 COMPUTER SCIENCE 61A March 2, 2017 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme programs,

More information

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees

Racket Values. cons Glues Two Values into a Pair. The Pros of cons: Programming with Pairs and Lists. Box-and-pointer diagrams for cons trees The Pros of cons: Programming with CS251 Programming Languages Fall 2017, Lyn Turbak Department of Computer Science Wellesley College booleans: #t, #f numbers: integers: 42, 0, -273 ra3onals: 2/3, -251/17

More information

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86

Symbolic Reasoning. Dr. Neil T. Dantam. Spring CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Symbolic Reasoning Spring / 86 Symbolic Reasoning Dr. Neil T. Dantam CSCI-561, Colorado School of Mines Spring 2019 Dantam (Mines CSCI-561) Symbolic Reasoning Spring 2019 1 / 86 Introduction Definition: Symbolic Reasoning Inference

More information

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages CSc 520 Principles of Programming Languages 3: Scheme Introduction Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian Collberg [1]

More information

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

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 6.037 Lecture 4 Interpretation Interpretation Parts of an interpreter Meta-circular Evaluator (Scheme-in-scheme!) A slight variation: dynamic scoping Original material by Eric Grimson Tweaked by Zev Benjamin,

More information

Macros & Streams Spring 2018 Discussion 9: April 11, Macros

Macros & Streams Spring 2018 Discussion 9: April 11, Macros CS 61A Macros & Streams Spring 2018 Discussion 9: April 11, 2018 1 Macros So far, we ve mostly explored similarities between the Python and Scheme languages. For example, the Scheme list data structure

More information

A Func'onal Introduc'on. COS 326 David Walker Princeton University

A Func'onal Introduc'on. COS 326 David Walker Princeton University A Func'onal Introduc'on COS 326 David Walker Princeton University Thinking Func'onally In Java or C, you get (most) work done by changing something temp = pair.x; pair.x = pair.y; pair.y = temp; commands

More information

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

FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE, FUNKCIONÁLNÍ A LOGICKÉ PROGRAMOVÁNÍ 2. ÚVOD DO LISPU: ATOMY, SEZNAMY, FUNKCE, 2011 Jan Janoušek MI-FLP Evropský sociální fond Praha & EU: Investujeme do vaší budoucnosti L I S P - Introduction L I S P

More information

INTRODUCTION TO SCHEME

INTRODUCTION TO SCHEME INTRODUCTION TO SCHEME PRINCIPLES OF PROGRAMMING LANGUAGES Norbert Zeh Winter 2019 Dalhousie University 1/110 SCHEME: A FUNCTIONAL PROGRAMMING LANGUAGE Functions are first-class values: Can be passed as

More information

Lists in Lisp and Scheme

Lists in Lisp and Scheme Lists in Lisp and Scheme a Lists in Lisp and Scheme Lists are Lisp s fundamental data structures, but there are others Arrays, characters, strings, etc. Common Lisp has moved on from being merely a LISt

More information

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson

Streams, Delayed Evaluation and a Normal Order Interpreter. CS 550 Programming Languages Jeremy Johnson Streams, Delayed Evaluation and a Normal Order Interpreter CS 550 Programming Languages Jeremy Johnson 1 Theme This lecture discusses the stream model of computation and an efficient method of implementation

More information

Modern Programming Languages. Lecture LISP Programming Language An Introduction

Modern Programming Languages. Lecture LISP Programming Language An Introduction Modern Programming Languages Lecture 18-21 LISP Programming Language An Introduction 72 Functional Programming Paradigm and LISP Functional programming is a style of programming that emphasizes the evaluation

More information

Lambda Calculus. Gunnar Gotshalks LC-1

Lambda Calculus. Gunnar Gotshalks LC-1 Lambda Calculus LC-1 l- Calculus History Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of l-calculus is equivalent in power to

More information

Introduction to lambda calculus Part 3

Introduction to lambda calculus Part 3 Introduction to lambda calculus Part 3 Antti-Juhani Kaijanaho 2017-01-27... 1 Untyped lambda calculus... 2 Typed lambda calculi In an untyped lambda calculus extended with integers, it is required that

More information

SOFTWARE ARCHITECTURE 6. LISP

SOFTWARE ARCHITECTURE 6. LISP 1 SOFTWARE ARCHITECTURE 6. LISP Tatsuya Hagino hagino@sfc.keio.ac.jp slides URL https://vu5.sfc.keio.ac.jp/sa/ 2 Compiler vs Interpreter Compiler Translate programs into machine languages Compilers are

More information

Introduction to Scheme

Introduction to Scheme How do you describe them Introduction to Scheme Gul Agha CS 421 Fall 2006 A language is described by specifying its syntax and semantics Syntax: The rules for writing programs. We will use Context Free

More information

COP4020 Programming Assignment 1 - Spring 2011

COP4020 Programming Assignment 1 - Spring 2011 COP4020 Programming Assignment 1 - Spring 2011 In this programming assignment we design and implement a small imperative programming language Micro-PL. To execute Mirco-PL code we translate the code to

More information

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing

Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs. Lexical addressing Computer Science 21b (Spring Term, 2015) Structure and Interpretation of Computer Programs Lexical addressing The difference between a interpreter and a compiler is really two points on a spectrum of possible

More information

Documentation for LISP in BASIC

Documentation for LISP in BASIC Documentation for LISP in BASIC The software and the documentation are both Copyright 2008 Arthur Nunes-Harwitt LISP in BASIC is a LISP interpreter for a Scheme-like dialect of LISP, which happens to have

More information

Concepts of programming languages

Concepts of programming languages Concepts of programming languages Lecture 7 Wouter Swierstra 1 Last time Relating evaluation and types How to handle variable binding in embedded languages? 2 DSLs: approaches A stand-alone DSL typically

More information

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

Topic III. LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Topic III LISP : functions, recursion, and lists References: Chapter 3 of Concepts in programming languages by J. C. Mitchell. CUP, 2003. Chapters 5( 4.5) and 13( 1) of Programming languages: Design and

More information

4.2 Variations on a Scheme -- Lazy Evaluation

4.2 Variations on a Scheme -- Lazy Evaluation [Go to first, previous, next page; contents; index] 4.2 Variations on a Scheme -- Lazy Evaluation Now that we have an evaluator expressed as a Lisp program, we can experiment with alternative choices in

More information

W1005 Intro to CS and Programming in MATLAB. Brief History of Compu?ng. Fall 2014 Instructor: Ilia Vovsha. hip://www.cs.columbia.

W1005 Intro to CS and Programming in MATLAB. Brief History of Compu?ng. Fall 2014 Instructor: Ilia Vovsha. hip://www.cs.columbia. W1005 Intro to CS and Programming in MATLAB Brief History of Compu?ng Fall 2014 Instructor: Ilia Vovsha hip://www.cs.columbia.edu/~vovsha/w1005 Computer Philosophy Computer is a (electronic digital) device

More information

A Brief Introduction to Common Lisp

A Brief Introduction to Common Lisp A Brief Introduction to Common Lisp David Gu Schloer Consulting Group david_guru@gty.org.in A Brief History Originally specified in 1958, Lisp is the second-oldest highlevel programming language in widespread

More information

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

Functional Languages. CSE 307 Principles of Programming Languages Stony Brook University Functional Languages CSE 307 Principles of Programming Languages Stony Brook University http://www.cs.stonybrook.edu/~cse307 1 Historical Origins 2 The imperative and functional models grew out of work

More information

FP Foundations, Scheme

FP Foundations, Scheme FP Foundations, Scheme In Text: Chapter 15 1 Functional Programming -- Prelude We have been discussing imperative languages C/C++, Java, Fortran, Pascal etc. are imperative languages Imperative languages

More information

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want

Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Computer Science 21b (Spring Term, 2017) Structure and Interpretation of Computer Programs Syntactic Sugar: Using the Metacircular Evaluator to Implement the Language You Want Here is one of the big ideas

More information

An Explicit-Continuation Metacircular Evaluator

An Explicit-Continuation Metacircular Evaluator Computer Science (1)21b (Spring Term, 2018) Structure and Interpretation of Computer Programs An Explicit-Continuation Metacircular Evaluator The vanilla metacircular evaluator gives a lot of information

More information

A Brief Introduction to Scheme (II)

A Brief Introduction to Scheme (II) A Brief Introduction to Scheme (II) Philip W. L. Fong pwlfong@cs.uregina.ca Department of Computer Science University of Regina Regina, Saskatchewan, Canada Lists Scheme II p.1/29 Lists Aggregate data

More information

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator.

Why do we need an interpreter? SICP Interpretation part 1. Role of each part of the interpreter. 1. Arithmetic calculator. .00 SICP Interpretation part Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment Environment as explicit parameter Defining new procedures Why do

More information

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones.

Below are example solutions for each of the questions. These are not the only possible answers, but they are the most common ones. 6.001, Fall Semester, 2002 Quiz II Sample solutions 1 MASSACHVSETTS INSTITVTE OF TECHNOLOGY Department of Electrical Engineering and Computer Science 6.001 Structure and Interpretation of Computer Programs

More information

A LISP Interpreter in ML

A LISP Interpreter in ML UNIVERSITY OF OSLO Department of Informatics A LISP Interpreter in ML Mandatory Assignment 1 INF3110 September 21, 2009 Contents 1 1 Introduction The purpose of this assignment is to write an interpreter,

More information

low and not larger than high. 18 points

low and not larger than high. 18 points CSE 330 Test 1 (1 point) Spring 015 Multiple Choice. Write your answer to the LEFT of each problem. 3 points each 1. Lisp was invented at: A. IBM B. MIT C. Netscape D. Stanford. In C++, what operator is

More information

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

Intro. Scheme Basics. scm> 5 5. scm> Intro Let s take some time to talk about LISP. It stands for LISt Processing a way of coding using only lists! It sounds pretty radical, and it is. There are lots of cool things to know about LISP; if

More information

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

Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I Recursive Functions of Symbolic Expressions and Their Computation by Machine Part I by John McCarthy Ik-Soon Kim Winter School 2005 Feb 18, 2005 Overview Interesting paper with By John Mitchell Interesting

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018 Class Information Midterm exam forum open in Sakai. HW4 and

More information

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

Fall 2017 Discussion 7: October 25, 2017 Solutions. 1 Introduction. 2 Primitives CS 6A Scheme Fall 207 Discussion 7: October 25, 207 Solutions Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write

More information

Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell

Dynamic Languages. CSE 501 Spring 15. With materials adopted from John Mitchell Dynamic Languages CSE 501 Spring 15 With materials adopted from John Mitchell Dynamic Programming Languages Languages where program behavior, broadly construed, cannot be determined during compila@on Types

More information

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal)

CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) CS61A Discussion Notes: Week 11: The Metacircular Evaluator By Greg Krimer, with slight modifications by Phoebus Chen (using notes from Todd Segal) What is the Metacircular Evaluator? It is the best part

More information

From Syntactic Sugar to the Syntactic Meth Lab:

From Syntactic Sugar to the Syntactic Meth Lab: From Syntactic Sugar to the Syntactic Meth Lab: Using Macros to Cook the Language You Want a V E N E TRUT H Brandeis S PA R T U N T O I T S I N N E R M OS T COMPUTER SCIENCE 21B: Structure and Interpretation

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Introduc;on to Hakell Lesson 27! 1 The origins: ML programming

More information

4/19/2018. Chapter 11 :: Functional Languages

4/19/2018. Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Historical Origins The imperative and functional models grew out of work undertaken by Alan Turing, Alonzo Church, Stephen

More information

MIT Scheme Reference Manual

MIT Scheme Reference Manual MIT Scheme Reference Manual Edition 1.95 for Scheme Release 7.6.0 26 November 2001 by Chris Hanson the MIT Scheme Team and a cast of thousands Copyright c 1988-2001 Massachusetts Institute of Technology

More information

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

CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College. Scheme CS251 Programming Languages Handout # 47 Prof. Lyn Turbak May 22, 2005 Wellesley College 1 Scheme Overview Scheme Scheme is a block-structured, lexically-scoped, properly tail-recursive dialect of Lisp

More information

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ CS101: Fundamentals of Computer Programming Dr. Tejada stejada@usc.edu www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++ 10 Stacks of Coins You have 10 stacks with 10 coins each that look and feel

More information

Parsing Scheme (+ (* 2 3) 1) * 1

Parsing Scheme (+ (* 2 3) 1) * 1 Parsing Scheme + (+ (* 2 3) 1) * 1 2 3 Compiling Scheme frame + frame halt * 1 3 2 3 2 refer 1 apply * refer apply + Compiling Scheme make-return START make-test make-close make-assign make- pair? yes

More information

CSCI-GA Scripting Languages

CSCI-GA Scripting Languages CSCI-GA.3033.003 Scripting Languages 12/02/2013 OCaml 1 Acknowledgement The material on these slides is based on notes provided by Dexter Kozen. 2 About OCaml A functional programming language All computation

More information

Notes on Higher Order Programming in Scheme. by Alexander Stepanov

Notes on Higher Order Programming in Scheme. by Alexander Stepanov by Alexander Stepanov August 1986 INTRODUCTION Why Scheme? Because it allows us to deal with: 1. Data Abstraction - it allows us to implement ADT (abstact data types) in a very special way. The issue of

More information

Organization of Programming Languages CS3200/5200N. Lecture 11

Organization of Programming Languages CS3200/5200N. Lecture 11 Organization of Programming Languages CS3200/5200N Razvan C. Bunescu School of Electrical Engineering and Computer Science bunescu@ohio.edu Functional vs. Imperative The design of the imperative languages

More information

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

Week 2: The Clojure Language. Background Basic structure A few of the most useful facilities. A modernized Lisp. An insider's opinion Week 2: The Clojure Language Background Basic structure A few of the most useful facilities A modernized Lisp Review of Lisp's origins and development Why did Lisp need to be modernized? Relationship to

More information

Sample Final Exam Questions

Sample Final Exam Questions 91.301, Organization of Programming Languages Fall 2015, Prof. Yanco Sample Final Exam Questions Note that the final is a 3 hour exam and will have more questions than this handout. The final exam will

More information

Metaprogramming assignment 3

Metaprogramming assignment 3 Metaprogramming assignment 3 Optimising embedded languages Due at noon on Thursday 29th November 2018 This exercise uses the BER MetaOCaml compiler, which you can install via opam. The end of this document

More information

Recursive Functions of Symbolic Expressions and Their Application, Part I

Recursive Functions of Symbolic Expressions and Their Application, Part I Recursive Functions of Symbolic Expressions and Their Application, Part I JOHN MCCARTHY Review: Amit Kirschenbaum Seminar in Programming Languages Recursive Functions of Symbolic Expressions and Their

More information

Ruby: Introduction, Basics

Ruby: Introduction, Basics Ruby: Introduction, Basics Computer Science and Engineering College of Engineering The Ohio State University Lecture 3 Ruby vs Java: Similarities Imperative and object-oriented Classes and instances (ie

More information

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters.

CS 61A Interpreters, Tail Calls, Macros, Streams, Iterators. Spring 2019 Guerrilla Section 5: April 20, Interpreters. CS 61A Spring 2019 Guerrilla Section 5: April 20, 2019 1 Interpreters 1.1 Determine the number of calls to scheme eval and the number of calls to scheme apply for the following expressions. > (+ 1 2) 3

More information

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

Spring 2018 Discussion 7: March 21, Introduction. 2 Primitives CS 61A Scheme Spring 2018 Discussion 7: March 21, 2018 1 Introduction In the next part of the course, we will be working with the Scheme programming language. In addition to learning how to write Scheme

More information

Common LISP Tutorial 1 (Basic)

Common LISP Tutorial 1 (Basic) Common LISP Tutorial 1 (Basic) CLISP Download https://sourceforge.net/projects/clisp/ IPPL Course Materials (UST sir only) Download https://silp.iiita.ac.in/wordpress/?page_id=494 Introduction Lisp (1958)

More information

Macros. Pat Hanrahan. CS448h Fall 2015

Macros. Pat Hanrahan. CS448h Fall 2015 Macros Pat Hanrahan CS448h Fall 2015 Macro Definitions in Lisp Timothy Harris Abstract In LISP 1.5 special forms are used for three logically separate purposes: a) to reach the alist, b) to allow functions

More information

Chapter 15. Functional Programming Languages

Chapter 15. Functional Programming Languages Chapter 15 Functional Programming Languages Chapter 15 Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming Language: Lisp Introduction

More information

Pattern Matching for Scheme

Pattern Matching for Scheme Pattern Matching for Scheme Andrew K. Wright March 1996 Department of Computer Science MS 132 Rice University 6100 Main Street Houston, Texas 77005-1892 Contents 1 Match 1 1.1 Definition... 1 1.1.1 Patterns...

More information

Functional Programming

Functional Programming Functional Programming CS 1025 Computer Science Fundamentals I Stephen M. Watt University of Western Ontario When the Function is the Thing In O-O programming, you typically know where an action is needed,

More information

Implemen'ng OCaml in OCaml

Implemen'ng OCaml in OCaml 1 An OCaml defini'on of OCaml evalua'on, or, Implemen'ng OCaml in OCaml COS 326 David Walker Princeton University slides copyright 2013-2015 David Walker and Andrew W. Appel permission granted to reuse

More information

Symbolic Computation and Common Lisp

Symbolic Computation and Common Lisp Symbolic Computation and Common Lisp Dr. Neil T. Dantam CSCI-56, Colorado School of Mines Fall 28 Dantam (Mines CSCI-56) Lisp Fall 28 / 92 Why? Symbolic Computing: Much of this course deals with processing

More information

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

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G. Scheme: Data CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, 2017 Glenn G. Chappell Department of Computer Science University of Alaska Fairbanks ggchappell@alaska.edu

More information

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B.

Test 1 Summer 2014 Multiple Choice. Write your answer to the LEFT of each problem. 5 points each 1. Preprocessor macros are associated with: A. C B. CSE 3302 Test 1 1. Preprocessor macros are associated with: A. C B. Java C. JavaScript D. Pascal 2. (define x (lambda (y z) (+ y z))) is an example of: A. Applying an anonymous function B. Defining a function

More information

Lambda Calculus LC-1

Lambda Calculus LC-1 Lambda Calculus LC-1 λ- Calculus History-1 Developed by Alonzo Church during 1930 s-40 s One fundamental goal was to describe what can be computed. Full definition of λ-calculus is equivalent in power

More information

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. Lecture 09: Data Abstraction ++ Parsing Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree. program text Parser AST Processor Compilers (and some interpreters)

More information

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

Symbolic Programming. Dr. Zoran Duric () Symbolic Programming 1/ 89 August 28, / 89 Symbolic Programming Symbols: +, -, 1, 2 etc. Symbolic expressions: (+ 1 2), (+ (* 3 4) 2) Symbolic programs are programs that manipulate symbolic expressions. Symbolic manipulation: you do it all the

More information

Chapter 11 :: Functional Languages

Chapter 11 :: Functional Languages Chapter 11 :: Functional Languages Programming Language Pragmatics Michael L. Scott Copyright 2016 Elsevier 1 Chapter11_Functional_Languages_4e - Tue November 21, 2017 Historical Origins The imperative

More information

Scheme: Expressions & Procedures

Scheme: Expressions & Procedures Scheme: Expressions & Procedures CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Friday, March 31, 2017 Glenn G. Chappell Department of Computer Science University

More information

Comp 311: Sample Midterm Examination

Comp 311: Sample Midterm Examination Comp 311: Sample Midterm Examination October 29, 2007 Name: Id #: Instructions 1. The examination is closed book. If you forget the name for a Scheme operation, make up a name for it and write a brief

More information

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College

CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College Functions in Racket CS251 Programming Languages Spring 2016, Lyn Turbak Department of Computer Science Wellesley College Racket Func+ons Functions: most important building block in Racket (and 251) Functions/procedures/methods/subroutines

More information

Compiler Optimization Intermediate Representation

Compiler Optimization Intermediate Representation Compiler Optimization Intermediate Representation Virendra Singh Associate Professor Computer Architecture and Dependable Systems Lab Department of Electrical Engineering Indian Institute of Technology

More information