ocaml

Why does the OCaml std lib have so many non-tail-recursive functions?

天涯浪子 提交于 2019-12-04 02:22:06
I have been rewriting many OCaml standard library functions to be tail-recursive lately. Given that this has entailed straight-forward CPS transformation, I am left puzzling over why the default versions are not written this way. As an example, in the standard library, map is defined as: let rec map f = function [] -> [] | a::l -> let r = f a in r :: map f l I have rewritten it to be: let map f l = let rec aux l k = match l with [] -> k [] | a::l -> aux l (fun rest -> k (f a :: rest)) in aux l (fun x -> x) In my experience, tail recursive versions of non-trivial functions often trade space

Is INRIA going to add concurrency primitives to OCaml?

被刻印的时光 ゝ 提交于 2019-12-04 01:13:23
By "concurrency" I mean lightweight processes like Erlang's actors and concurrent GC aimed to make such a processes work smoothly. It would be very cool if INRIA got rid of those drawbacks of the current OCaml implementation to make OCaml more prepared for the multicore future. P.S. F# isn't what I'm looking for. no I cannot be more concise without reproducing his explanation. It speaks for itself. Yes, this is from 2002, but I haven't heard him sway on the issue, and from the text, it doesn't seem probable at all that he would back down from these goals. For current developments on concurrent

OCaml : why comparison operator are type agnostic, whereas arithmetic ones are not?

人盡茶涼 提交于 2019-12-04 00:59:41
问题 I am wondering why < operator supports int, string, bool, or float, whereas + only support int. Could OCaml recognize the underlying arithmetic to use ? What makes comparison operator different from arithmetic ones ? Is it the same for other FP language ? 回答1: Comparison operators are special in OCaml. They are able to compare records, variant types, lists and other data structures recursively. They by-pass a type system (hope I formulated it correct), and have a special support in runtime. I

Running time in Ocaml

时光毁灭记忆、已成空白 提交于 2019-12-04 00:40:42
问题 How to figure out the amount of time my code has taken in ocaml? are there any functions to measure that? 回答1: If you want to measure execution time of individual functions, this utility function is helpful in many cases: let time f x = let t = Sys.time() in let fx = f x in Printf.printf "Execution time: %fs\n" (Sys.time() -. t); fx where f is any function which takes x as the argument and returns something. 回答2: In my own coding, I use Unix.gettimeofday () , which returns a float value with

__memcpy_sse2_unaligned - what does this mean in detail?

风格不统一 提交于 2019-12-04 00:36:57
While working on my compiler I got this error: Program received signal SIGSEGV, Segmentation fault. __memcpy_sse2_unaligned () at ../sysdeps/x86_64/multiarch/memcpy-sse2-unaligned.S:33 How do I get details of what went wrong here? I know from the backtrace it's a memcpy line that causes it, but how do I see how the memory is aligned? And how do I know how it should be aligned? The project is a compiler with an LLVM back-end using the Zend/PHP runtime with the OCaml garbage collector, so there's is a lot of things that can go wrong. I suspect this line being part of the problem: zend_string

Inconsistent assumptions over interface (Ocaml)

寵の児 提交于 2019-12-04 00:36:41
I suddenly started to get this error. I don't know how to diagnose or fix it. Am I supposed to grep through bar.ml and check every Big_int function against signature in Big_int.mli ? File "foo.ml", line 1, characters 0-1: Error: The files /home/bar.cmi and /usr/lib/ocaml/big_int.cmi make inconsistent assumptions over interface Big_int Generally this message means that one .mli file has been recompiled recently but not the other. Since one of the .mli files is in your OCaml library, it might mean that you've upgraded your OCaml installation but haven't recompiled bar.mli since then. What

Ocaml utop library paths, Core module

馋奶兔 提交于 2019-12-04 00:26:34
I am attempting to use the Core module in utop , as originated by Jane Street and installed using opam . Here's the problem utop # open Core.Std;; Error: Unbound module Core utop does not seem to have the path to the Core module. How do you specify a path that can be found by utop to access the Core module? Is there a utop init file that specifies library paths ? I have the same error message from the OCaml 4.01.0 interpreter. The only way I can avoid this error is actually changing directory to /Users/myname/.opam/system/lib/core . I had the same problem, the directions here got it working

Is it possible to use pipes in OCaml?

萝らか妹 提交于 2019-12-04 00:02:25
In F# I can't live without pipes ( <| and |> ) let console(dashboard : Dashboard ref) = let rec eat (command : string) = command.Split(' ','(',')') |> Seq.filter(fun s -> s.Length <> 0) |> fun C -> (Seq.head C).ToUpper() |> fun head -> Can I use <| and |> in OCaml? LiKao These are available since OCaml 4.01. However, <| is named @@ there, so it has the correct operator associativity. Alternatively, you can either define them yourself: let (|>) v f = f v let (<|) f v = f v (* or: *) let (@@) f v = f v Or you use Ocaml batteries included , which has the |> and <| operators defined in BatStd. 来源:

What is the meaning of Warning 40: this record … contains fields that are not visible in the current scope

回眸只為那壹抹淺笑 提交于 2019-12-03 23:56:05
Please consider the following code: module A = struct type r = { i : int; s: string } end module B = struct type r = { i : int; s : string } end let f (x : A.r) : B.r = match x with { i; s } -> { i = 2*i; s = "" } Two modules define exactly the same record. A function f converts an A record to a B record. The warning is already emitted during compilation, but also visible interactively. On the ocaml cli, it seems that a call to f does the intended thing: # let x = f { i = 5; s = "ab" };; Characters 10-29: let x = f { i = 5; s = "ab" };; ^^^^^^^^^^^^^^^^^^^ Warning 40: this record of type

Tools for profiling OCaml code

╄→гoц情女王★ 提交于 2019-12-03 23:20:45
Is anybody aware of programs for profiling OCaml code apart from using the -p option while compilation and then using gprof? I am asking this question in order to check if the sampling time of 0.01 second can be lowered further? poorman's profiler is perfectly applicable for OCaml programs. The same idea works out for profiling allocations as well. Never used it but ocamlviz is another option. You can also use ocaml-memprof, a compiler patch ( 3.12.0 and 3.12 1 ) written by Fabrice Le Fessant, that adds memory profiling features to ocaml programs. EDIT Now you have ocp-memprof , an OCaml