ocaml

Error: Cannot safely evaluate the definition of the recursively-defined module

坚强是说给别人听的谎言 提交于 2019-12-07 02:35:29
问题 I'm curious to understand why this error happens and which is the best way to get around it. I have a couple of files types.ml and types.mli which define a variant type value that can be of many different builtin OCaml types (float, int, list, map, set, etc..). Since I have to use the std-lib over this variant type I needed to concretize the Set module through the functor to be able to use sets of value type by defining the ValueSet module. The final .ml file is something like: module rec I :

Define recursive signatures for modules

梦想与她 提交于 2019-12-07 01:32:57
问题 I know that it is possible to define recursive modules, does anyone know how to define recursive signatures? For instance, I would like to realize: module type AAA = sig module Bbb : BBB type 'a t val f : 'a Bbb.t -> 'a t end module type BBB = sig module Aaa : AAA type 'a t val g : 'a Aaa.t -> 'a t end Could anyone help? 回答1: You can't, as far as I can tell. The closest solution is to limit the "recursive" bits to what is actually needed to express each signature separately: module type AA =

Verify that an OCaml function is tail-recursive

…衆ロ難τιáo~ 提交于 2019-12-07 01:15:41
问题 How can I tell if OCaml recognizes a particular function as tail-recursive? In particular, I want to find out if the OCaml compiler recognizes Short-circuited operators and tail recursion Thanks to Jeffrey's answer below, I tried this with the simple function let rec check_all l = match l with | [] -> true | hd :: tl -> hd && check_all tl and indeed, it does optimize to: camlTest__check_all_1008: .cfi_startproc .L102: cmpl $1, %eax je .L100 movl (%eax), %ebx cmpl $1, %ebx je .L101 movl 4(%eax

Collecting the output of an external command using OCaml

对着背影说爱祢 提交于 2019-12-06 19:19:31
问题 What is the right way to call an external command and collect its output in OCaml? In Python, I can do something like this: os.popen('cmd').read() How I can get all of an external program's output in OCaml? Or, better, OCaml with Lwt? Thanks. 回答1: You want Unix.open_process_in, which is described on page 388 of the OCaml system manual, version 3.10. 回答2: For Lwt, val pread : ?env:string array -> command -> string Lwt.t seems to be a good contender. Documentation here: http://ocsigen.org/docu

Cast float to int in OCaml

孤街醉人 提交于 2019-12-06 19:03:14
问题 How am I supposed to cast a float to an integer in OCaml? I know how to get a float from an int, but there doesn't seem to be an easy way to get an int from a float. 回答1: # int_of_float ;; - : float -> int = <fun> I assume you want a nearby int (this rounds towards zero, same as C does). If you want the IEEE representation, see Int64.bits_of_float . 回答2: you can simply truncate it, if the integer part of the float is what you want: printf "number\tint\tfloor\tceil\n"; List.iter (fun x ->

Why do some OCaml functions take () as a parameter?

时光毁灭记忆、已成空白 提交于 2019-12-06 19:03:04
问题 Example in Unix module: val environment : unit -> string array Why not just: val environment : string array ? 回答1: Because it denotes a function that takes a value of type unit as its parameter. The unit type is only inhabited by the value "()". This is usually used to mean that the function is going to perform some kind of IO or induce a side-effect, and needs no input. The second type signature you provided is the signature for a value, not a function that can be applied. If some expression

Impredicative polymorphism in F#

北城以北 提交于 2019-12-06 18:55:45
问题 OCaml's Hindley-Milner type system does not allow for impredicative polymorphism (à la System-F), except through a somewhat recent extension for record types. The same applies to F#. It however is sometimes desirable to translate programs written with impredicative polymorphism (e.g. Coq) into such languages. The solution for Coq's extractor to OCaml is to (sparingly) use Obj.magic , which is a kind of universal unsafe cast. This works because in OCaml's runtime system, all values have the

Functors with multiple arguments in OCaml

試著忘記壹切 提交于 2019-12-06 18:09:19
问题 I've the following situation: module type M = sig type s = ... end module Make(P: Something) : (M with type s = P.t) = struct type s = P.t ... end that works fine to generate modules of M type that use specific implementation of modules of type Something inside their implementation. Now suppose I have another module defined as module type AU = sig val feed : float -> unitv val nth : int -> (float -> float) val reset : unit -> unit end that has various implementations module SUAlg : AU =

What is the easiest way to add an element to the end of the list?

╄→гoц情女王★ 提交于 2019-12-06 18:05:53
问题 As :: : 'a -> 'a list -> 'a list is used to add an element to the begin of a list, Could anyone tell me if there is a function to add an element to the end of a list? If not, I guess List.rev (element::(List.rev list)) is the most straightforward way to do it? Thank you! 回答1: list@[element] should work. @ joins lists. 回答2: The reason there's not a standard function to do this is that appending at the end of a list is an anti-pattern (aka a "snoc list" or a Schlemiel the Painter algorithm).

Does `string` in OCaml support UTF-8?

女生的网名这么多〃 提交于 2019-12-06 17:56:44
问题 Does the type string in OCaml support utf8 ? Or what library I should use for utf8 string? 回答1: The string type of OCaml consists of a series of 8-bit bytes in essence. You can store a UTF-8 value in a string, and I have often done this. However, there's no built-in support for handling them. A good library for handling Unicode in OCaml (so I've heard) is Camomile. 回答2: There is also Uutf if you're looking for just unicode conversion. 来源: https://stackoverflow.com/questions/16152005/does