CSE341: Programming Languages Lecture 24 Racket Modules, Abstraction with Dynamic Types; Racket Contracts Dan Grossman Fall 2011.

Slides:



Advertisements
Similar presentations
Optional Static Typing Guido van Rossum (with Paul Prescod, Greg Stein, and the types-SIG)
Advertisements

CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Winter 2013.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
CSE341: Programming Languages Lecture 19 Introduction To Ruby; Dynamic OOP; "Duck Typing" Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 11 Closures-ish Java & C Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 12 Modules Dan Grossman Fall 2011.
CSE341: Programming Languages Lecture 17 Structs, Implementing Languages, Implementing Higher-Order Functions Dan Grossman Fall 2011.
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Abstract Classes and Interfaces
Cs205: engineering software university of virginia fall 2006 Data Abstraction David Evans
CSE 341 Programming Languages Racket Datatype Style Programming Zach Tatlock Spring 2014.
Programming LanguagesSection 41 Programming Languages Section 4. SML Remaining Topics Xiaojuan Cai Spring 2015.
CSE 413 Programming Languages & Implementation Hal Perkins Autumn 2012 Late binding and dynamic dispatch (Based on CSE 341 slides by Dan Grossman) 1.
CSE 413 Languages & Implementation Hal Perkins Autumn 2012 Structs, Implementing Languages (credits: Dan Grossman, CSE 341) 1.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Today: –Review declaration, implementation, simple class structure. –Add an exception class and show.
PROGRAMMING PRE- AND POSTCONDITIONS, INVARIANTS AND METHOD CONTRACTS B MODULE 2: SOFTWARE SYSTEMS 13 NOVEMBER 2013.
CSE 341 : Programming Languages Lecture 12 ML Modules Zach Tatlock Spring 2014.
1 Data Abstraction. Pairs and Lists. (SICP Sections – 2.2.1)
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
CSE 341 : Programming Languages Lecture 9 Lexical Scope, Closures Zach Tatlock Spring 2014.
מבוא מורחב שיעור 7 1 Lecture 7 Data Abstraction. Pairs and Lists. (Sections – 2.2.1)
CPSC 252 ADTs and C++ Classes Page 1 Abstract data types (ADTs) An abstract data type is a user-defined data type that has: private data hidden inside.
Abstraction A way of managing complexity for large programs A means of separating details of computation from use of computation Types of Abstraction Data.
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2017.
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2017.
Pairs and Lists. Data Abstraction. SICP: Sections – 2.2.1
Programming Languages Dan Grossman 2013
Agenda Warmup AP Exam Review: Litvin A2
Programming Languages Dan Grossman 2013
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Winter 2013.
slides created by Marty Stepp
CSE341: Programming Languages Lecture 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Zach Tatlock Winter 2018.
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 8 Lexical Scope and Function Closures Dan Grossman Winter 2013.
CSE341: Programming Languages Lecture 10 ML Modules
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.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2013.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2016.
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Zach Tatlock Winter 2018.
CSE 341 Section 5 Winter 2018.
CSE341: Programming Languages Lecture 14 Thunks, Laziness, Streams, Memoization Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2018.
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 10 ML Modules
CSE341: Programming Languages Lecture 17 Implementing Languages Including Closures Dan Grossman Spring 2016.
Adapted from slides by Nicholas Shahan and Dan Grossman
CSE341: Programming Languages Lecture 10 ML Modules
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.
Today’s topics Abstractions Procedural Data
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Autumn 2017.
CSE341: Programming Languages Lecture 10 ML Modules
CSE341: Programming Languages Lecture 8 Lexical Scope and Function Closures Dan Grossman Spring 2019.
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
CSE341: Programming Languages Lecture 10 ML Modules
Brett Wortzman Summer 2019 Slides originally created by Dan Grossman
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 16 Datatype-Style Programming With Lists or Structs Dan Grossman Spring 2019.
Presentation transcript:

CSE341: Programming Languages Lecture 24 Racket Modules, Abstraction with Dynamic Types; Racket Contracts Dan Grossman Fall 2011

Another modules lecture Recall lecture 12: SML modules. Key points: –Namespace management for larger programs (structures) –Hiding bindings inside the module ( gcd, reduce ) –Using an abstract type to enforce invariants Fall 20112CSE341: Programming Languages signature RATIONAL = sig type rational exception BadFrac val make_frac : int * int -> rational val add : rational * rational -> rational val toString : rational -> string end structure Rational :> RATIONAL = …

Racket is different More flexible namespace management –Convenient ways to rename during export/import –(In other languages, could write wrapper modules) Dynamic typing still has ways to create abstract types –Just need to be able to make a new type at run-time –This is what struct does; Scheme has nothing like it By default, each file is a module –Not necessary but convenient State-of-the-art contract system –Arbitrary dynamic checks of cross-module calls with blame assignment Fall 20113CSE341: Programming Languages

But first… Worth emphasizing that modules are not necessary for creating abstract types: local scope and closures are enough Recall our rationals example (but note Racket has built-in rationals): Interface: –make-frac rejects 0 denominator –add adds two rationals –print-rat prints a rational in reduced form Can implement this by maintaining these invariants: –num and den fields kept in reduced form –den is always positive Fall 20114CSE341: Programming Languages

Wrong approach [see lec24_non_modules.rkt] This uses local scope to hide gcd and reduce, but it exposes the rat constructor, so clients can make bad rationals –So to be "safe", add and print-rat can re-check invariants Fall 20115CSE341: Programming Languages (struct (rat num den) (define rat-funs (letrec ([gcd (lambda (x y) …)] [reduce (lambda (x y) …)] [make-frac (lambda (x y) …)] [add (lambda (r1 r2) …)] [print-rat (lambda (r) …)]) (list make-frac add print-rat))) (define make-frac (car rat-funs)) (define add (cadr rat-funs)) (define print-rat (caddr rat-funs))

Right approach [see lec24_non_modules.rkt] So we also need to hide the rat constructor! –Also hide mutators if you create them –Choose to hide accessors to keep representation opaque –This code doesn't "export" rat?, but doing so a good idea Fall 20116CSE341: Programming Languages (define rat-funs (let () (struct (rat num den) (define (gcd x y) …) (define (reduce x y) …) (define (make-frac x y) …) (define (add r1 r2) …) (define (print-rat r) …) (list make-frac add print-rat))) (define make-frac (car rat-funs)) (define add (cadr rat-funs)) (define print-rat (caddr rat-funs))

The key trick By hiding the constructor and accessors, clients cannot make rationals or access their pieces directly Clients can still pass non-rationals to add or print-rat, but any rational will satisfy the invariants Technique requires fundamentally on semantics of struct –Make a new (dynamic) type of thing –If struct were sugar for cons cells, then clients could use cons to make bad rationals So… to support abstract datatypes, dynamically typed languages need ways to make "new types of things" –Scheme traditionally had no such support Again, making rat? public makes perfect sense Fall 20117CSE341: Programming Languages

Racket modules The normal and convenient way puts bindings in a file and provides only the ones that should be public –Unlike SML, no separate notion of signature – module decides what to provide Default is private –(But REPL for "Run" of a file is "inside" that file's module) –Which is why previous lectures used (provide (all-provided-out)) –Can provide some of struct's functions See lec24_rationals.rkt –(provide make-frac add print-rat rat?) Fall 20118CSE341: Programming Languages

It's the same trick Modules take care of hiding bindings struct takes care of making a new type This doesn't work if rationals are implemented with an existing type like cons –Clients could use cons? to figure that out and then make bad rationals Common misconception: Dynamically typed languages can't support abstract types –Some may not, but they could Fall 20119CSE341: Programming Languages

Using modules [see lec24_client.rkt] Clients get a module's bindings with the require form –Many variations, using a file-name string is the simplest –Can also get only the bindings you want, either by listing them with the only-in syntax or listing what you don't want with the except-in syntax Convenient for avoiding name conflicts See the manual for details –Can also rename bindings: rename-in and prefix-in The provider can also rename when exporting Overall: convenient namespace management is a nice thing Fall CSE341: Programming Languages (require "rationals.rkt")

Contracts A contract is a pre- and post-condition for a function –Software methodology of "design-by-contract" –If a function fails, blame either the caller or callee Old idea; Racket's modules on the cutting edge Can provide functions with a contract –Any predicate (boolean-returning function) on arguments and result –Any cross-module call will have its arguments and result checked at run-time (could be expensive) to assign blame Intra-module calls (e.g., recursion) not checked (You're not responsible for the details, just the high-level idea) Fall CSE341: Programming Languages

Example lec24_rationals_contracts.rkt provides another implementation of a rationals library with contracts on each export It maintains different (weaker) invariants, putting more work on clients, with contracts checking that work: Exports rat constructor, but contract requires integer arguments and positive denominator from client –Maintains these invariants Exports rat-num, rat-den, and rat? Does not keep rationals in reduced form –add doesn't care and doesn't reduce –print-rat does care (contract checks it); up to client to either call reduce-rat or "know" the rational is reduced Fall CSE341: Programming Languages

Example provide (Note: needs DrRacket 5.2) contract-out exports bindings with given contracts -> takes predicate functions for each argument/result and checks them on inter-module calls at run-time –Can use library functions or our own (e.g., reduced-rat ) Client must satisfy argument contracts and can assume result contracts Fall CSE341: Programming Languages (provide (contract-out (rat (-> integer? (lambda(y)(and(integer? y)(> y 0))) rat?) (rat-num (-> rat? integer?)) (rat-den (-> rat? integer?)) (rat? (-> any/c boolean?)) (add (-> rat? rat? rat?)) (print-rat (-> reduced-rat void?)) (reduce-rat (-> rat? reduced-rat))))

Contracts vs. invariants If you set up strong abstractions and maintain invariants, then you need to do less run-time contract checking –Example: No need for reduced-rat to check that the rational fields are integers with positive denominator This is more efficient: only check dynamically what could fail if "the other party in the contract" is wrong –Of course, "redundant" checks are less redundant if your abstractions are leaky due to poor design / bugs Invariants are not an argument against contracts –The two are for different purposes, as in our example Fall CSE341: Programming Languages