ocaml

OCaml: How to Run a Script with Libraries Included

别等时光非礼了梦想. 提交于 2019-12-10 16:18:53
问题 I'm following the Real World OCaml book to learn OCaml and many of the programs require using the Jane Street Core library. When I use functions from this core library in the toplevel, it works great. There, I just use the following commands to open the Core library. $ #use "topfind";; #thread;; #camlp4o;; #require "core.top";; #require "core.syntax";; open Core.Std;; Then I can run this program line by line in the toplevel and functions like String.split work fine. # let languages = "OCaml

Tracking source position of AST nodes in a compiler (ocaml)

▼魔方 西西 提交于 2019-12-10 15:57:03
问题 I'm writing a compiler in ocaml, using ocamllex/yacc. Things are going well, but I've a design problem. For each AST node I create, it'd be good to have information about line/character position of that node in the source code. That would be useful for providing error messages to the user later. Now, I can add some kind of meta type to my nodes: type node = Node1 of ... * meta | Node2 of ... * meta but that seems redundant. Later, when I'm done with verifying the AST, I'll have to write match

Howto program thread-based parallel list iteration?

会有一股神秘感。 提交于 2019-12-10 15:53:08
问题 I need as an example how to program a parallel iter-function using ocaml-threads. My first idea was to have a function similiar to this: let procs = 4 ;; let rec _part part i lst = match lst with [] -> () | hd::tl -> let idx = i mod procs in (* Printf.printf "part idx=%i\n" idx; *) let accu = part.(idx) in part.(idx) <- (hd::accu); _part part (i+1) tl ;; Then a parallel iter could look like this (here as process-based variant): let iter f lst = let part = Array.create procs [] in _part part 0

Stackoverflow with specialized Hashtbl (via Hashtbl.make)

南楼画角 提交于 2019-12-10 15:39:03
问题 I am using this piece of code and a stackoverflow will be triggered, if I use Extlib's Hashtbl the error does not occur. Any hints to use specialized Hashtbl without stackoverflow? module ColorIdxHash = Hashtbl.Make( struct type t = Img_types.rgb_t let equal = (==) let hash = Hashtbl.hash end ) (* .. *) let (ctable: int ColorIdxHash.t) = ColorIdxHash.create 256 in for x = 0 to width -1 do for y = 0 to height -1 do let c = Img.get img x y in let rgb = Color.rgb_of_color c in if not

Picking which ocaml module to use with command line parameter

寵の児 提交于 2019-12-10 15:19:52
问题 In my code I've module M = Implementation1 and then I reference M , instead of Implementation1 . The problem is, I've to recompile my program to change Implementation1 to Implementation2 . I'd like to control which implementation to use from with a command line parameter. Is that possible? Is the situation simpler, when all implementations share signature? 回答1: Since both implementation are known statically you can use first class modules. There are quite a few different possibilities on how

First class Modules with parametric types (The type constructor F.f would escape its scope)

半世苍凉 提交于 2019-12-10 15:16:36
问题 I am currently playing around with modules, to see in which way they can be used in similar ways to Haskell type classes. Currently I am trying to play around with the functor type class: module type Functor = sig type 'a f val fmap : ('a -> 'b) -> ('a f -> 'b f) end module IdFunc = struct type 'a f = Id of 'a let fmap f = fun (Id a) -> Id (f a) let runId (Id a) = a end let outmap (module F : Functor) av = F.fmap f av However in this case outmap will not be typed correctly, the compiler

Ocaml - Accessing components in an array of records

ⅰ亾dé卋堺 提交于 2019-12-10 15:07:31
问题 I have a array of record type tt - originally with more components ;-) - and like to change its values within a for loop: type tt={mutable x: int};; let f0={x= -1};; let max=10;; let ff=Array.create max f0;; for i=0 to max-1 do ff.(i).x <- i;done;; Nevertheless, all fields of ff have the value 9 instead of having values from 0 to 9. Is ff.(i).x correct? I also tried for i=0 to max-1 do f0.x <- i;ff.(i) <- f0;done;; but with the same result... (I'm using OCaml version 4.00.1) What is wrong? I

OCaml and preprocessor have incompatible versions error when installing tcoq

一曲冷凌霜 提交于 2019-12-10 14:59:34
问题 I was trying to install tcoq and I had the following error: "/Users/pinocchio/.opam/4.05.0/bin/ocamlfind" ocamlc -rectypes -w -3-52-56 -c grammar/compat5.ml OCAMLC -c -pp grammar/gramCompat.mlp >> Fatal error: OCaml and preprocessor have incompatible versions Fatal error: exception Misc.Fatal_error make[1]: *** [grammar/gramCompat.cmo] Error 2 make: *** [submake] Error 2 does someone know: What the error means? How to fix it? I saw related post online: https://coq-club.inria.narkive.com

How was `val hash : 'a -> int` was implemented in OCaml?

泄露秘密 提交于 2019-12-10 14:49:02
问题 In OCaml, Hashtbl can hash any thing to an int Hashtbl.hash x associates a nonnegative integer to any value of any type. It is guaranteed that if x = y or Pervasives.compare x y = 0, then hash x = hash y. Moreover, hash always terminates, even on cyclic structures. I mean, in Java , we have hashCode() for every object which returns an integer and Java's Hashtable can hash that integer. But how did OCaml achieve that to hash anything? 回答1: It's not tricky. Hashtbl.hash just traverses data in a

How from Ocaml can I call C++ code using itself a shared library .so ?

北战南征 提交于 2019-12-10 14:44:38
问题 I need to build an Ocaml/C++ module which calls a shared object (.so under linux) As long as it is a question to compile a simple Ocaml/C++ stub, I manage the thing but when I need to link the .so with ocamlmklib or ocamlopt, it fails I work under gcc 4.5 (c++0x) files for the shared object : hello.hpp #include <iostream> #include <string> using namespace std; class HelloApplication { public : HelloApplication(); ~HelloApplication(); void say(string s); }; typedef HelloApplication *(*create