ocaml

Printing variant types in OCaml

给你一囗甜甜゛ 提交于 2019-12-23 19:10:31
问题 In my OCaml program, I spend considerable time wring "to_string" for variant types over and over again. Either I need them for debugging purpose, or because I need a specific formatted output. So far, they follow a template such as follows: let rec to_string = function | Var x -> x | Implies (f1, f2) -> Printf.sprintf "(=> %s %s)" (to_string f) (to_string f2) | And (f1, f2) -> Printf.sprintf "(& %s %s)" (to_string f1) (to_string f2) | Or (f1, f2) -> Printf.sprintf "(| %s %s)" (to_string f1)

Records with similar fields in OCaml

为君一笑 提交于 2019-12-23 17:23:44
问题 In this answer, the suggested way of "attaching" meta information to types was using a record: type _foo = ... and foo = {n:_foo; m:meta} but what if I have multiple types I'd like to wrap with meta information? Apparently all field names in record types must have different names, and writing: type _foo = ... and foo = {n:_foo; m:meta} ... type _fooX = ... and fooX = {nX:_fooX; mX:meta} seems redundant :/. Classes are the only way to solve this? I'd like to avoid dealing with classes, if

Is it possible to call into C functions from OCaml, without wrapped types?

扶醉桌前 提交于 2019-12-23 16:26:41
问题 I'm trying to write a (desperately tiny, simple — I have no idea what I'm doing, let's be real!) compiler in OCaml. I'd really like to avoid checking any ISO-C code into the project (despite knowing C fairly well; the goal here is to learn and use OCaml exclusively.) Pursuant to this, I need to write a “runtime” for the compiled language in OCaml, compile it separately from the primary project, and then link it against the compiler-itself's output. Unfortunately, it looks like any external

Does OCaml have the ability to pass by reference?

喜欢而已 提交于 2019-12-23 13:53:23
问题 In C++ a program can pass a reference, not value to a function. void incrementInt(int &x) { ++x; } Does OCaml offer this same functionality? 回答1: No, there is no strict equivalent. There are ref s, which are like pointers to new-allocated memory, and there are records, arrays, objects and values of other compound data types, that are passed "by object reference", which again means they act like pointer to new-allocated memory. However there's no equivalent to a pointer to a variable or a C++

Avoid the warning “Warning 21: this statement never returns (or has an unsound type.)”

老子叫甜甜 提交于 2019-12-23 13:21:03
问题 This is the first time that I met the following warning "Warning 21: this statement never returns (or has an unsound type.)" and I don't have an idea how to fix it. let display_committers_stats response = match response##readyState with | XmlHttpRequest.DONE -> begin match response##status with | 200 -> Js_client_ui.create_menu_tabs "GitSearchTabs"; let l = Json_parser.get_commits (Js.to_string response##responseText) in let values = Json_parser.group_commits_by_user l |> List.map (fun

OCamlbuild fails to compile complaining implementation of Netsys is not provided when it is

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-23 13:02:40
问题 I'm trying to compile an XML-RPC server written using the xmlrpc-light library in OCaml with ocamlbuild but I get: $ ocamlbuild server.native Finished, 0 targets (0 cached) in 00:00:00. + ocamlfind ocamlopt -linkpkg -package xmlrpc-light -package unix -package threads -package netsys -thread server.cmx -o server.native File "_none_", line 1, characters 0-1: Error: No implementations provided for the following modules: Netsys referenced from /usr/lib/ocaml/equeue/equeue.cmxa(Uq_engines)

How to traverse typed abstract syntax tree in OCaml compiler

本小妞迷上赌 提交于 2019-12-23 12:33:07
问题 I'm trying to dump type information of all identifiers in an OCaml project, basically it's the same as traversing the typed abstract syntax tree(https://github.com/ocaml/ocaml/blob/trunk/typing/typedtree.mli). Since I'm new to OCaml compiler's codebase, I'm not sure whether the Compiler has provided apis so we could easily write a plugin to do the job or we have to hack the compiler code? Also how does this interact with OCamlbuild? Thanks for any hints or advices. 回答1: Assuming you have

Abbreviating constructor names in Ocaml

喜你入骨 提交于 2019-12-23 11:48:31
问题 I have two modules. One defines a variant type: module A = struct type foo = Bar of material | Baz | Boo (* other stuff *) end and I would like to be able to use foo 's variants both as constructors and as left-hand-sides in another module module B = struct type foo = A.foo (* I can abbreviate A.foo by assigning it a local alias *) let f (x : foo) = match x with | Bar m -> Bar (g m) (* Any way to abbreviate Bar and friends? *) | Baz | Boo -> x end but per "referring to named objects" I have

OCaml mod function returns different result compared with %

*爱你&永不变心* 提交于 2019-12-23 10:15:30
问题 The modulo function in OCaml mod return results different when compared with the modulo operator in python . OCaml: # -1 mod 4 - : int = -1 Python: >>> -1 % 4 3 Why are the result different?. Is there any standard module function that operate as % in OCaml?. 回答1: Python is a bit different in its usage of the % operator, which really computes the modulo of two values, whereas other programming languages compute the remainder with the same operator. For example, the distinction is clear in

Compiling and running in OCaml

北慕城南 提交于 2019-12-23 10:09:42
问题 I'm new to OCaml and I would like to know how can I write an ocaml code into a file and then compile it to run it whenever I want. Now I'm using OCaml by typing ocaml in the mac or linux terminal and writing the code, but when I'm done and I close the software I lose all the work. 回答1: There're plenty of options, but the easiest one (to my opinion) is to use ocamlbuild. If you have your code in file program.ml , then ocamlbuild program.native will compile your program into a native binary, so