ocaml

Verify that an OCaml function is tail-recursive

巧了我就是萌 提交于 2019-12-05 05:50:08
How can I tell if OCaml recognizes a particular function as tail-recursive? In particular, I want to find out if the OCaml compiler recognizes Short-circuited operators and tail recursion Thanks to Jeffrey's answer below, I tried this with the simple function let rec check_all l = match l with | [] -> true | hd :: tl -> hd && check_all tl and indeed, it does optimize to: camlTest__check_all_1008: .cfi_startproc .L102: cmpl $1, %eax je .L100 movl (%eax), %ebx cmpl $1, %ebx je .L101 movl 4(%eax), %eax jmp .L102 .align 16 .L101: movl $1, %eax ret Starting from OCaml 4.03, and despite the typo in

How to use modules with js_of_ocaml?

左心房为你撑大大i 提交于 2019-12-05 05:36:00
I am currently working on a website project written in OCaml and compiled to javascript using js_of_ocaml. It works pretty well as long as I have only one source file using the command ocamlfind ocamlc -package js_of_ocaml -package js_of_ocaml.syntax -syntax camlp4o -linkpkg -o file.byte file.ml but I would like to include several modules in my project. How can I do that ? The other modules are actually a lexer and a parser poduced by ocamllex and menhir. I have read a tutorial on how to use ocamllex and menhir with js_of_ocaml but it makes wrong assumptions on where js_of_ocaml is installed

Converting OCaml strings to format6

本小妞迷上赌 提交于 2019-12-05 04:12:31
The following code does not compile: let x = "hello" in Printf.printf x The error is: Error: This expression has type string but an expression was expected of type ('a, out_channel, unit) format = ('a, out_channel, unit, unit, unit, unit) format6 1) Can someone give an explanation of the error message? 2) And why would a string cannot be passed to printf ? The first argument to printf must be of type ('a, out_channel, unit) format not string. String literals can be automatically converted to an appropriate format type, but strings in general can't. The reason for that is that the exact type of

fold_tree in OCaml

感情迁移 提交于 2019-12-05 04:08:37
As You may know, there are higher order functions in OCaml, such as fold_left, fold_right, filter etc. On my course in functional programming had been introduced function named fold_tree, which is something like fold_left/right, not on lists, but on (binary) trees. It looks like this: let rec fold_tree f a t = match t with Leaf -> a | Node (l, x, r) -> f x (fold_tree f a l) (fold_tree f a r);; Where tree is defined as: type 'a tree = Node of 'a tree * 'a * 'a tree | Leaf;; OK, here's my question: how does the fold_tree function work? Could you give me some examples and explain in human

Ocaml: Lazy Lists

余生长醉 提交于 2019-12-05 03:34:28
How can I make a lazy list representing a sequence of doubling numbers? Example: 1 2 4 8 16 32 Using streams: let f x = Stream.from (fun n -> Some (x * int_of_float (2.0 ** float_of_int n))) or let f x = let next = ref x in Stream.from (fun _ -> let y = !next in next := 2 * y ; Some y) Using a custom lazy_list type: type 'a lazy_list = | Nil | Cons of 'a * 'a lazy_list lazy_t let rec f x = lazy (Cons (x, f (2*x))) chollida The great blog enfranchised mind has a great article on this topic: http://enfranchisedmind.com/blog/posts/ocaml-lazy-lists-an-introduction/ You can also check out http:/

Cross-compiling ocaml apps for ARM

♀尐吖头ヾ 提交于 2019-12-05 02:32:47
I'm cross-compiling a touchscreen driver, which comes with an ocaml calibration application. I'm trying to compile the driver and the application for ARM, in particular, the Beagleboard, running Angström. It goes like this: ^_^[raziel@Bebop zytouch-driver-20081121]$ source /usr/local/angstrom/arm/environment-setup ^_^[raziel@Bebop zytouch-driver-20081121]$ make CC=arm-angstrom-linux-gnueabi-gcc arm-angstrom-linux-gnueabi-gcc -std=gnu99 -g -O2 -Wall -Wextra -Werror -Wstrict-prototypes -Wmissing-prototypes -c -o daemon/config.o daemon/config.c arm-angstrom-linux-gnueabi-gcc -std=gnu99 -g -O2

Functors in Ocaml

不想你离开。 提交于 2019-12-05 01:35:43
I am having a bit of a problem with a functor (and it's resultant type). Below, I have a Set functor that uses an Ordered type. I actually used the set.ml that comes with ocaml for some guidance, but I seem to be doing everything ahhem right. I created an Ordered module with integers and applied it to the Set functor to get the last module on this code sample, IntSet. The next line fails, when I try to insert an integer. I get the following type error: Error: This expression has type int but is here used with type SetInt.elt = Set(OrdInt).elt Don't get me wrong, the type system is correct here

Scripted main in OCaml?

帅比萌擦擦* 提交于 2019-12-05 01:34:30
问题 How can I emulate this Python idiom in OCaml? if __name__=="__main__": main() See RosettaCode for examples in other programming languages. 回答1: There is no notion of main module in Ocaml. All the modules in a program are equal. So you can't directly translate this Python idiom. The usual way in Ocaml is to have a separate file containing the call to main , as well as other stuff like command line parsing that only make sense in a standalone executable. Don't include that source file when

Resolving reduce/reduce conflict in yacc/ocamlyacc

ぐ巨炮叔叔 提交于 2019-12-05 01:24:43
I'm trying to parse a grammar in ocamlyacc (pretty much the same as regular yacc) which supports function application with no operators (like in Ocaml or Haskell), and the normal assortment of binary and unary operators. I'm getting a reduce/reduce conflict with the '-' operator, which can be used both for subtraction and negation. Here is a sample of the grammar I'm using: %token <int> INT %token <string> ID %token MINUS %start expr %type <expr> expr %nonassoc INT ID %left MINUS %left APPLY %% expr: INT { ExprInt $1 } | ID { ExprId $1 } | expr MINUS expr { ExprSub($1, $3) } | MINUS expr {

Impredicative polymorphism in F#

删除回忆录丶 提交于 2019-12-05 00:20:04
OCaml's Hindley-Milner type system does not allow for impredicative polymorphism (à la System-F), except through a somewhat recent extension for record types. The same applies to F#. It however is sometimes desirable to translate programs written with impredicative polymorphism (e.g. Coq) into such languages. The solution for Coq's extractor to OCaml is to (sparingly) use Obj.magic , which is a kind of universal unsafe cast. This works because in OCaml's runtime system, all values have the same size regardless of their type (32 or 64 bits depending on architecture) the more sophisticated type