src/Pure/Isar/isar_thy.ML
author ballarin
Thu Mar 24 17:03:37 2005 +0100 (2005-03-24 ago)
changeset 15624 484178635bd8
parent 15598 4ab52355bb53
child 15696 1da4ce092c0b
permissions -rw-r--r--
Further work on interpretation commands. New command `interpret' for
interpretation in proof contexts.
     1 (*  Title:      Pure/Isar/isar_thy.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Pure/Isar derived theory operations.
     6 *)
     7 
     8 signature ISAR_THY =
     9 sig
    10   val hide_space: string * xstring list -> theory -> theory
    11   val hide_space_i: string * string list -> theory -> theory
    12   val add_axioms: ((bstring * string) * Args.src list) list -> theory -> theory
    13   val add_axioms_i: ((bstring * term) * theory attribute list) list -> theory -> theory
    14   val add_defs: bool * ((bstring * string) * Args.src list) list -> theory -> theory
    15   val add_defs_i: bool * ((bstring * term) * theory attribute list) list -> theory -> theory
    16   val theorems: string -> ((bstring * Args.src list) * (thmref * Args.src list) list) list
    17     -> theory -> theory * (string * thm list) list
    18   val theorems_i: string ->
    19     ((bstring * theory attribute list) * (thm list * theory attribute list) list) list
    20     -> theory -> theory * (string * thm list) list
    21   val locale_theorems: string -> xstring ->
    22     ((bstring * Args.src list) * (thmref * Args.src list) list) list
    23     -> theory -> theory * (bstring * thm list) list
    24   val locale_theorems_i: string -> string ->
    25     ((bstring * Locale.multi_attribute list)
    26       * (thm list * Locale.multi_attribute list) list) list
    27     -> theory -> theory * (string * thm list) list
    28   val smart_theorems: string -> xstring option ->
    29     ((bstring * Args.src list) * (thmref * Args.src list) list) list
    30     -> theory -> theory * (string * thm list) list
    31   val declare_theorems: xstring option -> (thmref * Args.src list) list -> theory -> theory
    32   val apply_theorems: (thmref * Args.src list) list -> theory -> theory * thm list
    33   val apply_theorems_i: (thm list * theory attribute list) list -> theory -> theory * thm list
    34   val have_facts: ((string * Args.src list) * (thmref * Args.src list) list) list
    35     -> ProofHistory.T -> ProofHistory.T
    36   val have_facts_i: ((string * Proof.context attribute list) *
    37     (thm * Proof.context attribute list) list) list -> ProofHistory.T -> ProofHistory.T
    38   val from_facts: (thmref * Args.src list) list list -> ProofHistory.T -> ProofHistory.T
    39   val from_facts_i: (thm * Proof.context attribute list) list list
    40     -> ProofHistory.T -> ProofHistory.T
    41   val with_facts: (thmref * Args.src list) list list -> ProofHistory.T -> ProofHistory.T
    42   val with_facts_i: (thm * Proof.context attribute list) list list
    43     -> ProofHistory.T -> ProofHistory.T
    44   val using_facts: (thmref * Args.src list) list list -> ProofHistory.T -> ProofHistory.T
    45   val using_facts_i: (thm * Proof.context attribute list) list list
    46     -> ProofHistory.T -> ProofHistory.T
    47   val chain: ProofHistory.T -> ProofHistory.T
    48   val instantiate_locale: (string * Args.src list) * string -> ProofHistory.T
    49     -> ProofHistory.T
    50   val fix: (string list * string option) list -> ProofHistory.T -> ProofHistory.T
    51   val fix_i: (string list * typ option) list -> ProofHistory.T -> ProofHistory.T
    52   val let_bind: (string list * string) list -> ProofHistory.T -> ProofHistory.T
    53   val let_bind_i: (term list * term) list -> ProofHistory.T -> ProofHistory.T
    54   val invoke_case: string * string option list * Args.src list -> ProofHistory.T -> ProofHistory.T
    55   val invoke_case_i: string * string option list * Proof.context attribute list
    56     -> ProofHistory.T -> ProofHistory.T
    57   val theorem: string -> (bstring * Args.src list) * (string * (string list * string list))
    58     -> bool -> theory -> ProofHistory.T
    59   val theorem_i: string -> (bstring * theory attribute list) *
    60     (term * (term list * term list)) -> bool -> theory -> ProofHistory.T
    61   val multi_theorem: string -> (theory -> theory) -> bstring * Args.src list
    62     -> Args.src Locale.element Locale.elem_expr list
    63     -> ((bstring * Args.src list) * (string * (string list * string list)) list) list
    64     -> bool -> theory -> ProofHistory.T
    65   val multi_theorem_i: string -> (theory -> theory) -> bstring * theory attribute list
    66     -> Locale.multi_attribute Locale.element_i Locale.elem_expr list
    67     -> ((bstring * theory attribute list) * (term * (term list * term list)) list) list
    68     -> bool -> theory -> ProofHistory.T
    69   val locale_multi_theorem: string -> xstring
    70     -> bstring * Args.src list -> Args.src Locale.element Locale.elem_expr list
    71     -> ((bstring * Args.src list) * (string * (string list * string list)) list) list
    72     -> bool -> theory -> ProofHistory.T
    73   val locale_multi_theorem_i: string -> string -> bstring * Locale.multi_attribute list
    74     -> Locale.multi_attribute Locale.element_i Locale.elem_expr list
    75     -> ((bstring * Locale.multi_attribute list) * (term * (term list * term list)) list) list
    76     -> bool -> theory -> ProofHistory.T
    77   val smart_multi_theorem: string -> xstring option
    78     -> bstring * Args.src list -> Args.src Locale.element Locale.elem_expr list
    79     -> ((bstring * Args.src list) * (string * (string list * string list)) list) list
    80     -> bool -> theory -> ProofHistory.T
    81   val assume: ((string * Args.src list) * (string * (string list * string list)) list) list
    82     -> ProofHistory.T -> ProofHistory.T
    83   val assume_i:
    84     ((string * Proof.context attribute list) * (term * (term list * term list)) list) list
    85     -> ProofHistory.T -> ProofHistory.T
    86   val presume: ((string * Args.src list) * (string * (string list * string list)) list) list
    87     -> ProofHistory.T -> ProofHistory.T
    88   val presume_i:
    89     ((string * Proof.context attribute list) * (term * (term list * term list)) list) list
    90     -> ProofHistory.T -> ProofHistory.T
    91   val have: ((string * Args.src list) * (string * (string list * string list)) list) list
    92     -> ProofHistory.T -> ProofHistory.T
    93   val have_i: ((string * Proof.context attribute list) *
    94     (term * (term list * term list)) list) list -> ProofHistory.T -> ProofHistory.T
    95   val hence: ((string * Args.src list) * (string * (string list * string list)) list) list
    96     -> ProofHistory.T -> ProofHistory.T
    97   val hence_i: ((string * Proof.context attribute list) *
    98     (term * (term list * term list)) list) list -> ProofHistory.T -> ProofHistory.T
    99   val show: ((string * Args.src list) * (string * (string list * string list)) list) list
   100     -> bool -> ProofHistory.T -> ProofHistory.T
   101   val show_i: ((string * Proof.context attribute list) *
   102     (term * (term list * term list)) list) list -> bool -> ProofHistory.T -> ProofHistory.T
   103   val thus: ((string * Args.src list) * (string * (string list * string list)) list) list
   104     -> bool -> ProofHistory.T -> ProofHistory.T
   105   val thus_i: ((string * Proof.context attribute list)
   106     * (term * (term list * term list)) list) list -> bool -> ProofHistory.T -> ProofHistory.T
   107   val local_def: (string * Args.src list) * (string * (string * string list))
   108     -> ProofHistory.T -> ProofHistory.T
   109   val local_def_i: (string * Args.src list) * (string * (term * term list))
   110     -> ProofHistory.T -> ProofHistory.T
   111   val obtain: (string list * string option) list
   112     * ((string * Args.src list) * (string * (string list * string list)) list) list
   113     -> Toplevel.transition -> Toplevel.transition
   114   val obtain_i: (string list * typ option) list
   115     * ((string * Proof.context attribute list) * (term * (term list * term list)) list) list
   116     -> Toplevel.transition -> Toplevel.transition
   117   val begin_block: ProofHistory.T -> ProofHistory.T
   118   val next_block: ProofHistory.T -> ProofHistory.T
   119   val end_block: ProofHistory.T -> ProofHistory.T
   120   val defer: int option -> ProofHistory.T -> ProofHistory.T
   121   val prefer: int -> ProofHistory.T -> ProofHistory.T
   122   val apply: Method.text -> ProofHistory.T -> ProofHistory.T
   123   val apply_end: Method.text -> ProofHistory.T -> ProofHistory.T
   124   val proof: Method.text option -> ProofHistory.T -> ProofHistory.T
   125   val qed: Method.text option -> Toplevel.transition -> Toplevel.transition
   126   val terminal_proof: Method.text * Method.text option
   127     -> Toplevel.transition -> Toplevel.transition
   128   val default_proof: Toplevel.transition -> Toplevel.transition
   129   val immediate_proof: Toplevel.transition -> Toplevel.transition
   130   val done_proof: Toplevel.transition -> Toplevel.transition
   131   val skip_proof: Toplevel.transition -> Toplevel.transition
   132   val forget_proof: Toplevel.transition -> Toplevel.transition
   133   val get_thmss: (thmref * Args.src list) list -> Proof.state -> thm list
   134   val also: (thmref * Args.src list) list option -> Toplevel.transition -> Toplevel.transition
   135   val also_i: thm list option -> Toplevel.transition -> Toplevel.transition
   136   val finally: (thmref * Args.src list) list option -> Toplevel.transition -> Toplevel.transition
   137   val finally_i: thm list option -> Toplevel.transition -> Toplevel.transition
   138   val moreover: Toplevel.transition -> Toplevel.transition
   139   val ultimately: Toplevel.transition -> Toplevel.transition
   140   val parse_ast_translation: bool * string -> theory -> theory
   141   val parse_translation: bool * string -> theory -> theory
   142   val print_translation: bool * string -> theory -> theory
   143   val typed_print_translation: bool * string -> theory -> theory
   144   val print_ast_translation: bool * string -> theory -> theory
   145   val token_translation: string -> theory -> theory
   146   val method_setup: bstring * string * string -> theory -> theory
   147   val add_oracle: bstring * string -> theory -> theory
   148   val add_locale: bool * bstring * Locale.expr * Args.src Locale.element list -> theory -> theory
   149   val begin_theory: string -> string list -> (string * bool) list -> theory
   150   val end_theory: theory -> theory
   151   val kill_theory: string -> unit
   152   val theory: string * string list * (string * bool) list
   153     -> Toplevel.transition -> Toplevel.transition
   154   val init_context: ('a -> unit) -> 'a -> Toplevel.transition -> Toplevel.transition
   155   val context: string -> Toplevel.transition -> Toplevel.transition
   156 
   157   val register_globally:
   158     ((string * Args.src list) * Locale.expr) * string option list ->
   159     bool -> theory -> ProofHistory.T
   160   val register_locally:
   161     ((string * Args.src list) * Locale.expr) * string option list ->
   162     ProofHistory.T -> ProofHistory.T
   163 
   164 end;
   165 
   166 structure IsarThy: ISAR_THY =
   167 struct
   168 
   169 (** derived theory and proof operations **)
   170 
   171 (* name spaces *)
   172 
   173 local
   174 
   175 val kinds =
   176  [(Sign.classK, can o Sign.certify_class),
   177   (Sign.typeK, can o Sign.certify_tyname),
   178   (Sign.constK, can o Sign.certify_const)];
   179 
   180 fun gen_hide intern (kind, xnames) thy =
   181   (case assoc (kinds, kind) of
   182     SOME check =>
   183       let
   184         val sg = Theory.sign_of thy;
   185         val names = map (intern sg kind) xnames;
   186         val bads = filter_out (check sg) names;
   187       in
   188         if null bads then Theory.hide_space_i true (kind, names) thy
   189         else error ("Attempt to hide undeclared item(s): " ^ commas_quote bads)
   190       end
   191   | NONE => error ("Bad name space specification: " ^ quote kind));
   192 
   193 in
   194 
   195 fun hide_space x = gen_hide Sign.intern x;
   196 fun hide_space_i x = gen_hide (K (K I)) x;
   197 
   198 end;
   199 
   200 
   201 (* axioms and defs *)
   202 
   203 fun add_axms f args thy =
   204   f (map (fn (x, srcs) => (x, map (Attrib.global_attribute thy) srcs)) args) thy;
   205 
   206 val add_axioms = add_axms (#1 oo PureThy.add_axioms);
   207 val add_axioms_i = #1 oo PureThy.add_axioms_i;
   208 fun add_defs (x, args) = add_axms (#1 oo PureThy.add_defs x) args;
   209 fun add_defs_i (x, args) = (#1 oo PureThy.add_defs_i x) args;
   210 
   211 
   212 (* attributes *)
   213 
   214 local
   215 
   216 fun prep_attribs f = map (fn ((name, more_srcs), th_srcs) =>
   217   ((name, map f more_srcs), map (apsnd (map f)) th_srcs));
   218 
   219 in
   220 
   221 fun global_attribs thy = prep_attribs (Attrib.global_attribute thy);
   222 fun local_attribs thy = prep_attribs (Attrib.local_attribute thy);
   223 fun multi_attribs thy = prep_attribs (Attrib.multi_attribute thy);
   224 
   225 end;
   226 
   227 
   228 (* theorems *)
   229 
   230 local
   231 
   232 fun present_thmss kind (thy, named_thmss) =
   233  (conditional (kind <> "" andalso kind <> Drule.internalK) (fn () =>
   234     Context.setmp (SOME thy) (Present.results (kind ^ "s")) named_thmss);
   235   (thy, named_thmss));
   236 
   237 in
   238 
   239 fun theorems_i k = present_thmss k oo PureThy.note_thmss_i (Drule.kind k);
   240 fun locale_theorems_i k loc = present_thmss k oo Locale.note_thmss_i k loc;
   241 
   242 fun theorems k args thy = thy
   243   |> PureThy.note_thmss (Drule.kind k) (global_attribs thy args)
   244   |> present_thmss k;
   245 
   246 fun locale_theorems k loc args thy = thy
   247   |> Locale.note_thmss k loc (multi_attribs thy args)
   248   |> present_thmss k;
   249 
   250 fun smart_theorems k opt_loc args thy = thy
   251   |> (case opt_loc of
   252     NONE => PureThy.note_thmss (Drule.kind k) (global_attribs thy args)
   253   | SOME loc => Locale.note_thmss k loc (multi_attribs thy args))
   254   |> present_thmss k;
   255 
   256 fun declare_theorems opt_loc args = #1 o smart_theorems "" opt_loc [(("", []), args)];
   257 
   258 fun apply_theorems args = apsnd (List.concat o map snd) o theorems "" [(("", []), args)];
   259 fun apply_theorems_i args = apsnd (List.concat o map snd) o theorems_i "" [(("", []), args)];
   260 
   261 end;
   262 
   263 
   264 (* facts and forward chaining *)
   265 
   266 fun local_thmss f args state = f (local_attribs (Proof.theory_of state) args) state;
   267 fun local_thmss_i f args = f (map (fn ((name, more_atts), th_atts) =>
   268   ((name, more_atts), map (apfst single) th_atts)) args);
   269 
   270 fun chain_thmss f args = ProofHistory.apply (Proof.chain o f (map (pair ("", [])) args));
   271 
   272 val have_facts = ProofHistory.apply o local_thmss Proof.note_thmss;
   273 val have_facts_i = ProofHistory.apply o local_thmss_i Proof.note_thmss_i;
   274 val from_facts = chain_thmss (local_thmss Proof.note_thmss);
   275 val from_facts_i = chain_thmss (local_thmss_i Proof.note_thmss_i);
   276 val with_facts = chain_thmss (local_thmss Proof.with_thmss);
   277 val with_facts_i = chain_thmss (local_thmss_i Proof.with_thmss_i);
   278 
   279 fun using_facts args = ProofHistory.apply (fn state =>
   280   Proof.using_thmss (map (map (apsnd (map
   281     (Attrib.local_attribute (Proof.theory_of state))))) args) state);
   282 
   283 val using_facts_i = ProofHistory.apply o Proof.using_thmss_i o map (map (apfst single));
   284 
   285 val chain = ProofHistory.apply Proof.chain;
   286 
   287 
   288 (* locale instantiation *)
   289 
   290 fun instantiate_locale ((name, atts), loc) =
   291   ProofHistory.apply (fn state =>
   292     Proof.instantiate_locale (loc,
   293         (name, map (Attrib.local_attribute (Proof.theory_of state)) atts)) state);
   294 
   295 
   296 (* context *)
   297 
   298 val fix = ProofHistory.apply o Proof.fix;
   299 val fix_i = ProofHistory.apply o Proof.fix_i;
   300 val let_bind = ProofHistory.apply o Proof.let_bind;
   301 val let_bind_i = ProofHistory.apply o Proof.let_bind_i;
   302 
   303 fun invoke_case (name, xs, src) = ProofHistory.apply (fn state =>
   304   Proof.invoke_case (name, xs,
   305     map (Attrib.local_attribute (Proof.theory_of state)) src) state);
   306 
   307 val invoke_case_i = ProofHistory.apply o Proof.invoke_case;
   308 
   309 
   310 (* local results *)
   311 
   312 local
   313 
   314 fun prt_facts ctxt =
   315   List.concat o (separate [Pretty.fbrk, Pretty.str "and "]) o
   316     map (single o ProofContext.pretty_fact ctxt);
   317 
   318 fun pretty_results ctxt ((kind, ""), facts) =
   319       Pretty.block (Pretty.str kind :: Pretty.brk 1 :: prt_facts ctxt facts)
   320   | pretty_results ctxt ((kind, name), facts) = Pretty.blk (1,
   321       [Pretty.str (kind ^ " " ^ name ^ ":"), Pretty.fbrk,
   322         Pretty.blk (1, Pretty.str "(" :: prt_facts ctxt facts @ [Pretty.str ")"])]);
   323 
   324 fun pretty_rule s ctxt thm =
   325   Pretty.block [Pretty.str (s ^ " to solve goal by exported rule:"),
   326     Pretty.fbrk, ProofContext.pretty_thm ctxt thm];
   327 
   328 in
   329 
   330 val print_result = Pretty.writeln oo pretty_results;
   331 fun print_result' ctxt (k, res) = print_result ctxt ((k, ""), res);
   332 
   333 fun cond_print_result_rule int = if int
   334   then (print_result',
   335         priority oo (Pretty.string_of oo pretty_rule "Successful attempt"))
   336   else (K (K ()), K (K ()));
   337 
   338 fun proof'' f = Toplevel.proof' (f o cond_print_result_rule);
   339 
   340 fun check_goal false state = state
   341   | check_goal true state =
   342       let
   343         val rule = ref (NONE: thm option);
   344         fun fail exn =
   345           (["Problem! Local statement will fail to solve any pending goal"] @
   346           (case exn of NONE => [] | SOME e => [Toplevel.exn_message e]) @
   347           (case ! rule of NONE => [] | SOME thm =>
   348             [Pretty.string_of (pretty_rule "Failed attempt" (Proof.context_of state) thm)]))
   349           |> cat_lines |> error;
   350         val check =
   351           (fn () => Seq.pull (SkipProof.local_skip_proof (K (K ()),
   352             fn _ => fn thm => rule := SOME thm) true state))
   353           |> setmp proofs 0
   354           |> transform_error;
   355         val result = (check (), NONE) handle Interrupt => raise Interrupt | e => (NONE, SOME e);
   356       in (case result of (SOME _, NONE) => () | (_, exn) => fail exn); state end;
   357 
   358 end;
   359 
   360 
   361 (* statements *)
   362 
   363 local
   364 
   365 fun global_attributes thy ((name, src), s) = ((name, map (Attrib.global_attribute thy) src), s);
   366 fun local_attributes thy ((name, src), s) = ((name, map (Attrib.local_attribute thy) src), s);
   367 fun local_attributes' state = local_attributes (Proof.theory_of state);
   368 
   369 fun global_statement f args int thy =
   370   ProofHistory.init (Toplevel.undo_limit int) (f (map (global_attributes thy) args) thy);
   371 fun global_statement_i f args int thy = ProofHistory.init (Toplevel.undo_limit int) (f args thy);
   372 
   373 fun local_statement' f g args int = ProofHistory.apply (fn state =>
   374   f (map (local_attributes' state) args) int (g state));
   375 fun local_statement_i' f g args int = ProofHistory.apply (f args int o g);
   376 fun local_statement f g args = local_statement' (K o f) g args ();
   377 fun local_statement_i f g args = local_statement_i' (K o f) g args ();
   378 
   379 in
   380 
   381 fun multi_theorem k thy_mod a elems concl int thy =
   382   global_statement (Proof.multi_theorem k thy_mod NONE (apsnd (map (Attrib.global_attribute thy)) a)
   383     (map (Locale.map_attrib_elem_or_expr (Attrib.multi_attribute thy)) elems)) concl int thy;
   384 
   385 fun multi_theorem_i k thy_mod a = global_statement_i o Proof.multi_theorem_i k thy_mod NONE a;
   386 
   387 fun locale_multi_theorem k locale (name, atts) elems concl int thy =
   388   global_statement (Proof.multi_theorem k I
   389       (SOME (locale, (map (Attrib.multi_attribute thy) atts,
   390           map (map (Attrib.multi_attribute thy) o snd o fst) concl)))
   391       (name, []) (map (Locale.map_attrib_elem_or_expr (Attrib.multi_attribute thy)) elems))
   392       (map (apfst (apsnd (K []))) concl) int thy;
   393 
   394 fun locale_multi_theorem_i k locale (name, atts) elems concl =
   395   global_statement_i (Proof.multi_theorem_i k I (SOME (locale, (atts, map (snd o fst) concl)))
   396       (name, []) elems) (map (apfst (apsnd (K []))) concl);
   397 
   398 fun theorem k (a, t) = multi_theorem k I a [] [(("", []), [t])];
   399 fun theorem_i k (a, t) = multi_theorem_i k I a [] [(("", []), [t])];
   400 
   401 fun smart_multi_theorem k NONE a elems = multi_theorem k I a elems
   402   | smart_multi_theorem k (SOME locale) a elems = locale_multi_theorem k locale a elems;
   403 
   404 val assume      = local_statement Proof.assume I;
   405 val assume_i    = local_statement_i Proof.assume_i I;
   406 val presume     = local_statement Proof.presume I;
   407 val presume_i   = local_statement_i Proof.presume_i I;
   408 val have        = local_statement (Proof.have Seq.single true) I;
   409 val have_i      = local_statement_i (Proof.have_i Seq.single true) I;
   410 val hence       = local_statement (Proof.have Seq.single true) Proof.chain;
   411 val hence_i     = local_statement_i (Proof.have_i Seq.single true) Proof.chain;
   412 val show        = local_statement' (Proof.show check_goal Seq.single true) I;
   413 val show_i      = local_statement_i' (Proof.show_i check_goal Seq.single true) I;
   414 val thus        = local_statement' (Proof.show check_goal Seq.single true) Proof.chain;
   415 val thus_i      = local_statement_i' (Proof.show_i check_goal Seq.single true) Proof.chain;
   416 
   417 fun gen_def f ((name, srcs), def) = ProofHistory.apply (fn state =>
   418   f name (map (Attrib.local_attribute (Proof.theory_of state)) srcs) def state);
   419 
   420 val local_def = gen_def Proof.def;
   421 val local_def_i = gen_def Proof.def_i;
   422 
   423 fun obtain (xs, asms) = proof'' (ProofHistory.applys o (fn print => fn state =>
   424   Obtain.obtain xs (map (local_attributes' state) asms) print state));
   425 
   426 fun obtain_i (xs, asms) = proof'' (ProofHistory.applys o Obtain.obtain_i xs asms);
   427 
   428 end;
   429 
   430 
   431 (* blocks *)
   432 
   433 val begin_block = ProofHistory.apply Proof.begin_block;
   434 val next_block = ProofHistory.apply Proof.next_block;
   435 val end_block = ProofHistory.applys Proof.end_block;
   436 
   437 
   438 (* shuffle state *)
   439 
   440 fun shuffle meth i = Method.refine (Method.Basic (K (meth i))) o Proof.assert_no_chain;
   441 
   442 fun defer i = ProofHistory.applys (shuffle Method.defer i);
   443 fun prefer i = ProofHistory.applys (shuffle Method.prefer i);
   444 
   445 
   446 (* backward steps *)
   447 
   448 fun apply m = ProofHistory.applys
   449   (Seq.map (Proof.goal_facts (K [])) o Method.refine m o Proof.assert_backward);
   450 fun apply_end m = ProofHistory.applys (Method.refine_end m o Proof.assert_forward);
   451 val proof = ProofHistory.applys o Method.proof;
   452 
   453 
   454 (* local endings *)
   455 
   456 val local_qed = proof'' o (ProofHistory.applys oo Method.local_qed true);
   457 val skip_local_qed =
   458   Toplevel.skip_proof (History.apply (fn i => if i > 1 then i - 1 else raise Toplevel.UNDEF));
   459 val local_terminal_proof = proof'' o (ProofHistory.applys oo Method.local_terminal_proof);
   460 val local_default_proof = proof'' (ProofHistory.applys o Method.local_default_proof);
   461 val local_immediate_proof = proof'' (ProofHistory.applys o Method.local_immediate_proof);
   462 val local_done_proof = proof'' (ProofHistory.applys o Method.local_done_proof);
   463 val local_skip_proof = Toplevel.proof' (fn int =>
   464   ProofHistory.applys (SkipProof.local_skip_proof (cond_print_result_rule int) int));
   465 
   466 
   467 (* global endings *)
   468 
   469 fun global_result finish = Toplevel.proof_to_theory' (fn int => fn prf =>
   470   let
   471     val state = ProofHistory.current prf;
   472     val _ = if Proof.at_bottom state then () else raise Toplevel.UNDEF;
   473     val (thy, ((kind, name), res)) = finish int state;
   474   in
   475     if kind = "" orelse kind = Drule.internalK then ()
   476     else (print_result (Proof.context_of state) ((kind, name), res);
   477       Context.setmp (SOME thy) (Present.results kind) res);
   478     thy
   479   end);
   480 
   481 fun global_qed m = global_result (K (Method.global_qed true m));
   482 val skip_global_qed = Toplevel.skip_proof_to_theory (equal 1 o History.current);
   483 fun global_terminal_proof m = global_result (K (Method.global_terminal_proof m));
   484 val global_default_proof = global_result (K Method.global_default_proof);
   485 val global_immediate_proof = global_result (K Method.global_immediate_proof);
   486 val global_skip_proof = global_result SkipProof.global_skip_proof;
   487 val global_done_proof = global_result (K Method.global_done_proof);
   488 
   489 
   490 (* common endings *)
   491 
   492 fun qed meth = local_qed meth o global_qed meth o skip_local_qed o skip_global_qed;
   493 fun terminal_proof meths = local_terminal_proof meths o global_terminal_proof meths;
   494 val default_proof = local_default_proof o global_default_proof;
   495 val immediate_proof = local_immediate_proof o global_immediate_proof;
   496 val done_proof = local_done_proof o global_done_proof;
   497 val skip_proof = local_skip_proof o global_skip_proof;
   498 val forget_proof = Toplevel.proof_to_theory (Proof.theory_of o ProofHistory.current) o
   499   Toplevel.skip_proof_to_theory (K true);
   500 
   501 
   502 (* calculational proof commands *)
   503 
   504 local
   505 
   506 fun cond_print_calc int ctxt thms =
   507   if int then
   508     Pretty.writeln (Pretty.big_list "calculation:" (map (ProofContext.pretty_thm ctxt) thms))
   509   else ();
   510 
   511 fun proof''' f = Toplevel.proof' (f o cond_print_calc);
   512 
   513 fun gen_calc get f args prt state =
   514   f (Option.map (fn srcs => get srcs state) args) prt state;
   515 
   516 in
   517 
   518 fun get_thmss srcs = Proof.the_facts o local_thmss Proof.note_thmss [(("", []), srcs)];
   519 fun get_thmss_i thms _ = thms;
   520 
   521 fun also x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.also x);
   522 fun also_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.also x);
   523 fun finally x = proof''' (ProofHistory.applys o gen_calc get_thmss Calculation.finally x);
   524 fun finally_i x = proof''' (ProofHistory.applys o gen_calc get_thmss_i Calculation.finally x);
   525 val moreover = proof''' (ProofHistory.apply o Calculation.moreover);
   526 val ultimately = proof''' (ProofHistory.apply o Calculation.ultimately);
   527 
   528 end;
   529 
   530 
   531 (* translation functions *)
   532 
   533 fun advancedT false = ""
   534   | advancedT true = "Sign.sg -> ";
   535 
   536 fun advancedN false = ""
   537   | advancedN true = "advanced_";
   538 
   539 fun parse_ast_translation (a, txt) =
   540   txt |> Context.use_let ("val parse_ast_translation: (string * (" ^ advancedT a ^
   541       "Syntax.ast list -> Syntax.ast)) list")
   542     ("Theory.add_" ^ advancedN a ^ "trfuns (parse_ast_translation, [], [], [])");
   543 
   544 fun parse_translation (a, txt) =
   545   txt |> Context.use_let ("val parse_translation: (string * (" ^ advancedT a ^
   546       "term list -> term)) list")
   547     ("Theory.add_" ^ advancedN a ^ "trfuns ([], parse_translation, [], [])");
   548 
   549 fun print_translation (a, txt) =
   550   txt |> Context.use_let ("val print_translation: (string * (" ^ advancedT a ^
   551       "term list -> term)) list")
   552     ("Theory.add_" ^ advancedN a ^ "trfuns ([], [], print_translation, [])");
   553 
   554 fun print_ast_translation (a, txt) =
   555   txt |> Context.use_let ("val print_ast_translation: (string * (" ^ advancedT a ^
   556       "Syntax.ast list -> Syntax.ast)) list")
   557     ("Theory.add_" ^ advancedN a ^ "trfuns ([], [], [], print_ast_translation)");
   558 
   559 fun typed_print_translation (a, txt) =
   560   txt |> Context.use_let ("val typed_print_translation: (string * (" ^ advancedT a ^
   561       "bool -> typ -> term list -> term)) list")
   562     ("Theory.add_" ^ advancedN a ^ "trfunsT typed_print_translation");
   563 
   564 val token_translation =
   565   Context.use_let "val token_translation: (string * string * (string -> string * real)) list"
   566     "Theory.add_tokentrfuns token_translation";
   567 
   568 
   569 (* add method *)
   570 
   571 fun method_setup (name, txt, cmt) =
   572   Context.use_let
   573     "val thm = PureThy.get_thm_closure (Context.the_context ());\n\
   574     \val thms = PureThy.get_thms_closure (Context.the_context ());\n\
   575     \val method: bstring * (Args.src -> Proof.context -> Proof.method) * string"
   576     "PureIsar.Method.add_method method"
   577     ("(" ^ Library.quote name ^ ", " ^ txt ^ ", " ^ Library.quote cmt ^ ")");
   578 
   579 
   580 (* add_oracle *)
   581 
   582 fun add_oracle (name, txt) =
   583   Context.use_let
   584     "val oracle: bstring * (Sign.sg * Object.T -> term)"
   585     "Theory.add_oracle oracle"
   586     ("(" ^ Library.quote name ^ ", " ^ txt ^ ")");
   587 
   588 
   589 (* add_locale *)
   590 
   591 fun add_locale (do_pred, name, import, body) thy =
   592   Locale.add_locale do_pred name import
   593     (map (Locale.map_attrib_element (Attrib.multi_attribute thy)) body) thy;
   594 
   595 (* theory init and exit *)
   596 
   597 fun gen_begin_theory upd name parents files =
   598   let val ml_filename = Path.pack (ThyLoad.ml_path name);
   599       val () = if exists (equal ml_filename o #1) files
   600                then error ((quote ml_filename) ^ " not allowed in files section for theory " ^ name)
   601                else ()
   602   in ThyInfo.begin_theory Present.begin_theory upd name parents (map (apfst Path.unpack) files) end;
   603 
   604 val begin_theory = gen_begin_theory false;
   605 
   606 fun end_theory thy =
   607   if ThyInfo.check_known_thy (PureThy.get_name thy) then ThyInfo.end_theory thy
   608   else thy;
   609 
   610 val kill_theory = ThyInfo.if_known_thy ThyInfo.remove_thy;
   611 
   612 
   613 fun bg_theory (name, parents, files) int = gen_begin_theory int name parents files;
   614 fun en_theory thy = (end_theory thy; ());
   615 
   616 fun theory spec = Toplevel.init_theory (bg_theory spec) en_theory (kill_theory o PureThy.get_name);
   617 
   618 
   619 (* context switch *)
   620 
   621 fun fetch_context f x =
   622   (case Context.pass NONE f x of
   623     ((), NONE) => raise Toplevel.UNDEF
   624   | ((), SOME thy) => thy);
   625 
   626 fun init_context f x = Toplevel.init_theory (fn _ => fetch_context f x) (K ()) (K ());
   627 
   628 val context = init_context (ThyInfo.quiet_update_thy true);
   629 
   630 
   631 (* registration of locale interpretation *)
   632 
   633 fun register_globally (((prfx, atts), expr), insts) b thy =
   634   let
   635     val (thy', propss, activate) = Locale.prep_global_registration
   636           (prfx, map (Attrib.global_attribute thy) atts) expr insts thy;
   637     fun witness id (thy, thm) = let
   638         val thm' = Drule.freeze_all thm;
   639       in
   640         (Locale.add_global_witness id thm' thy, thm')
   641       end;
   642   in
   643     multi_theorem_i Drule.internalK activate ("", []) [] 
   644       (map (fn (id as (n, _), props) => ((NameSpace.base n, [witness id]), 
   645         map (fn prop => (prop, ([], []))) props)) propss) b thy'
   646   end;
   647 
   648 fun register_locally (((prfx, atts), expr), insts) =
   649   ProofHistory.apply (fn state =>
   650     let
   651       val ctxt = Proof.context_of state;
   652       val (ctxt', propss, activate) = Locale.prep_local_registration
   653             (prfx, map (Attrib.local_attribute' ctxt) atts) expr insts ctxt;
   654       fun witness id (ctxt, thm) = (Locale.add_local_witness id thm ctxt, thm);
   655     in state
   656       |> Proof.map_context (fn _ => ctxt')
   657       |> Proof.interpret_i activate Seq.single true
   658            (map (fn (id as (n, _), props) => ((NameSpace.base n, [witness id]),
   659              map (fn prop => (prop, ([], []))) props)) propss)
   660     end);
   661 
   662 end;