638 search results for ""
-
Mutability and Imperative Control Flow
Remark: References Are Single Field Records
create does the same as the ref function provided by the standard library. assign does the same as the ( := ) operator. deref does the same as the ( ! ) operator. The functions: Since ref
Introduction -
Mutability and Imperative Control Flow
References Inside Closures
Calling c2 () increments the counter associated with c2 . Since c2 has its own independent counter, it starts at 0. Another call to c1 () increments its counter, resulting in 2. Calling c1 (
Introduction -
Your First OCaml Program
A Sneak-Peek at Dune as a One-Stop Shop
The _build directory is where Dune stores all the files it generates. It can be deleted at any time, but subsequent builds will recreate it. Running tests Generating documentation Producing packag
First Steps -
A Tour of OCaml
Expressions and Definitions
This is interpreted as: “define dummy as the result of the structural equality test between the strings "hi" and "hello" .” OCaml also has a double equal operator == , which stands for phys
First Steps -
OCaml Programming Guidelines
Never copy-paste code when programming
In conclusion, copy-pasting code leads to programs that are more difficult to read and more difficult to maintain. It must be banished. Moreover, it is difficult to identify that the same ten line
Resources -
The Compiler Frontend: Parsing and Type Checking
Commonly Used Extension Attributes
This allows you to take advantage of a community of syntax augmentation. There are also a number of builtin attributes in the core OCaml compiler. Some are performance oriented and give directi
Runtime & Compiler -
Your First OCaml Program
Compiling OCaml Programs
In the rest of this tutorial, we will make more changes to this project in order to illustrate OCaml's tooling. Voilà! You've just written your first OCaml program. Let's see what happens when we
First Steps -
Profiling
Partially Applied Functions and Closures
Calling Array.map in this way is undoubtedly slower than writing a loop over the array by hand. The overhead is mainly in the fact that the closure must be evaluated for each element of the arr
Guides -
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 -
Understanding the Garbage Collector
The Long-Lived Major Heap
A major garbage collection must also stop the world to ensure that blocks can be moved around without this being observed by the live application. The mark-and-sweep phases run incrementally over
Runtime & Compiler -
Mutability and Imperative Control Flow
Bad: Undocumented Side Effects
If you're writing functions with non-obvious side effects, don't shadow existing definitions. Instead, give the function a descriptive name (for instance, Array.copy_with_analytics ) and document th
Introduction -
Understanding the Garbage Collector
Controlling Major Heap Collections
The space_overhead setting controls how aggressive the GC is about setting the slice size to a large size. This represents the proportion of memory used for live data that will be "wasted" beca
Runtime & Compiler -
Understanding the Garbage Collector
Generational Garbage Collection
<div class="note"> OCaml uses different memory layouts and garbage-collection algorithms for the major and minor heaps to account for this generational difference. We'll explain how they differ
Runtime & Compiler -
Memory Representation of Values
Polymorphic Variants
The extra space usage is generally not significant in a typical application, and polymorphic variants offer a great deal more flexibility than normal variants. However, if you're writing code tha
Runtime & Compiler -
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 -
Values and Functions
Anonymous Functions with Multiple Parameters
The way sweet_cat is written is an abbreviated version of sour_cat . Such a way of shortening syntax is called syntactic sugar . The way sour_cat is written corresponds more explicitly to the
Introduction -
Understanding the Garbage Collector
Allocating on the Minor Heap
Allocating a block in the minor heap just requires ptr to be decremented by the size of the block (including the header) and a check that it's not less than limit . If there isn't enough space
Runtime & Compiler -
Profiling
The “hello, world” Program
What have I missed out from this simple example? Well, the text segment above is not the whole story. It would be really nice if OCaml translated that simple hello world program into just the fiv
Guides -
The Compiler Frontend: Parsing and Type Checking
Static Type Checking
Subtyping in OCaml objects is always an explicit operation (via the :> operator). This means that it doesn't complicate the core type inference engine and can be tested as a separate concern. T
Runtime & Compiler -
The Compiler Backend: Bytecode and Native code
Embedding Native Code in C
<div class="note"> The embed_native.o is a standalone object file that has no further references to OCaml code beyond the runtime library, just as with the bytecode runtime. Do remember that
Runtime & Compiler -
Generating Documentation With odoc
Generating .mld Documentation Pages
For more information on how to write documentation pages for odoc , see the odoc for authors documentation . A common place to put .mld files is a directory named doc or docs . To make
Documentation -
Values and Functions
Types of Functions of Multiple Parameters
The type arrow operator associates to the right . Function types without parentheses should be treated as if they have parentheses to the right in the same way that the type of dummy_cat was decla
Introduction -
Managing Dependencies With opam
Adding Dependencies From the opam Repository
However, opam remains a central piece of the ecosystem, and it's very likely that you will have to work with *.opam files at some point, so we don't take a stance on whether you should specify yo
Projects -
The Compiler Frontend: Parsing and Type Checking
The Mapping Between Files and Modules
These two files produce essentially the same result as the following code. and a corresponding signature file: Create a file called alice.ml with the following contents: Individual compi
Runtime & Compiler -
Understanding the Garbage Collector
Setting the Size of the Minor Heap
</div> Changing the GC size dynamically will trigger an immediate minor heap collection. Note that Core increases the default minor heap size from the standard OCaml installation quite significa
Runtime & Compiler -
OCaml Programming Guidelines
Indenting expressions inside clauses
Criticism : May be not compact enough. For simple pattern-matchings (or simple clauses in complex matchings), the rule does not benefit readability. Justification : I don't see any reason for thi
Resources -
Running Executables and Tests with Dune
Automatic Recompiling on File Changes
dune build --watch monitors files and triggers necessary recompilation. Dune locks the build directory, so you can't run two Dune commands simultaneously. To run the application, stop the watch proce
Projects -
OCaml Programming Guidelines
How to Indent let ... in Constructs
Criticism : Lack of consistency. <!-- $MDX skip --> Variation: some write the keyword in alone on one line to set apart the final expression of the computation: Justification : It is sugge
Resources -
The Compiler Backend: Bytecode and Native code
Inspecting Assembly Output
The assembly code is highly architecture-specific, so the following discussion assumes an Intel or AMD 64-bit platform. We've generated the example code using -inline 20 and -nodynlink since
Runtime & Compiler -
Understanding the Garbage Collector
Allocating on the Major Heap
Each chunk's data area starts on a page boundary, and its size is a multiple of the page size (4 KB). It contains a contiguous sequence of heap blocks that can be as small as one or two 4 KB page
Runtime & Compiler -
Your First OCaml Program
Modules and the Standard Library, Cont'd
This replaces the function print_endline with the function printf from the Printf module in the standard library. Building and executing this modified version should produce the same output as
First Steps -
OCaml Programming Guidelines
Always give the same name to function arguments which have the same meaning
If necessary, make this nomenclature explicit in a comment at the top of the file. If there are several arguments with the same meaning, then attach numeral suffixes to them.
Resources -
Introduction to the OCaml Toplevel
Using a Pre-Processor Extension (PPX) in UTop
Assuming that the ppx_deriving package is installed in your opam switch, you run #require "ppx_deriving.show" . To activate a PPX in UTop, all you need to do is to load the corresponding library.
Tooling -
Managing Dependencies With opam
Adding a Dependency to Your dune-project File
Once you have added your dependency, you can build your project with dune build which will regenerate the *.opam files. If the project generates the opam file from the dune-project (you can
Projects -
Functors
Using an Existing Functor: Set.Make
There are no duplicates in a Set . Therefore, the string "funkt" is only displayed once, although it appears twice in the dune file. The functions StringSet.of_list and StringSet.iter are
Module System -
A Tour of OCaml
Type Parameters and Higher-Order Functions
Input list elements have the same type of its input. Output list elements have the same type of its output. The function List.map can be applied on any kind of list. Here it is given a list of int
First Steps -
OCaml Programming Guidelines
How to Develop as a Team: Version Control
An anonymous Git read-only mirror contains the working sources of the OCaml compilers , and the sources of other software related to OCaml. Users of the Git software version control system
Resources -
OCaml Programming Guidelines
No beastly indentation of functions and case analyses
Justification : You bump into the margin. The aesthetic value is doubtful. <!-- $MDX skip --> but choose to indent the line under the let keyword: <!-- $MDX skip --> This consists in ind
Resources -
A Tour of OCaml
Functions with Multiple Parameters and Partial Application
The function cat_hi , which resulted from the partial application of cat , behaves as follows: This returns a function that expects a single string, here the b from the definition of cat . Th
First Steps -
Memory Representation of Values
Obj Module Considered Harmful
Due to this encoding, there is a limit around 240 variants with parameters that applies to each type definition, but the only limit on the number of variants without parameters is the size of the
Runtime & Compiler -
Memory Representation of Values
Blocks and Values
int or char are stored directly as a value, shifted left by 1 bit, with the least significant bit set to 1. unit , [] , false are all stored as OCaml int 0. true is stored as OCaml int
Runtime & Compiler -
Understanding the Garbage Collector
The Gc Module and OCAMLRUNPARAM
</div> You can also control the behavior of OCaml programs by setting the OCAMLRUNPARAM environment variable before launching your application. This lets you set GC parameters without recompili
Runtime & Compiler -
Your First OCaml Program
Why Isn't There a Main Function?
However, it is common practice to single out a value that triggers all the side effects and mark it as the intended main entry point. In OCaml, that's the role of let () = , which evaluates the expr
First Steps -
OCaml Programming Guidelines
Indentation to the function's name
Justification : No indentation problem. If the name given to the expressions is meaningful, the code is more readable. Additional justification : If the argument's evaluation produces side effe
Resources -
The Compiler Backend: Bytecode and Native code
Debugging Native Code Binaries
Extra debugging information is inserted into the output assembly when the library is compiled in debug mode. These include the CFI stubs you will have noticed in the profiling output earlier ( .c
Runtime & Compiler -
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 -
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 -
Preprocessors and PPXs
One PPX for Multiple OCaml Versions
ppxlib deals with this issue by converting Parsetree types to and from the latest version. A PPX author then only needs to maintain their transformation for OCaml's latest version and get a PPX
Advanced Topics -
The Compiler Backend: Bytecode and Native code
Executing Bytecode
Dune can build a self-contained bytecode executable if you specify the byte_complete mode in the executable rule. For example, this dune file will generate a prog.bc.exe target: The custom
Runtime & Compiler -
Understanding the Garbage Collector
Understanding Allocation
<div class="note"> These poll points check ptr against limit and developers should expect them to be placed at the start of every function and the back edge of loops. The compiler includes
Runtime & Compiler