6.001 SICP 1 6.001 SICP – September 29 6001-Types and HOPs Trevor Darrell 32-D512 Office Hour: W 11 6.001 web page:

Slides:



Advertisements
Similar presentations
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 5 Functions for All Subtasks.
Advertisements

מבוא מורחב 1 Lecture #6. מבוא מורחב 2 Primality testing (application) Know how to test whether n is prime in  (log n) time => Can easily find very large.
CMPT 354, Simon Fraser University, Fall 2008, Martin Ester 52 Database Systems I Relational Algebra.
6.001 SICP SICP – September Introduction Trevor Darrell 32-D web page:
6.001 SICP SICP – October Trees Trevor Darrell 32-D512 Office Hour: W web page:
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
6.001 SICP SICP – September Processes, Substitution, Recusion, and Iteration Trevor Darrell 32-D web page:
1 Scheme Scheme is a functional language. Scheme is based on lambda calculus. lambda abstraction = function definition In Scheme, a function is defined.
6.001 SICP SICP – October environment Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP – September Introduction
Lecture 2: Fundamental Concepts
1 Module 2: Fundamental Concepts Problems Programs –Programming languages.
6.001 SICP SICP – October HOPs, Lists, Trees Trevor Darrell 32-D512 Office Hour: W web page:
6.001 SICP SICP – September ? 6001-Introduction Trevor Darrell 32-D web page: section.
מבוא מורחב - שיעור 2 1 Lecture 2 - Substitution Model (continued) - Recursion - Block structure and scope (if time permits)
SICP Interpretation part 1 Parts of an interpreter Arithmetic calculator Names Conditionals and if Store procedures in the environment.
1 Scheme Although Scheme is syntactically a simple language, it supports sophisticated modeling: higher-order functions are a powerful tool in describing.
CSE S. Tanimoto Lambda Calculus 1 Lambda Calculus What is the simplest functional language that is still Turing complete? Where do functional languages.
Principles of Programming Languages Lecture 1 Slides by Daniel Deutch, based on lecture notes by Prof. Mira Balaban.
Which program is better? Why? (define (prime? n) (= n (smallest-divisor n))) (define (smallest-divisor n) (find-divisor n 2)) (define (find-divisor n d)
SICP Interpretation Parts of an interpreter Arithmetic calculator Names Conditionals and if Storing procedures in the environment Environment as.
Functions, Procedures, and Abstraction Dr. José M. Reyes Álamo.
1 The Evaluator. 2 Compiler vs. Interpreter Command Processing Unit The Computer Program in Low Level Machine Language Program in High Level Language.
Python Functions.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
What is the main focus of this course? This course is about Computer Science Geometry was once equally misunderstood. Term comes from ghia & metra or earth.
1 Lecture 14: Assignment and the Environment Model (EM)
1/32 This Lecture Substitution model An example using the substitution model Designing recursive procedures Designing iterative procedures Proving that.
Lecture on Set! And Local CS 2135 Copyright Kathi Fisler, 2002 This material requires Advanced Language Level.
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
Spring 2008Programming Development Techniques 1 Topic 5.5 Higher Order Procedures (This goes back and picks up section 1.3 and then sections in Chapter.
CMSC 330: Organization of Programming Languages Operational Semantics.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
מבוא מורחב - שיעור 5 1 Lecture 5 Higher-order procedures.
1 Topic #3: Lambda CSE 413, Autumn 2007 Programming Languages.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Operational Semantics of Scheme
Lecture #5 מבוא מורחב.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Edited by Original material by Eric Grimson
Carlos Varela Rennselaer Polytechnic Institute September 5, 2017
September 4, 1997 Programming Languages (CS 550) Lecture 6 Summary Operational Semantics of Scheme using Substitution Jeremy R. Johnson TexPoint fonts.
Material in the textbook on pages
Subroutines Idea: useful code can be saved and re-used, with different data values Example: Our function to find the largest element of an array might.
Higher-Order Procedures
Functions, Procedures, and Abstraction
Lecture #5 מבוא מורחב.
The Metacircular Evaluator
Functional Programming
This Lecture Substitution model
The Metacircular Evaluator
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 12/25/2018.
6.001 SICP Environment model
The Metacircular Evaluator (Continued)
Streams, Delayed Evaluation and a Normal Order Interpreter
Lecture 13 - Assignment and the environments model Chapter 3
What would be our focus ? Geometry deals with Declarative or “What is” knowledge. Computer Science deals with Imperative or “How to” knowledge 2/23/2019.
6.001 SICP Environment model
6.001 SICP Explicit-control evaluator
6.001 SICP Variations on a Scheme
Lecture 5 Higher-order procedures מבוא מורחב - שיעור 5.
This Lecture Substitution model
6.001 SICP Interpretation Parts of an interpreter
6.001 SICP Environment model
Lecture 3: Rules of Evaluation CS200: Computer Science
CSE S. Tanimoto Lambda Calculus
Introduction to the Lab
This Lecture Substitution model
Good programming practices
Carlos Varela Rennselaer Polytechnic Institute September 6, 2019
Presentation transcript:

6.001 SICP SICP – September Types and HOPs Trevor Darrell 32-D512 Office Hour: W web page: section web page: Types Higher-order procedures

6.001 SICP 2 expression:evaluates to a value of type: 15number "hi"string squarenumber  number >number,number  boolean (> 5 4) ==> #t The type of a procedure is a contract: If the operands have the specified types, the procedure will result in a value of the specified type otherwise, its behavior is undefined –maybe an error, maybe random behavior Type examples

6.001 SICP 3 Types, precisely A type describes a set of scheme values number  number describes the set: all procedures, whose result is a number, which require one argument that must be a number Every scheme value has a type Some values can be described by multiple types If so, choose the type which describes the largest set Special form keywords like define do not name values therefore special form keywords have no type

6.001 SICP 4 Procedure Types procedure types (types which include  ) indicate number of arguments required type of each argument type of result of the procedure Types: a mathematical theory for reasoning efficiently about programs useful for preventing certain common types of errors basis for many analysis and optimization algorithms

6.001 SICP 5 Generic Type Variables Consider: (define identity (lambda (x) x)) (identity 10) ==> 10, etc…. what is it’s type? number -> number ? string -> string ? We use Generic Type Variables when any type is possible: The correct type of identity is: A -> A

6.001 SICP 6 Example Abstraction using Higher-order procedures Basic idea: use procedural abstraction to capture regular patterns: (* 2 8) (* 2 29) (* 2 55) (double 8) (double 29) (double 55) (define double (lambda (x) (* 2 x))) (* 3 8) (* 3 29) (* 3 55) (triple 8) (triple 29) (triple 55) (define triple (lambda (x) (* 3 x)))

6.001 SICP 7 (define double (lambda (x) (* 2 x))) (define triple (lambda (x) (* 3 x))) (define double (make-mult 2)) (define triple (make-mult 3)) (define make-mult (lambda (n) (lambda (x) (* n x)))) Example Abstraction using Higher-order procedures

6.001 SICP 8 Define a procedure swap that takes a single argument whose type is a function that takes two arguments, and returns a function that acts as the input function but with its argument positions swapped. E.g., (- 5 4)  1, ((swap -) 5 4)  -1. (define swap (lambda (f) (lambda (x y) (f y x)))) Example Abstraction using Higher-order procedures

6.001 SICP 9 Define a composing function Write a procedure composef that takes two arguments (eg. f and g), whose type is a function, and returns a function that first apply g to the input, then f to the result – i.e. ((composef f g) x) should be the same as (f (g x)). (define composef (lambda (f g) (lambda (x) (f (g x)))) )

6.001 SICP 10 composef example (define composef (lambda (f g) (lambda (x) (f (g x)))) ) ((composef double cube) 3) (((lambda (f g) (lambda (x) (f (g x)))) double cube) 3) ;; if double and cube were primitive: ((lambda (x) (double (cube x))) 3) (double (cube 3)) (double 27) 54

6.001 SICP 11 composef example (define composef (lambda (f g) (lambda (x) (f (g x)))) ) ((composef double cube) 3) (((lambda (f g) (lambda (x) (f (g x)))) double cube) 3) ;; now expand double and cube: (((lambda (f g) (lambda (x) (f (g x)))) (lambda (x) (* x 2)) (lambda (x) (* x x x))) 3) ;; continue on next slide

6.001 SICP 12 composef example (((lambda (f g) (lambda (x) (f (g x)))) (lambda (x) (* x 2)) (lambda (x) (* x x x))) 3) ;; note how confusing it is to have many different lambdas all with the same parameter name “x” ;; we can use the “lambda parameter renaming trick”, and rename some of the x’s to unused names when we copy in double and cube: (((lambda (f g) (lambda (x) (f (g x)))) (lambda (y) (* y 2)) (lambda (z) (* z z z))) 3) ;; now turn the crank! apply the composef lambda to the double and cube lambdas: ;; here’s the body of composef lambda: (lambda (x) (f (g x))) ;; f and g are replaced with the double and cube definitions, so ((lambda (x) ((lambda (y) (* y 2)) ((lambda (z) (* z z z)) x))) 3 ) ;; now apply the composed lambda ((lambda (y) (* y 2)) ((lambda (z) (* z z z)) 3)) ((lambda (y) (* y 2)) (* 3 3 3)) ((lambda (y) (* y 2)) 27) (* 27 2) 54 ;;!!!

6.001 SICP 13 Type of composef (define composef (lambda (f g) (lambda (x) (f (g x))))) (define myfunc (composef square double)) (myfunc 3) ==> 36 The value of a lambda expression is a procedure The body of composef is a lambda expression Therefore, the result value of composef is a procedure

6.001 SICP 14 Type of composef The result value of composef is a procedure composef: ?…  (?  ?) It has two arguments, both procedures composef: (?  ?),(?  ?)  (?  ?) The first arguent is the function f(), the second g(), and composef computes f(g(x)), so we realize that the output type of f is the output type of the composed function composef: (?  C),(?  ?)  (?  C) Similarly the input is the same as the input of g composef: (?  C),(A  ?)  (A  C) and the output of g goes into f! composef: (B  C),(A  B)  (A  C) ;; done!

6.001 SICP 15 Remember… calendar…