* [Caml-list] Even at compile time 2*2=4! @ 2002-11-19 3:18 Jakob Lichtenberg 2002-11-19 11:11 ` Alessandro Baretta 2002-11-19 11:12 ` Daniel de Rauglaudre 0 siblings, 2 replies; 6+ messages in thread From: Jakob Lichtenberg @ 2002-11-19 3:18 UTC (permalink / raw) To: caml-list Fellow O'Caml hackers, I have this complicated module 'fourLib.ml' that binds the value 'four' to the *computed* value of four: --- let four = (Printf.printf "Now we do the complicated calculation of 2*2...\n"; let res = 2*2 in res); --- A sample application 'sixteen.ml': --- let _ = Printf.printf "4 * 4 is %d" (FourLib.four * FourLib.four); --- I wish to package/compile my 'fourLib' so that I can avoid doing the complicated calculation '2*2' each and every time I load 'fourLib'. (I am sure that you can imagine an even more requiring computational task than evaluating '2*2'.) Currently I build as follows: > ocamlc -c fourLib.ml > ocamlc -c sixteen.ml > ocamlc -o sixteen fourLib.cmo sixteen.cmo Unfortunately 2*2 is first calculated when I run the application: > ./sixteen Now we do the complicated calculation of 2*2... 4 * 4 is 16 --- 1. I am willing to rewrite the library, the way stuff is build, etc.... I basically just want ocaml to evaluate 2*2 before the final linking. Is my only hope to serialize Four.four and then load it? 2. Can I build a new top-level interactive environment 'ocaml_with_fourLib' where this module is 'pre-loaded'. Again, the actual calculation of 2*2 should be done when I build ocaml_with_fourLib - not when I start it. Thanks, - Jakob Lichtenberg -- Jakob Lichtenberg - jl@itu.dk - http://www.itu.dk/people/jl/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Even at compile time 2*2=4! 2002-11-19 3:18 [Caml-list] Even at compile time 2*2=4! Jakob Lichtenberg @ 2002-11-19 11:11 ` Alessandro Baretta 2002-11-20 0:31 ` Pierre Weis 2002-11-19 11:12 ` Daniel de Rauglaudre 1 sibling, 1 reply; 6+ messages in thread From: Alessandro Baretta @ 2002-11-19 11:11 UTC (permalink / raw) To: Jakob Lichtenberg, ocaml Jakob Lichtenberg wrote: > > I wish to package/compile my 'fourLib' so that I can avoid doing the > complicated calculation '2*2' each and every time I load 'fourLib'. (I am > sure that you can imagine an even more requiring computational task than > evaluating '2*2'.) I wish to suggest one possibility. 1) You write a module defining the type of the datastructure you wish to precompute and load 2) You write a program importing the above module, which computes the datastructure and marshals it to a file. You run this program. 3) You write a library which, upon being loaded, searches the filesystem for the appropriate file, unmarshals it and binds the precomputed data to an identifier. 4) You write your programs referring to the the identifier exported by #3. eg. module: type_of_four.ml ********************* type four = int module: compute_four.ml ********************** open Type_of_four let four:four = 2 * 2 let _ = Marshal.to_channel (open_out "foo") four [] module: four.ml *************** open Type_of_four let (four:four) = Marshal.from_channel (open_in "foo") module: sixteen.ml ****************** open Four let sixteen = four * four Cheers, Alex ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Even at compile time 2*2=4! 2002-11-19 11:11 ` Alessandro Baretta @ 2002-11-20 0:31 ` Pierre Weis 2002-11-20 8:42 ` Alessandro Baretta 0 siblings, 1 reply; 6+ messages in thread From: Pierre Weis @ 2002-11-20 0:31 UTC (permalink / raw) To: Alessandro Baretta; +Cc: jl, caml-list > Jakob Lichtenberg wrote: > > > > > I wish to package/compile my 'fourLib' so that I can avoid doing the > > complicated calculation '2*2' each and every time I load 'fourLib'. (I am > > sure that you can imagine an even more requiring computational task than > > evaluating '2*2'.) > > I wish to suggest one possibility. > > 1) You write a module defining the type of the datastructure > you wish to precompute and load > 2) You write a program importing the above module, which > computes the datastructure and marshals it to a file. You > run this program. > 3) You write a library which, upon being loaded, searches > the filesystem for the appropriate file, unmarshals it and > binds the precomputed data to an identifier. > 4) You write your programs referring to the the identifier > exported by #3. [...] > Cheers, > Alex I clearly see what you want to do, this is as old as functional programming (remember the old LISP eval-when-compile form). As far as I know people want that feature because they think (well in fact, they say they KNOW for sure) that this will speed up their programs. This is unclear to me. I remember to have read the source code of a CAS, entirely written in Lisp, that started by computing a lot of constants with a huge number of decimals (including $\pi$ and $e$). So, I said "he guys, you could easily speed up your stuff by just defining the pre-calculated constants instead of computing them from scratch again and again at each start of your program!" They politely thanked me for the suggestion, but they had to let me know that they knew from time measurements that this ``presumably enhanced'' method was much slower than re-computation: building the constants directly in memory via computation was much faster than any way they had found to get them stored into the memory at the beginning of the program. Effectively, at launch time the constants have to be read from files and this is really slow ! (The constants written at compile time in the executable are stored to disk anyway and have to be read back when launching the program: in the case of the CAS program, loading those huge constants with the executable was slower than loading a small executable that re-computed them from scratch!) Life is not that simple... Pierre Weis INRIA, Projet Cristal, Pierre.Weis@inria.fr, http://pauillac.inria.fr/~weis/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Even at compile time 2*2=4! 2002-11-20 0:31 ` Pierre Weis @ 2002-11-20 8:42 ` Alessandro Baretta 0 siblings, 0 replies; 6+ messages in thread From: Alessandro Baretta @ 2002-11-20 8:42 UTC (permalink / raw) To: Ocaml Pierre Weis wrote: > (The constants written at compile time in the executable are stored to > disk anyway and have to be read back when launching the program: in > the case of the CAS program, loading those huge constants with the > executable was slower than loading a small executable that re-computed > them from scratch!) > > Life is not that simple... > > Pierre Weis I agree. This method is only useful if computational complexity is much greater than space complexity. Besides, even complexity is actually a fairly volatile concept in this case, because we are talking about computing a constant function (no input). To be honest, I can't think of a situtation where I'd use my own hack. I wonder what was Jakob's reason for posting originally. Alex ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Even at compile time 2*2=4! 2002-11-19 3:18 [Caml-list] Even at compile time 2*2=4! Jakob Lichtenberg 2002-11-19 11:11 ` Alessandro Baretta @ 2002-11-19 11:12 ` Daniel de Rauglaudre 2002-11-19 18:09 ` David Brown 1 sibling, 1 reply; 6+ messages in thread From: Daniel de Rauglaudre @ 2002-11-19 11:12 UTC (permalink / raw) To: caml-list Hi, On Mon, Nov 18, 2002 at 07:18:34PM -0800, Jakob Lichtenberg wrote: > 2. Can I build a new top-level interactive environment 'ocaml_with_fourLib' > where this module is 'pre-loaded'. Again, the actual calculation of 2*2 > should be done when I build ocaml_with_fourLib - not when I start it. There is no difference between "load" a module and "start" it: the fact of loading it is starting it. In some cases, you could resolve your problem with Camlp4, if your computation can be done at parse time. We can imagine a syntax extension, e.g. "compute" followed by an expression, ask the preprocessor to evalutate it and build the syntax tree of the result. For example: compute (2*2) would generate at parse time: 4 But this could not work: let x = 2 in compute(x*x) because the preprocessor does not know what is "x". On the other hand, if "x" is defined as a directive to the preprocessor, for example with "DEFINE x = 2" (new syntax extension pa_macro.cmo), it could work. -- Daniel de RAUGLAUDRE http://cristal.inria.fr/~ddr/ ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [Caml-list] Even at compile time 2*2=4! 2002-11-19 11:12 ` Daniel de Rauglaudre @ 2002-11-19 18:09 ` David Brown 0 siblings, 0 replies; 6+ messages in thread From: David Brown @ 2002-11-19 18:09 UTC (permalink / raw) To: Daniel de Rauglaudre; +Cc: caml-list On Tue, Nov 19, 2002 at 12:12:01PM +0100, Daniel de Rauglaudre wrote: > In some cases, you could resolve your problem with Camlp4, if your > computation can be done at parse time. We can imagine a syntax > extension, e.g. "compute" followed by an expression, ask the > preprocessor to evalutate it and build the syntax tree of the > result. For example: > compute (2*2) > would generate at parse time: > 4 > > But this could not work: > let x = 2 in > compute(x*x) > because the preprocessor does not know what is "x". To be useful, you would probably also have to be able to define functions for the preprocessor that could be used to compute the value. This also makes the interesting possibility of compiles that don't terminate... compute (forever ()) Dave Brown ------------------- To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/ Beginner's list: http://groups.yahoo.com/group/ocaml_beginners ^ permalink raw reply [flat|nested] 6+ messages in thread
end of thread, other threads:[~2002-11-20 8:41 UTC | newest] Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2002-11-19 3:18 [Caml-list] Even at compile time 2*2=4! Jakob Lichtenberg 2002-11-19 11:11 ` Alessandro Baretta 2002-11-20 0:31 ` Pierre Weis 2002-11-20 8:42 ` Alessandro Baretta 2002-11-19 11:12 ` Daniel de Rauglaudre 2002-11-19 18:09 ` David Brown
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox