Principles of Programming Languages

Size: px
Start display at page:

Download "Principles of Programming Languages"

Transcription

1 Principles of Programming Languages Lesson 6 - Defining a Programming Language Bottom Up Collaboration and Management - Elements of Programming Dana Fisman 1

2 What we accomplished so far Different programming languages encourage different programming practices (and make other difficult) o IP (imperative programming) vs. FP (functional programming) o FPs tools include: - first class citizen functions - immutability - functional abstractions 2

3 What we accomplished so far Two aspects in program definition: o The syntactic domain: Expressions - created inductively from atoms using combination operators o The semantic domain: Values - computed inductively when evaluating expressions o Operational semantics: give meaning to expressions by describing the evaluation process 3

4 What we accomplished so far Data types denotes sets of values on which operations can be preformed uniformly o Primitive types o User-defined types Data types can be related to one another using set relations (subsumed-in, disjoint, ) Data types can be created from other data types using set operations (union, intersection, product) 4

5 What we accomplished so far We can use a type-language to associate datatypes to expressions othe type-language is build inductively using the type-system) Some programs allow programmers to annotate variables and functions with type annotations A type checker can establish that a given program satisfies the type declaration constraints. 5

6 Our next mission Learn a small functional programming language with a formal definition of its syntax a formal definition of its operational semantics build an interpreter - a program computing the value of expressions following the inductive definition provided by the operational semantics Semantics: <expr> -> <value> interpreters provide 6 Interpreter: a program implementing the semantics function a clarification mechanism an Illustration mechanism

7 Which language? What properties should a language that is good for learning principles of programming languages have? 7

8 Why Scheme In one word: Minimalistic a small language - a small number of primitives, - a small number of constructs - a small number of data-types common syntax to all composite expressions common semantics to all composite expression Compare to Javascript Yet: it is expressive: Turing complete allows powerful functional abstractions which make programming productive 8

9 Plan Introduce syntactic constructs and their semantics in a gradual manner L0 L1 L2 L3 9 Scheme

10 Specifying a programming language How do we specify a programming language? What are the elements that together define a language? The key elements are: Primitives Primitive literal values (numbers, booleans) Primitive operations (arithmetic ops, comparison ops) Combination means Means to create compound values Means to create compound expressions Abstraction Expressions Means to manipulate compound objects as standalone units Two aspects Values 10

11 L0 - Syntax Atomic expressions 1. Number literal expression 0, 1, 2, 2. Boolean literal expression #t, #f 3. Primitive operation expression +, -, *, /, <, >, = Compound expressions 4. compound expressions (op expr 1 expr n ) 11

12 L0 - examples 8 (+ 1 7) 42 (- 45 3) 14 (* 2 7) 9 (/ 45 5) #f (> 2 7) 12

13 L0 - examples 16 (* (+ 1 1) (- 9 1)) #t (= (+ 1 5) (- 9 3)) (* (+ 1 1) (- 9 1)) 16 (= (+ 1 5) (- 9 3)) #t Why prefix notation? 10 ( ) 13

14 L0 - examples 4 4 #t #t + #<procedure:+> 14

15 L0 - Semantics Atomic expressions 1. Number literal expression 0, 1, 2, Evaluates to the corresponding numeric value 2. Boolean literal expression #t, #f Evaluates to the corresponding boolean value The atomic expressions evaluate to themselves 3. Primitive operation expression +, -, *, /, <, >, = Evaluates to the corresponding operation <procedure:op> Compound expressions 4. compound expressions (op expr 1 expr n ) 1. Let (val 0,val 1,,val n ) = (evaluate(op), evaluate(exp n ),, evaluate(exp n )) 2. Apply the procedure val 0 to the values val 1,,val n Recursive application 15

16 L0 - Syntax Atomic expressions 1. Number literal expression 0, 1, 2, 2. Boolean literal expression #t, #f 3. Primitive operation expression +, -, *, /, <, >, = Compound expressions 4. compound expressions (op expr 1 expr n ) What is missing from L0? (+ (- (* 2 37) 18) (* 9 (- (* 2 37) 18)))) abstraction - one of the key components! 16

17 Naming 17 We will add to L0 a means to name expressions. 6 (define size 6) size (* 2 size) 12 (define <name> <expr>) (define area (* size size)) area 36 size in this context is called a variable a variable as in math rather than as in IP The relation between a variable and the value it denotes is called a binding

18 Evaluating the define form (define <var> <expr>) The interpreter maintains a global environment: a table recording the bindings between variables and their values The evaluation rule for (define <var> <expr>) is 1. Let val = Evaluate(<expr>) 2. Add the binding (<var> val) to the global environment 3. Return void (= has no return value) 18 global env. variable value milion pi size 6 global env. : variable => value

19 L1 - Syntax Atomic expressions 1. Number literal expression 0, 1, 2, 2. Boolean literal expression #t, #f 3. Primitive operation expression +, -, *, /, <, >, = 4. Variable expression size, my-secret-num, Compound expressions 5. special compound expressions (define <var> <expr>) 6. Non-special compound expressions (expr 0 expr 1 expr n ) 19

20 L1 - examples (define size 6) (define pi 3.14) (define area (* (* size size) pi)) area (+ area (* 2 3)) In the evaluation of define the expr is evaluated variable value size 6 pi area

21 L1 - Semantics Atomic expressions 1. Number literal expression 0, 1, 2, 2. Boolean literal expression #t, #f 3. Primitive operation expression +, -, *, /, <, >, = 4. Variable expression size, secret-num, Compound expressions 5. special compound expressions (define <var> <expr>) Lookup the variable in the env. Return the corresponding value special form 6. Non-special compound expressions (expr 0 expr 1 expr n ) non-special form 21 Evaluate: variable => value Need an evaluation rule for each of our constructs

22 special vs. non-special forms Two types of compound expressions (expr 0 expr 1 expr n ) non-special form all expressions are evaluated (keyword ) special form not all expressions are evaluate first expr is assumed to be a procedure first expr is a keyword 22 the evaluation rule is the fixed the evaluation rule depends on the keyword

23 Evaluation of Non-Special Forms (expr_0 expr_1 expr_2 expr_n) 1. Evaluate all subexpressions expr_i. The value of expr_0 must be of type Procedure, otherwise the evaluation fails (run-time error). 2. Apply the procedure which is the value of expr_0, to the values of the other subexpressions. 23

24 Evaluation of Non-Special Forms (expr_0 expr_1 expr_2 expr_n) The evaluation rule is recursive (* 3 (+ 2 ( ))) 309 (* 3 ( )) (* 3 103) 24

25 L1 - Syntax Atomic expressions 1. Number literal expression 0, 1, 2, 2. Boolean literal expression #t, #f 3. Primitive operation expression +, -, *, /, <, >, = 4. Variable expression size, my-secret-num, Compound expressions 5. special compound expressions (define <var> <expr>) 6. Non-special compound expressions (expr 0 expr 1 expr n ) 25 What is missing from L1? No conditional expressions No way to define functions (that receive parameters)

26 Defining procedures In Typescript they are called functions, in Scheme procedures. What defines a function? Domain Range Giving it a name or not is a different issue a function associates an element d of the domain to an element r of the range a computable function associates with an element d of the domain some transformation (computation) on d 26

27 Defining procedures Syntax of procedure definition (lambda <parameters> <func-body>) where the syntax for <parameters> is And the syntax for <func-body> is (<param_1> <param_2> <param_n>) <expr_1> <expr_2> <expr_n> 27

28 Defining procedures (lambda <parameters> <func-body>) 28 (lambda (x) (* x x)) (lambda (x y z) (+ x y z)) Such expressions result in a procedure definition Not a procedure application (lambda (x y) (+ (* x x) (* 2 x y) (* y y))) We can think of lambda as the constructor of functions

29 Applying procedures (<proc> <parameters>) ((lambda (x) (* x x)) 3) 9 my-proc ((lambda (x y z) (+ x y z)) 1 2 3) my-proc 6 ((lambda (x y) (+ (* x x) (* 2 x y) (* y y))) 1 2) my-proc 9 In the same way as we apply primitive procedures (+ x y) (proc x y) can be a user-defined procedure 29

30 Note the difference The special lambda form The non-special form (lambda (x) (* x x)) ((lambda (x) (* x x)) 3) #procedure 9 user defined procedure Declaration of an anonymous procedure (same as x => x*x in Typescript) Note the syntactic differences The common way to apply procedure on parameters 30 The result is the value procedure The result is a value

31 Evaluating procedure applications ((lambda (<params>) <expr1> <expr2> <exprn>) <act-params>) The <func-body> can be made of multiple expressions The evaluation rule evaluates all the expressions and returns the value of the last one. What is this useful for? 31

32 What is it useful for? 9 ((lambda (x) (+ x 100) (* x (+ x 329)) (* x x)) 3) ((lambda (x) (display x) (* x x)) 3) 39 Only the last expression affects the returned value Multiple expressions in the body are useful when side-effects are desired ((lambda (x) (display x) (newline) (* x x)) 3) The expression for the returned value should always come last!

33 Naming procedures Every expression in Scheme can be named using the following syntax (define <name> <expr>) (define ten 10) ten 10 (define square (lambda (x) (* x x))) square name expr #<procedure:square> 16 (square 4) 33

34 Naming procedures For naming functions we also have the following (syntactic sugaring) form (define (<name> <parameters>) <body-expr>) (define (square x) (* x x))) (define square square (lambda (x) (* x x))) #<procedure:square> These two forms have exactly the (square 4) same semantics! (define area (* (* size size) pi)) Note the differences with area Had the function referred to a variable which is not local or a parameter, its current value would have been recorded as well! variable value size 6 pi area square #(Closure (x) (* x x)) The closure

35 The if special form (if <predicate> <consequent> <alternative>) (if (< 1 100) "then-part" "else-part") "then-part" (if (< 100 1) "then-part" "else-part") "else-part" (if (< 1 100) (+ 2 2) (* 7 7)) 4 (if (< 100 1) (+ 2 2) (* 7 7)) 35 49

36 The if special form Is the alternative (else-part) evaluated when the condition is (evaluated to) true? (if (< 1 100) (/ 1 0) (* 7 7)) /: division by zero (if (< 1 100) (+ 2 2) (/ 1 0)) 4 36

37 Evaluating the if special form (if <predicate> <consequent> <alternative>) Let p = Evaluate (<predicate>) If p is #t, Evaluate(<consequent>) and return it Otherwise Evaluate(<alternative>) and return it 37 In the if special form one of the expressions is not evaluated Unlike the non-special form in which all expressions are evaluated

38 The cond special form -Examples (cond ((> 1 2) first" ) ((> 2 2) "second") ((> 3 2) third" )) "third" (cond ((> 1 2) first" ) ((> 2 2) "second") (else third" )) "third" (cond ((> 1 2) "first" "FIRST") ((> 2 2) "second" "SECOND") ((> 3 2) "third" "THIRD")) "THIRD" (cond ((> 1 2) (display "first") "FIRST") ((> 2 2) (display "second") "SECOND") ((> 3 2) (display "third") "THIRD")) 38 third"third"

39 The cold special form (cond (<pred 1 > <expr-1 1 > <expr-2 1 > <expr-n 1 >) (<pred 2 > <expr-1 2 > <expr-2 2 > <expr-k 2 >) (<pred n > <expr-1 n > <expr-2 n > <expr-m n >) (else <expr-1 e > <expr-2 e > <expr-l e >)) A syntactic sugaring of if Thus, the only expressions that are evaluated are those corresponding to the first predicate to evaluate to true Only the predicates until the first predicate to evaluate to true are evaluated 39

40 L2 - Example Implementing Newton's method for computing square roots Newton's method is stated as this algorithm: If g is non-zero guess for a square root of x, then (g + x/g) / 2 is a better approximation of x. This algorithm is iterative! Interestingly, we can implement it although we have no construct for iteration 40

41 L2 - Example Auxiliary functions 41

42 L2 - Example As long as the guess is not good enough, improve it improved guess 42 First guess is set to 1 Check the difference between guess*guess and x is small enough Improve the guess according to Newton s method

43 L2 - Syntax Atomic expressions 1. Number literal expression 0, 1, 2, 2. Boolean literal expression #t, #f 3. Primitive operation expression +, -, *, /, <, >, = 4. Variable expression size, my-secret-num, Compound expressions 5. special compound expressions I. (define <var> <expr>) II. (lambda <parameters> <func-body>) III. (if <predicate> <consequent> <alternative>) IV. (cond ) 6. Non-special compound expressions (expr 0 expr 1 expr n ) 43

44 L2 - Semantics not all expressions are evaluated Atomic expressions 1. Number literal expression 0, 1, 2, 2. Boolean literal expression #t, #f 3. Primitive operation expression +, -, *, /, <, >, = 4. Variable expression size, my-secret-num, Compound expressions 5. special compound expressions I. (define <var> <expr>) II. (lambda <parameters> <func-body>) III. (if <predicate> <consequent> <alternative>) IV. (cond ) all are 6. Non-special compound expressions (expr 0 expr 1 expr n ) 44

45 Review of Last Lecture L3 L2 L1 if define cond only primitive ops and types lambda recursive comb. of primitive ops. supports recursive procedures Scheme 45

46 What is missing from L2? Compound values! In Javascript we have: array [] map {} What are the minimal constructs that we need to add in order to obtain array and map capabilities? 46

47 The pair compound data type The pair compound data type combines two values into a single unit. To support this the language provides: A value constructor (called cons) Accessors to take apart a compound pair value: for the first (called car) for the second (called cdr) A type predicate to check whether a value belongs to the set of pair values (called pair?) An equality predicate for pairs (called equal?) 47 To support pairs we add 5 primitive functions: cons, car, cdr, pair?, equal?

48 Pair, and associated types Types of the pair constructor and accessors: cons: [T 1 *T 2 -> Pair(T 1,T 2 )] car: [Pair(T 1,T 2 ) -> T 1 ] cdr: [Pair(T 1,T 2 ) -> T 2 ] pair?: [Any -> boolean] equal?: [Any * Any -> boolean] 48

49 L3 - pair examples (define foo (cons 1 2)) (car foo) 1 (cdr foo) 2 (define bar (cons 1 2)) (equal? foo bar) #t (pair? foo) #t 49 (pair? 2) #f

50 L3 - nesting pairs (define p12 (cons 1 2)) (define p123 (cons p12 3)) (define p1234 (cons p123 4)) (define p12345 (cons p1234 5)) p12345 '((((1. 2). 3). 4). 5) (car p12345) '(((1. 2). 3). 4) 3 (cdr (car (car p12345))) (cdr p12345) 5 Syntactic sugaring 3 (cdaar p12345))) 50 Can we use this to implement lists? What about the empty list???

51 Lists Using literals A list is an inductive data type. The base case is the empty list The inductive case creates a non-empty list by combining an element with a list (define L1234 '( ))) L1234 E1234 '( ) '( ) Using the list constructor (define E1234 (list ))) 51 (define Lemp '()) Lemp '() (define Eemp (list)) Eemp '() (define C1234 (cons 1 (cons 2 (cons 3 (cons 4 ())))))) C1234 '( ) Using the pair constructor

52 Lists Lists can indeed be implemented using pairs and the empty list (and in fact are implemented this way in scheme) But the language also offers special constructs to deal with lists: A list constructor : (list <el1> <eln>) A literal list : (<el1> <eln>) Accessors first, second, third, A type predicate to check whether a value belongs to the set of list values (called list?) An emptiness predicate for list (called empty?) 52

53 functions on list : length (define length (lambda (l) (if (empty? l) 0 (+ 1 (length (cdr l)))))) Recursive functions operating on inductive data types follow their inductive definition 53

54 functions on list : n-th (define nth (lambda (n l) (cond [(empty? l) '()] [(= n 0) (car l)] [else (nth (- n 1) (cdr l))]))) 54 (nth 2 '( )) 3 What happened here? (nth 8 '( )) '() (nth 0 '( )) 1

55 functions on list : n-th (define nth (lambda (n l) (cond [(empty? l) '()] [(= n 0) (car l)] [else (display (list "n:" n " l:" l)) (newline) (nth (- n 1) (cdr l))]))) Recall: the debug printing must precede the last expression, which is the returned value (nth 8 '( )) 55 (n: 8 l: ( )) (n: 7 l: (2 3 4)) (n: 6 l: (3 4)) (n: 5 l: (4)) '()

56 L3 - Syntax Atomic expressions 1. Number literal expression 0, 1, 2, 2. Boolean literal expression #t, #f 3. Primitive operation expression +, -, *, /, <, >, =, cons, car, cdr, pair?, list?, empty? 4. Variable expression size, my-secret-num, 5. The empty list literal expression `() Compound expressions 6. special compound expressions I. (define <var> <expr>) II. (lambda <parameters> <func-body>) III. (if <predicate> <consequent> <alternative>) IV. (cond ) 7. Non-special compound expressions (expr 0 expr 1 expr n ) 56

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Lesson 14 Type Checking Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Type Checking We return to the issue of type safety we discussed informally,

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

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt

Procedural abstraction SICP Data abstractions. The universe of procedures forsqrt. Procedural abstraction example: sqrt Data abstractions Abstractions and their variations Basic data abstractions Why data abstractions are useful Procedural abstraction Process of procedural abstraction Define formal parameters, capture process

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

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Collaboration and Management Dana Fisman Lesson 2 - Types with TypeScript 1 Types What are types in programming languages? What types are you

More information

An introduction to Scheme

An introduction to Scheme An introduction to Scheme Introduction A powerful programming language is more than just a means for instructing a computer to perform tasks. The language also serves as a framework within which we organize

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

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Slides by Dana Fisman based on book by Mira Balaban and lecuture notes by Michael Elhadad Lesson 15 Type Inference Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172

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 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

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

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

11/6/17. Functional programming. FP Foundations, Scheme (2) LISP Data Types. LISP Data Types. LISP Data Types. Scheme. LISP: John McCarthy 1958 MIT Functional programming FP Foundations, Scheme (2 In Text: Chapter 15 LISP: John McCarthy 1958 MIT List Processing => Symbolic Manipulation First functional programming language Every version after the

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

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

SCHEME AND CALCULATOR 5b

SCHEME AND CALCULATOR 5b SCHEME AND CALCULATOR 5b COMPUTER SCIENCE 6A July 25, 203 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

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

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

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

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Collaboration and Management Dana Fisman Lesson 5 - Data Types and Operations on Data 1 Types - what we already know Types define sets of values

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

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples

Introduction to Typed Racket. The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Introduction to Typed Racket The plan: Racket Crash Course Typed Racket and PL Racket Differences with the text Some PL Racket Examples Getting started Find a machine with DrRacket installed (e.g. the

More information

Chapter 1. Fundamentals of Higher Order Programming

Chapter 1. Fundamentals of Higher Order Programming Chapter 1 Fundamentals of Higher Order Programming 1 The Elements of Programming Any powerful language features: so does Scheme primitive data procedures combinations abstraction We will see that Scheme

More information

Administrivia. Simple data types

Administrivia. Simple data types Administrivia Lists, higher order procedures, and symbols 6.037 - Structure and Interpretation of Computer Programs Mike Phillips (mpp) Massachusetts Institute of Technology Project 0 was due today Reminder:

More information

User-defined Functions. Conditional Expressions in Scheme

User-defined Functions. Conditional Expressions in Scheme User-defined Functions The list (lambda (args (body s to a function with (args as its argument list and (body as the function body. No quotes are needed for (args or (body. (lambda (x (+ x 1 s to the increment

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages www.cs.bgu.ac.il/~ppl172 Lesson 4 - Type Checking Collaboration and Management Dana Fisman 1 Why declare types? Declaring types allows the compiler to detect errors

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

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

CSCC24 Functional Programming Scheme Part 2

CSCC24 Functional Programming Scheme Part 2 CSCC24 Functional Programming Scheme Part 2 Carolyn MacLeod 1 winter 2012 1 Based on slides from Anya Tafliovich, and with many thanks to Gerald Penn and Prabhakar Ragde. 1 The Spirit of Lisp-like Languages

More information

Typed Scheme: Scheme with Static Types

Typed Scheme: Scheme with Static Types Typed Scheme: Scheme with Static Types Version 4.1.1 Sam Tobin-Hochstadt October 5, 2008 Typed Scheme is a Scheme-like language, with a type system that supports common Scheme programming idioms. Explicit

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

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

Chapter 15 Functional Programming Languages

Chapter 15 Functional Programming Languages Chapter 15 Functional Programming Languages Fundamentals of Functional Programming Languages Introduction to Scheme A programming paradigm treats computation as the evaluation of mathematical functions.

More information

Typed Racket: Racket with Static Types

Typed Racket: Racket with Static Types Typed Racket: Racket with Static Types Version 5.0.2 Sam Tobin-Hochstadt November 6, 2010 Typed Racket is a family of languages, each of which enforce that programs written in the language obey a type

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

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 7a Andrew Tolmach Portland State University 1994-2016 Values and Types We divide the universe of values according to types A type is a set of values and a

More information

Denotational Semantics. Domain Theory

Denotational Semantics. Domain Theory Denotational Semantics and Domain Theory 1 / 51 Outline Denotational Semantics Basic Domain Theory Introduction and history Primitive and lifted domains Sum and product domains Function domains Meaning

More information

6.001 Notes: Section 6.1

6.001 Notes: Section 6.1 6.001 Notes: Section 6.1 Slide 6.1.1 When we first starting talking about Scheme expressions, you may recall we said that (almost) every Scheme expression had three components, a syntax (legal ways of

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

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

Introduction to Functional Programming

Introduction to Functional Programming Introduction to Functional Programming Xiao Jia xjia@cs.sjtu.edu.cn Summer 2013 Scheme Appeared in 1975 Designed by Guy L. Steele Gerald Jay Sussman Influenced by Lisp, ALGOL Influenced Common Lisp, Haskell,

More information

Turtles All The Way Down

Turtles All The Way Down Turtles All The Way Down Bertrand Russell had just finished giving a public lecture on the nature of the universe. An old woman said Prof. Russell, it is well known that the earth rests on the back of

More information

CSc 520 Principles of Programming Languages

CSc 520 Principles of Programming Languages CSc 520 Principles of Programming Languages 9: Scheme Metacircular Interpretation Christian Collberg collberg@cs.arizona.edu Department of Computer Science University of Arizona Copyright c 2005 Christian

More information

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018

Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters. Corky Cartwright January 26, 2018 Comp 411 Principles of Programming Languages Lecture 7 Meta-interpreters Corky Cartwright January 26, 2018 Denotational Semantics The primary alternative to syntactic semantics is denotational semantics.

More information

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP)

Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Fundamentals of Artificial Intelligence COMP221: Functional Programming in Scheme (and LISP) Prof. Dekai Wu Department of Computer Science and Engineering The Hong Kong University of Science and Technology

More information

6.001: Structure and Interpretation of Computer Programs

6.001: Structure and Interpretation of Computer Programs 6.001: Structure and Interpretation of Computer Programs Symbols Quotation Relevant details of the reader Example of using symbols Alists Differentiation Data Types in Lisp/Scheme Conventional Numbers

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

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

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Winter 2003 February 10, 2003 1 Introduction This document is a quick reference guide to common features of the Scheme language. It is by no means intended to be a complete

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

Principles of Programming Languages

Principles of Programming Languages Ben-Gurion University of the Negev Faculty of Natural Science Department of Computer Science Mira Balaban Lecture Notes April 9, 2013 Many thanks to Tamar Pinhas, Azzam Maraee, Ami Hauptman, Eran Tomer,

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

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

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

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns

Basic Scheme February 8, Compound expressions Rules of evaluation Creating procedures by capturing common patterns Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns Previous lecture Basics of Scheme Expressions and associated values (or syntax and

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

Project 2: Scheme Interpreter

Project 2: Scheme Interpreter Project 2: Scheme Interpreter CSC 4101, Fall 2017 Due: 12 November 2017 For this project, you will implement a simple Scheme interpreter in C++ or Java. Your interpreter should be able to handle the same

More information

Principles of Programming Languages

Principles of Programming Languages Ben-Gurion University of the Negev Faculty of Natural Science Department of Computer Science Mira Balaban Lecture Notes March 29, 2017 Many thanks to Tamar Pinhas, Ami Hauptman, Eran Tomer, Barak Bar-Orion,

More information

Functional Programming Languages (FPL)

Functional Programming Languages (FPL) Functional Programming Languages (FPL) 1. Definitions... 2 2. Applications... 2 3. Examples... 3 4. FPL Characteristics:... 3 5. Lambda calculus (LC)... 4 6. Functions in FPLs... 7 7. Modern functional

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

Scheme Quick Reference

Scheme Quick Reference Scheme Quick Reference COSC 18 Fall 2003 This document is a quick reference guide to common features of the Scheme language. It is not intended to be a complete language reference, but it gives terse summaries

More information

Programming Languages: Application and Interpretation

Programming Languages: Application and Interpretation Programming Languages: Application and Interpretation Version 6.7 October 26, 2016 This is the documentation for the software accompanying the textbook Programming Languages: Application and Interpretation

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

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

6.001 Notes: Section 8.1

6.001 Notes: Section 8.1 6.001 Notes: Section 8.1 Slide 8.1.1 In this lecture we are going to introduce a new data type, specifically to deal with symbols. This may sound a bit odd, but if you step back, you may realize that everything

More information

An Introduction to Scheme

An Introduction to Scheme An Introduction to Scheme Stéphane Ducasse stephane.ducasse@inria.fr http://stephane.ducasse.free.fr/ Stéphane Ducasse 1 Scheme Minimal Statically scoped Functional Imperative Stack manipulation Specification

More information

Principles of Programming Languages

Principles of Programming Languages Principles of Programming Languages Slides by Yaron Gonen and Dana Fisman Based on Book by Mira Balaban and Lesson 20 Lazy Lists Collaboration and Management Dana Fisman www.cs.bgu.ac.il/~ppl172 1 Lazy

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

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

Building a system for symbolic differentiation

Building a system for symbolic differentiation Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

More information

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams

6.037 Lecture 7B. Scheme Variants Normal Order Lazy Evaluation Streams 6.037 Lecture 7B Scheme Variants Normal Order Lazy Evaluation Streams Edited by Mike Phillips & Ben Vandiver Original Material by Eric Grimson & Duane Boning Further Variations on a Scheme Beyond Scheme

More information

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

Lambda Calculus and Lambda notation in Lisp II. Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky. λ Calculus Basis Lambda Calculus and Lambda notation in Lisp II Based on Prof. Gotshalks notes on Lambda Calculus and Chapter 9 in Wilensky Mathematical theory for anonymous functions» functions that have

More information

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

Announcements. The current topic: Scheme. Review: BST functions. Review: Representing trees in Scheme. Reminder: Lab 2 is due on Monday at 10:30 am. The current topic: Scheme! Introduction! Object-oriented programming: Python Functional programming: Scheme! Introduction! Numeric operators, REPL, quotes, functions, conditionals! Function examples, helper

More information

Functional programming with Common Lisp

Functional programming with Common Lisp Functional programming with Common Lisp Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada August 11, 2016 1 / 81 Expressions and functions

More information

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

Scheme Tutorial. Introduction. The Structure of Scheme Programs. Syntax Scheme Tutorial Introduction Scheme is an imperative language with a functional core. The functional core is based on the lambda calculus. In this chapter only the functional core and some simple I/O is

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

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 17: Functional Programming Zheng (Eddy Zhang Rutgers University April 4, 2018 Class Information Homework 6 will be posted later today. All test cases

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

Recap: Functions as first-class values

Recap: Functions as first-class values Recap: Functions as first-class values Arguments, return values, bindings What are the benefits? Parameterized, similar functions (e.g. Testers) Creating, (Returning) Functions Iterator, Accumul, Reuse

More information

TAIL RECURSION, SCOPE, AND PROJECT 4 11

TAIL RECURSION, SCOPE, AND PROJECT 4 11 TAIL RECURSION, SCOPE, AND PROJECT 4 11 COMPUTER SCIENCE 61A Noveber 12, 2012 1 Tail Recursion Today we will look at Tail Recursion and Tail Call Optimizations in Scheme, and how they relate to iteration

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Winter 2017 Lecture 7b Andrew Tolmach Portland State University 1994-2017 Values and Types We divide the universe of values according to types A type is a set of values and

More information

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics

CMSC 330: Organization of Programming Languages. Formal Semantics of a Prog. Lang. Specifying Syntax, Semantics Recall Architecture of Compilers, Interpreters CMSC 330: Organization of Programming Languages Source Scanner Parser Static Analyzer Operational Semantics Intermediate Representation Front End Back End

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

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2010 Handout Decaf Language Tuesday, Feb 2 The project for the course is to write a compiler

More information

Building a system for symbolic differentiation

Building a system for symbolic differentiation Computer Science 21b Structure and Interpretation of Computer Programs Building a system for symbolic differentiation Selectors, constructors and predicates: (constant? e) Is e a constant? (variable? e)

More information

Lexical Considerations

Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Fall 2005 Handout 6 Decaf Language Wednesday, September 7 The project for the course is to write a

More information

An Explicit Continuation Evaluator for Scheme

An Explicit Continuation Evaluator for Scheme Massachusetts Institute of Technology Course Notes 2 6.844, Spring 05: Computability Theory of and with Scheme February 17 Prof. Albert R. Meyer revised March 3, 2005, 1265 minutes An Explicit Continuation

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

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures

CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures CSE413: Programming Languages and Implementation Racket structs Implementing languages with interpreters Implementing closures Dan Grossman Fall 2014 Hi! I m not Hal J I love this stuff and have taught

More information

n n Try tutorial on front page to get started! n spring13/ n Stack Overflow!

n   n Try tutorial on front page to get started! n   spring13/ n Stack Overflow! Announcements n Rainbow grades: HW1-6, Quiz1-5, Exam1 n Still grading: HW7, Quiz6, Exam2 Intro to Haskell n HW8 due today n HW9, Haskell, out tonight, due Nov. 16 th n Individual assignment n Start early!

More information

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan

CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan CS 342 Lecture 8 Data Abstraction By: Hridesh Rajan 1 Com S 342 so far So far we have studied: Language design goals, Basic functional programming, Flat recursion over lists, Notion of Scheme procedures

More information

The PCAT Programming Language Reference Manual

The PCAT Programming Language Reference Manual The PCAT Programming Language Reference Manual Andrew Tolmach and Jingke Li Dept. of Computer Science Portland State University September 27, 1995 (revised October 15, 2002) 1 Introduction The PCAT language

More information

CPS 506 Comparative Programming Languages. Programming Language Paradigm

CPS 506 Comparative Programming Languages. Programming Language Paradigm CPS 506 Comparative Programming Languages Functional Programming Language Paradigm Topics Introduction Mathematical Functions Fundamentals of Functional Programming Languages The First Functional Programming

More information

1 Lexical Considerations

1 Lexical Considerations Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science 6.035, Spring 2013 Handout Decaf Language Thursday, Feb 7 The project for the course is to write a compiler

More information

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

SCHEME 7. 1 Introduction. 2 Primitives COMPUTER SCIENCE 61A. October 29, 2015 SCHEME 7 COMPUTER SCIENCE 61A October 29, 2015 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

The Typed Racket Guide

The Typed Racket Guide The Typed Racket Guide Version 5.3.6 Sam Tobin-Hochstadt and Vincent St-Amour August 9, 2013 Typed Racket is a family of languages, each of which enforce

More information

Homework 3 COSE212, Fall 2018

Homework 3 COSE212, Fall 2018 Homework 3 COSE212, Fall 2018 Hakjoo Oh Due: 10/28, 24:00 Problem 1 (100pts) Let us design and implement a programming language called ML. ML is a small yet Turing-complete functional language that supports

More information

Lecture #24: Programming Languages and Programs

Lecture #24: Programming Languages and Programs Lecture #24: Programming Languages and Programs A programming language is a notation for describing computations or processes. These range from low-level notations, such as machine language or simple hardware

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

15 Unification and Embedded Languages in Lisp

15 Unification and Embedded Languages in Lisp 15 Unification and Embedded Languages in Lisp Chapter Objectives Chapter Contents Pattern matching in Lisp: Database examples Full unification as required for Predicate Calculus problem solving Needed

More information

Programming Languages

Programming Languages Programming Languages Tevfik Koşar Lecture - XIII March 2 nd, 2006 1 Roadmap Functional Languages Lambda Calculus Intro to Scheme Basics Functions Bindings Equality Testing Searching 2 1 Functional Languages

More information