Compound Data in Java. Instances of Compound Data Types. Armadillos. Design: Functional to Object-Oriented. Beginner Scheme:



Similar documents
Lecture 9. Semantic Analysis Scoping and Symbol Table

Classes and Objects in Java Constructors. In creating objects of the type Fraction, we have used statements similar to the following:

Object-Oriented Design Lecture 4 CSU 370 Fall 2007 (Pucella) Tuesday, Sep 18, 2007

Writing Functions in Scheme. Writing Functions in Scheme. Checking My Answer: Empty List. Checking My Answer: Empty List

Introduction to formal semantics -

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

Moving from CS 61A Scheme to CS 61B Java

Chapter 2: Elements of Java

Lab Experience 17. Programming Language Translation

Semester Review. CSC 301, Fall 2015

Stack vs. Heap. Introduction to Programming. How the Stack Works. Primitive vs. Reference Types. Stack

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

How To Program In Scheme (Prolog)

Code Kingdoms Learning a Language

Programming Languages CIS 443

Semantic Analysis: Types and Type Checking

Basic Object-Oriented Programming in Java

Compiler Construction

PL / SQL Basics. Chapter 3

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

J a v a Quiz (Unit 3, Test 0 Practice)

Programming Languages

03 - Lexical Analysis

Data Structures and Algorithms

DATA 301 Introduction to Data Analytics Microsoft Excel VBA. Dr. Ramon Lawrence University of British Columbia Okanagan

Programming Language Rankings. Lecture 15: Type Inference, polymorphism & Type Classes. Top Combined. Tiobe Index. CSC 131! Fall, 2014!

Abstract Class & Java Interface

Dart a modern web language

Contents. 9-1 Copyright (c) N. Afshartous

Selection Statements

Computer Programming I

Mobile App Design Project #1 Java Boot Camp: Design Model for Chutes and Ladders Board Game

Example of a Java program

University of Toronto Department of Electrical and Computer Engineering. Midterm Examination. CSC467 Compilers and Interpreters Fall Semester, 2005

Introduction to Java

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Programming Languages Featherweight Java David Walker

Java 6 'th. Concepts INTERNATIONAL STUDENT VERSION. edition

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Visual Basic Programming. An Introduction

Stack Allocation. Run-Time Data Structures. Static Structures

Symbol Tables. Introduction

Programming Project 1: Lexical Analyzer (Scanner)

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

The C Programming Language course syllabus associate level

The Clean programming language. Group 25, Jingui Li, Daren Tuzi

COMP 356 Programming Language Structures Notes for Chapter 4 of Concepts of Programming Languages Scanning and Parsing

Government Girls Polytechnic, Bilaspur

qwertyuiopasdfghjklzxcvbnmqwerty uiopasdfghjklzxcvbnmqwertyuiopasd fghjklzxcvbnmqwertyuiopasdfghjklzx cvbnmqwertyuiopasdfghjklzxcvbnmq

Sample CSE8A midterm Multiple Choice (circle one)

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Handout 1. Introduction to Java programming language. Java primitive types and operations. Reading keyboard Input using class Scanner.

Arrays. Atul Prakash Readings: Chapter 10, Downey Sun s Java tutorial on Arrays:

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

C++ INTERVIEW QUESTIONS

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

arrays C Programming Language - Arrays

csce4313 Programming Languages Scanner (pass/fail)

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Hypercosm. Studio.

Statements and Control Flow

IRA EXAMPLES. This topic has two examples showing the calculation of the future value an IRA (Individual Retirement Account).

Programming by Contract. Programming by Contract: Motivation. Programming by Contract: Preconditions and Postconditions

CmpSci 187: Programming with Data Structures Spring 2015

The Cool Reference Manual

CS104: Data Structures and Object-Oriented Design (Fall 2013) October 24, 2013: Priority Queues Scribes: CS 104 Teaching Team

Designing with Exceptions. CSE219, Computer Science III Stony Brook University

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

Exercise 4 Learning Python language fundamentals

Functional Programming

Chapter 15 Functional Programming Languages

COSC Introduction to Computer Science I Section A, Summer Question Out of Mark A Total 16. B-1 7 B-2 4 B-3 4 B-4 4 B Total 19

Java Server Pages and Java Beans

CS 106 Introduction to Computer Science I

CSCI 3136 Principles of Programming Languages

Crash Course in Java

Static vs. Dynamic. Lecture 10: Static Semantics Overview 1. Typical Semantic Errors: Java, C++ Typical Tasks of the Semantic Analyzer

Data Integration through XML/XSLT. Presenter: Xin Gu

Fun with Phantom Types

Resco Mobile CRM Woodford (Rules Guide) Document version

Chapter 6: Programming Languages

Absolute Value Equations and Inequalities

AP Computer Science A 2004 Free-Response Questions

Syntax Check of Embedded SQL in C++ with Proto

Single Page Web App Generator (SPWAG)

CASCADING IF-ELSE. Cascading if-else Semantics. What the computer executes: What is the truth value 1? 3. Execute path 1 What is the truth value 2?

Chapter 8 Selection 8-1

Programming Database lectures for mathema

Sources: On the Web: Slides will be available on:

Parameter passing in LISP

Chapter 14: Boolean Expressions Bradley Kjell (Revised 10/08/08)

While Loop. 6. Iteration

Transcription:

Compound Data in Java Design: Functional to Object-Oriented Semantics of Local Definitions Beginner Scheme: ; A snake is ; (make-snake sym num sym) (define-struct snake (name weight food)) Beginner Java: class Snake { String name; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; this.food = food; Beginner Scheme: Beginner Java: Instances of Compound Data Types (make-snake Slinky 12 rats) (make-snake Slimey 5 grass) new Snake("Slinky", 12, "rats") new Snake("Slimey", 5, "grass") Armadillos class Dillo { boolean alive; Dillo(double weight, boolean alive) { this.weight = weight; this.alive = alive; new Dillo(2, true) new Dillo(3, false) 1-4

Posns class Posn { int x; int y; Posn(int x, int y) { this.x = x; this.y = y; new Posn(0, 0) new Posn(1, -2) Ants class Ant { Posn loc; Ant(double weight, Posn loc) { this.weight = weight; this.loc = loc; new Ant(0.0001, new Posn(0, 0)) new Ant(0.0002, new Posn(1, -2)) Beginner Scheme: Data with Variants ; An animal is either ; - snake ; - dillo ; - ant class Snake extends Animal { as before class Dillo extends Animal { as before class Ant extends Animal { as before Variants in Java A data definition with variants must refer only to other data definitions (which are not built in) ; A grade is either ; - false ; - num ; A grade is either ; - no-grade ; - num-grade ; A no-grade is ; (make-no-grade) (define-struct no-grade ()) ; A num-grade is ; (make-num-grade num) (define-struct num-grade (n)) A data definition can be a variant in at most one other data definition 5-11

From Scheme to Java So far, we ve translated data definitions: ; A snake is ; (make-snake sym num sym) (define-struct snake (name weight food)) class Snake { String name; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; this.food = food; Functions in Scheme ; A snake is ; (make-snake sym num sym) (define-struct snake (name weight food)) ; snake-lighter? : snake num -> bool ; Determines whether s is < n lbs (define (snake-lighter? s n) (< (snake-weight s) n)) (snake-lighter? (make-snake Slinky 10 rats) 10) "should be" false (snake-lighter? (make-snake Slimey 5 grass) 10) "should be" false Functions in Java class Snake { String name; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; this.food = food; // Determines whether it s < n lbs boolean islighter(double n) { return this.weight < n; new Snake("Slinky", 10, "rats").islighter(10) "should be" false Functions in Java class Snake { String name; String food; Snake(String name, double weight, String food) { this.name = name; A method in this.weight = weight; Snake has an this.food = food; implicit Snake this argument // Determines whether it s < n lbs boolean islighter(double n) { return this.weight < n; new Snake("Slinky", 10, "rats").islighter(10) "should be" false 12-15

Method Calls in Java Templates Original tests: Scheme: (snake-lighter? (make-snake Slinky 10 rats) 10) "should be" false In Scheme: ; A snake is ; (make-snake sym num sym) (define-struct snake (name weight food)) Java: new Snake("Slinky", 10, "rats").islighter(10) "should be" false ; func-for-snake : snake -> (define (func-for-snake s) (snake-name s) (snake-weight s) (snake-food s) ) Templates Functions with Variants Same idea works for Java: class Snake { String name; String food; Snake(String name, double weight, String food) { this.name = name; this.weight = weight; this.food = food; methodforsnake() { this.name this.weight this.food ; An animal is either ; - snake ; - dillo ; - ant ; animal-is-lighter? : animal num -> bool (define (animal-is-lighter? a n) [(snake? a) (snake-is-lighter? s n)] [(dillo? a) (dillo-is-ligheter? s n)] [(ant? a) (ant-is-lighter? s n)])) ; snake-is-lighter? : snake num -> bool (define (snake-is-lighter? s n) ) ; dillo-is-lighter? : dillo num -> bool (define (dillo-is-lighter? d n) ) ; ant-is-lighter? : ant num -> bool (define (ant-is-lighter? a n) ) 16-19

Methods with Variants Translating Functions to Methods abstract boolean islighter(double n); class Snake extends Animal { boolean islighter(double n) { class Dillo extends Animal { boolean islighter(double n) { class Ant extends Animal { boolean islighter(double n) { ; An animal is either ; - snake ; - dillo ; - ant ; animal-is-lighter? : animal num -> bool (define (animal-is-lighter? a n) [(snake? a) (snake-is-lighter? s n)] [(dillo? a) (dillo-is-ligheter? s n)] [(ant? a) (ant-is-lighter? s n)])) ; snake-is-lighter? : snake num -> bool (define (snake-is-lighter? s n) ) ; dillo-is-lighter? : dillo num -> bool (define (dillo-is-lighter? d n) ) ; ant-is-lighter? : ant num -> bool (define (ant-is-lighter? a n) ) abstract boolean islighter(double n); class Snake extends Animal { boolean islighter(double n) { class Dillo extends Animal { boolean islighter(double n) { class Ant extends Animal { boolean islighter(double n) { Translating Functions to Methods Translating Functions to Methods ; An animal is either ; - snake ; - dillo ; - ant ; animal-is-lighter? : animal num -> bool (define (animal-is-lighter? a n) [(snake? a) (snake-is-lighter? s n)] [(dillo? a) (dillo-is-ligheter? s n)] [(ant? a) (ant-is-lighter? s n)])) Data definition turns into class declarations ; An animal is either ; - snake ; - dillo ; - ant ; animal-is-lighter? : animal num -> bool (define (animal-is-lighter? a n) [(snake? a) (snake-is-lighter? s n)] [(dillo? a) (dillo-is-ligheter? s n)] [(ant? a) (ant-is-lighter? s n)])) Variant functions turn into variant methods all with the same contract after the implicit argument ; snake-is-lighter? : snake num -> bool (define (snake-is-lighter? s n) ) ; dillo-is-lighter? : dillo num -> bool (define (dillo-is-lighter? d n) ) ; ant-is-lighter? : ant num -> bool (define (ant-is-lighter? a n) ) abstract boolean islighter(double n); class Snake extends Animal { boolean islighter(double n) { ; snake-is-lighter? : snake num -> bool (define (snake-is-lighter? s n) ) ; dillo-is-lighter? : dillo num -> bool (define (dillo-is-lighter? d n) ) ; ant-is-lighter? : ant num -> bool (define (ant-is-lighter? a n) ) abstract boolean islighter(double n); class Snake extends Animal { boolean islighter(double n) { class Dillo extends Animal { boolean islighter(double n) { class Dillo extends Animal { boolean islighter(double n) { class Ant extends Animal { boolean islighter(double n) { class Ant extends Animal { boolean islighter(double n) { 20-23

Translating Functions to Methods Lists of Things ; An animal is either ; - snake ; - dillo ; - ant ; animal-is-lighter? : animal num -> bool (define (animal-is-lighter? a n) [(snake? a) (snake-is-lighter? s n)] [(dillo? a) (dillo-is-ligheter? s n)] [(ant? a) (ant-is-lighter? s n)])) ; snake-is-lighter? : snake num -> bool (define (snake-is-lighter? s n) ) ; dillo-is-lighter? : dillo num -> bool (define (dillo-is-lighter? d n) ) ; ant-is-lighter? : ant num -> bool (define (ant-is-lighter? a n) ) Function with variant-based cond turns into just an abstract method declaration abstract boolean islighter(double n); class Snake extends Animal { boolean islighter(double n) { class Dillo extends Animal { boolean islighter(double n) { class Ant extends Animal { boolean islighter(double n) { abstract class ListOfThing { abstract int length(); class EmptyListOfThing extends ListOfThing { EmptyListOfThing() { int length() { return 0; class ConsListOfThing extends ListOfThing { Thing first; ListOfThing rest; ConsListOfThing(Thing first, ListOfThing rest) { this.first = first; this.rest = rest; int length() { return 1 + this.rest.length(); Trees of Things Implementing Methods Directly abstract class TreeOfThing { abstract int count(); class EmptyTreeOfThing extends TreeOfThing { EmptyTreeOfThing() { int count() { return 0; class ConsTreeOfThing extends TreeOfThing { Thing v; TreeOfThing left; TreeOfThing right; ConsTreeOfThing(Thing v, TreeOfThing left, TreeOfThing right) { this.v = v; this.left = left; this.right = right; int count() { return 1 + this.left.count() + this.right.count(); Some Scheme methods on animal can be implemented with other animal functions: ; animal-light? : animal -> bool ; Determines whether a is less than 10 lbs (define (animal-light? a) (animal-lighter? a 10)) In Java, this corresponds to a non-abstract method in an abstract class: boolean islight() { return this.islighter(10); 24-28

Random Numbers Design: Functional to Object-Oriented Semantics of Local Definitions The random operator returns a different result for different calls with the same input: > (random 3) 0 > (random 3) 2 > (random 3) 1 > (random 3) 2 Random Symbols Suppose we need a random-symbol function > > huey > > louie Random Symbols ; random-symbol : sym sym sym -> sym (define (random-symbol a b c) [(= (random 3) 0) a] [(= (random 3) 1) b] [(= (random 3) 2) c])) This doesn t work, because random produces a different result each time Can we implement it with random? 29-33

Saving a Random Number A Random Constant On the other hand (define n (random 3)) (list n n n) produces (list 0 0 0), (list 1 1 1), or (list 2 2 2) Constant definitions name constants, so (random 3) must be evaluted when defining n Try it in the stepper Does this work? (define n (random 3)) ; random-symbol : sym sym sym -> sym (define (random-symbol a b c) [(= n 0) a] [(= n 1) b] [(= n 2) c])) Not quite, because it always picks the same symbol We want (define n (random 3)) that is local to random-symbol s body Local Definitions This works, in the Intermediate language ; random-symbol : sym sym sym -> sym (define (random-symbol a b c) [(= n 0) a] [(= n 1) b] [(= n 2) c]))) The local form has definitions and a body Local definitions are only visible in the body Local definitions are evaluated only when the local is evaluated The result of local is the result of its body (define (random-symbol a b c) [(= n 0) a] [(= n 1) b] [(= n 2) c]))) [(= n 0) huey] [(= n 1) ] [(= n 2) louie])) 34-42

[(= n 0) huey] [(= n 1) ] [(= n 2) louie])) (define n17 (random 3)) [(= n17 0) huey] (define n17 (random 3)) [(= n17 0) huey] [(= n17 0) huey] Evaluation of local lifts and renames the definition [(= n17 0) huey] [(= 1 0) huey] [(= 1 0) huey] [false huey] Evaluation of a constant name finds the value 43-50

[false huey] [(= 1 1) ] [(= 1 1) ] [true ] [true ] 51-58

[(= n 0) huey] [(= n 1) ] [(= n 2) louie])) [(= n 0) huey] [(= n 1) ] [(= n 2) louie])) (define n45 (random 3)) [(= n45 0) huey] [(= n45 1) ] [(= n45 2) louie]) Evaluation of local picks a new name each time (define n45 (random 3)) [(= n45 0) huey] [(= n45 1) ] [(= n45 2) louie]) (define n45 0) [(= n45 0) huey] [(= n45 1) ] [(= n45 2) louie]) Another Example ; kind-of-blue? : image -> bool (define (kind-of-blue? i) (and (> (total-blue (image->color-list i)) (total-red (image->color-list i))) (> (total-blue (image->color-list i)) (total-green (image->color-list i))))) Easier to read, converts image only once: (define (kind-of-blue? i) (local [(define colors (image->color-list i))] (and (> (total-blue colors) (total-red colors)) (> (total-blue colors) (total-green colors))))) 59-65

Better: Another Example (define (eat-apples l) [(empty? l) empty] [(cons? l) [(symbol=? (first l) apple) (eat-apples (rest l))] [else (cons (first l) (eat-apples (rest l)))])])) (define (eat-apples l) [(empty? l) empty] [(cons? l) (local [(define ate-rest (eat-apples (rest l)))] [(symbol=? (first l) apple) ate-rest] [else (cons (first l) ate-rest)]))])) Another Use for Local local can define functions as well as constants This is useful for making a function private (define (random-symbol a b c) (local [(define (real-random-symbol a b c) [(= n 0) a] [(= n 1) b] [(= n 2) c])))] [(and (symbol? a) (symbol? b) (symbol? c)) (real-random-symbol a b c)] [else (error random-symbol "not a symbol")]))) Use Check Syntax and mouse over variables Lexical Scope (define (random-symbol a b c) (local [(define (real-random-symbol a b c) [(= n 0) a] [(= n 1) b] [(= n 2) c])))] [(and (symbol? a) (symbol? b) (symbol? c)) (real-random-symbol a b c)] [else (error random-symbol "not a symbol")]))) Italic a could be changed to z without affecting non-italic a, no matter how the code runs In other words, bindings are static; this is lexical scope 66-70