ocaml

Interrupt a call in OCaml

半城伤御伤魂 提交于 2019-12-06 08:46:48
问题 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. 回答1: 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

Is it possible to make an opam “sandbox”?

喜欢而已 提交于 2019-12-06 08:25:20
问题 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? 回答1: 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 来源:

OCaml warning 31, compiler-libs, and ppx

不打扰是莪最后的温柔 提交于 2019-12-06 07:18:58
I'm porting my application from OCaml 4.02.3 to 4.03.0. Say you have the following in lexer.ml : type t = T [@@deriving sexp] let () = sexp_of_t |> ignore; print_endline "hai" I'm trying to run it as following: ocamlbuild -use-ocamlfind -pkg ppx_sexp_conv -cflags '-w @a-4-31' lexer.byte -- But I'm getting the following error: Warning 31: files lexer.cmo and /Users/vladimir/.opam/4.03.0+flambda/lib/ocaml/compiler-libs/ocamlcommon.cma(Lexer) both define a module named Lexer File "_none_", line 1: Error: Some fatal warnings were triggered (1 occurrences) I understand that compiler-libs has a

OCaml attributes

别说谁变了你拦得住时间么 提交于 2019-12-06 06:48:27
问题 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

OCaml - Cannot find graphics.cma

放肆的年华 提交于 2019-12-06 06:40:29
When loading the Graphics module in the toplevel, I get an error saying "Cannot find graphics.cma". I'm using OS X and I'm pretty sure I've installed OCaml correctly since I've been using it for about a month now. So it seems that the Graphics module wasn't included in the OCaml package. How can I fix this issue, or how can I install the Graphics module myself? First of all, check Graphics is really installed. This library is optional and therefore it may not be installed. There are several ways to check but the following should work for any situation: $ ls `ocamlc -where`/graphics* If there

Ocaml polymorphic records type is less general

浪尽此生 提交于 2019-12-06 06:18:53
Given the following type: type ('props,'state) reactInstance = { props: 'props; state: 'state; updater: 'event . (('props,'state) reactInstance -> 'event -> 'state) -> ('props,'state) reactInstance -> 'event -> unit;} I'm trying to achieve: let rec updater f instance event = let nextState = f instance event in let newInstance = { props; state = nextState; updater } in () let newInstance = { props; state = (reactClass.getInitialState ()); updater } I gave the updater a forall-like type definition. My main motivation is because the updater will get invoked with an event. No idea what that event

OCaml recursive modules across compilation units

给你一囗甜甜゛ 提交于 2019-12-06 05:45:57
I'm trying to split the following recursive modules into separate compilation units. Specifically, I'd like B to be in its own b.ml, to be able to reuse it with other A's. module type AT = sig type b type t = Foo of b | Bar val f : t -> b list end module type BT = sig type a type t = { aaa: a list; bo: t option } val g : t -> t list end module rec A : (AT with type b = B.t) = struct type b = B.t type t = Foo of b | Bar let f = function Foo b -> [ b ] | Bar -> [] end and B : (BT with type a = A.t) = struct type a = A.t type t = { aaa: a list; bo: t option } let g b = let ss = List.flatten (List

OCaml Optional Argument

ε祈祈猫儿з 提交于 2019-12-06 05:44:44
问题 How can I write a function in OCaml in which one or more argument are optional? let foo x y z = if(x+y > z) then true else false;; If foo do not receive the z argument it uses 0 as z . foo 3 3 2 -> true foo 3 3 10 -> false foo 2 1 -> true Is there any OCaml feature to achieve this? 回答1: OCaml doesn't have optional arguments like you'd find in Java or C#. Since functions can be partially applied, optional arguments can make it hard to tell when you're done passing arguments and would like the

SystemT Compiler and dealing with Infinite Types in Haskell

会有一股神秘感。 提交于 2019-12-06 02:42:41
I'm following this blog post: http://semantic-domain.blogspot.com/2012/12/total-functional-programming-in-partial.html It shows a small OCaml compiler program for System T (a simple total functional language) . The entire pipeline takes OCaml syntax (via Camlp4 metaprogramming) transforms them to OCaml AST that is translated to SystemT Lambda Calculus (see: module Term ) and then finally SystemT Combinator Calculus (see: module Goedel ). The final step is also wrapped with OCaml metaprogramming Ast.expr type. I'm attempting to translate it to Haskell without the use of Template Haskell. For

OCaml function with variable number of arguments

本秂侑毒 提交于 2019-12-06 02:13:59
问题 I'm exploring "advanced" uses of OCaml functions and I'm wondering how I can write a function with variable number of arguments. For example, a function like: let sum x1,x2,x3,.....,xn = x1+x2,+x3....+xn 回答1: With a bit of type hackery, sure: let sum f = f 0 let arg x acc g = g (acc + x) let z a = a And the (ab)usage: # sum z;; - : int = 0 # sum (arg 1) z;; - : int = 1 # sum (arg 1) (arg 2) (arg 3) z;; - : int = 6 Neat, huh? But don't use this - it's a hack. For an explanation, see this page