Hi,

I think that definition of "functor" on pp. 221 of CTM might be
intended as a general, language-independent definition.

In Oz, the only direct way (that I know of) to have a parametrized
functor is to use values from the lexical environment, as suggested by
Torsten.
This will not work with functors in a different compilation unit
(which are loaded from a .ozf file).

Another indirect possibility is to set a functor-level value AFTER the
functor has been loaded or applied, as in this example:
http://www.mail-archive.com/[email protected]/msg01784.html

A third possibility is to use a Module.manager instance directly and
register modules at URLs. For example:

declare

M = {New Module.manager init}

functor A
export
   TestA
define
   fun {TestA}
      "TestA"
   end
end

ModuleA = {M apply(A $)}
{M enter(url:'x-oz://test/a' ModuleA)}

functor B
import
   A at 'x-oz://test/a'
export
   TestB
define
   fun {TestB}
      "TestB"#{A.testA}
   end
end

ModuleB = {M apply(B $)}
in

{System.showInfo {ModuleB.testB}}


Hope this helps,
  Wolfgang


On Tue, Jul 27, 2010 at 9:12 AM, Lyle Kopnicky <[email protected]> wrote:
> Hi folks,
> I wonder if I'm missing something in the definition of functors. According
> to CTM (pp. 221), "A functor is a function whose arguments are the modules
> it needs and shose result is a new module." That sounds wonderful, as it
> allows me to compose modules at will. On pp. 223-4, the following function:
> fun {MyListFunctor}
>     proc {Append ... }
>     proc {MergeSort ...} ... end
>     proc {Sort ... } ... {MergeSort ... } ... end
>     proc {Member ...} ... end
> in
>     `export`(append: Append
>              sort: Sort
>              member: Member
>              ...)
> end
> can be written with the functor syntax as:
> functor
> export
>     append:Append
>     sort:Sort
>     member:Member
>     ...
> define
>     proc {Append ... } ... end
>     proc {MergeSort ... } ... end
>     proc {Sort ... } ... {MergeSort ...} ... end
>     proc {Member ...} ... end
> end
> The book does not say how to instantiate this module directly, but this
> documentation page implies it should look like this:
> declare [MyListModule] = {Module.apply [MyListFunctor]}
> if the functor were bound to the variable MyListFunctor.
> But I noticed in this example that no arguments were passed to the functor.
> Let's go back to the original function representation, and enhance it to
> take an argument:
> fun {MyListFunctor SortingModule}
>     proc {Append ... }
>     proc {MergeSort ...} ... end
>     proc {Sort ... } ... {SortingModule.sort ... } ... end
>     proc {Member ...} ... end
> in
>     `export`(append: Append
>              sort: Sort
>              member: Member
>              ...)
> end
> Now what I'd really like to pass here is just a function, but the definition
> of functors only promised that I could pass in modules. Now suppose I have
> such a sorting module in a variable:
> declare [MergeSortModule] = {Module.link ["MyMergeSort.ozf"]}
> Then I could call MyListFunctor with it:
> declare MyListModule = {MyListFunctor MergeSortModule}
> I ought to be able to do the same with the functor syntax, right? Except I
> don't know how to write it. The book says that the arguments are implemented
> through import statements. So...
> functor MyListFunctor
> import
>     SortingModule
> export
>     append:Append
>     sort:Sort
>     member:Member
>     ...
> define
>     proc {Append ... } ... end
>     proc {MergeSort ... } ... end
>     proc {Sort ... } ... {SortingModule ...} ... end
>     proc {Member ...} ... end
> end
> And then how would I pass the appropriate module in when I instantiate the
> functor? I guess I would like to write something like:
> declare MyListModule = {Module.apply [MyListFunctor MergeSortModule]}
> But this doesn't work. I could wrap the functor declaration in a function
> declaration that takes the module argument, but that defeats the purpose.
> Am I missing something? I'm not clear how to apply functors to arbitrary
> modules (or to any other sort of argument), rather than just importing from
> specific hard-wired places in the filesystem.
> Thanks,
> Lyle
> _________________________________________________________________________________
> mozart-users mailing list
> [email protected]
> http://www.mozart-oz.org/mailman/listinfo/mozart-users
>
_________________________________________________________________________________
mozart-users mailing list                               
[email protected]
http://www.mozart-oz.org/mailman/listinfo/mozart-users

Reply via email to