ocaml

“Error: unbound module” in OCaml

陌路散爱 提交于 2019-12-04 18:45:10
问题 Here's a simple example of using the library Cohttp: open Lwt open Cohttp open Cohttp_lwt_unix let body = Client.get (Uri.of_string "http://www.reddit.com/") >>= fun (resp, body) -> let code = resp |> Response.status |> Code.code_of_status in Printf.printf "Response code: %d\n" code; Printf.printf "Headers: %s\n" (resp |> Response.headers |> Header.to_string); body |> Cohttp_lwt.Body.to_string >|= fun body -> Printf.printf "Body of length: %d\n" (String.length body); body let () = let body =

OCaml - What is an unsound type?

∥☆過路亽.° 提交于 2019-12-04 18:44:59
问题 Recently I was given the code List.fold_left (fun acc x -> raise x ; acc) 3 I'm completely fine with this partial application having a functional value of type exn list -> int , and the fact it yields a warning isn't surprising. I am, however, not certain what half of the warning means: Warning 21: this statement never returns (or has an unsound type.) I can't actually find any reference to this warning where it isn't the result of a non-returning statement. Even the man page for ocamlc only

Explicit polymorphic type in record

谁都会走 提交于 2019-12-04 18:41:38
In OCaml, it is possible to define explicit polymorphic type in a record type foo = { f : 'a. unit -> 'a };; It seems we can assign only general values to f like { f = fun () -> failwith ""; } or { f = fun () -> exit 1; } How to use this language feature in real world? Is there any good practical example? This isn't really connected with records. If you declare any function to have type 'a. unit -> 'a (takes nothing and returns whatever the caller wanted) then you can only use it for functions that don't return. Here's a slightly more useful example: a record containing a function for finding

ocaml pretty printer (code formatter)

拥有回忆 提交于 2019-12-04 17:30:20
问题 I am looking for a code formatter or pretty printer for ocaml. Something like gofmt for the go programming language. It should preferably preserve comments. I am correcting hand-ins and some of the code is formatted in a way that makes it very hard to read. 回答1: if you don't care about comments, you can use camlp4: camlp4 <file> -parser o -printer o > <new-file> Or you can use external indenter tools, as ocp-indent. 来源: https://stackoverflow.com/questions/12798723/ocaml-pretty-printer-code

How to use -thread compiler flag with ocamlbuild?

南楼画角 提交于 2019-12-04 15:48:54
问题 I am using Jane Street 's async_core by adding package(async_core) in _tags . When I use ocamlbuild -use-ocamlfind -I src test/test_airport.native , it gives me the following error: camlfind ocamlopt -linkpkg -package async_core -package unix -package netclient -package mongo -package xml-light src/airport.cmx test/test_airport.cmx -o test/test_airport.native ocamlfind: Error from package `threads': Missing -thread or -vmthread switch I googled it and here is what I got http://caml.inria.fr

{X with value} in ocaml

本秂侑毒 提交于 2019-12-04 15:15:11
问题 I saw the following function call in Yacfe example: Visitor_c.vk_program { Visitor_c.default_visitor_c with Visitor_c.kexpr = (fun (k, bigf) exp -> match Ast_c.unwrap_expr exp with | Binary(e1, Logical (Eq), (((Constant(Int("0")) as _e2),_t),ii)) -> (match Ast_c.get_onlytype_expr e1 with | Some (qu, (Pointer _,_ii)) -> let idzero = Common.tuple_of_list1 ii in idzero.cocci_tag := Ast_cocci.MINUS (Ast_cocci.NoPos, [[null_addon]]), []; | _ -> k exp ) | _ -> k exp ); } ast; I can see a function

Is it possible to make an opam “sandbox”?

南笙酒味 提交于 2019-12-04 14:49:52
I have two ocaml projects being compiled with ocaml 4.02.1. Is there a way to create separate opam installations for each project instead of having both projects install their dependencies in the global 4.02.1 opam switch? ivg In opam you can have several installations of the same compiler: opam switch -A 4.02.1 proj1 opam switch -A 4.02.1 proj2 will create two separate independent stacks for each project. You may also find these commands useful: opam switch export opam switch import 来源: https://stackoverflow.com/questions/27640897/is-it-possible-to-make-an-opam-sandbox

Interrupt a call in OCaml

大兔子大兔子 提交于 2019-12-04 14:11:02
I'd like to interrupt a call if it takes too long to compute, like this try do_something () with Too_long -> something_else () Is it possible to do something like that in OCaml? The function do_something may not be modified. In general the only way to interrupt a function is to use a signal, as Basile suggested. Unfortunately the control flow will be transferred to a signal handler, so that you will be unable to return a value that you like. To get a more fine-grained control, you can run you do_something in separate thread. A first approximation would be the following function: exception

Is it possible to define an exception inside a function

谁说胖子不能爱 提交于 2019-12-04 13:10:33
One way to implement "early returns" in OCaml is via exceptions: exception Exit let myfunc () = try for i = 0 to .... do if .. then raise Exit done; false with Exit -> true However, is there a way to declare this Exit exception in the body of the function, so its name is not visible to other functions in the module? (* I would like to do this, but it gives a syntax error *) let myfunc () = exception Exit try for i = 0 to .... do if .. then raise Exit done; false with Exit -> true Yes, what you want is possible by using a local module: let myfunc () = let module M = struct exception Exit end in

OCaml attributes

你离开我真会死。 提交于 2019-12-04 12:10:44
I was looking at the manual and found that there are attributes in OCaml for declaring things as deprecated (see http://caml.inria.fr/pub/docs/manual-ocaml/extn.html ), but I can not figure out how to get them to be recognized by the compiler. Here's the program that I wrote: let x = 1 [@@ocaml.deprecated "don't use this"] type t = X | Y [@@ocaml.deprecated "don't use this"] let _ = let y = Y in match y with | X -> print_string (string_of_int x) | Y -> assert false (I also tried [@@deprecated ...] rather than [@@ocaml.deprecated ...] with the same results). I don't get any warnings when I run: