<div dir="ltr"><br><br><div class="gmail_quote">On Wed, Jul 30, 2008 at 6:57 PM, Eli Barzilay <span dir="ltr">&lt;<a href="mailto:eli@barzilay.org">eli@barzilay.org</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><br></div>Seems like there is only `namespace-module-registry&#39;, which returns a<br>
value that you can&#39;t really use for anything (see the docs). &nbsp;But I<br>
think that to merge two namespaces you&#39;ll need to scan the bindings<br>
from each one and create the appropriate bindings in the result, and<br>
`identifier-binding&#39; is what is needed for doing this.<font color="#888888"><br></font></blockquote><div><br>Thanks Eli.<br><br>I got from identifier-binding to resolved-module-path, and was able to get quite a bit to work, except the following issues:<br>
<ol><li>namespace-require does not work with built-in modules such as %network</li><li>namespace-require also does not work with top-level modules such as &#39;cake <br></li><li>top-level bindings requires a different approach <br>
</li></ol>Lastly - it appears expensive to having to loop through all bindings... hopefully something like namespace-module-registry can offer more in the future? ;)<br><br>Below is what I&#39;ve got in case anyone is interested... <br>
<br><span style="font-family: courier new,monospace;">;; retrieve the module binding by sym and namespace </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(define (get-module-binding sym (ns (current-namespace)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp; (parameterize ((current-namespace ns))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; (identifier-binding (namespace-symbol-&gt;identifier sym))))</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; retrieve the module path (resolved) by sym and namespace </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(define (get-module-path sym (ns (current-namespace)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp; (parameterize ((current-namespace ns))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; (let ((binding (get-module-binding sym ns)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (cond ((or (eq? binding #f) (equal? binding &#39;lexical))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; binding)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (else</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (resolved-module-path-name</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (module-path-index-resolve</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (car binding))))))))</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; filters duplicates in a list.&nbsp; </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(define (list-&gt;unique-list lst)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp; (define (helper rest acc)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; (cond ((null? rest) (reverse acc))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (else</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (if (member (car rest) acc)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (helper (cdr rest) acc)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (helper (cdr rest) (cons (car rest) acc))))))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp; (helper lst &#39;()))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; turn the symbols into module path and filter dupes. </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(define (namespace-&gt;module-paths (ns (current-namespace)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; (list-&gt;unique-list</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp; (map (lambda (sym)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (get-module-path sym ns))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (namespace-mapped-symbols ns))))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; compare two module paths and return the differences (one way)&nbsp; </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(define (namespace-module-paths-diff from-ns to-ns)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp; (let ((from-paths (namespace-&gt;module-paths from-ns))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (to-paths (namespace-&gt;module-paths to-ns)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; (filter (lambda (p)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (not (member p to-paths)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; from-paths)))</span><br style="font-family: courier new,monospace;">
<br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; take the diffs in module paths and require them. </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(define (namespace-merge from-ns to-ns)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp; (let ((ns-paths (namespace-module-paths-diff from-ns to-ns)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp; (parameterize ((current-namespace to-ns))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (for-each (lambda (path)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (when path</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ;; doesn&#39;t work with #%network or top-level modules </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; (namespace-require path)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ns-paths))))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; examples...</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(define ns (make-base-namespace))</span><br style="font-family: courier new,monospace;"><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">;; current-namespace in top-level is scheme. will choke on #%network for now. </span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;">(namespace-merge (current-namespace) ns)</span><br style="font-family: courier new,monospace;"><br><br>&nbsp;</div></div></div>