ocaml

What is the most efficient implementation of arrays with functional updates?

早过忘川 提交于 2019-11-30 03:59:15
I need an array-like data structure with the fastest possible functional update. I've seen a few different implementation of flexible arrays that provide me with this property (Braun, Random Access Lists) but I'm wondering if there is an implementation that is specifically optimized for the case when we are not interested in append or prepend - just updates. Jean-Cristophe Filliâtre has a very nice implementation of persistent arrays, that is described in the paper linked at the same page (which is about persistent union-find, of which persistent arrays are a core component). The code is

Parsing grammars using OCaml

爷,独闯天下 提交于 2019-11-30 03:57:12
I have a task to write a (toy) parser for a (toy) grammar using OCaml and not sure how to start (and proceed with) this problem. Here's a sample Awk grammar: type ('nonterm, 'term) symbol = N of 'nonterm | T of 'term;; type awksub_nonterminals = Expr | Term | Lvalue | Incrop | Binop | Num;; let awksub_grammar = (Expr, function | Expr -> [[N Term; N Binop; N Expr]; [N Term]] | Term -> [[N Num]; [N Lvalue]; [N Incrop; N Lvalue]; [N Lvalue; N Incrop]; [T"("; N Expr; T")"]] | Lvalue -> [[T"$"; N Expr]] | Incrop -> [[T"++"]; [T"--"]] | Binop -> [[T"+"]; [T"-"]] | Num -> [[T"0"]; [T"1"]; [T"2"]; [T

Topological sort in OCaml

久未见 提交于 2019-11-30 03:42:49
I'm trying to write topological sorting in ocaml, but I'm a beginner (in OCaml & graphs algorithms) and I can't do this by myself. It's easier for me to think about topological sorting in, for example, C++ (and there is a lot examples of topological sorting in C++ on the Internet), but I want to learn something new. Moreover, I've found some examples of topological sorting written in OCaml, but I don't understand them, to be frankly. Let's say I have a list (int * int list) list , for example: myList = [(1, [2]); (5, [6; 7]); (3, [2]); (6, [3; 7]); (8, [7]); (4, [3; 1])];; and that means, that

How to merge OCaml module types (signatures) defining the same type?

谁都会走 提交于 2019-11-30 03:19:18
问题 In OCaml, I have two module types defining a type t : module type Asig = sig type t val a : t end module type Bsig = sig type t val b : t end I want to automate the creation of a module type merging them. I want to create a module type equivalent to: module type ABsig_manual = sig type t val a : t val b : t end I tried module type ABsig = sig include Asig include Bsig end but this fails with Error: Multiple definition of the type name t . It seems impossible to add a type constraint to the

OCaml: Why I can't use this operator infix?

Deadly 提交于 2019-11-30 03:05:58
I defined a custom equality operator (the definition is not really important so I will insert dummy stuff): let ( ~=~ ) a b = true If I try to use it infix: if a ~=~ b then 1 else 2 I get the following error: This expression is not a function; it cannot be applied . I can fix this either by renaming the operator from ~=~ to =~ or by calling it as a function: if (~=~) a b then 1 else 2 . This seems that is a general problem with operators that start with ~ . My question is why I can't use such operators infix? Is anything special about ~ symbol? Note: I already went through documentation but I

How does the OCaml type inferencing algorithm work?

蓝咒 提交于 2019-11-30 03:02:04
问题 I'm currently learning OCaml, and I'm curious to HOW OCaml does its type inferencing. I know that it's done through a process called unification, and I tried reading about the algorithm in the published paper but the notation threw me off. Can anyone describe the step-by-step process for me? 回答1: Actually, it can be argued that unification is an implementation detail of the algorithm. The type system is only a set of rules. The rules allow to check an existing typing derivation. The rules do

Higher-order type constructors and functors in Ocaml

一个人想着一个人 提交于 2019-11-30 02:57:44
Can the following polymorphic functions let id x = x;; let compose f g x = f (g x);; let rec fix f = f (fix f);; (*laziness aside*) be written for types/type constructors or modules/functors? I tried type 'x id = Id of 'x;; type 'f 'g 'x compose = Compose of ('f ('g 'x));; type 'f fix = Fix of ('f (Fix 'f));; for types but it doesn't work. Here's a Haskell version for types: data Id x = Id x data Compose f g x = Compose (f (g x)) data Fix f = Fix (f (Fix f)) -- examples: l = Compose [Just 'a'] :: Compose [] Maybe Char type Natural = Fix Maybe -- natural numbers are fixpoint of Maybe n = Fix

Know of an OCAML IDE? [closed]

一世执手 提交于 2019-11-30 02:02:40
Know of an OCAML/CAML IDE? Especially one that runs on Linux? Alex M Emacs in Caml mode , or Tuareg mode , or TypeRex mode . TypeRex adds auto-completion to Taureg in emacs - a really nice feature for people who prefer the more graphical IDE's. There is Camelia . You can also integrate OCaml into Eclipse . Also in Emacs you can use ocaml-mode and tuareg-mode. NullPointer I vote OcaIDE . Now it has upgraded to v1.2.5. it become an up-to-date IDE (supporting ocaml 3.10-3.11, especially ocamlbuild, which is a great time-saver) and armed with rich, stable features. I've installed OcaIDE on an

How do I declare a map type in Reason ML?

只谈情不闲聊 提交于 2019-11-30 01:53:07
问题 One advantage of Reason ML over JavaScript is that it provides a Map type that uses structural equality rather than reference equality. However, I cannot find usage examples of this. For example, how would I declare a type scores that is a map of strings to integers? /* Something like this */ type scores = Map<string, int>; And how would I construct an instance? /* Something like this */ let myMap = scores(); let myMap2 = myMap.set('x', 100); 回答1: The standard library Map is actually quite

How do I do automatic data serialization of data objects?

可紊 提交于 2019-11-30 00:26:41
One of the huge benefits in languages that have some sort of reflection/introspecition is that objects can be automatically constructed from a variety of sources. For example, in Java I can use the same objects for persisting to a db (with Hibernate), serializing to XML (with JAXB), and serializing to JSON (json-lib). You can do the same in Ruby and Python also usually following some simple rules for properties or annotations for Java. Thus I don't need lots "Domain Transfer Objects". I can concentrate on the domain I am working in. It seems in very strict FP like Haskell and Ocaml this is not