ocaml

When does the relaxed value restriction kick in in OCaml?

北慕城南 提交于 2019-11-29 09:58:28
Can someone give a concise description of when the relaxed value restriction kicks in? I've had trouble finding a concise and clear description of the rules. There's Garrigue's paper: http://caml.inria.fr/pub/papers/garrigue-value_restriction-fiwflp04.pdf but it's a little dense. Anyone know of a pithier source? An Addendum Some good explanations were added below, but I was unable to find an explanation there for the following behavior: # let _x = 3 in (fun () -> ref None);; - : unit -> 'a option ref = <fun> # let _x = ref 3 in (fun () -> ref None);; - : unit -> '_a option ref = <fun> Can

int * int vs (int * int) in OCaml sum type

给你一囗甜甜゛ 提交于 2019-11-29 09:56:18
type foo = A of int * int | B of (int * int) What is the difference between int * int and (int * int) there? The only difference I see is in pattern matching: let test_foo = function | A (f, s) -> (f, s) | B b -> b Is it just a syntactic sugar? How do you select which one to use? Is there any performance difference between these two forms? Yes, there is a performance difference: In memory A (23, 42) will contain a tag identifying it as an A and the two integers 23 and 42. B (23, 42) will contain a tag identifying it as a B and a pointer to a tuple containing the integers 23 and 42 . So there

OCaml Printf.sprintf

不问归期 提交于 2019-11-29 09:32:43
Why does this behavior occur? # Printf.sprintf ("Foo %d %s") 2 "bar";; - : string = "Foo 2 bar" # Printf.sprintf ("Foo %d" ^ " %s") 2 "bar";; Printf.sprintf ("Foo %d" ^ " %s") 2 "bar";; Error: This expression has type string but an expression was expected of type ('a -> 'b -> 'c, unit, string) format = ('a -> 'b -> 'c, unit, string, string, string, string) format6 I would expect that the string concatenation would be evaluated first, so everything will proceed as normal. Does this have to do with the type system trickery that Printf employs? Yes, it has to do with type system trickery. If you

The right way to use a data structure in OCaml

左心房为你撑大大i 提交于 2019-11-29 08:34:44
Ok, I have written a binary search tree in OCaml. type 'a bstree = |Node of 'a * 'a bstree * 'a bstree |Leaf let rec insert x = function |Leaf -> Node (x, Leaf, Leaf) |Node (y, left, right) as node -> if x < y then Node (y, insert x left, right) else if x > y then Node (y, left, insert x right) else node I guess the above code does not have problems. When using it, I write let root = insert 4 Leaf let root = insert 5 root ... Is this the correct way to use/insert to the tree? I mean, I guess I shouldn't declare the root and every time I again change the variable root's value, right? If so, how

automata in ocaml

有些话、适合烂在心里 提交于 2019-11-29 08:04:01
问题 I am a bit new to OCaml. I want to implement product construction algorithm for automata in ocaml. I am confused how to represent automata in ocaml. Can someone help me? 回答1: A clean representation for a finite deterministic automaton would be: type ('state,'letter) automaton = { initial : 'state ; final : 'state -> bool ; transition : 'letter -> 'state -> 'state ; } For instance, an automaton which determines whether a word contains an odd number of 'a' could be represented as such: let odd

Why there is a plus sign before this type?

末鹿安然 提交于 2019-11-29 06:22:15
问题 I was browsing ocaml's standard library, and came across this code in the map.ml file. module type S = sig type key type +'a t val empty: 'a t' I'm wondering why there is type +'a t , and why the author use it instead of simply 'a t . Its behaviour is strange and I can't deduce the usage of it. # type +'a t = 'a list;; type 'a t = 'a list # type +'a t = +'a list;; Characters 13-14: type +'a t = +'a list;; ^ Error: Syntax error Thanks 回答1: To build up on Jeffrey's answer, the reason the

gcc ld: symbol(s) not found for architecture x86_64

佐手、 提交于 2019-11-29 06:22:05
Alright so I'm making a lexer and a parser using Ocamlyacc. I've done my research and I think it's something to do with my makefile not picking the right bit version for my compiler or something like it? I don't know much about makefiles which is why I'm asking. I've run my program on another computer where it works without trouble so it gotta be something to do with my machine. It's a MacBook Pro 64 bit. I'm using Xcode 4.2.1. Here's the makefile: SHELL = /bin/sh C_C = gcc CPP_C = g++ ifdef GPROF C_CPP_FLAGS = -pg -O3 else ifndef DEBUG C_CPP_FLAGS = -O3 else C_CPP_FLAGS = -g endif endif C_LD

How to write code in F# for what functors do in OCaml?

我的梦境 提交于 2019-11-29 06:11:16
I have many programs written in OCaml, some of them use functors. Now, I am considering of writing and re-writing a part of code in F# (to benefit some advantages that OCaml does not have). One thing I am afraid of is to write code in F# for what functors do in OCaml. For instance, how could we emulate this example from OCaml manual in F#? type comparison = Less | Equal | Greater module type ORDERED_TYPE = sig type t val compare: t -> t -> comparison end module Set = functor (Elt: ORDERED_TYPE) -> struct type element = Elt.t type set = element list let empty = [] let rec add x s = match s with

How can I install OCaml with OPam on windows?

雨燕双飞 提交于 2019-11-29 05:56:06
How can I install OCaml with OPam on windows? I have been able to setup OCaml 4.03.0 in Windows 10, using the Opam package manager, by following the tutorial from this website: http://fdopen.github.io/opam-repository-mingw/ . Here are the detailed steps that I did: Install OCaml, Opam and Cygwin: Download the installation package from this link: http://fdopen.github.io/opam-repository-mingw/installation/ . There are both the 32 bit and 64 bit version, but I suggest to install the OCaml 64bit . When running the graphical installation file, it will automatically install OCaml 4.02.3, Cygwin,

Is there a way to print user-defined datatypes in ocaml?

回眸只為那壹抹淺笑 提交于 2019-11-29 05:44:57
I can't use print_endline because it requires a string, and I don't (think) I have any way to convert my very simple user-defined datatypes to strings. How can I check the values of variables of these datatypes? In many cases, it's not hard to write your own string_of_ conversion routine. That's a simple alternative that doesn't require any extra libraries or non-standard OCaml extensions. For the courses I teach that use OCaml, this is often the simplest mechanism for students. (It would be nice if there were support for a generic conversion to strings though; perhaps the OCaml deriving stuff