Short Circuits Design Recipe Redux Structures

Slides:



Advertisements
Similar presentations
Types and Programming Languages Lecture 4 Simon Gay Department of Computing Science University of Glasgow 2006/07.
Advertisements

While loops.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
P ROGRAMMING L ANGUAGES : C ONTROL 1. S LIDES R EFERENCES Kenneth C. Louden, Control I: Expressions and Statements: Principles and Practice. 2.
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
CS-1010 Dr. Mark L. Hornick 1 Selection Statements and conditional expressions.
Six compound procedures and higher-order procedures.
Loops – While Loop Repetition Statements While Reading for this Lecture, L&L, 5.5.
C. Varela; Adapted w/permission from S. Haridi and P. Van Roy1 Declarative Computation Model Kernel language semantics Carlos Varela RPI Adapted with permission.
Eight compound procedures and higher-order procedures.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
Thirteen conditional expressions: letting programs make “decisions”
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
SchemeCOP Introduction to Scheme. SchemeCOP Scheme Meta-language for coding interpreters –“ clean ” semantics Scheme = LISP + ALGOL –simple.
A Model For The Python Interpreter. What happens when you… Type in an expression (anything other than assignment statement…for now)? Answer: the expression.
Cs1120 Fall 2009 David Evans Lecture 19: Stateful Evaluation.
Chapter 9: MuPAD Programming II Procedures MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Interpretation Environments and Evaluation. CS 354 Spring Translation Stages Lexical analysis (scanning) Parsing –Recognizing –Building parse tree.
Day Class Definitions and Methods Local & global variables, parameters & arguments,
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CMSC 330: Organization of Programming Languages Operational Semantics a.k.a. “WTF is Project 4, Part 3?”
1/33 Basic Scheme February 8, 2007 Compound expressions Rules of evaluation Creating procedures by capturing common patterns.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
CMSC 330: Organization of Programming Languages Operational Semantics.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
Computer Science 1000 LOGO II. Boolean Expressions like Excel and Scratch, LOGO supports three Boolean operators less than (
Operational Semantics of Scheme
Abstract Syntax cs7100 (Prasad) L7AST.
Principles of programming languages 12: Functional programming
Basic Scheme February 8, 2007 Compound expressions Rules of evaluation
Chapter 4 Repetition Statements (loops)
CSc 120 Introduction to Computer Programing II Adapted from slides by
Class 11: Two-argument recursion
Chapter 4: Making Decisions.
The interpreter.
Introduction to Scheme
Control Structures.
Agenda Warmup AP Exam Review: Litvin A2
CS 326 Programming Languages, Concepts and Implementation
CS 326 Programming Languages, Concepts and Implementation
Expanded Recursive Diagrams OCAML rapid tour, day 2
Selection Statements by Ahmet Sacan
Env. Model Implementation
While Loops in Python.
Outline Altering flow of control Boolean expressions
Chapter 4: Types.
The Metacircular Evaluator
Lecture 23 Pages : Separating Syntactic Analysis from Execution. We omit many details so you have to read the section in the book. The halting.
Objectives After studying this chapter, you should be able to:
slides created by Marty Stepp
Dynamic Scoping Lazy Evaluation
Procedures App B: SLLGEN 1.
3. Decision Structures Rocky K. C. Chang 19 September 2018
Sit next to someone. Today, we do some work in pairs.
Rules of evaluation The value of a number is itself.
The Metacircular Evaluator (Continued)
Abstract Syntax cs7100 (Prasad) L7AST.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
6.001 SICP Interpretation Parts of an interpreter
Assignments and Procs w/Params
topics interpreters meta-linguistic abstraction eval and apply
Types, Truth, and Expressions
Rehearsal: Lazy Evaluation Infinite Streams in our lazy evaluator
Rules of evaluation The value of a number is itself.
Reasoning with Types.
Presentation transcript:

Short Circuits Design Recipe Redux Structures

Processing Start with a top-level environment (TLE) containing builtins like the addition and multiplication procedure; there’s also a current environment, which starts out as the TLE Top level expressions are evaluated to produce a value v; the printed representation of v is printed out. An identifier definition (define ident exp) is processed by checking that the identifier ident is not bound in the top-level environment; if it is, it’s an error; otherwise it gets bound to the value of exp. A function definition (define (ident arg1 ... argn) body) is processed by checking that ident is not bound in the TLE; if it is bound, it’s an error. Otherwise, ident is bound to a closure whose “args” piece contains the identifiers arg1, ..., argn, and whose “body” piece contains the expression body (and not the value of the expression body!)

Evaluation Numbers evaluate to themselves; so do strings. The value of an identifier is the value to which it’s bound in the current enviroment; if it’s not bound, evaluation produces an error.

Proc-app-expressions (builtin) The value of a procedure-application-expression (proc c1 ... cn) is determined by evaluating the expression proc; if the resulting value is neither a builtin procedure nor a user-defined procedure, it’s an error. Otherwise, we evaluate the expressions c1 ... cn (if any) in order to get values v1,...,vn. (a) if proc evaluated to a builtin, the builtin procedure is applied to the values v1, . . . , vn to produce a result r, which is the value of the procedure application expression.

Proc-app-expr (user-defined procedures) (b) if proc evaluated to a user-defined procedure, then we temporarily extend the current environment by binding the arguments of the closure to the corresponding values (if the number of arguments and number of values don’t match, it’s an error) evaluate the body of the closure in this enlarged environment to get a value v reset the current environment to its prior state, losing the newly-added bindings the value of the procedure-application-expression is v.

New rule: and-expressions An and-expression (and exp1 .. expn) is evaluated by evaluating exp1, whose value must be a boolean. If it is false, the value of the and-expression is false and the remaining expressions are never evaluated. If it is true, however, we evaluate exp2; again, the value must be a boolean or it’s an error. If the resulting value is false, the value of the and-expression is false and the remaining expressions are never evaluated. We proceed similarly through any remaining expressions. If all expressions evaluate to true, then the value of the and-expression is true.

New rule: or-expressions An or-expression (or exp1 .. expn) is evaluated by evaluating exp1, whose value must be a boolean. If it is true, the value of the or-expression is true and the remaining expressions are never evaluated. If it is false, however, we evaluate exp2; again, the value must be a boolean or it’s an error. If the resulting value is true, the value of the or-expression is true and the remaining expressions are never evaluated. We proceed similarly through any remaining expressions. If all expressions evaluate to false, then the value of the or-expression is false.

New rule: if-expressions An if-expression (if test-exp true-exp false-exp) is evaluated by evaluating test-exp, whose value must be a boolean. If it is true, the value of the if-expression is the value of true-exp, and false-exp is never evaluated. If it is false, the value of the if-expression is the value of false- exp, and true-exp is never evaluated.

Example (define x 0) (if (= x 0) 7 (/ 1 x))

Exercise (if (> 1 0) + −)

Design recipe Provide data definitions for all non-atomic types Provide examples of data the procedure will process Specify the procedure’s type signature, saying the type of data consumed and the type of data produced Following the type-signature, code the procedure’s call structure, i.e., give names to the procedure and its arguments. Write a specification of the procedure (in words, using names) Write test cases +8. Code the procedure 9. Run your program