Skip to content

Instantly share code, notes, and snippets.

@fogus
Created July 1, 2010 19:34
Show Gist options
  • Save fogus/460441 to your computer and use it in GitHub Desktop.
Save fogus/460441 to your computer and use it in GitHub Desktop.
(def constrained-sqr
(with-constraints
sqr
sqr-contract))
(constrained-sqr 5)
;=> 25
(constrained-sqr -5)
;=> 25
(constrained-sqr 0)
; java.lang.AssertionError: Assert failed: (not= 0 num)
(constrained-sqr :a)
; java.lang.AssertionError: Assert failed: (number? num)
(defn sqr [n] (* n n))
(sqr-8bit 100)
; java.lang.AssertionError: Assert failed: (< % 256)
(def sqr-8bit-contract
(contract
[n]
:ensures
(< % 256)))
(def sqr-8bit
(with-constraints
constrained-sqr
sqr-8bit-contract))
(sqr-8bit 5)
;=> 25
(sqr-8bit 0)
; java.lang.AssertionError: Assert failed: (not= 0 num)
(def sqr-contract
(contract
[num]
:requires
number?
(not= 0 num)
:ensures
pos? number?))
(def sqr-8bit-contract
(contract
[n]
:requires
(< n 16)
integer?
pos?
:ensures
(< % 256)))
(def sqr-8bit
(with-constraints
constrained-sqr
sqr-8bit-contract))
(sqr-8bit 15)
;=> 225
(sqr-8bit -5)
;=> java.lang.AssertionError: Assert failed: (pos? n)
(sqr-8bit 15.9687194)
; java.lang.AssertionError: Assert failed: (integer? n)
(sqr-8bit 16)
; java.lang.AssertionError: Assert failed: (< n 16)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment