I’ve produced a project structure using:
dune init proj lichessocaml
I wrote the code in bin/main.ml and made sure it worked in principle on the REPL. Now, I’m trying to separate the functional code into a library used by main.
I’ve added “open Mylibrary” to the top of main – which the compiler doesn’t complain about, however main can’t seem to see the types declared in the library.
THE QUESTION: what do I need to do for main to be able to see inside the library? Ideally I would expose just the types and the to_* functions from the code below:
module StringMap = Map.Make(String)
type outcome_enum = WhiteWin | BlackWin | Draw
type interval = { mins: int; inc: int }
type game = {
utc_date: string;
time_control: interval;
termination: string;
white: string;
black: string;
white_elo: int;
black_elo: int;
result: outcome_enum;
site: string;
event: string;
opening: string;
eco: string;
game: string;
}
let to_pairs x =
let re = Re2.create_exn "([A-Za-z]+) \"([^\"]+)" in
try
let match_ = Re2.first_match_exn re x in
let k = Re2.Match.get match_ ~sub:(`Index 1) in
let v = Re2.Match.get match_ ~sub:(`Index 2) in
Some ((Option.value k ~default:""),(Option.value v ~default:""))
with
_ -> let re = Re2.create_exn "^(.+)$" in
try Some ("Game", (Re2.find_first_exn re x))
with _ -> None
let rec to_map ?(m=StringMap.empty) f =
match f () with
| None -> to_map ~m:m f
| Some ("Game" as k,v) -> Some (StringMap.add k v m)
| Some (k,v) -> to_map ~m:(StringMap.add k v m) f
let get_interval x =
try
let re = Re2.create_exn "([0-9]+)\\+([0-9]+)" in
let match_ = Re2.first_match_exn re x in
let mins = Re2.Match.get match_ ~sub:(`Index 1) in
let inc = Re2.Match.get match_ ~sub:(`Index 2) in
{ mins=int_of_string (Option.value mins ~default:"0");
inc=int_of_string (Option.value inc ~default:"0")}
with _ -> {mins=0; inc=0}
let get_result x =
match x with
| "1-0" -> WhiteWin
| "0-1" -> BlackWin
| _ -> Draw
let get_int x =
int_of_string x
let to_record m =
match m with
| None -> None
| Some m -> (
let f k= StringMap.find k m in
Some {
event=f "Event";
white=f "White";
black=f "Black";
utc_date=f "UTCDate";
time_control=get_interval (f "TimeControl");
termination=f "Termination";
white_elo=get_int (f "WhiteElo");
black_elo=get_int (f "BlackElo");
result=get_result (f "Result");
site=f "Site";
opening=f "Opening";
eco=f "ECO";
game=f "Game";
})