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<http://www.mozart-oz.org/documentation/apptut/node4.html#chapter.modman>
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