diff options
Diffstat (limited to 'lib/money.ml')
| -rw-r--r-- | lib/money.ml | 79 |
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 @@ | |||
| 1 | open Prelude | ||
| 2 | |||
| 3 | type polarity = Increase_on_debit | Increase_on_credit | ||
| 4 | |||
| 5 | module Debit_credit = struct | ||
| 6 | type t = Debit | Credit [@@deriving string, sexp_of] | ||
| 7 | |||
| 8 | (* let opposite = function Debit -> Credit | Credit -> Debit *) | ||
| 9 | end | ||
| 10 | |||
| 11 | module 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 | ||
| 22 | end = 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 | ||
| 32 | end | ||
| 33 | |||
| 34 | module 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 | ||
| 50 | end = 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)) | ||
| 71 | end | ||
| 72 | |||
| 73 | module 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) | ||
| 79 | end | ||