package tezos-benchmark

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file model.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2022 Nomadic Labs. <contact@nomadic-labs.com>               *)
(*                                                                           *)
(* Permission is hereby granted, free of charge, to any person obtaining a   *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense,  *)
(* and/or sell copies of the Software, and to permit persons to whom the     *)
(* Software is furnished to do so, subject to the following conditions:      *)
(*                                                                           *)
(* The above copyright notice and this permission notice shall be included   *)
(* in all copies or substantial portions of the Software.                    *)
(*                                                                           *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,  *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL   *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING   *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER       *)
(* DEALINGS IN THE SOFTWARE.                                                 *)
(*                                                                           *)
(*****************************************************************************)

type (_, _, _) arity =
  | Zero_arity : ('elt, 'elt, unit) arity
  | Succ_arity : ('elt, 'b, 'a) arity -> ('elt, 'elt -> 'b, int * 'a) arity

let arity_0 = Zero_arity

let arity_1 = Succ_arity arity_0

let arity_2 = Succ_arity arity_1

let arity_3 = Succ_arity arity_2

module type Model_impl = sig
  type arg_type

  val name : Namespace.t

  val takes_saturation_reprs : bool

  module Def (X : Costlang.S) : sig
    type model_type

    val arity : (X.size, model_type, arg_type) arity

    val model : model_type X.repr
  end
end

module type Instantiated = sig
  type 'a repr

  type size

  type arg_type

  type model_type

  val arity : (size, model_type, arg_type) arity

  val model : arg_type -> size repr
end

type 'arg model = (module Model_impl with type arg_type = 'arg)

module type App = sig
  type t

  val applied : t
end

module type Applied = functor (X : Costlang.S) ->
  App with type t = X.size X.repr

type applied = (module Applied)

type packed_model = Model : _ model -> packed_model

type _ t =
  | Abstract : {conv : 'workload -> 'arg; model : 'arg model} -> 'workload t
  | Aggregate : {
      model : 'workload -> applied;
      sub_models : packed_model list;
    }
      -> 'workload t

let pp_packed_model ppf (Model model) =
  let module Model = (val model) in
  let module Pp = Model.Def (Costlang.Pp) in
  Format.fprintf ppf "@[<v2>%a:@ %s@]" Namespace.pp Model.name Pp.model

let pp ppf = function
  | Abstract {model; _} ->
      Format.fprintf ppf "@[<v2>Abstract@ %a@]" pp_packed_model (Model model)
  | Aggregate {sub_models; _} ->
      Format.fprintf
        ppf
        "@[<v2>Aggregate with submodels:@ @[%a@]@]"
        (Format.pp_print_list pp_packed_model)
        sub_models

let apply_model : 'arg -> 'arg model -> applied =
  fun (type e) (elim : e) ((module Impl) : e model) ->
   let module Applied (X : Costlang.S) = struct
     include Impl.Def (X)

     type t = X.size X.repr

     let rec apply :
         type a b c.
         (int -> c X.repr) -> (c, a, b) arity -> a X.repr -> b -> c X.repr =
      fun conv arity f arg ->
       match arity with
       | Zero_arity -> f
       | Succ_arity ar ->
           let arg, rest = arg in
           apply conv ar (X.app f (conv arg)) rest

     let applied = apply X.int arity model elim
   end in
   ((module Applied) : applied)

module Instantiate (X : Costlang.S) (M : Model_impl) :
  Instantiated
    with type 'a repr = 'a X.repr
     and type size = X.size
     and type arg_type = M.arg_type = struct
  type 'a repr = 'a X.repr

  type size = X.size

  include M
  include Def (X)

  let rec apply :
      type a b c.
      (int -> c X.repr) -> (c, a, b) arity -> a X.repr -> b -> c X.repr =
   fun conv arity f arg ->
    match arity with
    | Zero_arity -> f
    | Succ_arity ar ->
        let arg, rest = arg in
        apply conv ar (X.app f (conv arg)) rest

  let model elim = apply X.int arity model elim
end

let set_takes_saturation_reprs (type a) b ((module Model) : a model) : a model =
  let module Model' = struct
    include Model

    let takes_saturation_reprs = b
  end in
  (module Model')

let make ?(takes_saturation_reprs = false) ~conv model =
  let model = set_takes_saturation_reprs takes_saturation_reprs model in
  Abstract {conv; model}

let make_aggregated ~model ~sub_models = Aggregate {model; sub_models}

let apply model workload =
  match model with
  | Abstract {conv; model} -> apply_model (conv workload) model
  | Aggregate {model; _} -> model workload

let force_aggregated ~model =
  match model with
  | Aggregate _ -> model
  | Abstract {conv = _; model = model2} ->
      Aggregate {model = apply model; sub_models = [Model model2]}

let add_aggregated_models :
    ('w1 -> applied) -> ('w2 -> applied) -> 'w1 * 'w2 -> applied =
 fun m1 m2 (w1, w2) ->
  let (module M1) = m1 w1 in
  let (module M2) = m2 w2 in
  let module M (X : Costlang.S) = struct
    type t = X.size X.repr

    let applied =
      let (module M1 : App with type t = X.size X.repr) = (module M1 (X)) in
      let (module M2 : App with type t = X.size X.repr) = (module M2 (X)) in
      X.(M1.applied + M2.applied)
  end in
  (module M : Applied)

let add_model m1 m2 =
  let m1 = force_aggregated ~model:m1 in
  let m2 = force_aggregated ~model:m2 in
  match (m1, m2) with
  | ( Aggregate {model = m1; sub_models = l1},
      Aggregate {model = m2; sub_models = l2} ) ->
      Aggregate {model = add_aggregated_models m1 m2; sub_models = l1 @ l2}
  | _ -> assert false (* impossible *)

let precompose : type a b. (a -> b) -> b t -> a t =
 fun f model ->
  match model with
  | Abstract {conv; model} ->
      let conv x = conv (f x) in
      Abstract {conv; model}
  | Aggregate {model; sub_models} ->
      Aggregate {model = (fun x -> model (f x)); sub_models}

let get_free_variable_set (type a) (model : a model) =
  let module M = (val model) in
  let module T0 = Costlang.Fold_constants (Costlang.Free_variables) in
  let module T1 = Costlang.Beta_normalize (T0) in
  let module R = M.Def (T1) in
  T0.prj @@ T1.prj R.model

(* No workload application.  For [Aggregate _], only extract
   the free variables of the [sub_models].
*)
let get_free_variable_set_of_t =
  let get_free_variables_of_packed_model (Model (module Model) : packed_model) =
    let module M = Model.Def (Costlang.Free_variables) in
    M.model
  in
  function
  | Abstract {model; _} -> get_free_variables_of_packed_model (Model model)
  | Aggregate {sub_models; _} ->
      List.fold_left
        (fun acc packed_model ->
          Free_variable.Set.union acc
          @@ get_free_variables_of_packed_model packed_model)
        Free_variable.Set.empty
        sub_models

let get_free_variable_set_applied (type workload) (model : workload t)
    (workload : workload) =
  (* If a parameter is fixed to 0 in the workload data, the application
      of the workload can eliminate free variables multiplied
      by the parameter.

      The typical example is the intercept case where some parameters
      tend to be fixed to 0.  This may not work when the intercept point
      is not at "zero"s.

      It is unfortunate that we need to apply workload data to a model to
      know which variables can be optimized out.  We may be able to do it
      without workload, but it seems not an easy task.
  *)
  let applied = apply model workload in
  let module M = (val applied) in
  let module T0 = Costlang.Fold_constants (Costlang.Free_variables) in
  let module T1 = Costlang.Beta_normalize (T0) in
  let module R = M (T1) in
  T0.prj @@ T1.prj R.applied

(* -------------------------------------------------------------------------- *)
(* Commonly used models *)

let zero =
  let module M = struct
    type arg_type = unit

    let name = Namespace.root "zero"

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size

      let arity = arity_0

      let model = int 0
    end
  end in
  (module M : Model_impl with type arg_type = unit)

let unknown_const1 ~name ~const =
  let module M = struct
    type arg_type = unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size

      let arity = arity_0

      let model = free ~name:const
    end
  end in
  (module M : Model_impl with type arg_type = unit)

let unknown_const1_skip1 ~name ~const =
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model = lam ~name:"size" @@ fun (_ : size repr) -> free ~name:const
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let unknown_const1_skip2 ~name ~const =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun (_ : size repr) ->
        lam ~name:"size2" @@ fun (_ : size repr) -> free ~name:const
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let linear ~name ~coeff =
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model = lam ~name:"size" @@ fun size -> free ~name:coeff * size
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let affine ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size ->
        free ~name:intercept + (free ~name:coeff * size)
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let affine_offset ~name ~intercept ~coeff ~offset =
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size ->
        free ~name:intercept + (free ~name:coeff * sat_sub size (int offset))
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let quadratic ~name ~coeff =
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size -> free ~name:coeff * (size * size)
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let nlogn ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size ->
        free ~name:intercept + (free ~name:coeff * (size * log2 (int 1 + size)))
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let nsqrtn_const ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size ->
        free ~name:intercept + (free ~name:coeff * (size * sqrt size))
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let logn ~name ~coeff =
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size -> free ~name:coeff * log2 (int 1 + size)
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let linear_sum ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept + (free ~name:coeff * (size1 + size2))
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let linear_sat_sub ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept + (free ~name:coeff * sat_sub size1 size2)
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let linear_max ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept + (free ~name:coeff * max size1 size2)
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let linear_min ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept + (free ~name:coeff * min size1 size2)
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let linear_min_offset ~name ~intercept ~coeff ~offset =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept
        + (free ~name:coeff * sat_sub (min size1 size2) (int offset))
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let linear_mul ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept + (free ~name:coeff * (size1 * size2))
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let bilinear ~name ~coeff1 ~coeff2 =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        (free ~name:coeff1 * size1) + (free ~name:coeff2 * size2)
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let bilinear_affine ~name ~intercept ~coeff1 ~coeff2 =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept
        + (free ~name:coeff1 * size1)
        + (free ~name:coeff2 * size2)
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let affine_skip1 ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun (_size1 : size repr) ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept + (free ~name:coeff * size2)
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let nlogm ~name ~intercept ~coeff =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept
        + (free ~name:coeff * (size1 * log2 (int 1 + size2)))
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let n_plus_logm ~name ~intercept ~linear_coeff ~log_coeff =
  let module M = struct
    type arg_type = int * (int * unit)

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size

      let arity = arity_2

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        free ~name:intercept
        + (free ~name:linear_coeff * size1)
        + (free ~name:log_coeff * log2 (int 1 + size2))
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * unit))

let trilinear ~name ~coeff1 ~coeff2 ~coeff3 =
  let module M = struct
    type arg_type = int * (int * (int * unit))

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size -> size -> size

      let arity = arity_3

      let model =
        lam ~name:"size1" @@ fun size1 ->
        lam ~name:"size2" @@ fun size2 ->
        lam ~name:"size3" @@ fun size3 ->
        (free ~name:coeff1 * size1)
        + (free ~name:coeff2 * size2)
        + (free ~name:coeff3 * size3)
    end
  end in
  (module M : Model_impl with type arg_type = int * (int * (int * unit)))

let breakdown ~name ~coeff1 ~coeff2 ~break =
  assert (0 <= break) ;

  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size ->
        (free ~name:coeff1 * min (int break) size)
        + (free ~name:coeff2 * sat_sub size (int break))
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let breakdown2 ~name ~coeff1 ~coeff2 ~coeff3 ~break1 ~break2 =
  assert (0 <= break1 && break1 <= break2) ;
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size ->
        (free ~name:coeff1 * min (int break1) size)
        + (free ~name:coeff2 * sat_sub (min (int break2) size) (int break1))
        + (free ~name:coeff3 * sat_sub size (int break2))
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let breakdown2_const ~name ~coeff1 ~coeff2 ~coeff3 ~const ~break1 ~break2 =
  assert (0 <= break1 && break1 <= break2) ;
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size ->
        (free ~name:coeff1 * min (int break1) size)
        + (free ~name:coeff2 * sat_sub (min (int break2) size) (int break1))
        + (free ~name:coeff3 * sat_sub size (int break2))
        + free ~name:const
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

let breakdown2_const_offset ~name ~coeff1 ~coeff2 ~coeff3 ~const ~break1 ~break2
    ~offset =
  assert (0 <= break1 && break1 <= break2) ;
  let module M = struct
    type arg_type = int * unit

    let name = name

    let takes_saturation_reprs = false

    module Def (X : Costlang.S) = struct
      open X

      type model_type = size -> size

      let arity = arity_1

      let model =
        lam ~name:"size" @@ fun size ->
        let_ ~name:"size" (sat_sub size (int offset)) @@ fun size ->
        (free ~name:coeff1 * min (int break1) size)
        + (free ~name:coeff2 * sat_sub (min (int break2) size) (int break1))
        + (free ~name:coeff3 * sat_sub size (int break2))
        + free ~name:const
    end
  end in
  (module M : Model_impl with type arg_type = int * unit)

module type Binary_operation = sig
  module Def (X : Costlang.S) : sig
    val op : X.size X.repr -> X.size X.repr -> X.size X.repr
  end
end

module Synthesize
    (B : Binary_operation)
    (X : Model_impl)
    (Y : Model_impl with type arg_type = X.arg_type) (Names : sig
      val name : Namespace.t

      val x_label : string

      val y_label : string
    end) : Model_impl with type arg_type = X.arg_type = struct
  type arg_type = X.arg_type

  let name = Names.name

  (* Use X's configuration *)
  let takes_saturation_reprs = X.takes_saturation_reprs

  module Def (C : Costlang.S) = struct
    module Args = X.Def (Costlang.Arg_names)
    module BinOp = B.Def (C)
    module X = X.Def (C)
    module Y = Y.Def (C)

    type model_type = X.model_type

    let arity = X.arity

    let rec synthesize :
        type a args_model x_model y_model.
        (Costlang.Arg_names.size, args_model, a) arity ->
        args_model Costlang.Arg_names.repr ->
        (C.size, x_model, a) arity ->
        x_model C.repr ->
        (C.size, y_model, a) arity ->
        y_model C.repr ->
        x_model C.repr =
     fun arg_arity args arity1 term1 arity2 term2 ->
      match arg_arity with
      | Zero_arity ->
          (* These bindings of Zero_arity are necessary for type checking *)
          let Zero_arity = arity1 in
          let Zero_arity = arity2 in
          let open C in
          let_ ~name:Names.x_label term1 @@ fun term1 ->
          let_ ~name:Names.y_label term2 @@ fun term2 -> BinOp.op term1 term2
      | Succ_arity arg_arity ->
          let (Succ_arity arity1) = arity1 in
          let (Succ_arity arity2) = arity2 in
          let open C in
          lam ~name:(Costlang.Arg_names.arg_name args) @@ fun arg ->
          synthesize
            arg_arity
            (Costlang.Arg_names.unwrap_size args)
            arity1
            (app term1 arg)
            arity2
            (app term2 arg)

    let model = synthesize Args.arity Args.model X.arity X.model Y.arity Y.model
  end
end

let synthesize (type a) ~name ~binop ~x_label ~x_model ~y_label ~y_model =
  let (module B : Binary_operation) = binop in
  let ((module X) : a model) = x_model in
  let ((module Y) : a model) = y_model in
  let module M =
    Synthesize (B) (X) (Y)
      (struct
        let x_label = x_label

        let y_label = y_label

        let name = name
      end)
  in
  ((module M) : a model)
OCaml

Innovation. Community. Security.