| 5831 |      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
 | 
| 5991 |     12 |   val restart: Toplevel.transition -> Toplevel.transition
 | 
| 5831 |     13 |   val quit: Toplevel.transition -> Toplevel.transition
 | 
|  |     14 |   val use: string -> Toplevel.transition -> Toplevel.transition
 | 
|  |     15 |   val use_mltext_theory: string -> Toplevel.transition -> Toplevel.transition
 | 
|  |     16 |   val use_mltext: string -> Toplevel.transition -> Toplevel.transition
 | 
|  |     17 |   val use_commit: Toplevel.transition -> Toplevel.transition
 | 
|  |     18 |   val cd: string -> Toplevel.transition -> Toplevel.transition
 | 
|  |     19 |   val pwd: Toplevel.transition -> Toplevel.transition
 | 
|  |     20 |   val use_thy: string -> Toplevel.transition -> Toplevel.transition
 | 
|  |     21 |   val load: string -> Toplevel.transition -> Toplevel.transition
 | 
|  |     22 |   val print_theory: Toplevel.transition -> Toplevel.transition
 | 
|  |     23 |   val print_syntax: Toplevel.transition -> Toplevel.transition
 | 
| 5880 |     24 |   val print_theorems: Toplevel.transition -> Toplevel.transition
 | 
| 5831 |     25 |   val print_attributes: Toplevel.transition -> Toplevel.transition
 | 
|  |     26 |   val print_methods: Toplevel.transition -> Toplevel.transition
 | 
|  |     27 |   val print_binds: Toplevel.transition -> Toplevel.transition
 | 
|  |     28 |   val print_lthms: Toplevel.transition -> Toplevel.transition
 | 
| 5880 |     29 |   val print_thms: xstring * Args.src list -> Toplevel.transition -> Toplevel.transition
 | 
| 5831 |     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 | 
 | 
| 5913 |     41 | val break = Toplevel.keep (fn state => raise Toplevel.BREAK state);
 | 
| 5831 |     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 | 
 | 
| 5991 |     48 | val restart = Toplevel.imperative (fn () => raise Toplevel.RESTART);
 | 
| 5831 |     49 | val quit = Toplevel.imperative quit;
 | 
|  |     50 | 
 | 
|  |     51 | 
 | 
|  |     52 | (* use ML text *)
 | 
|  |     53 | 
 | 
|  |     54 | fun use name = Toplevel.imperative (fn () => Use.use name);
 | 
|  |     55 | 
 | 
|  |     56 | (*passes changes of theory context*)
 | 
|  |     57 | val use_mltext_theory = Toplevel.theory o IsarThy.use_mltext_theory;
 | 
|  |     58 | 
 | 
|  |     59 | (*ignore result theory context*)
 | 
|  |     60 | fun use_mltext txt = Toplevel.keep (fn state =>
 | 
|  |     61 |   (IsarThy.use_mltext txt (try Toplevel.theory_of state); ()));
 | 
|  |     62 | 
 | 
|  |     63 | (*Note: commit is dynamically bound*)
 | 
|  |     64 | val use_commit = use_mltext "commit();";
 | 
|  |     65 | 
 | 
|  |     66 | 
 | 
|  |     67 | (* current working directory *)
 | 
|  |     68 | 
 | 
|  |     69 | fun print_cd _ = writeln (Library.pwd ());
 | 
|  |     70 | 
 | 
|  |     71 | fun cd dir = Toplevel.imperative (fn () => (Use.cd dir; print_cd ()));
 | 
|  |     72 | val pwd = Toplevel.imperative print_cd;
 | 
|  |     73 | 
 | 
|  |     74 | 
 | 
|  |     75 | (* load theory files *)
 | 
|  |     76 | 
 | 
|  |     77 | fun use_thy name = Toplevel.imperative (fn () => ThyRead.use_thy name);
 | 
|  |     78 | 
 | 
|  |     79 | fun load name =
 | 
|  |     80 |   Toplevel.imperative (fn () => Toplevel.excursion (OuterSyntax.read_file (name ^ ".thy")));
 | 
|  |     81 | 
 | 
|  |     82 | 
 | 
|  |     83 | (* print theory contents *)
 | 
|  |     84 | 
 | 
|  |     85 | val print_theory = Toplevel.keep (PureThy.print_theory o Toplevel.theory_of);
 | 
|  |     86 | val print_syntax = Toplevel.keep (Display.print_syntax o Toplevel.theory_of);
 | 
| 5880 |     87 | val print_theorems = Toplevel.keep (PureThy.print_theorems o Toplevel.theory_of);
 | 
| 5831 |     88 | val print_attributes = Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
 | 
|  |     89 | val print_methods = Toplevel.keep (Method.print_methods o Toplevel.theory_of);
 | 
|  |     90 | 
 | 
|  |     91 | 
 | 
|  |     92 | (* print proof context contents *)
 | 
|  |     93 | 
 | 
|  |     94 | val print_binds = Toplevel.keep (ProofContext.print_binds o Proof.context_of o Toplevel.proof_of);
 | 
|  |     95 | val print_lthms = Toplevel.keep (ProofContext.print_thms o Proof.context_of o Toplevel.proof_of);
 | 
|  |     96 | 
 | 
|  |     97 | 
 | 
|  |     98 | (* print theorems *)
 | 
|  |     99 | 
 | 
| 5880 |    100 | fun global_attribs thy ths srcs =
 | 
|  |    101 |   #2 (Attribute.applys ((Theory.copy thy, ths), map (Attrib.global_attribute thy) srcs));
 | 
|  |    102 | 
 | 
|  |    103 | fun local_attribs st ths srcs =
 | 
|  |    104 |   #2 (Attribute.applys ((Proof.context_of st, ths),
 | 
|  |    105 |     map (Attrib.local_attribute (Proof.theory_of st)) srcs));
 | 
|  |    106 | 
 | 
| 5913 |    107 | fun print_thms (name, srcs) = Toplevel.keep (fn state =>
 | 
| 5831 |    108 |   let
 | 
| 5880 |    109 |     val ths = map (apfst (Thm.transfer (Toplevel.theory_of state)))
 | 
| 5913 |    110 |       (Toplevel.node_cases PureThy.get_tthms (ProofContext.get_tthms o Proof.context_of)
 | 
| 5831 |    111 |         state name);
 | 
| 5880 |    112 |     val ths' = Toplevel.node_cases global_attribs local_attribs state ths srcs;
 | 
| 5913 |    113 |   in Attribute.print_tthms ths' end);
 | 
| 5895 |    114 | 
 | 
| 5831 |    115 | 
 | 
|  |    116 | (* print types, terms and props *)
 | 
|  |    117 | 
 | 
|  |    118 | fun read_typ thy = Sign.read_typ (Theory.sign_of thy, K None);
 | 
|  |    119 | 
 | 
|  |    120 | fun read_term T thy s =
 | 
|  |    121 |   Thm.term_of (#1 (Thm.read_def_cterm (Theory.sign_of thy, K None, K None) [] true (s, T)));
 | 
|  |    122 | 
 | 
|  |    123 | 
 | 
|  |    124 | fun print_type s = Toplevel.keep (fn state =>
 | 
|  |    125 |   let
 | 
|  |    126 |     val sign = Toplevel.sign_of state;
 | 
|  |    127 |     val T = Toplevel.node_cases read_typ (ProofContext.read_typ o Proof.context_of) state s;
 | 
|  |    128 |   in Pretty.writeln (Pretty.quote (Sign.pretty_typ sign T)) end);
 | 
|  |    129 | 
 | 
|  |    130 | fun print_term s = Toplevel.keep (fn state =>
 | 
|  |    131 |   let
 | 
|  |    132 |     val sign = Toplevel.sign_of state;
 | 
|  |    133 |     val t = Toplevel.node_cases (read_term (TVar (("'z", 0), logicS)))
 | 
|  |    134 |       (ProofContext.read_term o Proof.context_of) state s;
 | 
|  |    135 |     val T = Term.type_of t;
 | 
|  |    136 |   in
 | 
|  |    137 |     Pretty.writeln (Pretty.block [Pretty.quote (Sign.pretty_term sign t), Pretty.fbrk,
 | 
|  |    138 |       Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ sign T)])
 | 
|  |    139 |   end);
 | 
|  |    140 | 
 | 
|  |    141 | fun print_prop s = Toplevel.keep (fn state =>
 | 
|  |    142 |   let
 | 
|  |    143 |     val sign = Toplevel.sign_of state;
 | 
|  |    144 |     val prop =
 | 
|  |    145 |       Toplevel.node_cases (read_term propT) (ProofContext.read_prop o Proof.context_of) state s;
 | 
|  |    146 |   in Pretty.writeln (Pretty.quote (Sign.pretty_term sign prop)) end);
 | 
|  |    147 | 
 | 
|  |    148 | 
 | 
|  |    149 | end;
 |