Programming Languages Dan Grossman 2013

Slides:



Advertisements
Similar presentations
CSE341: Programming Languages Lecture 2 Functions, Pairs, Lists Dan Grossman Winter 2013.
Advertisements

CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 16 Macros Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 12 Modules Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 24 Racket Modules, Abstraction with Dynamic Types; Racket Contracts Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 17 Structs, Implementing Languages, Implementing Higher-Order Functions Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 11 Type Inference Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 15 Macros Dan Grossman Spring 2013.
Programming LanguagesSection 61 Programming Languages Section 6. Racket v.s. ML Xiaojuan Cai Spring 2015.
CSE341: Programming Languages Structs, Implementing Languages, Implementing Higher-Order Functions Alan Borning (slides “borrowed” from Dan Grossman) Fall.
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
6.001 SICP 1/ SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams.
SICP Object Oriented Programming Data Abstraction using Procedures with State Message-Passing Object Oriented Modeling Class diagrams Instance.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Winter 2013.
Programming Languages Dan Grossman 2013 Datatype-Programming in Racket Without Structs.
Programming Languages Dan Grossman 2013 Optional: Dynamic Dispatch Manually in Racket.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
6.001 SICP Object Oriented Programming
CSE341: Programming Languages Lecture 15 Macros
Programming Languages Dan Grossman 2013
Programming Languages Dan Grossman 2013
The role of abstractions
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
Emily Leland (Not Nick) Spring 2017
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2018.
Preparing for MUPL! Justin Harjanto
Lecture 15: Tables and OOP
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
Data abstraction, revisited
CSE 341 Section 7 Winter 2018 Adapted from slides by Eric Mullen, Nicholas Shahan, Dan Grossman, and Tam Dang.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2018.
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Adapted from slides by Nicholas Shahan and Dan Grossman
Data Mutation Primitive and compound data mutators set! for names
6.001 SICP Data Mutation Primitive and Compound Data Mutators
Adapted from slides by Nicholas Shahan, Dan Grossman, and Tam Dang
CSE341: Programming Languages Lecture 15 Macros
Dan Grossman / Eric Mullen Autumn 2017
Nicholas Shahan Spring 2016
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
CSE341: Programming Languages Lecture 11 Type Inference
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 15 Macros
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2019.
CSE341: Programming Languages Lecture 15 Macros
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

Programming Languages Dan Grossman 2013 Advantages of Structs

Contrasting Approaches Versus This is not a case of syntactic sugar (struct add (e1 e2) #:transparent) (define (add e1 e2) (list 'add e1 e2)) (define (add? e) (eq? (car e) 'add)) (define (add-e1 e) (car (cdr e))) (define (add-e2 e) (car (cdr (cdr e)))) Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages The key difference (struct add (e1 e2) #:transparent) The result of calling (add x y) is not a list And there is no list for which add? returns #t struct makes a new kind of thing: extending Racket with a new kind of data So calling car, cdr, or mult-e1 on “an add” is a run-time error Jan-Mar 2013 Dan Grossman, Programming Languages

List approach is error-prone (define (add e1 e2) (list 'add e1 e2)) (define (add? e) (eq? (car e) 'add)) (define (add-e1 e) (car (cdr e))) (define (add-e2 e) (car (cdr (cdr e)))) Can break abstraction by using car, cdr, and list-library functions directly on “add expressions” Silent likely error: (define xs (list (add (const 1)(const 4)) …)) (car (car xs)) Can make data that add? wrongly answers #t to (cons 'add "I am not an add") Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Summary of advantages Struct approach: Is better style and more concise for defining data types Is about equally convenient for using data types But much better at timely errors when misusing data types Cannot accessor functions on wrong kind of data Cannot confuse tester functions Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages More with abstraction Struct approach is even better combined with other Racket features not discussed here: The module system lets us hide the constructor function to enforce invariants List-approach cannot hide cons from clients Dynamically-typed languages can have abstract types by letting modules define new types! The contract system lets us check invariants even if constructor is exposed For example, fields of “an add” must also be “expressions” Jan-Mar 2013 Dan Grossman, Programming Languages

Dan Grossman, Programming Languages Struct is special Often we end up learning that some convenient feature could be coded up with other features Not so with struct definitions: A function cannot introduce multiple bindings Neither functions nor macros can create a new kind of data Result of constructor function returns #f for every other tester function: number?, pair?, other structs’ tester functions, etc. Jan-Mar 2013 Dan Grossman, Programming Languages