816 search results for ""

Showing 101 - 150
  1. 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
  2. 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
  3. 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
  4. 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
  5. How to Work with the Garbage Collector

  6. How to Work with the Garbage Collector

    The Gc Module

    (We haven't seen the { expression with field = value } form before, but it should be mostly obvious what it does). The above code causes the GC to print a message at the start of every major co

    Guides
  7. How to Work with the Garbage Collector

    Finalisation and the Weak Module

    <!-- $MDX skip --> Finally, we have some test code. I won't reproduce the test code here, but you can download the complete program and test code objcache.ml and compile it with: The sync_r

    Guides
  8. How to Work with the Garbage Collector

    Exercises

    Implement the record as an object , and allow it to transparently pad/unpad strings. You will need to provide methods to set and get the name and address fields (four public methods in all). Hid

    Guides
  9. File Manipulation

  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. File Manipulation

    Example

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

    Tutorials
  16. Common Error Messages

  17. Common Error Messages

    Type Errors

    Resources
  18. Common Error Messages

    This expression has type ... but is here used with type ...

    For the compiler, the second definition of my_type is totally independent from the first definition. So we have defined two types which have the same name. Since "a" was defined earlier, it belon

    Resources
  19. Common Error Messages

    Warning: This optional argument cannot be erased

    See the Labels section for more details on functions with labelled arguments. The solution is simply to add one argument of type unit, like this: Functions with optional arguments must have

    Resources
  20. Common Error Messages

    The type of this expression... contains type variables that cannot be generalized

    Now x has a known type: The compiler tells us that the type of x is not fully known yet. But by using x later, the compiler can infer the type of x : Data of type '_weak<n> may be al

    Resources
  21. Common Error Messages

    Pattern Matching Warnings and Errors

    Resources
  22. Common Error Messages

    This pattern is unused

    In our example, it is now clear that only the first item of the pair will ever be tested. This leads to the following results: the tree of cases is traversed linearly, from left to right. There

    Resources
  23. Common Error Messages

    This pattern-matching is not exhaustive

    In general, that kind of simplification is not possible and the best solution is to add a catch-all case which should never be reached: A short solution without pattern matching would be: O

    Resources
  24. Common Error Messages

    Problems Recompiling Valid Programs

    Resources
  25. Common Error Messages

    x.cmi is not a compiled interface

    It means that some_module.cmi is not valid according to the current version of the OCaml compiler. Most of the time, removing the old compiled files ( .cmi, .cmo, *.cmx, ...) and recompiling is

    Resources
  26. Common Error Messages

    Warning: Illegal backslash escape in string

    Recent versions of OCaml warn you against unprotected backslashes in strings since they should be doubled. Such a message may be displayed when compiling an older program, and can be turned off

    Resources
  27. Calling C Libraries

  28. 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
  29. 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
  30. Transitioning to Multicore with ThreadSanitizer

  31. Transitioning to Multicore with ThreadSanitizer

    An Example Application

    Underneath the hood, the library may have been implemented in various ways. Consider the following thread-unsafe implementation in bank.ml : Consider a little bank library with the following

    Guides
  32. Transitioning to Multicore with ThreadSanitizer

    A Proposed Workflow

    Install TSan Write a parallel test runner Run tests under TSan If TSan complains about data races, address the reported issue and go to step 2. Now if we want to see if this code is Multicore read

    Guides
  33. Transitioning to Multicore with ThreadSanitizer

    Following the Workflow

    We will now go through the proposed workflow for our example application.

    Guides
  34. Transitioning to Multicore with ThreadSanitizer

    Install the Instrumenting TSan Compiler (Step 0)

    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 e

    Guides
  35. Transitioning to Multicore with ThreadSanitizer

    Write a Parallel Test Runner (Step 1)

    From the above run under a regular 5.1.0 compiler, one may get the impression that everything is OK, as the balances sum to a total of $700 as expected, indicating that no money is lost. One

    Guides
  36. Transitioning to Multicore with ThreadSanitizer

    Run the Parallel Tests Under TSan (Step 2)

    A write in one Domain coming from the array assignment in Bank.transfer A read in another Domain coming from a call to Stdlib.Array.iteri to read and print the array entries in print_balan

    Guides
  37. Transitioning to Multicore with ThreadSanitizer

    Address the Reported Races and Rerun the Tests (Steps 3 and 2)

    How come we may hit a resource deadlock error when adding just two pairs of Mutex.lock and Mutex.unlock calls? Rerunning our tests, we obtain: One way to address the reported races is to

    Guides
  38. Transitioning to Multicore with ThreadSanitizer

    Address the Reported Races and Rerun the Tests, Take 2 (Steps 3 and 2)

    This works well and TSan no longer complains, so our little library is ready for OCaml 5.x parallelism, hurrah! We can now rerun our tests under TSan to confirm the fix: Oh, wait! When raisin

    Guides
  39. Transitioning to Multicore with ThreadSanitizer

    Final Remarks and a Word of Warning

    With that warning in mind and TSan in hand, you should now be equipped to hunt for data races. As a final word of warning, Domain s are so fast that in a too simple test runner, one Domain

    Guides
  40. Comparison of Standard Containers

  41. Comparison of Standard Containers

    Lists: Immutable Singly-Linked Lists

    Not very efficient for: random access, indexed elements Well-suited for: I/O, pattern-matching Adding an element: O(1) , cons operator :: Length: O(n) , function List.length Accessing cell i

    Resources
  42. Comparison of Standard Containers

    Arrays: Mutable Vectors

    Well-suited for the following cases: dealing with sets of elements of known size, accessing elements by numeric index, and modifying in-place elements. Basic arrays have a fixed length. Adding an el

    Resources
  43. Comparison of Standard Containers

    Strings: Immutable Vectors

    Adding an element (by creating a new string): O(n) Length: O(1) Accessing character i : O(1) Finding an element: O(n) Strings are very similar to arrays, but they are immutable. Strings are s

    Resources
  44. Comparison of Standard Containers

    Set and Map: Immutable Trees

    Sets and maps are very useful in compilation and metaprogramming, but in other situations, hash tables are often more appropriate (see below). Adding an element: O(log n) Returning the number of

    Resources
  45. Comparison of Standard Containers

    Hashtbl: Automatically Growing Hash Tables

    Adding an element: O(1) if the initial size of the table is larger than the number of elements it contains; O(log n) on average if n elements have been added in a table which is initially mu

    Resources
  46. Comparison of Standard Containers

    Buffer: Extensible Strings

    Adding a char: O(1) if the buffer is big enough, or O(log n) on average if the initial buffer size was much smaller than the number of bytes n . Adding a string of k chars: O(k * "adding

    Resources
  47. Comparison of Standard Containers

    Queue

    Adding an element: O(1) Taking an element: O(1) Length: O(1) OCaml queues are mutable first-in-first-out (FIFO) data structures.

    Resources
  48. Comparison of Standard Containers

    Stack

    Adding an element: O(1) Taking an element: O(1) Length: O(1) OCaml stacks are mutable last-in-first-out (LIFO) data structures. They are just like lists except they are mutable, i.e., adding an

    Resources
  49. Using the OCaml Compiler Toolchain

  50. 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