[racket] Quick question on units, composition
You need to use define-unit to get this to work:
(define-signature t^ (g))
(define-unit t@
; unit
(import (prefix config: s^))
(export t^)
(define (g x)
(config:f x))
(displayln '(hello world)))
(define (f x) (* 2 x)) ;; define the needed dependence
(define-values/invoke-unit/infer (export t^) (link t@)) ;; infer f is what we import, run unit, export g
g ;; is available now
In general, units are values like lambdas that come, in principle, with two operations:
(1) linking
(2) invoking
The former satisfies (possibly mutual) dependencies of units without running any code inside of units. The latter runs the code, the last value is returned, like in a lambda.
Here is another way to run with a prefix:
(define-signature s^ (f))
(define-signature t^ (g))
(define t@
(unit
(import (prefix config: s^))
(export t^)
(define (g x)
(config:f x))
(displayln '(hello world))))
(define s@
(unit
(import)
(export s^)
(define (f y)
y)))
(define main-2@
(compound-unit
(import)
(export t)
(link
(((s : s^)) s@)
(((t : t^)) t@ s))))
(invoke-unit main-2@)
On Feb 20, 2014, at 9:33 PM, Paul Meier wrote:
> Hi friends,
>
> Just a quick question on using Units -- I have one unit that imports a signature and appends a prefix, a la
>
> (define-unit my-unit@
> (import (prefix config: config-signature^))
> (export my-unit^)
> ;; implements my-unit^...
> )
>
> But when I attempt to invoke this after invoking a unit satisfying the dependency signature (above, `config-signature`), I get errors that it can't locate identifiers from the imported signatures.
>
> I suspect this is to do with the fact that I'm invoking the dependency at the toplevel using define-values/invoke-unit/infer, per the Racket Guide, but a perusal of the Racket Reference suggests another method of invocation might be more suitable, since define-values is just exporting bindings to the global namespace, and here I'm referring to them by a prefixed form.
>
> That said, I'm not entirely clear on what what value the `invoke-units` syntax returns, or what side effects invocation of a unit even does from reading the docs (if it isn't defining bindings).
>
> Any help would be appreciated! Thanks ^_^
>
> -Paul
>
> ____________________
> Racket Users list:
> http://lists.racket-lang.org/users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20140221/84705f21/attachment.html>