ocaml

Explicit polymorphic type in record

落花浮王杯 提交于 2019-12-12 09:15:18
问题 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? 回答1: 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

Ocaml modules implementation

孤者浪人 提交于 2019-12-12 08:28:15
问题 Ocaml's standard library contains various modules: List , Map , Nativeint , etc. I know that interfaces for these modules are provided (e.g. for the List module), but I am interested in the algorithms and their implementations used in modules' functions. Where can I find that? 回答1: On your system: /usr/lib/ocaml/list.ml and other .ml files On the web: https://github.com/ocaml/ocaml/blob/trunk/stdlib/list.ml and other .ml files in https://github.com/ocaml/ocaml/tree/trunk/stdlib The List

Understand Core's `Fn.const`

余生颓废 提交于 2019-12-12 07:57:56
问题 Jane Street's Core lib has such a function: Fn.const . https://github.com/janestreet/core_kernel/blob/master/lib/fn.ml let const c = (); fun _ -> c val const : 'a -> 'b -> 'a produces a function that just returns its first argument I really don't understand it. What's the purpose of this function? In what scenario we have to use it? Why put (); first? Why not write it as let const c = fun () -> c ? this will give a function taking unit as parameter and always returns initial c . If I do let f

how to pattern match, execute a function then pattern match on the executed function to execute another function

余生长醉 提交于 2019-12-12 06:37:02
问题 I'm suppose to write a function that copies elements in a array from one place to the other. copy_obj is the function doing that. now I am given a list of pointers that represents the locations of elements i need to copy therefore I need to apply the function copy_obj on each elements in the list with the address of the free location where I should start to copy. In my code it is f. Considering that the function copy_obj returns a pair of addresses, and one is the updated value of free, I

OCaml error with types

拜拜、爱过 提交于 2019-12-12 06:23:20
问题 I have the following error from OCaml and I don't understand why. I'm trying to define an interpreter in OCaml. I have some types and functions to evaluate these types. I paste the relevant code. I have these types: type ide = string type exp = Eint of int | Ebool of bool | Var of ide | Prod of exp * exp | Sum of exp * exp | Diff of exp * exp | Eq of exp * exp | Minus of exp | Iszero of exp | Or of exp * exp | And of exp * exp | Not of exp | Ifthenelse of exp * exp * exp | Let of ide * exp *

Eager side effect (printf) in ocaml

孤街醉人 提交于 2019-12-12 06:13:55
问题 Newbie question: Say I have a function do_sth that is very slow, and it is applied to range 1 to n. I want to print the result of do_sth i when it is looping. How to do this? A naive attempt fails as the values will only be printed after the whole loop: let rec loop i = if i>1000 then 0 else let fi = do_sth i in ( Printf.printf "%d %d\n" i fi; fi + loop (i+1) ) let f_sum = loop 1 回答1: Effects in Ocaml are eager by default: the language is said to have eager evaluation 1 , as opposed to so

Possible OCaml code generation bug

£可爱£侵袭症+ 提交于 2019-12-12 03:49:33
问题 The following self contained code highlights a problem in OCaml, possibly with the code generation. Array x has connectivity information for nodes in [0..9]. Function init_graph originally constructed explicit arrays of incoming nodes for every node. The reduced version shown below just prints the two connected nodes. Function init_graph2 is identical to init_graph except for a "useless" else branch. But outputs produced by these two functions are quite different. You can run it and see that

How to properly time an ocaml program?

孤街浪徒 提交于 2019-12-12 01:56:52
问题 let t = Unix.gettimeofday() let rec nothing i = match i with | 1000000000 -> 1 | _ -> nothing (i+1) let () = Printf.printf "%d %fs\n" (nothing 0) (Unix.gettimeofday() -. t) I use the command ocamlc unix.cma test.ml to compile it to bytecode. The execution time is apparently several seconds, but the program prints 0.000001s. And if I replace Unix.gettimeofday() with Sys.time(), it is just 0.000000s. Running the code in utop is not much better. It gives about 0.02s, while I count at least 5s. I

OCaml: Unbound module Core with ~/.ocamlinit setup

拟墨画扇 提交于 2019-12-12 01:55:15
问题 I have installed a few packages using opam, such as Core and Batteries. The ocamlinit file is as follows: (* Added by OPAM. *) #use "topfind" #thread #camlp4o #require "core.top" #require "core.syntax" #require "batteries" let () = try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH") with Not_found -> () ;; When I run this with utop I can see the modules for Batteries but I cannot see any of the modules for Core. When I try to do open Core or open Core.Std I get an unbound module

OCaml: Call function within another function

≡放荡痞女 提交于 2019-12-12 01:40:16
问题 In one module I have a user defined type and a recursive function that returns a string. Then I want to create a function that will create an object of that type and pass it to the function. Here is a simple example of the code I have: type species = Animal of string | Mammal of species | Fish of species let rec species_to_string = function | Animal (x) -> x | Mammal (x) -> "Mammal (" ^ (species_to_string x) ^ ")" | Fish (x) -> "Fish (" ^ (species_to_string x) ^ ")" let process () = let dog =