ocaml

Implementing type equation generator in OCaml

Deadly 提交于 2019-12-08 08:24:36
问题 type exp = | CONST of int | VAR of var | ADD of exp * exp | SUB of exp * exp | ISZERO of exp | IF of exp * exp * exp | LET of var * exp * exp | PROC of var * exp | CALL of exp * exp and var = string type typ = TyInt | TyBool | TyFun of typ * typ | TyVar of tyvar and tyvar = string type typ_eqn = (typ * typ) list module TEnv = struct type t = var -> typ let empty = fun _ -> raise (Failure "Type Env is empty") let extend (x,t) tenv = fun y -> if x = y then t else (tenv y) let find tenv x = tenv

How to parse a string to a regex type in OCaml

风格不统一 提交于 2019-12-08 07:45:12
问题 We define a regex type like this: type regex_t = | Empty_String | Char of char | Union of regex_t * regex_t | Concat of regex_t * regex_t | Star of regex_t We want to write a function string_to_regex: string -> regex_t . The only char for Empty_string is 'E' The only chars for Char are 'a'..'z' '|' is for Union '*' is for Star Concat is assumed for continuous parsing. '(' / ')' have highest Precedence, then star, then concat, then union For example, (a|E)*(a|b) will be Concat(Star(Union(Char

functions calls order in OCaml

懵懂的女人 提交于 2019-12-08 07:36:56
问题 I discovered the trace function in OCaml to follow functions calls. I tried it with fibonacci however the order of function calls was not the one I was expecting. In my code I call fib (n - 1) first then fib (n - 2) but, with trace, it tells me that fib (n - 2) is actually called first. What is the reason? let rec fib n = if n < 2 then 1 else fib (n - 1) + fib (n - 2);; When I trace fib 3 I get: fib <-- 3 fib <-- 1 fib --> 1 fib <-- 2 fib <-- 0 fib --> 1 fib <-- 1 fib --> 1 fib --> 2 fib -->

ocaml: exposing a printf function in an object's method

我只是一个虾纸丫 提交于 2019-12-08 06:26:05
问题 I have the following (simplified) ocaml code, for a logger: type log_level = | Error | Warn | Info let ord lvl = match lvl with | Error -> 50 | Warn -> 40 | Info -> 30 let current_level = ref (ord Warn) let logf name lvl = let do_log str = if (ord lvl) >= !current_level then print_endline str in Printf.ksprintf do_log The logf function can be used with a printf format, as in: logf "func" Warn "testing with string: %s and int: %d" "str" 42; 1) How can I wrap this up in an object, so that name

Can't load batteries using FindLib in Ocaml TopLevel

旧城冷巷雨未停 提交于 2019-12-08 05:40:43
问题 I successfully installed ocaml-batteries-included and findlib . I can do 'ocamlfind ocamlc -package batteries -c mycode.ml` without problems. Also, if I do ocamlfind list , I get $ ocamlfind list batteries (version: 2.0) batteries.pa_comprehension (version: 2.0) batteries.pa_comprehension.syntax (version: 2.0) batteries.pa_llist (version: 2.0) batteries.pa_llist.syntax (version: 2.0) batteries.pa_string (version: 2.0) batteries.pa_string.syntax (version: 2.0) batteries.syntax (version: 2.0)

OCaml: Automate custom pretty-printer installing

送分小仙女□ 提交于 2019-12-08 05:06:55
问题 I have implemented a pretty-printer for a module. Currently I start up utop , load the dependencies then do #install_printer pp_custom;; where pp_custom is the pretty-printer. I would like to automate this so that I can have it in a way that is similar to the lacaml library where the pretty-printer for the matrix is "installed" by default. How would I go about to doing that? 回答1: In short, you need to run #install_printer directive whenever you load your library in the top. I'm using the

ocamlbuild specify output location/name

三世轮回 提交于 2019-12-08 04:20:02
问题 Can I specify the build location and file name with the ocamlbuild tool? I would like to be able to say (in pseudocode): ocamlbuild myapp.ml -b native -o bin/myapp 回答1: There is no such option, but ocamlbuild is extensible and allows you to add your own options with Options.add . Of course, you will also need to add some implementation. Basically, hijacking the rule for native and extending it with installation procedure may work. To extend ocamlbuild you should write a plugin (other option

not able to install anything using opam

时光怂恿深爱的人放手 提交于 2019-12-08 03:54:47
问题 I installed opam with homebrew on Mac OS X 10.10. But I can't install anything using opam. This is an example error output that get when I try to install ocamlfind: # opam-version 1.2.0 # os darwin # command ./configure -bindir /Users/roger/.opam/system/bin -sitelib /Users/roger/.opam/system/lib -mandir /Users/roger/.opam/system/man -config /Users/roger/.opam/system/lib/findlib.conf -no-topfind # path /Users/roger/.opam/system/build/ocamlfind.1.5.5 # compiler system (4.02.1) # exit-code 1 #

Pattern matching on a GADT fails

。_饼干妹妹 提交于 2019-12-08 03:43:45
问题 I was playing around a bit more with ReasonML and found pattern matching on type t from the following example to not be working with the error Error: This pattern matches values of type t(float) but a pattern was expected which matches values of type t(int) Type float is not compatible with type int type t('a) = | One: t(int) | Two: t(float); let x = fun | One => None | Two => None; Now on some level this makes sense to me if this was about the return type of a function. I found an answer (I

Ocaml polymorphic records type is less general

穿精又带淫゛_ 提交于 2019-12-08 03:30:27
问题 Given the following type: type ('props,'state) reactInstance = { props: 'props; state: 'state; updater: 'event . (('props,'state) reactInstance -> 'event -> 'state) -> ('props,'state) reactInstance -> 'event -> unit;} I'm trying to achieve: let rec updater f instance event = let nextState = f instance event in let newInstance = { props; state = nextState; updater } in () let newInstance = { props; state = (reactClass.getInitialState ()); updater } I gave the updater a forall-like type