OCaml » Stack Overflow
39 FOLLOWERS
Stack Overflow is the largest, most trusted online community for developers to learn, share their programming knowledge, and build their careers. OCaml is a strict statically-typed functional programming language, focusing on expressivity, correctness, and efficiency.
OCaml » Stack Overflow
2d ago
I have just learned something about GADT in OCaml through Real World OCaml, and want to try to transfer the first little language there into a interpreter, thus using menhir.
The ast definition is totally the same as the example.Here is it:
type _ value =
| Int : int -> int value
| Bool : bool -> bool value
type _ expr =
| Value: 'a value -> 'a expr
| Eq : int expr * int expr -> bool expr
| Plus : int expr * int expr -> int expr
| If : bool expr * 'a expr * 'a expr -> 'a expr
let eval_value : type a. a value -> a = function
| Int x -> x
| Bool x ..read more
OCaml » Stack Overflow
2d ago
In Haskell, there are two main ways to look up information on functions.
Sites like Hoogle and Stackage. These sites provide two main types of searching:
Searching for the name of a function. For example, here is a search on Hoogle for a function called catMaybes.
This search returns the type of the catMaybes function, as well as the package and module it is defined in.
The major use-case for this type of search is when you see a function used somewhere and you want to know its type and what package it is defined in.
Searching for the type of a function. For example, here is a search on ..read more
OCaml » Stack Overflow
3d ago
I'm trying to generate .automaton files I added this to the dune file inside lib/ folder
(menhir
(flags --explain --dump)
(modules parser))
Then I run dune build, if I do find . -name '*.automaton', it finds no file, I was expecting the automaton file. What I'm missing?
I set up the project here: https://github.com/dhilst/pfpy, to reproduce
git clone https://github.com/dhilst/pfpy.git && cd pfpy
opam install --deps-only .
dune build ..read more
OCaml » Stack Overflow
1w ago
Having a custom variant type as below:
type yolo =
| A of string
| B of yolo
| C of yolo * yolo
| D of yolo * yolo
I also have a function that performs some operations on instances of yolo similar to this:
let rec swag y =
match y with
| A _ -> 1
| B _ -> 2
| C (left,right) -> (swag left) + (swag right)
| D (left,right) -> (swag left) + (swag right)
Since operations for cases C and D are exactly the same I would like to merge them, is it possible ..read more
OCaml » Stack Overflow
1w ago
I have the following files:
SetMaker.mli
module type Element = sig
type t
val create : 'a -> t
val compare : t -> t -> int
val to_string : t -> string
end
module type Set = sig
type t
val empty : unit -> t
end
module Make (M : Element) : Set with type t = (M.t list)
SetMaker.ml
module type Element = sig
type t
val create : 'a -> t
val compare : t -> t -> int
val to_string : t -> string
end
module type Set = sig
type t
val empty : unit -> t
end
module Make (M:Element) = struct
type t = M.t list
let empty () = []
end
main.ml
open ..read more
OCaml » Stack Overflow
2w ago
I have written the following two files, and although it works, it feels odd that the code would duplicate these long type declarations. On the other hand, if I remove either module type declaration then the code no longer compiles.
Is there a more proper way to write this kind of thing? Or is it correct to duplicate these type declarations in the .mli and .ml files?
set.mli
module type Element = sig
type t
val create : 'a -> t
val compare : t -> t -> int
val to_string : t -> string
end
module type Set = sig
type t
val empty : unit -> t
end
module Make : functor (M ..read more
OCaml » Stack Overflow
2w ago
Problem
I am trying to use the Z3 optimization capabilities to take into account some objectives when checking satisfiability. When I ask Z3 to minimize x + y with x > 0 and y > 0, the obtained interpretation with get_model assigns 1 to both x and y. I am, however, interested in obtaining interpretations that are "closer" to the optimal solution. I tried a workaround (which I explain below), but I'm wondering whether there exists some "built-in" feature in Z3 that allows to "better" interpret unbounded objectives.
What I tried
I tried to use get_lower and get_upper, which gives a symboli ..read more
OCaml » Stack Overflow
2w ago
I am trying to define a SCALAR signature with its own infix operator in
module type SCALAR =
sig
type t (* type of scalar element *)
val zero : t
val one : t
val (++) : t -> t -> t (* infix addition: t ++ t *)
end
with using SCALAR signature, I defined a INT module:
module INT : SCALAR with type t = int
=
struct
type t = int
let zero = 0
let one = 1
let (++) x y = x + y
end
I expected adding two INT modules with defined infix operator like INT.zero ++ INT.one is valid operation and expected its returning value in ..read more
OCaml » Stack Overflow
2w ago
Can algebraic handlers in OCaml be separated and put into different modules, then be nested together in some other file?
For instance, say you have a program in a file called program.ml that uses a user-defined function put: int -> unit that modifies a variable x. In this scenario, there is an effect Put_found: int -> unit Effect.t and put is defined as
put n = x:= n; perform(Put_found n)
A sample program would look like: put(5);put(0).
There are also two compilation units Handler1 and Handler2, each with their ml and mli files. The former contains a function run_h1 that when given the ..read more
OCaml » Stack Overflow
3w ago
Haskell has PatternSynonyms which enables to simplify a complex pattern matching. An example is shown in that extension description page. That is, for Type below,
data Type = App String [Type]
with these pattern synonyms
pattern Arrow t1 t2 = App "->" [t1, t2]
pattern Int = App "Int" []
pattern Maybe t = App "Maybe" [t]
we can simplify the pattern matches as follows
collectArgs :: Type -> [Type]
collectArgs (Arrow t1 t2) = t1 : collectArgs t2
collectArgs _ = []
isInt :: Type -> Bool
isInt Int = True
isInt _ = False
isIntEndo :: Type -> Bool
isI ..read more