src/Pure/goal_display.ML
author wenzelm
Sun Nov 11 20:31:46 2012 +0100 (2012-11-11)
changeset 50081 9b92ee8dec98
parent 49847 ed5080c03165
child 50201 c26369c9eda6
permissions -rw-r--r--
tuned;
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@39125
    10
  val goals_limit_default: int Unsynchronized.ref
wenzelm@39163
    11
  val goals_limit_raw: Config.raw
wenzelm@39125
    12
  val goals_limit: int Config.T
wenzelm@39125
    13
  val goals_total: bool Config.T
wenzelm@39125
    14
  val show_main_goal_default: bool Unsynchronized.ref
wenzelm@39163
    15
  val show_main_goal_raw: Config.raw
wenzelm@39125
    16
  val show_main_goal: bool Config.T
wenzelm@39050
    17
  val show_consts_default: bool Unsynchronized.ref
wenzelm@39163
    18
  val show_consts_raw: Config.raw
wenzelm@39050
    19
  val show_consts: bool Config.T
wenzelm@32145
    20
  val pretty_flexpair: Proof.context -> term * term -> Pretty.T
wenzelm@39125
    21
  val pretty_goals: Proof.context -> thm -> Pretty.T list
wenzelm@39125
    22
  val pretty_goals_without_context: thm -> Pretty.T list
wenzelm@49847
    23
  val pretty_goal: {main: bool, limit: bool} -> Proof.context -> thm -> Pretty.T
wenzelm@32089
    24
end;
wenzelm@32089
    25
wenzelm@32187
    26
structure Goal_Display: GOAL_DISPLAY =
wenzelm@32089
    27
struct
wenzelm@32089
    28
wenzelm@39125
    29
val goals_limit_default = Unsynchronized.ref 10;
wenzelm@39163
    30
val goals_limit_raw = Config.declare "goals_limit" (fn _ => Config.Int (! goals_limit_default));
wenzelm@39163
    31
val goals_limit = Config.int goals_limit_raw;
wenzelm@39125
    32
wenzelm@39125
    33
val goals_total = Config.bool (Config.declare "goals_total" (fn _ => Config.Bool true));
wenzelm@39050
    34
wenzelm@39125
    35
val show_main_goal_default = Unsynchronized.ref false;
wenzelm@39163
    36
val show_main_goal_raw =
wenzelm@39125
    37
  Config.declare "show_main_goal" (fn _ => Config.Bool (! show_main_goal_default));
wenzelm@39163
    38
val show_main_goal = Config.bool show_main_goal_raw;
wenzelm@39125
    39
wenzelm@39050
    40
val show_consts_default = Unsynchronized.ref false;
wenzelm@39163
    41
val show_consts_raw = Config.declare "show_consts" (fn _ => Config.Bool (! show_consts_default));
wenzelm@39163
    42
val show_consts = Config.bool show_consts_raw;
wenzelm@32089
    43
wenzelm@32145
    44
fun pretty_flexpair ctxt (t, u) = Pretty.block
wenzelm@32145
    45
  [Syntax.pretty_term ctxt t, Pretty.str " =?=", Pretty.brk 1, Syntax.pretty_term ctxt u];
wenzelm@32089
    46
wenzelm@32089
    47
wenzelm@32089
    48
(*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
wenzelm@32089
    49
wenzelm@32089
    50
local
wenzelm@32089
    51
wenzelm@32089
    52
fun ins_entry (x, y) =
wenzelm@32089
    53
  AList.default (op =) (x, []) #>
wenzelm@32089
    54
  AList.map_entry (op =) x (insert (op =) y);
wenzelm@32089
    55
wenzelm@32089
    56
val add_consts = Term.fold_aterms
wenzelm@32089
    57
  (fn Const (c, T) => ins_entry (T, (c, T))
wenzelm@32089
    58
    | _ => I);
wenzelm@32089
    59
wenzelm@32089
    60
val add_vars = Term.fold_aterms
wenzelm@32089
    61
  (fn Free (x, T) => ins_entry (T, (x, ~1))
wenzelm@32089
    62
    | Var (xi, T) => ins_entry (T, xi)
wenzelm@32089
    63
    | _ => I);
wenzelm@32089
    64
wenzelm@32089
    65
val add_varsT = Term.fold_atyps
wenzelm@32089
    66
  (fn TFree (x, S) => ins_entry (S, (x, ~1))
wenzelm@32089
    67
    | TVar (xi, S) => ins_entry (S, xi)
wenzelm@32089
    68
    | _ => I);
wenzelm@32089
    69
wenzelm@32089
    70
fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
wenzelm@32089
    71
fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
wenzelm@32089
    72
wenzelm@32089
    73
fun consts_of t = sort_cnsts (add_consts t []);
wenzelm@32089
    74
fun vars_of t = sort_idxs (add_vars t []);
wenzelm@32089
    75
fun varsT_of t = rev (sort_idxs (Term.fold_types add_varsT t []));
wenzelm@32089
    76
wenzelm@32089
    77
in
wenzelm@32089
    78
wenzelm@39125
    79
fun pretty_goals ctxt0 state =
wenzelm@32089
    80
  let
wenzelm@39134
    81
    val ctxt = ctxt0
wenzelm@39134
    82
      |> Config.put show_free_types false
wenzelm@39134
    83
      |> Config.put show_types
wenzelm@39134
    84
       (Config.get ctxt0 show_types orelse
wenzelm@39134
    85
        Config.get ctxt0 show_sorts orelse
wenzelm@39134
    86
        Config.get ctxt0 show_all_types)
wenzelm@39134
    87
      |> Config.put show_sorts false;
wenzelm@39125
    88
wenzelm@39134
    89
    val show_sorts0 = Config.get ctxt0 show_sorts;
wenzelm@39134
    90
    val show_types = Config.get ctxt show_types;
wenzelm@39134
    91
    val show_consts = Config.get ctxt show_consts
wenzelm@39134
    92
    val show_main_goal = Config.get ctxt show_main_goal;
wenzelm@39125
    93
    val goals_limit = Config.get ctxt goals_limit;
wenzelm@39125
    94
    val goals_total = Config.get ctxt goals_total;
wenzelm@39115
    95
wenzelm@32145
    96
    val prt_sort = Syntax.pretty_sort ctxt;
wenzelm@32145
    97
    val prt_typ = Syntax.pretty_typ ctxt;
wenzelm@32145
    98
    val prt_term = Syntax.pretty_term ctxt;
wenzelm@32145
    99
wenzelm@32089
   100
    fun prt_atoms prt prtT (X, xs) = Pretty.block
wenzelm@32089
   101
      [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
wenzelm@32089
   102
        Pretty.brk 1, prtT X];
wenzelm@32089
   103
wenzelm@32145
   104
    fun prt_var (x, ~1) = prt_term (Syntax.free x)
wenzelm@32145
   105
      | prt_var xi = prt_term (Syntax.var xi);
wenzelm@32089
   106
wenzelm@32145
   107
    fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
wenzelm@32145
   108
      | prt_varT xi = prt_typ (TVar (xi, []));
wenzelm@32089
   109
wenzelm@32145
   110
    val prt_consts = prt_atoms (prt_term o Const) prt_typ;
wenzelm@32145
   111
    val prt_vars = prt_atoms prt_var prt_typ;
wenzelm@32145
   112
    val prt_varsT = prt_atoms prt_varT prt_sort;
wenzelm@32089
   113
wenzelm@32089
   114
wenzelm@32089
   115
    fun pretty_list _ _ [] = []
wenzelm@32089
   116
      | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
wenzelm@32089
   117
wenzelm@45666
   118
    fun pretty_subgoal (n, A) =
wenzelm@45666
   119
      Pretty.markup Isabelle_Markup.subgoal
wenzelm@45666
   120
        [Pretty.str (" " ^ string_of_int n ^ ". "), prt_term A];
wenzelm@32089
   121
    fun pretty_subgoals As = map pretty_subgoal (1 upto length As ~~ As);
wenzelm@32089
   122
wenzelm@32145
   123
    val pretty_ffpairs = pretty_list "flex-flex pairs:" (pretty_flexpair ctxt);
wenzelm@32089
   124
wenzelm@32089
   125
    val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
wenzelm@32089
   126
    val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
wenzelm@32089
   127
    val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
wenzelm@32089
   128
wenzelm@32089
   129
wenzelm@32089
   130
    val {prop, tpairs, ...} = Thm.rep_thm state;
wenzelm@32089
   131
    val (As, B) = Logic.strip_horn prop;
wenzelm@32089
   132
    val ngoals = length As;
wenzelm@32089
   133
  in
wenzelm@39134
   134
    (if show_main_goal then [prt_term B] else []) @
wenzelm@39134
   135
     (if ngoals = 0 then [Pretty.str "No subgoals!"]
wenzelm@39134
   136
      else if ngoals > goals_limit then
wenzelm@39134
   137
        pretty_subgoals (take goals_limit As) @
wenzelm@39134
   138
        (if goals_total then [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
wenzelm@39134
   139
         else [])
wenzelm@39134
   140
      else pretty_subgoals As) @
wenzelm@39134
   141
    pretty_ffpairs tpairs @
wenzelm@39134
   142
    (if show_consts then pretty_consts prop else []) @
wenzelm@39134
   143
    (if show_types then pretty_vars prop else []) @
wenzelm@39134
   144
    (if show_sorts0 then pretty_varsT prop else [])
wenzelm@32089
   145
  end;
wenzelm@32089
   146
wenzelm@39125
   147
fun pretty_goals_without_context th =
wenzelm@39125
   148
  let val ctxt =
wenzelm@39125
   149
    Config.put show_main_goal true (Syntax.init_pretty_global (Thm.theory_of_thm th))
wenzelm@39125
   150
  in pretty_goals ctxt th end;
wenzelm@32089
   151
wenzelm@49847
   152
fun pretty_goal {main, limit} ctxt th =
wenzelm@49847
   153
  Pretty.chunks (pretty_goals
wenzelm@49847
   154
    (ctxt
wenzelm@49847
   155
      |> Config.put show_main_goal main
wenzelm@49847
   156
      |> not limit ? Config.put goals_limit (Thm.nprems_of th)
wenzelm@49847
   157
      |> Config.put goals_total false) th);
wenzelm@49847
   158
wenzelm@32089
   159
end;
wenzelm@32089
   160
wenzelm@32089
   161
end;
wenzelm@32089
   162