summaryrefslogtreecommitdiffstats
path: root/lib/money.ml
diff options
context:
space:
mode:
authorRutger Broekhoff2026-03-09 22:29:18 +0100
committerRutger Broekhoff2026-03-09 22:29:18 +0100
commitc90ff5253efd858a2bf0c20eaa2ee9763a402783 (patch)
treed1c5ab837ece7034d882368f1beeeb56b934ac4d /lib/money.ml
parent2f94997e2befc70ada84bd04a56831efe2747220 (diff)
downloadrdcapsis-c90ff5253efd858a2bf0c20eaa2ee9763a402783.tar.gz
rdcapsis-c90ff5253efd858a2bf0c20eaa2ee9763a402783.zip
oha!
Diffstat (limited to 'lib/money.ml')
-rw-r--r--lib/money.ml79
1 files changed, 79 insertions, 0 deletions
diff --git a/lib/money.ml b/lib/money.ml
new file mode 100644
index 0000000..a06af64
--- /dev/null
+++ b/lib/money.ml
@@ -0,0 +1,79 @@
1open Prelude
2
3type polarity = Increase_on_debit | Increase_on_credit
4
5module Debit_credit = struct
6 type t = Debit | Credit [@@deriving string, sexp_of]
7
8 (* let opposite = function Debit -> Credit | Credit -> Debit *)
9end
10
11module Amount : sig
12 type t
13
14 val equal : t -> t -> bool
15 val compare : t -> t -> int
16 val of_bigint : Bigint.t -> t option
17 val to_bigint : t -> Bigint.t
18 val ( + ) : t -> t -> t
19 val ( = ) : t -> t -> bool
20 val sexp_of_t : t -> Sexp.t
21 val zero : t
22end = struct
23 type t = Bigint.t [@@deriving sexp_of]
24
25 let equal = Bigint.equal
26 let compare = Bigint.compare
27 let of_bigint x = if Bigint.(zero <= x) then Some x else None
28 let to_bigint x = x
29 let ( + ) x y = Bigint.(x + y)
30 let ( = ) = equal
31 let zero = Bigint.zero
32end
33
34module Diff : sig
35 type t
36
37 val equal : t -> t -> bool
38 val compare : t -> t -> int
39 val of_bigint : Bigint.t -> t
40 val to_bigint : t -> Bigint.t
41 val ( + ) : t -> t -> t
42 val ( +% ) : t -> Amount.t -> t
43 val ( - ) : t -> t -> t
44 val ( -% ) : t -> Amount.t -> t
45 val ( = ) : t -> t -> bool
46 val neg : t -> t
47 val ( ~$ ) : int -> t
48 val sexp_of_t : t -> Sexp.t
49 val of_amount : Amount.t -> Debit_credit.t -> polarity -> t
50end = struct
51 type t = Bigint.t [@@deriving sexp_of]
52
53 let equal = Bigint.equal
54 let compare = Bigint.compare
55 let of_bigint x = x
56 let to_bigint x = x
57 let ( + ) x y = Bigint.(x + y)
58 let ( +% ) x y = x + of_bigint (Amount.to_bigint y)
59 let ( - ) x y = Bigint.(x - y)
60 let ( -% ) x y = x - of_bigint (Amount.to_bigint y)
61 let ( = ) = equal
62 let neg = Bigint.neg
63 let ( ~$ ) = Fn.compose of_bigint Bigint.of_int
64
65 let of_amount x (dc : Debit_credit.t) (on_debit : polarity) =
66 match (dc, on_debit) with
67 | Debit, Increase_on_debit -> of_bigint (Amount.to_bigint x)
68 | Credit, Increase_on_debit -> neg (of_bigint (Amount.to_bigint x))
69 | Credit, Increase_on_credit -> of_bigint (Amount.to_bigint x)
70 | Debit, Increase_on_credit -> neg (of_bigint (Amount.to_bigint x))
71end
72
73module Commodity_id = struct
74 type t = string [@@deriving equal, compare, sexp]
75
76 module Map = Map.Make (struct
77 type nonrec t = t [@@deriving equal, compare, sexp]
78 end)
79end