ocaml

OCaml: Is there a function with type 'a -> 'a other than the identity function?

耗尽温柔 提交于 2019-12-06 17:23:56
问题 This isn't a homework question, by the way. It got brought up in class but my teacher couldn't think of any. Thanks. 回答1: How do you define the identity functions ? If you're only considering the syntax, there are different identity functions, which all have the correct type: let f x = x let f2 x = (fun y -> y) x let f3 x = (fun y -> y) (fun y -> y) x let f4 x = (fun y -> (fun y -> y) y) x let f5 x = (fun y z -> z) x x let f6 x = if false then x else x There are even weirder functions: let f7

Implementing type equation generator in OCaml

为君一笑 提交于 2019-12-06 16:28:27
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 x end let rec gen_equations : TEnv.t -> exp -> typ -> typ_eqn =fun tenv e ty -> match e with | CONST n

OCaml Mutex module cannot be found

亡梦爱人 提交于 2019-12-06 15:58:50
I tried to use Mutex module, such as Mutex.create(), but compiler says Unbound module Mutex. Does it require some special namespace? Thanks For toplevel : ocaml -I +threads # #load "unix.cma";; # #load "threads.cma";; # Mutex.create ();; - : Mutex.t = <abstr> For ocamlc : ocamlc -thread unix.cma threads.cma src.ml For ocamlopt : ocamlopt -thread unix.cmxa threads.cmxa src.ml For findlib : ocamlfind ocamlc -thread -package threads -linkpkg src.ml 来源: https://stackoverflow.com/questions/17188866/ocaml-mutex-module-cannot-be-found

OCaml: Automate custom pretty-printer installing

自作多情 提交于 2019-12-06 15:32:26
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? In short, you need to run #install_printer directive whenever you load your library in the top. I'm using the following code to evaluate code in the toplevel: open Core_kernel.Std open Or_error let eval_exn str = let

Need and impossibility of having a type of a signature

柔情痞子 提交于 2019-12-06 13:48:53
问题 I have defined 2 signature and 4 modules as follows, and it works fine: module type TRIANGLE = sig type 'a t val get ... val set ... ... end module type MATRIX = sig type 'a t val unionArrayArray: 'a TriangleArray.t -> 'a TriangleArray.t -> 'a t val unionListList: 'a TriangleList.t -> 'a TriangleList.t -> 'a t val unionArrayList: 'a TriangleArray.t -> 'a TriangleList.t -> 'a t val unionListArray: 'a TriangleList.t -> 'a TriangleArray.t -> 'a t ... end module rec MatrixArray: MATRIX = struct

The correct way to use Ocamlnet 3 - Http_client.Convenience.http_post

孤街醉人 提交于 2019-12-06 13:35:29
I am trying to use Http_client.Convenience.http_post to make a http post request . The API is fairly simple: val http_post : string -> (string * string) list -> string Does a "POST" request with the given URL and returns the response body. The list contains the parameters send with the POST request. What I wish to do is to construct a http post request to get the flight information via google flights , explained as part 1 in here: http://www.nohup.in/blog/using-json-google-flights To maintain the format of the Post request , I took a screenshot as this: So finally, I construct a Http_client

ocamlbuild; building toplevel

只谈情不闲聊 提交于 2019-12-06 11:49:34
问题 Having successfully reorganized my project for ocamlbuild with subdirectories and using ocamlfind, I've found it difficult to build the top-level. I've constructed a .mltop file containing all the modules that would be included and added the packages to the _tags , but the build doesn't work. It cannot find the C functions that are compiled with one of the modules. With -classic-display on, I can see that file, libcside.a , not being included and isn't even being compiled at all! The c file

Round-robin algorithm in OCaml

喜夏-厌秋 提交于 2019-12-06 11:44:45
This is the followup question of What's the grouping plan so that every two people are grouped together just once? Basically, I implemented the Round robin algorithm . By the algorithm, it can generate pairs list where each possible pair of elements are grouped together exactly once. For example, we have a, b, c, d , then On first day, we do a b c d Then we group like [(a,c);(b,d)]. Then we round it clockwise like a c d b Then we group like [(a,d);(c,b)]. Then we round it clockwise like a d b c Then we group like [(a,b);(d,c)]. (Note, a is fixed all the time.) Finally I can get [(a,c);(b,d)] [

Tree to ordered list with tail recursion

梦想与她 提交于 2019-12-06 11:00:55
I am actually sitting over a hour on a problem and don´t find a solution for it. I have this data type: type 'a tree = Empty | Node of 'a * 'a tree * 'a tree And i have to find a function which converts a given tree in a ordered list. There is also no invariant like that the left child has to be less then the right. I already found a "normal" recursion solution but not a tail recursive solution. I already thought about to build a unordered list and sort it with List.sort , but this uses a merge sort which is not tail recursive. Maybe someone has a good advice. Thank you! If you want to

Ocaml Lwt - some implementations of multiplayer game

南笙酒味 提交于 2019-12-06 10:40:18
I'm going on to writing a simple implementation of tic-tac-toe server (via telnet). The task - players connect to server and after they send START the server looks for a partner who typed START too, and game begins. A piece of code: let handle_income () = let con = Lwt_unix.accept sock in con >>= fun (cli, addr) -> let player = Lwt.return {state = Sleeping; descriptor = Lwt.return cli} in send_to_client player "Welcome to the server. To start game type in START and press Enter"; player;; let rec make_ready player = player >>= fun {state; descriptor} -> send_to_client player "Waiting for start