Skip to content

Commit 041e05f

Browse files
committed
documenting recursive contracts
1 parent 1ee3c1f commit 041e05f

File tree

2 files changed

+28
-2
lines changed

2 files changed

+28
-2
lines changed

doc/main/contracts.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ <h1 class="title">Contracts.js Documentation</h1>
139139
<li><a href="#or"><span class="toc-section-number">4.5.1</span> <code>or</code></a></li>
140140
</ul></li>
141141
<li><a href="#naming-contracts"><span class="toc-section-number">4.6</span> Naming Contracts</a></li>
142-
<li><a href="#parametric-polymorphism"><span class="toc-section-number">4.7</span> Parametric Polymorphism</a></li>
142+
<li><a href="#recursive-contracts"><span class="toc-section-number">4.7</span> Recursive Contracts</a></li>
143+
<li><a href="#parametric-polymorphism"><span class="toc-section-number">4.8</span> Parametric Polymorphism</a></li>
143144
</ul></li>
144145
<li><a href="#faq"><span class="toc-section-number">5</span> FAQ</a><ul>
145146
<li><a href="#do-i-have-to-use-macros"><span class="toc-section-number">5.1</span> Do I have to use macros?</a></li>
@@ -424,7 +425,14 @@ <h2 id="naming-contracts"><span class="header-section-number">4.6</span> Naming
424425

425426
@ (NumId, Num) -&gt; Num
426427
function (f, x) { return f(x); }</code></pre>
427-
<h2 id="parametric-polymorphism"><span class="header-section-number">4.7</span> Parametric Polymorphism</h2>
428+
<h2 id="recursive-contracts"><span class="header-section-number">4.7</span> Recursive Contracts</h2>
429+
<p>You can define contracts that have a recursive definition naturally:</p>
430+
<pre class="js"><code>@ let MyObj = Null or {
431+
a: Num,
432+
b: MyObj
433+
}</code></pre>
434+
<p>This definition checks that the <code>b</code> property is either a <code>null</code> or an object that satisfies the <code>{a: Num, b: MyObj}</code> contract. Note that this will explore the entire object each time a value crosses the contract boundary so it could be potentially expensive if the object is deeply nested.</p>
435+
<h2 id="parametric-polymorphism"><span class="header-section-number">4.8</span> Parametric Polymorphism</h2>
428436
<p>Note: requires proxies (so use Firefox out of the box or Chrome/V8/node with the <code>--harmony</code> flag).</p>
429437
<p>Parametric polymorphic functions can be defined using <code>forall</code>:</p>
430438
<pre class="js"><code>@ forall &lt;name (,) ...&gt; &lt;contract&gt;</code></pre>

doc/main/contracts.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ tricky invariants.
328328
329329
330330
331+
331332
## Array Contracts
332333
333334
Contracts on arrays use the familiar array literal notation:
@@ -415,6 +416,23 @@ this, you can use `let` after the `@` symbol:
415416
function (f, x) { return f(x); }
416417
```
417418
419+
## Recursive Contracts
420+
421+
You can define contracts that have a recursive definition naturally:
422+
423+
```js
424+
@ let MyObj = Null or {
425+
a: Num,
426+
b: MyObj
427+
}
428+
```
429+
430+
This definition checks that the `b` property is either a `null` or an
431+
object that satisfies the `{a: Num, b: MyObj}` contract. Note that
432+
this will explore the entire object each time a value crosses the
433+
contract boundary so it could be potentially expensive if the object
434+
is deeply nested.
435+
418436
## Parametric Polymorphism
419437
420438
Note: requires proxies (so use Firefox out of the box or

0 commit comments

Comments
 (0)