src/Pure/Isar/isar_cmd.ML
author wenzelm
Thu Jul 06 18:11:48 2000 +0200 (2000-07-06 ago)
changeset 9273 798673f65f02
parent 9219 84af672218b9
child 9454 ea80449107cc
permissions -rw-r--r--
allow comment in more commands;
     1 (*  Title:      Pure/Isar/isar_cmd.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Non-logical toplevel commands.
     7 *)
     8 
     9 signature ISAR_CMD =
    10 sig
    11   val welcome: Toplevel.transition -> Toplevel.transition
    12   val init_toplevel: Toplevel.transition -> Toplevel.transition
    13   val exit: Toplevel.transition -> Toplevel.transition
    14   val quit: Toplevel.transition -> Toplevel.transition
    15   val touch_child_thys: string -> Toplevel.transition -> Toplevel.transition
    16   val touch_all_thys: Toplevel.transition -> Toplevel.transition
    17   val touch_thy: string -> Toplevel.transition -> Toplevel.transition
    18   val remove_thy: string -> Toplevel.transition -> Toplevel.transition
    19   val kill_thy: string -> Toplevel.transition -> Toplevel.transition
    20   val pr: string list * int option -> Toplevel.transition -> Toplevel.transition
    21   val disable_pr: Toplevel.transition -> Toplevel.transition
    22   val enable_pr: Toplevel.transition -> Toplevel.transition
    23   val cannot_undo: string -> Toplevel.transition -> Toplevel.transition
    24   val clear_undos_theory: int -> Toplevel.transition -> Toplevel.transition
    25   val redo: Toplevel.transition -> Toplevel.transition
    26   val undos_proof: int -> Toplevel.transition -> Toplevel.transition
    27   val kill_proof_notify: (unit -> unit) -> Toplevel.transition -> Toplevel.transition
    28   val kill_proof: Toplevel.transition -> Toplevel.transition
    29   val undo_theory: Toplevel.transition -> Toplevel.transition
    30   val undo: Toplevel.transition -> Toplevel.transition
    31   val kill: Toplevel.transition -> Toplevel.transition
    32   val back: bool * Comment.text -> Toplevel.transition -> Toplevel.transition
    33   val use: string * Comment.text -> Toplevel.transition -> Toplevel.transition
    34   val use_mltext_theory: string * Comment.text -> Toplevel.transition -> Toplevel.transition
    35   val use_mltext: bool -> string * Comment.text -> Toplevel.transition -> Toplevel.transition
    36   val use_setup: string * Comment.text -> theory -> theory
    37   val use_commit: Toplevel.transition -> Toplevel.transition
    38   val cd: string -> Toplevel.transition -> Toplevel.transition
    39   val pwd: Toplevel.transition -> Toplevel.transition
    40   val use_thy: string -> Toplevel.transition -> Toplevel.transition
    41   val use_thy_only: string -> Toplevel.transition -> Toplevel.transition
    42   val update_thy: string -> Toplevel.transition -> Toplevel.transition
    43   val update_thy_only: string -> Toplevel.transition -> Toplevel.transition
    44   val pretty_setmargin: int -> Toplevel.transition -> Toplevel.transition
    45   val print_context: Toplevel.transition -> Toplevel.transition
    46   val print_theory: Toplevel.transition -> Toplevel.transition
    47   val print_syntax: Toplevel.transition -> Toplevel.transition
    48   val print_theorems: Toplevel.transition -> Toplevel.transition
    49   val print_attributes: Toplevel.transition -> Toplevel.transition
    50   val print_trans_rules: Toplevel.transition -> Toplevel.transition
    51   val print_methods: Toplevel.transition -> Toplevel.transition
    52   val print_antiquotations: Toplevel.transition -> Toplevel.transition
    53   val print_thms_containing: xstring list -> Toplevel.transition -> Toplevel.transition
    54   val print_binds: Toplevel.transition -> Toplevel.transition
    55   val print_lthms: Toplevel.transition -> Toplevel.transition
    56   val print_cases: Toplevel.transition -> Toplevel.transition
    57   val print_thms: string list * (string * Args.src list) list
    58     -> Toplevel.transition -> Toplevel.transition
    59   val print_prop: string list * string -> Toplevel.transition -> Toplevel.transition
    60   val print_term: string list * string -> Toplevel.transition -> Toplevel.transition
    61   val print_type: string list * string -> Toplevel.transition -> Toplevel.transition
    62 end;
    63 
    64 structure IsarCmd: ISAR_CMD =
    65 struct
    66 
    67 
    68 (* variations on init / exit *)
    69 
    70 val init_toplevel = Toplevel.imperative (fn () => raise Toplevel.RESTART);
    71 val welcome = Toplevel.imperative (writeln o Session.welcome);
    72 
    73 val exit = Toplevel.keep (fn state =>
    74   (Context.set_context (try Toplevel.theory_of state);
    75     writeln "Leaving the Isar loop.  Invoke 'loop();' to restart.";
    76     raise Toplevel.TERMINATE));
    77 
    78 val quit = Toplevel.imperative quit;
    79 
    80 
    81 (* touch theories *)
    82 
    83 fun touch_child_thys name = Toplevel.imperative (fn () => ThyInfo.touch_child_thys name);
    84 val touch_all_thys = Toplevel.imperative ThyInfo.touch_all_thys;
    85 fun touch_thy name = Toplevel.imperative (fn () => ThyInfo.touch_thy name);
    86 fun remove_thy name = Toplevel.imperative (fn () => ThyInfo.remove_thy name);
    87 fun kill_thy name = Toplevel.imperative (fn () => IsarThy.kill_theory name);
    88 
    89 
    90 (* print state *)
    91 
    92 fun with_modes modes e =
    93   Library.setmp print_mode (modes @ ! print_mode) e ();
    94 
    95 fun pr (ms, limit) = Toplevel.keep (fn state =>
    96   ((case limit of Some n => goals_limit := n | None => ()); Toplevel.quiet := false;
    97     with_modes ms (fn () => Toplevel.print_state true state)));
    98 
    99 val disable_pr = Toplevel.imperative (fn () => Toplevel.quiet := true);
   100 val enable_pr = Toplevel.imperative (fn () => Toplevel.quiet := false);
   101 
   102 
   103 (* history commands *)
   104 
   105 fun cannot_undo txt = Toplevel.imperative (fn () => error ("Cannot undo " ^ quote txt));
   106 val clear_undos_theory = Toplevel.history o History.clear;
   107 val redo = Toplevel.history History.redo o Toplevel.proof ProofHistory.redo;
   108 
   109 fun undos_proof n = Toplevel.proof (fn prf =>
   110   if ProofHistory.is_initial prf then raise Toplevel.UNDEF else funpow n ProofHistory.undo prf);
   111 
   112 fun kill_proof_notify (f: unit -> unit) = Toplevel.history (fn hist =>
   113   (case History.current hist of
   114     Toplevel.Theory _ => raise Toplevel.UNDEF
   115   | Toplevel.Proof _ => (f (); History.undo hist)));
   116 
   117 val kill_proof = kill_proof_notify (K ());
   118 
   119 val undo_theory = Toplevel.history (fn hist =>
   120   if History.is_initial hist then raise Toplevel.UNDEF else History.undo hist);
   121 
   122 val undo = Toplevel.kill o undo_theory o undos_proof 1;
   123 val kill = Toplevel.kill o kill_proof;
   124 
   125 val back = Toplevel.proof o ProofHistory.back o Comment.ignore;
   126 
   127 
   128 (* use ML text *)
   129 
   130 fun use (name, comment_ignore) = Toplevel.keep (fn state =>
   131   Context.setmp (try Toplevel.theory_of state) ThyInfo.use name);
   132 
   133 (*passes changes of theory context*)
   134 val use_mltext_theory = Toplevel.theory' o Context.use_mltext_theory o Comment.ignore;
   135 
   136 (*ignore result theory context*)
   137 fun use_mltext v (txt, comment_ignore) = Toplevel.keep' (fn verb => fn state =>
   138   (Context.use_mltext txt (v andalso verb) (try Toplevel.theory_of state)));
   139 
   140 val use_setup = Context.use_setup o Comment.ignore;
   141 
   142 (*Note: commit is dynamically bound*)
   143 val use_commit = use_mltext false ("commit();", Comment.none);
   144 
   145 
   146 (* current working directory *)
   147 
   148 fun cd dir = Toplevel.imperative (fn () => (File.cd (Path.unpack dir)));
   149 val pwd = Toplevel.imperative (fn () => writeln (Path.pack (File.pwd ())));
   150 
   151 
   152 (* load theory files *)
   153 
   154 fun use_thy name = Toplevel.imperative (fn () => Context.save ThyInfo.use_thy name);
   155 fun use_thy_only name = Toplevel.imperative (fn () => Context.save ThyInfo.use_thy_only name);
   156 fun update_thy name = Toplevel.imperative (fn () => Context.save ThyInfo.update_thy name);
   157 fun update_thy_only name =
   158   Toplevel.imperative (fn () => Context.save ThyInfo.update_thy_only name);
   159 
   160 
   161 (* pretty_setmargin *)
   162 
   163 fun pretty_setmargin n = Toplevel.imperative (fn () => Pretty.setmargin n);
   164 
   165 
   166 (* print theory *)
   167 
   168 val print_context = Toplevel.keep Toplevel.print_state_context;
   169 val print_theory = Toplevel.keep (PureThy.print_theory o Toplevel.theory_of);
   170 val print_syntax = Toplevel.keep (Display.print_syntax o Toplevel.theory_of);
   171 val print_theorems = Toplevel.keep (PureThy.print_theorems o Toplevel.theory_of);
   172 val print_attributes = Toplevel.keep (Attrib.print_attributes o Toplevel.theory_of);
   173 val print_trans_rules = Toplevel.keep (Toplevel.node_case Calculation.print_global_rules
   174   (Calculation.print_local_rules o Proof.context_of));
   175 val print_methods = Toplevel.keep (Method.print_methods o Toplevel.theory_of);
   176 val print_antiquotations = Toplevel.imperative IsarOutput.print_antiquotations;
   177 
   178 fun print_thms_containing cs = Toplevel.keep (fn state =>
   179   ThmDatabase.print_thms_containing (Toplevel.theory_of state) cs);
   180 
   181 
   182 (* print proof context contents *)
   183 
   184 val print_binds = Toplevel.keep (ProofContext.print_binds o Proof.context_of o Toplevel.proof_of);
   185 val print_lthms = Toplevel.keep (ProofContext.print_thms o Proof.context_of o Toplevel.proof_of);
   186 val print_cases = Toplevel.keep (ProofContext.print_cases o Proof.context_of o Toplevel.proof_of);
   187 
   188 
   189 (* print theorems / types / terms / props *)
   190 
   191 fun string_of_thms state ms args = with_modes ms (fn () =>
   192   Pretty.string_of (Proof.pretty_thms (IsarThy.get_thmss args state)));
   193 
   194 fun string_of_prop state ms s =
   195   let
   196     val sign = Proof.sign_of state;
   197     val prop = ProofContext.read_prop (Proof.context_of state) s;
   198   in with_modes ms (fn () => Pretty.string_of (Pretty.quote (Sign.pretty_term sign prop))) end;
   199 
   200 fun string_of_term state ms s =
   201   let
   202     val sign = Proof.sign_of state;
   203     val t = ProofContext.read_term (Proof.context_of state) s;
   204     val T = Term.type_of t;
   205   in
   206     with_modes ms (fn () => Pretty.string_of
   207       (Pretty.block [Pretty.quote (Sign.pretty_term sign t), Pretty.fbrk,
   208         Pretty.str "::", Pretty.brk 1, Pretty.quote (Sign.pretty_typ sign T)]))
   209   end;
   210 
   211 fun string_of_type state ms s =
   212   let
   213     val sign = Proof.sign_of state;
   214     val T = ProofContext.read_typ (Proof.context_of state) s;
   215   in with_modes ms (fn () => Pretty.string_of (Pretty.quote (Sign.pretty_typ sign T))) end;
   216 
   217 fun print_item string_of (x, y) = Toplevel.keep (fn state =>
   218   writeln (string_of (Toplevel.node_case Proof.init_state Proof.enter_forward state) x y));
   219 
   220 val print_thms = print_item string_of_thms;
   221 val print_prop = print_item string_of_prop;
   222 val print_term = print_item string_of_term;
   223 val print_type = print_item string_of_type;
   224 
   225 
   226 end;