src/Pure/goal_display.ML
author wenzelm
Mon Mar 10 15:04:01 2014 +0100 (2014-03-10)
changeset 56026 893fe12639bc
parent 52284 b12f2cef3ee5
child 56438 7f6b2634d853
permissions -rw-r--r--
tuned signature -- prefer Name_Space.get with its builtin error;
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@52284
    84
    val prt_term =
wenzelm@52284
    85
      singleton (Syntax.uncheck_terms ctxt) #>
wenzelm@52284
    86
      Type_Annotation.ignore_free_types #>
wenzelm@52284
    87
      Syntax.unparse_term ctxt;
wenzelm@32145
    88
wenzelm@32089
    89
    fun prt_atoms prt prtT (X, xs) = Pretty.block
wenzelm@32089
    90
      [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
wenzelm@32089
    91
        Pretty.brk 1, prtT X];
wenzelm@32089
    92
wenzelm@32145
    93
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@32145
    94
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@32089
    95
wenzelm@32145
    96
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@32145
    97
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@32089
    98
wenzelm@32145
    99
    val prt_consts = prt_atoms (prt_term o Const) prt_typ;
wenzelm@32145
   100
    val prt_vars = prt_atoms prt_var prt_typ;
wenzelm@32145
   101
    val prt_varsT = prt_atoms prt_varT prt_sort;
wenzelm@32089
   102
wenzelm@32089
   103
wenzelm@32089
   104
    fun pretty_list _ _ [] = []
wenzelm@32089
   105
      | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
wenzelm@32089
   106
wenzelm@50537
   107
    fun pretty_subgoal s A =
wenzelm@50537
   108
      Pretty.markup (Markup.subgoal s) [Pretty.str (" " ^ s ^ ". "), prt_term A];
wenzelm@50537
   109
    val pretty_subgoals = map_index (fn (i, A) => pretty_subgoal (string_of_int (i + 1)) A);
wenzelm@32089
   110
wenzelm@32145
   111
    val pretty_ffpairs = pretty_list "flex-flex pairs:" (pretty_flexpair ctxt);
wenzelm@32089
   112
wenzelm@32089
   113
    val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
wenzelm@32089
   114
    val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
wenzelm@32089
   115
    val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
wenzelm@32089
   116
wenzelm@32089
   117
wenzelm@32089
   118
    val {prop, tpairs, ...} = Thm.rep_thm state;
wenzelm@32089
   119
    val (As, B) = Logic.strip_horn prop;
wenzelm@32089
   120
    val ngoals = length As;
wenzelm@32089
   121
  in
wenzelm@50543
   122
    (if show_main_goal then [Pretty.mark Markup.goal (prt_term B)] else []) @
wenzelm@39134
   123
     (if ngoals = 0 then [Pretty.str "No subgoals!"]
wenzelm@39134
   124
      else if ngoals > goals_limit then
wenzelm@39134
   125
        pretty_subgoals (take goals_limit As) @
wenzelm@51959
   126
        [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
wenzelm@39134
   127
      else pretty_subgoals As) @
wenzelm@39134
   128
    pretty_ffpairs tpairs @
wenzelm@39134
   129
    (if show_consts then pretty_consts prop else []) @
wenzelm@39134
   130
    (if show_types then pretty_vars prop else []) @
wenzelm@39134
   131
    (if show_sorts0 then pretty_varsT prop else [])
wenzelm@32089
   132
  end;
wenzelm@32089
   133
wenzelm@39125
   134
fun pretty_goals_without_context th =
wenzelm@39125
   135
  let val ctxt =
wenzelm@39125
   136
    Config.put show_main_goal true (Syntax.init_pretty_global (Thm.theory_of_thm th))
wenzelm@39125
   137
  in pretty_goals ctxt th end;
wenzelm@32089
   138
wenzelm@51959
   139
val pretty_goal = Pretty.chunks oo pretty_goals;
wenzelm@51958
   140
val string_of_goal = Pretty.string_of oo pretty_goal;
wenzelm@49847
   141
wenzelm@32089
   142
end;
wenzelm@32089
   143
wenzelm@32089
   144
end;
wenzelm@32089
   145