From f95748e82a354f2ac857359774ec3227705838af Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Tue, 26 Aug 2025 11:16:29 +0200 Subject: Prelude --- lib/convert.ml | 2 +- lib/iban.ml | 4 ++-- lib/iban.mli | 2 +- lib/ingcsv.ml | 18 +----------------- lib/ledger.ml | 26 +++++++++++--------------- lib/prelude.ml | 31 +++++++++++++++++++++++++++++++ 6 files changed, 47 insertions(+), 36 deletions(-) create mode 100644 lib/prelude.ml (limited to 'lib') diff --git a/lib/convert.ml b/lib/convert.ml index fb41020..5afc95e 100644 --- a/lib/convert.ml +++ b/lib/convert.ml @@ -1,4 +1,4 @@ -open Core +open Prelude open Ledger open Result.Let_syntax diff --git a/lib/iban.ml b/lib/iban.ml index 1db6c7b..fbea774 100644 --- a/lib/iban.ml +++ b/lib/iban.ml @@ -1,11 +1,11 @@ -open Core +open Prelude open Option.Let_syntax type t = string (* Modulo-97 arithmetic. Prevents us from having to use Zarith here. *) module M97 : sig - type t + type t = private int val of_int : int -> t val lt : t -> t -> bool diff --git a/lib/iban.mli b/lib/iban.mli index c2cad9f..fa18a63 100644 --- a/lib/iban.mli +++ b/lib/iban.mli @@ -1,4 +1,4 @@ -open Core +open Prelude type t diff --git a/lib/ingcsv.ml b/lib/ingcsv.ml index 53258fe..edb8aaa 100644 --- a/lib/ingcsv.ml +++ b/lib/ingcsv.ml @@ -1,6 +1,5 @@ -open Core +open Prelude open Result.Let_syntax -module Time_ns = Time_ns_unix module Debit_credit = struct type t = Debit | Credit [@@deriving string, sexp_of] @@ -561,21 +560,6 @@ let tx_from_prim ptx ~ams_tz : (tx, parse_err_ext) result = type csv_err = Parse_err of parse_err_ext | Exn of exn -module List = struct - include List - - let map_result ~(f : 'a -> ('b, 'c) result) : 'a list -> ('b list, 'c) result - = - let open Result.Let_syntax in - let rec go = function - | [] -> return [] - | x :: xs -> - let%map x' = f x and xs' = go xs in - x' :: xs' - in - go -end - let read_channel (c : In_channel.t) ~ams_tz : (tx list, csv_err) result = let%bind ptxs = try diff --git a/lib/ledger.ml b/lib/ledger.ml index ba21e49..84a0146 100644 --- a/lib/ledger.ml +++ b/lib/ledger.ml @@ -1,4 +1,4 @@ -open Core +open Prelude type account_type = Asset | Equity | Liability | Expense | Income @@ -87,12 +87,6 @@ module Labels = struct Sexp.List [ Sexp.Atom "Unit_label"; [%sexp_of: unit_tag] tag ])) end -module Z = struct - include Z - - let sexp_of_t x = Sexp.Atom (Z.to_string x) -end - module Money : sig type t @@ -142,7 +136,15 @@ type bal_assert = { module Account_id_map = Map.Make (Account_id) module Tx : sig - type t + (* Private because we only want to allow constructing balanced transactions. *) + type t = private { + cleared : Date.t option; + commodity_id : commodity_id; + debit : scalar Account_id_map.t; + credit : scalar Account_id_map.t; + labels : Labels.t; + } + type error = Unbalanced val make : @@ -153,14 +155,8 @@ module Tx : sig labels:Labels.t -> (t, error) result - val cleared : t -> Date.t option - val commodity_id : t -> commodity_id - val debit : t -> scalar Account_id_map.t - val credit : t -> scalar Account_id_map.t - val labels : t -> Labels.t val sexp_of_t : t -> Sexp.t end = struct - (* We hide this because we only want to allow constructing balanced transactions *) type t = { cleared : Date.t option; commodity_id : commodity_id; @@ -168,7 +164,7 @@ end = struct credit : scalar Account_id_map.t; labels : Labels.t; } - [@@deriving fields, sexp_of] + [@@deriving sexp_of] type error = Unbalanced diff --git a/lib/prelude.ml b/lib/prelude.ml new file mode 100644 index 0000000..57f7af3 --- /dev/null +++ b/lib/prelude.ml @@ -0,0 +1,31 @@ +include Core +module Time_ns = Time_ns_unix + +module List = struct + include List + + let map_result ~(f : 'a -> ('b, 'c) result) : 'a list -> ('b list, 'c) result + = + let open Result.Let_syntax in + let rec go = function + | [] -> return [] + | x :: xs -> + let%map x' = f x and xs' = go xs in + x' :: xs' + in + go +end + +module Z = struct + include Z + + let sexp_of_t x = Sexp.Atom (Z.to_string x) +end + +module Result = struct + include Result + + let unwrap = function + | Error _ -> failwith "Result.unwrap: unexpected (Error _)" + | Ok v -> v +end -- cgit v1.2.3