1 / 13
Nov 2006

C++ and other programmers can take advantage of standard libraries such as the STL, eg

include

include

etc.

However, in caml, certain standard libraries have to be included at link time. In the toplevel these libraries can be used by using the #load command. However, in order to use these libraries in a compiled program, they have to be linked at compile time; the ocaml compilers give an error if they see #load. The addition of the following to the compiler arguments would suffice:

"str.cmxa nums.cmxa"

str.cmxa gives additional functions on strings, including regular expression support. nums.cmxa provides functions for operations on arbitrarily large integers and rational numbers.

for information about the num library: http://caml.inria.fr/pub/docs/manual-ocaml/manual036.html7

for information about the str library:
http://caml.inria.fr/pub/docs/manual-ocaml/libref/Str.html11

for threads, ocamlopt also needs "-thread" and "threads/threads.cmxa"

Certain functions in unix.cmxa can be useful, but aren't often useful in algorithms. It's probably too dangerous to include for security reasons anyway.

This has been requested before:

https://www.spoj.pl/forum/viewtopic.php?t=3222

  • created

    Nov '06
  • last reply

    Sep '13
  • 12

    replies

  • 2.0k

    views

  • 6

    users

  • 3

    links

I guess it would be a good thing.
The only problem I see is: wouldn't it considerably (i.e. tens of milliseconds) slow down programs which don't use it?

Threads are pointless (and forbidden) anyway.

Well, security checks are done on a lower level - but there's no point in including something that wouldn't work or wouldn't help anyway.

Noix

I'll do a test or two and see what I come up with.

First I generated 100,000 integers and wrote them (one per line) to the file "randomnums.txt".

Using ocaml I wrote a program to read the 100,000 numbers and cube them. It times the time it takes to do that and does it 100 times. Then it takes the average. I ran the program 3 times without linking nums.cmxa or str.cmxa, and 3 times with them.

So in all I read the 100,000 numbers 600 times; 300 times without the libraries, and 300 times with.

I give the results here and then the code. Please note that the numbers the program gives are seconds. In addition, following the program's output, the time command gives the system resources the program used to run. I compiled with -unsafe because I remember seeing in another post that the judging system compiles with it.

Here is without the extra libraries:

Jeff$ ocamlopt -unsafe -o timecubes test.ml
Jeff$ time ./timecubes
0.03027624
real    0m3.040s
user    0m2.894s
sys     0m0.142s
Jeff$ time ./timecubes
0.03027839
real    0m3.037s
user    0m2.894s
sys     0m0.140s
Jeff$ time ./timecubes
0.03028689
real    0m3.038s
user    0m2.894s
sys     0m0.141s

Here is with the extra libraries:

Jeff$ ocamlopt -unsafe -o timecubes str.cmxa nums.cmxa test.ml
Jeff$ time ./timecubes0.03027955
real    0m3.041s
user    0m2.894s
sys     0m0.143s
Jeff$ time ./timecubes
0.03027548
real    0m3.037s
user    0m2.894s
sys     0m0.140s
Jeff$ time ./timecubes
0.03028571
real    0m3.038s
user    0m2.894s
sys     0m0.141s
Jeff$ time ./timecubes
0.03028155
real    0m3.037s
user    0m2.894s
sys     0m0.140s

So there is perhaps an increase in the time it takes to execute the program, but the difference is closer to .00001 second than it is to .01 second. I'll leave it up to you to conclude if this is significant, but it looks like an insignificant amount of time to me.

PS this was run on a 2 ghz intel core duo.

Here's the source code:

(*
store the execution times as a list of type float.
doit reads numbers from the given input channel, which contains 100,000 integers, 1 per line. it calculates the cube of each.
time_doit opens random_nums.txt which contains 100,000 integers, runs doit on the file, then closes it. It puts the time doit took to run on the list, times.
average_time gives the average time of n exections of time_doit
*)
let times = ref [];;
let rec doit ic =
  let n = int_of_string (input_line ic) in
   ignore(n*n*n);
  doit ic;;
let time_doit () =
  let inchan = open_in "randomnums.txt" in
  let start = Sys.time () in
  try
    doit inchan;
  with _ ->
    close_in inchan;
    times := (Sys.time () -. start)::(!times);;
let rec average_time = function
    0 ->
      (List.fold_left
	(fun sum a -> sum+.a)
	0.0
	!times) /. float_of_int (List.length (!times))
  | n ->
      time_doit ();
      average_time (n-1);;
print_endline (string_of_float (average_time 100));
exit 0;

edit - I forgot to close the input channel! It didn't matter, but I had to fix it anyway smile It shouldn't change the results in a significant way.

[compiling ocaml programs with str and num]

Thanks for the help!

From now on, Ocaml programs are compiled/linked aganist "str" and "num" libraries.
Old solutions seem to work. Equally fast.

If you could, please test if "str" and "num" work now.

Noix

I tried solving problem 31 by using Big_int but I got a compilation error:

No implementations provided for the following modules:
  Big_int referenced from /tmp/tested.cmx

Here's the code I tried, which compiles locally for me. However, I get the same error if I omit "nums.cmxa" from the compiler arguments.

open Big_int;;
let rec multiply =
  function
      0 -> exit 0;
    | n ->
	Scanf.scanf
	  "\n%s %s"
	  (fun a b ->
	    print_endline
	      (string_of_big_int (mult_big_int (big_int_of_string a) (big_int_of_string b))));
	multiply (pred n);;
multiply (Scanf.scanf "%i" (fun a -> a));;

afaik the command for compiling should be

ocamlopt -unsafe -o outfile nums.cmxa str.cmxa source.ml

just to test the str.cmxa library, I submitted

Str.string_match (Str.regexp "[a-z]+") "aaa" 0;;
exit 0;;

but got

No implementations provided for the following modules:
  Str referenced from /tmp/tested.cmx

Sorry, it should be ok now (this code works now - it gives "WA" for TEST problem).

The problem was that I compiled it as "source.ml nums.cmxa str.cmxa", not "nums.cmxa str.cmxa source.ml".

3 years later

Are those libraries disabled again?

I get compilation error:

No implementations provided for the following modules:
  Big_int referenced from /tmp/tested.cmx
5 months later

These libraries are still enabled. Please, check your code again.

5 months later
2 years later

Czy wie ktoś się stało z tym zadaniem? Nie można otworzyć strony ponieważ wyskakuje komunikat "Nieprawidłowy kod problemu".