(Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 AM, Oct 27, 2017

Similar documents
(Provisional) Lecture 08: List recursion and recursive diagrams 10:00 AM, Sep 22, 2017

(Provisional) Lecture 20: OCaml Fun!

Homework 6: Higher-Order Procedures Due: 10:00 PM, Oct 17, 2017

Lab 7: OCaml 12:00 PM, Oct 22, 2017

Lecture 27: (PROVISIONAL) Insertion Sort, Selection Sort, and Merge Sort 10:00 AM, Nov 8, 2017

Homework 6: Higher-Order Procedures Due: 11:59 PM, Oct 16, 2018

Lab 10: OCaml sequences, comparators, stable sorting 12:00 PM, Nov 12, 2017

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

(Provisional) Lecture 32: Estimated Value, Minimax, Functional Data 10:00 AM, Nov 20, 2017

The Design Recipe Fall 2017

CMSC 330: Organization of Programming Languages. Operational Semantics

Homework 8: Matrices Due: 11:59 PM, Oct 30, 2018

Project 2: Scheme Interpreter

Homework 7: Subsets Due: 11:59 PM, Oct 23, 2018

The Environment Model. Nate Foster Spring 2018

Tail Calls. CMSC 330: Organization of Programming Languages. Tail Recursion. Tail Recursion (cont d) Names and Binding. Tail Recursion (cont d)

Racket Style Guide Fall 2017

RACKET BASICS, ORDER OF EVALUATION, RECURSION 1

Homework 3: Recursion Due: 11:59 PM, Sep 25, 2018

6.001 Notes: Section 15.1

Due: 9 February 2017 at 1159pm (2359, Pacific Standard Time)

The Environment Model

Lecture 21: Red-Black Trees

The Design Recipe Fall 2018

Lecture 1: Overview

Lecture 23: Priority Queues 10:00 AM, Mar 19, 2018

Lecture 19: Signatures, Structures, and Type Abstraction

CS2112 Fall Assignment 4 Parsing and Fault Injection. Due: March 18, 2014 Overview draft due: March 14, 2014

(Refer Slide Time: 01.26)

CS Introduction to Data Structures How to Parse Arithmetic Expressions

CSE 413 Languages & Implementation. Hal Perkins Winter 2019 Structs, Implementing Languages (credits: Dan Grossman, CSE 341)

Lab 1: Setup 12:00 PM, Sep 10, 2017

Have the students look at the editor on their computers. Refer to overhead projector as necessary.

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

An Introduction to Functions

1 Leaffix Scan, Rootfix Scan, Tree Size, and Depth

The Substitution Model

Lecture 3: Recursion; Structural Induction

Lecture 7: Lists, Version 2 (with Mutation)

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Priority Queues / Heaps Date: 9/27/17

Types, Expressions, and States

Lab 10: Sockets 12:00 PM, Apr 4, 2018

Lecture 5: Implementing Lists, Version 1

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

Module 9: Trees. If you have not already, make sure you. Read How to Design Programs Sections 14, 15, CS 115 Module 9: Trees

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

Section 05: Solutions

Solving Linear Recurrence Relations (8.2)

C++ for Java Programmers

OCaml Style Guide Fall 2017

/633 Introduction to Algorithms Lecturer: Michael Dinitz Topic: Sorting lower bound and Linear-time sorting Date: 9/19/17

If Statements, For Loops, Functions

Functional abstraction. What is abstraction? Eating apples. Readings: HtDP, sections Language level: Intermediate Student With Lambda

Functional abstraction

Lecture 7. Binary Search Trees and Red-Black Trees

(Refer Slide Time: 4:00)

Scribe: Virginia Williams, Sam Kim (2016), Mary Wootters (2017) Date: May 22, 2017

CSCI0170. Today s topics. Predicates Natural Number recursion Recursion Diagrams List recursion A first glance at the design recipe

CS115 - Module 10 - General Trees

CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures. Dan Grossman Autumn 2018

Class Structure. Prerequisites

Algebraic Specifications

Lecture 14: Exceptions 10:00 AM, Feb 26, 2018

SCHEME AND CALCULATOR 5b

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

Binary Search Trees. Carlos Moreno uwaterloo.ca EIT

Lab 4: Imperative & Debugging 12:00 PM, Feb 14, 2018

CS61A Notes Week 6: Scheme1, Data Directed Programming You Are Scheme and don t let anyone tell you otherwise

Project 4 Due 11:59:59pm Thu, May 10, 2012

Lecture 6: Sequential Sorting

The Substitution Model. Nate Foster Spring 2018

Formal Semantics. Prof. Clarkson Fall Today s music: Down to Earth by Peter Gabriel from the WALL-E soundtrack

Lecture 8: Iterators and More Mutation

Lecture 10 Parsing 10.1

9 R1 Get another piece of paper. We re going to have fun keeping track of (inaudible). Um How much time do you have? Are you getting tired?

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

CS 360 Programming Languages Interpreters

Difference Between Dates Case Study 2002 M. J. Clancy and M. C. Linn

News. Programming Languages. Recap. Recap: Environments. Functions. of functions: Closures. CSE 130 : Fall Lecture 5: Functions and Datatypes

Repetition Through Recursion

Introduction to Haskell

6.001 Notes: Section 6.1

Lecture 7: Implementing Lists, Version 2

Typing Control. Chapter Conditionals

Maps; Binary Search Trees

In Our Last Exciting Episode

CS103 Handout 50 Fall 2018 November 30, 2018 Problem Set 9

Typical workflow. CSE341: Programming Languages. Lecture 17 Implementing Languages Including Closures. Reality more complicated

CS61C : Machine Structures

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

15-451/651: Design & Analysis of Algorithms November 4, 2015 Lecture #18 last changed: November 22, 2015

An Explicit Continuation Evaluator for Scheme

Recursively Enumerable Languages, Turing Machines, and Decidability

Programming Languages

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

Divisibility Rules and Their Explanations

Note that this is a rep invariant! The type system doesn t enforce this but you need it to be true. Should use repok to check in debug version.

Lecture 2: SML Basics

Section 05: Solutions

CSE 241 Class 17. Jeremy Buhler. October 28, Ordered collections supported both, plus total ordering operations (pred and succ)

Transcription:

Integrated Introduction to Computer Science Hughes (Provisional) Lecture 22: Rackette Overview, Binary Tree Analysis 10:00 Contents 1 Announcements 1 2 An OCaml Debugging Tip 1 3 Introduction to Rackette 1 3.1 Parsing........................................... 2 3.2 Processing/Evaluation................................... 3 3.3 Environments, Definitions, and Identifiers........................ 3 4 Balanced Binary Trees and Big-O 4 5 Summary 5 Objectives By the end of this lecture, you will: ˆ have a broad overview of the Rackette project ˆ have analyzed the contains7? procedure 1 Announcements This upcoming Friday you ll submit Eliza, after which you ll be assigned a new project, Rackette. In this lecture, we ll introduce many of the concepts you ll need to tackle to successfully complete Rackette. 2 An OCaml Debugging Tip Unlike Racket, OCaml doesn t come with a built-in interaction window. Instead, to see if our code is working, we compile our OCaml program through the terminal. As you ve probably seen, it will spit out some text, as well as error messages if applicable. However, OCaml s error messages often seem jumbled and difficult to read, especially when we re trying to implement many different procedures in a single.ml file.

To isolate errors, we can instead run the ocaml command in the terminal, allowing us to use the interactive version of OCaml, directly in the terminal. We can now copy and paste one chunk of our code in at a time (likely one procedure at a time), running each in the terminal separately, and seeing which errors it generates on its own. If something goes wrong, the OCaml program will even underline for you the place where it first got confused. This is a useful debugging technique because we can better pinpoint what s going wrong in our code, or at least get a clearer sense of where to start looking. 3 Introduction to Rackette Most of today s lecture notes will be on Rackette. There are two reasons we need to start this. You re going to see how some of what you ll doing in Eliza will be relevant for the next project. Also, you have a lab this weekend that does a little part of Rackette as a warmup. We want to give you an introdution in class first. Let s start with the main rackette program. The type signature for the main rackette procedure will be: rackette: string ->string. It will take in Racket code in the form of a string, and output what the interactions pane of DrRacket would output, likewise in the form of a string. A typical input to Rackette may be: (define (f x) (+ x 1)) (f 3) Note, however, you are writing a Rackette interpeter, not a Racket interpreter; there are some simplifications in Rackette. For example, the only numbers are integers. You don t have to include let* (but it s fun!). You also don t have to include every built-in that we ve used in Racket. Built-ins such as + need to only take two arguments in your implementation. You will implement if-expressions, cond-expressions, lambda-expressions, and procedure-application expressions. There will probably be more simplifications that you ll see later. The first step in breaking down something that takes in text as input is a process calling parsing. 3.1 Parsing Parsing involves taking a sentence or a program and identifying the major parts. In the case of a sentence, you might identify the subject and predicate, for example. In Rackette, parsing means looking at the input text and seeing whether it matches the BNF for Rackette. There are really cool tools that take BNF as input, and produce a parser as output. But, we re not going to use one of those you re going to write a Rackette-specific parser yourself! Experience shows that this is best done as a multi-step process. First, check that parens match, and identify numbers, strings, and non-number entities. Then, parse the resulting things to be sure they re legal Rackette. When we describe this, we re essentially talking about a language whose BNF looks like this: 2

<program> ::= <item>* <item> ::= <list> <num> <ident> <num> ::= <non - zero - digit><digit>* <digit> ::= 0 <non - zero - digit> <non - zero - digit> ::= 1 2 3 4 5 6 7 8 9 <list> ::= ( <item> <item*> ) <ident> ::=... Parsing ensures that the provided string looks somewhat like our BNF. To simplify this, we ve written a procedure for you called read. Its type signature is read: string ->racket_text. [We might change one of the type names, like racket_text, before the Rackette project is handed out.] Here s a sample input and output for read: Input: "(define (f x ) (+ x 1)) (f 3)" Output: [List [Ident "define" ; List [ Ident "f" ; Ident "x"] ; List [Ident "+" ; Ident "x" ; Num 1]] ; List[Ident "f" ; Num 3]] Your task will be to take something like this and essentially ask, Is that a legal Rackette program? Each item in a Rackette program is either a definition or an expression. So specifically, we ll want to ask: does that first thing in the list match the notion of a definition? We know that a definition should begin with the keyword define, followed by an identifier or a list. Our example output does in fact begin with define, and is followed by a list! (And, as you probably guessed, you can use pattern matching to check this in your implementation!) 3.2 Processing/Evaluation Once we ve figured out that we have a definition followed by a procedure application expression, we need to process/evaluate these. Take the above example: (define (f x) (+ x 1))\\ (f 3) Processing the first line will add a new binding to the top-level environment. Evaluating the second line will require looking up bindings for f and + in the top level environment, and doing some arithmetic. The good news is that OCaml knows how to do arithmetic! 3.3 Environments, Definitions, and Identifiers An environment is a list of all the associations of identifiers to values produced by a definition, or in other words, a list of bindings. We ll be doing lookups and extending environments with new bindings. We also need to create an empty environment, and determine how to add a binding to an environment. In order to do these things, we first have to figure out how to represent a value. A value could be an integer, string, boolean, closure, or a builtin. That suggests a type definition with multiple options. For now, we ll just say there are two possible values, namely Int of int String of string, because that captures all the subtleties we ll n eed. 3

Now, let s discuss identifiers, or as we ve been calling them, names. First, let s think of an example of an identifier anything from list1 to + works. For instance, the identifier + is bound to a built-in procedure specifically, the built-in procedure for addition. Regardless, we come to the conclusion that identifiers are simply strings. Finally, we need to figure out how to represent a binding. A binding relates an identifier to its value. So, we could represent a binding as a tuple of an identifier and value. To summarize, here are the the types we ve identified so far. type identifier = ID of string type binding = identifier * value type value = Int of int String of string type environment = binding list; 4 Balanced Binary Trees and Big-O Analysis for contains7? for a balanced tree Recall the contains7? procedure we wrote that searched for 7 within a binary tree. For the purposes of this analysis, let s look at contains7? for a balanced tree. Let C(d) be the largest number of elementary operations in the evaluation of contains7? atoi on any input tree of depth d. Normally, we say on any input of size n. Here, I m saying on any input tree of depth d. This ll simplify the process, and you ll soon see why. As always, we ll start with our base case. How long does it take to see if a tree of depth 0 contains the number 7? Essentially, it is a constant time operation. The only possibility is a leaf which doesn t contain 7, or a node which may or may not contain 7. Let us say: C(0) = A (1) In our general recursive case, we have to see if the node is 7, and if it isn t, we have to look in one of the subtrees. We do a little bit of constant work checking if the node we are at is 7, and we recur on one of the subtrees. Thus, we can say: C(d) B + C(d 1) (2) This looks pretty similar to the recurrence relations we ve previously proved. We can say C O(n n). Now, we need to determine what the depth of a tree with n nodes actually is. It could be hugely unbalanced the left sub-tree of all nodes could be a leaf, and the right sub-tree could be a node for all but the last node. We ll show that if you have a balanced tree with no more than n nodes, the tree has depth less than 1 + log(n). This is equivalent to showing that a balanced tree of depth d has at least 2 d 1 nodes, as this statement is the contrapositive of the previous. Let s prove it. Let S(d) be the smallest number of nodes in any balanced tree of depth d. We claim S(d) = 2 d 1 1. 4

S(1) = 0, as a tree of depth 1 contains only a single leaf, or in other words, no nodes. Hence our claim is true for d = 1. Suppose a tree of depth k contains fewer than 2 k 1 1 nodes, and k is the smallest number for which this occurs. Then k 0 andk 1, because there are no depth-0 trees, and depth-1 trees all contain 0 nodes. Each child contains at least 2 k 2 1 nodes. Then, we must account for the top node by adding one. We now have a total of 1 + 2(2 k 2 1) = 1 + 2 k 1 2 = 2 k 1 1 nodes. The tree contains at least 2 k 1 1 nodes. This is a contradiction. As such, our claim must hold for all number of nodes. Depth d implies about 2 d nodes in a balanced tree. If we set n = 2 d, then the depth can be represented by about log(n). So, having n nodes corresponds to having a depth of about log(n) (plus or minus one, and hence searching in a binary-search-tree has a runtime in O(n log n), which is awesome! 5 Summary Ideas ˆ A binding associates an identifier with a value. ˆ An environment is a collection of associations of identifiers to values (bindings) produced by a definition. We can represent environments as lists of bindings. ˆ We ve begun looking into the process of parsing, proccessing, and evaluating. Skills ˆ We have learned a helpful way to debug our OCaml procedures. ˆ We have gone through a brief overview of key concepts in the Rackette project. ˆ We have analyzed contains7?, a procedure that recurs on balanced binary trees. Please let us know if you find any mistakes, inconsistencies, or confusing language in this or any other CS17document by filling out the anonymous feedback form: http://cs.brown.edu/ courses/cs017/feedback. 5