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