summaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/ledger.ml142
1 files changed, 49 insertions, 93 deletions
diff --git a/lib/ledger.ml b/lib/ledger.ml
index 7bb824c..115588a 100644
--- a/lib/ledger.ml
+++ b/lib/ledger.ml
@@ -253,103 +253,16 @@ module Account_structure0 = struct
253module Account_type = struct 253module Account_type = struct
254 type 'a elem = Leaf | Node of 'a [@@deriving sexp_of] 254 type 'a elem = Leaf | Node of 'a [@@deriving sexp_of]
255 255
256 include Account_structure0.Make (struct 256 module Scaffold = struct
257 type 'a t = 'a elem [@@deriving sexp_of] 257 type 'a t = 'a elem [@@deriving sexp_of]
258 end)
259end
260
261module Account_structure (F : Account_structure0.Scaffold) = struct
262 include Account_structure0.Make (F)
263
264 module Mapper = struct
265 type nonrec 'b t = {
266 car :
267 'a.
268 'a f F.t ->
269 ('a Account_type.f Account_type.elem -> Account_type.t0) ->
270 ('b * 'a f F.t) option;
271 }
272
273 let map (type b c) (f : c t) (mkt : b Account_type.f -> Account_type.t0) :
274 b f -> (c * b f) option =
275 let open Option.Let_syntax in
276 function
277 | Accounts_payable v ->
278 let%map c, v' =
279 f.car v (fun el -> mkt (Account_type.Accounts_payable el))
280 in
281 (c, Accounts_payable v')
282 | Accounts_receivable v ->
283 let%map c, v' =
284 f.car v (fun el -> mkt (Account_type.Accounts_receivable el))
285 in
286 (c, Accounts_receivable v')
287 | Bank v ->
288 let%map c, v' = f.car v (fun el -> mkt (Account_type.Bank el)) in
289 (c, Bank v')
290 | Cash v ->
291 let%map c, v' = f.car v (fun el -> mkt (Account_type.Cash el)) in
292 (c, Cash v')
293 | Credit v ->
294 let%map c, v' = f.car v (fun el -> mkt (Account_type.Credit el)) in
295 (c, Credit v')
296 | Mutual_fund v ->
297 let%map c, v' =
298 f.car v (fun el -> mkt (Account_type.Mutual_fund el))
299 in
300 (c, Mutual_fund v')
301 | Stock v ->
302 let%map c, v' = f.car v (fun el -> mkt (Account_type.Stock el)) in
303 (c, Stock v')
304 | Savings v ->
305 let%map c, v' = f.car v (fun el -> mkt (Account_type.Savings el)) in
306 (c, Savings v')
307 | Checking v ->
308 let%map c, v' = f.car v (fun el -> mkt (Account_type.Checking el)) in
309 (c, Checking v')
310 end
311
312 module Folder = struct
313 type nonrec 'b t = { car : 'a. 'a f F.t -> 'b }
314
315 let fold (type b c) (f : c t) : b f -> c = function
316 | Accounts_payable v -> f.car v
317 | Accounts_receivable v -> f.car v
318 | Bank v -> f.car v
319 | Cash v -> f.car v
320 | Credit v -> f.car v
321 | Mutual_fund v -> f.car v
322 | Stock v -> f.car v
323 | Savings v -> f.car v
324 | Checking v -> f.car v
325 end 258 end
326 259
327 module Folder2 = struct 260 include Account_structure0.Make (Scaffold)
328 type nonrec ('outer, 'acc) t = { 261end
329 car :
330 'inner.
331 'inner f F.t ->
332 (module Account_structure0.Gen_f_cons.S
333 with type inner = 'inner
334 and type outer = 'outer) ->
335 'acc;
336 }
337 262
338 let fold (type a acc) (f : (a, acc) t) : a f -> acc = function 263module Account_structure (F : Account_structure0.Scaffold) = struct
339 | Accounts_payable v -> 264 module Structure = Account_structure0.Make (F)
340 f.car v (module Account_structure0.Gen_f_cons.Accounts_payable) 265 include Structure
341 | _ -> failwith "kaas"
342 (*
343 | Accounts_receivable v ->
344 f.car v (fun inner -> H.Accounts_receivable inner)
345 | Bank v -> f.car v (fun inner -> H.Bank inner)
346 | Cash v -> f.car v (fun inner -> H.Cash inner)
347 | Credit v -> f.car v (fun inner -> H.Credit inner)
348 | Mutual_fund v -> f.car v (fun inner -> H.Mutual_fund inner)
349 | Stock v -> f.car v (fun inner -> H.Stock inner)
350 | Savings v -> f.car v (fun inner -> H.Savings inner)
351 | Checking v -> f.car v (fun inner -> H.Checking inner) *)
352 end
353 266
354 module Folder3 (Acc : sig 267 module Folder3 (Acc : sig
355 type 'a t 268 type 'a t
@@ -380,6 +293,49 @@ module Account_structure (F : Account_structure0.Scaffold) = struct
380 | Savings v -> f.car v (fun inner -> H.Savings inner) 293 | Savings v -> f.car v (fun inner -> H.Savings inner)
381 | Checking v -> f.car v (fun inner -> H.Checking inner) *) 294 | Checking v -> f.car v (fun inner -> H.Checking inner) *)
382 end 295 end
296
297 module Folder = struct
298 type nonrec 'b t = { car : 'a. 'a f F.t -> 'b }
299
300 let fold (type b c) (f : c t) : b f -> c =
301 let module Inst = Folder3 (struct
302 type 'a t = c
303 end) in
304 Inst.fold { car = (fun v _cons -> f.car v) }
305 end
306
307 module Mapper = struct
308 type nonrec 'b t = {
309 car :
310 'a.
311 'a f F.t ->
312 ('a Account_type.f Account_type.elem -> Account_type.t0) ->
313 ('b * 'a f F.t) option;
314 }
315
316 let map (type b c) (f : c t) (mkt : b Account_type.f -> Account_type.t0) :
317 b f -> (c * b f) option =
318 let module Inst = Folder3 (struct
319 type 'b t = (c * 'b f) option
320 end) in
321 Inst.fold
322 {
323 car =
324 (fun (type inner)
325 v
326 (module Gen_cons : Account_structure0.Gen_f_cons.S
327 with type inner = inner
328 and type outer = b)
329 ->
330 let open Option.Let_syntax in
331 let module Type_cons =
332 Gen_cons.Specialize (Account_type.Scaffold) (Account_type)
333 in
334 let module Own_cons = Gen_cons.Specialize (F) (Structure) in
335 let%map c, v' = f.car v (fun el -> mkt (Type_cons.cons el)) in
336 (c, Own_cons.cons v'));
337 }
338 end
383end 339end
384 340
385module Typed_account_path = struct 341module Typed_account_path = struct