src/Pure/Isar/isar_cmd.ML
author wenzelm
Mon Nov 16 11:33:14 1998 +0100 (1998-11-16 ago)
changeset 5895 457b42674b57
parent 5880 feec44106a8e
child 5913 c543568ccaca
permissions -rw-r--r--
added print_thm;
     1 (*  Title:      Pure/Isar/isar_cmd.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Non-logical toplevel commands.
     6 *)
     7 
     8 signature ISAR_CMD =
     9 sig
    10   val break: Toplevel.transition -> Toplevel.transition
    11   val exit: Toplevel.transition -> Toplevel.transition
    12   val quit: Toplevel.transition -> Toplevel.transition
    13   val use: string -> Toplevel.transition -> Toplevel.transition
    14   val use_mltext_theory: string -> Toplevel.transition -> Toplevel.transition
    15   val use_mltext: string -> Toplevel.transition -> Toplevel.transition
    16   val use_commit: Toplevel.transition -> Toplevel.transition
    17   val cd: string -> Toplevel.transition -> Toplevel.transition
    18   val pwd: Toplevel.transition -> Toplevel.transition
    19   val use_thy: string -> Toplevel.transition -> Toplevel.transition
    20   val load: string -> Toplevel.transition -> Toplevel.transition
    21   val print_theory: Toplevel.transition -> Toplevel.transition
    22   val print_syntax: Toplevel.transition -> Toplevel.transition
    23   val print_theorems: Toplevel.transition -> Toplevel.transition
    24   val print_attributes: Toplevel.transition -> Toplevel.transition
    25   val print_methods: Toplevel.transition -> Toplevel.transition
    26   val print_binds: Toplevel.transition -> Toplevel.transition
    27   val print_lthms: Toplevel.transition -> Toplevel.transition
    28   val print_thms: xstring * Args.src list -> Toplevel.transition -> Toplevel.transition
    29   val print_thm: xstring * Args.src list -> Toplevel.transition -> Toplevel.transition
    30   val print_prop: string -> Toplevel.transition -> Toplevel.transition
    31   val print_term: string -> Toplevel.transition -> Toplevel.transition
    32   val print_type: string -> Toplevel.transition -> Toplevel.transition
    33 end;
    34 
    35 structure IsarCmd: ISAR_CMD =
    36 struct
    37 
    38 
    39 (* variations on exit *)
    40 
    41 val break = Toplevel.imperative (fn () => raise Toplevel.BREAK);
    42 
    43 val exit = Toplevel.keep (fn state =>
    44   (Context.set_context (try Toplevel.theory_of state);
    45     writeln "Leaving the Isar loop.  Invoke 'main_loop();' to restart.";
    46     raise Toplevel.TERMINATE));
    47 
    48 val quit = Toplevel.imperative quit;
    49 
    50 
    51 (* use ML text *)
    52 
    53 fun use name = Toplevel.imperative (fn () => Use.use name);
    54 
    55 (*passes changes of theory context*)
    56 val use_mltext_theory = Toplevel.theory o IsarThy.use_mltext_theory;
    57 
    58 (*ignore result theory context*)
    59 fun use_mltext txt = Toplevel.keep (fn state =>
    60   (IsarThy.use_mltext txt (try Toplevel.theory_of state); ()));
    61 
    62 (*Note: commit is dynamically bound*)
    63 val use_commit = use_mltext "commit();";
    64 
    65 
    66 (* current working directory *)
    67 
    68 fun print_cd _ = writeln (Library.pwd ());
    69 
    70 fun cd dir = Toplevel.imperative (fn () => (Use.cd dir; print_cd ()));
    71 val pwd = Toplevel.imperative print_cd;
    72 
    73 
    74 (* load theory files *)
    75 
    76 fun use_thy name = Toplevel.imperative (fn () => ThyRead.use_thy name);
    77 
    78 fun load name =
    79   Toplevel.imperative (fn () => Toplevel.excursion (OuterSyntax.read_file (name ^ ".thy")));
    80 
    81 
    82 (* print theory contents *)
    83 
    84 val print_theory = Toplevel.keep (PureThy.print_theory o Toplevel.theory_of);
    85 val print_syntax = Toplevel.keep (Display.print_syntax o Toplevel.theory_of);
    86 val print_theorems = Toplevel.keep (PureThy.print_theorems o Toplevel.theory_of);
    87 val print_attributes = Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
    88 val print_methods = Toplevel.keep (Method.print_methods o Toplevel.theory_of);
    89 
    90 
    91 (* print proof context contents *)
    92 
    93 val print_binds = Toplevel.keep (ProofContext.print_binds o Proof.context_of o Toplevel.proof_of);
    94 val print_lthms = Toplevel.keep (ProofContext.print_thms o Proof.context_of o Toplevel.proof_of);
    95 
    96 
    97 (* print theorems *)
    98 
    99 fun global_attribs thy ths srcs =
   100   #2 (Attribute.applys ((Theory.copy thy, ths), map (Attrib.global_attribute thy) srcs));
   101 
   102 fun local_attribs st ths srcs =
   103   #2 (Attribute.applys ((Proof.context_of st, ths),
   104     map (Attrib.local_attribute (Proof.theory_of st)) srcs));
   105 
   106 fun gen_print_thms global_get local_get (name, srcs) = Toplevel.keep (fn state =>
   107   let
   108     val prt_tthm = Attribute.pretty_tthm;
   109     fun prt_tthms [th] = prt_tthm th
   110       | prt_tthms ths = Pretty.block (Pretty.fbreaks (map prt_tthm ths));
   111 
   112     val ths = map (apfst (Thm.transfer (Toplevel.theory_of state)))
   113       (Toplevel.node_cases global_get (local_get o Proof.context_of)
   114         state name);
   115     val ths' = Toplevel.node_cases global_attribs local_attribs state ths srcs;
   116   in Pretty.writeln (prt_tthms ths') end);
   117 
   118 val print_thms = gen_print_thms PureThy.get_tthms ProofContext.get_tthms;
   119 val print_thm =
   120   gen_print_thms (Library.single oo PureThy.get_tthm) (Library.single oo ProofContext.get_tthm);
   121 
   122 
   123 (* print types, terms and props *)
   124 
   125 fun read_typ thy = Sign.read_typ (Theory.sign_of thy, K None);
   126 
   127 fun read_term T thy s =
   128   Thm.term_of (#1 (Thm.read_def_cterm (Theory.sign_of thy, K None, K None) [] true (s, T)));
   129 
   130 
   131 fun print_type s = Toplevel.keep (fn state =>
   132   let
   133     val sign = Toplevel.sign_of state;
   134     val T = Toplevel.node_cases read_typ (ProofContext.read_typ o Proof.context_of) state s;
   135   in Pretty.writeln (Pretty.quote (Sign.pretty_typ sign T)) end);
   136 
   137 fun print_term s = Toplevel.keep (fn state =>
   138   let
   139     val sign = Toplevel.sign_of state;
   140     val t = Toplevel.node_cases (read_term (TVar (("'z", 0), logicS)))
   141       (ProofContext.read_term o Proof.context_of) state s;
   142     val T = Term.type_of t;
   143   in
   144     Pretty.writeln (Pretty.block [Pretty.quote (Sign.pretty_term sign t), Pretty.fbrk,
   145       Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ sign T)])
   146   end);
   147 
   148 fun print_prop s = Toplevel.keep (fn state =>
   149   let
   150     val sign = Toplevel.sign_of state;
   151     val prop =
   152       Toplevel.node_cases (read_term propT) (ProofContext.read_prop o Proof.context_of) state s;
   153   in Pretty.writeln (Pretty.quote (Sign.pretty_term sign prop)) end);
   154 
   155 
   156 end;