From 46169ec3eb38e177cafd7faf6338d36c6a9e3971 Mon Sep 17 00:00:00 2001 From: Rutger Broekhoff Date: Thu, 27 Nov 2025 23:35:08 +0100 Subject: Whatever all of this is --- lib/ledger.ml | 192 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 137 insertions(+), 55 deletions(-) (limited to 'lib/ledger.ml') diff --git a/lib/ledger.ml b/lib/ledger.ml index 84a0146..7805179 100644 --- a/lib/ledger.ml +++ b/lib/ledger.ml @@ -1,7 +1,5 @@ open Prelude -type account_type = Asset | Equity | Liability | Expense | Income - type tx_type = | Interest_tx | Online_banking_tx @@ -14,11 +12,10 @@ type tx_type = | Direct_debit_tx | Periodic_tx -type iban_tag = Account_tag | Counterparty_iban_tag -[@@deriving compare, sexp_of] +type iban_tag = Account_tag | Counterparty_iban_tag [@@deriving compare, sexp] type unit_tag = Filed_tag | Google_pay_tag | Auto_round_savings_tag -[@@deriving compare, sexp_of] +[@@deriving compare, sexp] type string_tag = | Desc_tag @@ -32,7 +29,7 @@ type string_tag = | Terminal_tag | Card_seq_no_tag | Savings_account_tag -[@@deriving compare, sexp_of] +[@@deriving compare, sexp] module Label = struct type 'a t = @@ -62,29 +59,41 @@ end module Labels = struct include Dmap.Make (Label) - let sexp_of_t m = - Sexp.List - (bindings m - |> List.map ~f:(function - | Binding (Iban_label tag, iban) -> - Sexp.List - [ - Sexp.Atom "Iban_label"; - [%sexp_of: iban_tag] tag; - [%sexp_of: Iban.t] iban; - ] - | Binding (String_label tag, s) -> - Sexp.List - [ - Sexp.Atom "String_label"; - [%sexp_of: string_tag] tag; - Sexp.Atom s; - ] - | Binding (Timestamp_label, ts) -> - Sexp.List - [ Sexp.Atom "Timestamp_label"; [%sexp_of: Time_ns_unix.t] ts ] - | Binding (Unit_label tag, ()) -> - Sexp.List [ Sexp.Atom "Unit_label"; [%sexp_of: unit_tag] tag ])) + let sexp_of_binding = function + | Binding (Iban_label tag, iban) -> + Sexp.List + [ + Sexp.Atom "iban"; [%sexp_of: iban_tag] tag; [%sexp_of: Iban.t] iban; + ] + | Binding (String_label tag, s) -> + Sexp.List + [ Sexp.Atom "string"; [%sexp_of: string_tag] tag; Sexp.Atom s ] + | Binding (Timestamp_label, ts) -> + Sexp.List [ Sexp.Atom "timestamp"; [%sexp_of: Time_ns_unix.t] ts ] + | Binding (Unit_label tag, ()) -> + Sexp.List [ Sexp.Atom "unit"; [%sexp_of: unit_tag] tag ] + + let binding_of_sexp sexp = + match sexp with + | Sexp.List [ Sexp.Atom "iban"; tag_sexp; iban_sexp ] -> + Binding + ( Iban_label ([%of_sexp: iban_tag] tag_sexp), + [%of_sexp: Iban.t] iban_sexp ) + | Sexp.List [ Sexp.Atom "string"; tag_sexp; Sexp.Atom s ] -> + Binding (String_label ([%of_sexp: string_tag] tag_sexp), s) + | Sexp.List [ Sexp.Atom "timestamp"; ts_sexp ] -> + Binding (Timestamp_label, [%of_sexp: Time_ns_unix.t] ts_sexp) + | Sexp.List [ Sexp.Atom "unit"; tag_sexp ] -> + Binding (Unit_label ([%of_sexp: unit_tag] tag_sexp), ()) + | _ -> of_sexp_error "Labels.binding_of_sexp: invalid binding" sexp + + let sexp_of_t m = Sexp.List (bindings m |> List.map ~f:sexp_of_binding) + + let t_of_sexp sexp = + match sexp with + | Sexp.List labels -> + Sequence.(of_list labels >>| binding_of_sexp |> to_seq) |> of_seq + | Sexp.Atom _ -> of_sexp_error "Labels.t_of_sexp: list needed" sexp end module Money : sig @@ -92,27 +101,33 @@ module Money : sig val equal : t -> t -> bool val compare : t -> t -> int - val of_z : Z.t -> t - val to_z : t -> Z.t + val of_bigint : Bigint.t -> t + val to_bigint : t -> Bigint.t val ( + ) : t -> t -> t val ( - ) : t -> t -> t + val ( = ) : t -> t -> bool + val ( ~$ ) : int -> t val sexp_of_t : t -> Sexp.t end = struct - type t = Z.t [@@deriving sexp_of] - - let equal = Z.equal - let compare = Z.compare - let of_z = Fn.id - let to_z = Fn.id - let ( + ) x y = Z.(x + y) - let ( - ) x y = Z.(x - y) -end + type t = Bigint.t [@@deriving sexp_of] -type scalar = Amount of Money.t | Rate of Z.t -[@@deriving equal, compare, sexp_of] + let equal = Bigint.equal + let compare = Bigint.compare + let of_bigint = Fn.id + let to_bigint = Fn.id + let ( + ) x y = Bigint.(x + y) + let ( - ) x y = Bigint.(x - y) + let ( = ) = equal + let ( ~$ ) = Fn.compose of_bigint Bigint.of_int +end type commodity_id = string -(* TODO: consider making this UUID *) [@@deriving sexp] +(* TODO: consider making this UUID *) [@@deriving equal, compare, sexp] + +type scalar = + | Amount of Money.t + | Rate of { in_primary_commodity : Money.t; rate : Bigdecimal.t } +[@@deriving equal, compare, sexp_of] module Account_id = struct type t = string list [@@deriving sexp, compare] @@ -135,13 +150,18 @@ type bal_assert = { module Account_id_map = Map.Make (Account_id) +module Debit_credit = struct + type t = Debit | Credit [@@deriving string, sexp_of] + + let opposite = function Debit -> Credit | Credit -> Debit +end + module Tx : sig (* 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; + entries : (Debit_credit.t * scalar * Money.t option) Account_id_map.t; labels : Labels.t; } @@ -150,8 +170,7 @@ module Tx : sig val make : cleared:Date.t option -> commodity_id:commodity_id -> - debit:scalar Account_id_map.t -> - credit:scalar Account_id_map.t -> + entries:(Debit_credit.t * scalar * Money.t option) Account_id_map.t -> labels:Labels.t -> (t, error) result @@ -160,23 +179,86 @@ end = struct type t = { cleared : Date.t option; commodity_id : commodity_id; - debit : scalar Account_id_map.t; - credit : scalar Account_id_map.t; + entries : (Debit_credit.t * scalar * Money.t option) Account_id_map.t; labels : Labels.t; } [@@deriving sexp_of] type error = Unbalanced - (* TODO: check if debits and credits are balanced *) - let is_balanced _debits _credits = true + let is_balanced entries = + Map.fold entries + ~init:Money.(~$0, ~$0) + ~f:(fun ~key:_ ~data:(type_, scalar, _oassert) (ds, cs) -> + let m = + match scalar with + | Amount m -> m + | Rate { in_primary_commodity = m; _ } -> m + in + match type_ with + | Debit_credit.Debit -> Money.(ds + m, cs) + | Debit_credit.Credit -> Money.(ds, cs + m)) + |> fun (ds, cs) -> Money.(ds = cs) - let make ~cleared ~commodity_id ~debit ~credit ~labels = - if not (is_balanced debit credit) then Error Unbalanced - else Ok { cleared; commodity_id; debit; credit; labels } + let make ~cleared ~commodity_id ~entries ~labels = + if not (is_balanced entries) then Error Unbalanced + else Ok { cleared; commodity_id; entries; labels } end -type item = Tx_item of Tx.t | Bal_assert_item of bal_assert +type item = + | Tx_item of Tx.t + | Bal_assert_item of bal_assert (*| Account_decl_item of account_decl*) [@@deriving sexp_of] type t = item list [@@deriving sexp_of] + +module Account = struct + type global_type = Asset | Equity | Liability | Expense | Income + [@@deriving compare, sexp] + + type asset + type global + + type 'a subcategory = + | Asset : asset subcategory option -> global subcategory + | Checking : asset subcategory + + type 'a t = Sub of ('a, 'a t) category String.Map.t + + let world : global t = + Sub + (String.Map.of_alist_exn [ ("Assets", Asset (Some ( + String.Map.of_alist_exn [ + ("Checking", Checking) + ] + ))) ]) +end + +(* +module World = struct + type t = (commodity_id * Money.t) Account_id_map.t + + let empty : t = Account_id_map.empty + + let apply_tx_entry_base aid primary_commodity debit_credit scalar = + let amount = Scalar.to_amount ~commodity:primary_commodity scalar in + Map.update aid ~f:(function + | None -> + + (* + let assert_bal aid sc world = + + let apply_tx_entry aid (dc, sc, oassert) world = *) + + let apply_tx (tx : Tx.t) world = + Map.fold tx.entries ~init:world ~f:(fun ~key:account_id ~data:(type_, scalar, _oassert) world -> + + + ) + + let apply : item -> t -> t = function + | Tx_item tx -> apply_tx tx + | Bal_assert_item ba -> apply_ba ba +end *) + +let make = Fn.id -- cgit v1.2.3