Created
July 1, 2010 19:34
-
-
Save fogus/460441 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn sqr [n] (* n n)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(sqr-8bit 100) | |
; java.lang.AssertionError: Assert failed: (< % 256) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(def sqr-contract | |
(contract | |
[num] | |
:requires | |
number? | |
(not= 0 num) | |
:ensures | |
pos? number?)) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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