<div dir="ltr"><br><br><div class="gmail_quote">On Wed, Jul 30, 2008 at 6:57 PM, Eli Barzilay <span dir="ltr"><<a href="mailto:eli@barzilay.org">eli@barzilay.org</a>></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', which returns a<br>
value that you can't really use for anything (see the docs). But I<br>
think that to merge two namespaces you'll need to scan the bindings<br>
from each one and create the appropriate bindings in the result, and<br>
`identifier-binding' 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 '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'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;"> (parameterize ((current-namespace ns))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (identifier-binding (namespace-symbol->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;"> (parameterize ((current-namespace ns))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (let ((binding (get-module-binding sym ns)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (cond ((or (eq? binding #f) (equal? binding 'lexical))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> binding)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (else</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (resolved-module-path-name</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (module-path-index-resolve</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (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. </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;">(define (list->unique-list lst)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (define (helper rest acc)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (cond ((null? rest) (reverse acc))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (else</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (if (member (car rest) acc)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (helper (cdr rest) acc)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (helper (cdr rest) (cons (car rest) acc))))))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (helper lst '()))</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->module-paths (ns (current-namespace)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (list->unique-list</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (map (lambda (sym)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (get-module-path sym ns))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (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) </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;"> (let ((from-paths (namespace->module-paths from-ns))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (to-paths (namespace->module-paths to-ns)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (filter (lambda (p)</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (not (member p to-paths)))</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> 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;"> (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;"> (parameterize ((current-namespace to-ns))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> (for-each (lambda (path)</span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (when path</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> ;; doesn't work with #%network or top-level modules </span><br style="font-family: courier new,monospace;"><span style="font-family: courier new,monospace;"> (namespace-require path)))</span><br style="font-family: courier new,monospace;">
<span style="font-family: courier new,monospace;"> 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> </div></div></div>