src/Pure/display.ML
changeset 12081 f9735aad76dc
parent 12067 b2f5fbb39f14
child 12418 753054ec8e88
equal deleted inserted replaced
12080:4c1e3a2a87c3 12081:f9735aad76dc
     6 Printing of theories, theorems, etc.
     6 Printing of theories, theorems, etc.
     7 *)
     7 *)
     8 
     8 
     9 signature BASIC_DISPLAY =
     9 signature BASIC_DISPLAY =
    10 sig
    10 sig
       
    11   val goals_limit: int ref
    11   val show_hyps: bool ref
    12   val show_hyps: bool ref
    12   val show_tags: bool ref
    13   val show_tags: bool ref
    13   val string_of_thm: thm -> string
    14   val string_of_thm: thm -> string
    14   val print_thm: thm -> unit
    15   val print_thm: thm -> unit
    15   val print_thms: thm list -> unit
    16   val print_thms: thm list -> unit
    25 end;
    26 end;
    26 
    27 
    27 signature DISPLAY =
    28 signature DISPLAY =
    28 sig
    29 sig
    29   include BASIC_DISPLAY
    30   include BASIC_DISPLAY
    30   val pretty_thm_aux: (sort -> Pretty.T) -> (term -> Pretty.T) -> bool -> thm -> Pretty.T
    31   val pretty_thm_aux: (sort -> Pretty.T) * (term -> Pretty.T) -> bool -> thm -> Pretty.T
    31   val pretty_thm_no_quote: thm -> Pretty.T
    32   val pretty_thm_no_quote: thm -> Pretty.T
    32   val pretty_thm: thm -> Pretty.T
    33   val pretty_thm: thm -> Pretty.T
    33   val pretty_thms: thm list -> Pretty.T
    34   val pretty_thms: thm list -> Pretty.T
    34   val pretty_thm_sg: Sign.sg -> thm -> Pretty.T
    35   val pretty_thm_sg: Sign.sg -> thm -> Pretty.T
    35   val pretty_thms_sg: Sign.sg -> thm list -> Pretty.T
    36   val pretty_thms_sg: Sign.sg -> thm list -> Pretty.T
    40   val pprint_cterm: cterm -> pprint_args -> unit
    41   val pprint_cterm: cterm -> pprint_args -> unit
    41   val pretty_theory: theory -> Pretty.T
    42   val pretty_theory: theory -> Pretty.T
    42   val pprint_theory: theory -> pprint_args -> unit
    43   val pprint_theory: theory -> pprint_args -> unit
    43   val pretty_full_theory: theory -> Pretty.T list
    44   val pretty_full_theory: theory -> Pretty.T list
    44   val pretty_name_space: string * NameSpace.T -> Pretty.T
    45   val pretty_name_space: string * NameSpace.T -> Pretty.T
    45   val pretty_goals_marker: string -> int -> thm -> Pretty.T list
    46   val pretty_goals_aux: (sort -> Pretty.T) * (typ -> Pretty.T) * (term -> Pretty.T)
       
    47     -> string -> bool * bool -> int -> thm -> Pretty.T list
    46   val pretty_goals: int -> thm -> Pretty.T list
    48   val pretty_goals: int -> thm -> Pretty.T list
    47   val pretty_sub_goals: bool -> int -> thm -> Pretty.T list
       
    48   val print_goals_marker: string -> int -> thm -> unit
       
    49   val print_goals: int -> thm -> unit
    49   val print_goals: int -> thm -> unit
    50   val current_goals_markers: (string * string * string) ref
    50   val current_goals_markers: (string * string * string) ref
    51   val print_current_goals_default: (int -> int -> thm -> unit)
    51   val print_current_goals_default: (int -> int -> thm -> unit)
    52   val print_current_goals_fn : (int -> int -> thm -> unit) ref
    52   val print_current_goals_fn : (int -> int -> thm -> unit) ref
    53 end;
    53 end;
    56 struct
    56 struct
    57 
    57 
    58 
    58 
    59 (** print thm **)
    59 (** print thm **)
    60 
    60 
       
    61 val goals_limit = ref 10;       (*max number of goals to print -- set by user*)
    61 val show_hyps = ref true;       (*false: print meta-hypotheses as dots*)
    62 val show_hyps = ref true;       (*false: print meta-hypotheses as dots*)
    62 val show_tags = ref false;      (*false: suppress tags*)
    63 val show_tags = ref false;      (*false: suppress tags*)
    63 
    64 
    64 fun pretty_tag (name, args) = Pretty.strs (name :: map quote args);
    65 fun pretty_tag (name, args) = Pretty.strs (name :: map quote args);
    65 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    66 val pretty_tags = Pretty.list "[" "]" o map pretty_tag;
    66 
    67 
    67 fun pretty_thm_aux pretty_sort pretty_term quote th =
    68 fun pretty_thm_aux (pretty_sort, pretty_term) quote th =
    68   let
    69   let
    69     val {hyps, prop, der = (ora, _), ...} = rep_thm th;
    70     val {hyps, prop, der = (ora, _), ...} = rep_thm th;
    70     val xshyps = Thm.extra_shyps th;
    71     val xshyps = Thm.extra_shyps th;
    71     val (_, tags) = Thm.get_name_tags th;
    72     val (_, tags) = Thm.get_name_tags th;
    72 
    73 
    86       else [Pretty.brk 1, pretty_tags tags];
    87       else [Pretty.brk 1, pretty_tags tags];
    87   in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
    88   in Pretty.block (prt_term prop :: (hsymbs @ tsymbs)) end;
    88 
    89 
    89 fun gen_pretty_thm quote th =
    90 fun gen_pretty_thm quote th =
    90   let val sg = Thm.sign_of_thm th
    91   let val sg = Thm.sign_of_thm th
    91   in pretty_thm_aux (Sign.pretty_sort sg) (Sign.pretty_term sg) quote th end;
    92   in pretty_thm_aux (Sign.pretty_sort sg, Sign.pretty_term sg) quote th end;
    92 
    93 
    93 val pretty_thm = gen_pretty_thm true;
    94 val pretty_thm = gen_pretty_thm true;
    94 val pretty_thm_no_quote = gen_pretty_thm false;
    95 val pretty_thm_no_quote = gen_pretty_thm false;
    95 
    96 
    96 
    97 
   250 
   251 
   251 (*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
   252 (*print thm A1,...,An/B in "goal style" -- premises as numbered subgoals*)
   252 
   253 
   253 local
   254 local
   254 
   255 
   255   (* utils *)
   256 fun ins_entry (x, y) [] = [(x, [y])]
   256 
   257   | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
   257   fun ins_entry (x, y) [] = [(x, [y])]
   258       if x = x' then (x', y ins ys') :: pairs
   258     | ins_entry (x, y) ((pair as (x', ys')) :: pairs) =
   259       else pair :: ins_entry (x, y) pairs;
   259         if x = x' then (x', y ins ys') :: pairs
   260 
   260         else pair :: ins_entry (x, y) pairs;
   261 fun add_consts (Const (c, T), env) = ins_entry (T, (c, T)) env
   261 
   262   | add_consts (t $ u, env) = add_consts (u, add_consts (t, env))
   262   fun add_consts (Const (c, T), env) = ins_entry (T, (c, T)) env
   263   | add_consts (Abs (_, _, t), env) = add_consts (t, env)
   263     | add_consts (t $ u, env) = add_consts (u, add_consts (t, env))
   264   | add_consts (_, env) = env;
   264     | add_consts (Abs (_, _, t), env) = add_consts (t, env)
   265 
   265     | add_consts (_, env) = env;
   266 fun add_vars (Free (x, T), env) = ins_entry (T, (x, ~1)) env
   266 
   267   | add_vars (Var (xi, T), env) = ins_entry (T, xi) env
   267   fun add_vars (Free (x, T), env) = ins_entry (T, (x, ~1)) env
   268   | add_vars (Abs (_, _, t), env) = add_vars (t, env)
   268     | add_vars (Var (xi, T), env) = ins_entry (T, xi) env
   269   | add_vars (t $ u, env) = add_vars (u, add_vars (t, env))
   269     | add_vars (Abs (_, _, t), env) = add_vars (t, env)
   270   | add_vars (_, env) = env;
   270     | add_vars (t $ u, env) = add_vars (u, add_vars (t, env))
   271 
   271     | add_vars (_, env) = env;
   272 fun add_varsT (Type (_, Ts), env) = foldr add_varsT (Ts, env)
   272 
   273   | add_varsT (TFree (x, S), env) = ins_entry (S, (x, ~1)) env
   273   fun add_varsT (Type (_, Ts), env) = foldr add_varsT (Ts, env)
   274   | add_varsT (TVar (xi, S), env) = ins_entry (S, xi) env;
   274     | add_varsT (TFree (x, S), env) = ins_entry (S, (x, ~1)) env
   275 
   275     | add_varsT (TVar (xi, S), env) = ins_entry (S, xi) env;
   276 fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
   276 
   277 fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
   277   fun sort_idxs vs = map (apsnd (sort (prod_ord string_ord int_ord))) vs;
   278 
   278   fun sort_cnsts cs = map (apsnd (sort_wrt fst)) cs;
   279 fun consts_of t = sort_cnsts (add_consts (t, []));
   279 
   280 fun vars_of t = sort_idxs (add_vars (t, []));
   280 
   281 fun varsT_of t = rev (sort_idxs (it_term_types add_varsT (t, [])));
   281   (* prepare atoms *)
       
   282 
       
   283   fun consts_of t = sort_cnsts (add_consts (t, []));
       
   284   fun vars_of t = sort_idxs (add_vars (t, []));
       
   285   fun varsT_of t = rev (sort_idxs (it_term_types add_varsT (t, [])));
       
   286 
       
   287   fun pretty_goals_marker_aux begin_goal print_msg print_goal maxgoals state =
       
   288     let
       
   289       val {sign, ...} = rep_thm state;
       
   290 
       
   291       val prt_term = Sign.pretty_term sign;
       
   292       val prt_typ = Sign.pretty_typ sign;
       
   293       val prt_sort = Sign.pretty_sort sign;
       
   294 
       
   295       fun prt_atoms prt prtT (X, xs) = Pretty.block
       
   296         [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
       
   297           Pretty.brk 1, prtT X];
       
   298 
       
   299       fun prt_var (x, ~1) = prt_term (Syntax.free x)
       
   300         | prt_var xi = prt_term (Syntax.var xi);
       
   301 
       
   302       fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
       
   303         | prt_varT xi = prt_typ (TVar (xi, []));
       
   304 
       
   305       val prt_consts = prt_atoms (prt_term o Const) prt_typ;
       
   306       val prt_vars = prt_atoms prt_var prt_typ;
       
   307       val prt_varsT = prt_atoms prt_varT prt_sort;
       
   308 
       
   309 
       
   310       fun pretty_list _ _ [] = []
       
   311         | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
       
   312 
       
   313       fun pretty_subgoal (n, A) =
       
   314         Pretty.blk (0, [Pretty.str (begin_goal ^ " " ^ string_of_int n ^ ". "), prt_term A]);
       
   315       fun pretty_subgoals As = map pretty_subgoal (1 upto length As ~~ As);
       
   316 
       
   317       val pretty_ffpairs = pretty_list "flex-flex pairs:" (prt_term o Logic.mk_flexpair);
       
   318 
       
   319       val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
       
   320       val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
       
   321       val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
       
   322 
       
   323 
       
   324       val {prop, ...} = rep_thm state;
       
   325       val (tpairs, As, B) = Logic.strip_horn prop;
       
   326       val ngoals = length As;
       
   327 
       
   328       fun pretty_gs (types, sorts) =
       
   329         (if print_goal then [prt_term B] else []) @
       
   330          (if ngoals = 0 then [Pretty.str "No subgoals!"]
       
   331           else if ngoals > maxgoals then
       
   332             pretty_subgoals (take (maxgoals, As)) @
       
   333             (if print_msg then
       
   334                 [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
       
   335              else [])
       
   336           else pretty_subgoals As) @
       
   337         pretty_ffpairs tpairs @
       
   338         (if ! show_consts then pretty_consts prop else []) @
       
   339         (if types then pretty_vars prop else []) @
       
   340         (if sorts then pretty_varsT prop else []);
       
   341     in
       
   342       setmp show_no_free_types true
       
   343         (setmp show_types (! show_types orelse ! show_sorts)
       
   344           (setmp show_sorts false pretty_gs))
       
   345      (! show_types orelse ! show_sorts, ! show_sorts)
       
   346   end;
       
   347 
   282 
   348 in
   283 in
   349   fun pretty_goals_marker bg = pretty_goals_marker_aux bg true true;
   284 
   350   val print_goals_marker = (Pretty.writeln o Pretty.chunks) ooo pretty_goals_marker;
   285 fun pretty_goals_aux (prt_sort, prt_typ, prt_term) begin_goal (msg, main) maxgoals state =
   351   val pretty_sub_goals = pretty_goals_marker_aux "" false;
   286   let
   352   val pretty_goals = pretty_goals_marker "";
   287     fun prt_atoms prt prtT (X, xs) = Pretty.block
   353   val print_goals = print_goals_marker "";
   288       [Pretty.block (Pretty.commas (map prt xs)), Pretty.str " ::",
       
   289         Pretty.brk 1, prtT X];
       
   290 
       
   291     fun prt_var (x, ~1) = prt_term (Syntax.free x)
       
   292       | prt_var xi = prt_term (Syntax.var xi);
       
   293 
       
   294     fun prt_varT (x, ~1) = prt_typ (TFree (x, []))
       
   295       | prt_varT xi = prt_typ (TVar (xi, []));
       
   296 
       
   297     val prt_consts = prt_atoms (prt_term o Const) prt_typ;
       
   298     val prt_vars = prt_atoms prt_var prt_typ;
       
   299     val prt_varsT = prt_atoms prt_varT prt_sort;
       
   300 
       
   301 
       
   302     fun pretty_list _ _ [] = []
       
   303       | pretty_list name prt lst = [Pretty.big_list name (map prt lst)];
       
   304 
       
   305     fun pretty_subgoal (n, A) =
       
   306       Pretty.blk (0, [Pretty.str (begin_goal ^ " " ^ string_of_int n ^ ". "), prt_term A]);
       
   307     fun pretty_subgoals As = map pretty_subgoal (1 upto length As ~~ As);
       
   308 
       
   309     val pretty_ffpairs = pretty_list "flex-flex pairs:" (prt_term o Logic.mk_flexpair);
       
   310 
       
   311     val pretty_consts = pretty_list "constants:" prt_consts o consts_of;
       
   312     val pretty_vars = pretty_list "variables:" prt_vars o vars_of;
       
   313     val pretty_varsT = pretty_list "type variables:" prt_varsT o varsT_of;
       
   314 
       
   315 
       
   316     val {prop, ...} = rep_thm state;
       
   317     val (tpairs, As, B) = Logic.strip_horn prop;
       
   318     val ngoals = length As;
       
   319 
       
   320     fun pretty_gs (types, sorts) =
       
   321       (if main then [prt_term B] else []) @
       
   322        (if ngoals = 0 then [Pretty.str "No subgoals!"]
       
   323         else if ngoals > maxgoals then
       
   324           pretty_subgoals (take (maxgoals, As)) @
       
   325           (if msg then [Pretty.str ("A total of " ^ string_of_int ngoals ^ " subgoals...")]
       
   326            else [])
       
   327         else pretty_subgoals As) @
       
   328       pretty_ffpairs tpairs @
       
   329       (if ! show_consts then pretty_consts prop else []) @
       
   330       (if types then pretty_vars prop else []) @
       
   331       (if sorts then pretty_varsT prop else []);
       
   332   in
       
   333     setmp show_no_free_types true
       
   334       (setmp show_types (! show_types orelse ! show_sorts)
       
   335         (setmp show_sorts false pretty_gs))
       
   336    (! show_types orelse ! show_sorts, ! show_sorts)
       
   337   end;
       
   338 
       
   339 fun pretty_goals_marker bg n th =
       
   340   let val sg = Thm.sign_of_thm th in
       
   341     pretty_goals_aux (Sign.pretty_sort sg, Sign.pretty_typ sg, Sign.pretty_term sg)
       
   342       bg (true, true) n th
       
   343   end;
       
   344 
       
   345 val pretty_goals = pretty_goals_marker "";
       
   346 val print_goals_marker = (Pretty.writeln o Pretty.chunks) ooo pretty_goals_marker;
       
   347 val print_goals = print_goals_marker "";
       
   348 
   354 end;
   349 end;
   355 
   350 
   356 
   351 
   357 (* print_current_goals *)
   352 (* print_current_goals *)
   358 
   353