forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcamlinternalFormat.ml
2976 lines (2745 loc) · 122 KB
/
camlinternalFormat.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
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Benoit Vaugon, ENSTA *)
(* *)
(* Copyright 2014 Institut National de Recherche en Informatique et *)
(* en Automatique. *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
open CamlinternalFormatBasics
(******************************************************************************)
(* Tools to manipulate scanning set of chars (see %[...]) *)
type mutable_char_set = bytes
(* Create a fresh, empty, mutable char set. *)
let create_char_set () = Bytes.make 32 '\000'
(* Add a char in a mutable char set. *)
let add_in_char_set char_set c =
let ind = int_of_char c in
let str_ind = ind lsr 3 and mask = 1 lsl (ind land 0b111) in
Bytes.set char_set str_ind
(char_of_int (int_of_char (Bytes.get char_set str_ind) lor mask))
let freeze_char_set char_set =
Bytes.to_string char_set
(* Compute the complement of a char set. *)
let rev_char_set char_set =
let char_set' = create_char_set () in
for i = 0 to 31 do
Bytes.set char_set' i
(char_of_int (int_of_char (String.get char_set i) lxor 0xFF));
done;
Bytes.unsafe_to_string char_set'
(* Return true if a `c' is in `char_set'. *)
let is_in_char_set char_set c =
let ind = int_of_char c in
let str_ind = ind lsr 3 and mask = 1 lsl (ind land 0b111) in
(int_of_char (String.get char_set str_ind) land mask) <> 0
(******************************************************************************)
(* Ignored param conversion *)
(* GADT used to abstract an existential type parameter. *)
(* See param_format_of_ignored_format. *)
type ('a, 'b, 'c, 'd, 'e, 'f) param_format_ebb = Param_format_EBB :
('x -> 'a, 'b, 'c, 'd, 'e, 'f) fmt ->
('a, 'b, 'c, 'd, 'e, 'f) param_format_ebb
(* Compute a padding associated to a pad_option (see "%_42d"). *)
let pad_of_pad_opt pad_opt = match pad_opt with
| None -> No_padding
| Some width -> Lit_padding (Right, width)
(* Compute a precision associated to a prec_option (see "%_.42f"). *)
let prec_of_prec_opt prec_opt = match prec_opt with
| None -> No_precision
| Some ndec -> Lit_precision ndec
(* Turn an ignored param into its equivalent not-ignored format node. *)
(* Used for format pretty-printing and Scanf. *)
let param_format_of_ignored_format : type a b c d e f x y .
(a, b, c, d, y, x) ignored -> (x, b, c, y, e, f) fmt ->
(a, b, c, d, e, f) param_format_ebb =
fun ign fmt -> match ign with
| Ignored_char ->
Param_format_EBB (Char fmt)
| Ignored_caml_char ->
Param_format_EBB (Caml_char fmt)
| Ignored_string pad_opt ->
Param_format_EBB (String (pad_of_pad_opt pad_opt, fmt))
| Ignored_caml_string pad_opt ->
Param_format_EBB (Caml_string (pad_of_pad_opt pad_opt, fmt))
| Ignored_int (iconv, pad_opt) ->
Param_format_EBB (Int (iconv, pad_of_pad_opt pad_opt, No_precision, fmt))
| Ignored_int32 (iconv, pad_opt) ->
Param_format_EBB
(Int32 (iconv, pad_of_pad_opt pad_opt, No_precision, fmt))
| Ignored_nativeint (iconv, pad_opt) ->
Param_format_EBB
(Nativeint (iconv, pad_of_pad_opt pad_opt, No_precision, fmt))
| Ignored_int64 (iconv, pad_opt) ->
Param_format_EBB
(Int64 (iconv, pad_of_pad_opt pad_opt, No_precision, fmt))
| Ignored_float (pad_opt, prec_opt) ->
Param_format_EBB
(Float (Float_f, pad_of_pad_opt pad_opt, prec_of_prec_opt prec_opt, fmt))
| Ignored_bool pad_opt ->
Param_format_EBB (Bool (pad_of_pad_opt pad_opt, fmt))
| Ignored_format_arg (pad_opt, fmtty) ->
Param_format_EBB (Format_arg (pad_opt, fmtty, fmt))
| Ignored_format_subst (pad_opt, fmtty) ->
Param_format_EBB
(Format_subst (pad_opt, fmtty, fmt))
| Ignored_reader ->
Param_format_EBB (Reader fmt)
| Ignored_scan_char_set (width_opt, char_set) ->
Param_format_EBB (Scan_char_set (width_opt, char_set, fmt))
| Ignored_scan_get_counter counter ->
Param_format_EBB (Scan_get_counter (counter, fmt))
| Ignored_scan_next_char ->
Param_format_EBB (Scan_next_char fmt)
(******************************************************************************)
(* Types *)
type ('b, 'c) acc_formatting_gen =
| Acc_open_tag of ('b, 'c) acc
| Acc_open_box of ('b, 'c) acc
(* Reversed list of printing atoms. *)
(* Used to accumulate printf arguments. *)
and ('b, 'c) acc =
| Acc_formatting_lit of ('b, 'c) acc * formatting_lit
(* Special fmtting (box) *)
| Acc_formatting_gen of ('b, 'c) acc * ('b, 'c) acc_formatting_gen
(* Special fmtting (box) *)
| Acc_string_literal of ('b, 'c) acc * string (* Literal string *)
| Acc_char_literal of ('b, 'c) acc * char (* Literal char *)
| Acc_data_string of ('b, 'c) acc * string (* Generated string *)
| Acc_data_char of ('b, 'c) acc * char (* Generated char *)
| Acc_delay of ('b, 'c) acc * ('b -> 'c)
(* Delayed printing (%a, %t) *)
| Acc_flush of ('b, 'c) acc (* Flush *)
| Acc_invalid_arg of ('b, 'c) acc * string
(* Raise Invalid_argument msg *)
| End_of_acc
(* List of heterogeneous values. *)
(* Used to accumulate scanf callback arguments. *)
type ('a, 'b) heter_list =
| Cons : 'c * ('a, 'b) heter_list -> ('c -> 'a, 'b) heter_list
| Nil : ('b, 'b) heter_list
(* Existential Black Boxes. *)
(* Used to abstract some existential type parameters. *)
(* GADT type associating a padding and an fmtty. *)
(* See the type_padding function. *)
type ('a, 'b, 'c, 'd, 'e, 'f) padding_fmtty_ebb = Padding_fmtty_EBB :
('x, 'y) padding * ('y, 'b, 'c, 'd, 'e, 'f) fmtty ->
('x, 'b, 'c, 'd, 'e, 'f) padding_fmtty_ebb
(* GADT type associating a padding, a precision and an fmtty. *)
(* See the type_padprec function. *)
type ('a, 'b, 'c, 'd, 'e, 'f) padprec_fmtty_ebb = Padprec_fmtty_EBB :
('x, 'y) padding * ('y, 'z) precision * ('z, 'b, 'c, 'd, 'e, 'f) fmtty ->
('x, 'b, 'c, 'd, 'e, 'f) padprec_fmtty_ebb
(* GADT type associating a padding and an fmt. *)
(* See make_padding_fmt_ebb and parse_format functions. *)
type ('a, 'b, 'c, 'e, 'f) padding_fmt_ebb = Padding_fmt_EBB :
(_, 'x -> 'a) padding *
('a, 'b, 'c, 'd, 'e, 'f) fmt ->
('x, 'b, 'c, 'e, 'f) padding_fmt_ebb
(* GADT type associating a precision and an fmt. *)
(* See make_precision_fmt_ebb and parse_format functions. *)
type ('a, 'b, 'c, 'e, 'f) precision_fmt_ebb = Precision_fmt_EBB :
(_, 'x -> 'a) precision *
('a, 'b, 'c, 'd, 'e, 'f) fmt ->
('x, 'b, 'c, 'e, 'f) precision_fmt_ebb
(* GADT type associating a padding, a precision and an fmt. *)
(* See make_padprec_fmt_ebb and parse_format functions. *)
type ('p, 'b, 'c, 'e, 'f) padprec_fmt_ebb = Padprec_fmt_EBB :
('x, 'y) padding * ('y, 'p -> 'a) precision *
('a, 'b, 'c, 'd, 'e, 'f) fmt ->
('p, 'b, 'c, 'e, 'f) padprec_fmt_ebb
(* Abstract the 'a and 'd parameters of an fmt. *)
(* Output type of the format parsing function. *)
type ('b, 'c, 'e, 'f) fmt_ebb = Fmt_EBB :
('a, 'b, 'c, 'd, 'e, 'f) fmt ->
('b, 'c, 'e, 'f) fmt_ebb
(* GADT type associating an fmtty and an fmt. *)
(* See the type_format_gen function. *)
type ('a, 'b, 'c, 'd, 'e, 'f) fmt_fmtty_ebb = Fmt_fmtty_EBB :
('a, 'b, 'c, 'd, 'y, 'x) fmt *
('x, 'b, 'c, 'y, 'e, 'f) fmtty ->
('a, 'b, 'c, 'd, 'e, 'f) fmt_fmtty_ebb
(* GADT type associating an fmtty and an fmt. *)
(* See the type_ignored_format_substitution function. *)
type ('a, 'b, 'c, 'd, 'e, 'f) fmtty_fmt_ebb = Fmtty_fmt_EBB :
('a, 'b, 'c, 'd, 'y, 'x) fmtty *
('x, 'b, 'c, 'y, 'e, 'f) fmt_fmtty_ebb ->
('a, 'b, 'c, 'd, 'e, 'f) fmtty_fmt_ebb
(* Abstract all fmtty type parameters. *)
(* Used to compare format types. *)
type fmtty_ebb = Fmtty_EBB : ('a, 'b, 'c, 'd, 'e, 'f) fmtty -> fmtty_ebb
(* Abstract all padding type parameters. *)
(* Used to compare paddings. *)
type padding_ebb = Padding_EBB : ('a, 'b) padding -> padding_ebb
(* Abstract all precision type parameters. *)
(* Used to compare precisions. *)
type precision_ebb = Precision_EBB : ('a, 'b) precision -> precision_ebb
(******************************************************************************)
(* Constants *)
(* Default precision for float printing. *)
let default_float_precision = -6
(* For %h and %H formats, a negative precision means "as many digits as
necessary". For the other FP formats, we take the absolute value
of the precision, hence 6 digits by default. *)
(******************************************************************************)
(* Externals *)
external format_float: string -> float -> string
= "caml_format_float"
external format_int: string -> int -> string
= "caml_format_int"
external format_int32: string -> int32 -> string
= "caml_int32_format"
external format_nativeint: string -> nativeint -> string
= "caml_nativeint_format"
external format_int64: string -> int64 -> string
= "caml_int64_format"
external hexstring_of_float: float -> int -> char -> string
= "caml_hexstring_of_float"
(******************************************************************************)
(* Tools to pretty-print formats *)
(* Type of extensible character buffers. *)
type buffer = {
mutable ind : int;
mutable bytes : bytes;
}
(* Create a fresh buffer. *)
let buffer_create init_size = { ind = 0; bytes = Bytes.create init_size }
(* Check size of the buffer and grow it if needed. *)
let buffer_check_size buf overhead =
let len = Bytes.length buf.bytes in
let min_len = buf.ind + overhead in
if min_len > len then (
let new_len = max (len * 2) min_len in
let new_str = Bytes.create new_len in
Bytes.blit buf.bytes 0 new_str 0 len;
buf.bytes <- new_str;
)
(* Add the character `c' to the buffer `buf'. *)
let buffer_add_char buf c =
buffer_check_size buf 1;
Bytes.set buf.bytes buf.ind c;
buf.ind <- buf.ind + 1
(* Add the string `s' to the buffer `buf'. *)
let buffer_add_string buf s =
let str_len = String.length s in
buffer_check_size buf str_len;
String.blit s 0 buf.bytes buf.ind str_len;
buf.ind <- buf.ind + str_len
(* Get the content of the buffer. *)
let buffer_contents buf =
Bytes.sub_string buf.bytes 0 buf.ind
(***)
(* Convert an integer conversion to char. *)
let char_of_iconv iconv = match iconv with
| Int_d | Int_pd | Int_sd -> 'd' | Int_i | Int_pi | Int_si -> 'i'
| Int_x | Int_Cx -> 'x' | Int_X | Int_CX -> 'X' | Int_o | Int_Co -> 'o'
| Int_u -> 'u'
(* Convert a float conversion to char. *)
let char_of_fconv fconv = match fconv with
| Float_f | Float_pf | Float_sf -> 'f' | Float_e | Float_pe | Float_se -> 'e'
| Float_E | Float_pE | Float_sE -> 'E' | Float_g | Float_pg | Float_sg -> 'g'
| Float_G | Float_pG | Float_sG -> 'G' | Float_F -> 'F'
| Float_h | Float_ph | Float_sh -> 'h' | Float_H | Float_pH | Float_sH -> 'H'
(* Convert a scanning counter to char. *)
let char_of_counter counter = match counter with
| Line_counter -> 'l'
| Char_counter -> 'n'
| Token_counter -> 'N'
(***)
(* Print a char_set in a buffer with the OCaml format lexical convention. *)
let bprint_char_set buf char_set =
let rec print_start set =
let is_alone c =
let before, after = Char.(chr (code c - 1), chr (code c + 1)) in
is_in_char_set set c
&& not (is_in_char_set set before && is_in_char_set set after) in
if is_alone ']' then buffer_add_char buf ']';
print_out set 1;
if is_alone '-' then buffer_add_char buf '-';
and print_out set i =
if i < 256 then
if is_in_char_set set (char_of_int i) then print_first set i
else print_out set (i + 1)
and print_first set i =
match char_of_int i with
| '\255' -> print_char buf 255;
| ']' | '-' -> print_out set (i + 1);
| _ -> print_second set (i + 1);
and print_second set i =
if is_in_char_set set (char_of_int i) then
match char_of_int i with
| '\255' ->
print_char buf 254;
print_char buf 255;
| ']' | '-' when not (is_in_char_set set (char_of_int (i + 1))) ->
print_char buf (i - 1);
print_out set (i + 1);
| _ when not (is_in_char_set set (char_of_int (i + 1))) ->
print_char buf (i - 1);
print_char buf i;
print_out set (i + 2);
| _ ->
print_in set (i - 1) (i + 2);
else (
print_char buf (i - 1);
print_out set (i + 1);
)
and print_in set i j =
if j = 256 || not (is_in_char_set set (char_of_int j)) then (
print_char buf i;
print_char buf (int_of_char '-');
print_char buf (j - 1);
if j < 256 then print_out set (j + 1);
) else
print_in set i (j + 1);
and print_char buf i = match char_of_int i with
| '%' -> buffer_add_char buf '%'; buffer_add_char buf '%';
| '@' -> buffer_add_char buf '%'; buffer_add_char buf '@';
| c -> buffer_add_char buf c;
in
buffer_add_char buf '[';
print_start (
if is_in_char_set char_set '\000'
then ( buffer_add_char buf '^'; rev_char_set char_set )
else char_set
);
buffer_add_char buf ']'
(***)
(* Print a padty in a buffer with the format-like syntax. *)
let bprint_padty buf padty = match padty with
| Left -> buffer_add_char buf '-'
| Right -> ()
| Zeros -> buffer_add_char buf '0'
(* Print the '_' of an ignored flag if needed. *)
let bprint_ignored_flag buf ign_flag =
if ign_flag then buffer_add_char buf '_'
(***)
let bprint_pad_opt buf pad_opt = match pad_opt with
| None -> ()
| Some width -> buffer_add_string buf (string_of_int width)
(***)
(* Print padding in a buffer with the format-like syntax. *)
let bprint_padding : type a b . buffer -> (a, b) padding -> unit =
fun buf pad -> match pad with
| No_padding -> ()
| Lit_padding (padty, n) ->
bprint_padty buf padty;
buffer_add_string buf (string_of_int n);
| Arg_padding padty ->
bprint_padty buf padty;
buffer_add_char buf '*'
(* Print precision in a buffer with the format-like syntax. *)
let bprint_precision : type a b . buffer -> (a, b) precision -> unit =
fun buf prec -> match prec with
| No_precision -> ()
| Lit_precision n ->
buffer_add_char buf '.';
buffer_add_string buf (string_of_int n);
| Arg_precision ->
buffer_add_string buf ".*"
(***)
(* Print the optional '+', ' ' or '#' associated to an int conversion. *)
let bprint_iconv_flag buf iconv = match iconv with
| Int_pd | Int_pi -> buffer_add_char buf '+'
| Int_sd | Int_si -> buffer_add_char buf ' '
| Int_Cx | Int_CX | Int_Co -> buffer_add_char buf '#'
| Int_d | Int_i | Int_x | Int_X | Int_o | Int_u -> ()
(* Print an complete int format in a buffer (ex: "%3.*d"). *)
let bprint_int_fmt buf ign_flag iconv pad prec =
buffer_add_char buf '%';
bprint_ignored_flag buf ign_flag;
bprint_iconv_flag buf iconv;
bprint_padding buf pad;
bprint_precision buf prec;
buffer_add_char buf (char_of_iconv iconv)
(* Print a complete int32, nativeint or int64 format in a buffer. *)
let bprint_altint_fmt buf ign_flag iconv pad prec c =
buffer_add_char buf '%';
bprint_ignored_flag buf ign_flag;
bprint_iconv_flag buf iconv;
bprint_padding buf pad;
bprint_precision buf prec;
buffer_add_char buf c;
buffer_add_char buf (char_of_iconv iconv)
(***)
(* Print the optional '+' associated to a float conversion. *)
let bprint_fconv_flag buf fconv = match fconv with
| Float_pf | Float_pe | Float_pE
| Float_pg | Float_pG | Float_ph | Float_pH ->
buffer_add_char buf '+'
| Float_sf | Float_se | Float_sE
| Float_sg | Float_sG | Float_sh | Float_sH ->
buffer_add_char buf ' '
| Float_f | Float_e | Float_E
| Float_g | Float_G | Float_F | Float_h | Float_H ->
()
(* Print a complete float format in a buffer (ex: "%+*.3f"). *)
let bprint_float_fmt buf ign_flag fconv pad prec =
buffer_add_char buf '%';
bprint_ignored_flag buf ign_flag;
bprint_fconv_flag buf fconv;
bprint_padding buf pad;
bprint_precision buf prec;
buffer_add_char buf (char_of_fconv fconv)
(* Compute the literal string representation of a formatting_lit. *)
(* Also used by Printf and Scanf where formatting is not interpreted. *)
let string_of_formatting_lit formatting_lit = match formatting_lit with
| Close_box -> "@]"
| Close_tag -> "@}"
| Break (str, _, _) -> str
| FFlush -> "@?"
| Force_newline -> "@\n"
| Flush_newline -> "@."
| Magic_size (str, _) -> str
| Escaped_at -> "@@"
| Escaped_percent -> "@%"
| Scan_indic c -> "@" ^ (String.make 1 c)
(* Compute the literal string representation of a formatting. *)
(* Also used by Printf and Scanf where formatting is not interpreted. *)
let string_of_formatting_gen : type a b c d e f .
(a, b, c, d, e, f) formatting_gen -> string =
fun formatting_gen -> match formatting_gen with
| Open_tag (Format (_, str)) -> str
| Open_box (Format (_, str)) -> str
(***)
(* Print a literal char in a buffer, escape '%' by "%%". *)
let bprint_char_literal buf chr = match chr with
| '%' -> buffer_add_string buf "%%"
| _ -> buffer_add_char buf chr
(* Print a literal string in a buffer, escape all '%' by "%%". *)
let bprint_string_literal buf str =
for i = 0 to String.length str - 1 do
bprint_char_literal buf str.[i]
done
(******************************************************************************)
(* Format pretty-printing *)
(* Print a complete format type (an fmtty) in a buffer. *)
let rec bprint_fmtty : type a b c d e f g h i j k l .
buffer -> (a, b, c, d, e, f, g, h, i, j, k, l) fmtty_rel -> unit =
fun buf fmtty -> match fmtty with
| Char_ty rest -> buffer_add_string buf "%c"; bprint_fmtty buf rest;
| String_ty rest -> buffer_add_string buf "%s"; bprint_fmtty buf rest;
| Int_ty rest -> buffer_add_string buf "%i"; bprint_fmtty buf rest;
| Int32_ty rest -> buffer_add_string buf "%li"; bprint_fmtty buf rest;
| Nativeint_ty rest -> buffer_add_string buf "%ni"; bprint_fmtty buf rest;
| Int64_ty rest -> buffer_add_string buf "%Li"; bprint_fmtty buf rest;
| Float_ty rest -> buffer_add_string buf "%f"; bprint_fmtty buf rest;
| Bool_ty rest -> buffer_add_string buf "%B"; bprint_fmtty buf rest;
| Alpha_ty rest -> buffer_add_string buf "%a"; bprint_fmtty buf rest;
| Theta_ty rest -> buffer_add_string buf "%t"; bprint_fmtty buf rest;
| Any_ty rest -> buffer_add_string buf "%?"; bprint_fmtty buf rest;
| Reader_ty rest -> buffer_add_string buf "%r"; bprint_fmtty buf rest;
| Ignored_reader_ty rest ->
buffer_add_string buf "%_r";
bprint_fmtty buf rest;
| Format_arg_ty (sub_fmtty, rest) ->
buffer_add_string buf "%{"; bprint_fmtty buf sub_fmtty;
buffer_add_string buf "%}"; bprint_fmtty buf rest;
| Format_subst_ty (sub_fmtty, _, rest) ->
buffer_add_string buf "%("; bprint_fmtty buf sub_fmtty;
buffer_add_string buf "%)"; bprint_fmtty buf rest;
| End_of_fmtty -> ()
(***)
let rec int_of_custom_arity : type a b c .
(a, b, c) custom_arity -> int =
function
| Custom_zero -> 0
| Custom_succ x -> 1 + int_of_custom_arity x
(* Print a complete format in a buffer. *)
let bprint_fmt buf fmt =
let rec fmtiter : type a b c d e f .
(a, b, c, d, e, f) fmt -> bool -> unit =
fun fmt ign_flag -> match fmt with
| String (pad, rest) ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
bprint_padding buf pad; buffer_add_char buf 's';
fmtiter rest false;
| Caml_string (pad, rest) ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
bprint_padding buf pad; buffer_add_char buf 'S';
fmtiter rest false;
| Int (iconv, pad, prec, rest) ->
bprint_int_fmt buf ign_flag iconv pad prec;
fmtiter rest false;
| Int32 (iconv, pad, prec, rest) ->
bprint_altint_fmt buf ign_flag iconv pad prec 'l';
fmtiter rest false;
| Nativeint (iconv, pad, prec, rest) ->
bprint_altint_fmt buf ign_flag iconv pad prec 'n';
fmtiter rest false;
| Int64 (iconv, pad, prec, rest) ->
bprint_altint_fmt buf ign_flag iconv pad prec 'L';
fmtiter rest false;
| Float (fconv, pad, prec, rest) ->
bprint_float_fmt buf ign_flag fconv pad prec;
fmtiter rest false;
| Char rest ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
buffer_add_char buf 'c'; fmtiter rest false;
| Caml_char rest ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
buffer_add_char buf 'C'; fmtiter rest false;
| Bool (pad, rest) ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
bprint_padding buf pad; buffer_add_char buf 'B';
fmtiter rest false;
| Alpha rest ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
buffer_add_char buf 'a'; fmtiter rest false;
| Theta rest ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
buffer_add_char buf 't'; fmtiter rest false;
| Custom (arity, _, rest) ->
for _i = 1 to int_of_custom_arity arity do
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
buffer_add_char buf '?';
done;
fmtiter rest false;
| Reader rest ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
buffer_add_char buf 'r'; fmtiter rest false;
| Flush rest ->
buffer_add_string buf "%!";
fmtiter rest ign_flag;
| String_literal (str, rest) ->
bprint_string_literal buf str;
fmtiter rest ign_flag;
| Char_literal (chr, rest) ->
bprint_char_literal buf chr;
fmtiter rest ign_flag;
| Format_arg (pad_opt, fmtty, rest) ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
bprint_pad_opt buf pad_opt; buffer_add_char buf '{';
bprint_fmtty buf fmtty; buffer_add_char buf '%'; buffer_add_char buf '}';
fmtiter rest false;
| Format_subst (pad_opt, fmtty, rest) ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
bprint_pad_opt buf pad_opt; buffer_add_char buf '(';
bprint_fmtty buf fmtty; buffer_add_char buf '%'; buffer_add_char buf ')';
fmtiter rest false;
| Scan_char_set (width_opt, char_set, rest) ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
bprint_pad_opt buf width_opt; bprint_char_set buf char_set;
fmtiter rest false;
| Scan_get_counter (counter, rest) ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
buffer_add_char buf (char_of_counter counter);
fmtiter rest false;
| Scan_next_char rest ->
buffer_add_char buf '%'; bprint_ignored_flag buf ign_flag;
bprint_string_literal buf "0c"; fmtiter rest false;
| Ignored_param (ign, rest) ->
let Param_format_EBB fmt' = param_format_of_ignored_format ign rest in
fmtiter fmt' true;
| Formatting_lit (fmting_lit, rest) ->
bprint_string_literal buf (string_of_formatting_lit fmting_lit);
fmtiter rest ign_flag;
| Formatting_gen (fmting_gen, rest) ->
bprint_string_literal buf "@{";
bprint_string_literal buf (string_of_formatting_gen fmting_gen);
fmtiter rest ign_flag;
| End_of_format -> ()
in fmtiter fmt false
(***)
(* Convert a format to string. *)
let string_of_fmt fmt =
let buf = buffer_create 16 in
bprint_fmt buf fmt;
buffer_contents buf
(******************************************************************************)
(* Type extraction *)
type (_, _) eq = Refl : ('a, 'a) eq
(* Invariant: this function is the identity on values.
In particular, if (ty1, ty2) have equal values, then
(trans (symm ty1) ty2) respects the 'trans' precondition. *)
let rec symm : type a1 b1 c1 d1 e1 f1 a2 b2 c2 d2 e2 f2 .
(a1, b1, c1, d1, e1, f1,
a2, b2, c2, d2, e2, f2) fmtty_rel
-> (a2, b2, c2, d2, e2, f2,
a1, b1, c1, d1, e1, f1) fmtty_rel
= function
| Char_ty rest -> Char_ty (symm rest)
| Int_ty rest -> Int_ty (symm rest)
| Int32_ty rest -> Int32_ty (symm rest)
| Int64_ty rest -> Int64_ty (symm rest)
| Nativeint_ty rest -> Nativeint_ty (symm rest)
| Float_ty rest -> Float_ty (symm rest)
| Bool_ty rest -> Bool_ty (symm rest)
| String_ty rest -> String_ty (symm rest)
| Theta_ty rest -> Theta_ty (symm rest)
| Alpha_ty rest -> Alpha_ty (symm rest)
| Any_ty rest -> Any_ty (symm rest)
| Reader_ty rest -> Reader_ty (symm rest)
| Ignored_reader_ty rest -> Ignored_reader_ty (symm rest)
| Format_arg_ty (ty, rest) ->
Format_arg_ty (ty, symm rest)
| Format_subst_ty (ty1, ty2, rest) ->
Format_subst_ty (ty2, ty1, symm rest)
| End_of_fmtty -> End_of_fmtty
let rec fmtty_rel_det : type a1 b c d1 e1 f1 a2 d2 e2 f2 .
(a1, b, c, d1, e1, f1,
a2, b, c, d2, e2, f2) fmtty_rel ->
((f1, f2) eq -> (a1, a2) eq)
* ((a1, a2) eq -> (f1, f2) eq)
* ((e1, e2) eq -> (d1, d2) eq)
* ((d1, d2) eq -> (e1, e2) eq)
= function
| End_of_fmtty ->
(fun Refl -> Refl),
(fun Refl -> Refl),
(fun Refl -> Refl),
(fun Refl -> Refl)
| Char_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| String_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Int_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Int32_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Int64_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Nativeint_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Float_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Bool_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Theta_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Alpha_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Any_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Reader_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
(fun Refl -> let Refl = ed Refl in Refl),
(fun Refl -> let Refl = de Refl in Refl)
| Ignored_reader_ty rest ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
(fun Refl -> let Refl = ed Refl in Refl),
(fun Refl -> let Refl = de Refl in Refl)
| Format_arg_ty (_ty, rest) ->
let fa, af, ed, de = fmtty_rel_det rest in
(fun Refl -> let Refl = fa Refl in Refl),
(fun Refl -> let Refl = af Refl in Refl),
ed, de
| Format_subst_ty (ty1, ty2, rest) ->
let fa, af, ed, de = fmtty_rel_det rest in
let ty = trans (symm ty1) ty2 in
let ag, ga, dj, jd = fmtty_rel_det ty in
(fun Refl -> let Refl = fa Refl in let Refl = ag Refl in Refl),
(fun Refl -> let Refl = ga Refl in let Refl = af Refl in Refl),
(fun Refl -> let Refl = ed Refl in let Refl = dj Refl in Refl),
(fun Refl -> let Refl = jd Refl in let Refl = de Refl in Refl)
(* Precondition: we assume that the two fmtty_rel arguments have equal
values (at possibly distinct types); this invariant comes from the way
fmtty_rel witnesses are produced by the type-checker
The code below uses (assert false) when this assumption is broken. The
code pattern is the following:
| Foo x, Foo y ->
(* case where indeed both values
start with constructor Foo *)
| Foo _, _
| _, Foo _ ->
(* different head constructors: broken precondition *)
assert false
*)
and trans : type
a1 b1 c1 d1 e1 f1
a2 b2 c2 d2 e2 f2
a3 b3 c3 d3 e3 f3
.
(a1, b1, c1, d1, e1, f1,
a2, b2, c2, d2, e2, f2) fmtty_rel
-> (a2, b2, c2, d2, e2, f2,
a3, b3, c3, d3, e3, f3) fmtty_rel
-> (a1, b1, c1, d1, e1, f1,
a3, b3, c3, d3, e3, f3) fmtty_rel
= fun ty1 ty2 -> match ty1, ty2 with
| Char_ty rest1, Char_ty rest2 -> Char_ty (trans rest1 rest2)
| String_ty rest1, String_ty rest2 -> String_ty (trans rest1 rest2)
| Bool_ty rest1, Bool_ty rest2 -> Bool_ty (trans rest1 rest2)
| Int_ty rest1, Int_ty rest2 -> Int_ty (trans rest1 rest2)
| Int32_ty rest1, Int32_ty rest2 -> Int32_ty (trans rest1 rest2)
| Int64_ty rest1, Int64_ty rest2 -> Int64_ty (trans rest1 rest2)
| Nativeint_ty rest1, Nativeint_ty rest2 -> Nativeint_ty (trans rest1 rest2)
| Float_ty rest1, Float_ty rest2 -> Float_ty (trans rest1 rest2)
| Alpha_ty rest1, Alpha_ty rest2 -> Alpha_ty (trans rest1 rest2)
| Alpha_ty _, _ -> assert false
| _, Alpha_ty _ -> assert false
| Theta_ty rest1, Theta_ty rest2 -> Theta_ty (trans rest1 rest2)
| Theta_ty _, _ -> assert false
| _, Theta_ty _ -> assert false
| Any_ty rest1, Any_ty rest2 -> Any_ty (trans rest1 rest2)
| Any_ty _, _ -> assert false
| _, Any_ty _ -> assert false
| Reader_ty rest1, Reader_ty rest2 -> Reader_ty (trans rest1 rest2)
| Reader_ty _, _ -> assert false
| _, Reader_ty _ -> assert false
| Ignored_reader_ty rest1, Ignored_reader_ty rest2 ->
Ignored_reader_ty (trans rest1 rest2)
| Ignored_reader_ty _, _ -> assert false
| _, Ignored_reader_ty _ -> assert false
| Format_arg_ty (ty1, rest1), Format_arg_ty (ty2, rest2) ->
Format_arg_ty (trans ty1 ty2, trans rest1 rest2)
| Format_arg_ty _, _ -> assert false
| _, Format_arg_ty _ -> assert false
| Format_subst_ty (ty11, ty12, rest1),
Format_subst_ty (ty21, ty22, rest2) ->
let ty = trans (symm ty12) ty21 in
let _, f2, _, f4 = fmtty_rel_det ty in
let Refl = f2 Refl in
let Refl = f4 Refl in
Format_subst_ty (ty11, ty22, trans rest1 rest2)
| Format_subst_ty _, _ -> assert false
| _, Format_subst_ty _ -> assert false
| End_of_fmtty, End_of_fmtty -> End_of_fmtty
| End_of_fmtty, _ -> assert false
| _, End_of_fmtty -> assert false
let rec fmtty_of_formatting_gen : type a b c d e f .
(a, b, c, d, e, f) formatting_gen ->
(a, b, c, d, e, f) fmtty =
fun formatting_gen -> match formatting_gen with
| Open_tag (Format (fmt, _)) -> fmtty_of_fmt fmt
| Open_box (Format (fmt, _)) -> fmtty_of_fmt fmt
(* Extract the type representation (an fmtty) of a format. *)
and fmtty_of_fmt : type a b c d e f .
(a, b, c, d, e, f) fmt -> (a, b, c, d, e, f) fmtty =
fun fmtty -> match fmtty with
| String (pad, rest) ->
fmtty_of_padding_fmtty pad (String_ty (fmtty_of_fmt rest))
| Caml_string (pad, rest) ->
fmtty_of_padding_fmtty pad (String_ty (fmtty_of_fmt rest))
| Int (_, pad, prec, rest) ->
let ty_rest = fmtty_of_fmt rest in
let prec_ty = fmtty_of_precision_fmtty prec (Int_ty ty_rest) in
fmtty_of_padding_fmtty pad prec_ty
| Int32 (_, pad, prec, rest) ->
let ty_rest = fmtty_of_fmt rest in
let prec_ty = fmtty_of_precision_fmtty prec (Int32_ty ty_rest) in
fmtty_of_padding_fmtty pad prec_ty
| Nativeint (_, pad, prec, rest) ->
let ty_rest = fmtty_of_fmt rest in
let prec_ty = fmtty_of_precision_fmtty prec (Nativeint_ty ty_rest) in
fmtty_of_padding_fmtty pad prec_ty
| Int64 (_, pad, prec, rest) ->
let ty_rest = fmtty_of_fmt rest in
let prec_ty = fmtty_of_precision_fmtty prec (Int64_ty ty_rest) in
fmtty_of_padding_fmtty pad prec_ty
| Float (_, pad, prec, rest) ->
let ty_rest = fmtty_of_fmt rest in
let prec_ty = fmtty_of_precision_fmtty prec (Float_ty ty_rest) in
fmtty_of_padding_fmtty pad prec_ty
| Char rest -> Char_ty (fmtty_of_fmt rest)
| Caml_char rest -> Char_ty (fmtty_of_fmt rest)
| Bool (pad, rest) -> fmtty_of_padding_fmtty pad (Bool_ty (fmtty_of_fmt rest))
| Alpha rest -> Alpha_ty (fmtty_of_fmt rest)
| Theta rest -> Theta_ty (fmtty_of_fmt rest)
| Custom (arity, _, rest) -> fmtty_of_custom arity (fmtty_of_fmt rest)
| Reader rest -> Reader_ty (fmtty_of_fmt rest)
| Format_arg (_, ty, rest) ->
Format_arg_ty (ty, fmtty_of_fmt rest)
| Format_subst (_, ty, rest) ->
Format_subst_ty (ty, ty, fmtty_of_fmt rest)
| Flush rest -> fmtty_of_fmt rest
| String_literal (_, rest) -> fmtty_of_fmt rest
| Char_literal (_, rest) -> fmtty_of_fmt rest
| Scan_char_set (_, _, rest) -> String_ty (fmtty_of_fmt rest)
| Scan_get_counter (_, rest) -> Int_ty (fmtty_of_fmt rest)
| Scan_next_char rest -> Char_ty (fmtty_of_fmt rest)
| Ignored_param (ign, rest) -> fmtty_of_ignored_format ign rest
| Formatting_lit (_, rest) -> fmtty_of_fmt rest
| Formatting_gen (fmting_gen, rest) ->
concat_fmtty (fmtty_of_formatting_gen fmting_gen) (fmtty_of_fmt rest)
| End_of_format -> End_of_fmtty
and fmtty_of_custom : type x y a b c d e f .
(a, x, y) custom_arity -> (a, b, c, d, e, f) fmtty ->
(y, b, c, d, e, f) fmtty =
fun arity fmtty -> match arity with
| Custom_zero -> fmtty
| Custom_succ arity -> Any_ty (fmtty_of_custom arity fmtty)
(* Extract the fmtty of an ignored parameter followed by the rest of
the format. *)
and fmtty_of_ignored_format : type x y a b c d e f .
(a, b, c, d, y, x) ignored ->
(x, b, c, y, e, f) fmt ->
(a, b, c, d, e, f) fmtty =
fun ign fmt -> match ign with
| Ignored_char -> fmtty_of_fmt fmt
| Ignored_caml_char -> fmtty_of_fmt fmt
| Ignored_string _ -> fmtty_of_fmt fmt
| Ignored_caml_string _ -> fmtty_of_fmt fmt
| Ignored_int (_, _) -> fmtty_of_fmt fmt
| Ignored_int32 (_, _) -> fmtty_of_fmt fmt
| Ignored_nativeint (_, _) -> fmtty_of_fmt fmt
| Ignored_int64 (_, _) -> fmtty_of_fmt fmt
| Ignored_float (_, _) -> fmtty_of_fmt fmt
| Ignored_bool _ -> fmtty_of_fmt fmt
| Ignored_format_arg _ -> fmtty_of_fmt fmt
| Ignored_format_subst (_, fmtty) -> concat_fmtty fmtty (fmtty_of_fmt fmt)
| Ignored_reader -> Ignored_reader_ty (fmtty_of_fmt fmt)
| Ignored_scan_char_set _ -> fmtty_of_fmt fmt
| Ignored_scan_get_counter _ -> fmtty_of_fmt fmt
| Ignored_scan_next_char -> fmtty_of_fmt fmt
(* Add an Int_ty node if padding is taken as an extra argument (ex: "%*s"). *)
and fmtty_of_padding_fmtty : type x a b c d e f .
(x, a) padding -> (a, b, c, d, e, f) fmtty -> (x, b, c, d, e, f) fmtty =
fun pad fmtty -> match pad with
| No_padding -> fmtty
| Lit_padding _ -> fmtty
| Arg_padding _ -> Int_ty fmtty
(* Add an Int_ty node if precision is taken as an extra argument (ex: "%.*f").*)
and fmtty_of_precision_fmtty : type x a b c d e f .
(x, a) precision -> (a, b, c, d, e, f) fmtty -> (x, b, c, d, e, f) fmtty =
fun prec fmtty -> match prec with
| No_precision -> fmtty
| Lit_precision _ -> fmtty
| Arg_precision -> Int_ty fmtty
(******************************************************************************)
(* Format typing *)
(* Exception raised when a format does not match a given format type. *)
exception Type_mismatch
(* Type a padding. *)
(* Take an Int_ty from the fmtty if the integer should be kept as argument. *)
(* Raise Type_mismatch in case of type mismatch. *)
let type_padding : type a b c d e f x y .
(x, y) padding -> (a, b, c, d, e, f) fmtty ->
(a, b, c, d, e, f) padding_fmtty_ebb =
fun pad fmtty -> match pad, fmtty with
| No_padding, _ -> Padding_fmtty_EBB (No_padding, fmtty)
| Lit_padding (padty, w), _ -> Padding_fmtty_EBB (Lit_padding (padty,w),fmtty)
| Arg_padding padty, Int_ty rest -> Padding_fmtty_EBB (Arg_padding padty,rest)
| _ -> raise Type_mismatch
(* Convert a (upadding, uprecision) to a (padding, precision). *)
(* Take one or two Int_ty from the fmtty if needed. *)
(* Raise Type_mismatch in case of type mismatch. *)
let type_padprec : type a b c d e f x y z .
(x, y) padding -> (y, z) precision -> (a, b, c, d, e, f) fmtty ->
(a, b, c, d, e, f) padprec_fmtty_ebb =
fun pad prec fmtty -> match prec, type_padding pad fmtty with
| No_precision, Padding_fmtty_EBB (pad, rest) ->
Padprec_fmtty_EBB (pad, No_precision, rest)
| Lit_precision p, Padding_fmtty_EBB (pad, rest) ->
Padprec_fmtty_EBB (pad, Lit_precision p, rest)
| Arg_precision, Padding_fmtty_EBB (pad, Int_ty rest) ->
Padprec_fmtty_EBB (pad, Arg_precision, rest)
| _, Padding_fmtty_EBB (_, _) -> raise Type_mismatch
(* Type a format according to an fmtty. *)
(* If typing succeed, generate a copy of the format with the same
type parameters as the fmtty. *)
(* Raise [Failure] with an error message in case of type mismatch. *)
let rec type_format :
type a1 b1 c1 d1 e1 f1
a2 b2 c2 d2 e2 f2 .
(a1, b1, c1, d1, e1, f1) fmt