<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:sy="http://purl.org/rss/1.0/modules/syndication/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" version="2.0">

<channel>
	<title>Arcadian Visions</title>
	
	<link>http://www.arcadianvisions.com/blog</link>
	<description>(\ blogs -&gt; blogs + 1)</description>
	<lastBuildDate>Sun, 27 Sep 2009 22:31:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ArcadianVisions" /><feedburner:info xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" uri="arcadianvisions" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Classy Units</title>
		<link>http://www.arcadianvisions.com/blog/?p=149</link>
		<comments>http://www.arcadianvisions.com/blog/?p=149#comments</comments>
		<pubDate>Sun, 27 Sep 2009 22:31:44 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.arcadianvisions.com/blog/?p=149</guid>
		<description><![CDATA[Haskell type classes are a very nice take on ad hoc polymorphism. Much like overloading in object oriented languages, type classes provide a mechanism for different implementations of the same conceptual operation to go by the same name.
A main difference is the total separation of interface from state representation. With class-based object orientation, programmers are [...]]]></description>
			<content:encoded><![CDATA[<p>Haskell <a href="http://www.haskell.org/tutorial/classes.html">type classes</a> are a very nice take on ad hoc polymorphism. Much like overloading in object oriented languages, type classes provide a mechanism for different implementations of the same conceptual operation to go by the same name.</p>
<p>A main difference is the total separation of interface from state representation. With class-based object orientation, programmers are often encouraged to define some thing, a class, that is a bit of state along with operations for manipulating that state in various ways. At some point, the programmer may want variations of those manipulations, and so can delve into inheritance. If more flexibility is needed (namely, the state representation shouldn&#8217;t be inherited), the programmer can turn to interfaces in languages like C#, Java, etc.</p>
<p>Now the programmer can program to the interface, and have wildly different state representations implement that interface. Type classes support that style of development in Haskell, but one can gain some of the same operational benefits by working with <a href="http://docs.plt-scheme.org/guide/units.html">Units in PLT Scheme</a>. Since I&#8217;ve <a href="http://www.arcadianvisions.com/blog/?p=71">talked</a> about Units <a href="http://www.arcadianvisions.com/blog/?p=135">before</a>, let&#8217;s just get right into this example of their usage.</p>
<p>Suppose I want to overload arithmetic operators in a very ad hoc way. More specifically, when I define a new data structure I want the freedom to define addition over that data structure such that I can write another function that uses the <code>+</code> operator without worrying about having to call a different <code>+</code> for different data structures. There are many ways of solving this kind of problem, this is but one.</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;">#lang scheme
<span style="color: #660000;">&#40;</span>require <span style="color: #660000;">&#40;</span>prefix<span style="color: #000000;">-</span>in base: <span style="color: #660000;">&#40;</span>only<span style="color: #000000;">-</span>in scheme <span style="color: #000000;">+</span> <span style="color: #000000;">/</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;typeclass-units.ss&quot;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; A class of things for which addition is defined.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> addable<span style="color: #000000;">^</span> <span style="color: #660000;">&#40;</span>zero <span style="color: #000000;">+</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; A class of things for which division is defined.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> divisible<span style="color: #000000;">^</span> <span style="color: #660000;">&#40;</span><span style="color: #000000;">/</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Standard arithmetic on numbers.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-instance</span> <span style="color: #660000;">&#40;</span>addable<span style="color: #000000;">^</span> divisible<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span> num@
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> zero <span style="color: #cc66cc;">0</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #000000;">+</span> base:<span style="color: #000000;">+</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #000000;">/</span> base:<span style="color: #000000;">/</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Addition over strings is defined as string-append.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-instance</span> addable<span style="color: #000000;">^</span> string@
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> zero <span style="color: #008800;">&quot;&quot;</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #000000;">+</span> <span style="color: #0000ff; font-weight: bold;">string-append</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; The &quot;+&quot; function is a generic addition operation.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-constrained</span> <span style="color: #660000;">&#40;</span><span style="color: #000000;">+</span> <span style="color: #660000;">&#40;</span>addable<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span> <span style="color: #000000;">=&gt;</span> x y<span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #000000;">+</span> x y<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span><span style="color: #000000;">+</span> num@<span style="color: #660000;">&#41;</span> <span style="color: #cc66cc;">3</span> <span style="color: #cc66cc;">4</span><span style="color: #660000;">&#41;</span>              <span style="color: #666600; font-style: italic;">; Adding numbers</span>
<span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span><span style="color: #000000;">+</span> string@<span style="color: #660000;">&#41;</span> <span style="color: #008800;">&quot;hi &quot;</span> <span style="color: #008800;">&quot;there&quot;</span><span style="color: #660000;">&#41;</span> <span style="color: #666600; font-style: italic;">; Adding strings</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; A helper combinator.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>flip f<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>Î»<span style="color: #660000;">&#40;</span>x y<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>f y x<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; The definition of the &quot;mean&quot; function requires both addition and division.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-constrained</span> <span style="color: #660000;">&#40;</span>mean <span style="color: #660000;">&#40;</span>addable<span style="color: #000000;">^</span> divisible<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span> <span style="color: #000000;">=&gt;</span> items<span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #000000;">/</span> <span style="color: #660000;">&#40;</span>foldl <span style="color: #660000;">&#40;</span>flip <span style="color: #000000;">+</span><span style="color: #660000;">&#41;</span> zero items<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">length</span> items<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Computing the mean of a list of numbers.</span>
<span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>mean num@ num@<span style="color: #660000;">&#41;</span> '<span style="color: #660000;">&#40;</span><span style="color: #cc66cc;">4</span> <span style="color: #cc66cc;">5</span> <span style="color: #cc66cc;">6</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Try giving an incompatible instance of divisible^... doesn't work.</span>
<span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>mean string@ num@<span style="color: #660000;">&#41;</span> '<span style="color: #660000;">&#40;</span><span style="color: #008800;">&quot;what &quot;</span> <span style="color: #008800;">&quot;is &quot;</span> <span style="color: #008800;">&quot;an average &quot;</span> <span style="color: #008800;">&quot;string?&quot;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>What&#8217;s going on here? Let&#8217;s start from near the bottom with the <code>mean</code> function that computes the arithmetic mean (average) of a list of items. In order to write this function the way I have chosen to write it, I require definitions of addition and division for the items in the list. My function is not just,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>mean items<span style="color: #660000;">&#41;</span> ...<span style="color: #660000;">&#41;</span></pre></div></div>

<p>as that implies that the function is defined for any type of items. Instead, I want to let the caller of this function supply me with suitable definitions of addition and division for the items in question. To this end, I define <code>mean</code> to be constrained to the cases where the caller can supply such definitions.</p>
<p>This is all tied together through the Units system. In fact, this usage of Units is quite a bit restricted, but I think the syntax for this simpler usage is commensurately simpler, so perhaps of some use.</p>
<p>To define our interface, we use stock Unit signatures such as,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> addable<span style="color: #000000;">^</span> <span style="color: #660000;">&#40;</span>zero <span style="color: #000000;">+</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>that says that an addable thing has definitions for an additive identity, zero, and a definition of the <code>+</code> operator. To implement this interface, or define an instance of the type class, we can use a form like,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-instance</span> addable<span style="color: #000000;">^</span> string@
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> zero <span style="color: #008800;">&quot;&quot;</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #000000;">+</span> <span style="color: #0000ff; font-weight: bold;">string-append</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>that defines a <code>+</code> operator and a <code>zero</code> for strings which I can use in expressions like,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span><span style="color: #000000;">+</span> string@<span style="color: #660000;">&#41;</span> <span style="color: #008800;">&quot;hi &quot;</span> <span style="color: #008800;">&quot;there&quot;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>to yield the string <code>"hi there"</code>.</p>
<p>Definitions of <code>+</code> and <code>/</code> for numbers are straightforward, but I&#8217;m not sure what I would want division over strings to mean, so I haven&#8217;t defined it. If I wanted to try computing a mean of a string, I would need to supply an instance definition of <code>divisible^</code>, so I tried giving it one for numbers at the end of that code listing&#8230; which didn&#8217;t work because the <code>/</code> from <code>num@</code> doesn&#8217;t work for strings.<br />
</p>
<hr />
<br />
Perhaps, though, agreement on a name alone is not enough. We may want to impose some limitations on how somebody can implement an interface, and thankfully we can do just this by attaching <a href="http://docs.plt-scheme.org/guide/contracts.html">contracts</a> to our <a href="http://docs.plt-scheme.org/guide/Contracts_for_Units.html">signatures</a>.</p>
<p>Let&#8217;s say that we&#8217;re in a punchy mood and define a &#8220;shape&#8221; to be anything that has an area,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> shape<span style="color: #000000;">^</span> <span style="color: #660000;">&#40;</span>area<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>We then find ourselves dealing with some joker who gave us an instance like this,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-instance</span> shape<span style="color: #000000;">^</span> bad@
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>area b<span style="color: #660000;">&#41;</span> <span style="color: #008800;">&quot;I'm not an area!&quot;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>Clearly, this is not an area.</p>
<p>To stop this sort of thing before it catches on, we can tighten our interface with a contract,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #666600; font-style: italic;">;; Shapes have area</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> shape<span style="color: #000000;">^</span>
  <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>contracted <span style="color: #660000;">&#91;</span>area <span style="color: #660000;">&#40;</span><span style="color: #000000;">-&gt;</span> any<span style="color: #000000;">/</span>c <span style="color: #0000ff; font-weight: bold;">number?</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#93;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>Now, when we call the <code>bad@</code> implementation of <code>area</code>, we get this error before the return value gets back into our code,<br />
<code>(unit bad@) broke the contract (-> any/c number?) on area; expected &lt;number?&gt;, given: "I'm not an area!"</code><br />
</p>
<hr />
<br />
Significant caveats: </p>
<ul>
<li>Instance passing is explicit, putting a burden on the caller of a constrained function.</li>
<li>The simplified Unit syntax hides the lexical binding of the instance definitions supplied to constrained functions.</li>
<li>There is no mechanism to implicitly pass supplied instance definitions to constrained functions called by other constrained functions.</li>
</ul>
<p>This means that the functions that have constraints are, with this implementation, treated as terminal. Dynamic dispatch, as with multi-methods, can offer nicer call-site syntax, but the approach to dynamically binding implementations to identifiers shown here allows for a single resolution of that dispatch to be captured in a closure. If I have a large block of code dependent on some set of signatures, I can provide the relevant implementations and get a specialized closure back. For instance, the expression,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #660000;">&#40;</span><span style="color: #000000;">+</span> string@<span style="color: #660000;">&#41;</span></pre></div></div>

<p>results in a function for which references to the <code>+</code> function are bound to the implementation defined for strings. This is just the kind of thing that Units excel at, and I hope that the uses shown here can, if nothing else, encourage others to explore specific applications of the power of Units.<br />
</p>
<hr />
<br />
While developing the macro support for this Unit usage pattern, I played with several experiments which demonstrate different approaches to working with interfaces defined in this manner, some very much like OO classes.</p>
<p>The <a href="http://www.arcadianvisions.com/downloads/Scheme/typeclass-units/headable-test.ss">simplest example</a> is one that implements a funny head function for both lists and vectors. The <a href="http://www.arcadianvisions.com/downloads/Scheme/typeclass-units/classy-arithmetic.ss">arithmetic example</a> is next up, followed by the much more object oriented <a href="http://www.arcadianvisions.com/downloads/Scheme/typeclass-units/shape-typeclass-test.ss">shapes example</a>.</p>
<p>The little macros that provide this syntax are defined in the <a href="http://www.arcadianvisions.com/downloads/Scheme/typeclass-units/typeclass-units.ss">typeclass-units</a> module.</p>
<img src="http://feeds.feedburner.com/~r/ArcadianVisions/~4/l1pw25IuHxw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.arcadianvisions.com/blog/?feed=rss2&amp;p=149</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Invoking Units: Every Which Way …</title>
		<link>http://www.arcadianvisions.com/blog/?p=135</link>
		<comments>http://www.arcadianvisions.com/blog/?p=135#comments</comments>
		<pubDate>Mon, 13 Jul 2009 05:24:53 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>

		<guid isPermaLink="false">http://www.arcadianvisions.com/blog/?p=135</guid>
		<description><![CDATA[Units in PLT Scheme offer great promise in the area of parameterizing modular code. Rather than lean on require forms (or imports in other languages) for bringing identifier bindings into scope, Units offer programmatic parameterization. Instead of editing the source code of a module that depends on some other code, one can provide that parameter [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://docs.plt-scheme.org/guide/units.html">Units</a> in PLT Scheme offer great promise in the area of parameterizing modular code. Rather than lean on <code>require</code> forms (or <code>import</code>s in other languages) for bringing identifier bindings into scope, Units offer programmatic parameterization. Instead of editing the source code of a module that depends on some other code, one can provide that parameter at, or close to, the ultimate use site of the fully realized Unit (perhaps in a module dedicated specifically to wiring together the components of an application). However, the reality isn&#8217;t always characterized by blue skies and smooth sailing. One area that has often bothered me is that if I create a Unit <code>foo</code> that depends on some unit exporting a signature <code>setup^</code> (<i>i.e.</i> foo&#8217;s setup), the process of invoking <code>foo</code> can be somewhat cumbersome.</p>
<p>More specifically, I might have some unit <code>g</code> that exports the <code>setup^</code> signature, so how do I go about invoking <code>foo</code> with <code>g</code> as a parameter? It sounds like I want something that looks like <code>(foo g)</code>, but perhaps that&#8217;s not quite right since we often want the invocation of a Unit to add bindings to the caller&#8217;s lexical context. This explains why Unit invocation is actually done by forms such as <code>define-values/invoke-unit</code> or <code>define-values/invoke-unit/infer</code>. An emerging theme is that these are rather long names! To make matters worse, the <code>/infer</code> variants of Unit functions require that argument identifiers be bound by forms like <code>define-unit</code>. But that is a serious limitation, as illustrated in <a href="http://www.arcadianvisions.com/blog/?p=71">an earlier post</a>. </p>
<p>If we set aside the <code>/infer</code> functionality, then <code>define-values/invoke-unit</code> itself leaves a bit to be desired since it requires explicit import and export annotations. But before we can even call <code>define-values/invoke-unit</code> we must link the Unit exporting <code>setup^</code> to the Unit importing it, in our case, <code>foo</code>. This requires the creation of a <code>compound-unit</code> that specifies the necessary linking. Unfortunately, while that syntax is powerfully expressive, it is also somewhat scary compared to the initial hope for invocation syntax looking like <code>(foo g)</code>.</p>
<p>To this end, I have written up several options for our example Unit invocation scenario. Each option may be uncommented (one at a time), and the entire module evaluated to see that <code>bar</code> is appropriately bound. The idea in these examples is that any helper functionality can be put into another module, while each <i>Option</i> represents a possible call-site syntax. The in-line <code>unit</code> expressions are also likely to be pulled in from elsewhere (<i>e.g.</i> one may have several back-end service providers expressed as Units that can be supplied to a client of any service exporting a particular signature).</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;">#lang scheme
&nbsp;
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> setup<span style="color: #000000;">^</span> <span style="color: #660000;">&#40;</span>baz<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> foo<span style="color: #000000;">^</span> <span style="color: #660000;">&#40;</span>bar<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-unit</span> foo@
  <span style="color: #660000;">&#40;</span>import setup<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span>export foo<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> bar <span style="color: #660000;">&#40;</span>baz <span style="color: #cc66cc;">2</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Option 1</span>
<span style="color: #666600; font-style: italic;">;; Put the setup requirements into the current namespace so that the </span>
<span style="color: #666600; font-style: italic;">;; import binding can be inferred from context.</span>
<span style="color: #666600; font-style: italic;">;(define (baz x) (+ x 40))</span>
<span style="color: #666600; font-style: italic;">;(define-values/invoke-unit/infer foo@)</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Option 2</span>
<span style="color: #666600; font-style: italic;">;; Manually create a unit to provide setup^ and link it to foo@ before </span>
<span style="color: #666600; font-style: italic;">;; invoking the compound unit to get the desired bar binding.</span>
<span style="color: #666600; font-style: italic;">;(define-values/invoke-unit </span>
<span style="color: #666600; font-style: italic;">;  (compound-unit</span>
<span style="color: #666600; font-style: italic;">;    (import)</span>
<span style="color: #666600; font-style: italic;">;    (export Foo)</span>
<span style="color: #666600; font-style: italic;">;    (link (((Setup : setup^)) (unit (import) </span>
<span style="color: #666600; font-style: italic;">;                                    (export setup^)</span>
<span style="color: #666600; font-style: italic;">;                                    (define (baz x) (+ x 40))))</span>
<span style="color: #666600; font-style: italic;">;          (((Foo : foo^)) foo@ Setup)))</span>
<span style="color: #666600; font-style: italic;">;  (import)</span>
<span style="color: #666600; font-style: italic;">;  (export foo^))</span>
&nbsp;
&nbsp;
<span style="color: #666600; font-style: italic;">;; Use a helper function to do the linking. The helper function can live in </span>
<span style="color: #666600; font-style: italic;">;; another module.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>init<span style="color: #000000;">-</span>setup setup<span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">compound-unit</span>
    <span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
    <span style="color: #660000;">&#40;</span>export Foo<span style="color: #660000;">&#41;</span>
    <span style="color: #660000;">&#40;</span>link <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>Setup : setup<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span> setup<span style="color: #660000;">&#41;</span>
          <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>Foo : foo<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span> foo@ Setup<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Option 3</span>
<span style="color: #666600; font-style: italic;">;; Use the helper function with an explicit define-values/invoke-unit. </span>
<span style="color: #666600; font-style: italic;">;; This has the benefit of hiding the linking.</span>
<span style="color: #666600; font-style: italic;">;(define-values/invoke-unit</span>
<span style="color: #666600; font-style: italic;">;  (init-setup (unit (import) (export setup^) (define (baz x) (+ x 40))))</span>
<span style="color: #666600; font-style: italic;">;  (import)</span>
<span style="color: #666600; font-style: italic;">;  (export foo^))</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; A helper macro that can reduce the call site burden to knowing how to </span>
<span style="color: #666600; font-style: italic;">;; provide the necessary setup^ bindings, but requires neither an empty </span>
<span style="color: #666600; font-style: italic;">;; (import) nor a mention of the foo^ signature. Thanks to Jon Rafkind.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-syntax</span> <span style="color: #660000;">&#40;</span>mkfoo stx<span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">syntax-case</span> stx <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#41;</span>
    <span style="color: #660000;">&#91;</span><span style="color: #660000;">&#40;</span>_ setup<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>syntax<span style="color: #000000;">-</span>local<span style="color: #000000;">-</span>introduce
                #'<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-values/invoke-unit</span> 
                    <span style="color: #660000;">&#40;</span>init<span style="color: #000000;">-</span>setup setup<span style="color: #660000;">&#41;</span>
                    <span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
                    <span style="color: #660000;">&#40;</span>export foo<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#93;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Option 4</span>
<span style="color: #666600; font-style: italic;">;; Use a macro to completely hide the requirements of using the compound </span>
<span style="color: #666600; font-style: italic;">;; unit.</span>
<span style="color: #666600; font-style: italic;">;(mkfoo (unit (import) (export setup^) (define (baz x) (+ x 40))))</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Get the caller's lexical context for the generated bindings by using </span>
<span style="color: #666600; font-style: italic;">;; a provided export signature. Thanks to Matthew Flatt.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-syntax</span> mkfoo2
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">syntax-rules</span> <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#41;</span>
    <span style="color: #660000;">&#91;</span><span style="color: #660000;">&#40;</span>_ setup sig<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-values/invoke-unit</span>
                     <span style="color: #660000;">&#40;</span>init<span style="color: #000000;">-</span>setup setup<span style="color: #660000;">&#41;</span>
                     <span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
                     <span style="color: #660000;">&#40;</span>export sig<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#93;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Option 5</span>
<span style="color: #666600; font-style: italic;">;; Provide the export signature from the caller's context so that bindings </span>
<span style="color: #666600; font-style: italic;">;; are made in the right context.</span>
<span style="color: #666600; font-style: italic;">;(mkfoo2 (unit (import) (export setup^) (define (baz x) (+ x 40))) foo^)</span>
&nbsp;
<span style="color: #666600; font-style: italic;">; Make sure we have a binding for bar!</span>
bar</pre></div></div>

<img src="http://feeds.feedburner.com/~r/ArcadianVisions/~4/mMEGqQhFmxs" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.arcadianvisions.com/blog/?feed=rss2&amp;p=135</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A LaTeX wrapper for homework</title>
		<link>http://www.arcadianvisions.com/blog/?p=115</link>
		<comments>http://www.arcadianvisions.com/blog/?p=115#comments</comments>
		<pubDate>Wed, 01 Apr 2009 21:25:10 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Software]]></category>
		<category><![CDATA[LaTeX]]></category>

		<guid isPermaLink="false">http://www.arcadianvisions.com/blog/?p=115</guid>
		<description><![CDATA[Apropos of nothing&#8230; here are two examples of empty LaTeX files suitable for everyday use (e.g. homework). These templates don&#8217;t necessarily conform to any particular standard, but I think they are reasonable.

\documentclass{article}
\usepackage{amsmath, amsthm, amssymb}
\usepackage{listings}
\usepackage{graphicx}
\usepackage{float}
\usepackage{enumerate}
\usepackage{fancyhdr}
\usepackage[left=0.75in, top=1in, right=0.75in, bottom=1in]{geometry}
\pagestyle{plain}
\begin{document}
    \rhead{Student Name\\School 101: Homework 1}
    \thispagestyle{fancy}
&#160;
    % The list [...]]]></description>
			<content:encoded><![CDATA[<p>Apropos of nothing&#8230; here are two examples of empty LaTeX files suitable for everyday use (<em>e.g.</em> homework). These templates don&#8217;t necessarily conform to any particular standard, but I think they are reasonable.</p>

<div class="wp_syntax"><div class="code"><pre class="latex" style="font-family:monospace;"><span style="color: #800000; font-weight: normal;">\<span style="color: #800000; font-weight: bold;">documentclass</span></span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">article</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">amsmath, amsthm, amssymb</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">listings</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">graphicx</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">float</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">enumerate</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">fancyhdr</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">left=0.75in, top=1in, right=0.75in, bottom=1in</span><span style="color: #E02020; ">]{</span><span style="color: #2020C0; font-weight: normal;">geometry</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\pagestyle</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">plain</span><span style="color: #E02020; ">}</span>
<span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">document</span></span><span style="color: #E02020; ">}</span>
    <span style="color: #800000; font-weight: normal;">\rhead</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">Student Name\<span style="color: #800000; font-weight: normal;">\School</span> 101: Homework 1</span><span style="color: #E02020; ">}</span>
    <span style="color: #800000; font-weight: normal;">\thispagestyle</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">fancy</span><span style="color: #E02020; ">}</span>
&nbsp;
    <span style="color: #2C922C; font-style: italic;">% The list environment is just to get some vertical spacing</span>
    <span style="color: #800000; font-weight: normal;">\list</span><span style="color: #E02020; ">{</span><span style="color: #E02020; ">}</span> <span style="color: #800000; font-weight: normal;">\<span style="color: #800000; font-weight: bold;">item</span></span> <span style="color: #800000; font-weight: normal;">\endlist</span>
&nbsp;
    <span style="color: #2C922C; font-style: italic;">% Let the homework begin!</span>
    <span style="color: #800000; font-weight: normal;">\section*</span><span style="color: #E02020; ">{</span><span style="color: #00008B; font-weight: bold;">Question 1</span><span style="color: #E02020; ">}</span>
    <span style="color: #800000; font-weight: normal;">\subsection*</span><span style="color: #E02020; ">{</span><span style="color: #00008B; font-weight: bold;">Part (a)</span><span style="color: #E02020; ">}</span>
    Yes
    <span style="color: #800000; font-weight: normal;">\subsection*</span><span style="color: #E02020; ">{</span><span style="color: #00008B; font-weight: bold;">Part (b)</span><span style="color: #E02020; ">}</span>
    No
&nbsp;
    <span style="color: #800000; font-weight: normal;">\section*</span><span style="color: #E02020; ">{</span><span style="color: #00008B; font-weight: bold;">Question 2</span><span style="color: #E02020; ">}</span>
    Maybe
<span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">document</span></span><span style="color: #E02020; ">}</span></pre></div></div>

<p>Looks like this,<br />
<a href="http://www.arcadianvisions.com/images/LaTeX/latex-template1.png"><img style="border:1px solid black; display:block; margin-left:auto; margin-right:auto;" src="http://www.arcadianvisions.com/images/LaTeX/latex-template1.png" width="50%" /></a></p>
<p>A variation with a simpler header,</p>

<div class="wp_syntax"><div class="code"><pre class="latex" style="font-family:monospace;"><span style="color: #800000; font-weight: normal;">\<span style="color: #800000; font-weight: bold;">documentclass</span></span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">article</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">amsmath, amsthm, amssymb</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">listings</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">graphicx</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">float</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\usepackage</span><span style="color: #E02020; ">[</span><span style="color: #C08020; font-weight: normal;">left=1in, top=1in, right=1in, bottom=1in</span><span style="color: #E02020; ">]{</span><span style="color: #2020C0; font-weight: normal;">geometry</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\pagestyle</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">plain</span><span style="color: #E02020; ">}</span>
<span style="color: #800000; font-weight: normal;">\newtheorem</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">theorem</span><span style="color: #E02020; ">}{</span><span style="color: #2020C0; font-weight: normal;">Theorem</span><span style="color: #E02020; ">}</span><span style="color: #2C922C; font-style: italic;">%[section]</span>
&nbsp;
<span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">document</span></span><span style="color: #E02020; ">}</span>
    <span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">flushright</span></span><span style="color: #E02020; ">}</span>
    Student Name<span style="color: #E02020; ">\</span><span style="color: #800000; font-weight: normal;">\School</span> 101
    <span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">flushright</span></span><span style="color: #E02020; ">}</span>
&nbsp;
    <span style="color: #800000; font-weight: normal;">\section*</span><span style="color: #E02020; ">{</span><span style="color: #00008B; font-weight: bold;">Exercise 42</span><span style="color: #E02020; ">}</span>
    Eureka!
&nbsp;
    <span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">theorem</span></span><span style="color: #E02020; ">}</span>
        <span style="color: #800000; font-weight: normal;">\<span style="color: #800000; font-weight: bold;">textit</span></span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;">(A useful equivalence)</span><span style="color: #E02020; ">}\\</span>
        <span style="color: #8020E0; font-weight: normal;">$P = NP$</span>
    <span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">theorem</span></span><span style="color: #E02020; ">}</span>
    <span style="color: #C00000; font-weight: normal;">\begin</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">proof</span></span><span style="color: #E02020; ">}</span>
        By inspection.
    <span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">proof</span></span><span style="color: #E02020; ">}</span>
<span style="color: #C00000; font-weight: normal;">\end</span><span style="color: #E02020; ">{</span><span style="color: #2020C0; font-weight: normal;"><span style="color: #0000D0; font-weight: normal;">document</span></span><span style="color: #E02020; ">}</span></pre></div></div>

<p>looks like this,<br />
<a href="http://www.arcadianvisions.com/images/LaTeX/latex-template2.png"><img style="border:1px solid black; display:block; margin-left:auto; margin-right:auto;" src="http://www.arcadianvisions.com/images/LaTeX/latex-template2.png" width="50%" /></a></p>
<p>Enjoy!</p>
<img src="http://feeds.feedburner.com/~r/ArcadianVisions/~4/fPTAj5gUK-Y" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.arcadianvisions.com/blog/?feed=rss2&amp;p=115</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scheming in Units</title>
		<link>http://www.arcadianvisions.com/blog/?p=71</link>
		<comments>http://www.arcadianvisions.com/blog/?p=71#comments</comments>
		<pubDate>Tue, 31 Mar 2009 22:11:01 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scheme]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.arcadianvisions.com/blog/?p=71</guid>
		<description><![CDATA[After working with modules in a language like Standard ML, modular abstraction in other programming languages can feel somewhat primitive. One sometimes wishes to abstract an inter-module dependency, or, put another way, parameterize a module by the implementation of an interface. In object-oriented terms, this is a functional analogy of programming to an interface. To [...]]]></description>
			<content:encoded><![CDATA[<p>After working with <a href="http://www.dcs.ed.ac.uk/home/stg/NOTES/node95.html">modules</a> in a language like <a href="http://www.smlnj.org/">Standard ML</a>, modular abstraction in other programming languages can feel somewhat primitive. One sometimes wishes to abstract an inter-module dependency, or, put another way, parameterize a module by the implementation of an interface. In object-oriented terms, this is a functional analogy of programming to an interface. To this end, <a href="http://www.pltscheme.org">PLT Scheme</a> provides a mechanism called <a href="http://docs.plt-scheme.org/guide/units.html">Units</a>. Unfortunately, I found working through the Guide and Reference (standard documentation) rather hard going as usage is somewhat complicated by the availability of syntax-directed static analysis in some situations but not others. What I really missed in the documentation was a simple, generic example of working with different implementations of one signature. What follows is an example program that demonstrates several variations of just that technique with PLT Scheme 4.1.5. This is by no means a replacement for the Guide or Reference, but instead a supplementary example. One additional note: I am deliberately using several variations of the syntax for working with Units in order to provide examples of a few different styles.</p>
<p>The program we are writing is one that asks the user for his or her name, and then displays a greeting. However, the interface logic doesn&#8217;t specify the implementation for either of those actions. In this toy example, the idea is to provide implementations in different human languages. Note that the entire implementation is abstract, not just the string data. So, for example, the action of asking for a name could involve very different dialogues in different implementations (<em>e.g.</em> one question, or a series of questions warming up to asking for a name), or even a network transport layer for interacting with a remote user. The key feature is that the interface logic is entirely parameterized by the two actions I am calling <code>ask-for-name</code> and <code>greet</code>.</p>
<p>Let&#8217;s make that parameterization concrete with the file <code>service-sig.ss</code></p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #666600; font-style: italic;">;;; The service we are abstracting over is one that provides functions for </span>
<span style="color: #666600; font-style: italic;">;;; basic user interaction.</span>
#lang scheme
&nbsp;
<span style="color: #660000;">&#40;</span>provide service<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> service<span style="color: #000000;">^</span>
  <span style="color: #660000;">&#40;</span>ask<span style="color: #000000;">-</span>for<span style="color: #000000;">-</span>name
   greet<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>With that signature defined, we can define our user interaction logic. Here is the file <code>interaction.ss</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #666600; font-style: italic;">;;; The interaction logic is dependent on a service for directly interfacing</span>
<span style="color: #666600; font-style: italic;">;;; with the user. In this example, the functions ask-for-name and greet will</span>
<span style="color: #666600; font-style: italic;">;;; be supplied by some unit implementing the service^ signature.</span>
#lang scheme
&nbsp;
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-sig.ss&quot;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span>provide user<span style="color: #000000;">-</span>interface<span style="color: #000000;">^</span> interaction@<span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-signature</span> user<span style="color: #000000;">-</span>interface<span style="color: #000000;">^</span>
  <span style="color: #660000;">&#40;</span>prompt<span style="color: #000000;">-</span>user<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-unit</span> interaction@
  <span style="color: #660000;">&#40;</span>import service<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span>export user<span style="color: #000000;">-</span>interface<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>prompt<span style="color: #000000;">-</span>user<span style="color: #660000;">&#41;</span>
    <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">let</span> <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>name <span style="color: #660000;">&#40;</span>ask<span style="color: #000000;">-</span>for<span style="color: #000000;">-</span>name<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
      <span style="color: #660000;">&#40;</span>greet name<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>We&#8217;re going to need an implementation of our &#8220;service,&#8221; so let&#8217;s start with an English language version, <code>service-impl1.ss</code></p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #666600; font-style: italic;">;;; An English language implementation of the service^ signature.</span>
#lang scheme<span style="color: #000000;">/</span>unit
&nbsp;
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-sig.ss&quot;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>export service<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>ask<span style="color: #000000;">-</span>for<span style="color: #000000;">-</span>name<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>printf <span style="color: #008800;">&quot;What is your name? &quot;</span><span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>read<span style="color: #000000;">-</span>line<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>greet name<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>printf <span style="color: #008800;">&quot;Well, hello there, ~a!~n&quot;</span> name<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>That&#8217;s a lot of foundational work; let&#8217;s see how it comes together before going any further. The first version of our program, <code>service-program1.ss</code></p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #666600; font-style: italic;">;;; Basic usage of the interaction unit. We supply interaction@ with an </span>
<span style="color: #666600; font-style: italic;">;;; implemenation of the service^ signature so that we can use it.</span>
#lang scheme
&nbsp;
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-impl1.ss&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-impl2.ss&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;interaction.ss&quot;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; If we don't want programatic linking, then we can really lean on inference.</span>
<span style="color: #666600; font-style: italic;">;; Change service-impl1@ to service-impl2@ for a different language.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-compound-unit/infer</span> my<span style="color: #000000;">-</span>program
  <span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span>export user<span style="color: #000000;">-</span>interface<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span>link service<span style="color: #000000;">-</span>impl1@ interaction@<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span> 
&nbsp;
<span style="color: #666600; font-style: italic;">;; Brings an implementation of the user-interface^ signature into scope.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-values/invoke-unit/infer</span> my<span style="color: #000000;">-</span>program<span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Kick off the interactive program</span>
<span style="color: #660000;">&#40;</span>prompt<span style="color: #000000;">-</span>user<span style="color: #660000;">&#41;</span></pre></div></div>

<p>I&#8217;ve snuck in another implementation of our <code>service^</code> signature. Here is the Spanish language version, <code>service-impl2.ss</code>,</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #666600; font-style: italic;">;;; An Spanish language implementation of the service^ signature.</span>
#lang scheme<span style="color: #000000;">/</span>unit
&nbsp;
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-sig.ss&quot;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>export service<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>ask<span style="color: #000000;">-</span>for<span style="color: #000000;">-</span>name<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>printf <span style="color: #008800;">&quot;Â¿CÃ³mo te llamas? &quot;</span><span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>read<span style="color: #000000;">-</span>line<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> <span style="color: #660000;">&#40;</span>greet name<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>printf <span style="color: #008800;">&quot;Â¡Bueno! Â¿CÃ³mo estÃ¡s, ~a?~n&quot;</span> name<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span></pre></div></div>

<p>One can play around with <code>service-program1.ss</code> as indicated in the comments. We have now achieved our goal of writing our program in such a way that we do not need to touch the interaction logic in order to supply different implementations of our <code>service^</code> signature.</p>
<p>But Units can be taken further! Units are first class in PLT Scheme, so we can work with them at runtime. The requirement that we modify the source of <code>service-program1.ss</code> to change language front-ends feels a bit clumsy, so let&#8217;s write a program that will select which implementation to use for itself, <code>service-program2.ss</code></p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #666600; font-style: italic;">;;; Units are first class in PLT Scheme, so we can programatically control </span>
<span style="color: #666600; font-style: italic;">;;; how they are linked together. In this variation, we bind an identifier </span>
<span style="color: #666600; font-style: italic;">;;; to a particular Unit depending on the user's choice of language. We </span>
<span style="color: #666600; font-style: italic;">;;; then link the interaction Unit with that new binding in order to </span>
<span style="color: #666600; font-style: italic;">;;; execute our program.</span>
#lang scheme
&nbsp;
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-impl1.ss&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-impl2.ss&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;interaction.ss&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-sig.ss&quot;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span>printf <span style="color: #008800;">&quot;Choose your language: (1) English or (2) for Spanish&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> language <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #660000;">&#40;</span>regexp<span style="color: #000000;">-</span>match #px<span style="color: #008800;">&quot;2&quot;</span> <span style="color: #660000;">&#40;</span>read<span style="color: #000000;">-</span>line<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
                     'spanish
                     'english<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; If we are willing to bind a temporary at the top-level, then we can </span>
<span style="color: #666600; font-style: italic;">;; use a define-unit form which will let us take advantage of /infer </span>
<span style="color: #666600; font-style: italic;">;; forms when using the unit. </span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-unit-binding</span> impl@ 
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">cond</span> 
    <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>eq? language 'english<span style="color: #660000;">&#41;</span> service<span style="color: #000000;">-</span>impl1@<span style="color: #660000;">&#41;</span>
    <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>eq? language 'spanish<span style="color: #660000;">&#41;</span> service<span style="color: #000000;">-</span>impl2@<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span>export service<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-compound-unit/infer</span> my<span style="color: #000000;">-</span>program
  <span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span>export user<span style="color: #000000;">-</span>interface<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span>
  <span style="color: #660000;">&#40;</span>link impl@ interaction@<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Brings an implementation of the user-interface^ signature into scope.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-values/invoke-unit/infer</span> my<span style="color: #000000;">-</span>program<span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Kick off the interactive program</span>
<span style="color: #660000;">&#40;</span>prompt<span style="color: #000000;">-</span>user<span style="color: #660000;">&#41;</span></pre></div></div>

<p>For one final variation, the top-level binding of <code>impl@</code> bothers me a bit since it is purely a detail of <code>my-program</code>&#8217;s implementation. In <code>service-program3.ss</code>, we move <code>impl@</code> into <code>my-program</code>&#8217;s definition.</p>

<div class="wp_syntax"><div class="code"><pre class="pltscheme" style="font-family:monospace;"><span style="color: #666600; font-style: italic;">;;; We can achieve programatic linking without binding a new identifier at the </span>
<span style="color: #666600; font-style: italic;">;;; top-level, but this strategy reduces the degree to which the Unit </span>
<span style="color: #666600; font-style: italic;">;;; system is able to infer imports and exports.</span>
#lang scheme
&nbsp;
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-impl1.ss&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-impl2.ss&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;interaction.ss&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span>require <span style="color: #008800;">&quot;service-sig.ss&quot;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #660000;">&#40;</span>printf <span style="color: #008800;">&quot;Choose your language: (1) English or (2) for Spanish&quot;</span><span style="color: #660000;">&#41;</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> language <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #660000;">&#40;</span>regexp<span style="color: #000000;">-</span>match #px<span style="color: #008800;">&quot;2&quot;</span> <span style="color: #660000;">&#40;</span>read<span style="color: #000000;">-</span>line<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
                     'spanish
                     'english<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; An example of using first class units. We have to make imports</span>
<span style="color: #666600; font-style: italic;">;; and exports much more explicit since we are not using a top-level </span>
<span style="color: #666600; font-style: italic;">;; form for binding impl@, our temporary unit.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define</span> my<span style="color: #000000;">-</span>program
  <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">let</span> <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>impl@ <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">cond</span>
                 <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>eq? language 'english<span style="color: #660000;">&#41;</span> service<span style="color: #000000;">-</span>impl1@<span style="color: #660000;">&#41;</span>
                 <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>eq? language 'spanish<span style="color: #660000;">&#41;</span> service<span style="color: #000000;">-</span>impl2@<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
    <span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">compound-unit</span>
     <span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span>
     <span style="color: #660000;">&#40;</span>export UI<span style="color: #660000;">&#41;</span>
     <span style="color: #660000;">&#40;</span>link <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>UI : user<span style="color: #000000;">-</span>interface<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span> interaction@ Lang<span style="color: #660000;">&#41;</span>
           <span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span><span style="color: #660000;">&#40;</span>Lang : service<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span> impl@<span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Brings an implementation of the user-interface^ signature into scope.</span>
<span style="color: #660000;">&#40;</span><span style="color: #0000ff; font-weight: bold;">define-values/invoke-unit</span> my<span style="color: #000000;">-</span>program <span style="color: #660000;">&#40;</span>import<span style="color: #660000;">&#41;</span> <span style="color: #660000;">&#40;</span>export user<span style="color: #000000;">-</span>interface<span style="color: #000000;">^</span><span style="color: #660000;">&#41;</span><span style="color: #660000;">&#41;</span>
&nbsp;
<span style="color: #666600; font-style: italic;">;; Kick off the interactive program</span>
<span style="color: #660000;">&#40;</span>prompt<span style="color: #000000;">-</span>user<span style="color: #660000;">&#41;</span></pre></div></div>

<p>All the source files may be found in this <a href="http://www.arcadianvisions.com/downloads/Scheme/unit-service.zip">archive</a>.</p>
<img src="http://feeds.feedburner.com/~r/ArcadianVisions/~4/z5doqucYjlQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.arcadianvisions.com/blog/?feed=rss2&amp;p=71</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Octave + Image package + Mac OS X</title>
		<link>http://www.arcadianvisions.com/blog/?p=65</link>
		<comments>http://www.arcadianvisions.com/blog/?p=65#comments</comments>
		<pubDate>Fri, 14 Nov 2008 15:21:43 +0000</pubDate>
		<dc:creator>Anthony</dc:creator>
				<category><![CDATA[Octave]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Software]]></category>

		<guid isPermaLink="false">http://www.arcadianvisions.com/blog/?p=65</guid>
		<description><![CDATA[A quick public service note for anyone else trying to install the Image package (1.0.8 was the version I downloaded) with Octave (3.0.3 was the version I had) installed as Octave.app from the Octave Forge page. 
I had ImageMagick 6.4.1 installed via fink on OS 10.4.11, but installing the Octave Image package was failing on
mkoctfile [...]]]></description>
			<content:encoded><![CDATA[<p>A quick public service note for anyone else trying to install the <a href="http://octave.sourceforge.net/image/index.html">Image</a> package (1.0.8 was the version I downloaded) with Octave (3.0.3 was the version I had) installed as Octave.app from the <a href="http://octave.sourceforge.net/">Octave Forge page</a>. </p>
<p>I had ImageMagick 6.4.1 installed via fink on OS 10.4.11, but installing the Octave Image package was failing on<br />
<blockquote><code>mkoctfile __magick_read__.cc</code></p></blockquote>
<p> Looking in image-1.0.8/src/Makefile, there is the line </p>
<blockquote><p><code>$(MKOCTFILE) $< `Magick++-config --cppflags` `Magick++-config --ldflags`</code></code></p></blockquote>
<p> which, it turns out, needed to be<br />
<blockquote><code>$(MKOCTFILE) $< `Magick++-config --cppflags` `Magick++-config --ldflags` `Magick++-config --libs`</code></code></p></blockquote>
<p> for me. </p>
<p>Since it took me several hours to sort this all out, I&#8217;m putting it here for anyone else having trouble getting the Image package installation to link with ImageMagick.</p>
<img src="http://feeds.feedburner.com/~r/ArcadianVisions/~4/VSljyNXJPA0" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://www.arcadianvisions.com/blog/?feed=rss2&amp;p=65</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss><!-- Dynamic Page Served (once) in 5.694 seconds --><!-- Cached page served by WP-Cache -->
