Skip to content
This repository
tree: f3fef854bc
Fetching contributors…

Cannot retrieve contributors at this time

executable file 1009 lines (913 sloc) 47.537 kb
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 1001 1002 1003 1004 1005 1006 1007 1008 1009
(*
Copyright © 2011 MLstate

This file is part of OPA.

OPA is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero General Public License, version 3, as published by
the Free Software Foundation.

OPA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
more details.

You should have received a copy of the GNU Affero General Public License
along with OPA. If not, see <http://www.gnu.org/licenses/>.
*)
(*
@author François Pessaux
*)



(* depends *)
module Format = Base.Format



let type_variables_counter = ref 0
let row_variables_counter = ref 0
let column_variables_counter = ref 0



(* ************************************************************************** *)
(** {b Descr}: List of abbreviations assigned to type during pretty-print of
a type. This list is filled and cleared by side effect.
Abbreviations are reminded to finally, at the end of a scheme, type or
sequence printing be "explained". In other words, once a single type, a
type scheme or a sequence of types are printed, all the abbreviation are
displayed giving their name and the type they represent. "Explaining" them
at the end allows to factorize them and have all abbreviations for all
types printed once instead of havign them printed for each type they appear
in.
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
let abbreviations = ref ([] : W_Algebra.simple_type list)



(* ************************************************************************** *)
(** {b Descr}: Lists of types, row variables and column variables encountered
during pretty-print in which the markers [W_Algebra.TM_print_final_abbrevd]
and [W_Algebra.VM_print_final_abbrevd] must be changed in place by
[W_Algebra.TM_print_sequence_abbrevd] and
[W_Algebra.VM_print_sequence_abbrevd].
These lists are filled and cleared by side effect.
See also [toggle_final_abbrevd_for_sequence] to get an explanation about
the reason of this and how it works.
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
let tys_to_reprint_if_sequence = ref ([] : W_Algebra.simple_type list)
let row_vars_to_reprint_if_sequence = ref ([] : W_Algebra.row_variable list)
let col_vars_to_reprint_if_sequence = ref ([] : W_Algebra.column_variable list)



(* ************************************************************************** *)
(* int_to_base_26 : int -> string *)
(** {b Descr}: Transforms an integer to a string compound of only a-z chars.
This is used to write variables names. In fact, that only an
integer->base 26 printer. *)
(* ************************************************************************** *)
let rec int_to_base_26 i =
  if i >= 26 then
    let ch = (i mod 26) + (Char.code 'a') in
    (int_to_base_26 (i / 26)) ^ Char.escaped (Char.chr ch)
  else
    let ch = (i mod 26) + (Char.code 'a') in
    Char.escaped (Char.chr ch)



let create_type_variable_name _debug_var_desc =
  (* <---------- DEBUG
let debug_name =
(match _debug_var_desc with
| W_Algebra.SType_var bla ->
"(id:" ^ (string_of_int bla.W_Algebra.tv_debug_count) ^ ")" ^
"(qml:" ^ (QmlAst.TypeVar.to_string bla.W_Algebra.tv_qml) ^ ")" ^
(if bla.W_Algebra.tv_level = W_CoreTypes.generic_binding_level then
"GEN"
else ("_lev:" ^ (string_of_int bla.W_Algebra.tv_level)))
| _ -> "(other)") in
<---------- END DEBUG *)
  let name =
    (* <---------- DEBUG
debug_name ^
<---------- END DEBUG *)
    "'" ^ (int_to_base_26 !type_variables_counter) in
  incr type_variables_counter ;
  name



let create_row_variable_name _is_generalized =
  let name =
(* (if is_generalized then "" else "_") ^ *)
    "'r." ^ (int_to_base_26 !row_variables_counter) in
  incr row_variables_counter ;
  name



let create_column_variable_name _is_generalized =
  let name =
(* (if is_generalized then "" else "_") ^ *)
    "'c." ^ (int_to_base_26 !column_variables_counter) in
  incr column_variables_counter ;
  name



(* ************************************************************************** *)
(** {b Descr}: Function that prepares the final pretty-printing of a
[simple_type]. Since type algebra allows cyclic types, we must make a first
pass to detect cycles and give them a name. Hence this pass descends on the
type and if a type appears to be already seen in a recursive context, it
creates an abbreviation ("abbrev") for it, i.e. a nickname that will be used
to represent this type everywhere it appears instead of going on printing
it by descending on its structure. This especially prevents pretty-printing
from looping and make types representation more compact.
So, each time a type is seen as recursive, an abbreviation is created and
this type is marked as [TM_print_final_abbrevd] with this abbreviation
reminded. Note that type variables are labeled using the same marking
mechanism.
This function **must** then be called before invoking the "real" final
pretty-print routine.
Note that since this function modifies the types markers, types must be
cleaned up after the final pretty-print routine call.
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
let rec __prepare_print_simple_type ty =
  let ty = W_CoreTypes.simple_type_repr ty in
  match ty.W_Algebra.sty_mark with
  | W_Algebra.TM_print_sequence_abbrevd _ ->
      (* Can occur if we are printing several types in sequence to keep sharing
between visible. *)
      ()
  | W_Algebra.TM_print_prep_seen xtimes ->
      (* Increment the number of times the type was seen. *)
      incr xtimes
  | W_Algebra.TM_not_seen | W_Algebra.TM_print_prep_seen_not_rec-> (
      (* The type was never seen of just seen but as non-recursive. We will
tell that temporarily, if it seen without telling if it is
recursive. *)
      ty.W_Algebra.sty_mark <- W_Algebra.TM_print_prep_seen (ref 1) ;
      (* We now descend on it structure. If at the return of the sub-term it
appears that the type is marked [TM_print_prep_seen] more than 1,
then this means that this type appears in its sub-terms, and hence is
recursive. *)
      (match ty.W_Algebra.sty_desc with
       | W_Algebra.SType_var _ -> ()
       | W_Algebra.SType_arrow (args_tys, res_ty) ->
           List.iter __prepare_print_simple_type args_tys ;
           __prepare_print_simple_type res_ty
       | W_Algebra.SType_named named ->
           (* We never print the real representation of a named type. *)
           (* <---------- DEBUG
(match named.W_Algebra.nst_unwinded with
| None -> ()
| Some t -> __prepare_print_simple_type t) ;
<---------- END DEBUG *)
           List.iter __prepare_print_simple_type named.W_Algebra.nst_args
       | W_Algebra.SType_sum_of_records sumcases_col ->
           __prepare_print_column_type sumcases_col
       | W_Algebra.SType_forall scheme ->
           __prepare_print_scheme_parameters
             (scheme.W_Algebra.ty_parameters, scheme.W_Algebra.row_parameters,
              scheme.W_Algebra.column_parameters) ;
           __prepare_print_simple_type scheme.W_Algebra.body
      ) ;
      (* Now we check if the current type is recursive, i.e if it appeared in
the subtree below it. If not, then we don't need to alias is with a
"where" construction and we turn back its marker as "seen but non
recursive" (i.e. [TM_print_prep_seen_not_rec]). *)
      (match ty.W_Algebra.sty_mark with
       | W_Algebra.TM_print_prep_seen xtimes' ->
           if !xtimes' = 1 then
             ty.W_Algebra.sty_mark <- W_Algebra.TM_print_prep_seen_not_rec
       | _ ->
           (* At least, the type must be marker as [TM_print_prep_seen] with
1, since this is at what we initialized its marker just before
starting descending in its sub-term. *)
           assert false)
    )
  | _ (* Other markers. *) -> assert false



(* ************************************************************************** *)
(** {b Descr}: Function that prepares the final pretty-printing of a [row_type].
For more information, read the comment in the header of the function
[__prepare_print_simple_type].
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
and __prepare_print_column_type column =
  (* First, get the canonical representation of the column. *)
  let (col_records, col_ending) =
    (W_CoreTypes.column_type_repr column).W_Algebra.ct_value in
  (match col_ending with
   | W_Algebra.Var_column v ->
       (match v.W_Algebra.cv_mark with
        | W_Algebra.VM_print_prep_seen xtimes -> incr xtimes
        | W_Algebra.VM_print_sequence_abbrevd _ ->
            (* Can occur if we are printing several types in sequence to keep
sharing between visible. *)
            ()
        | W_Algebra.VM_not_seen
        | W_Algebra.VM_print_prep_seen_not_rec ->
            (* By side effect, mark the column as seen once from now. *)
            v.W_Algebra.cv_mark <- W_Algebra.VM_print_prep_seen (ref 1)
        | _ (* Other markers. *) -> assert false)
   | W_Algebra.Closed_column -> ()) ;
  (* Recurse the inspection in each record forming the cases of the sum. *)
  List.iter __prepare_print_row_type col_records ;
  (* Now, revert the marker to tell we saw this column ending but not in a
recursive context. If the marker indicates that this variable was already
abbreviated for sequence print, then we don't touch it to prevent sharing
of the name from being broken between all the sequentially displayed
columns. *)
  (match col_ending with
   | W_Algebra.Var_column v -> (
       match v.W_Algebra.cv_mark with
       | W_Algebra.VM_print_sequence_abbrevd _ -> ()
       | W_Algebra.VM_print_prep_seen xtimes' ->
           if !xtimes' = 1 then
             v.W_Algebra.cv_mark <- W_Algebra.VM_print_prep_seen_not_rec
       | _ -> assert false
     )
   | _ -> ())



(* ************************************************************************** *)
(** {b Descr}: Function that prepares the final pretty-printing of a
[column_type].
For more information, read the comment in the header of the function
[__prepare_print_simple_type].
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
and __prepare_print_row_type row =
  (* First, get the canonical representation of the row. *)
  let (row_fields, row_ending) =
    (W_CoreTypes.row_type_repr row).W_Algebra.rt_value in
  (* Since row are well-sorted, a row variable is always preceded by the
same set of elements. Hence, if we already saw it ending variable, we have
also already seen its preceding elements and there is no need to explore
the whole row again. *)
  (match row_ending with
   | W_Algebra.Var_row v ->
       (match v.W_Algebra.rv_mark with
        | W_Algebra.VM_print_prep_seen xtimes -> incr xtimes
        | W_Algebra.VM_print_sequence_abbrevd _ ->
            (* Can occur if we are printing several types in sequence to keep
sharing between visible. *)
            ()
        | W_Algebra.VM_not_seen
        | W_Algebra.VM_print_prep_seen_not_rec ->
            (* By side effect, mark the row as seen once from now. *)
            v.W_Algebra.rv_mark <- W_Algebra.VM_print_prep_seen (ref 1)
        | _ (* Other markers. *) -> assert false)
   | W_Algebra.Closed_row -> ()) ;
  (* Recurse the inspection in each field of the row. *)
  List.iter
    (fun (_, field_ty) -> __prepare_print_simple_type field_ty) row_fields ;
  (* Now, revert the marker to tell we saw this row ending but not in a
recursive context. If the marker indicates that this variable was already
abbreviated for sequence print, then we don't touch it to prevent sharing
of the name from being broken between all the sequentially displayed
rows. *)
  (match row_ending with
   | W_Algebra.Var_row v -> (
       match v.W_Algebra.rv_mark with
       | W_Algebra.VM_print_sequence_abbrevd _ -> ()
       | W_Algebra.VM_print_prep_seen xtimes' ->
           if !xtimes' = 1 then
             v.W_Algebra.rv_mark <- W_Algebra.VM_print_prep_seen_not_rec
       | _ -> assert false
     )
   | _ -> ())



(* ************************************************************************** *)
(** {b Descr}: Function that prepares the final pretty-printing of a
[type_scheme].
For more information, read the comment in the header of the function
[__prepare_print_simple_type].
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
and __prepare_print_scheme_parameters
    (ty_parameters, row_parameters, column_parameters) =
  (* Important: start by taking care of the scheme parameter in case some
variables of the scheme do not appear in the scheme's body (pathological
case like type ('a) t = int) ! In effect, in this case, only descending on
the scheme's body won't "see" the variables since they do not appear in
its body, then assertions will fail during the "final" pretty-print
routine call. *)
  List.iter __prepare_print_simple_type ty_parameters ;
  List.iter
    (fun row_var ->
       (* Set the variable's marker as [VM_print_prep_seen_not_rec] to tell
that we saw the variable, but actually not in a recursive context. *)
       row_var.W_Algebra.rv_mark <- W_Algebra.VM_print_prep_seen_not_rec)
    row_parameters ;
  (* Do the same thing for column variables. *)
  List.iter
    (fun col_var ->
       col_var.W_Algebra.cv_mark <- W_Algebra.VM_print_prep_seen_not_rec)
    column_parameters



(* ************************************************************************** *)
(** {b Descr}: Function that changes in place the markers
[W_Algebra.TM_print_final_abbrevd] of types, and
[W_Algebra.VM_print_final_abbrevd] of row variables and column variables in
the lists received as arguments into the marker
[W_Algebra.TM_print_sequence_abbrevd] and
[W_Algebra.VM_print_sequence_abbrevd] keeping the abbreviation string
unchanged.
This is used to make so that when printing several types in sequence, each
time we print a new type in this sequence, even if it was previously
printed in the sequence, then instead of using its abbreviation that was
possibly created, the first occurrence of this type won't use it and will
be printed normally. However, if the type appears several time during this
print step, then next occurrences will be printed using the abbreviation,
which is the *same* than the one used in previous prints of the sequence.
This way, identity sharing between several prints of types is kept and each
individual printed type will expose its structure.
In other words, this avoids the behaviour like:
"
Types { A ; ~a } and { B ; ~b } are incompatible.
Bla bla { <~a> } and { < ~b> } bla bla bla.
"
that occured before because while printing the first message, abbreviations
were created for the types, and then when printing the second message, the
printing routine directly found these abbreviations hence used them to
represent the types instead of showing again their structure.
Now the messages look like:
"
Types { A ; ~a } and { B ; ~b } are incompatible.
Bla bla { A ; ~a } and { B ; ~b } bla bla bla.
"
clearly showing again the form of the types, but still keeping the variables
~a and ~b shared between the messages.
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
let toggle_final_abbrevd_for_sequence in_tys in_row_vars in_col_vars =
  List.iter
    (fun t ->
      match t.W_Algebra.sty_mark with
      | W_Algebra.TM_print_final_abbrevd abb_name ->
          t.W_Algebra.sty_mark <-
            W_Algebra.TM_print_sequence_abbrevd abb_name
      | _ -> ())
    in_tys ;
  List.iter
    (fun v ->
      match v.W_Algebra.rv_mark with
      | W_Algebra.VM_print_final_abbrevd abb_name ->
          v.W_Algebra.rv_mark <- W_Algebra.VM_print_sequence_abbrevd abb_name
      | _ -> ())
    in_row_vars ;
  List.iter
    (fun v ->
      match v.W_Algebra.cv_mark with
      | W_Algebra.VM_print_final_abbrevd abb_name ->
          v.W_Algebra.cv_mark <- W_Algebra.VM_print_sequence_abbrevd abb_name
      | _ -> ())
    in_col_vars



(* ************************************************************************** *)
(** {b Descr} : Tests if the type [ty] is "void", i.e "{}", in other words
a closed sum with one unique case being the empty closed record type.
ATTENTION: this function is only intended to be used to lighten the
pretty print of types, avoiding to write ": { }" for the type of record
fields that are of type void. For instance, this allows to print
{ a : int ; b }
instead of
{ a : int ; b : { } }
ATTENTION: This function assumes that [W_CoreTypes.xxx_type_repr] was
already called on [ty] since it is used by [__pp_fields_of_row]. Do not
try to use it in other and more general contexts, this may give
unpredictable results.

{b Args} :
- [ty] : The type to test an determines if it represents "void".

{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
let is_void ty =
  match ty.W_Algebra.sty_desc with
  | W_Algebra.SType_sum_of_records
        { W_Algebra.ct_value = ([uniq], W_Algebra.Closed_column) } -> (
      match uniq with
      | { W_Algebra.rt_value = ([], W_Algebra.Closed_row) } -> true
      | _ -> false
    )
  | _ -> false



let rec __pp_simple_type prio ppf ty =
  (* Morally, no need to "repr" since this has already been done by
[__prepare_print_simple_type], but... *)
  let ty = W_CoreTypes.simple_type_repr ty in
  match ty.W_Algebra.sty_mark with
  | W_Algebra.TM_print_final_abbrevd abb_name ->
      Format.fprintf ppf "%s" abb_name
  | W_Algebra.TM_print_sequence_abbrevd _
  | W_Algebra.TM_print_prep_seen_not_rec
  | W_Algebra.TM_print_prep_seen _ -> (
      (* If the type is seen recursive, we introduce an abbrev and turn the
type's marker as "abbreviated". If the type is a variable, then we
generate a variable name, and turn the type's marker as abbreviated
... to this variable name.
Otherwise, we simply descend printing on the type's structure. *)
      let (need_abbrev, register_abbrev) =
        (match ty.W_Algebra.sty_mark with
         | W_Algebra.TM_print_sequence_abbrevd abb_name -> (
             (* Now that the type will be re-print once for this new printing
sequence step, we can ask for it to be printed next as its
abbreviation. *)
             ty.W_Algebra.sty_mark <-
               W_Algebra.TM_print_final_abbrevd abb_name ;
             tys_to_reprint_if_sequence := ty :: !tys_to_reprint_if_sequence ;
             (* Handle the special case where the type is a variable. Since
this variable already has an abbrev, we don't want to create
another one. However, since we say we don't need abbrev, we will
descent in the type structure to print it and this descent
doesn't print variables since they are expected to be handled
and printed by assigning them an abbrev the first time they are
encountered. *)
             (match ty.W_Algebra.sty_desc with
              | W_Algebra.SType_var _ -> Format.fprintf ppf "%s" abb_name
              | _ -> ()) ;
             (false, false)
           )
         | W_Algebra.TM_print_prep_seen_not_rec ->
             (* Simple match that performs ty =?= SType_var. *)
             (match ty.W_Algebra.sty_desc with
              | W_Algebra.SType_var _ -> (true, false)
              | _ -> (false, false))
         | W_Algebra.TM_print_prep_seen xtimes ->
             (* An abbrev is needed if the type remained tagged as seen several
time (i.e. > 1), i.e. seen several times in its sub-terms.
However, if the type a a variable, we generate an "abbrev" that
is rather its name, but we don't want to register it. *)
             if !xtimes > 1 then
               (match ty.W_Algebra.sty_desc with
                | W_Algebra.SType_var _ ->
                    (* Variable seen recursively, so don't register its
abbreviation that is rather its name. *)
                    (true, false)
                | _ ->
                    (* The type is seen recursively and is not a variable, hence
we want to register its abbreviation. *)
                    (true, true))
             else (false, false)
         | _ -> assert false) in
      if need_abbrev then (
        (* Ok, introduce an abbreviation. *)
        let abb_name = create_type_variable_name ty.W_Algebra.sty_desc in
        (* Turn the marker of the type to say it is now abbreviated. *)
        ty.W_Algebra.sty_mark <- W_Algebra.TM_print_final_abbrevd abb_name ;
        tys_to_reprint_if_sequence := ty :: !tys_to_reprint_if_sequence ;
        (* And simply print the abbreviation. *)
        Format.fprintf ppf "%s" abb_name ;
        (* Finally, remind the abbreviation to be able to summarize and explain
them later (if it's not a variable, since we treated variables and
real cycles the same way, we don't want to "summarize and explain"
variables !). *)
        if register_abbrev then abbreviations := ty :: !abbreviations
      )
      else (
        (* Descend on the type's structure. *)
        match ty.W_Algebra.sty_desc with
        | W_Algebra.SType_var _ -> () (* Done above. *)
        | W_Algebra.SType_arrow (args_tys, res_ty) ->
            if prio >= 2 then Format.fprintf ppf "@[<1>("
            else Format.fprintf ppf "@[" ;
            Format.fprintf ppf "%a@ ->@ %a"
              (__pp_comma_separeted_simple_types 2) args_tys
              (__pp_simple_type 1) res_ty ;
            if prio >= 2 then Format.fprintf ppf ")@]"
            else Format.fprintf ppf "@]"
        | W_Algebra.SType_named { W_Algebra.nst_name = name ;
                                  W_Algebra.nst_args = args ;
                                  W_Algebra.nst_unwinded = _manifest } ->
            (* We never print the real representation of a named type. *)
            Format.fprintf ppf "%s@,"
              (QmlAst.TypeIdent.to_printable_string name) ;
            (* Only if there are parameters to the type constructor, print them
separated by a comma and enclosed between parentheses. Since
arguments of the constructor are always enclosed by parens,
priority lowers to 0. *)
            if args <> [] then
              Format.fprintf ppf "@[<1>(%a)@]"
                (__pp_comma_separeted_simple_types 0) args ;
            (* <---------- DEBUG
(match _manifest with
| None -> ()
| Some t ->
Format.fprintf ppf "@[<1>=@ %a@]" (__pp_simple_type 0) t)
<---------- END DEBUG *)
        | W_Algebra.SType_sum_of_records sumcases_column ->
            __pp_column_type ppf sumcases_column
        | W_Algebra.SType_forall scheme ->
            Format.fprintf ppf "@[!%a%a@]"
              __pp_scheme_parameters
              (scheme.W_Algebra.ty_parameters,
               scheme.W_Algebra.row_parameters,
               scheme.W_Algebra.column_parameters)
              (__pp_simple_type prio) scheme.W_Algebra.body
      )
    )
  | _ (* Other markers. *) -> assert false



(* ************************************************************************** *)
(** {b Descr}: Simply prints the rows of a column type, separating each by a
trailing "/", except for the last one. This allows to avoid spurious
trailing "/" in case the column hosting the rows is closed.
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
and __pp_rows_of_column ppf = function
  | [] -> ()
  | [last_row] -> Format.fprintf ppf "%a" __pp_row_type last_row
  | h :: q ->
      Format.fprintf ppf "%a /@ " __pp_row_type h ;
      __pp_rows_of_column ppf q



and __pp_column_type ppf column =
  (* No need to "repr" since it was already done by
[__prepare_print_column_type]. *)
  let (col_rows, col_ending) = column.W_Algebra.ct_value in
  Format.fprintf ppf "@[" ;
  let print_col_ending_as =
    (match col_ending with
     | W_Algebra.Closed_column -> ""
     | W_Algebra.Var_column v -> (
         match v.W_Algebra.cv_mark with
         | W_Algebra.VM_print_sequence_abbrevd abb_name ->
             (* Now that the column will be re-print once for this new printing
sequence step, we can ask for it to be printed next as its
abbreviation. *)
             v.W_Algebra.cv_mark <-
               W_Algebra.VM_print_final_abbrevd abb_name ;
             col_vars_to_reprint_if_sequence :=
               v :: !col_vars_to_reprint_if_sequence ;
             abb_name
         | W_Algebra.VM_print_prep_seen _
         | W_Algebra.VM_print_prep_seen_not_rec ->
             let abb_name =
               create_column_variable_name
                 (v.W_Algebra.cv_level = W_CoreTypes.generic_binding_level) in
             v.W_Algebra.cv_mark <- W_Algebra.VM_print_final_abbrevd abb_name ;
             col_vars_to_reprint_if_sequence :=
               v :: !col_vars_to_reprint_if_sequence ;
             abb_name
         | W_Algebra.VM_print_final_abbrevd abb_name ->
             (* The column ending has already be aliased, hence this means that
it has already be seen. *)
             abb_name
         | _ (* Other markers. *) -> assert false)) in
  Format.fprintf ppf "%a" __pp_rows_of_column col_rows ;
  (* Only if something was printed before and we still have to print something
we must print a separating "/". *)
  if (col_rows <> []) && (print_col_ending_as <> "") then
    Format.fprintf ppf " /@ " ;
  if print_col_ending_as <> "" then
    Format.fprintf ppf "%s" print_col_ending_as ;
  Format.fprintf ppf "@]"



(* ************************************************************************** *)
(** {b Descr}: Simply prints the fields of a row type, separating each by a
trailing ";", except for the last one. This allows to avoid spurious
trailing ";" in case the row hosting the fields is closed.
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
and __pp_fields_of_row ppf = function
  | [] -> ()
  | [(last_field_label, last_field_ty)] ->
      Format.fprintf ppf "@[%s" last_field_label ;
      if not (is_void last_field_ty) then
        Format.fprintf ppf ":@ %a" (__pp_simple_type 0) last_field_ty ;
      Format.fprintf ppf "@]"
  | (field_label, field_ty) :: q ->
      Format.fprintf ppf "@[%s" field_label ;
      if not (is_void field_ty) then
        Format.fprintf ppf ":@ %a" (__pp_simple_type 0) field_ty ;
      Format.fprintf ppf ";@]@ " ;
      __pp_fields_of_row ppf q



and __pp_row_type ppf row =
  (* No need to "repr" since it was already done by
[__prepare_print_row_type]. *)
  let (row_fields, row_ending) = row.W_Algebra.rt_value in
  Format.fprintf ppf "@[<2>{ " ;
  let print_row_ending_as =
    (match row_ending with
     | W_Algebra.Closed_row -> ""
     | W_Algebra.Var_row v -> (
         match v.W_Algebra.rv_mark with
         | W_Algebra.VM_print_sequence_abbrevd abb_name ->
             (* Now that the row will be re-print once for this new printing
sequence step, we can ask for it to be printed next as its
abbreviation. *)
             v.W_Algebra.rv_mark <- W_Algebra.VM_print_final_abbrevd abb_name ;
             row_vars_to_reprint_if_sequence :=
               v :: !row_vars_to_reprint_if_sequence ;
             abb_name
         | W_Algebra.VM_print_prep_seen _
         | W_Algebra.VM_print_prep_seen_not_rec ->
             let abb_name =
               create_row_variable_name
                 (v.W_Algebra.rv_level = W_CoreTypes.generic_binding_level) in
             v.W_Algebra.rv_mark <- W_Algebra.VM_print_final_abbrevd abb_name ;
             row_vars_to_reprint_if_sequence :=
               v :: !row_vars_to_reprint_if_sequence ;
             abb_name
         | W_Algebra.VM_print_final_abbrevd abb_name ->
             (* The row ending has already be aliased, hence this means that
it has already be seen. *)
             abb_name
         | _ (* Other markers. *) -> assert false)) in
  (* Sort the fields in alphabetical order on their name to ease finding a field
name or compare 2 rows in case of error. *)
  let sorted_row_fields =
    List.sort (fun (n1, _) (n2, _) -> compare n1 n2) row_fields in
  Format.fprintf ppf "%a" __pp_fields_of_row sorted_row_fields ;
  (* Only if something was printed before and we still have to print something
we must print a separating ";". *)
  if (sorted_row_fields <> []) && (print_row_ending_as <> "") then
    Format.fprintf ppf ";@ " ;
  if print_row_ending_as <> "" then
    Format.fprintf ppf "%s" print_row_ending_as ;
  Format.fprintf ppf " }@]"



(* ************************************************************************** *)
(** {b Descr}: Internal function (not to be exported or called at toplevel)
that prints a list of [W_Algebra.simple_type] separating them by a ","
character.
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
and __pp_comma_separeted_simple_types prio ppf = function
  | [] -> ()
  | [last] -> Format.fprintf ppf "%a" (__pp_simple_type prio) last
  | h :: q ->
      Format.fprintf ppf "%a,@ %a"
        (__pp_simple_type prio) h (__pp_comma_separeted_simple_types prio) q



(* ************************************************************************** *)
(** {b Descr}: Just an helper providing a pretty printer for the list of
generalized variables of a type scheme. *)
(* ************************************************************************** *)
and __pp_scheme_parameters
    ppf (ty_parameters, row_parameters, column_parameters) =
  if ty_parameters <> [] || row_parameters<> [] || column_parameters <> [] then
    (
      Format.fprintf ppf "@[<1>for all@ ";
      (* For type variables, directly take benefit from the [simple_type] pretty
print function since by construction, generalized type variables are
[simple_type]s always being ... [SType_var]. *)
      List.iter
        (fun v ->
          (* No need to deal with [VM_print_sequence_abbrevd] and
[VM_print_final_abbrevd] since here types are variables, hence
are not subject to abbreviation. *)
          Format.fprintf ppf "%a@ " (__pp_simple_type 0) v)
        ty_parameters ;
      List.iter
        (fun v ->
           let abb_name =
             create_row_variable_name
               (v.W_Algebra.rv_level = W_CoreTypes.generic_binding_level) in
           (* Set the marker so that printing routine won't believe that the
row variable being already seen it must be printed as an
abbreviation when we will be printing the body of the type
forall. *)
           v.W_Algebra.rv_mark <- W_Algebra.VM_print_sequence_abbrevd abb_name ;
           Format.fprintf ppf "%s@ " abb_name)
        row_parameters ;
      List.iter
        (fun v ->
           let abb_name =
             create_column_variable_name
               (v.W_Algebra.cv_level = W_CoreTypes.generic_binding_level) in
           (* Same remark than for row variables about printing not as an
abbreviation when we will be printing the body of the type
forall. *)
           v.W_Algebra.cv_mark <- W_Algebra.VM_print_sequence_abbrevd abb_name ;
           Format.fprintf ppf "%s@ " abb_name)
        column_parameters ;
      Format.fprintf ppf "@].@ "
    )



(* ************************************************************************** *)
(** {b Descr}: Used to initiate pretty-print of abbreviations. In other words,
because the type to print of mandatorily [TM_print_final_abbrevd], if we
use the regular pretty-print routine, we will get printed ... its
abbreviation name ! Which is not very interesting !
So, for the first time, we descent on the type's structure. Then next,
we fall back on the regular pretty-print routine.
Note by the way that the type we enter in is mandatorily marked as
[TM_print_final_abbrevd] because it has been explored first by the
regular pretty-print routine [__pp_simple_type] and registered as
abbreviated, hence marked accordingly. In effect, the regular pretty-print
routine is called before to print the global and initial type (in which
there is a part that is the abbreviated one we are processing).
This function must only be called by [__pp_abbreviations].
{b Visibility}: Not exported outside this module. *)
(* ************************************************************************** *)
let __explain_simple_type_abbrev prio ppf abbreved_ty =
  let abbreved_ty = W_CoreTypes.simple_type_repr abbreved_ty in
  match abbreved_ty.W_Algebra.sty_mark with
  | W_Algebra.TM_print_sequence_abbrevd _
  | W_Algebra.TM_print_final_abbrevd _ -> (
      match abbreved_ty.W_Algebra.sty_desc with
      | W_Algebra.SType_var _ ->
          (* Since variables are never abbreviated, this should never happen. *)
          assert false
      | W_Algebra.SType_arrow (args_tys, res_ty) ->
          if prio >= 2 then Format.fprintf ppf "@[<1>("
          else Format.fprintf ppf "@[" ;
          Format.fprintf ppf "%a@ ->@ %a"
            (__pp_comma_separeted_simple_types 2) args_tys
            (__pp_simple_type 1) res_ty ;
          if prio >= 2 then Format.fprintf ppf ")@]"
          else Format.fprintf ppf "@]"
      | W_Algebra.SType_named { W_Algebra.nst_name = name ;
                                W_Algebra.nst_args = args ;
                                W_Algebra.nst_unwinded = _manifest } ->
          (* We never print the real representation of a named type. *)
          Format.fprintf ppf "%s" (QmlAst.TypeIdent.to_printable_string name) ;
          (* Only if there are parameters to the type constructor, print them
separated by a comma and enclosed between parentheses. Since
arguments of the constructor are always enclosed by parens,
priority lowers to 0. *)
          if args <> [] then
            Format.fprintf ppf "@[<1>(%a)@]"
              (__pp_comma_separeted_simple_types 0) args ;
          (* <---------- DEBUG
(match _manifest with
| None -> ()
| Some t -> Format.fprintf ppf "@[<1>=@ %a@]" (__pp_simple_type 0) t)
<---------- END DEBUG *)
      | W_Algebra.SType_sum_of_records sumcases_column ->
          __pp_column_type ppf sumcases_column
      | W_Algebra.SType_forall scheme ->
          Format.fprintf ppf "@[!%a%a@]"
            __pp_scheme_parameters
            (scheme.W_Algebra.ty_parameters,
             scheme.W_Algebra.row_parameters,
             scheme.W_Algebra.column_parameters)
            (__pp_simple_type prio) scheme.W_Algebra.body
    )
  | _ (* Other markers. *) -> assert false



let rec __pp_abbreviations ppf abbreviated_tys =
  (* Clear the found abbreviations list *)
  abbreviations := [] ;
  List.iter
    (fun abbreviated_ty ->
       (* Note that repr has already be done, so no need to do it again. *)
       match abbreviated_ty.W_Algebra.sty_mark with
       | W_Algebra.TM_print_final_abbrevd abb_name
       | W_Algebra.TM_print_sequence_abbrevd abb_name ->
           Format.fprintf ppf "@\n @[where %s = %a@]"
             abb_name (__explain_simple_type_abbrev 0) abbreviated_ty
       | _ -> assert false)
  abbreviated_tys ;
  (* Check if during abbreviations printing we have found hidden new ones. *)
  let abbreviated_tys2 = !abbreviations in
  (* If yes, go on printing. This must terminate because each time we find
something new, we mark it as [TM_print_final_abbrevd] so when all is
marked [TM_print_final_abbrevd], we do not have anything to do. *)
  if abbreviated_tys2 <> [] then
    __pp_abbreviations ppf (List.rev abbreviated_tys2)



(* ************************************************************************** *)
(** {b Descr}: Pretty printer for a simple type. This function first resets the
known type variables to make sure that the type is printed with variables
starting from 'a, then 'b and so on, hence preventing having raving
variables names due to the fact that we previously encountered tons of
type variables in other types. Then, it simply calls the recursive type
pretty printer.
{b Visibility}: Exported outside this module. *)
(* ************************************************************************** *)
let pp_simple_type ppf ty =
  type_variables_counter := 0 ;
  row_variables_counter := 0 ;
  column_variables_counter := 0 ;
  abbreviations := [] ;
  (* Do the pre-print stuff. *)
  __prepare_print_simple_type ty ;
  (* And now really print. *)
  Format.fprintf ppf "@[%a@]" (__pp_simple_type 0) ty ;
  (* Print the abbreviations if some. *)
  __pp_abbreviations ppf (List.rev !abbreviations) ;
  (* Don't forget to cleanup the type. *)
  W_CoreTypes.cleanup_simple_type ty ;
  (* Since we are not printing a sequence of types, we can directly forget all
the types that should be re-printed once even if they have an already
known abbreviations. *)
  tys_to_reprint_if_sequence := [] ;
  row_vars_to_reprint_if_sequence := [] ;
  col_vars_to_reprint_if_sequence := []



let (pp_simple_type_start_sequence, pp_simple_type_continue_sequence,
     pp_simple_type_end_sequence, pp_nothing_end_sequence) =
  let printed_tys = ref [] in
  (
   (* *********************************************************************** *)
   (** {b Descr} Body of the [pp_simple_type_start_sequence] function. This
function works in combinaison with [pp_simple_type_continue_sequence] and
[pp_simple_type_end_sequence] to allow printing several separate types
without breaking the sharing between them, i.e. with making visible the
fact that some parts are common to them.
This function must be called to initiate and print the first of these
types.
{b Visibility}: Exported outside this module. *)
   (* *********************************************************************** *)
   (fun ppf ty ->
     type_variables_counter := 0 ;
     row_variables_counter := 0 ;
     column_variables_counter := 0 ;
     abbreviations := [] ;
     printed_tys := [] ;
     (* Do the pre-print stuff. *)
     __prepare_print_simple_type ty ;
     (* And now really print. *)
     Format.fprintf ppf "@[%a@]" (__pp_simple_type 0) ty ;
     printed_tys := ty :: !printed_tys ;
     (* To make so that types previously printed in the sequence will be
re-printed once without abbreviation in a future, toggle their marker
[W_Algebra.TM_print_final_abbrevd] into
[W_Algebra.TM_print_sequence_abbrevd]. *)
     toggle_final_abbrevd_for_sequence
       !tys_to_reprint_if_sequence !row_vars_to_reprint_if_sequence
       !col_vars_to_reprint_if_sequence ;
     (* Now, forget theses types since they are changed in place. *)
     tys_to_reprint_if_sequence := [] ;
     row_vars_to_reprint_if_sequence := [] ;
     col_vars_to_reprint_if_sequence := []),



   (* *********************************************************************** *)
   (** {b Descr} Body of the [pp_simple_type_continue_sequence] function. This
function works in combinaison with [pp_simple_type_start_sequence] and
[pp_simple_type_end_sequence].
More info in the header of [pp_simple_type_start_sequence].
This function must be called to print all types between (exclusive) the
first and the last ones of the sequence to print.
{b Visibility}: Exported outside this module. *)
   (* *********************************************************************** *)
   (fun ppf ty ->
     (* Do the pre-print stuff and keep in mind the already seen types and
variables from the previous calls. *)
     __prepare_print_simple_type ty ;
     (* And now really print. *)
     Format.fprintf ppf "@[%a@]" (__pp_simple_type 0) ty ;
     printed_tys := ty :: !printed_tys ;
     (* To make so that types previously printed in the sequence will be
re-printed once without abbreviation in a future, toggle their marker
[W_Algebra.TM_print_final_abbrevd] into
[W_Algebra.TM_print_sequence_abbrevd]. *)
     toggle_final_abbrevd_for_sequence
       !tys_to_reprint_if_sequence !row_vars_to_reprint_if_sequence
       !col_vars_to_reprint_if_sequence ;
     (* Now, forget theses types since they are changed in place. *)
     tys_to_reprint_if_sequence := [] ;
     row_vars_to_reprint_if_sequence := [] ;
     col_vars_to_reprint_if_sequence := []),



   (* *********************************************************************** *)
   (** {b Descr}: Body of the [pp_simple_type_end_sequence] function. This
function works in combinaison with [pp_simple_type_start_sequence] and
[pp_simple_type_continue_sequence].
More info in the header of [pp_simple_type_start_sequence].
This function must be called to print the last type of the sequence to
print. In particular, this has the effect of printing the abbreviations
values and flushing the "already seen" types by cleaning the markers of
all of them.
It is hence **really** important to end the printing sequence by calling
this function.
{b Visibility}: Exported outside this module. *)
   (* *********************************************************************** *)
   (fun ppf ty ->
     (* Do the pre-print stuff. Like in [pp_simple_type_continue_sequence], we
keep in mind the already seen types and variables from the previous
calls. *)
     __prepare_print_simple_type ty ;
     (* And now really print. *)
     Format.fprintf ppf "@[%a@]" (__pp_simple_type 0) ty ;
     (* Print the abbreviations if some. *)
     __pp_abbreviations ppf (List.rev !abbreviations) ;
     printed_tys := ty :: !printed_tys ;
     (* Don't forget to cleanup **all** the printed types. *)
     List.iter W_CoreTypes.cleanup_simple_type !printed_tys ;
     (* Clean the list of printed types, this may save a bit of memory by
directly releasing these types and leaving the GC getting them. *)
     printed_tys := [] ;
     (* Since we are at the end of a sequence of types, we can directly forget
all the types that should be re-printed once even if they have an
already known abbreviations. *)
     tys_to_reprint_if_sequence := [] ;
     row_vars_to_reprint_if_sequence := [] ;
     col_vars_to_reprint_if_sequence := []),



   (* *********************************************************************** *)
   (** {b Descr}: Body of the [pp_nothing_end_sequence] function. This
function works in combinaison with [pp_simple_type_start_sequence] and
[pp_simple_type_continue_sequence].
This function must be called when a print sequence was started but it
appears that we have nothing more to print although the last call was
not [pp_simple_type_end_sequence]. This happens for example when
[W_ReportErrors.pp_incompatible_types_if_more_precise] is called but
has nothing more precise to print. If the present function is not
called, there may remain some uncleared markers in the previously
printed types, and hence cause assert failures later.
{b Visibility}: Exported outside this module. *)
   (* *********************************************************************** *)
   (fun () ->
      (* Cleanup **all** the printed types. *)
      List.iter W_CoreTypes.cleanup_simple_type !printed_tys ;
      (* Clean the list of printed types, this may save a bit of memory by
directly releasing these types and leaving the GC getting them. *)
      printed_tys := [] ;
      (* Since we are at the end of a sequence of types, we can directly forget
all the types that should be re-printed once even if they have an
already known abbreviations. *)
      tys_to_reprint_if_sequence := [] ;
      row_vars_to_reprint_if_sequence := [] ;
      col_vars_to_reprint_if_sequence := [])
  )



(* ************************************************************************** *)
(** {b Descr}: Pretty printer for a type scheme. It displays the scheme by
explicitly prefixing its body by the list of generalized variables.
{b Visibility}: Exported outside this module. *)
(* ************************************************************************** *)
let pp_scheme ppf scheme =
  type_variables_counter := 0 ;
  row_variables_counter := 0 ;
  column_variables_counter := 0 ;
  abbreviations := [] ;
  let scheme_params =
    (scheme.W_Algebra.ty_parameters,
     scheme.W_Algebra.row_parameters,
     scheme.W_Algebra.column_parameters) in
  (* Do the pre-print stuff. *)
  __prepare_print_scheme_parameters scheme_params ;
  __prepare_print_simple_type scheme.W_Algebra.body ;
  (* And now really print. *)
  Format.fprintf ppf "@[%a" __pp_scheme_parameters scheme_params ;
  Format.fprintf ppf "%a@]" (__pp_simple_type 0) scheme.W_Algebra.body ;
  (* Print the abbreviations if some. *)
  __pp_abbreviations ppf (List.rev !abbreviations) ;
  (* Don't forget to cleanup the scheme's body. Also do so on the types
representing generalized variables in case they would not be bound in the
type (pathological case like type ('a) t = int). *)
  List.iter W_CoreTypes.cleanup_simple_type scheme.W_Algebra.ty_parameters ;
  W_CoreTypes.cleanup_simple_type scheme.W_Algebra.body



let print_simple_type = pp_simple_type Format.std_formatter
let print_scheme = pp_scheme Format.std_formatter
Something went wrong with that request. Please try again.