20
Dilbert, by Scott Adams

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

  • Upload
    others

  • View
    4

  • Download
    0

Embed Size (px)

Citation preview

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

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

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

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

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

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

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

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

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

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

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

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

(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

(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

(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

(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

(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

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

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

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

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