816 search results for ""

Showing 501 - 550
  1. Formatting and Wrapping Text

    Packing and Structural “hov” Boxes

    the vertical or horizontal packing box (as obtained by the open_hovbox procedure): break hints are used to cut the line when there is no more room on the line; no new line occurs if there is

    Tutorials
  2. Formatting and Wrapping Text

    Differences Between a Packing and a Structural “hov” Box

    If we replace the packing boxes by structural boxes (open_box), each break hint that precedes a closing parenthesis can show the boxes structure, if it leads to a new line; hence [(---[(----[(

    Tutorials
  3. Formatting and Wrapping Text

    Practical Advice

    Boxes must be opened and closed consistently ( open_* and close_box must be nested like parentheses). Never hesitate to open a box. Output many break hints, otherwise the pretty-printer is in a

    Tutorials
  4. Formatting and Wrapping Text

    Printing to stdout : Using printf

    For instance “ @[ ” open a box ( open_box 0 ). You may precise the type as an extra argument. For instance @[<hov n> is equivalent to open_hovbox n . “ @] ” close a box (

    Tutorials
  5. Formatting and Wrapping Text

    A Concrete Example

    <!-- $MDX skip --> In Caml Light, replace the first line by: I use the format library to print the lambda-terms: First, I give the abstract syntax of lambda-terms: Thus the problem is to pr

    Tutorials
  6. Formatting and Wrapping Text

    Most General Pretty-Printing: Using fprintf

    Given those general printing routines, procedures to print to stdout or stderr is just a matter of partial application: Using fprintf , the lambda-terms printing routines can be written as

    Tutorials
  7. Command-line Arguments

  8. Command-line Arguments

    Sys.argv

    <!-- $MDX dir=examples --> Note that ocaml launched a subprocess that actually runs the program where argv is args.ml arg1 arg2 arg3 . You can also compile your program using ocamlopt -o ar

    Tutorials
  9. Command-line Arguments

    Using the Arg Module

    The Arg module has many more actions than just Set and Set_string , and some lower-level function for parsing more complicated command lines. Here is the whole program: <!-- $MDX dir=exam

    Tutorials
  10. Command-line Arguments

    Other Tools for Parsing Command-Line Options

    Cmdliner is a modern interface for command line processing, which also generates UNIX man pages automatically. Clap is an imperative command line parser. Minicli has good support for rej

    Tutorials
  11. File Manipulation

  12. File Manipulation

    Buffered Channels

    channels that write to a file: type out_channel channels that read from a file: type in_channel The normal way of opening a file in OCaml returns a channel . There are two kinds of channels:

    Tutorials
  13. File Manipulation

    Writing

    Standard out_channel s: stdout , stderr Commonly used functions: open_out , open_out_bin , flush , close_out , close_out_noerr Open the file to obtain an out_channel Write to the channel

    Tutorials
  14. File Manipulation

    Reading

    Standard in_channel : stdin Commonly used functions: open_in , open_in_bin , close_in , close_in_noerr Open the file to obtain an in_channel Read characters from the channel. Reading consum

    Tutorials
  15. File Manipulation

    Seeking

    Whenever you write or read something to or from a channel, the current position changes to the next character after what you just wrote or read. Occasionally, you may want to skip to a particular

    Tutorials
  16. File Manipulation

    Gotchas

    Don't forget to flush your out_channel s if you want to actually write something. This is particularly important if you are writing to non-files such as the standard output ( stdout ) or a socke

    Tutorials
  17. File Manipulation

    Example

    <!-- $MDX dir=examples --> We can compile and run this example: <!-- $MDX file=examples/file_manip.ml -->

    Tutorials
  18. Calling C Libraries

  19. Calling C Libraries

    MiniGtk

    (Pop quiz: what happens if we need to define a class which is both a base class from which other classes can be derived, and is also a non-virtual class of which the user should be allowed to cre

    Tutorials
  20. Calling C Libraries

    Wrapping Calls to C Libraries

    In order for it to get passed to OCaml code at all, we must somehow convert it to a value . Luckily we can quite easily use the C API to create value blocks which the OCaml garbage collector

    Tutorials
  21. Calling Fortran Libraries

  22. Calling Fortran Libraries

    Step 1: Compile the Fortran Routine

    Note that it's up to the caller to know that dens and temp are actually arrays. Failure to pass an array will cause a segmentation violation since the gtd6_ function is using them as arrays (

    Tutorials
  23. Calling Fortran Libraries

    Step 2: Create the C Wrapper

    The file must include the OCaml header files alloc.h , memory.h , and mlvalue.h . The function first calls the CAMLparam5 macro. This is required at the start of any function that uses the CAM

    Tutorials
  24. Calling Fortran Libraries

    Step 3: Compile the Shared Library

    This will create a shared object library called wrapper.so containing the fortran function and the wrapper function. The -lg2c option is required to provide the implementations of the built in fo

    Tutorials
  25. Calling Fortran Libraries

    Step 4: Now to OCaml

    And voila, we've called the fortran function from OCaml. prompt> ocamlc -o test gtd6.cmo wrapper.so prompt> ocamlc -c gtd6.ml At this point, the steps that are given are to compile this into byt

    Tutorials
  26. Using the OCaml Compiler Toolchain

  27. Using the OCaml Compiler Toolchain

    Compilation Basics

    In this section, we will first see how to compile a simple program using only ocamlc or ocamlopt . Then we will see how to use libraries and how to take advantage of the findlib system, wh

    Guides
  28. Using the OCaml Compiler Toolchain

    The ocamlc and ocamlopt Compilers

    If your program depends upon third-party libraries, you must pass them on the command line. You must also indicate the libraries on which these libraries depend. You must also pass the -I option

    Guides
  29. Using the OCaml Compiler Toolchain

    Using the ocamlfind Front-End

    Separate compilation (one command for module1.ml , another for module2.ml and another to link the final output) is usually not performed manually but only when using an automated build system t

    Guides
  30. Using the OCaml Compiler Toolchain

    Interlude: Making a Custom Toplevel

    OCamlfind also supports ocamlmktop : We run toplevel and get an OCaml toplevel with modules Unix , Module1 , and Module2 all available, allowing us to experiment interactively with our p

    Guides
  31. Using the OCaml Compiler Toolchain

    Dune: An Automated Build System

    The dune quick-start guide shows you how to write such description files for more complicated situations, and how to structure, build, and run dune projects. The most popular modern system

    Guides
  32. Using the OCaml Compiler Toolchain

    Bytecode Using Dune

    We can also do that with .exe . The .bc stands for generic bytecode file, and it can be an executable or library. Finally, we compile and execute it. Next, create a main.ml file, ad

    Guides
  33. Using the OCaml Compiler Toolchain

    Other Build Systems

    OMake Another OCaml build system. GNU make GNU make can build anything, including OCaml. May be used in conjunction with OCamlmakefile Oasis Generates a configure, build, and install system from

    Guides
  34. Debugging

  35. Debugging

    Tracing Functions Calls in the Toplevel

    The simplest way to debug programs in the toplevel is to follow the function calls, by “tracing” the faulty function:

    Guides
  36. Debugging

    Polymorphic Functions

    For our sorting routine, a single type constraint on the argument of the exchange function warranties a monomorphic typing, that allows a proper trace of function calls: A simple way to overc

    Guides
  37. Debugging

    Limitations

    Compiler builtins are described in the standard library documentation. At compile time, the __LOC__ builtin is substituted with its location in the program, described as a string "File %S, l

    Guides
  38. Debugging

    The OCaml Debugger

    ocamldebug runs on ocamlc bytecode programs (it does not work on native code executables), and it does not work under native Windows ports of OCaml (but it runs under the Cygwin port). We now

    Guides
  39. Debugging

    Launching the Debugger

    Then the debugger answers with a banner and a prompt: We launch the debugger: At runtime, the program raises an uncaught exception Not_found . Suppose we want to find where and why this e

    Guides
  40. Debugging

    Finding the Cause of a Spurious Exception

    To sum up: if you're developing a program you can compile it with the -g option, to be ready to debug the program if necessary. Hence, to find a spurious exception you just need to type ocamlde

    Guides
  41. Debugging

    Getting Help and Info in the Debugger

    To get more info about the current status of the debugger you can ask it directly at the toplevel prompt of the debugger; for instance:

    Guides
  42. Debugging

    Setting Break Points

    Now we can guess why List.assoc will fail to find "INRIA" in the list... Then, we can step and find what happens just before ( <|b|> ) List.assoc is about to be called in find_address : L

    Guides
  43. Debugging

    Using the Debugger Under Emacs

    Under Emacs you call the debugger using ESC-x ocamldebug a.out . Then Emacs will send you directly to the file and character reported by the debugger, and you can step back and forth using ES

    Guides
  44. Debugging

    Printing a Back Trace for an Uncaught Exception

    The environment variable OCAMLRUNPARAM also works when working on a program built with dune : From this back trace it should be clear that we receive a Not_found exception in List.assoc

    Guides
  45. Debugging

    Detecting a Data Race with Thread Sanitizer

    With the introduction of Multicore parallelism in OCaml 5, comes the risk of introducing data races among the involved Domain s. Luckily the Thread Sanitizer (TSan) mode for OCaml is helpful to

    Guides
  46. Debugging

    Installing a TSan Switch

    If the above fails during installation of conf-unwind with No package 'libunwind' found , try setting the environment variable PKG_CONFIG_PATH to point to the location of libunwind.pc , for

    Guides
  47. Debugging

    Detecting a Data Race

    The TSan instrumentation benefits from compiling programs with debug information, which happens by default under dune . To manually invoke the ocamlopt compiler under our 5.2.0+tsan switch

    Guides
  48. Error Handling

  49. Error Handling

    Errors as Special Values

    The rest of this document presents and compares approaches towards error handling. Exceptions provide a means to deal with errors at the control flow level, while option and result make error

    Guides
  50. Error Handling

    Exceptions

    Here, we add a variant Foo to the type exn and create a function that will raise this exception. Now, how do we handle exceptions? The construct is try ... with ... : Exceptions belong to

    Guides