ocamlfind, where to find/how to build?

I know next to nothing about ocaml, but I want to install Coccinelle (on cygwin), which has a dependency to the binary ocamlfind. However, installing either ocaml-3.11.0-win-mgw.exe or ocaml-3.11.0-win-msvc.exe from http://caml.inria.fr/download.en.html, there are no such binary. In fact not a single file in the installed directory contains any reference to the string "ocamlfind". And downloading the source code (ocaml-3.12.1), grep only finds the following references: ./Changes:- PR#5165: o

load|use|require a library object file within an Ocaml source file

The Ocaml manual contains an exercise (here) in which library object files are loaded in the toplevel loop (the ocaml interactive interpreter) in the following way: #load "dynlink.cma";; #load "camlp4o.cma";; I'm trying to replicate the subsequent code in a compilable source file, and the code requires the above library object files. Can I load these files with a line of code within the source file and compile it with ocamlc? I've tried "#load", "load", "#use", "use", "#require", "require", a

Ocaml topfind lwt

I am trying to use lwt module with Ocaml %cat .ocamlinit #use "topfind";; #require "lwt.simple-top";; #require "lwt.syntax";; #require "findlib";; #require "num.core";; #load "nums.cma";; #load "unix.cma";; #camlp4o;; open Lwt;; open Lwt_io;; When I try to use any Lwt API using this command to build "ocamlbuild -use-ocamlfind myFile.native" I receive this error: "Error: Unbound module Lwt" When I type in terminal %ocaml it loads successfully and i can use Lwt.API's What is going on? any

ocaml sprintf clarification

I do not understand why this is the case: Printf.sprintf "%08s" "s" = Printf.sprintf "%8s" "s" - : bool = true In other words, I would expect: Printf.sprintf "%08s" "s" - : string = "0000000s" and not the actual result: - : string = " s" could someone please clarify why this is so?

Higher order types in OCaml (phantom types subtyping)

I'm using phantom types to emulate the state of a stack, as a wrapper module for ocaml-lua (Lua communicates with C/OCaml through a stack). Small code example: type 's t type empty type 's table type top val newstate : unit -> empty t (* stack empty *) val getglobal : empty t -> top table t (* stack: -1 => table *) Certain stack operations are possible on both tables and array-tables (no real arrays in Lua); some aren't. So if I have types type 's table type 's array

ocaml - deoptionalize a list: is there a simpler way?

I have written a function to deoptionalize an integer list and I would like to know if there is a better way to write it. let deoptionalize (lst:'a option list) : 'a list = List.map ~f:(fun x -> match x with Some x -> x | None -> assert false) (List.filter ~f:(fun x -> x <> None) lst) ;; In the assignment I am currently working its using map and filter is a must.

OCaml: retain value of variable with control statements

I'm very new to OCaml / functional programming, and I'm confused about the implementation of some things that are relatively simple other languages I know. I could use any and all help. Chiefly: in a program I'm working on, I either increment or decrement a variable based on a certain parameter. Here's something representative of what I have: let tot = ref 0 in for i = 0 to s do if test_num > 0 then tot := !tot + other_num else tot := !tot - other_num done;; This

Ocaml Combining the result of functors into a single module

Say I have 2 functors that look something like: module X (T : sig type t end) = struct type t = T.t ... end module Y (T : sig type t end) = struct type t = T.t ... end And I'd like to combine their result in a single module: module M = struct include X(struct type t = int end) include Y(struct type t = int end) end Of course this doesn't work because I'm defining t twice inside of M. I know one way would be not to specify t inside of X or Y but I can't do that since then I can

Eager side effect (printf) in ocaml

Newbie question: Say I have a function do_sth that is very slow, and it is applied to range 1 to n. I want to print the result of do_sth i when it is looping. How to do this? A naive attempt fails as the values will only be printed after the whole loop: let rec loop i = if i>1000 then 0 else let fi = do_sth i in ( Printf.printf "%d %d\n" i fi; fi + loop (i+1) ) let f_sum = loop 1

Ocaml Difference in program behavior with different make files

I have this OCaml program open Core.Std;; open Printf;; let all l = List.fold ~f:(&&) ~init:true l;; let any l = List.fold ~f:(||) ~init:false l;; let main () = let bools = [true; false; true; true; false; true] in printf "%b %b\n" (all bools) (any bools);; main();; And then two make files, the first is all: a.out @true a.out: fold.cmx ocamlfind ocamlopt -g -linkpkg -package core -package core_kernel -thread -w -10 fold.cmx fold.cmx: fold.ml fold.cmi ocamlfind oca

Types of functions returning functions in OCaml and how they are displayed

I am studying for a test, and one of the practice problems is to write a function of the type (int->int)->(int->int). The provided answer fun x y -> (x 1) + y But when I put something like let funct x y = (x 1) + y;; it spits out (int->int)->int->int. Is this any different from the version with the extra parentheses. If yes how, if no why not?

Are there any usage examples for the OCaml ppx_xml_conv module

I'm looking for a simple example for the ppx_xml_conv module from janestreet. I'm not terribly familiar with the (relatively) new ppx thing and can't really figure it out from the source code. Ultimately, I'm trying to write a client for an old SOAP service, and want to turn the xsd (from the wsdl) into a type and serializer/deserializer.

OCaml function name as string

I sometimes write functions that make the assumption that some arguments cannot occur. If they do, this is a bug and I fail: let foo = function | 0 -> () | _ -> failwith "foo: bad argument" If I rename the function later on, I have to remember to also change the string. Is there a way to do this in a more systematic manner? My mind wanders around solutions like | _ -> failwith (FUNCTION_NAME ^ ": bad argument") where FUNCTION_NAME is a string variable that the compiler or interp

How to get "opam init --comp=..." to install at the latest OCaml version?

If there is no version of OCaml already installed on the computer, running opam init will ask you to specify a version of OCaml to install by default. That's great. Is there a way to ask it to install the latest released version? It would make scripting the installation easier than having to look up what is the latest version each time.

Suppress a warning from OCaml in a specific line

I am currently building a project, that allows different types of functions, with a different amount of arguments (but all of them of the same type). To define those functions, I used this code for each of them: let {name} [a; b; {...}] = {...} My code around that ensures that the number of elements in the list is right, and I'd be fine with just a runtime error occurring if this isn't the case. But I am stuck with a warning, that I'd like to hide because I am aware of this pattern matching

Ocaml Type error in merge sort program

I am trying to write a simple merge sort code in OCaml, which also removed duplicates. The code that I have written is: let mergesort l r = match l with [] -> [] | [x]-> [x] | x::xs -> let rec split l3 = match l3 with [] -> ([], []) | [h] -> ([h], []) | h1::h2::t -> let (left, right) = split t in (h1::left, h2::right) in let rec merge l1 l2 r =

Ocaml How does this code for calculating mean and standard deviation work?

I don't quite understand the following code which calculates the mean and standard deviation of a list of numbers: let stats l = let rec helper rest n sum sum_squares = match rest with | [] -> let nf = float_of_int n in (sum /. nf, sqrt (sum_squares /. nf)) | h :: t -> helper t (n+1) (sum+.h) (sum_squares +. (h*.h)) in helper l 0 0.0 0.0;; For example: let (mean, sd) = stats [1.; 2.; 3.; 4.; 5.];; To which the interpreter responds val mean : float = 3. va

Creating debug builds of Ocaml code with Jbuilder

I have been reading the tutorials for the Ocaml language and for Jbuilder. The official tutorial indicates that one must compile Ocaml code using the '-g' flag with ocamlc in order to then run ocamldebug. I cannot find any mention of debug builds on the Jbuilder documentation. The only section that seems close is https://jbuilder.readthedocs.io/en/latest/jbuild.html#ocaml-flags. However, even if I add '-g' as a compilation flag.. (executable ((name [REDACTED]) (public_name [REDACTED]) (l

Diamond inheritance with OCaml modules

I have four modules. Ring, Field who is a submodule of Ring, Coefficient which is a submodule of Ring, Coefficientdivisible which is a submodule of Ring and Coefficicent. module type Ring = sig type t val add : t -> t -> t multiply : t -> t -> t val zero : t val one : t val opposite : t (* a+ opposite a =0*) end module type Field = sig include Ring val division : t -> t-> t end module type Coefficient = sig include Ring val stri

Ocaml How to deal with all those functors

I was here for something else but trying to write up my question, I realize there's no way I'm doing this right. I've been using mirage and irmin for a little while, and as long as all the code stays in the Main module everything is great. But of course, it quickly becomes a ridiculously huge file, and trying to split it in modules drives me mad with types escaping their scopes and whatnot. Instead of just passing console from start to some other functions, I have to put those other functions i

Ocaml syntax quite weird

I have this program in Ocaml that reads a line from keyboard and returns an int : let get_int () = print_string "Insert a number\n" ; let input = read_line() in let return__ = int_of_string( input ) ;; print_string "I'll print what you write : "; print_int ( get_int() ); print_string "\n" The problem is a syntax error on line 5, ";;" said the compiler. I know that already exist functions that do this but I'm doing this to learn. I read the official Oca

Ocaml LSP server example

I have a project in which I need to write an LSP server in OCaml for a developing language. So I did a lot of research on this but I'm having a bit of a hard time getting through it. The Microsoft tutorial https://microsoft.github.io/language-server-protocol/specification speaks sometimes on the server side, sometimes on the client side, sometimes JSON RPC through which requests and notifications must pass between the client and the server, and all of that mixed up, plus TypeScript and not OCaml

Ocaml Drop first n items in a list

I'm trying to make a function which drops the first n items of a list: let rec drop n h = if n == 0 then h else (drop n-1 (match h with a::b -> b));; This is giving: Characters 43-49: if n == 0 then h else (drop n-1 (match h with a::b -> b));; ^^^^^^ Error: This expression has type 'a -> 'b but is here used with type int What is wrong here? This is my first day in OCAML (with functional programming in general), i'm just following manuals and tutoria

ocaml hashtbl remove function

How come the Hashtbl remove restores the previous binding. Hashtbl.add t key1 Hashtbl.remove t key1 Hashtbl.remove t key1 => This should do anything but not restore the key1 ! Anyway, how come I can remove something making sure and if it was deleted before then proper flow shall be followed? val remove : ('a, 'b) t -> 'a -> unit Hashtbl.remove tbl x removes the current binding of x in tbl, restoring the previous binding if it exists. It does nothing if x is not bound in tbl.

tuple of tuple of tuple in ocaml

I have : type 'a box = | Item of ('a * 'a box) | Empty I need to create the function concat : val concat : 'a box box -> 'a box Concatenate a box of boxs. The elements of the argument are all concatenated together (in the same order) to give the result. I dont inderstand how to create a varible like 'a box box. I tried to do : let elt = Item(1, Item(2,Empty)) let elt2 = Item(elt, Item(20, Empty)), But its failed! do you know why?

Transpose of a int list list in Ocaml

I hope that this will be quite easy for some of you. I found this solution in this forum: let rec transpose list = match list with | [] -> [] | [] :: xss -> transpose xss | (x::xs) :: xss -> (x :: List.map List.hd xss) :: transpose (xs :: List.map List.tl xss) This is a transposition of a list of lists and it works great. Now I need the same this but instead of transposing an a' list list to a' list list to transpose int list list to int list list. Thank you al

Ocaml How to fully utilise lwt_pool?

I am struggling at the check and validate function for Lwt_pool.create and have some questions here. val create : int -> ?check:('a -> (bool -> unit) -> unit) -> ?validate:('a -> bool Lwt.t) -> (unit -> 'a Lwt.t) -> 'a t First of all, let me describe the background of my usage. I wish to use Lwt_pool to manage a pool of database connections. The database is MongoDB and the driver was made by myself (Mongo.ml). The driver is actually simple that it is jus

OCaml cons (::) operator?

In OCaml, is there a way to refer to the cons operator by itself? For example, I can use (+) and ( * ) as int -> int -> int functions, but I cannot use (::) as a 'a -> 'a list -> 'a list function, as the following example show: # (+) 3 5;; - : int = 8 # ( * ) 4 6;; - : int = 24 # (::) 1 [2;3;4];; Error: Syntax error: operator expected. Is there a way to produce a result like (::) other than with fun x y -> x::y? And does anyone know why (::) wasn't implemented in OCaml?

How to implement mixins in OCaml

How to implement mixins in the OCaml module system. The closest I achieved is to use the following methodology, which I illustrate on a MixinFormat which adds usual output functions print, output and to_string on a mixin able to format. Assume that we implement a module MixinFormat with the following signature: module MixinFormat : sig (** Formatable mixin. *) (** Input signature of the functor [MixinFormat.Make]. *) module type Basis = sig type t (** The type of formatted elements. *)

Ocaml How to omit "open ..."?

Suppose I have some file frobozz.ml whose first line is: open Foo I understand that if I get rid of this line1, frobozz.ml may no longer compile, unless I also replace some identifiers X in the rest of its content with their "fully qualified" form Foo.X. Is there an efficient/safe way (as opposed to compile-and-see, i.e. trial-and-error) to determine which identifiers X in frobozz.ml I would need to do this to (if I delete its open Foo line)? (Sorry for the stupid question: I'm very new wit

Ocaml need to create a function builiding function

I need to fill in an implementation for the function build that generates random expressions. The function is of type build : (int*int -> int)*int -> expr . The function is called with the argument pair (rand,depth) . The first element rand is a random number generator of type int * int -> int . Each call rand (i,j) returns a random integer between i inclusive and j exclusive. Use this function to randomly select operators when composing subexpressions to build up larger expressions. The secon

union of list in ocaml without using library

I have a assignment which requires writing a code to union list without using any library functions this is my code let rec union (l1:'a list) (l2:'a list) = let rec f x l = match l with | [] -> true | hd::tl -> if x = hd then false else f x tl in match l2 with | [] -> l1 | hd::tl -> if f hd l1 then union hd::l1 tl else union l1 tl but the compiler said that "This expression has type 'a but an expression was expected of type 'a list The

OCaml string length limitation when reading from stdin\file

As part of a Compiler Principles course I'm taking in my university, we're writing a compiler that's implemented in OCaml, which compiles Scheme code into CISC-like assembly (which is just C macros). the basic operation of the compiler is such: Read a *.scm file and convert it to an OCaml string. Parse the string and perform various analyses. Run a code generator on the AST output from the semantic analyzer, that outputs text into a *.c file. Compile that file with GCC and run it in the termin

OCaml native compiler for HP-UX IA64

Is there a way to build a stable OCaml native compiler for HP-UX IA64? I stumbled upon the comment below on https://news.ycombinator.com/item?id=6711348 We actively use/used OCaml in production on SPARC/Solaris, POWER/AIX, HP-UX/IA64. (The optimized compiler wouldn't build on AIX.) I googled about this but couldn't find any useful information. So now I'm trying to Build HP-UX IA64 OCaml bytecode interpreter (and ship my code with) Mix HP-UX build process with IA64 code generator for oth

Ocaml How to control the buffer of Scanf.Scanning?

The Scanf module appears to behave somewhat counterintuitively in that it doesn’t respect the state of the underlying channel: (* prepare test data *) let () = let oc = open_out "test.txt" in output_string oc "abcdefghij\n"; close_out oc ;; let ic = open_in "test.txt" (* ic at offset 0: “ab…” *) let () = let sc = Scanf.Scanning.from_channel ic in let s = Scanf.bscanf sc "%2s" (fun s -> s) in Printf.eprintf "read [%s]\n" s (* -> [ab] *) ;; (* sc out of scope at this point *)

Ignore a function in OCaml elegantly

I define a function f in utop of OCaml version 4.04. utop # let f = function x -> x + 1;; val f : int -> int = <fun> When I try to ignore f, I encounter a warning. utop # let a = ignore (f : int -> int); f 2;; Characters 15-19: Warning 5: this function application is partial, maybe some arguments are missing val a : int = 3 Warning 5 is triggered because the expression following ignore has a function interface int -> int.

Ocaml Circular dependency reasonml

Let's say that I have a 1 -> n relation: one todo can has many (or zero) notes and a note can have zero or one todo. How can I achieve this relation in ReasonML? (binding for an external lib) Here is what I have come with at the moment (which is of course not working) module Note = { module Attributes = { [@bs.deriving abstract] type t = { [@bs.optional] id: float, [@bs.optional] text: string, [@bs.optional] todo: Todo.Attributes.t, }; };

Ocaml Cstruct and Lwt_bytes

As far as I can tell, Lwt_bytes seems to use the same type as Cstruct (or probably uses cstruct itself), but some some reason I can't make the two of them work together : Lwt_io.write_from_exactly out b.Cstruct.buffer 0 16 Error: This expression has type Cstruct.buffer = (char, Bigarray.int8_unsigned_elt, Bigarray.c_layout) Bigarray.Array1.t but an expression was expected of type bytes Isn't bytes that exact same type ? How can I make that work ? I'm tr

Ocaml Split list with map

How can I replicate the function of the List module: "List.split", with map? I've found a way to do it with fold_right, but I don't quite understand how to do it with map... let split l = List.fold_right(fun (x, y) (xs, ys) -> (x :: xs, y :: ys)) l ([], []);;

Ocaml calling recursive function when passing a functon for higher order function

I want to recursively call higher order function. so I have a list and I'm passing this list to a function name funcName. but I need to pass a function. and I want to put a logic there. i want to check if the element is eric. how do I recursive function for fun i-> syntax? let aaa c : bool = let rec helper c = match c with |element(i) -> funcName (fun i->if (List.hd i)=eric then true else //now i want to recursively call List.tl insi

Ocaml Graphics event

I made this function here to get an event from keyboard : let get_move() : string = let e = Graphics.wait_next_event [Key_pressed] in let ke = if e.keypressed then Printf.sprintf "%c" e.key else "" in ke;; I am making a tetris, so I would like that the bloc can go down while this function is waiting an event. Currently, when it wait an event, it "pause" the loop. What should I do ?

OCaml assertion fail

Hey everyone I'm new to OCaml and having some problems. So I'm trying to do a recursive function that takes a non-empty list and returns the last element. My list can be any data type though. Here is what I have right now and it's currently failing on the assertion statement saying "This expression has type int but an expression was expected of type int list. let rec last l = match l with [] -> [] |[x] -> [x] |_::t -> last t ;; assert(last [1; 2; 3; 4] = 4);; assert(last [&quo

odoc (documentation generator for OCaml) remove 'Stdlib' prefix

I'm using odoc, along with dune to generate documentation for a library. This is made by doing: dune build @doc However, this generates signatures like: val print : Stdlib.Format.formatter -> t -> unit While what i would like is: val print : Format.formatter -> t -> unit OCamldoc used to provide the -hide option which could do exactly this. Does odoc have something similar? If yes, how to use it from dune? Optionnaly, it would be perfect to have a link to the Format module of the S

Ocaml Problem at building conf-pkg-config 2 when installing tsdl with opam on windows 64

I get this error when installing tsdl for my caml project: #=== ERROR while compiling conf-pkg-config.2 ==================================# # context 2.0.8 | win32/x86_64 | ocaml-variants.4.11.1+mingw64c | git+https: //github.com/fdopen/opam-repository-mingw.git#opam2 # path ~/.opam/4.11.1+mingw64c/.opam-switch/build/conf-pkg-config.2 # command dash.exe -ec pkg-config --help # exit-code 127 # env-file ~/.opam/log/conf-pkg-config-1212-9b8556.env # output-file ~/.opam/log/conf

Removing the eventual trailing '\n' in a string in OCaml

I need to create a function that receives a string and checks if the last char is a "\n" or not. If so, returns the same string without it's last char. The ways I can think of doing this are not the slightest efficient. I need it to be efficient.

Ocaml Why does camlp4o fail to parse (or) as a binary function?

In vanilla OCaml, (or) is a binary function just like (+) and all the others, so code like this works fine: let any (truths:bool list) = List.fold_left (or) false truths But in any environment where camlp4o is loaded, this fails to parse with: Error: Parse error: ")" or "module" or [expr] expected after "(" (in [expr]) Meanwhile, (+) and the other integer arithmetic operators continue to work as expected in either environment: let sum (nums:int list) = List.fold_left (+) 0 nums Why is t

  1    2   3   4   5   6  ... 下一页 最后一页 共 15 页