816 search results for ""
-
A Tour of OCaml
Conclusion
In this tutorial, OCaml was used interactively. The next tutorial, Your First OCaml Program , shows you how to write OCaml files, how to compile them, and how to kickstart a project. <!-- 1. Values
First Steps -
Your First OCaml Program
Your First OCaml Program
Working Within an opam Switch
When you work on several OCaml projects simultaneously, you should create more opam switches. For instructions on how to do that, see Introduction to opam Switches . When you installed OCaml, a glo
First StepsYour 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 StepsYour First OCaml Program
Watch Mode
Before we dive in, note that you will typically want to use Dune's watch mode to continually compile and optionally restart your program. This ensures that the language server has the freshest poss
First StepsYour 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 StepsYour 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 StepsYour First OCaml Program
Every File Defines a Module
<!-- FIXME refer to Dune/Library tutorial when available --> Note : If you add a file named hello.ml in the lib directory, Dune will consider this the whole Hello module and it will make En
First StepsYour First OCaml Program
Defining Module Interfaces
This is because we haven't changed lib/en.mli . Since it does not list hello , it is therefore private. Trying to compile this fails. Also edit the bin/main.ml file like this: Amend the
First StepsYour First OCaml Program
Defining Multiple Modules in a Library
A more detailed introduction to modules can be found at Modules . Finally, run dune build and dune exec hello to see the new output, using the modules you just created in the hello librar
First StepsYour First OCaml Program
Installing and Using Modules From a Package
Finally, execute as before: Fun fact : Dune configuration files are S-expressions. Before the example will build and run, you need to tell Dune that it needs Sexplib to compile the project. D
First StepsYour First OCaml Program
Using the Preprocessor to Generate Code
Here is the result: Finally, you'll also need to edit bin/main.ml v has the type string list . We're using String.split_on_char to turn a string into a string list by splitting the str
First StepsYour 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 StepsYour First OCaml Program
Minimum Setup
Note : minimo.exe is not a file name. This is how Dune is told to compile the minimo.ml file using OCaml's native compiler instead of the bytecode compiler. As a fun fact, note that an empty file
First StepsYour First OCaml Program
Conclusion
<!-- TODO: link Project Quickstart If you're already familiar with lists, maps, and folds, and need to be productive as fast as possible, dive into the “Project Quickstart” guide. --> This tuto
First StepsConfiguring Your Editor
Configuring Your Editor
Visual Studio Code
Upon first loading an OCaml source file, you may be prompted to select the toolchain in use. Pick the version of OCaml you are using, e.g., 5.1.0 from the list. For VSCode, install the OCaml Pl
ToolingConfiguring Your Editor
Editor Features at Your Disposal
If your editor is setup correctly, here are some important features you can begin using to your advantage:
ToolingConfiguring Your Editor
1) Hovering for Type Information
This is a great feature that lets 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 Ho
ToolingConfiguring Your Editor
2) Jump to Definitions With Ctrl + Click
If you hold down the Ctrl key while hovering, the code appears as a clickable link which if clicked takes you to the file where the implementation is. This can be great if you want to understand ho
ToolingConfiguring Your Editor
3) OCaml Commands With Ctrl + Shift + P
Pressing the key combination Ctrl + Shift + P opens a modal dialog at the top. If you type the word ocaml , you will be presented with a list of various OCaml commands at your disposal which can b
ToolingConfiguring Your Editor
Windows Users
If you used the DkML distribution, you will need to: 1. Go to File > Preferences > Settings view (or press Ctrl , ) 2. Select User > Extensions > OCaml Platform 3. Uncheck OCaml: U
ToolingConfiguring Your Editor
Emacs
A major mode, which, among other things, supports syntax highlighting and the structuring of indentation levels A minor mode, which will interact with a language server (such as ocaml-lsp-server ).
ToolingConfiguring Your Editor
Choosing a major mode
For the purposes of this tutorial, we are going to focus on the use of tuareg as the major mode, but you should feel free to experiment and choose your favourite one! To use tuareg , you can add
ToolingConfiguring Your Editor
MELPA and use-package
The use-package macro has been built into Emacs since version 29.1. If you are on an older version, you will need to install use-package manually. You will also need to ensure MELPA is configur
ToolingConfiguring 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 your switch : Next, we
ToolingConfiguring Your Editor
Finer configuration
OCaml-eglot can be finely configured, the project README gives several configuration paths to adapt perfectly to your workflow. You will also find there an exhaustive presentation of the different
ToolingConfiguring 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 con
ToolingConfiguring Your Editor
Structured Navigation and Editing
Note: For advanced structured navigation and editing, OCaml is supported by Combobulate . It leverages tree-sitter to provide syntax-aware movement and structural editing for your OCaml code. You
ToolingConfiguring Your Editor
Vim
After installing Merlin above, instructions will be printed on how to link Merlin with your editor. If you do not have them visible, just run this command: For Vim, we won't use the LSP server b
ToolingConfiguring Your Editor
Talking to Merlin
ToolingConfiguring Your Editor
Getting Type Information in Vim
In the Vim editor, press 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. Other Me
ToolingConfiguring Your Editor
Neovim
A newer, more recommended way is to use the new Neovim LSP API for versions newer than v0.11.0 via vim.lsp . A more traditional way is to use nvim-lspconfig . For more info, kickstart.nvim has a
ToolingConfiguring Your Editor
Using vim.lsp
See :h lsp-config for more detail on configuration options. Add this to your toplevel init.lua .
ToolingConfiguring Your Editor
Using vim.lsp With runtimepath
Then enable them in the toplevel init.lua . Add your LSP config to lsp/ocamllsp.lua . Your Neovim config should have the following structure now. Run the following at the root of your config
ToolingConfiguring Your Editor
Using nvim-lspconfig
There is no need to pass more settings to setup because nvim-lspconfig provides reasonable defaults. See the nvim-lspconfig OCaml LSP configuration for more info. Add this to your nvim-lspco
ToolingIntroduction to the OCaml Toplevel
Introduction to the OCaml Toplevel
Using Packages in UTop
ToolingIntroduction to the OCaml Toplevel
Loading Libraries In UTop
Tip : UTop knows about the available libraries and completion works. Outside utop you can use ocamlfind list to display the complete list of libraries. Note that opam package may bundle several l
ToolingIntroduction 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.
ToolingIntroduction to opam Switches
Introduction to opam Switches
What Is an opam Switch?
After installing OCaml , you'll have a single switch called default . An opam switch is an isolated OCaml environment. Each switch has its own OCaml compiler, installed packages, and binaries, all
ToolingIntroduction to opam Switches
Global vs. Local Switches
For project work, we recommend local switches. They keep each project's dependencies isolated and make it easy for collaborators to reproduce your setup. Global switches are named environments sto
ToolingIntroduction to opam Switches
Listing Available Compilers
Omitting ocaml-base-compiler shows the full list, including compiler variants with additional options enabled (such as flambda optimisations or frame pointers). These are useful for advanced use ca
ToolingIntroduction to opam Switches
Creating a Global Switch
This switch is now available from any directory. It lives at ~/.opam/my_switch/ . Then update your shell environment: To create a named global switch:
ToolingIntroduction to opam Switches
Creating a Local Switch
If the directory contains .opam files, opam will automatically install their dependencies into the new switch. You can prevent this with opam switch create . 5.4.1 --no-install . This creates an
ToolingIntroduction to opam Switches
Listing Your Switches
The arrow -> marks the currently active switch:
ToolingIntroduction to opam Switches
Switching Between Switches
Local switches are selected automatically when you cd into their directory, but you still need to run eval $(opam env) (or use a tool like direnv ) to update the shell environment. The eval $(
ToolingIntroduction to opam Switches
Removing a Switch
Learn more about opam switches in the opam manual and the opam-switch reference . For a local switch, use the path: To delete a switch you no longer need:
ToolingOCaml on Windows