src/Pure/ML/ml_pretty.ML
author wenzelm
Thu, 07 Apr 2016 13:35:08 +0200
changeset 62899 845ed4584e21
parent 62878 1cec457e0a03
child 62900 c641bf9402fd
permissions -rw-r--r--
clarified bootstrap of @{make_string} -- avoid query on ML environment;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
62508
d0b68218ea55 discontinued RAW session: bootstrap directly from isabelle_process RAW_ML_SYSTEM;
wenzelm
parents: 62503
diff changeset
     1
(*  Title:      Pure/ML/ml_pretty.ML
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
     3
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
     4
Minimal support for raw ML pretty printing, notably for toplevel pp.
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
     5
*)
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
     6
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
     7
signature ML_PRETTY =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
     8
sig
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
     9
  datatype pretty =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    10
    Block of (string * string) * bool * FixedInt.int * pretty list |
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    11
    String of string * FixedInt.int |
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    12
    Break of bool * FixedInt.int * FixedInt.int
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    13
  val block: pretty list -> pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    14
  val str: string -> pretty
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    15
  val brk: FixedInt.int -> pretty
62784
0371c369ab1d adapted to Poly/ML repository version 2e40cadc975a;
wenzelm
parents: 62711
diff changeset
    16
  val pair: ('a * FixedInt.int -> pretty) -> ('b * FixedInt.int -> pretty) ->
0371c369ab1d adapted to Poly/ML repository version 2e40cadc975a;
wenzelm
parents: 62711
diff changeset
    17
    ('a * 'b) * FixedInt.int -> pretty
0371c369ab1d adapted to Poly/ML repository version 2e40cadc975a;
wenzelm
parents: 62711
diff changeset
    18
  val enum: string -> string -> string -> ('a * FixedInt.int -> pretty) ->
0371c369ab1d adapted to Poly/ML repository version 2e40cadc975a;
wenzelm
parents: 62711
diff changeset
    19
    'a list * FixedInt.int -> pretty
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    20
  val to_polyml: pretty -> PolyML_Pretty.pretty
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    21
  val from_polyml: PolyML_Pretty.pretty -> pretty
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    22
  val format_polyml: int -> PolyML_Pretty.pretty -> string
62662
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
    23
  val format: int -> pretty -> string
62899
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
    24
  val default_margin: int
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
    25
  val string_of_polyml: PolyML_Pretty.pretty -> string
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
    26
  val make_string_global: string
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
    27
  val make_string_local: string -> string
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    28
end;
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    29
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    30
structure ML_Pretty: ML_PRETTY =
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    31
struct
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    32
62661
c23ff2f45a18 clarified modules;
wenzelm
parents: 62508
diff changeset
    33
(* datatype pretty *)
c23ff2f45a18 clarified modules;
wenzelm
parents: 62508
diff changeset
    34
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    35
datatype pretty =
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    36
  Block of (string * string) * bool * FixedInt.int * pretty list |
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    37
  String of string * FixedInt.int |
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    38
  Break of bool * FixedInt.int * FixedInt.int;
30622
dba663f1afa8 Datatype for ML pretty printing (cf. mlsource/MLCompiler/Pretty.sml in Poly/ML 5.3).
wenzelm
parents:
diff changeset
    39
61869
ba466ac335e3 clarified underlying datatypes;
wenzelm
parents: 61864
diff changeset
    40
fun block prts = Block (("", ""), false, 2, prts);
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    41
fun str s = String (s, FixedInt.fromInt (size s));
61862
e2a9e46ac0fb support pretty break indent, like underlying ML systems;
wenzelm
parents: 38635
diff changeset
    42
fun brk width = Break (false, width, 0);
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    43
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    44
fun pair pretty1 pretty2 ((x, y), depth: FixedInt.int) =
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    45
  block [str "(", pretty1 (x, depth), str ",", brk 1, pretty2 (y, depth - 1), str ")"];
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    46
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    47
fun enum sep lpar rpar pretty (args, depth: FixedInt.int) =
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    48
  let
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    49
    fun elems _ [] = []
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    50
      | elems 0 _ = [str "..."]
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    51
      | elems d [x] = [pretty (x, d)]
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    52
      | elems d (x :: xs) = pretty (x, d) :: str sep :: brk 1 :: elems (d - 1) xs;
62387
ad3eb2889f9a support for polyml-git ec49a49972c5 (branch FixedPrecisionInt);
wenzelm
parents: 61925
diff changeset
    53
  in block (str lpar :: (elems (FixedInt.max (depth, 0)) args @ [str rpar])) end;
38635
f76ad0771f67 added ML toplevel pretty-printing for tables, using dummy for anything other than Poly/ML 5.3.0 (or later);
wenzelm
parents: 30623
diff changeset
    54
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    55
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    56
(* convert *)
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    57
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    58
fun to_polyml (Break (false, width, offset)) = PolyML_Pretty.PrettyBreak (width, offset)
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    59
  | to_polyml (Break (true, _, _)) =
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    60
      PolyML_Pretty.PrettyBlock (0, false, [PolyML_Pretty.ContextProperty ("fbrk", "")],
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    61
        [PolyML_Pretty.PrettyString " "])
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    62
  | to_polyml (Block ((bg, en), consistent, ind, prts)) =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    63
      let val context =
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    64
        (if bg = "" then [] else [PolyML_Pretty.ContextProperty ("begin", bg)]) @
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    65
        (if en = "" then [] else [PolyML_Pretty.ContextProperty ("end", en)])
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    66
      in PolyML_Pretty.PrettyBlock (ind, consistent, context, map to_polyml prts) end
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    67
  | to_polyml (String (s, len)) =
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    68
      if len = FixedInt.fromInt (size s) then PolyML_Pretty.PrettyString s
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    69
      else
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    70
        PolyML_Pretty.PrettyBlock
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    71
          (0, false,
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    72
            [PolyML_Pretty.ContextProperty ("length", FixedInt.toString len)], [PolyML_Pretty.PrettyString s]);
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    73
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    74
val from_polyml =
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    75
  let
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    76
    fun convert _ (PolyML_Pretty.PrettyBreak (width, offset)) = Break (false, width, offset)
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    77
      | convert _ (PolyML_Pretty.PrettyBlock (_, _,
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    78
            [PolyML_Pretty.ContextProperty ("fbrk", _)], [PolyML_Pretty.PrettyString " "])) =
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    79
          Break (true, 1, 0)
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    80
      | convert len (PolyML_Pretty.PrettyBlock (ind, consistent, context, prts)) =
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    81
          let
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    82
            fun property name default =
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    83
              (case List.find (fn PolyML_Pretty.ContextProperty (a, _) => name = a | _ => false) context of
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    84
                SOME (PolyML_Pretty.ContextProperty (_, b)) => b
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    85
              | _ => default);
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    86
            val bg = property "begin" "";
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    87
            val en = property "end" "";
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    88
            val len' = property "length" len;
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    89
          in Block ((bg, en), consistent, ind, map (convert len') prts) end
62823
751bcf0473a7 tuned signature;
wenzelm
parents: 62819
diff changeset
    90
      | convert len (PolyML_Pretty.PrettyString s) =
62503
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    91
          String (s, FixedInt.fromInt (case Int.fromString len of SOME i => i | NONE => size s))
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    92
  in convert "" end;
19afb533028e clarified modules;
wenzelm
parents: 62387
diff changeset
    93
62661
c23ff2f45a18 clarified modules;
wenzelm
parents: 62508
diff changeset
    94
62662
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
    95
(* format *)
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
    96
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
    97
fun format_polyml margin prt =
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
    98
  let
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
    99
    val result = Unsynchronized.ref [];
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   100
    val () = PolyML.prettyPrint (fn s => result := s :: ! result, margin) prt
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   101
  in String.concat (List.rev (! result)) end;
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   102
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   103
fun format margin = format_polyml margin o to_polyml;
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   104
62899
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
   105
val default_margin = 76;
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
   106
62662
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   107
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   108
(* make string *)
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   109
62899
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
   110
val string_of_polyml = format_polyml default_margin;
62662
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   111
62899
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
   112
fun make_string depth =
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
   113
  "(fn x => ML_Pretty.string_of_polyml (ML_system_pretty (x, FixedInt.fromInt (" ^ depth ^ "))))";
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
   114
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
   115
val make_string_global = make_string "ML_Print_Depth.get_print_depth ()";
845ed4584e21 clarified bootstrap of @{make_string} -- avoid query on ML environment;
wenzelm
parents: 62878
diff changeset
   116
fun make_string_local local_env = make_string (local_env ^ ".ML_print_depth ()");
62662
291cc01f56f5 @{make_string} is available during Pure bootstrap;
wenzelm
parents: 62661
diff changeset
   117
62661
c23ff2f45a18 clarified modules;
wenzelm
parents: 62508
diff changeset
   118
end;