<html><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space; ">
<div><br></div><div><br></div><div><div>On Nov 6, 2008, at 10:53 PM, David Yrueta wrote:</div><br class="Apple-interchange-newline"><blockquote type="cite"><span style="font-size:small"><span><span><span></span></span></span></span></blockquote><br></div><div>Think of cons as a function with contract </div><div><br></div><div> X [Listof X] -> [Listof X]</div><div><br></div><div>The key is to start this "pattern matching exercises" by using variables for the various components of the contract: </div><div><br></div><div>1. natural-f takes four arguments and returns the third one. Also the fourth one is guaranteed to be a Nat and the first is a function of two args: </div><div><br></div><div> (X Y -> Z) W U Nat -> U</div><div><br></div><div>2. The result of f is also a possible result of the function, so let's say Z = U</div><div><br></div><div> (X Y -> U) W U Nat -> U </div><div><br></div><div>3. The second input to f is the result of natural-f, so Y = U </div><div><br></div><div> (X U -> U) W U Nat -> U </div><div><br></div><div>4. The second argument is used as the first input to f so U = W </div><div><br></div><div> (X U -> U) U U Nat -> U </div><div><br></div><div>5. No other constraints so we're fine </div><div><br></div><div>And I may have made a mistake so check the calculation -- Matthias</div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br></div><div><br><blockquote type="cite"><span style="font-size:small"><span><span><span>If function "natural-f" --</span></span></span></span><div><span style="font-size:small"><span><span><span><br> </span> </span><span> </span></span></span></div><div><div><span style="font-size:small"><span><span><span>;apply function f to x n times with a base case of TH where n=0</span></span></span></span></div> <div><span style="font-size:small"><span><span><span>;(check-expect (natural-f cons 'x empty 5) (list 'x 'x 'x 'x 'x))</span></span></span></span></div> <div><span style="font-size:small"><span><span><span>;(check-expect (natural-f + 1 5 9) 14)</span></span></span></span></div><div><span style="font-size:small"><span><span><span>(define (natural-f f x TH n)</span></span></span></span></div> <div><span style="font-size:small"><span><span><span> (cond</span></span></span></span></div><div><span style="font-size:small"><span><span><span> [(zero? n) TH]</span></span></span></span></div> <div><span style="font-size:small"><span><span><span> [else (f x</span></span></span></span></div><div><span style="font-size:small"><span><span><span> (natural-f f x TH (sub1 n) ))]))</span></span></span></span></div> <div><span><span><span><br></span></span></span></div><div><span><span><span>-- properly abstracts over the the functions </span></span></span></div> <div><span style="color:rgb(0, 128, 128)"><span><br></span></span><span style="font-size:16px"><table><tbody><tr><td><div align="left"><pre style="margin-left:2em;color:rgb(165, 42, 42)"><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>copy</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:green"><span style="font-weight:bold"><span style="font-size:small"><span><span>:</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><strong><span style="font-size:small"><span><span>N</span></span></span></strong><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>X</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><tt><span style="font-weight:bold"><span style="font-size:small"><span><span>-></span></span></span></span></tt><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>listof</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>X</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>)</span></span></span></span></code></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
</span></span></span></span><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; to create a list that contains</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
</span></span></span></span><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>obj</span></span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> times</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>define</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>copy</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>obj</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>)
(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>cond</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
[(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>zero?</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>) </span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>empty</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>]
[</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>else</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>cons</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>obj</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
(</span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>copy</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>sub1</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>) </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>obj</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>))]))
</span></span></span></span></pre></div></td><td><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span></td> <td><div align="left"><pre style="margin-left:2em;color:rgb(165, 42, 42)"><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n-adder</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:green"><span style="font-weight:bold"><span style="font-size:small"><span><span>:</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><strong><span style="font-size:small"><span><span>N</span></span></span></strong><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>number</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><tt><span style="font-weight:bold"><span style="font-size:small"><span><span>-></span></span></span></span></tt><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>number</span></span></span></span></span></code></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
</span></span></span></span><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; to add </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> to </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>x</span></span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> using</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
</span></span></span></span><span style="color:teal"><span style="font-weight:bold"><span style="font-size:small"><span><span>;; </span></span></span></span><code style="color:rgb(165, 42, 42)"><span style="font-weight:bold"><span style="font-size:small"><span><span>(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>+</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:green"><span style="font-weight:bold"><span style="font-size:small"><span><span>1</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> ...)</span></span></span></span></code><span style="font-weight:bold"><span style="font-size:small"><span><span> only</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>define</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n-adder</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>x</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>)
(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>cond</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
[(</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>zero?</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>) </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>x</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>]
[</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>else</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>+</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:green"><span style="font-weight:bold"><span style="font-size:small"><span><span>1</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>
(</span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n-adder</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> (</span></span></span></span><span style="color:rgb(153, 0, 0)"><span style="font-weight:bold"><span style="font-size:small"><span><span>sub1</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span> </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>n</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>) </span></span></span></span><span style="color:navy"><span style="font-weight:bold"><span style="font-size:small"><span><span>x</span></span></span></span></span><span style="font-weight:bold"><span style="font-size:small"><span><span>))]))</span></span></span></span></pre> </div></td></tr></tbody></table></span></div><div><span><span style="font-size:small"><span><span>--then what is the general contract for "natural-f"?</span></span></span></span></div> <div><span><span style="font-size:small"><span><span><br></span></span></span></span></div><div><span><span style="font-size:small"><span><span>Considering "natural-f" as an abstraction of function "n-adder" above, the resulting contract would be:</span></span></span></span></div> <div><span><span style="font-size:small"><span style="text-decoration:underline"><span><span>(number number -> number) number number number -> number </span></span></span></span></span><span style="font-size:small"><span><span><br> </span> </span><span> </span></span></div><div><span><span>(ex. (check-expect (natural-f + 1 5 9) 14)</span></span></div><div><span style="font-size:16px"><div><span style="font-size:small"><span><span><br> </span> </span></span></div><div><span style="font-size:small"><span><span>But what would the contract for "natural-f" be if it was viewed as an abstraction of function "copy?"</span></span></span></div> <div><span style="font-size:13px"><span><span>(ex.(check-expect (natural-f cons 'x empty 5) (list 'x 'x 'x 'x 'x))</span></span></span></div> <div><span style="font-size:small"><span><span><br></span></span></span></div><div><span style="font-size:small"><span><span>In that case, argument "f" would take the value of "cons," so the question (for me) comes down to how to represent the primitive "cons" as a function type within a contract. </span></span></span></div> <div><span style="font-size:small"><span><span><br></span></span></span></div><div><span style="font-size:small"><span><span>Is "cons" represented as (X Y -> Y)</span></span></span></div> <div><span style="font-size:small"><span><span>where X is any Scheme ITEM, Y is the Scheme-value list, and the result is basic type Y (Scheme-value list)?</span></span></span></div> <div><span style="font-size:small"><span><span><br></span></span></span></div><div><span style="font-size:small"><span><span>Or is "cons" represented as (X list -> (listof X)</span></span></span></div> <div> <span style="font-size:13px"><span><span>where X is any Scheme ITEM, "list" is Scheme-value list, and the result is defined type (listof X)?</span></span></span></div> <div><span style="font-size:13px"><span><span><br></span></span></span></div><div><span style="font-size:13px"><span><span>The difference between the two contracts appears to be in the output types. The first output type is basic, which is consistent with the contract output for "n-adder"; the second output type is defined, which conflicts with "n-adder." Avoiding conflict means choosing the first representation of "cons" in place of f in "natural-f"'s contract. S my guess is that the proper general contract for "natural-f" would be:</span></span></span></div> <div><span style="font-size:13px"><span><span><br></span></span></span></div><div><span style="font-size:13px"><span style="font-weight:bold"><span><span>;natural-f: (X Y -> Y) X Y number -> Y</span></span></span><span><span><br> </span> </span></span></div><div><span style="font-size:13px;font-weight:bold"><span><span><br></span></span></span></div><div><span style="font-size:13px"><span><span>Is this right? If not, where am I going wrong?</span></span></span></div> <div><span style="font-size:13px"><span><span><br></span></span></span></div><div><span style="font-size:13px"><span><span>Thanks,</span></span></span></div> <div><span style="font-size:13px"><span><span>Dave </span></span></span></div><div><br></div><div> </div><div><br></div><div><br> </div><div><br></div><div><br></div></span></div><div><br></div><div><br> </div><div><span style="font-size:16px"><br></span></div><div><span style="font-size:16px"><br> </span><div align="left"><pre style="margin-left:2em;color:rgb(165, 42, 42)"></pre></div></div></div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; ">_________________________________________________</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span class="Apple-converted-space"> </span>For list-related administrative tasks:</div><div style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; "><span class="Apple-converted-space"> </span><a href="http://list.cs.brown.edu/mailman/listinfo/plt-scheme">http://list.cs.brown.edu/mailman/listinfo/plt-scheme</a></div> </blockquote></div><br></body></html>