Questions tagged [ocaml]
OCaml is a strict statically-typed functional programming language, focusing on expressiveness, correctness, and efficiency.
7,524
questions
0
votes
1
answer
11
views
How can I add the -ml flag (or any flag, really) to ocamllex in a dune file?
Here is my current dune file:
(library
(name parsing)
(libraries toto fmt menhirLib)
(modules parser lexer lex_and_parse)
)
(ocamllex lexer)
(menhir
(modules parser)
(flags --explain -v))
I know ...
0
votes
2
answers
56
views
Unbound record field api_key in my OCaml code
I keep getting the below error when I run dune build:
File "lib/ip2locationio.ml", line 25, characters 73-80:
25 | let uri = Uri.of_string ("https://api.ip2location.io/?key=" ^ ...
1
vote
0
answers
40
views
Is is possible to tell ocamlformat to ignore a specific portion of code
Is it possible to make ocamlformat ignore a specific portion of code by inserting some specific comments?
Here's what I tried, without success, using information found online:
(* @@disable *)
let ...
1
vote
2
answers
32
views
Subtype coercion with first-class modules
Given the following module signatures, where B subsumes A:
module type A = sig
type t
end
module type B = sig
type t
val x: t
end
Normally, any module that satisfies B will then also satisfy A....
-1
votes
1
answer
41
views
Dumping a data structure like pickle in python [closed]
Could you please recommend library where I could dump a runtime data structure such as a Map or Set which behaves like the pickle library that does the object serialization in Python?
0
votes
0
answers
28
views
How to upgrade ocamls package ocamlc-loc to 3.7.0 or higher to end conflict with ocaml-lsp-server?
Im trying to install ocaml-lsp-server but i get this error:
**input: opam install ocaml-lsp-server
output: [ocaml-lsp-server.1.14.2] no changes from git+https://github.com/ocaml/oc
aml-lsp.git
The ...
0
votes
1
answer
31
views
running many lines of ocaml code on vscode
I just started learning ocaml and I have a problem with my vscode.
I noticed that when i write too many lines of codes they won't be compiled, only some of them.
For example in the 1st picture, it ...
1
vote
3
answers
37
views
Fatal error: exception Invalid_argument("String.sub / Bytes.sub")
I am learning OCaml and working my way through the advent of code 2022.
But it seems that reading the input.txt file is causing some issue.
A X
B Y
C Z
I am getting this strange error, that comes ...
1
vote
2
answers
47
views
OCaml multiline string with ocamlformat produces ugly results
I have this sql query to build a table in Sqlite3, I'm using a multiline string which works fine.
let create_blog_table db =
let sql =
"
CREATE TABLE IF NOT EXISTS blog(
id ...
0
votes
1
answer
35
views
ocaml sqlite3 prepared statement
I have the following insert for sqlite3 in ocaml, which works ok.
type article = { author : string; content: string }
let insert_article db (article: article) =
let sql = Printf.sprintf "...
0
votes
1
answer
42
views
Why is dune refusing to download dependencies?
I expected dune to automatically download dependencies when running dune build, but that doesn't happen.
I've tried multiple variants of this, and I tried with ocaml versions 4.14 and 5.0, and with ...
0
votes
2
answers
62
views
Why does my recursion never terminate in OCaml?
let qrec=Queue.create ()
let rec queueaddrec1 n=
if n==1 then
Queue.add 1 qrec
else
Queue.add n qrec;
queueaddrec1 (n-1)
let ()=
queueaddrec1 5;
Queue.iter print_int qrec;
VScode told me ...
-1
votes
2
answers
41
views
How to add item in queue with a rec function in ocaml?
code:
let qrec = Queue.create ()
let q = Queue.create ()
let queueaddforto =
for i=1 to 5 do
Queue.add i q
done
let rec queueaddrec n =
if n > 0 then
Queue.add n qrec
else
...
0
votes
3
answers
72
views
How to get right result of this ocaml code?
code:
let a = ref 0
type t =
| Int of string
| Add of t * t
let rec makestring_t = function
| Int a -> a
| Add (b, c) ->
incr a;
"(Add" ^ string_of_int !a ^
&...
0
votes
1
answer
42
views
Cannot solve a type mismatching with GADT
I have following type definition of x with a GADT.
type x = X : 'a option -> x
and I'm trying to write a function to get the value of option accompanying with tag X
I first tried the following
let ...
-1
votes
1
answer
47
views
Union Function call in OCaml
module type SET =
sig
type 'a set
val emptyset : 'a set
val union : 'a set -> 'a set -> 'a set
end
module Set : SET =
struct
type 'a set = 'a -> bool
let emptyset : 'a set = ...
-1
votes
2
answers
80
views
Signature mismatch with Set implementation
module type SET =
sig
type 'a set
val member : 'a -> 'a set -> bool
val subset : 'a set -> 'a set -> bool
end;;
module MySet : SET = struct
type 'a set = 'a -> bool
let ...
2
votes
1
answer
46
views
Can I define a variant type as "some other variant plus a new constructor" without copy-paste spam?
If I have some variant type foo:
type foo =
| A
| B of string
and I want to create a new variant type bar that looks exactly like foo, but with a new constructor (arbitrarily e.g. C of float * ...
2
votes
1
answer
40
views
Troubleshooting Compilation Error with lwt_ppx in OCaml: Unable to Compile File
I'm encountering an issue while using lwt_ppx and the let%lwt syntax as described in the CS3110 textbook's section on promises (https://cs3110.github.io/textbook/chapters/ds/promises.html).
In the ...
3
votes
1
answer
48
views
Expose type constructor in module without duplication
So I have the following module:
module MyMod : sig
type t = A | B
val foo : t -> int
end = struct
type t = A | B
let bar x = x * x
let foo = function
| A -> bar 2
...
0
votes
1
answer
65
views
Static linking of library into a dynamically-linked binding with OCaml's dune
I have an OCaml library that has a C binding. The C binding has a dependency on a native component that I would like to also build with dune. At the moment, the dune file looks like:
(rule
(deps
(...
2
votes
1
answer
54
views
Reverse heterogeneous gadt list with recursively dependent elements
Having seen this answer about the elements of a List that depend on the type of preceding elements , where the problem was solved with the following list definition:
type ('a,'b) mlist =
| MNil : ('...
2
votes
1
answer
60
views
Recursive Lisp parser with Ocaml Opal
I'm trying to write a simple Lisp parser with OCaml Opal:
This is my AST:
type atom = Num of int | Ident of string [@@deriving show]
type sexp = Atom of atom | ListSexp of sexp list [@@deriving show]
...
0
votes
1
answer
71
views
How to extract the exact information of `GenArg`?
I'm writing a Coq formatter (code). So far, I got the AST of a Coq code and pretty-printed a few of its nodes. However, I cannot pretty-print VernacExtend nodes.
The CoqAst below represents the AST of ...
1
vote
1
answer
51
views
Why does Dune expose only `Foo__.Bar` module, and not `Foo.Bar`?
Here, utop are launched via dune utop.
The problem
Suppose I have initialized a project by dune init project foo, and created two files: lib/foo.ml and lib/bar.ml.
lib/foo.ml:
let x = 3
lib/bar.ml:
...
0
votes
1
answer
62
views
Unbound Module when using Dream eml pre-processor
I'm trying to learn OCaml by way of building a website with this lovely library I found called Dream (https://aantron.github.io/dream/), but I've run into an issue when trying to work with the ...
0
votes
1
answer
61
views
How to structure project for testing in dune
I've created an OCaml project using dune with the intent of running through these exercises:
dune init project ocaml_exercises
I've then created two lib files, last.ml and last_two.ml which contain ...
1
vote
1
answer
48
views
OCaml Module Naming
I'm trying to get started with OCaml, and I've created a new project with:
dune init project 01_calorie_counting
And that created a sensible enough looking project skeleton:
.
├── 01_calorie_counting....
1
vote
2
answers
110
views
How to generate the AST of a Coq source code?
I'm trying to write a Coq source code formatter, and I want the AST for a Coq source code.
Firstly I tried to generate ASTs from the official Coq library but gave it up because I couldn't know how.
...
2
votes
2
answers
67
views
How to transform exceptions into result in OCaml
I'm using this function
(** Wraps an exception in result *)
let try_ (f : 'a -> 'b) (x : 'a) : ('b, exn) result =
try
Ok (f x)
with
e -> Error e
I'm not very sure that this is safe,...
2
votes
1
answer
32
views
Including subpackages using dune (no implementation provided for modules, but modules are in dune file)
While compiling a project with dune that uses the fmt and logs packages , I ended up getting this weird error
File "_none_", line 1:
Error: No implementations provided for the ...
0
votes
0
answers
53
views
Using dune to copy files from one directory to another
I know about the copy_files stanza, but I'm stuck trying to copy files from one subdirectory to another (e.g. the root). Specifically, I'm trying to copy %{workspace_root}/static/ to _build/default/ ...
0
votes
1
answer
99
views
What does (depends ...) in dune-project do?
I wanted to specify the versions of my dependencies in my dune-project file explicitly, to avoid any breakages due to newer versions (in the same way I might list Java dependencies in a Gradle build ...
1
vote
1
answer
63
views
Depth first search: is immutabilty and speed mutualy exclusive?
The DFS I was taught in school goes something like this:
(* graph representation: ith element of the array is a list of successors of the node i *)
let graph_example = [|
[1; 2];
[3; 0; 2];
[0; ...
1
vote
1
answer
31
views
More verbose exceptions for out of bounds array accesses in OCaml [duplicate]
For out of bounds array accesses OCaml throws an exception like this one:
# [|1;2;3|].(12);;
Exception: Invalid_argument "index out of bounds".
Ok on the toplevel—but in compiled code an ...
0
votes
1
answer
71
views
Using monad transformers in OCaml
I'm getting myself familiar with monad transformers in OCaml using monads library.
Here is an example I'm working with:
open Base
open Stdio
open Monads.Std
module St = struct
include Monad.State....
0
votes
1
answer
72
views
How to serialize runtime Postgres queries in OCaml using Caqti?
I have a use case where I receive a query from the user as a string. execute the query, and return the output as json.
I'm using the Caqti library, which let me do type safe queries, the problem is ...
-1
votes
1
answer
38
views
Module Editor in OCaml [closed]
First, define the structure part of the module Editor. Specify an abstract data type
Editor.t of the edited text. Represent the edited text as a list of lines. Further, represent
a line by a list of ...
0
votes
1
answer
40
views
How can I see how dune is invoking the ocaml compiler?
I'd like to reproduce some of dune's functionality in buck2, but have no idea how to implement dune's functionality where a wrapped module can specify a library interface.
I'd like to see how dune is ...
2
votes
2
answers
109
views
Tail recursive remove duplicate consecutive entries in list
I'm attempting problem 8 of the 99 OCaml problems, which asks you to write a function compress that removes consecutive duplicate entires in a list:
assert (
compress
[ "a"; "a&...
2
votes
1
answer
54
views
Specifying ocamllex encoding
I'm currently developing a parser according to a specification, and I'm completely unable to find anywhere in the docs information about text encoding. It sounds weird to me that the docs of a lexing ...
0
votes
1
answer
65
views
Ocamlfind cannot find package 'zmq'
I was trying to compile Ocaml tools as part of the Quantum Package 2. The error prompts me
[2/3] Compiling OCaml tools
make: Entering directory '/home/parallels/qp2/ocaml'
ocamlbuild -j 0 -cflags &...
1
vote
1
answer
34
views
Flattening a Nested List in OCaml
I'm doing problem 7 of the OCaml exercises. It basically asks given a nested list as defined:
type 'a node =
| One of 'a
| Many of 'a node list
Write a function function flatten that flattens ...
-2
votes
1
answer
102
views
Is there a way to do 'interactive programming' in OCaml? [closed]
I found this post on Null Program titled 'Interactive Programming in C' and I was wondering if there was a way to do that in OCaml.
I tried googling my goal, but I cannot seem to find any information ...
2
votes
1
answer
61
views
Write barrier when swapping array elements
Consider the following code:
let swap_array_elements (arr:string array) (i:int) (j:int) =
let v_i = arr.(i)
and v_j = arr.(j) in
arr.(i) <- v_j;
arr.(j) <- v_i
In the AMD64 assembly ...
1
vote
1
answer
67
views
Opam switch not working: "error Buffer *OCaml* has no process"
I am on macOS 13.4, I set up my Ocaml environment with emacs but I'm still new at it so I got trouble solving some of my problems:
For the project I am working on I had to clone my working repository ...
0
votes
1
answer
74
views
Can't compile an OCaml program that uses sqlite3 module using dune
I wanted to test the Sqlite3 bindings in OCaml.
I have downloaded the appropriate package with opam, it works fine using utop.
I have tested it like this:
#use "topfind";;
#require "...
1
vote
1
answer
44
views
Can an OCaml generic function call a C function and pass in a generic value as a parameter?
I'm considering invoking a C function from an OCaml generic function and pass in a generic value as a parameter to the C function. Is it possible? If yes, how can the C function know the type of the ...
4
votes
0
answers
121
views
How can I compile OCaml code to run on Android?
I am trying to compile Unison, which is written in OCaml, for Android.
The only two OCaml toolchains for Android that I am aware of are opam-cross-android and opam-android-repository. However, they ...
2
votes
1
answer
62
views
Generating executable .ml test cases from a glob of plaintext files using dune
I'm in the process of writing a test suite for some lexing/parsing and it would be much cleaner if I could drop test input/output files in a directory and have dune generate OCaml test cases for each ...