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