ocaml

Further refining the signature of a nested module in the signature of the parent

▼魔方 西西 提交于 2019-12-11 02:29:45
问题 I have an ML file which contains a nested module. For example: let f n = n + 1 module type M_type = sig val g : int -> int val h : int -> int end module M : M_type = struct let g n = n + 2 let h n = n + 3 end let j n = n |> M.h |> M.g |> f When writing an MLI for this ML, I wish not to expose M.h , but I do wish to expose M.g . Such as the following: module type M_type = sig val g : int -> int end module M : M_type val j : int -> int The above combination of ML and MLI does not compile. The

Differences between OCaml 2 and 3

微笑、不失礼 提交于 2019-12-11 02:20:12
问题 I'm interested in learning this language, but it seems that there is very few tutorials and books on the subject. I've found only one suitable book on the subject (Developing Applications With Objective Caml) which would be absolutely perfect but the problem is that it is based on version 2.04. So my only concern is if there would not be any serious problems with using this book, for OCaml 3.x. Or if there are any good tutorials concerning OCaml online please let me know. Thank you 回答1: There

A functor including an inheritance of modules does not work

♀尐吖头ヾ 提交于 2019-12-11 01:39:08
问题 I am still struggling with my design and implementation of modules, I have defined the following: module type MATRIX = sig type 'a t val init: 'a -> 'a t val print: 'a t -> unit end module type MMM = sig type 'a t end module type AMATRIX = sig include MATRIX module Mmm : MMM module Matrix: MATRIX val matrix_of_amatrix: 'a t -> int -> int -> 'a Matrix.t end module MatrixArray: MATRIX = struct type 'a t = 'a array array let init (e: 'a) : 'a t = failwith "to do" let print (x: 'a t) : unit =

An elegant way to parse sexp

谁都会走 提交于 2019-12-11 00:54:39
问题 sexp is like this: type sexp = Atom of string | List of sexp list , e.g., "((a b) ((c d) e) f)" . I have written a parser to parse a sexp string to the type: let of_string s = let len = String.length s in let empty_buf () = Buffer.create 16 in let rec parse_atom buf i = if i >= len then failwith "cannot parse" else match s.[i] with | '(' -> failwith "cannot parse" | ')' -> Atom (Buffer.contents buf), i-1 | ' ' -> Atom (Buffer.contents buf), i | c when i = len-1 -> (Buffer.add_char buf c; Atom

ocaml llvm kaleidoscope tutorial “Unbound module LlvmExecutionEngine”

大兔子大兔子 提交于 2019-12-10 23:59:40
问题 I switched to my mac and no longer have this issue but have a similar one. OCaml llvm "Unbound module ExecutionEngine" I'm trying to get this to work: https://github.com/llvm-mirror/llvm/tree/master/examples/OCaml-Kaleidoscope/Chapter7 from this tutorial http://llvm.org/docs/tutorial/OCamlLangImpl7.html (I'm 99% sure these two are by the same people) After getting around a few issues I have reached a stumbling block for the last few hours of me@mypc:~/Desktop/llvm-master/examples/OCaml

OCaml: design datatypes for a text adventure game

不羁的心 提交于 2019-12-10 22:13:28
问题 I am trying to make a simple naive text adventure game (base one this page) to learn OCaml. The game is about making an game engine, so all the information about rooms, items ect, is store in a json file. Sample json file would be like this: { "rooms": [ { "id": "room1", "description": "This is Room 1. There is an exit to the north.\nYou should drop the white hat here.", "items": ["black hat"], "points": 10, "exits": [ { "direction": "north", "room": "room2" } ], "treasure": ["white hat"] },

OCaml recursive function to apply a function n times

橙三吉。 提交于 2019-12-10 21:32:40
问题 I want to create a function of type int -> ('a -> 'a) -> 'a -> 'a in OCaml that takes an int n (non-neg) and a function f 'a -> 'a and an argument a of type 'a. f should be called on a n times. I've tried 3 different things but can only get int -> ('a -> 'b) -> 'a -> 'b, here are a few things I've tried. let rec f n g a = g a; f (n-1) g a;; which gives val f : int -> ('a -> 'b) -> 'a -> 'c = <fun> and I've tried let rec f n g a = if n > 0 then f (n-1) g a else g a ;; which gave me val f : int

Writing an interpreter in OCaml [closed]

隐身守侯 提交于 2019-12-10 20:22:03
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 3 years ago . I'm following a course in my University that ask me to write an interpreter in OCaml of a language starting from its operational semantics. Unfortunately, they didn't give us much resources from which we can learn about it, except from the lesson's slides. Can someone suggest me some book or some website where I

UML for OCaml immediate objects

旧时模样 提交于 2019-12-10 19:16:20
问题 I have created an immediate object in OCaml. let x = object (self) val dataMember = 3 method aMethod = print_endline "Called a method" end;; As the object doesn't have a name (is it considered anonymous?), how can it be correctly represented in UML? Thanks. 回答1: You could give it a nonce-name, or some other formulaic value. "Correctly" in this context really just means something that will be clear. There is no "right answer" handed down from the International UML Standards Body or anything.

OCaml Pattern match with non-constants

白昼怎懂夜的黑 提交于 2019-12-10 18:51:56
问题 Is it possible to do pattern matching on variables instead of constant values: # let x = 2 in let y = 5 in match 2 with | x -> "foo" | y -> "bar" | _ -> "baz";; let y = 5 in Warning 26: unused variable y. let x = 2 in Warning 26: unused variable x. | y -> "bar" Warning 11: this match case is unused. | _ -> "baz";; Warning 11: this match case is unused. - : string = "foo" Obviously, with this syntax, the x -> "foo" case takes everything. Is there a way to make it be equivalent to: match 2 with