ocaml

Where/how to declare the unique key of variables in a compiler written in Ocaml?

痞子三分冷 提交于 2019-12-05 10:42:31
I am writing a compiler of mini-pascal in Ocaml. I would like my compiler to accept the following code for instance: program test; var a,b : boolean; n : integer; begin ... end. I have difficulties in dealing with the declaration of variables (the part following var ). At the moment, the type of variables is defined like this in sib_syntax.ml : type s_var = { s_var_name: string; s_var_type: s_type; s_var_uniqueId: s_uniqueId (* key *) } Where s_var_uniqueId (instead of s_var_name ) is the unique key of the variables. My first question is, where and how I could implement the mechanism of

How to set the default-directory of compilation in Emacs?

纵饮孤独 提交于 2019-12-05 10:33:19
问题 I am coding OCaml under Emacs, I have one makefile in the working folder, and several sub-folders containing .ml files. If I launch M-x compile and make works fine on a buffer of makefile , but does not work on a buffer of a .ml file, it gives me an error: -*- mode: compilation; default-directory: "..." -*- Compilation started at Fri Jan 27 18:51:35 make -k make: *** No targets specified and no makefile found. Stop. Compilation exited abnormally with code 2 at Fri Jan 27 18:51:35 It is

Linear types in OCaml

和自甴很熟 提交于 2019-12-05 10:30:28
Rust has a linear type system. Is there any (good) way to simulate this in OCaml? E.g., when using ocaml-lua, I want to make sure some functions are called only when Lua is in a specific state (table on top of stack, etc). Edti : Here's a recent paper about resource polymorphism relevant to the question: https://arxiv.org/abs/1803.02796 As suggested by John Rivers, you can use a monadic style to represent "effectful" computation in a way that hides the linear constraint in the effect API. Below is one example where a type ('a, 'st) t is used to represent computation using a file handle (whose

OCaml: Set modules

自作多情 提交于 2019-12-05 10:26:08
问题 I want to use OCaml to generates sets of data and make comparisons between them. I have seen the documentation for Module types like Set.OrderType , Set.Make , etc, but I can't figure out how to initialize a set or otherwise use them. 回答1: Sets are defined using a functorial interface. For any given type, you have to create a Set module for that type using the Set.Make functor. An unfortunate oversight of the standard libraries is that they don't define Set instances for the built-in types.

Difference between module and package Ocaml

倖福魔咒の 提交于 2019-12-05 10:09:58
I'm basically trying to follow this stackoverflow answer located in this post: What is the best module for HttpRequest in OCaml and I'm running in to problems. When I am trying to run a single file with just open Lwt ;; I am getting and error saying it is an unbound module. I have run the following opam instruction: opam install lwt and it did install the correct package . So I think the problem is the difference between a module and a package, which I don't really understand. I was looking at this question as a possible answer, but I wasn't sure if it was what I needed. Unbound modules in

Are custom blocks ever copied by OCaml?

谁说我不能喝 提交于 2019-12-05 08:59:58
Imagine I've a C library called libcat for interacting with my cat fluffy. I'm therefore writing bindings for OCaml to simplify interactions with fluffy. module type CAT = sig type cat val find : ... -> cat val feed : cat -> unit ... end ;; module Cat : CAT = ... There is considerable memory management already built into libcat, like say caching, freeing destroyed toys, and maybe even a limited scope garbage collector for emptying the litter. Yet, overall libcat requires that users explicitly free unused resources, like lost toys. I've written a C stub for Cat.find that finds and allocates the

How to print a tree structure into a string fast in Ocaml?

ε祈祈猫儿з 提交于 2019-12-05 07:47:50
Assume I have a mathematical expression in a "tree" form in OCaml. It's represented as an algebraic type like this: type expr = Number of int |Plus of expr*expr Well, this is a very simplified definition, but it's enough to describe the problem. I want to convert it to a string in a reverse polish notation, so that Plus (Number i, Number j) becomes (+ i j) . A straightforward implementation would be let rec convert = function Number i -> string_of_int i |Plus (a,b) -> (let s = convert a in let p = convert b in "(+"^s^" "^p^")") But the thing is that it's incredibly slow on some input (that

Wildcard pattern overriding subtype constraint on polymorphic variant

允我心安 提交于 2019-12-05 07:20:13
Given these types type a = [ `A ] type b = [ a | `B | `C ] and this function let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | `C -> "C" applying a value of type a works without issue, as expected: let a: a = `A let _ = pp a However, if the function is modified to include a wildcard pattern let pp: [< b] -> string = function | `A -> "A" | `B -> "B" | _ -> "?" even though everything else remains the same, it now yields the following error (on let _ = pp a ): This expression has type b -> string but an expression was expected of type a -> 'a Type b = [ `A | `B ] is not compatible with

Define recursive signatures for modules

半世苍凉 提交于 2019-12-05 06:50:50
I know that it is possible to define recursive modules, does anyone know how to define recursive signatures? For instance, I would like to realize: module type AAA = sig module Bbb : BBB type 'a t val f : 'a Bbb.t -> 'a t end module type BBB = sig module Aaa : AAA type 'a t val g : 'a Aaa.t -> 'a t end Could anyone help? You can't, as far as I can tell. The closest solution is to limit the "recursive" bits to what is actually needed to express each signature separately: module type AA = sig module B : sig type t end type t val f : unit -> B.t end module type BB = sig module A : sig type t end

Error: Cannot safely evaluate the definition of the recursively-defined module

和自甴很熟 提交于 2019-12-05 06:32:28
I'm curious to understand why this error happens and which is the best way to get around it. I have a couple of files types.ml and types.mli which define a variant type value that can be of many different builtin OCaml types (float, int, list, map, set, etc..). Since I have to use the std-lib over this variant type I needed to concretize the Set module through the functor to be able to use sets of value type by defining the ValueSet module. The final .ml file is something like: module rec I : sig type value = Nil | Int of int | Float of float | Complex of Complex.t | String of string | List of