src/Pure/display.ML
author wenzelm
Fri Apr 03 14:36:20 1998 +0200 (1998-04-03)
changeset 4782 9c0b31da51c6
parent 4498 a088ec3e4f5e
child 4950 226f2cde9f4d
permissions -rw-r--r--
tuned comments;
paulson@1591
     1
(*  Title:      Pure/display.ML
paulson@1591
     2
    ID:         $Id$
paulson@1591
     3
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
paulson@1591
     4
    Copyright   1993  University of Cambridge
paulson@1591
     5
paulson@1591
     6
Printing of theories, theorems, etc.
paulson@1591
     7
*)
paulson@1591
     8
paulson@1591
     9
signature DISPLAY =
paulson@1591
    10
  sig
paulson@1591
    11
  val pprint_cterm	: cterm -> pprint_args -> unit
paulson@1591
    12
  val pprint_ctyp	: ctyp -> pprint_args -> unit
paulson@1591
    13
  val pprint_theory	: theory -> pprint_args -> unit
paulson@1591
    14
  val pprint_thm	: thm -> pprint_args -> unit
wenzelm@4126
    15
  val pretty_ctyp	: ctyp -> Pretty.T
wenzelm@3547
    16
  val pretty_cterm	: cterm -> Pretty.T
paulson@1591
    17
  val pretty_thm	: thm -> Pretty.T
paulson@1591
    18
  val print_cterm	: cterm -> unit
paulson@1591
    19
  val print_ctyp	: ctyp -> unit
wenzelm@3851
    20
  val show_consts	: bool ref
paulson@1591
    21
  val print_goals	: int -> thm -> unit
wenzelm@4498
    22
  val pretty_name_space : string * NameSpace.T -> Pretty.T
paulson@1591
    23
  val print_syntax	: theory -> unit
paulson@1591
    24
  val print_theory	: theory -> unit
wenzelm@3873
    25
  val print_data	: theory -> string -> unit
paulson@1591
    26
  val print_thm		: thm -> unit
paulson@1591
    27
  val prth		: thm -> thm
wenzelm@4270
    28
  val prthq		: thm Seq.seq -> thm Seq.seq
paulson@1591
    29
  val prths		: thm list -> thm list
paulson@1591
    30
  val show_hyps		: bool ref
paulson@1591
    31
  val string_of_cterm	: cterm -> string
paulson@1591
    32
  val string_of_ctyp	: ctyp -> string
paulson@1591
    33
  val string_of_thm	: thm -> string
paulson@1591
    34
  end;
paulson@1591
    35
paulson@1591
    36
paulson@1591
    37
structure Display : DISPLAY =
paulson@1591
    38
struct
paulson@1591
    39
paulson@1591
    40
(*If false, hypotheses are printed as dots*)
paulson@1591
    41
val show_hyps = ref true;
paulson@1591
    42
paulson@1591
    43
fun pretty_thm th =
paulson@1591
    44
  let
paulson@1591
    45
    val {sign, hyps, prop, ...} = rep_thm th;
paulson@1591
    46
    val xshyps = extra_shyps th;
paulson@1591
    47
    val hlen = length xshyps + length hyps;
paulson@1591
    48
    val hsymbs =
paulson@1591
    49
      if hlen = 0 then []
paulson@1591
    50
      else if ! show_hyps then
paulson@1591
    51
        [Pretty.brk 2, Pretty.list "[" "]"
wenzelm@3785
    52
          (map (Sign.pretty_term sign) hyps @
wenzelm@3785
    53
           map (Sign.pretty_sort sign) xshyps)]
paulson@1591
    54
      else
paulson@1591
    55
        [Pretty.brk 2, Pretty.str ("[" ^ implode (replicate hlen ".") ^ "]")];
paulson@1591
    56
  in
paulson@1591
    57
    Pretty.block (Sign.pretty_term sign prop :: hsymbs)
paulson@1591
    58
  end;
paulson@1591
    59
paulson@1591
    60
val string_of_thm = Pretty.string_of o pretty_thm;
paulson@1591
    61
val pprint_thm = Pretty.pprint o Pretty.quote o pretty_thm;
paulson@1591
    62
paulson@1591
    63
paulson@1591
    64
(** Top-level commands for printing theorems **)
paulson@1591
    65
val print_thm = writeln o string_of_thm;
paulson@1591
    66
paulson@1591
    67
fun prth th = (print_thm th; th);
paulson@1591
    68
paulson@1591
    69
(*Print and return a sequence of theorems, separated by blank lines. *)
paulson@1591
    70
fun prthq thseq =
wenzelm@4270
    71
  (Seq.print (fn _ => print_thm) 100000 thseq; thseq);
paulson@1591
    72
paulson@1591
    73
(*Print and return a list of theorems, separated by blank lines. *)
wenzelm@4210
    74
fun prths ths = (seq (fn th => (print_thm th; writeln "")) ths; ths);
paulson@1591
    75
paulson@1591
    76
paulson@1591
    77
(* other printing commands *)
paulson@1591
    78
wenzelm@4126
    79
fun pretty_ctyp cT =
wenzelm@4126
    80
  let val {sign, T} = rep_ctyp cT in Sign.pretty_typ sign T end;
wenzelm@4126
    81
paulson@1591
    82
fun pprint_ctyp cT =
paulson@1591
    83
  let val {sign, T} = rep_ctyp cT in Sign.pprint_typ sign T end;
paulson@1591
    84
paulson@1591
    85
fun string_of_ctyp cT =
paulson@1591
    86
  let val {sign, T} = rep_ctyp cT in Sign.string_of_typ sign T end;
paulson@1591
    87
paulson@1591
    88
val print_ctyp = writeln o string_of_ctyp;
paulson@1591
    89
wenzelm@3547
    90
fun pretty_cterm ct =
wenzelm@3547
    91
  let val {sign, t, ...} = rep_cterm ct in Sign.pretty_term sign t end;
wenzelm@3547
    92
paulson@1591
    93
fun pprint_cterm ct =
paulson@1591
    94
  let val {sign, t, ...} = rep_cterm ct in Sign.pprint_term sign t end;
paulson@1591
    95
paulson@1591
    96
fun string_of_cterm ct =
paulson@1591
    97
  let val {sign, t, ...} = rep_cterm ct in Sign.string_of_term sign t end;
paulson@1591
    98
paulson@1591
    99
val print_cterm = writeln o string_of_cterm;
paulson@1591
   100
paulson@1591
   101
wenzelm@4250
   102
wenzelm@4250
   103
(** print theory **)
paulson@1591
   104
paulson@1591
   105
val pprint_theory = Sign.pprint_sg o sign_of;
paulson@1591
   106
paulson@1591
   107
val print_syntax = Syntax.print_syntax o syn_of;
wenzelm@3873
   108
val print_data = Sign.print_data o sign_of;
paulson@1591
   109
wenzelm@4250
   110
wenzelm@4498
   111
(* pretty_name_space  *)
wenzelm@4498
   112
wenzelm@4498
   113
fun pretty_name_space (kind, space) =
wenzelm@4498
   114
  let
wenzelm@4498
   115
    fun prt_entry (name, accs) = Pretty.block
wenzelm@4498
   116
      (Pretty.str (quote name ^ " =") :: Pretty.brk 1 ::
wenzelm@4498
   117
        Pretty.commas (map (Pretty.str o quote) accs));
wenzelm@4498
   118
  in
wenzelm@4498
   119
    Pretty.fbreaks (Pretty.str (kind ^ ":") :: map prt_entry (NameSpace.dest space))
wenzelm@4498
   120
    |> Pretty.block
wenzelm@4498
   121
  end;
wenzelm@4498
   122
wenzelm@4498
   123
wenzelm@4498
   124
wenzelm@4250
   125
(* print signature *)
wenzelm@4250
   126
wenzelm@4250
   127
fun print_sign sg =
wenzelm@4250
   128
  let
wenzelm@4250
   129
    fun prt_cls c = Sign.pretty_sort sg [c];
wenzelm@4250
   130
    fun prt_sort S = Sign.pretty_sort sg S;
wenzelm@4250
   131
    fun prt_arity t (c, Ss) = Sign.pretty_arity sg (t, Ss, [c]);
wenzelm@4250
   132
    fun prt_typ ty = Pretty.quote (Sign.pretty_typ sg ty);
wenzelm@4250
   133
wenzelm@4250
   134
    val ext_class = Sign.cond_extern sg Sign.classK;
wenzelm@4250
   135
    val ext_tycon = Sign.cond_extern sg Sign.typeK;
wenzelm@4250
   136
    val ext_const = Sign.cond_extern sg Sign.constK;
wenzelm@4250
   137
wenzelm@4250
   138
wenzelm@4250
   139
    fun pretty_classes cs = Pretty.block
wenzelm@4250
   140
      (Pretty.breaks (Pretty.str "classes:" :: map prt_cls cs));
wenzelm@4250
   141
wenzelm@4250
   142
    fun pretty_classrel (c, cs) = Pretty.block
wenzelm@4250
   143
      (prt_cls c :: Pretty.str " <" :: Pretty.brk 1 ::
wenzelm@4250
   144
        Pretty.commas (map prt_cls cs));
wenzelm@4250
   145
wenzelm@4250
   146
    fun pretty_default S = Pretty.block
wenzelm@4250
   147
      [Pretty.str "default:", Pretty.brk 1, prt_sort S];
wenzelm@4250
   148
wenzelm@4250
   149
    fun pretty_ty (t, n) = Pretty.block
wenzelm@4250
   150
      [Pretty.str (ext_tycon t), Pretty.str (" " ^ string_of_int n)];
wenzelm@4250
   151
wenzelm@4250
   152
    fun pretty_abbr (t, (vs, rhs)) = Pretty.block
wenzelm@4250
   153
      [prt_typ (Type (t, map (fn v => TVar ((v, 0), [])) vs)),
wenzelm@4250
   154
        Pretty.str " =", Pretty.brk 1, prt_typ rhs];
wenzelm@4250
   155
wenzelm@4250
   156
    fun pretty_arities (t, ars) = map (prt_arity t) ars;
wenzelm@4250
   157
wenzelm@4250
   158
    fun pretty_const (c, ty) = Pretty.block
wenzelm@4250
   159
      [Pretty.str c, Pretty.str " ::", Pretty.brk 1, prt_typ ty];
wenzelm@4250
   160
wenzelm@4250
   161
    val {self = _, tsig, const_tab, syn = _, path, spaces, data} = Sign.rep_sg sg;
wenzelm@4440
   162
    val spaces' = sort_wrt fst spaces;
wenzelm@4250
   163
    val {classes, classrel, default, tycons, abbrs, arities} =
wenzelm@4250
   164
      Type.rep_tsig tsig;
wenzelm@4250
   165
    val consts = sort_wrt fst (map (apfst ext_const) (Symtab.dest const_tab));
wenzelm@4250
   166
  in
wenzelm@4250
   167
    Pretty.writeln (Pretty.strs ("stamps:" :: Sign.stamp_names_of sg));
wenzelm@4256
   168
    Pretty.writeln (Pretty.strs ("data:" :: Sign.data_kinds data));
wenzelm@4782
   169
    Pretty.writeln (Pretty.strs ["name prefix:", NameSpace.pack path]);
wenzelm@4498
   170
    Pretty.writeln (Pretty.big_list "name spaces:" (map pretty_name_space spaces'));
wenzelm@4250
   171
    Pretty.writeln (pretty_classes classes);
wenzelm@4250
   172
    Pretty.writeln (Pretty.big_list "class relation:" (map pretty_classrel classrel));
wenzelm@4250
   173
    Pretty.writeln (pretty_default default);
wenzelm@4250
   174
    Pretty.writeln (Pretty.big_list "type constructors:" (map pretty_ty tycons));
wenzelm@4250
   175
    Pretty.writeln (Pretty.big_list "type abbreviations:" (map pretty_abbr abbrs));
wenzelm@4250
   176
    Pretty.writeln (Pretty.big_list "type arities:" (flat (map pretty_arities arities)));
wenzelm@4250
   177
    Pretty.writeln (Pretty.big_list "consts:" (map pretty_const consts))
wenzelm@4250
   178
  end;
wenzelm@4250
   179
wenzelm@4250
   180
wenzelm@4250
   181
(* print axioms, oracles, theorems *)
wenzelm@4250
   182
wenzelm@3811
   183
fun print_thy thy =
paulson@1591
   184
  let
wenzelm@3990
   185
    val {sign, axioms, oracles, ...} = rep_theory thy;
wenzelm@3990
   186
    val axioms = Symtab.dest axioms;
wenzelm@3811
   187
    val oras = map fst (Symtab.dest oracles);
paulson@1591
   188
wenzelm@3936
   189
    fun prt_axm (a, t) = Pretty.block
wenzelm@3990
   190
      [Pretty.str (Sign.cond_extern sign Theory.axiomK a ^ ":"), Pretty.brk 1,
wenzelm@3936
   191
        Pretty.quote (Sign.pretty_term sign t)];
paulson@1591
   192
  in
wenzelm@3990
   193
    Pretty.writeln (Pretty.big_list "axioms:" (map prt_axm axioms));
wenzelm@3990
   194
    Pretty.writeln (Pretty.strs ("oracles:" :: oras));
wenzelm@4782
   195
    print_data thy "Pure/theorems"	(*forward reference!*)
paulson@1591
   196
  end;
paulson@1591
   197
wenzelm@4250
   198
fun print_theory thy = (print_sign (sign_of thy); print_thy thy);
paulson@1591
   199
paulson@1591
   200
paulson@1591
   201
wenzelm@4250
   202
(** print_goals **)
paulson@1591
   203
wenzelm@4250
   204
(*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
wenzelm@4250
   205
wenzelm@4250
   206
(*also show consts in case of showing types?*)
wenzelm@3851
   207
val show_consts = ref false;
wenzelm@3851
   208
paulson@1591
   209
paulson@1591
   210
local
wenzelm@3851
   211
wenzelm@3851
   212
  (* utils *)
paulson@1591
   213
paulson@1591
   214
  fun ins_entry (x, y) [] = [(x, [y])]
paulson@1591
   215
    | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
wenzelm@3851
   216
        if x = x' then (x', y ins ys') :: pairs
paulson@1591
   217
        else pair :: ins_entry (x, y) pairs;
paulson@1591
   218
wenzelm@3990
   219
  fun add_consts (Const (c, T), env) = ins_entry (T, (c, T)) env
wenzelm@3851
   220
    | add_consts (t $ u, env) = add_consts (u, add_consts (t, env))
wenzelm@3851
   221
    | add_consts (Abs (_, _, t), env) = add_consts (t, env)
wenzelm@3851
   222
    | add_consts (_, env) = env;
paulson@1591
   223
wenzelm@3851
   224
  fun add_vars (Free (x, T), env) = ins_entry (T, (x, ~1)) env
wenzelm@3851
   225
    | add_vars (Var (xi, T), env) = ins_entry (T, xi) env
wenzelm@3851
   226
    | add_vars (Abs (_, _, t), env) = add_vars (t, env)
wenzelm@3851
   227
    | add_vars (t $ u, env) = add_vars (u, add_vars (t, env))
wenzelm@3851
   228
    | add_vars (_, env) = env;
paulson@1591
   229
wenzelm@3851
   230
  fun add_varsT (Type (_, Ts), env) = foldr add_varsT (Ts, env)
wenzelm@3851
   231
    | add_varsT (TFree (x, S), env) = ins_entry (S, (x, ~1)) env
wenzelm@3851
   232
    | add_varsT (TVar (xi, S), env) = ins_entry (S, xi) env;
paulson@1591
   233
wenzelm@4440
   234
  fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
wenzelm@4440
   235
  fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
paulson@1591
   236
paulson@1591
   237
wenzelm@3851
   238
  (* prepare atoms *)
wenzelm@3851
   239
wenzelm@3990
   240
  fun consts_of t = sort_cnsts (add_consts (t, []));
wenzelm@3851
   241
  fun vars_of t = sort_idxs (add_vars (t, []));
wenzelm@3851
   242
  fun varsT_of t = rev (sort_idxs (it_term_types add_varsT (t, [])));
wenzelm@3851
   243
wenzelm@3851
   244
in
wenzelm@3851
   245
wenzelm@3851
   246
  fun print_goals maxgoals state =
wenzelm@3851
   247
    let
wenzelm@3851
   248
      val {sign, ...} = rep_thm state;
paulson@1591
   249
wenzelm@3851
   250
      val prt_term = Sign.pretty_term sign;
wenzelm@3851
   251
      val prt_typ = Sign.pretty_typ sign;
wenzelm@3851
   252
      val prt_sort = Sign.pretty_sort sign;
wenzelm@3851
   253
wenzelm@3851
   254
      fun prt_atoms prt prtT (X, xs) = Pretty.block
wenzelm@3851
   255
        [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
wenzelm@3851
   256
          Pretty.brk 1, prtT X];
paulson@1591
   257
wenzelm@3851
   258
      fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@3851
   259
        | prt_var xi = prt_term (Syntax.var xi);
wenzelm@3851
   260
wenzelm@3851
   261
      fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@3851
   262
        | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@3851
   263
wenzelm@3990
   264
      val prt_consts = prt_atoms (prt_term o Const) prt_typ;
wenzelm@3851
   265
      val prt_vars = prt_atoms prt_var prt_typ;
wenzelm@3851
   266
      val prt_varsT = prt_atoms prt_varT prt_sort;
paulson@1591
   267
paulson@1591
   268
wenzelm@3851
   269
      fun print_list _ _ [] = ()
wenzelm@3851
   270
        | print_list name prt lst = (writeln "";
wenzelm@3851
   271
            Pretty.writeln (Pretty.big_list name (map prt lst)));
wenzelm@3851
   272
wenzelm@3851
   273
      fun print_subgoals (_, []) = ()
wenzelm@3851
   274
        | print_subgoals (n, A :: As) = (Pretty.writeln (Pretty.blk (0,
wenzelm@3851
   275
            [Pretty.str (" " ^ string_of_int n ^ ". "), prt_term A]));
wenzelm@3851
   276
              print_subgoals (n + 1, As));
paulson@1591
   277
wenzelm@3851
   278
      val print_ffpairs =
wenzelm@3851
   279
        print_list "Flex-flex pairs:" (prt_term o Logic.mk_flexpair);
wenzelm@3851
   280
wenzelm@3851
   281
      val print_consts = print_list "Constants:" prt_consts o consts_of;
wenzelm@3851
   282
      val print_vars = print_list "Variables:" prt_vars o vars_of;
wenzelm@3851
   283
      val print_varsT = print_list "Type variables:" prt_varsT o varsT_of;
wenzelm@3851
   284
wenzelm@3851
   285
wenzelm@3851
   286
      val {prop, ...} = rep_thm state;
wenzelm@3851
   287
      val (tpairs, As, B) = Logic.strip_horn prop;
wenzelm@3851
   288
      val ngoals = length As;
paulson@1591
   289
wenzelm@3851
   290
      fun print_gs (types, sorts) =
wenzelm@3851
   291
       (Pretty.writeln (prt_term B);
wenzelm@3851
   292
        if ngoals = 0 then writeln "No subgoals!"
wenzelm@3851
   293
        else if ngoals > maxgoals then
wenzelm@3851
   294
          (print_subgoals (1, take (maxgoals, As));
wenzelm@3851
   295
            writeln ("A total of " ^ string_of_int ngoals ^ " subgoals..."))
wenzelm@3851
   296
        else print_subgoals (1, As);
wenzelm@3851
   297
wenzelm@3851
   298
        print_ffpairs tpairs;
paulson@1591
   299
wenzelm@3851
   300
        if types andalso ! show_consts then print_consts prop else ();
wenzelm@3851
   301
        if types then print_vars prop else ();
wenzelm@3851
   302
        if sorts then print_varsT prop else ());
wenzelm@3851
   303
    in
wenzelm@3851
   304
      setmp show_no_free_types true
wenzelm@3851
   305
        (setmp show_types (! show_types orelse ! show_sorts)
wenzelm@3851
   306
          (setmp show_sorts false print_gs))
wenzelm@3851
   307
     (! show_types orelse ! show_sorts, ! show_sorts)
wenzelm@3851
   308
  end;
paulson@1591
   309
wenzelm@3851
   310
end;
paulson@1591
   311
paulson@1591
   312
paulson@1591
   313
end;
paulson@1591
   314
paulson@1591
   315
open Display;