OCaml isn’t Python. For one thing, it is a compiled language, not an interpreted language. You can certainly find ways to package up the compilation process, but as with C or Rust or what have you, generally speaking, you need to compile first and then run your code. This has some disadvantages (you add a step to execution) but also some advantages (OCaml can be many orders of magnitude faster in execution.)
[Edited to add: I may be really wrong here, see @kit-ty-kate’s answer below.]
you’re gonna have to rely on topfind pragma if you want libraries but if you just need the standard library (i would advise for it), it works just fine as is.
I don’t think there’s any reason to recommend against trying code this way, particularly while learning the language. It’s similar to working in an interactive ocaml/utop session which is a perfectly reasonable approach.
What the others say about “Ocaml is a compiled language, and isn’t meant to be used for source-code-level scripting” is pretty much right. But like any other language with a reasonable toplevel, you can do it.
Furthermore, there are serious packages, such as topkg that run as a script. We have an interpreter as part of OCaml, and there’s nothing wrong with taking advantage of it.
I have found that the final link stage of compilation can become a huge time sink relative to the compilation of touched files and found it beneficial to have a driver script load up libraries and launch without the linking of massive bytecode or native execs . Finished code can be packaged up as a compiled library leaving behind a smaller body of interpreted code going forward.
Not done this yet but on the wish-list is a way to move a bunch of files in and out of library status so as to maintain/extend it in the interpreter setting.
If you have a compelling need to do this, with a library that isn’t built into ocaml, you can build it into what’s in essence an extended ocaml interpreter. I’ve done that with the Unix library.
#require is ocamlfind specific it loads ocamlfind packages. utop integrates it by default but you can also use it in ocaml after a suitable #use "topfind":
> ocaml
OCaml version 4.12.0
# #use "topfind";;
- : unit = ()
Findlib has been successfully loaded. Additional directives:
#require "package";; to load a package
#list;; to list the available packages
#camlp4o;; to load camlp4 (standard syntax)
#camlp4r;; to load camlp4 (revised syntax)
#predicates "p,q,...";; to set these predicates
Topfind.reset();; to force that packages will be reloaded
#thread;; to enable threads
- : unit = ()