ocaml

How to show the types of each declaraion of a ocaml program?

冷暖自知 提交于 2019-12-11 19:23:21
问题 I like to use ocaml in a terminal to get interactive result or type from each ocaml command, for instance: Objective Caml version 3.11.2 # let a = 5;; val a : int = 5 But when there are many commands, it is normal that we put all in a file like test.ml , then compile it. At the moment, I use ocamlc -o test test.ml . But when I do test in a terminal, I could not see the types of each declaration, which is a pity. Does anyone know how to show that? Thank you very much. 回答1: In the toplevel you

Make a table containing tokens visible for both .mly an .mll

纵然是瞬间 提交于 2019-12-11 19:05:57
问题 I would like to define a keyword_table which maps some strings to some tokens, and I would like to make this table visible for both parser.mly and lexer.mll . It seems that the table has to be defined in parser.mly , %{ open Utility (* where hash_table is defined to make a table from a list *) let keyword_table = hash_table [ "Call", CALL; "Case", CASE; "Close", CLOSE; "Const", CONST; "Declare", DECLARE; "DefBool", DEFBOOL; "DefByte", DEFBYTE ] %} However, I could NOT use it in lexer.mll ,

Syntax error in function to shift array by k

霸气de小男生 提交于 2019-12-11 17:05:33
问题 I'm trying to shift array by k to left. Here's my code. But I'm getting compile error on shifted;; line. let shift_left (arr: array) (kk: int) = let size = Array.length arr in let k = kk mod size in let shifted = Array.make size 0 in for i = 0 to size - 1 do if i < k then (shifted.(size - k + i) <- arr.(i)) else (shifted.(i-k) <- arr.(i)) done shifted;; let arr = [| 1; 2; 3; 4; 5; 6; 7; 8; 9; 10 |];; let shifted = shift arr 4;; Array.iter print_int arr; print_string "\n";; Array.iter print

Ocaml - wrong type

我与影子孤独终老i 提交于 2019-12-11 16:58:09
问题 I want check if tree is balanced (It means that each leaf is on the same depth) but I have problem with wrong type. type 'a tree = Node of 'a * 'a tree list;; let rec fold_tree f (Node (x,l)) = f x (map (fold_tree f) l);; let is_balanced t = fst( fold_tree (fun _ (l: (bool * int) list ) -> ((fold_left (fun h (flag,first_value)-> ((fst h)=first_value)&&flag,first_value) (true,snd(hd l) ) l)) ) t);; The problem is there: ((fold_left(fun h (flag,first_value)-> ((fst h)=first_value)&&flag,first

Function bigAdd (addition of two int lists)

痴心易碎 提交于 2019-12-11 16:51:18
问题 I need help with the bigAdd, like what exactly I should put for the f base and arg. Big add is supposed take in 2 int arrays and output the sum into another int array like # bigAdd [9;9] [1;0;0;2];; - : int list = [1;1;0;1] # bigAdd [9;9;9;9] [9;9;9];; - : int list = [1;0;9;9;8] I have so far let rec padZero l1 l2 = if List.length l1 > List.length l2 then (padZero l1 ([0]@l2)) else if List.length l2 > List.length l1 then (padZero ([0]@l1) l2) else (l1, l2) let rec removeZero l = match l with

overgeneralized curried fns

强颜欢笑 提交于 2019-12-11 15:56:08
问题 module MapHelpers (Ord : Map.OrderedType) = struct include Map.Make (Ord) let add_all a b = fold add a b end works but the seemingly equivalent module MapHelpers (Ord : Map.OrderedType) = struct include Map.Make (Ord) let add_all = fold add end fails to compile with File "Foo.ml", line 2, characters 18-104: Error: The type of this module, functor (Ord : Map.OrderedType) -> sig ... val add_all : '_a t -> '_a t -> '_a t end, contains type variables that cannot be generalized Command exited with

ocaml toplevel throws error unbounded module

房东的猫 提交于 2019-12-11 14:37:52
问题 I installed OCAML and OPAM, then I installed libraries such as ocaml-http using OPAM. When I tried to open module Http_types, ocaml toplevel is throwing error unbounded module. I tried to set CAML_LD_LIBRARY_PATH in /home/ubuntu/.opam/opam-init/variables.sh old one generated during opam init: CAML_LD_LIBRARY_PATH=/home/ubuntu/.opam/4.01.0/lib/stublibs; export CAML_LD_LIBRARY_PATH; PERL5LIB=/home/ubuntu/.opam/4.01.0/lib/perl5:$PERL5LIB; export PERL5LIB; OCAML_TOPLEVEL_PATH=/home/ubuntu/.opam/4

Subtype constraints in interfaces

為{幸葍}努か 提交于 2019-12-11 13:19:21
问题 I want to compose several "traits" across several modules. A function might require multiple such "traits" as its input, i.e.: type 'a x_t = < get_x : int ; .. > as 'a constraint 'a = < get_b : float ; .. > val foo : x_t -> float composing these traits manually in the interface is cumbersome and error-prone, but perfectly possible. But in an ideal world, I should be able to use the "trait's" name instead of manually composing all the required fields, i.e. write something like: type 'a x_t = <

Calling functions in other files in OCaml

落花浮王杯 提交于 2019-12-11 13:16:16
问题 I have hello.ml that has a length function: let rec length l = match l with [] -> 0 | h::t -> 1 + length t ;; call.ml that uses the function: #use "hello.ml" ;; print_int (length [1;2;4;5;6;7]) ;; In interpreter mode (ocaml), I can use ocaml call.ml to get the results, but when I tried to compile it with ocamlc or ocamlbuild, I got compilation error. File "call.ml", line 1, characters 0-1: Error: Syntax error Then, how to modify the caller, callee, and build command to compile the code into

Synchronizing client calls in an xml-rpc-light server in OCaml

折月煮酒 提交于 2019-12-11 13:16:08
问题 I'm writing an XML-RPC module in OCaml using the xml-rpc-light library. I'd like to have a server which can receive concurrent requests and gather the data sent by all the requests in a common 'state'. To be concrete, but simplifying the real problem, suppose the server provides the function send : int -> bool which sends an integer and returns true on success and false on failure, and that the server wants to keep a list of all the integers that were ever called since it started (including