Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Working on teasing out the docs for contract

  • Loading branch information...
commit bdafc1dfa75c08a1430af9b0809a0397f59181f2 1 parent 4c537a3
Fogus fogus authored
Showing with 31 additions and 18 deletions.
  1. +31 −18 docs/contract.org
49 docs/contract.org
View
@@ -67,30 +67,43 @@ TODO
And all appears to be in order -- except:
-<div class="gist">
- <div class="gist-file">
- <div class="gist-data gist-syntax">
- <div class="gist-highlight">
-<pre><div class="line" id="LC1"><span class="p">(</span><span class="nf">sqr-8bit</span> <span class="mi">100</span><span class="p">)</span></div><div class="line" id="LC2"><span class="c1">; java.lang.AssertionError:</span></div><div class="line" id="LC4"><span class="c1">; Assert failed: (&lt; % 256)</span></div></pre>
- </div>
- </div>
- </div>
-</div>
+#+begin_src clojure
+(sqr-8bit 100)
+;; java.lang.AssertionError:
+;; Assert failed: (< % 256)
+#+end_src
That is, calling the function ~sqr-8bit~ with ~100~ causes a *post-condition* failure! The reason for this is because the underlying ~sqr~ is the same old arbitrary-precision version when what we really want is a function that deals in only 8-bit values. There are two possible ways to do this:
1. Create a version of ~sqr-8bit~ that does in fact deal in 8-bit values
2. Tighten the constraint on ~constrained-sqr~ further by applying another contract
-<div class="gist">
- <div class="gist-file">
- <div class="gist-data gist-syntax">
- <div class="gist-highlight">
-<pre><div class="line" id="LC1"><span class="p">(</span><span class="k">def </span><span class="nv">sqr-8bit-contract</span></div><div class="line" id="LC2">&nbsp;&nbsp;<span class="p">(</span><span class="nf">contract</span> <span class="nv">atari-constraints</span></div><div class="line" id="LC3">&nbsp;&nbsp;&nbsp;&nbsp;<span class="s">"Defines the constraints for Atari 2600 sqr"</span></div><div class="line" id="LC4">&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">[</span><span class="nv">n</span><span class="p">]</span> <span class="p">[(</span><span class="nb">&lt; </span><span class="nv">n</span> <span class="mi">16</span><span class="p">)</span> <span class="nv">integer?</span> <span class="nv">pos?</span> <span class="nv">=&gt;</span> <span class="p">(</span><span class="nb">&lt; </span><span class="nv">%</span> <span class="mi">256</span><span class="p">)]))</span></div><div class="line" id="LC5"><br/></div><div class="line" id="LC6"><span class="p">(</span><span class="k">def </span><span class="nv">sqr-8bit</span></div><div class="line" id="LC7">&nbsp;&nbsp;<span class="p">(</span><span class="nf">with-constraints</span></div><div class="line" id="LC8">&nbsp;&nbsp;&nbsp;&nbsp;<span class="nv">constrained-sqr</span> </div><div class="line" id="LC9">&nbsp;&nbsp;&nbsp;&nbsp;<span class="nv">sqr-8bit-contract</span><span class="p">))</span></div><div class="line" id="LC10"><br/></div><div class="line" id="LC12"><span class="p">(</span><span class="nf">sqr-8bit</span> <span class="mi">15</span><span class="p">)</span></div><div class="line" id="LC13"><span class="c1">;=&gt; 225</span></div><div class="line" id="LC14"><br/></div><div class="line" id="LC15"><span class="p">(</span><span class="nf">sqr-8bit</span> <span class="mi">-5</span><span class="p">)</span></div><div class="line" id="LC16"><span class="c1">; java.lang.AssertionError: </span></div><div class="line" id="LC17"><span class="c1">; Assert failed: (pos? n)</span></div><div class="line" id="LC18"><br/></div><div class="line" id="LC19"><span class="p">(</span><span class="nf">sqr-8bit</span> <span class="mf">15.9687194</span><span class="p">)</span></div><div class="line" id="LC20"><span class="c1">; java.lang.AssertionError: </span></div><div class="line" id="LC21"><span class="c1">; Assert failed: (integer? n)</span></div><div class="line" id="LC22"><br/></div><div class="line" id="LC23"><span class="p">(</span><span class="nf">sqr-8bit</span> <span class="mi">16</span><span class="p">)</span></div><div class="line" id="LC24"><span class="c1">; java.lang.AssertionError: </span></div><div class="line" id="LC25"><span class="c1">; Assert failed: (&lt; n 16)</span></div></pre>
- </div>
- </div>
- </div>
-</div>
+#+begin_src clojure
+(def sqr-8bit-contract
+ (contract atari-constraints
+ "Defines the constraints for Atari 2600 sqr"
+ [n] [(< n 16) integer? pos? => (< % 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)
+#+end_src
Using ~contract~ and ~with-constraints~ you were able to tighten the constraints on both the pre- and post-conditions of the ~sqr~ function. However, what if you wanted to relax the requirements? Stay tuned.
Please sign in to comment.
Something went wrong with that request. Please try again.