816 search results for ""
-
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 -
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 -
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 -
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 -
How to Work with the Garbage Collector
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
GuidesHow 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
GuidesHow 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
GuidesFile Manipulation
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:
TutorialsFile 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
TutorialsFile 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
TutorialsFile 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
TutorialsFile 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
TutorialsFile Manipulation
Example
<!-- $MDX dir=examples --> We can compile and run this example: <!-- $MDX file=examples/file_manip.ml -->
TutorialsCommon Error Messages
Common Error Messages
Type Errors
ResourcesCommon 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
ResourcesCommon 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
ResourcesCommon 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
ResourcesCommon Error Messages
Pattern Matching Warnings and Errors
ResourcesCommon 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
ResourcesCommon 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
ResourcesCommon Error Messages
Problems Recompiling Valid Programs
ResourcesCommon 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
ResourcesCommon 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
ResourcesCalling C Libraries
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
TutorialsCalling 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
TutorialsTransitioning to Multicore with ThreadSanitizer
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
GuidesTransitioning 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
GuidesTransitioning to Multicore with ThreadSanitizer
Following the Workflow
We will now go through the proposed workflow for our example application.
GuidesTransitioning 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
GuidesTransitioning 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
GuidesTransitioning 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
GuidesTransitioning 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
GuidesTransitioning 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
GuidesTransitioning 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
GuidesComparison of Standard Containers
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
ResourcesComparison 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
ResourcesComparison 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
ResourcesComparison 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
ResourcesComparison 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
ResourcesComparison 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
ResourcesComparison 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.
ResourcesComparison 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
ResourcesUsing the OCaml Compiler Toolchain
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