CSCI 2041: Deep and Shallow Equality

Similar documents
CSCI 2041: First Class Functions

CSCI 2041: Functions, Mutation, and Arrays

CSCI 2041: Basic OCaml Syntax and Features

CSCI 2041: Tail Recursion and Activation Records

CSCI 2041: Pattern Matching Basics

CSCI 2041: Object Systems

CSCI 1103: Input with TextIO

COSE212: Programming Languages. Lecture 3 Functional Programming in OCaml

CMSC 330, Fall 2013, Practice Problems 3

Lecture #23: Conversion and Type Inference

PROGRAMMING IN HASKELL. Chapter 5 - List Comprehensions

CSCI 2041: Data Types in OCaml

Conversion vs. Subtyping. Lecture #23: Conversion and Type Inference. Integer Conversions. Conversions: Implicit vs. Explicit. Object x = "Hello";

It is better to have 100 functions operate one one data structure, than 10 functions on 10 data structures. A. Perlis

CSCE 314 TAMU Fall CSCE 314: Programming Languages Dr. Flemming Andersen. Haskell Functions

C++: Const Function Overloading Constructors and Destructors Enumerations Assertions

CSE 3302 Programming Languages Lecture 8: Functional Programming

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

Industrial Programming

CSci 4223 Lecture 12 March 4, 2013 Topics: Lexical scope, dynamic scope, closures

CS153: Compilers Lecture 15: Local Optimization

Informatics 1 Functional Programming Lectures 13 and 14 Monday 11 and Tuesday 12 November Type Classes. Don Sannella University of Edinburgh

Vectors in Scheme. Data Structures in Scheme

n Closed book n You are allowed 5 cheat pages n Practice problems available in Course Materials n Check grades in Rainbow grades

Programming Languages and Techniques (CIS120)

Section 13.5: Equations of Lines and Planes. 1 Objectives. 2 Assignments. 3 Lecture Notes

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

CS 211: Using ArrayList, Implementing Arraylist

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

CSCI 2041: Lists and Recursion

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

CSCI 1103: Simple Objects

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

CSC312 Principles of Programming Languages : Functional Programming Language. Copyright 2006 The McGraw-Hill Companies, Inc.

CSCI 2041: Advanced Language Processing

Overview. Declarative Languages D7012E. Overloading. Overloading Polymorphism Subtyping

Programming Languages

CSCI 2041: Lazy Evaluation

CS 310: Priority Queues and Binary Heaps

COMP1917 Computing 1 Written Exam Sample Questions

CSCI 2021: Introduction

CSC324 Principles of Programming Languages

CSE 341 Section 5. Winter 2018

G Programming Languages - Fall 2012

News. Programming Languages. Complex types: Lists. Recap: ML s Holy Trinity. CSE 130: Spring 2012

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Winter 2013

CMSC330 Fall 2015 Midterm #1 12:30pm/2:00pm/5:00pm

CSCI-GA Scripting Languages

CSE 130 [Winter 2014] Programming Languages

CMSC 330: Organization of Programming Languages

CMSC 330, Fall 2013, Practice Problem 3 Solutions

Run-time Environments - 3

6.034 Artificial Intelligence February 9, 2007 Recitation # 1. (b) Draw the tree structure corresponding to the following list.

Chapter 15. Functional Programming. Topics. Currying. Currying: example. Currying: example. Reduction

Announcements for this Lecture

Introduction to ML. Mooly Sagiv. Cornell CS 3110 Data Structures and Functional Programming

Topic 7: Algebraic Data Types

CS 211: Methods, Memory, Equality

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Programming Languages Fall 2013

Announcements. Working on requirements this week Work on design, implementation. Types. Lecture 17 CS 169. Outline. Java Types

OF VICTORIA EXAMINATIONS- DECEMBER 2011 CSC

Verifying Safety Property of Lustre Programs: Temporal Induction

INTRODUCTION TO FUNCTIONAL PROGRAMMING

Types and Type Inference

CMSC 330: Organization of Programming Languages. Lets, Tuples, Records

G Programming Languages Spring 2010 Lecture 8. Robert Grimm, New York University

Testing. Wouter Swierstra and Alejandro Serrano. Advanced functional programming - Lecture 2. [Faculty of Science Information and Computing Sciences]

Programming Languages and Compilers (CS 421)

CSCI 1103: Static Methods

Programming Languages

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

Solution Manual for Data Structures and Problem Solving Using C++ 2nd edition by Mark A. Weiss

Overloading, Type Classes, and Algebraic Datatypes

CS 440: Programming Languages and Translators, Spring 2019 Mon

CS1 Lecture 12 Feb. 11, 2019

Announcements. CSCI 334: Principles of Programming Languages. Lecture 18: C/C++ Announcements. Announcements. Instructor: Dan Barowy

Dialects of ML. CMSC 330: Organization of Programming Languages. Dialects of ML (cont.) Features of ML. Functional Languages. Features of ML (cont.

COMPUTER APPLICATION

Programming Languages

CS1 Lecture 3 Jan. 22, 2018

Example problems. Combinatorial auction. Towers of Hanoi Rectangle packing Shortest route. 8 queens Soduko Maximizing (minimizing) costs

Functional Programming. Lecture 2: Algebra

A Third Look At ML. Chapter Nine Modern Programming Languages, 2nd ed. 1

Closures. Mooly Sagiv. Michael Clarkson, Cornell CS 3110 Data Structures and Functional Programming

Polymorphism and Type Inference

Final Labs and Tutors

Logical and Bitwise Expressions

Data III & Integers I

OCaml. ML Flow. Complex types: Lists. Complex types: Lists. The PL for the discerning hacker. All elements must have same type.

CSE450. Translation of Programming Languages. Lecture 11: Semantic Analysis: Types & Type Checking

CSE341: Programming Languages Lecture 9 Function-Closure Idioms. Dan Grossman Fall 2011

CS 211: Existing Classes in the Java Library

COP 3502 (Computer Science I) Final Exam 12/10/2015. Last Name:, First Name:

Scientific Computing

Interpreters. Prof. Clarkson Fall Today s music: Step by Step by New Kids on the Block

9/19/12. Why Study Discrete Math? What is discrete? Sets (Rosen, Chapter 2) can be described by discrete math TOPICS

Plan (next 4 weeks) 1. Fast forward. 2. Rewind. 3. Slow motion. Rapid introduction to what s in OCaml. Go over the pieces individually

TUPLES AND RECURSIVE LISTS 5

Exercises on ML. Programming Languages. Chanseok Oh

Transcription:

CSCI 2041: Deep and Shallow Equality Chris Kauffman Last Updated: Thu Oct 18 22:42:18 CDT 2018 1

Logistics Goals Today Finish Higher-order Funcs Deep/Shallow Equality Later this Week Wed: Scope and Functions Fri: Curried Funcs/Return Vals Next Week Mon: Review Wed: Exam 2 Fri: Lecture Exam 1 Regrade requests via Gradescope, Due Mon 10/15 Assignment 3 multimanager Manage multiple lists Records to track lists/undo option to deal with editing Higher-order funcs for easy bulk operations Due Mon 10/22 Test cases over the weekend 2

Exercise: Deep and Shallow Equality Some folks have noticed OCaml has two means of comparing values a = b : structural or deep equality a == b : physical or shallow equality Code in equality.ml to the right uses both What gets printed for the examples shown? 1 let a = 5 in 2 let b = 5 in 3 let c = a in 4 printf "---ints---\n"; 5 printf "a=b : %b\n" (a=b); 6 printf "a==b : %b\n" (a==b); 7 printf "a=c : %b\n" (a=c); 8 printf "a==c : %b\n" (a==c); 9 10 let x = ref 5 in 11 let y = ref 5 in 12 let z = x in 13 printf "---int Refs---\n"; 14 printf "x=y : %b\n" (x=y); 15 printf "x==y : %b\n" (x==y); 16 printf "x=z : %b\n" (x=z); 17 printf "x==z : %b\n" (x==z); 3

Answers: Deep and Shallow Equality 1 let a = 5 in (* box with 5 *) 2 let b = 5 in (* box with 5 *) 3 let c = a in (* box with copy a s contents *) 4 printf "---ints---\n"; 5 printf "a=b : %b\n" (a=b); (* a=b : true *) 6 printf "a==b : %b\n" (a==b); (* a==b : true *) 7 printf "a=c : %b\n" (a=c); (* a=c : true *) 8 printf "a==c : %b\n" (a==c); (* a==c : true *) 9 10 let x = ref 5 in (* pointer to box w/ 5 *) 11 let y = ref 5 in (* pointer to new box w/ 5 *) 12 let z = x in (* copy x s pointer *) 13 printf "---int Refs---\n"; 14 printf "x=y : %b\n" (x=y); (* x=y : true : same contents *) 15 printf "x==y : %b\n" (x==y); (* x==y : false : different locations*) 16 printf "x=z : %b\n" (x=z); (* x=z : true : same contents *) 17 printf "x==z : %b\n" (x==z); (* x==z : true : same location *) 4

Answers: Deep and Shallow Equality Deep equality checks entire structure for corresponding equal values Shallow equality checks only if memory box contains the same value Pointers are stored as integers (notated in figure as #2048) Both work the same for boxed values like int Return different answers for unboxed values like refs 5

Deep vs Shallow Equality is in Every Language C/C++ == and!= operators compare single blocks of memory, mostly shallow equality Typically write an equality function to compare deep/recursive data Java == and!= identical to C a.equals(b): create methods to define meaning of deep equality for a class Scheme equal? : deep equality eq? : shallow equality ; create two distinct lists, same elems guile-scheme> (define x (list 1 2 3)) guile-scheme> (define y (list 1 2 3)) ; check deep and shallow equality guile-scheme> (equal? x y) ; deep #t ; true guile-scheme> (eq? x y) ; shallow #f ; false Python Like Scheme, different op names x == y : deep equality x is y : shallow equality 6

Convenient Deep Equality in OCaml Equal Unequal = <> Deep ==!= Shallow Data defined via standard mechanisms in OCaml gets automatically has deep equality defined for it Arrays, Strings, Tuples, Records, Algebraic, all "just work" 1 let s = "hi" in (* pointer to string of chars *) 2 let t = "hi" in (* pointer to different string of chars *) 3 let u = s in (* pointer to same place as s *) 4 printf "---Strings---\n"; 5 printf "s=t : %b\n" (s=t); (* s=t : true : same contents *) 6 printf "s==t : %b\n" (s==t); (* s==t : false : different locations*) 7 printf "s=u : %b\n" (s=u); (* s=u : true : same contents *) 8 printf "s==u : %b\n" (s==u); (* s==u : true : same location *) 9 10 let f = {s="yo"; i=2} in (* pointer to new record *) 11 let g = {s="yo"; i=2} in (* pointer to new record *) 12 let h = f in (* pointer to existing record *) 13 printf "---Records---\n"; 14 printf "f=g : %b\n" (f=g); (* f=g : true : same contents *) 15 printf "f==g : %b\n" (f==g); (* f==g : false : different locations*) 16 printf "f=h : %b\n" (f=h); (* f=h : true : same contents *) 17 printf "f==h : %b\n" (f==h); (* f==h : true : same location *) 7

Choosing Deep vs Shallow Equality Generally use Deep equality, usually what is "intended" Are these two things equal to one another? Keep in mind Deep equality may visit whole data structure All chars of a string All elements of a list or array All fields of a record, etc. O(N) operation where N is the size of the data structure This may have performance implications: In some special cases, may be reasonable to use Shallow equality which is an O(1) operation 8

Library Functions and Deep/Shallow Equality Some Library function distinguish between use of Deep/Shallow equality q suffix in function name indicates Shallow Equality is used Examples from the List module val mem : a -> a list -> bool mem elem list is true if and only if elem is equal to an element of list. val memq : a -> a list -> bool Same as List.mem, but uses physical (shallow) equality instead of structural (deep) equality to compare list elements. val assoc : a -> ( a * b) list -> b assoc key alist returns the value associated with key in the list of pairs alist. val assq : a -> ( a * b) list -> b Same as List.assoc, but uses physical (shallow) equality instead of structural (deep) equality to compare keys. 9

Exercise: Deep / Shallow Differences Code below searches a list for an element using mem : deep equality memq : shallow equality Determine values for results of searches Draw a picture of x,y,z, lista, listb to justify answers 1 let x = "yikes" in 2 let y = "boo!" in 3 let z = "gulp" in 4 5 let lista = [ x; y; z] in 6 let listb = ["yikes"; "boo!"; "gulp"] in 7 8 let d_ya = List.mem y lista in 9 let s_ya = List.memq y lista in 10 11 let d_yb = List.mem y listb in 12 let s_yb = List.memq y listb in 10

Answers: Deep / Shallow Differences 1 let x = "yikes" in 2 let y = "boo!" in 3 let z = "gulp" in 4 5 let lista = [ x; y; z] in 6 let listb = ["yikes"; "boo!"; "gulp"] in 7 8 let d_ya = List.mem y lista in (* deep equals: true *) 9 let s_ya = List.memq y lista in (* shallow equals: true *) 10 11 let d_yb = List.mem y listb in (* deep equals : true *) 12 let s_yb = List.memq y listb in (* shallow equals: false *) 11