List Info

Thread: "ocaml_beginners"::[] Re: problem with recursive modules?




"ocaml_beginners"::[] Re: problem with recursive modules?
user name
2006-12-08 16:34:11

Le ven 08 déc 2006 15:52:50 CET,
&quot;drehman27" < drehman27%40yahoo.com">drehman27yahoo.com> a écrit :

> Ok. This is the program I wrote. But I am still having errors:
&gt; File "test.ml", line 21, characters 19-55:
&gt; Unbound value BenchmarkFoo.print_benchmark_results
>;
> I am trying to build a small framework for testing different machine
&gt; learning algorithms, and I thought that using functors would be a nice
> way to reuse code from the framework in each algorithm that I
> implement. I didn't think it was that hard to use functors in OCaml.

It is not that hard (well, until you begin to play with abstraction and
type constraints). Basically, a functor is a function at the module
level. You can also define functor types, as you've done below, but
then you have to apply them to a module to obtain a module type.

&gt; module type FrameworkType =
> functor (Alg : Algorithm) ->
>; sig
> val print_benchmark_results : unit -> unit
> end

>; module rec FooAlgorithm : Algorithm =
> struct
&gt; let run_algorithm () = ()
> let results () = BenchmarkFoo.print_benchmark_results ()
> end
> and BenchmarkFoo : FrameworkType = Framework(FooAlgorithm)

Here, FrameworkType is a functor type, hence it has no component at all.
Besides, this is not the type of Framework(FooAlgorithm), which would
rather be FrameworkType(FooAlgorithm). It seems however that you can't
use that as the type of a recursive module. Thus
it seems that you should write something like

module type FrameworkType =
sig (* here we define a plain module type, not a functor type*)
val print_benchmark_results : unit -> unit
end
...
module rec ...
and BenchmarkFoo : FrameworkType = Framework(FooAlgorithm)

In general, I'd say that it is sufficient to write the signature
(module type) corresponding to the result of the functor application,
rather than a whole functor type. Those are needed only if you want to
define an higher-order functor.

--
E tutto per oggi, a la prossima volta.
Virgile

__._,_.___
.

__,_._,___
"ocaml_beginners"::[] Re: problem with recursive modules?
user name
2006-12-08 17:03:14

It still doesn't work. Could you please fix my example so that it
compiles? Thanks

--- In ocaml_beginners%40yahoogroups.com">ocaml_beginnersyahoogroups.com, Virgile Prevosto
<virgile.prevosto...> wrote:
&gt;
> Le ven 08 déc 2006 15:52:50 CET,
> "drehman27" <drehman27...> a écrit :
>
> > Ok. This is the program I wrote. But I am still having errors:
&gt; > File "test.ml", line 21, characters 19-55:
&gt; > Unbound value BenchmarkFoo.print_benchmark_results
>; >
> > I am trying to build a small framework for testing different machine
&gt; > learning algorithms, and I thought that using functors would be a nice
> > way to reuse code from the framework in each algorithm that I
> > implement. I didn't think it was that hard to use functors in OCaml.
&gt;
> It is not that hard (well, until you begin to play with abstraction and
> type constraints). Basically, a functor is a function at the module
&gt; level. You can also define functor types, as you've done below, but
> then you have to apply them to a module to obtain a module type.
>;
> > module type FrameworkType =
> > functor (Alg : Algorithm) ->
>; > sig
> > val print_benchmark_results : unit -> unit
> > end
>
> > module rec FooAlgorithm : Algorithm =
> > struct
&gt; > let run_algorithm () = ()
> > let results () = BenchmarkFoo.print_benchmark_results ()
> > end
> > and BenchmarkFoo : FrameworkType = Framework(FooAlgorithm)
>
> Here, FrameworkType is a functor type, hence it has no component at all.
> Besides, this is not the type of Framework(FooAlgorithm), which would
>; rather be FrameworkType(FooAlgorithm). It seems however that you can't
>; use that as the type of a recursive module. Thus
> it seems that you should write something like
>
> module type FrameworkType =
> sig (* here we define a plain module type, not a functor type*)
> val print_benchmark_results : unit -> unit
> end
> ...
> module rec ...
> and BenchmarkFoo : FrameworkType = Framework(FooAlgorithm)
>
> In general, I'd say that it is sufficient to write the signature
> (module type) corresponding to the result of the functor application,
> rather than a whole functor type. Those are needed only if you want to
> define an higher-order functor.
>
> --
> E tutto per oggi, a la prossima volta.
&gt; Virgile
&gt;

__._,_.___
.

__,_._,___
[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )