638 search results for ""
-
OCaml Programming Guidelines
Naming Anonymous Functions
Justification : Much clearer, in particular if the name given to the function is meaningful. <!-- $MDX skip --> write <!-- $MDX skip --> In the case of an iterator whose argument is a comp
Resources -
Functors
Write a Functor to Extend Modules
Modules Array and List appear augmented with Array.scan_left and List.scan_left . For brevity, the output of the first two toplevel commands is not shown here. Run the dune utop command. O
Module System -
OCaml on Windows
Visual Studio Code (VSCode)
If you use WSL2 , you will remotely connect to your WSL2 instance from VSCode. Microsoft has a useful blog post that covers getting WSL2 and Visual Studio Code connected. Go to File > Prefer
Resources -
Values and Functions
Local Definitions
In both examples, d and e are local definitions. Arbitrary combinations of chaining or nesting are allowed. e is bound to 6 inside e * 5 d is bound to 30 inside d * 7 Here is how scopin
Introduction -
Basic Data Types and Pattern Matching
Introduction
<!-- The goal of this tutorial is to provide for the following capabilities: - Handle data of all predefined types using dedicated syntax - Write variant type definitions: simple, recursive, and poly
Introduction -
Introduction to opam Switches
Local Switches
Local switches are automatically selected based on the current working directory. When you navigate into a directory with an associated local switch, opam uses that switch for any OCaml-related opera
Tooling -
OCaml Programming Guidelines
Usage in Module Interface
The function's usage must appear in the module's interface that exports it, not in the program that implements it. Choose comments as in the OCaml system's interface modules, which will subsequen
Resources -
Basic Data Types and Pattern Matching
User-Defined Types
These three kinds of type definitions are covered in the next three sections. Variant Record Aliases User-defined types are always introduced using the type … = … statement. The keyword type
Introduction -
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 -
Values and Functions
Function as Values
<!-- For now, let's put aside those definitions and instead start playing with functions. Their meaning will arise from experience. Once things make sense, using these terms is just a means to interac
Introduction -
Error Handling
Asynchronous Exceptions
The latter is thrown when the user interrupts an interactive execution. Because they are loosely or not at all related with the program logic, it mostly doesn't make sense to track the place wher
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 -
Higher Order Functions
Async code
<!-- Comment let f () = 42 let f () = g 42 One possible issue with the repeat function used in the intro comes from the fact it is used to produce a side effect. Therefore two stories get mixed tog
Introduction -
Configuring Your Editor
1) Hovering for Type Information
This is a great feature that let's you see type information of any OCaml variable or function. All you have to do is place your cursor over the code and it will be displayed in the tooltip. VSCode H
Tooling -
Configuring Your Editor
Getting Type Information
OCaml-eglot README provides a comprehensive overview of all the functions available in this mode! Emacs Type information Opening an OCaml file should launch an ocaml-lsp server, and you can co
Tooling -
Installing OCaml
Install Platform Tools
You're now all set and ready to start hacking. All these tools can be installed using a single command: UTop , a modern interactive toplevel (REPL: Read-Eval-Print Loop) Dune , a fast and full-fe
First Steps -
Configuring Your Editor
Getting Type Information
In the Vim editor, press the Esc to enter command mode. Place the cursor over the variable. Type :MerlinTypeOf and press Enter . The type information will be displayed in the command bar. Othe
Tooling -
Operators
Using Binary Operators
Finally, in the third line, all the arguments expected by List.filter are provided. The returned list contains the values satisfying the ( <= ) 10 function. The first shows the List.filter typ
Advanced Topics -
Error Handling
Safe vs. Unsafe Functions
The main ways to write such safe error-handling functions are to use either option (next section) or result (following section) values. Although handling errors in data using those types may a
Guides -
Managing Dependencies With opam
Installing a Git Dependency in Your Switch
You can install a package in your active switch directly from a Git URL:
Projects -
Functors
Extending a Module with a Standard Library Functor
This allows the user to seemingly extend the module String with a submodule Set . Check the behaviour using opam exec -- dune exec funkt < dune . funkt.ml Using the include statement, here i
Module System -
OCaml Programming Guidelines
Using Tab Stops
Justification : Between one display and another, the indentation of the program changes completely. It can also become completely wrong if the programmer used both tabulations and spaces to inden
Resources -
Values and Functions
Anonymous Functions
Anonymous functions are often passed as arguments to other functions. The identity function, which takes anything and returns it unchanged The square function, which takes an integer and returns it
Introduction -
Values and Functions
Defining Global Functions
The former explicitly binds the anonymous function to a name. The latter uses a more compact syntax and avoids the fun keyword and the arrow symbol. The expression, which happens to be a function
Introduction -
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 -
Basic Data Types and Pattern Matching
Function Parameter Aliases
This is useful for matching variant values of parameters. Function parameters can also be given a name with pattern matching for tuples and records.
Introduction -
Configuring Your Editor
LSP setup for OCaml
You are now ready to edit OCaml code productively with Emacs! And that's all there is to it! Now all you need to do is install ocaml-lsp-server and ocamlformat in our switch : Next, we n
Tooling -
Modules
Displaying a Module's Interface
If you are using Dune, .cmi file are in the _build directory. Otherwise, you can compile manually to generate them. The command ocamlc -c cairo.ml will create cairo.cmo (the executable byteco
Module System -
OCaml Programming Guidelines
Formatting Guidelines
Pseudo spaces law : never hesitate to separate words in your programs with spaces. The space bar is the easiest key to find on the keyboard, so press it as often as necessary! If you choose no
Resources -
Introduction to opam Switches
Global Switches
Opam's system switch is a global switch that is associated with the OCaml installation on your operating system. The system switch is accessible across the entire system. When creating an opam swi
Tooling -
Profiling
Polymorphic Types
Lesson: if you have a function which is unintentionally polymorphic then you can help the compiler by specifying types for one or more of the arguments. Disappointingly although the definition o
Guides -
Objects
Inheritance and Coercions
Is it possible to coerce from a superclass (e.g., widget ) to a subclass (e.g., button )? The answer, perhaps surprisingly, is NO! Coercing in this direction is unsafe . You might try to coerc
Advanced Topics -
Hash Tables
Module Hashtbl
To find out whether there is an entry in my_hash for a letter we would do: In other contexts, one may prefer new values to replace the previous ones. In this case, one uses Hashtbl.rep
Data Structures -
Installing OCaml
Initialise opam
Any problems installing? Be sure to read the latest release notes . You can file an issue at <https://github.com/ocaml/opam/issues> or <https://github.com/ocaml-windows/papercuts/issues> . Note
First Steps -
Monads
Composition and Monad Laws
In that formulation, it becomes immediately clear that return is a left and right identity, and that composition is associative. Law 1: return >=> f behaves the same as f . Law 2: f >=> r
Data Structures -
OCaml Programming Guidelines
Arithmetic operators: the same rules as in mathematics
For example: 1 + 2 * x means 1 + (2 * x) .
Resources -
OCaml Programming Guidelines
How to Choose Identifiers
It's hard to choose identifiers whose name evokes the meaning of the corresponding portion of the program. This is why you must devote particular care to this, emphasising clarity and regularity
Resources -
Fix Homebrew Errors on Apple M1
Disable Rosetta
It's essential to close your current Terminal window and open a new one for it to work properly. Then run the following command. If you get the output shown, you're ready to brew! Close Terminal
Resources -
OCaml Programming Guidelines
Managing Program Development
Below are tips from veteran OCaml programmers that have served in developing the compilers. These are good examples of large, complex programs developed by small teams.
Resources -
Formatting and Wrapping Text
Principles
boxes : a box is a logical pretty-printing unit, which defines a behaviour of the pretty-printing engine to display the material inside the box. break hints : a break hint is a directive to the p
Tutorials -
Values and Functions
What is a Value?
An expression's type (before evaluation) and its resulting value's type (after computation) are the same. This allows the compiler to avoid runtime type checks in binaries. In OCaml, the compiler rem
Introduction -
OCaml Programming Guidelines
Sequences inside branches of if
<!-- $MDX skip --> In the same way, a sequence which appears in the then or else part of a conditional must be delimited:
Resources -
OCaml Programming Guidelines
Height of the Page
Justification : When a function goes beyond one screenful, it's time to divide it into subproblems and handle them independently. Beyond a screenful, one gets lost in the code. The indentation is
Resources -
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 -
A Tour of OCaml
Defining a Higher-Order Function
It is possible to pass a function as argument to another function. Functions having other functions as parameters are called higher-order functions. This was illustrated earlier using function L
First Steps -
Monads
The Monad Signature
So the next few sections look at several different examples of code in which monads can be discovered. Because monads are a design pattern, they aren't always obvious; it can take some study to t
Data Structures -
OCaml Programming Guidelines
Subdividing into modules
For each interface, you must document the things defined by the module: functions, types, exceptions, etc. For each module, you must explicitly write an interface. You must subdivide your program
Resources -
OCaml on Windows
Diskuv OCaml
To learn more about Diskuv OCaml, see the official Diskuv OCaml documentation . Check that OCaml is installed properly with the following commands in your shell (PowerShell or Command Prompt).
Resources -
Running Commands in an opam Switch
Using opam exec
This will launch the version of the OCaml REPL within the context of the current opam switch. Example: Replace <command> with the actual command you want to run. This ensures that the command is
Projects -
Profiling
Using Instruments on macOS
From there, you can click on your program there and dig to see which functions are taking the longest to execute. <img width="100%" alt="macOS Instruments" src="/media/tutorials/macos-instruments.
Guides