dilbert, by scott adams - san jose state universityaustin/cs152-fall16/slides/cs152-day08... ·...
TRANSCRIPT
![Page 1: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/1.jpg)
Dilbert, by Scott Adams
![Page 2: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/2.jpg)
CS 152: Programming Language Paradigms
Prof. Tom Austin San José State University
Contracts
![Page 3: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/3.jpg)
What is a contract?
![Page 4: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/4.jpg)
Contracts: • clearly delineate obligations and
responsibilities for each party • let each party know what guarantees
to expect from others • establish who is to blame when
something goes wrong
![Page 5: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/5.jpg)
So what is a programming contract?
I can sort that list of numbers in three
weeks for $4K, cash up front.
![Page 6: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/6.jpg)
Programming contracts focus on blame
Did the caller pass the wrong arguments?
Or did the function/module return the wrong thing?
![Page 7: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/7.jpg)
A programming contract consists of: • Preconditions: requirements for the input – if they do not hold, we blame the caller
• Postconditions: promises for the output – if they do not hold, we blame the library
![Page 8: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/8.jpg)
Contracts in Racket
![Page 9: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/9.jpg)
Review: modules
(provide add-one fav-num) (define fav-num 42) (define (add-one x) (+ x 1)) (define (dec-one x) (- x 1))
add-one and fav-num are
public.
![Page 10: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/10.jpg)
Using a module
(require "math-module.rkt") (add-one fav-num) ; evaluates to 43 (dec-one 10)
Import module from specified file
dec-one: unbound identifier in module in: dec-one
![Page 11: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/11.jpg)
Contracts & Boundaries
• Modules provide natural boundaries between code. • contract-out specifies the requirements.
![Page 12: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/12.jpg)
(provide (contract-out [fav-num number?]) ) (define fav-num "four") (define (add-one x) (+ x 1)) (define (dec-one x) (- x 1))
The module guarantees that fav-num is a
number
But…
![Page 13: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/13.jpg)
(require "math-module.rkt") (+ fav-num 1)
fav-num: broke its contract promised: number? produced: "four" in: number? contract from: …/math-module.rkt blaming: …/math-module.rkt at: …/math-module.rkt:6.11
![Page 14: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/14.jpg)
(provide (contract-out [fav-num number?] [add-one (-> number? number?)])) (define fav-num 42) (define (add-one x) (+ x 1)) (define (dec-one x) (- x 1))
-> is a contract combinator.
It combines a contract on the
input with a contract on the
output
![Page 15: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/15.jpg)
(provide (contract-out [fav-num number?] [add-one (number? . -> . number?)])) (define fav-num 42) (define (add-one x) (+ x 1)) (define (dec-one x) (- x 1))
Alternate format.
![Page 16: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/16.jpg)
(require "math-module.rkt") (add-one "forty-nine")
add-one: contract violation expected: number? given: "forty-nine" in: the 1st argument of (-> number? number?) contract from: …/math-module.rkt blaming: …/using-contract.rkt at: …/math-module.rkt:7.11
![Page 17: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/17.jpg)
Contracts on Functions
• The good: able to catch internal errors. • The bad: repeats checking of
contracts – degrades performance.
![Page 18: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/18.jpg)
Contract on a function
(define/contract (make-num-pair x y)
(-> number? number? pair?) (cons x y))
![Page 19: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/19.jpg)
Quicksort (in-class)
![Page 20: Dilbert, by Scott Adams - San Jose State Universityaustin/cs152-fall16/slides/CS152-Day08... · 2016. 9. 21. · Dilbert, by Scott Adams . CS 152: Programming Language Paradigms Prof](https://reader035.vdocument.in/reader035/viewer/2022071512/6131c3c31ecc51586944f0e8/html5/thumbnails/20.jpg)
Contracts & structs lab
• Implement contracts for a banking account module. • See Canvas for details. • For more information on
contracts, see http://docs.racket-lang.org/guide/contracts.html