src/Pure/goal_display.ML
author wenzelm
Tue May 28 23:06:32 2013 +0200 (2013-05-28)
changeset 52211 66bc827e37f8
parent 52185 1b481b490454
child 52284 b12f2cef3ee5
permissions -rw-r--r--
explicit support for type annotations within printed syntax trees;
eliminated obsolete show_free_types;
wenzelm@32187
     1
(*  Title:      Pure/goal_display.ML
wenzelm@32089
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@32089
     3
    Author:     Makarius
wenzelm@32089
     4
wenzelm@32089
     5
Display tactical goal state.
wenzelm@32089
     6
*)
wenzelm@32089
     7
wenzelm@32187
     8
signature GOAL_DISPLAY =
wenzelm@32089
     9
sig
wenzelm@39163
    10
  val goals_limit_raw: Config.raw
wenzelm@39125
    11
  val goals_limit: int Config.T
wenzelm@39163
    12
  val show_main_goal_raw: Config.raw
wenzelm@39125
    13
  val show_main_goal: bool Config.T
wenzelm@39163
    14
  val show_consts_raw: Config.raw
wenzelm@39050
    15
  val show_consts: bool Config.T
wenzelm@32145
    16
  val pretty_flexpair: Proof.context -> term * term -> Pretty.T
wenzelm@39125
    17
  val pretty_goals: Proof.context -> thm -> Pretty.T list
wenzelm@39125
    18
  val pretty_goals_without_context: thm -> Pretty.T list
wenzelm@51958
    19
  val pretty_goal: Proof.context -> thm -> Pretty.T
wenzelm@51958
    20
  val string_of_goal: Proof.context -> thm -> string
wenzelm@32089
    21
end;
wenzelm@32089
    22
wenzelm@32187
    23
structure Goal_Display: GOAL_DISPLAY =
wenzelm@32089
    24
struct
wenzelm@32089
    25
wenzelm@51960
    26
val goals_limit_raw = Config.declare_option "goals_limit";
wenzelm@39163
    27
val goals_limit = Config.int goals_limit_raw;
wenzelm@39125
    28
wenzelm@52043
    29
val show_main_goal_raw = Config.declare_option "show_main_goal";
wenzelm@39163
    30
val show_main_goal = Config.bool show_main_goal_raw;
wenzelm@39125
    31
wenzelm@52043
    32
val show_consts_raw = Config.declare_option "show_consts";
wenzelm@39163
    33
val show_consts = Config.bool show_consts_raw;
wenzelm@32089
    34
wenzelm@32145
    35
fun pretty_flexpair ctxt (t, u) = Pretty.block
wenzelm@32145
    36
  [Syntax.pretty_term ctxt t, Pretty.str " =?=", Pretty.brk 1, Syntax.pretty_term ctxt u];
wenzelm@32089
    37
wenzelm@32089
    38
wenzelm@32089
    39
(*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
wenzelm@32089
    40
wenzelm@32089
    41
local
wenzelm@32089
    42
wenzelm@32089
    43
fun ins_entry (x, y) =
wenzelm@32089
    44
  AList.default (op =) (x, []) #>
wenzelm@32089
    45
  AList.map_entry (op =) x (insert (op =) y);
wenzelm@32089
    46
wenzelm@32089
    47
val add_consts = Term.fold_aterms
wenzelm@32089
    48
  (fn Const (c, T) => ins_entry (T, (c, T))
wenzelm@32089
    49
    | _ => I);
wenzelm@32089
    50
wenzelm@32089
    51
val add_vars = Term.fold_aterms
wenzelm@32089
    52
  (fn Free (x, T) => ins_entry (T, (x, ~1))
wenzelm@32089
    53
    | Var (xi, T) => ins_entry (T, xi)
wenzelm@32089
    54
    | _ => I);
wenzelm@32089
    55
wenzelm@32089
    56
val add_varsT = Term.fold_atyps
wenzelm@32089
    57
  (fn TFree (x, S) => ins_entry (S, (x, ~1))
wenzelm@32089
    58
    | TVar (xi, S) => ins_entry (S, xi)
wenzelm@32089
    59
    | _ => I);
wenzelm@32089
    60
wenzelm@32089
    61
fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
wenzelm@32089
    62
fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
wenzelm@32089
    63
wenzelm@32089
    64
fun consts_of t = sort_cnsts (add_consts t []);
wenzelm@32089
    65
fun vars_of t = sort_idxs (add_vars t []);
wenzelm@32089
    66
fun varsT_of t = rev (sort_idxs (Term.fold_types add_varsT t []));
wenzelm@32089
    67
wenzelm@32089
    68
in
wenzelm@32089
    69
wenzelm@39125
    70
fun pretty_goals ctxt0 state =
wenzelm@32089
    71
  let
wenzelm@39134
    72
    val ctxt = ctxt0
wenzelm@52185
    73
      |> Config.put show_types (Config.get ctxt0 show_types orelse Config.get ctxt0 show_sorts)
wenzelm@39134
    74
      |> Config.put show_sorts false;
wenzelm@39125
    75
wenzelm@39134
    76
    val show_sorts0 = Config.get ctxt0 show_sorts;
wenzelm@39134
    77
    val show_types = Config.get ctxt show_types;
wenzelm@39134
    78
    val show_consts = Config.get ctxt show_consts
wenzelm@39134
    79
    val show_main_goal = Config.get ctxt show_main_goal;
wenzelm@39125
    80
    val goals_limit = Config.get ctxt goals_limit;
wenzelm@39115
    81
wenzelm@32145
    82
    val prt_sort = Syntax.pretty_sort ctxt;
wenzelm@32145
    83
    val prt_typ = Syntax.pretty_typ ctxt;
wenzelm@52211
    84
    val prt_term = Syntax.pretty_term ctxt o Type_Annotation.ignore_free_types;
wenzelm@32145
    85
wenzelm@32089
    86
    fun prt_atoms prt prtT (X, xs) = Pretty.block
wenzelm@32089
    87
      [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
wenzelm@32089
    88
        Pretty.brk 1, prtT X];
wenzelm@32089
    89
wenzelm@32145
    90
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@32145
    91
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@32089
    92
wenzelm@32145
    93
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@32145
    94
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@32089
    95
wenzelm@32145
    96
    val prt_consts = prt_atoms (prt_term o Const) prt_typ;
wenzelm@32145
    97
    val prt_vars = prt_atoms prt_var prt_typ;
wenzelm@32145
    98
    val prt_varsT = prt_atoms prt_varT prt_sort;
wenzelm@32089
    99
wenzelm@32089
   100
wenzelm@32089
   101
    fun pretty_list _ _ [] = []
wenzelm@32089
   102
      | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
wenzelm@32089
   103
wenzelm@50537
   104
    fun pretty_subgoal s A =
wenzelm@50537
   105
      Pretty.markup (Markup.subgoal s) [Pretty.str (" " ^ s ^ ". "), prt_term A];
wenzelm@50537
   106
    val pretty_subgoals = map_index (fn (i, A) => pretty_subgoal (string_of_int (i + 1)) A);
wenzelm@32089
   107
wenzelm@32145
   108
    val pretty_ffpairs = pretty_list "flex-flex pairs:" (pretty_flexpair ctxt);
wenzelm@32089
   109
wenzelm@32089
   110
    val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
wenzelm@32089
   111
    val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
wenzelm@32089
   112
    val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
wenzelm@32089
   113
wenzelm@32089
   114
wenzelm@32089
   115
    val {prop, tpairs, ...} = Thm.rep_thm state;
wenzelm@32089
   116
    val (As, B) = Logic.strip_horn prop;
wenzelm@32089
   117
    val ngoals = length As;
wenzelm@32089
   118
  in
wenzelm@50543
   119
    (if show_main_goal then [Pretty.mark Markup.goal (prt_term B)] else []) @
wenzelm@39134
   120
     (if ngoals = 0 then [Pretty.str "No subgoals!"]
wenzelm@39134
   121
      else if ngoals > goals_limit then
wenzelm@39134
   122
        pretty_subgoals (take goals_limit As) @
wenzelm@51959
   123
        [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
wenzelm@39134
   124
      else pretty_subgoals As) @
wenzelm@39134
   125
    pretty_ffpairs tpairs @
wenzelm@39134
   126
    (if show_consts then pretty_consts prop else []) @
wenzelm@39134
   127
    (if show_types then pretty_vars prop else []) @
wenzelm@39134
   128
    (if show_sorts0 then pretty_varsT prop else [])
wenzelm@32089
   129
  end;
wenzelm@32089
   130
wenzelm@39125
   131
fun pretty_goals_without_context th =
wenzelm@39125
   132
  let val ctxt =
wenzelm@39125
   133
    Config.put show_main_goal true (Syntax.init_pretty_global (Thm.theory_of_thm th))
wenzelm@39125
   134
  in pretty_goals ctxt th end;
wenzelm@32089
   135
wenzelm@51959
   136
val pretty_goal = Pretty.chunks oo pretty_goals;
wenzelm@51958
   137
val string_of_goal = Pretty.string_of oo pretty_goal;
wenzelm@49847
   138
wenzelm@32089
   139
end;
wenzelm@32089
   140
wenzelm@32089
   141
end;
wenzelm@32089
   142