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