src/Pure/goals.ML
author wenzelm
Mon Jun 21 16:41:06 2004 +0200 (2004-06-21)
changeset 14990 582b655da757
parent 14900 c66394c408f7
child 15457 1fbd4aba46e3
permissions -rw-r--r--
pretty_abbr;
clasohm@1460
     1
(*  Title: 	Pure/goals.ML
clasohm@0
     2
    ID:         $Id$
wenzelm@12012
     3
    Author: 	Lawrence C Paulson and Florian Kammueller, Cambridge University Computer Laboratory
clasohm@0
     4
    Copyright   1993  University of Cambridge
clasohm@0
     5
wenzelm@12012
     6
Old-style locales and goal stack package.  The goal stack initially
wenzelm@12012
     7
holds a dummy proof, and can never become empty.  Each goal stack
wenzelm@12012
     8
consists of a list of levels.  The undo list is a list of goal stacks.
wenzelm@12012
     9
Finally, there may be a stack of pending proofs.  Additional support
wenzelm@12012
    10
for locales.
clasohm@0
    11
*)
clasohm@0
    12
wenzelm@12012
    13
signature BASIC_GOALS =
wenzelm@11884
    14
sig
wenzelm@12012
    15
  val Open_locale: xstring -> unit
wenzelm@12012
    16
  val Close_locale: xstring -> unit
wenzelm@12012
    17
  val Print_scope: unit -> unit
wenzelm@12012
    18
  val thm: xstring -> thm
wenzelm@12012
    19
  val thms: xstring -> thm list
clasohm@0
    20
  type proof
wenzelm@7234
    21
  val reset_goals       : unit -> unit
nipkow@5041
    22
  val atomic_goal	: theory -> string -> thm list
nipkow@5041
    23
  val atomic_goalw	: theory -> thm list -> string -> thm list
wenzelm@6189
    24
  val Goal		: string -> thm list
wenzelm@6189
    25
  val Goalw		: thm list -> string -> thm list
clasohm@1460
    26
  val ba		: int -> unit
clasohm@1460
    27
  val back		: unit -> unit
clasohm@1460
    28
  val bd		: thm -> int -> unit
clasohm@1460
    29
  val bds		: thm list -> int -> unit
clasohm@1460
    30
  val be		: thm -> int -> unit
clasohm@1460
    31
  val bes		: thm list -> int -> unit
clasohm@1460
    32
  val br		: thm -> int -> unit
clasohm@1460
    33
  val brs		: thm list -> int -> unit
clasohm@1460
    34
  val bw		: thm -> unit
clasohm@1460
    35
  val bws		: thm list -> unit
clasohm@1460
    36
  val by		: tactic -> unit
clasohm@1460
    37
  val byev		: tactic list -> unit
clasohm@1460
    38
  val chop		: unit -> unit
clasohm@1460
    39
  val choplev		: int -> unit
paulson@6017
    40
  val export_thy        : theory -> thm -> thm
wenzelm@5246
    41
  val export            : thm -> thm
wenzelm@6189
    42
  val Export		: thm -> thm
clasohm@1460
    43
  val fa		: unit -> unit
clasohm@1460
    44
  val fd		: thm -> unit
clasohm@1460
    45
  val fds		: thm list -> unit
clasohm@1460
    46
  val fe		: thm -> unit
clasohm@1460
    47
  val fes		: thm list -> unit
clasohm@1460
    48
  val filter_goal	: (term*term->bool) -> thm list -> int -> thm list
clasohm@1460
    49
  val fr		: thm -> unit
clasohm@1460
    50
  val frs		: thm list -> unit
clasohm@1460
    51
  val getgoal		: int -> term
clasohm@1460
    52
  val gethyps		: int -> thm list
clasohm@1460
    53
  val goal		: theory -> string -> thm list
clasohm@1460
    54
  val goalw		: theory -> thm list -> string -> thm list
clasohm@1460
    55
  val goalw_cterm	: thm list -> cterm -> thm list
clasohm@1460
    56
  val pop_proof		: unit -> thm list
clasohm@1460
    57
  val pr		: unit -> unit
wenzelm@8884
    58
  val disable_pr        : unit -> unit
wenzelm@8884
    59
  val enable_pr         : unit -> unit
paulson@2580
    60
  val prlev		: int -> unit
paulson@2514
    61
  val prlim		: int -> unit
clasohm@1460
    62
  val premises		: unit -> thm list
clasohm@1460
    63
  val prin		: term -> unit
clasohm@1460
    64
  val printyp		: typ -> unit
clasohm@1460
    65
  val pprint_term	: term -> pprint_args -> unit
clasohm@1460
    66
  val pprint_typ	: typ -> pprint_args -> unit
clasohm@1460
    67
  val print_exn		: exn -> 'a
clasohm@1460
    68
  val print_sign_exn	: Sign.sg -> exn -> 'a
clasohm@1460
    69
  val prove_goal	: theory -> string -> (thm list -> tactic list) -> thm
lcp@577
    70
  val prove_goalw      : theory->thm list->string->(thm list->tactic list)->thm
clasohm@1460
    71
  val prove_goalw_cterm	: thm list->cterm->(thm list->tactic list)->thm
paulson@5311
    72
  val prove_goalw_cterm_nocheck	: thm list->cterm->(thm list->tactic list)->thm
wenzelm@11884
    73
  val quick_and_dirty_prove_goalw_cterm: theory -> thm list -> cterm
wenzelm@11884
    74
    -> (thm list -> tactic list) -> thm
berghofe@13712
    75
  val simple_prove_goal_cterm : cterm->(thm list->tactic list)->thm
clasohm@1460
    76
  val push_proof	: unit -> unit
clasohm@1460
    77
  val read		: string -> term
clasohm@1460
    78
  val ren		: string -> int -> unit
clasohm@1460
    79
  val restore_proof	: proof -> thm list
clasohm@1460
    80
  val result		: unit -> thm  
wenzelm@3532
    81
  val result_error_fn   : (thm -> string -> thm) ref
clasohm@1460
    82
  val rotate_proof	: unit -> thm list
clasohm@1460
    83
  val uresult		: unit -> thm  
clasohm@1460
    84
  val save_proof	: unit -> proof
clasohm@1460
    85
  val topthm		: unit -> thm
clasohm@1460
    86
  val undo		: unit -> unit
wenzelm@11884
    87
  val bind_thm          : string * thm -> unit
wenzelm@11884
    88
  val bind_thms         : string * thm list -> unit
wenzelm@11884
    89
  val qed               : string -> unit
wenzelm@11884
    90
  val qed_goal          : string -> theory -> string -> (thm list -> tactic list) -> unit
wenzelm@11884
    91
  val qed_goalw         : string -> theory -> thm list -> string
wenzelm@11884
    92
    -> (thm list -> tactic list) -> unit
wenzelm@11884
    93
  val qed_spec_mp       : string -> unit
wenzelm@11884
    94
  val qed_goal_spec_mp  : string -> theory -> string -> (thm list -> tactic list) -> unit
wenzelm@11884
    95
  val qed_goalw_spec_mp : string -> theory -> thm list -> string
wenzelm@11884
    96
    -> (thm list -> tactic list) -> unit
wenzelm@11884
    97
  val no_qed: unit -> unit
wenzelm@11884
    98
  val thms_containing   : xstring list -> (string * thm) list
wenzelm@11884
    99
  val findI             : int -> (string * thm) list
wenzelm@11884
   100
  val findEs            : int -> (string * thm) list
wenzelm@11884
   101
  val findE             : int -> int -> (string * thm) list
wenzelm@11884
   102
end;
clasohm@0
   103
wenzelm@12012
   104
signature GOALS =
wenzelm@12012
   105
sig
wenzelm@12012
   106
  include BASIC_GOALS
wenzelm@12012
   107
  type locale
wenzelm@12012
   108
  val print_locales: theory -> unit
wenzelm@12012
   109
  val get_thm: theory -> xstring -> thm
wenzelm@12012
   110
  val get_thms: theory -> xstring -> thm list
wenzelm@12012
   111
  val add_locale: bstring -> (bstring option) -> (string * string * mixfix) list ->
wenzelm@12012
   112
    (string * string) list -> (string * string) list -> theory -> theory
wenzelm@12012
   113
  val add_locale_i: bstring -> (bstring option) -> (string * typ * mixfix) list ->
wenzelm@12012
   114
    (string * term) list -> (string * term) list -> theory -> theory
wenzelm@12012
   115
  val open_locale: xstring -> theory -> theory
wenzelm@12012
   116
  val close_locale: xstring -> theory -> theory
wenzelm@12012
   117
  val print_scope: theory -> unit
wenzelm@12012
   118
  val read_cterm: Sign.sg -> string * typ -> cterm
wenzelm@12012
   119
  val setup: (theory -> theory) list
wenzelm@12012
   120
end;
wenzelm@12012
   121
wenzelm@13272
   122
structure Goals: GOALS =
clasohm@0
   123
struct
clasohm@0
   124
wenzelm@12012
   125
(*** Old-style locales ***)
wenzelm@12012
   126
wenzelm@12012
   127
(* Locales. The theory section 'locale' declarings constants,
wenzelm@12012
   128
assumptions and definitions that have local scope.  Typical form is
wenzelm@12012
   129
wenzelm@12012
   130
    locale Locale_name =
wenzelm@12012
   131
      fixes   (*variables that are fixed in the locale's scope*)
wenzelm@12012
   132
	v :: T
wenzelm@12012
   133
      assumes (*meta-hypothesis that hold in the locale*)
wenzelm@12012
   134
	Asm_name "meta-formula"  
wenzelm@12012
   135
      defines (*local definitions of fixed variables in terms of others*)
wenzelm@12012
   136
	v_def "v x == ...x..."
wenzelm@12012
   137
*)
wenzelm@12012
   138
wenzelm@12012
   139
(** type locale **)
wenzelm@12012
   140
wenzelm@12012
   141
type locale =
wenzelm@12012
   142
 {ancestor: string option,
wenzelm@12012
   143
  consts: (string * typ) list,
wenzelm@12012
   144
  nosyn: string list,
wenzelm@12012
   145
  rules: (string * term) list,
wenzelm@12012
   146
  defs: (string * term) list,
wenzelm@12012
   147
  thms: (string * thm) list,
wenzelm@12012
   148
  defaults: (string * sort) list * (string * typ) list * string list};
wenzelm@12012
   149
wenzelm@12012
   150
fun make_locale ancestor consts nosyn rules defs thms defaults =
wenzelm@12012
   151
  {ancestor = ancestor, consts = consts, nosyn = nosyn, rules = rules, 
wenzelm@12012
   152
   defs = defs, thms = thms, defaults = defaults}: locale;
wenzelm@12012
   153
wenzelm@12012
   154
fun pretty_locale sg (name, {ancestor, consts, rules, defs, nosyn = _, thms = _, defaults = _}) =
wenzelm@12012
   155
  let
wenzelm@12012
   156
    val prt_typ = Pretty.quote o Sign.pretty_typ sg;
wenzelm@12012
   157
    val prt_term = Pretty.quote o Sign.pretty_term sg;
wenzelm@12012
   158
wenzelm@12012
   159
    fun pretty_const (c, T) = Pretty.block
wenzelm@12012
   160
      [Pretty.str (c ^ " ::"), Pretty.brk 1, prt_typ T];
wenzelm@12012
   161
wenzelm@12012
   162
    fun pretty_axiom (a, t) = Pretty.block
wenzelm@12012
   163
      [Pretty.str (a ^ ":"), Pretty.brk 1, prt_term t];
wenzelm@12012
   164
wenzelm@12012
   165
    val anc = case ancestor of
wenzelm@12012
   166
                  None => ""
wenzelm@12012
   167
                | Some(loc) => ((Sign.base_name loc) ^ " +")
wenzelm@12012
   168
  in
wenzelm@12012
   169
    Pretty.big_list (name ^ " = " ^ anc)
wenzelm@12012
   170
     [Pretty.big_list "consts:" (map pretty_const consts),
wenzelm@12012
   171
      Pretty.big_list "rules:" (map pretty_axiom rules),
wenzelm@12012
   172
      Pretty.big_list "defs:" (map pretty_axiom defs)]
wenzelm@12012
   173
  end;
wenzelm@12012
   174
wenzelm@12012
   175
wenzelm@12012
   176
wenzelm@12012
   177
(** theory data **)
wenzelm@12012
   178
wenzelm@12012
   179
(* data kind 'Pure/old-locales' *)
wenzelm@12012
   180
wenzelm@12012
   181
type locale_data =
wenzelm@12012
   182
  {space: NameSpace.T,
wenzelm@12012
   183
    locales: locale Symtab.table,
wenzelm@12012
   184
    scope: (string * locale) list ref};
wenzelm@12012
   185
wenzelm@12012
   186
fun make_locale_data space locales scope =
wenzelm@12012
   187
  {space = space, locales = locales, scope = scope}: locale_data;
wenzelm@12012
   188
wenzelm@12012
   189
structure LocalesArgs =
wenzelm@12012
   190
struct
wenzelm@12012
   191
  val name = "Pure/old-locales";
wenzelm@12012
   192
  type T = locale_data;
wenzelm@12012
   193
wenzelm@12012
   194
  val empty = make_locale_data NameSpace.empty Symtab.empty (ref []);
wenzelm@12012
   195
  fun copy {space, locales, scope = ref locs} = make_locale_data space locales (ref locs);
wenzelm@12012
   196
  fun prep_ext {space, locales, scope = _} = make_locale_data space locales (ref []);
wenzelm@12012
   197
  fun merge ({space = space1, locales = locales1, scope = _},
wenzelm@12012
   198
    {space = space2, locales = locales2, scope = _}) =
wenzelm@12012
   199
      make_locale_data (NameSpace.merge (space1, space2))
wenzelm@12012
   200
        (Symtab.merge (K true) (locales1, locales2))
wenzelm@12012
   201
        (ref []);
wenzelm@12012
   202
wenzelm@12012
   203
  fun print sg {space, locales, scope} =
wenzelm@12012
   204
    let
wenzelm@12012
   205
      fun extrn name =
wenzelm@12012
   206
        if ! long_names then name else NameSpace.extern space name;
wenzelm@12012
   207
      val locs = map (apfst extrn) (Symtab.dest locales);
wenzelm@12012
   208
      val scope_names = rev (map (extrn o fst) (! scope));
wenzelm@12012
   209
    in
wenzelm@12012
   210
      [Display.pretty_name_space ("locale name space", space),
wenzelm@12012
   211
        Pretty.big_list "locales:" (map (pretty_locale sg) locs),
wenzelm@12012
   212
        Pretty.strs ("current scope:" :: scope_names)]
wenzelm@12012
   213
      |> Pretty.chunks |> Pretty.writeln
wenzelm@12012
   214
    end;
wenzelm@12012
   215
end;
wenzelm@12012
   216
wenzelm@12012
   217
wenzelm@12012
   218
structure LocalesData = TheoryDataFun(LocalesArgs);
wenzelm@12012
   219
val print_locales = LocalesData.print;
wenzelm@12012
   220
wenzelm@12012
   221
wenzelm@12012
   222
(* access locales *)
wenzelm@12012
   223
wenzelm@12012
   224
fun get_locale_sg sg name = Symtab.lookup (#locales (LocalesData.get_sg sg), name);
wenzelm@12012
   225
wenzelm@12012
   226
val get_locale = get_locale_sg o Theory.sign_of;
wenzelm@12012
   227
wenzelm@12012
   228
fun put_locale (name, locale) thy =
wenzelm@12012
   229
  let
wenzelm@12012
   230
    val {space, locales, scope} = LocalesData.get thy;
wenzelm@12012
   231
    val space' = NameSpace.extend (space, [name]);
wenzelm@12012
   232
    val locales' = Symtab.update ((name, locale), locales);
wenzelm@12012
   233
  in thy |> LocalesData.put (make_locale_data space' locales' scope) end;
wenzelm@12012
   234
wenzelm@12012
   235
fun lookup_locale thy xname =
wenzelm@12012
   236
  let
wenzelm@12012
   237
    val {space, locales, ...} = LocalesData.get thy;
wenzelm@12012
   238
    val name = NameSpace.intern space xname;
wenzelm@12012
   239
  in apsome (pair name) (get_locale thy name) end;
wenzelm@12012
   240
wenzelm@12012
   241
wenzelm@12012
   242
(* access scope *)
wenzelm@12012
   243
wenzelm@12012
   244
fun get_scope_sg sg =
wenzelm@12012
   245
  if Sign.eq_sg (sg, Theory.sign_of ProtoPure.thy) then []
wenzelm@12012
   246
  else ! (#scope (LocalesData.get_sg sg));
wenzelm@12012
   247
wenzelm@12012
   248
val get_scope = get_scope_sg o Theory.sign_of;
wenzelm@12012
   249
wenzelm@12012
   250
fun change_scope f thy =
wenzelm@12012
   251
  let val {scope, ...} = LocalesData.get thy
wenzelm@12012
   252
  in scope := f (! scope) end;
wenzelm@12012
   253
wenzelm@12012
   254
wenzelm@12012
   255
wenzelm@12012
   256
(** scope operations **)
wenzelm@12012
   257
wenzelm@12012
   258
(* change scope *)
wenzelm@12012
   259
wenzelm@12012
   260
fun the_locale thy xname =
wenzelm@12012
   261
  (case lookup_locale thy xname of
wenzelm@12012
   262
    Some loc => loc
wenzelm@12012
   263
  | None => error ("Unknown locale " ^ quote xname));
wenzelm@12012
   264
wenzelm@12012
   265
fun open_locale xname thy =
wenzelm@12012
   266
  let val loc = the_locale thy xname;
wenzelm@12012
   267
      val anc = #ancestor(#2(loc));
wenzelm@12012
   268
      val cur_sc = get_scope thy;
wenzelm@12012
   269
      fun opn lc th = (change_scope (cons lc) th; th)
wenzelm@12012
   270
  in case anc of
wenzelm@12012
   271
         None => opn loc thy
wenzelm@12012
   272
       | Some(loc') => 
wenzelm@12012
   273
           if loc' mem (map fst cur_sc) 
wenzelm@12012
   274
           then opn loc thy
wenzelm@12012
   275
           else (warning ("Opening locale " ^ quote loc' ^ ", required by " ^ 
wenzelm@12012
   276
			  quote xname);
wenzelm@12012
   277
                 opn loc (open_locale (Sign.base_name loc') thy))
wenzelm@12012
   278
  end;
wenzelm@12012
   279
wenzelm@12012
   280
fun pop_locale [] = error "Currently no open locales"
wenzelm@12012
   281
  | pop_locale (_ :: locs) = locs;
wenzelm@12012
   282
wenzelm@12012
   283
fun close_locale name thy = 
wenzelm@12012
   284
   let val lname = (case get_scope thy of (ln,_)::_ => ln
wenzelm@12012
   285
                                        | _ => error "No locales are open!")
wenzelm@12012
   286
       val ok = (name = Sign.base_name lname) handle _ => false
wenzelm@12012
   287
   in if ok then (change_scope pop_locale thy; thy)
wenzelm@12012
   288
      else error ("locale " ^ name ^ " is not top of scope; top is " ^ lname)
wenzelm@12012
   289
   end;
wenzelm@12012
   290
wenzelm@12012
   291
fun print_scope thy = 
wenzelm@12012
   292
Pretty.writeln (Pretty.strs ("current scope:" :: rev(map (Sign.base_name o fst) (get_scope thy))));
wenzelm@12012
   293
wenzelm@12012
   294
(*implicit context versions*)
wenzelm@12012
   295
fun Open_locale xname = (open_locale xname (Context.the_context ()); ());
wenzelm@12012
   296
fun Close_locale xname = (close_locale xname (Context.the_context ()); ());
wenzelm@12012
   297
fun Print_scope () = (print_scope (Context.the_context ()); ());
wenzelm@12012
   298
wenzelm@12012
   299
wenzelm@12012
   300
(** functions for goals.ML **)
wenzelm@12012
   301
wenzelm@12012
   302
(* in_locale: check if hyps (: term list) of a proof are contained in the
wenzelm@12012
   303
   (current) scope. This function is needed in prepare_proof. It needs to
wenzelm@12012
   304
   refer to the signature, because theory is not available in prepare_proof. *)
wenzelm@12012
   305
wenzelm@12012
   306
fun in_locale hyps sg =
wenzelm@12012
   307
    let val cur_sc = get_scope_sg sg;
wenzelm@12012
   308
        val rule_lists = map (#rules o snd) cur_sc;
wenzelm@12012
   309
        val def_lists = map (#defs o snd) cur_sc;
wenzelm@12012
   310
        val rules = map snd (foldr (op union) (rule_lists, []));
wenzelm@12012
   311
        val defs = map snd (foldr (op union) (def_lists, []));
wenzelm@12012
   312
        val defnrules = rules @ defs;
wenzelm@12012
   313
    in
wenzelm@12012
   314
        hyps subset defnrules
wenzelm@12012
   315
    end;
wenzelm@12012
   316
wenzelm@12012
   317
wenzelm@12012
   318
(* is_open_loc: check if any locale is open, i.e. in the scope of the current thy *)
wenzelm@12012
   319
fun is_open_loc_sg sign =
wenzelm@12012
   320
    let val cur_sc = get_scope_sg sign
wenzelm@12012
   321
    in not(null(cur_sc)) end;
wenzelm@12012
   322
wenzelm@12012
   323
val is_open_loc = is_open_loc_sg o Theory.sign_of;
wenzelm@12012
   324
wenzelm@12012
   325
wenzelm@12012
   326
(* get theorems *)
wenzelm@12012
   327
wenzelm@12012
   328
fun get_thm_locale name ((_, {thms, ...}: locale)) = assoc (thms, name);
wenzelm@12012
   329
wenzelm@12012
   330
fun get_thmx f get thy name =
wenzelm@12012
   331
  (case get_first (get_thm_locale name) (get_scope thy) of
wenzelm@12012
   332
    Some thm => f thm
wenzelm@12012
   333
  | None => get thy name);
wenzelm@12012
   334
wenzelm@12012
   335
val get_thm = get_thmx I PureThy.get_thm;
wenzelm@12012
   336
val get_thms = get_thmx (fn x => [x]) PureThy.get_thms;
wenzelm@12012
   337
wenzelm@12012
   338
fun thm name = get_thm (Context.the_context ()) name;
wenzelm@12012
   339
fun thms name = get_thms (Context.the_context ()) name;
wenzelm@12012
   340
wenzelm@12012
   341
wenzelm@12012
   342
(* get the defaults of a locale, for extension *)
wenzelm@12012
   343
wenzelm@12012
   344
fun get_defaults thy name = 
wenzelm@12012
   345
  let val (lname, loc) = the_locale thy name;
wenzelm@12012
   346
  in #defaults(loc)
wenzelm@12012
   347
  end;
wenzelm@12012
   348
wenzelm@12012
   349
wenzelm@12012
   350
(** define locales **)
wenzelm@12012
   351
wenzelm@12012
   352
(* prepare types *)
wenzelm@12012
   353
wenzelm@12012
   354
fun read_typ sg (envT, s) =
wenzelm@12012
   355
  let
wenzelm@12012
   356
    fun def_sort (x, ~1) = assoc (envT, x)
wenzelm@12012
   357
      | def_sort _ = None;
wenzelm@12012
   358
    val T = Type.no_tvars (Sign.read_typ (sg, def_sort) s) handle TYPE (msg, _, _) => error msg;
wenzelm@12012
   359
  in (Term.add_typ_tfrees (T, envT), T) end;
wenzelm@12012
   360
wenzelm@12012
   361
fun cert_typ sg (envT, raw_T) =
wenzelm@12012
   362
  let val T = Type.no_tvars (Sign.certify_typ sg raw_T) handle TYPE (msg, _, _) => error msg
wenzelm@12012
   363
  in (Term.add_typ_tfrees (T, envT), T) end;
wenzelm@12012
   364
wenzelm@12012
   365
wenzelm@12012
   366
(* prepare props *)
wenzelm@12012
   367
wenzelm@12012
   368
val add_frees = foldl_aterms (fn (vs, Free v) => v ins vs | (vs, _) => vs);
wenzelm@12012
   369
wenzelm@12012
   370
fun enter_term t (envS, envT, used) =
wenzelm@12012
   371
  (Term.add_term_tfrees (t, envS), add_frees (envT, t), Term.add_term_tfree_names (t, used));
wenzelm@12012
   372
wenzelm@12012
   373
fun read_axm sg ((envS, envT, used), (name, s)) =
wenzelm@12012
   374
  let
wenzelm@12012
   375
    fun def_sort (x, ~1) = assoc (envS, x)
wenzelm@12012
   376
      | def_sort _ = None;
wenzelm@12012
   377
    fun def_type (x, ~1) = assoc (envT, x)
wenzelm@12012
   378
      | def_type _ = None;
wenzelm@12012
   379
    val (_, t) = Theory.read_def_axm (sg, def_type, def_sort) used (name, s);
wenzelm@12012
   380
  in
wenzelm@12012
   381
    (enter_term t (envS, envT, used), t)
wenzelm@12012
   382
  end;
wenzelm@12012
   383
wenzelm@12012
   384
wenzelm@12012
   385
fun cert_axm sg ((envS, envT, used), (name, raw_t)) =
wenzelm@12012
   386
  let val (_, t) = Theory.cert_axm sg (name, raw_t)
wenzelm@12012
   387
  in (enter_term t (envS, envT, used), t) end;
wenzelm@12012
   388
wenzelm@12012
   389
wenzelm@12012
   390
(* read_cterm: read in a string as a certified term, and respect the bindings
wenzelm@12012
   391
   that already exist for subterms. If no locale is open, this function is equal to
wenzelm@12012
   392
   Thm.read_cterm  *)
wenzelm@12012
   393
wenzelm@12012
   394
fun read_cterm sign =
wenzelm@12012
   395
    let val cur_sc = get_scope_sg sign;
wenzelm@12012
   396
        val defaults = map (#defaults o snd) cur_sc;
wenzelm@12012
   397
        val envS = flat (map #1 defaults);
wenzelm@12012
   398
        val envT = flat (map #2 defaults);
wenzelm@12012
   399
        val used = flat (map #3 defaults);
wenzelm@12012
   400
        fun def_sort (x, ~1) = assoc (envS, x)
wenzelm@12012
   401
          | def_sort _ = None;
wenzelm@12012
   402
        fun def_type (x, ~1) = assoc (envT, x)
wenzelm@12012
   403
          | def_type _ = None;
wenzelm@12012
   404
    in (if (is_open_loc_sg sign)
wenzelm@12012
   405
        then (#1 o read_def_cterm (sign, def_type, def_sort) used true)
wenzelm@12012
   406
        else Thm.read_cterm sign)
wenzelm@12012
   407
    end;
wenzelm@12012
   408
wenzelm@12012
   409
(* basic functions needed for definitions and display *)
wenzelm@12012
   410
(* collect all locale constants of a scope, i.e. a list of locales *)
wenzelm@12012
   411
fun collect_consts sg =
wenzelm@12012
   412
    let val cur_sc = get_scope_sg sg;
wenzelm@12012
   413
        val locale_list = map snd cur_sc;
wenzelm@12012
   414
        val const_list = flat (map #consts locale_list)
wenzelm@12012
   415
    in map fst const_list end;
wenzelm@12012
   416
wenzelm@12012
   417
(* filter out the Free's in a term *)
wenzelm@12012
   418
fun list_frees t =
wenzelm@12012
   419
    case t of Const(c,T) => []
wenzelm@12012
   420
  | Var(v,T) => []
wenzelm@12012
   421
  | Free(v,T)=> [Free(v,T)]
wenzelm@12012
   422
  | Bound x  => []
wenzelm@12012
   423
  | Abs(a,T,u) => list_frees u
wenzelm@12012
   424
  | t1 $ t2  => (list_frees t1)  @ (list_frees t2);
wenzelm@12012
   425
wenzelm@12012
   426
(* filter out all Free's in a term that are not contained
wenzelm@12012
   427
   in a list of strings. Used to prepare definitions. The list of strings
wenzelm@12012
   428
   will be the consts of the scope. We filter out the "free" Free's to be
wenzelm@12012
   429
   able to bind them *)
wenzelm@12012
   430
fun difflist term clist =
wenzelm@12012
   431
    let val flist = list_frees term;
wenzelm@12012
   432
        fun builddiff [] sl = []
wenzelm@12012
   433
          | builddiff (t :: tl) sl =
wenzelm@12012
   434
            let val Free(v,T) = t
wenzelm@12012
   435
            in
wenzelm@12012
   436
                if (v mem sl)
wenzelm@12012
   437
                then builddiff tl sl
wenzelm@12012
   438
                else t :: (builddiff tl sl)
wenzelm@12012
   439
            end;
wenzelm@12012
   440
    in distinct(builddiff flist clist) end;
wenzelm@12012
   441
wenzelm@12012
   442
(* Bind a term with !! over a list of "free" Free's.
wenzelm@12012
   443
   To enable definitions like x + y == .... (without quantifier).
wenzelm@12012
   444
   Complications, because x and y have to be removed from defaults *)
wenzelm@12012
   445
fun abs_over_free clist ((defaults: (string * sort) list * (string * typ) list * string list), (s, term)) =
wenzelm@12012
   446
    let val diffl = rev(difflist term clist);
wenzelm@12012
   447
        fun abs_o (t, (x as Free(v,T))) = all(T) $ Abs(v, T, abstract_over (x,t))
wenzelm@12012
   448
          | abs_o (_ , _) = error ("Can't be: abs_over_free");
wenzelm@12012
   449
        val diffl' = map (fn (Free (s, T)) => s) diffl;
wenzelm@12012
   450
        val defaults' = (#1 defaults, filter (fn x => not((fst x) mem diffl')) (#2 defaults), #3 defaults)
wenzelm@12012
   451
    in (defaults', (s, foldl abs_o (term, diffl))) end;
wenzelm@12012
   452
wenzelm@12012
   453
(* assume a definition, i.e assume the cterm of a definiton term and then eliminate
wenzelm@12012
   454
   the binding !!, so that the def can be applied as rewrite. The meta hyp will still contain !! *)
wenzelm@12012
   455
fun prep_hyps clist sg = forall_elim_vars(0) o Thm.assume o (Thm.cterm_of sg);
wenzelm@12012
   456
wenzelm@12012
   457
wenzelm@12012
   458
(* concrete syntax *)
wenzelm@12012
   459
wenzelm@12012
   460
fun mark_syn c = "\\<^locale>" ^ c;
wenzelm@12012
   461
wenzelm@12012
   462
fun mk_loc_tr c ts = list_comb (Free (c, dummyT), ts);
wenzelm@12012
   463
wenzelm@12012
   464
wenzelm@12012
   465
(* add_locale *)
wenzelm@12012
   466
wenzelm@12012
   467
fun gen_add_locale prep_typ prep_term bname bancestor raw_consts raw_rules raw_defs thy =
wenzelm@12012
   468
  let val sign = Theory.sign_of thy;
wenzelm@12012
   469
wenzelm@12012
   470
    val name = Sign.full_name sign bname;
wenzelm@12012
   471
wenzelm@12012
   472
    val (envSb, old_loc_consts, _) = 
wenzelm@12012
   473
                    case bancestor of
wenzelm@12012
   474
                       Some(loc) => (get_defaults thy loc)
wenzelm@12012
   475
                     | None      => ([],[],[]);
wenzelm@12012
   476
wenzelm@12012
   477
    val old_nosyn = case bancestor of 
wenzelm@12012
   478
                       Some(loc) => #nosyn(#2(the_locale thy loc))
wenzelm@12012
   479
                     | None      => [];
wenzelm@12012
   480
wenzelm@12012
   481
    (* Get the full name of the ancestor *)
wenzelm@12012
   482
    val ancestor = case bancestor of 
wenzelm@12012
   483
                       Some(loc) => Some(#1(the_locale thy loc))
wenzelm@12012
   484
                     | None      => None;
wenzelm@12012
   485
wenzelm@12012
   486
     (* prepare locale consts *)
wenzelm@12012
   487
wenzelm@12012
   488
    fun prep_const (envS, (raw_c, raw_T, raw_mx)) =
wenzelm@12012
   489
      let
wenzelm@12012
   490
        val c = Syntax.const_name raw_c raw_mx;
wenzelm@12012
   491
        val c_syn = mark_syn c;
wenzelm@12012
   492
        val mx = Syntax.fix_mixfix raw_c raw_mx;
wenzelm@12012
   493
        val (envS', T) = prep_typ sign (envS, raw_T) handle ERROR =>
wenzelm@12012
   494
          error ("The error(s) above occured in locale constant " ^ quote c);
wenzelm@12012
   495
        val trfun = if mx = Syntax.NoSyn then None else Some (c_syn, mk_loc_tr c);
wenzelm@12012
   496
      in (envS', ((c, T), (c_syn, T, mx), trfun)) end;
wenzelm@12012
   497
wenzelm@12012
   498
    val (envS0, loc_consts_syn) = foldl_map prep_const (envSb, raw_consts);
wenzelm@12012
   499
    val loc_consts = map #1 loc_consts_syn;
wenzelm@12012
   500
    val loc_consts = old_loc_consts @ loc_consts;
wenzelm@12012
   501
    val loc_syn = map #2 loc_consts_syn;
wenzelm@12012
   502
    val nosyn = old_nosyn @ (map (#1 o #1) (filter (fn x => (#3(#2 x)) = NoSyn) loc_consts_syn));
wenzelm@12012
   503
    val loc_trfuns = mapfilter #3 loc_consts_syn;
wenzelm@12012
   504
wenzelm@12012
   505
wenzelm@12012
   506
    (* 1st stage: syntax_thy *)
wenzelm@12012
   507
wenzelm@12012
   508
    val syntax_thy =
wenzelm@12012
   509
      thy
wenzelm@14900
   510
      |> Theory.add_modesyntax_i Syntax.default_mode loc_syn
wenzelm@12012
   511
      |> Theory.add_trfuns ([], loc_trfuns, [], []);
wenzelm@12012
   512
wenzelm@12012
   513
    val syntax_sign = Theory.sign_of syntax_thy;
wenzelm@12012
   514
wenzelm@12012
   515
wenzelm@12012
   516
    (* prepare rules and defs *)
wenzelm@12012
   517
wenzelm@12012
   518
    fun prep_axiom (env, (a, raw_t)) =
wenzelm@12012
   519
      let
wenzelm@12012
   520
        val (env', t) = prep_term syntax_sign (env, (a, raw_t)) handle ERROR =>
wenzelm@12012
   521
          error ("The error(s) above occured in locale rule / definition " ^ quote a);
wenzelm@12012
   522
      in (env', (a, t)) end;
wenzelm@12012
   523
wenzelm@12012
   524
    val ((envS1, envT1, used1), loc_rules) =
wenzelm@12012
   525
      foldl_map prep_axiom ((envS0, loc_consts, map fst envS0), raw_rules);
wenzelm@12012
   526
    val (defaults, loc_defs) = 
wenzelm@12012
   527
	foldl_map prep_axiom ((envS1, envT1, used1), raw_defs);
wenzelm@12012
   528
wenzelm@12012
   529
    val old_loc_consts = collect_consts syntax_sign;
wenzelm@12012
   530
    val new_loc_consts = (map #1 loc_consts);
wenzelm@12012
   531
    val all_loc_consts = old_loc_consts @ new_loc_consts;
wenzelm@12012
   532
wenzelm@12012
   533
    val (defaults, loc_defs_terms) = 
wenzelm@12012
   534
	foldl_map (abs_over_free all_loc_consts) (defaults, loc_defs);
wenzelm@12012
   535
    val loc_defs_thms = 
wenzelm@12012
   536
	map (apsnd (prep_hyps (map #1 loc_consts) syntax_sign)) loc_defs_terms;
wenzelm@12012
   537
    val (defaults, loc_thms_terms) = 
wenzelm@12012
   538
	foldl_map (abs_over_free all_loc_consts) (defaults, loc_rules);
wenzelm@12012
   539
    val loc_thms = map (apsnd (prep_hyps (map #1 loc_consts) syntax_sign))
wenzelm@12012
   540
		       (loc_thms_terms)
wenzelm@12012
   541
                   @ loc_defs_thms;
wenzelm@12012
   542
wenzelm@12012
   543
wenzelm@12012
   544
    (* error messages *) 
wenzelm@12012
   545
wenzelm@12012
   546
    fun locale_error msg = error (msg ^ "\nFor locale " ^ quote name);
wenzelm@12012
   547
wenzelm@12012
   548
    val err_dup_locale =
wenzelm@12012
   549
      if is_none (get_locale thy name) then []
wenzelm@12012
   550
      else ["Duplicate definition of locale " ^ quote name];
wenzelm@12012
   551
wenzelm@12012
   552
    (* check if definientes are locale constants 
wenzelm@12012
   553
       (in the same locale, so no redefining!) *)
wenzelm@12012
   554
    val err_def_head =
wenzelm@12012
   555
      let fun peal_appl t = 
wenzelm@12012
   556
            case t of 
wenzelm@12012
   557
                 t1 $ t2 => peal_appl t1
wenzelm@12012
   558
               | Free(t) => t
wenzelm@12012
   559
               | _ => locale_error ("Bad form of LHS in locale definition");
wenzelm@12012
   560
	  fun lhs (_, Const ("==" , _) $  d1 $ d2) = peal_appl d1
wenzelm@12012
   561
	    | lhs _ = locale_error ("Definitions must use the == relation");
wenzelm@12012
   562
          val defs = map lhs loc_defs;
wenzelm@12012
   563
          val check = defs subset loc_consts
wenzelm@12012
   564
      in if check then [] 
wenzelm@12012
   565
         else ["defined item not declared fixed in locale " ^ quote name]
wenzelm@12012
   566
      end; 
wenzelm@12012
   567
wenzelm@12012
   568
    (* check that variables on rhs of definitions are either fixed or on lhs *)
wenzelm@12012
   569
    val err_var_rhs = 
wenzelm@12012
   570
      let fun compare_var_sides (t, (_, Const ("==", _) $ d1 $ d2)) = 
wenzelm@12012
   571
		let val varl1 = difflist d1 all_loc_consts;
wenzelm@12012
   572
		    val varl2 = difflist d2 all_loc_consts
wenzelm@12012
   573
		in t andalso (varl2 subset varl1)
wenzelm@12012
   574
		end
wenzelm@12012
   575
            | compare_var_sides (_,_) = 
wenzelm@12012
   576
		locale_error ("Definitions must use the == relation")
wenzelm@12012
   577
          val check = foldl compare_var_sides (true, loc_defs)
wenzelm@12012
   578
      in if check then []
wenzelm@12012
   579
         else ["nonfixed variable on right hand side of a locale definition in locale " ^ quote name]
wenzelm@12012
   580
      end;
wenzelm@12012
   581
wenzelm@12012
   582
    val errs = err_dup_locale @ err_def_head @ err_var_rhs;
wenzelm@12012
   583
  in
wenzelm@12012
   584
    if null errs then ()
wenzelm@12012
   585
    else error (cat_lines errs);
wenzelm@12012
   586
wenzelm@12012
   587
    syntax_thy
wenzelm@12012
   588
    |> put_locale (name, 
wenzelm@12012
   589
		   make_locale ancestor loc_consts nosyn loc_thms_terms 
wenzelm@12012
   590
                                        loc_defs_terms   loc_thms defaults)
wenzelm@12012
   591
  end;
wenzelm@12012
   592
wenzelm@12012
   593
wenzelm@12012
   594
val add_locale = gen_add_locale read_typ read_axm;
wenzelm@12012
   595
val add_locale_i = gen_add_locale cert_typ cert_axm;
wenzelm@12012
   596
wenzelm@12012
   597
(** print functions **)
wenzelm@12012
   598
(* idea: substitute all locale contants (Free's) that are syntactical by their
wenzelm@12012
   599
         "real" constant representation (i.e. \\<^locale>constname).
wenzelm@12012
   600
   - function const_ssubst does this substitution
wenzelm@12012
   601
   - function pretty_term:
wenzelm@12012
   602
             if locale is open then do this substitution & then call Sign.pretty_term
wenzelm@12012
   603
             else call Sign.pretty_term
wenzelm@12012
   604
*)
wenzelm@12012
   605
(* substitutes all Free variables s in t by Const's s *)
wenzelm@12012
   606
fun const_ssubst t s =
wenzelm@12012
   607
    case t  of
wenzelm@12012
   608
        Free(v,T) => if v = s then Const("\\<^locale>" ^ s,T) else Free(v,T)
wenzelm@12012
   609
      | Const(c,T) => Const(c,T)
wenzelm@12012
   610
      | Var(v,T) => Var(v,T)
wenzelm@12012
   611
      | Bound x  => Bound x
wenzelm@12012
   612
      | Abs(a,T,u) => Abs(a,T, const_ssubst u s)
wenzelm@12012
   613
      | t1 $ t2  => const_ssubst t1 s $ const_ssubst t2 s;
wenzelm@12012
   614
wenzelm@12012
   615
(* FIXME: improve: can be expressed with foldl *)
wenzelm@12012
   616
fun const_ssubst_list [] t = t
wenzelm@12012
   617
  | const_ssubst_list (s :: l) t = const_ssubst_list l (const_ssubst t s);
wenzelm@12012
   618
wenzelm@12012
   619
(* pretty_term *)
wenzelm@12012
   620
fun pretty_term sign =
wenzelm@12012
   621
    if (is_open_loc_sg sign) then
wenzelm@12012
   622
        let val locale_list = map snd(get_scope_sg sign);
wenzelm@12012
   623
            val nosyn = flat (map #nosyn locale_list);
wenzelm@12012
   624
            val str_list = (collect_consts sign) \\ nosyn
wenzelm@12012
   625
        in Sign.pretty_term sign o (const_ssubst_list str_list)
wenzelm@12012
   626
        end
wenzelm@12012
   627
    else Sign.pretty_term sign;
wenzelm@12012
   628
wenzelm@12012
   629
wenzelm@12012
   630
(** locale theory setup **)
wenzelm@12012
   631
wenzelm@12012
   632
val setup =
wenzelm@12012
   633
 [LocalesData.init];
wenzelm@12012
   634
wenzelm@12012
   635
wenzelm@12012
   636
wenzelm@12012
   637
(*** Goal package ***)
wenzelm@12012
   638
clasohm@0
   639
(*Each level of goal stack includes a proof state and alternative states,
clasohm@0
   640
  the output of the tactic applied to the preceeding level.  *)
wenzelm@4270
   641
type gstack = (thm * thm Seq.seq) list;
clasohm@0
   642
clasohm@0
   643
datatype proof = Proof of gstack list * thm list * (bool*thm->thm);
clasohm@0
   644
wenzelm@5246
   645
clasohm@0
   646
(*** References ***)
clasohm@0
   647
clasohm@0
   648
(*Current assumption list -- set by "goal".*)
clasohm@0
   649
val curr_prems = ref([] : thm list);
clasohm@0
   650
clasohm@0
   651
(*Return assumption list -- useful if you didn't save "goal"'s result. *)
clasohm@0
   652
fun premises() = !curr_prems;
clasohm@0
   653
clasohm@0
   654
(*Current result maker -- set by "goal", used by "result".  *)
wenzelm@7234
   655
fun init_mkresult _ = error "No goal has been supplied in subgoal module";
wenzelm@7234
   656
val curr_mkresult = ref (init_mkresult: bool*thm->thm);
clasohm@0
   657
wenzelm@6390
   658
val dummy = trivial(read_cterm (Theory.sign_of ProtoPure.thy)
clasohm@0
   659
    ("PROP No_goal_has_been_supplied",propT));
clasohm@0
   660
clasohm@0
   661
(*List of previous goal stacks, for the undo operation.  Set by setstate. 
clasohm@0
   662
  A list of lists!*)
wenzelm@4270
   663
val undo_list = ref([[(dummy, Seq.empty)]] : gstack list);
clasohm@0
   664
clasohm@0
   665
(* Stack of proof attempts *)
clasohm@0
   666
val proofstack = ref([]: proof list);
clasohm@0
   667
wenzelm@7234
   668
(*reset all refs*)
wenzelm@7234
   669
fun reset_goals () =
wenzelm@7234
   670
  (curr_prems := []; curr_mkresult := init_mkresult;
oheimb@7942
   671
    undo_list := [[(dummy, Seq.empty)]]);
wenzelm@7234
   672
clasohm@0
   673
clasohm@0
   674
(*** Setting up goal-directed proof ***)
clasohm@0
   675
clasohm@0
   676
(*Generates the list of new theories when the proof state's signature changes*)
clasohm@0
   677
fun sign_error (sign,sign') =
wenzelm@3974
   678
  let val names = Sign.stamp_names_of sign' \\ Sign.stamp_names_of sign
wenzelm@3974
   679
  in  case names of
wenzelm@3974
   680
        [name] => "\nNew theory: " ^ name
wenzelm@3974
   681
      | _       => "\nNew theories: " ^ space_implode ", " names
clasohm@0
   682
  end;
clasohm@0
   683
paulson@1928
   684
(*Default action is to print an error message; could be suppressed for
paulson@1928
   685
  special applications.*)
wenzelm@3669
   686
fun result_error_default state msg : thm =
wenzelm@11884
   687
  Pretty.str "Bad final proof state:" :: Display.pretty_goals (!goals_limit) state @
wenzelm@11562
   688
    [Pretty.str msg, Pretty.str "Proof failed!"] |> Pretty.chunks |> Pretty.string_of |> error;
paulson@1928
   689
wenzelm@3532
   690
val result_error_fn = ref result_error_default;
paulson@1928
   691
wenzelm@5246
   692
(* alternative to standard: this function does not discharge the hypotheses
wenzelm@5246
   693
   first. Is used for locales, in the following function prepare_proof *)
wenzelm@5246
   694
fun varify th =
wenzelm@5246
   695
  let val {maxidx,...} = rep_thm th
wenzelm@5246
   696
  in
wenzelm@5246
   697
    th |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
wenzelm@7637
   698
       |> Drule.strip_shyps_warning
wenzelm@5246
   699
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
wenzelm@5246
   700
  end;
wenzelm@5246
   701
wenzelm@5246
   702
(** exporting a theorem out of a locale means turning all meta-hyps into assumptions
paulson@6017
   703
    and expand and cancel the locale definitions. 
paulson@6017
   704
    export goes through all levels in case of nested locales, whereas
wenzelm@6189
   705
    export_thy just goes one up. **)
paulson@6017
   706
paulson@6017
   707
fun get_top_scope_thms thy = 
wenzelm@12012
   708
   let val sc = (get_scope_sg (Theory.sign_of thy))
paulson@6017
   709
   in if (null sc) then (warning "No locale in theory"; [])
wenzelm@14643
   710
      else map Thm.prop_of (map #2 (#thms(snd(hd sc))))
paulson@6017
   711
   end;
paulson@6017
   712
paulson@6017
   713
fun implies_intr_some_hyps thy A_set th = 
paulson@6017
   714
   let 
wenzelm@6390
   715
       val sign = Theory.sign_of thy;
paulson@6017
   716
       val used_As = A_set inter #hyps(rep_thm(th));
paulson@6017
   717
       val ctl = map (cterm_of sign) used_As
paulson@6017
   718
   in foldl (fn (x, y) => Thm.implies_intr y x) (th, ctl)
paulson@6017
   719
   end; 
paulson@6017
   720
paulson@6017
   721
fun standard_some thy A_set th =
paulson@6017
   722
  let val {maxidx,...} = rep_thm th
paulson@6017
   723
  in
paulson@6017
   724
    th |> implies_intr_some_hyps thy A_set
paulson@6017
   725
       |> forall_intr_frees |> forall_elim_vars (maxidx + 1)
wenzelm@7637
   726
       |> Drule.strip_shyps_warning
paulson@6017
   727
       |> zero_var_indexes |> Thm.varifyT |> Thm.compress
paulson@6017
   728
  end;
paulson@6017
   729
paulson@6017
   730
fun export_thy thy th = 
paulson@6017
   731
  let val A_set = get_top_scope_thms thy
paulson@6017
   732
  in
paulson@6017
   733
     standard_some thy [] (Seq.hd ((REPEAT (FIRSTGOAL (rtac reflexive_thm))) (standard_some thy A_set th)))
paulson@6017
   734
  end;
paulson@6017
   735
wenzelm@5246
   736
val export = standard o Seq.hd o (REPEAT (FIRSTGOAL (rtac reflexive_thm))) o standard;
wenzelm@5246
   737
wenzelm@6189
   738
fun Export th = export_thy (the_context ()) th;
wenzelm@6189
   739
wenzelm@6189
   740
clasohm@0
   741
(*Common treatment of "goal" and "prove_goal":
nipkow@5041
   742
  Return assumptions, initial proof state, and function to make result.
nipkow@5041
   743
  "atomic" indicates if the goal should be wrapped up in the function
nipkow@5041
   744
  "Goal::prop=>prop" to avoid assumptions being returned separately.
nipkow@5041
   745
*)
nipkow@5041
   746
fun prepare_proof atomic rths chorn =
lcp@230
   747
  let val {sign, t=horn,...} = rep_cterm chorn;
wenzelm@9533
   748
      val _ = Term.no_dummy_patterns horn handle TERM (msg, _) => error msg;
berghofe@13660
   749
      val (As, B) = Logic.strip_horn horn;
nipkow@5041
   750
      val atoms = atomic andalso
nipkow@5041
   751
            forall (fn t => not(Logic.is_implies t orelse Logic.is_all t)) As;
nipkow@5041
   752
      val (As,B) = if atoms then ([],horn) else (As,B);
lcp@230
   753
      val cAs = map (cterm_of sign) As;
wenzelm@11963
   754
      val prems = map (rewrite_rule rths o forall_elim_vars 0 o Thm.assume) cAs;
nipkow@5041
   755
      val cB = cterm_of sign B;
wenzelm@11963
   756
      val st0 = let val st = Drule.impose_hyps cAs (Drule.mk_triv_goal cB)
nipkow@5041
   757
                in  rewrite_goals_rule rths st end
clasohm@0
   758
      (*discharges assumptions from state in the order they appear in goal;
clasohm@1460
   759
	checks (if requested) that resulting theorem is equivalent to goal. *)
clasohm@0
   760
      fun mkresult (check,state) =
wenzelm@4270
   761
        let val state = Seq.hd (flexflex_rule state)
paulson@1565
   762
	    		handle THM _ => state   (*smash flexflex pairs*)
clasohm@1460
   763
	    val ngoals = nprems_of state
wenzelm@7637
   764
            val ath = implies_intr_list cAs state
nipkow@5041
   765
            val th = ath RS Drule.rev_triv_goal
wenzelm@1240
   766
            val {hyps,prop,sign=sign',...} = rep_thm th
paulson@9573
   767
            val final_th = if (null(hyps)) then standard th else varify th
paulson@9573
   768
        in  if not check then final_th
wenzelm@3532
   769
	    else if not (Sign.eq_sg(sign,sign')) then !result_error_fn state
clasohm@1460
   770
		("Signature of proof state has changed!" ^
clasohm@1460
   771
		 sign_error (sign,sign'))
wenzelm@3532
   772
            else if ngoals>0 then !result_error_fn state
clasohm@1460
   773
		(string_of_int ngoals ^ " unsolved goals!")
wenzelm@12012
   774
            else if (not (null hyps) andalso (not (in_locale hyps sign)))
wenzelm@5246
   775
		 then !result_error_fn state
paulson@5748
   776
                  ("Additional hypotheses:\n" ^ 
paulson@5748
   777
		   cat_lines 
paulson@5748
   778
		    (map (Sign.string_of_term sign) 
wenzelm@12012
   779
		     (filter (fn x => (not (in_locale [x] sign))) 
paulson@5748
   780
		      hyps)))
wenzelm@14643
   781
	    else if Pattern.matches (Sign.tsig_of sign)
wenzelm@10487
   782
			            (Envir.beta_norm (term_of chorn), Envir.beta_norm prop)
paulson@9573
   783
		 then final_th
wenzelm@3532
   784
	    else  !result_error_fn state "proved a different theorem"
clasohm@0
   785
        end
nipkow@678
   786
  in
wenzelm@14643
   787
     if Sign.eq_sg(sign, Thm.sign_of_thm st0)
clasohm@0
   788
     then (prems, st0, mkresult)
clasohm@0
   789
     else error ("Definitions would change the proof state's signature" ^
wenzelm@14643
   790
		 sign_error (sign, Thm.sign_of_thm st0))
clasohm@0
   791
  end
clasohm@0
   792
  handle THM(s,_,_) => error("prepare_proof: exception THM was raised!\n" ^ s);
clasohm@0
   793
clasohm@0
   794
(*Prints exceptions readably to users*)
lcp@577
   795
fun print_sign_exn_unit sign e = 
clasohm@0
   796
  case e of
clasohm@0
   797
     THM (msg,i,thms) =>
clasohm@1460
   798
	 (writeln ("Exception THM " ^ string_of_int i ^ " raised:\n" ^ msg);
clasohm@1460
   799
	  seq print_thm thms)
clasohm@0
   800
   | THEORY (msg,thys) =>
clasohm@1460
   801
	 (writeln ("Exception THEORY raised:\n" ^ msg);
wenzelm@4994
   802
	  seq (Pretty.writeln o Display.pretty_theory) thys)
clasohm@0
   803
   | TERM (msg,ts) =>
clasohm@1460
   804
	 (writeln ("Exception TERM raised:\n" ^ msg);
clasohm@1460
   805
	  seq (writeln o Sign.string_of_term sign) ts)
clasohm@0
   806
   | TYPE (msg,Ts,ts) =>
clasohm@1460
   807
	 (writeln ("Exception TYPE raised:\n" ^ msg);
clasohm@1460
   808
	  seq (writeln o Sign.string_of_typ sign) Ts;
clasohm@1460
   809
	  seq (writeln o Sign.string_of_term sign) ts)
clasohm@0
   810
   | e => raise e;
clasohm@0
   811
lcp@577
   812
(*Prints an exception, then fails*)
lcp@577
   813
fun print_sign_exn sign e = (print_sign_exn_unit sign e; raise ERROR);
lcp@577
   814
clasohm@0
   815
(** the prove_goal.... commands
clasohm@0
   816
    Prove theorem using the listed tactics; check it has the specified form.
clasohm@0
   817
    Augment signature with all type assignments of goal.
clasohm@0
   818
    Syntax is similar to "goal" command for easy keyboard use. **)
clasohm@0
   819
clasohm@0
   820
(*Version taking the goal as a cterm*)
paulson@5311
   821
fun prove_goalw_cterm_general check rths chorn tacsf =
nipkow@5041
   822
  let val (prems, st0, mkresult) = prepare_proof false rths chorn
clasohm@0
   823
      val tac = EVERY (tacsf prems)
clasohm@0
   824
      fun statef() = 
wenzelm@4270
   825
	  (case Seq.pull (tac st0) of 
clasohm@1460
   826
	       Some(st,_) => st
clasohm@1460
   827
	     | _ => error ("prove_goal: tactic failed"))
wenzelm@14825
   828
  in  mkresult (check, cond_timeit (!Output.timing) statef)  end
lcp@914
   829
  handle e => (print_sign_exn_unit (#sign (rep_cterm chorn)) e;
wenzelm@6960
   830
	       writeln ("The exception above was raised for\n" ^ 
wenzelm@11884
   831
		      Display.string_of_cterm chorn); raise e);
lcp@545
   832
paulson@5614
   833
(*Two variants: one checking the result, one not.  
paulson@5614
   834
  Neither prints runtime messages: they are for internal packages.*)
paulson@5614
   835
fun prove_goalw_cterm rths chorn = 
wenzelm@14825
   836
	setmp Output.timing false (prove_goalw_cterm_general true rths chorn)
paulson@5614
   837
and prove_goalw_cterm_nocheck rths chorn = 
wenzelm@14825
   838
	setmp Output.timing false (prove_goalw_cterm_general false rths chorn);
paulson@5311
   839
clasohm@0
   840
clasohm@0
   841
(*Version taking the goal as a string*)
clasohm@0
   842
fun prove_goalw thy rths agoal tacsf =
wenzelm@6390
   843
  let val sign = Theory.sign_of thy
lcp@230
   844
      val chorn = read_cterm sign (agoal,propT)
paulson@5614
   845
  in  prove_goalw_cterm_general true rths chorn tacsf end
lcp@545
   846
  handle ERROR => error (*from read_cterm?*)
wenzelm@3536
   847
		("The error(s) above occurred for " ^ quote agoal);
clasohm@0
   848
clasohm@0
   849
(*String version with no meta-rewrite-rules*)
clasohm@0
   850
fun prove_goal thy = prove_goalw thy [];
clasohm@0
   851
wenzelm@11884
   852
(*quick and dirty version (conditional)*)
wenzelm@11884
   853
fun quick_and_dirty_prove_goalw_cterm thy rews ct tacs =
wenzelm@11884
   854
  prove_goalw_cterm rews ct
wenzelm@11884
   855
    (if ! quick_and_dirty then (K [SkipProof.cheat_tac thy]) else tacs);
wenzelm@11884
   856
clasohm@0
   857
clasohm@0
   858
(*** Commands etc ***)
clasohm@0
   859
clasohm@0
   860
(*Return the current goal stack, if any, from undo_list*)
clasohm@0
   861
fun getstate() : gstack = case !undo_list of 
clasohm@0
   862
      []   => error"No current state in subgoal module"
clasohm@0
   863
    | x::_ => x;
clasohm@0
   864
clasohm@0
   865
(*Pops the given goal stack*)
clasohm@0
   866
fun pop [] = error"Cannot go back past the beginning of the proof!"
clasohm@0
   867
  | pop (pair::pairs) = (pair,pairs);
clasohm@0
   868
clasohm@0
   869
wenzelm@8884
   870
(* Print a level of the goal stack -- subject to quiet mode *)
wenzelm@8884
   871
wenzelm@8884
   872
val quiet = ref false;
wenzelm@8884
   873
fun disable_pr () = quiet := true;
wenzelm@8884
   874
fun enable_pr () = quiet := false;
wenzelm@8884
   875
wenzelm@3532
   876
fun print_top ((th, _), pairs) =
wenzelm@8884
   877
  if ! quiet then ()
wenzelm@11884
   878
  else ! Display.print_current_goals_fn (length pairs) (! goals_limit) th;
clasohm@0
   879
clasohm@0
   880
(*Printing can raise exceptions, so the assignment occurs last.
wenzelm@4270
   881
  Can do   setstate[(st,Seq.empty)]  to set st as the state.  *)
clasohm@0
   882
fun setstate newgoals = 
clasohm@0
   883
  (print_top (pop newgoals);  undo_list := newgoals :: !undo_list);
clasohm@0
   884
clasohm@0
   885
(*Given a proof state transformation, return a command that updates
clasohm@0
   886
    the goal stack*)
clasohm@0
   887
fun make_command com = setstate (com (pop (getstate())));
clasohm@0
   888
clasohm@0
   889
(*Apply a function on proof states to the current goal stack*)
clasohm@0
   890
fun apply_fun f = f (pop(getstate()));
clasohm@0
   891
clasohm@0
   892
(*Return the top theorem, representing the proof state*)
clasohm@0
   893
fun topthm () = apply_fun  (fn ((th,_), _) => th);
clasohm@0
   894
clasohm@0
   895
(*Return the final result.  *)
clasohm@0
   896
fun result () = !curr_mkresult (true, topthm());
clasohm@0
   897
clasohm@0
   898
(*Return the result UNCHECKED that it equals the goal -- for synthesis,
clasohm@0
   899
  answer extraction, or other instantiation of Vars *)
clasohm@0
   900
fun uresult () = !curr_mkresult (false, topthm());
clasohm@0
   901
clasohm@0
   902
(*Get subgoal i from goal stack*)
berghofe@13799
   903
fun getgoal i = Logic.get_goal (prop_of (topthm())) i;
nipkow@13646
   904
clasohm@0
   905
(*Return subgoal i's hypotheses as meta-level assumptions.
clasohm@0
   906
  For debugging uses of METAHYPS*)
clasohm@0
   907
local exception GETHYPS of thm list
clasohm@0
   908
in
clasohm@0
   909
fun gethyps i = 
paulson@1500
   910
    (METAHYPS (fn hyps => raise (GETHYPS hyps)) i (topthm());  [])
clasohm@0
   911
    handle GETHYPS hyps => hyps
clasohm@0
   912
end;
clasohm@0
   913
clasohm@0
   914
(*Which thms could apply to goal i? (debugs tactics involving filter_thms) *)
clasohm@0
   915
fun filter_goal could ths i = filter_thms could (999, getgoal i, ths);
clasohm@0
   916
clasohm@0
   917
(*For inspecting earlier levels of the backward proof*)
clasohm@0
   918
fun chop_level n (pair,pairs) = 
clasohm@0
   919
  let val level = length pairs
paulson@2126
   920
  in  if n<0 andalso ~n <= level
paulson@2580
   921
      then  List.drop (pair::pairs, ~n) 
paulson@2126
   922
      else if 0<=n andalso n<= level
paulson@2580
   923
      then  List.drop (pair::pairs, level - n) 
clasohm@0
   924
      else  error ("Level number must lie between 0 and " ^ 
clasohm@1460
   925
		   string_of_int level)
clasohm@0
   926
  end;
clasohm@0
   927
paulson@2514
   928
(*Print the given level of the proof; prlev ~1 prints previous level*)
wenzelm@8884
   929
fun prlev n = (enable_pr (); apply_fun (print_top o pop o (chop_level n)));
wenzelm@8884
   930
fun pr () = (enable_pr (); apply_fun print_top);
clasohm@0
   931
paulson@2514
   932
(*Set goals_limit and print again*)
paulson@2514
   933
fun prlim n = (goals_limit:=n; pr());
paulson@2514
   934
clasohm@0
   935
(** the goal.... commands
clasohm@0
   936
    Read main goal.  Set global variables curr_prems, curr_mkresult. 
clasohm@0
   937
    Initial subgoal and premises are rewritten using rths. **)
clasohm@0
   938
clasohm@0
   939
(*Version taking the goal as a cterm; if you have a term t and theory thy, use
wenzelm@6390
   940
    goalw_cterm rths (cterm_of (Theory.sign_of thy) t);      *)
nipkow@5041
   941
fun agoalw_cterm atomic rths chorn = 
nipkow@5041
   942
  let val (prems, st0, mkresult) = prepare_proof atomic rths chorn
clasohm@0
   943
  in  undo_list := [];
wenzelm@4270
   944
      setstate [ (st0, Seq.empty) ];  
clasohm@0
   945
      curr_prems := prems;
clasohm@0
   946
      curr_mkresult := mkresult;
clasohm@0
   947
      prems
clasohm@0
   948
  end;
clasohm@0
   949
nipkow@5041
   950
val goalw_cterm = agoalw_cterm false;
nipkow@5041
   951
clasohm@0
   952
(*Version taking the goal as a string*)
nipkow@5041
   953
fun agoalw atomic thy rths agoal = 
wenzelm@12012
   954
    agoalw_cterm atomic rths (read_cterm(Theory.sign_of thy)(agoal,propT))
wenzelm@5246
   955
    handle ERROR => error (*from type_assign, etc via prepare_proof*)
wenzelm@5246
   956
	("The error(s) above occurred for " ^ quote agoal);
clasohm@0
   957
nipkow@5041
   958
val goalw = agoalw false;
nipkow@5041
   959
clasohm@0
   960
(*String version with no meta-rewrite-rules*)
nipkow@5041
   961
fun agoal atomic thy = agoalw atomic thy [];
nipkow@5041
   962
val goal = agoal false;
nipkow@5041
   963
nipkow@5041
   964
(*now the versions that wrap the goal up in `Goal' to make it atomic*)
nipkow@5041
   965
val atomic_goalw = agoalw true;
nipkow@5041
   966
val atomic_goal = agoal true;
nipkow@5041
   967
wenzelm@6189
   968
(*implicit context versions*)
wenzelm@6189
   969
fun Goal s = atomic_goal (Context.the_context ()) s;
wenzelm@6189
   970
fun Goalw thms s = atomic_goalw (Context.the_context ()) thms s;
wenzelm@6189
   971
berghofe@13712
   972
(*simple version with minimal amount of checking and postprocessing*)
berghofe@13712
   973
fun simple_prove_goal_cterm G f =
berghofe@13712
   974
  let
berghofe@13712
   975
    val As = Drule.strip_imp_prems G;
berghofe@13712
   976
    val B = Drule.strip_imp_concl G;
berghofe@13712
   977
    val asms = map (norm_hhf_rule o assume) As;
berghofe@13712
   978
    fun check None = error "prove_goal: tactic failed"
berghofe@13712
   979
      | check (Some (thm, _)) = (case nprems_of thm of
berghofe@13712
   980
            0 => thm
berghofe@13712
   981
          | i => !result_error_fn thm (string_of_int i ^ " unsolved goals!"))
berghofe@13712
   982
  in
berghofe@13712
   983
    standard (implies_intr_list As
berghofe@13712
   984
      (check (Seq.pull (EVERY (f asms) (trivial B)))))
berghofe@13712
   985
  end;
berghofe@13712
   986
clasohm@0
   987
clasohm@0
   988
(*Proof step "by" the given tactic -- apply tactic to the proof state*)
clasohm@0
   989
fun by_com tac ((th,ths), pairs) : gstack =
wenzelm@4270
   990
  (case  Seq.pull(tac th)  of
clasohm@0
   991
     None      => error"by: tactic failed"
clasohm@0
   992
   | Some(th2,ths2) => 
clasohm@0
   993
       (if eq_thm(th,th2) 
paulson@3707
   994
	  then warning "Warning: same as previous level"
clasohm@1460
   995
	  else if eq_thm_sg(th,th2) then ()
wenzelm@4280
   996
	  else warning ("Warning: signature of proof state has changed" ^
wenzelm@14643
   997
		       sign_error (Thm.sign_of_thm th, Thm.sign_of_thm th2));
clasohm@0
   998
       ((th2,ths2)::(th,ths)::pairs)));
clasohm@0
   999
wenzelm@14825
  1000
fun by tac = cond_timeit (!Output.timing) 
clasohm@0
  1001
    (fn() => make_command (by_com tac));
clasohm@0
  1002
clasohm@0
  1003
(* byev[tac1,...,tacn] applies tac1 THEN ... THEN tacn.
clasohm@0
  1004
   Good for debugging proofs involving prove_goal.*)
clasohm@0
  1005
val byev = by o EVERY;
clasohm@0
  1006
clasohm@0
  1007
clasohm@0
  1008
(*Backtracking means find an alternative result from a tactic.
clasohm@0
  1009
  If none at this level, try earlier levels*)
clasohm@0
  1010
fun backtrack [] = error"back: no alternatives"
clasohm@0
  1011
  | backtrack ((th,thstr) :: pairs) =
wenzelm@4270
  1012
     (case Seq.pull thstr of
clasohm@1460
  1013
	  None      => (writeln"Going back a level..."; backtrack pairs)
clasohm@1460
  1014
	| Some(th2,thstr2) =>  
clasohm@1460
  1015
	   (if eq_thm(th,th2) 
paulson@3707
  1016
	      then warning "Warning: same as previous choice at this level"
clasohm@1460
  1017
	      else if eq_thm_sg(th,th2) then ()
paulson@3707
  1018
	      else warning "Warning: signature of proof state has changed";
clasohm@1460
  1019
	    (th2,thstr2)::pairs));
clasohm@0
  1020
clasohm@0
  1021
fun back() = setstate (backtrack (getstate()));
clasohm@0
  1022
clasohm@0
  1023
(*Chop back to previous level of the proof*)
clasohm@0
  1024
fun choplev n = make_command (chop_level n);
clasohm@0
  1025
clasohm@0
  1026
(*Chopping back the goal stack*)
clasohm@0
  1027
fun chop () = make_command (fn (_,pairs) => pairs);
clasohm@0
  1028
clasohm@0
  1029
(*Restore the previous proof state;  discard current state. *)
clasohm@0
  1030
fun undo() = case !undo_list of
clasohm@0
  1031
      [] => error"No proof state"
clasohm@0
  1032
    | [_] => error"Already at initial state"
clasohm@0
  1033
    | _::newundo =>  (undo_list := newundo;  pr()) ;
clasohm@0
  1034
clasohm@0
  1035
clasohm@0
  1036
(*** Managing the proof stack ***)
clasohm@0
  1037
clasohm@0
  1038
fun save_proof() = Proof(!undo_list, !curr_prems, !curr_mkresult);
clasohm@0
  1039
clasohm@0
  1040
fun restore_proof(Proof(ul,prems,mk)) =
clasohm@0
  1041
 (undo_list:= ul;  curr_prems:= prems;  curr_mkresult := mk;  prems);
clasohm@0
  1042
clasohm@0
  1043
clasohm@0
  1044
fun top_proof() = case !proofstack of
clasohm@1460
  1045
	[] => error("Stack of proof attempts is empty!")
clasohm@0
  1046
    | p::ps => (p,ps);
clasohm@0
  1047
clasohm@0
  1048
(*  push a copy of the current proof state on to the stack *)
clasohm@0
  1049
fun push_proof() = (proofstack := (save_proof() :: !proofstack));
clasohm@0
  1050
clasohm@0
  1051
(* discard the top proof state of the stack *)
clasohm@0
  1052
fun pop_proof() = 
clasohm@0
  1053
  let val (p,ps) = top_proof()
clasohm@0
  1054
      val prems = restore_proof p
clasohm@0
  1055
  in proofstack := ps;  pr();  prems end;
clasohm@0
  1056
clasohm@0
  1057
(* rotate the stack so that the top element goes to the bottom *)
clasohm@0
  1058
fun rotate_proof() = let val (p,ps) = top_proof()
clasohm@1460
  1059
		    in proofstack := ps@[save_proof()];
clasohm@1460
  1060
		       restore_proof p;
clasohm@1460
  1061
		       pr();
clasohm@1460
  1062
		       !curr_prems
clasohm@1460
  1063
		    end;
clasohm@0
  1064
clasohm@0
  1065
clasohm@0
  1066
(** Shortcuts for commonly-used tactics **)
clasohm@0
  1067
clasohm@0
  1068
fun bws rls = by (rewrite_goals_tac rls);
clasohm@0
  1069
fun bw rl = bws [rl];
clasohm@0
  1070
clasohm@0
  1071
fun brs rls i = by (resolve_tac rls i);
clasohm@0
  1072
fun br rl = brs [rl];
clasohm@0
  1073
clasohm@0
  1074
fun bes rls i = by (eresolve_tac rls i);
clasohm@0
  1075
fun be rl = bes [rl];
clasohm@0
  1076
clasohm@0
  1077
fun bds rls i = by (dresolve_tac rls i);
clasohm@0
  1078
fun bd rl = bds [rl];
clasohm@0
  1079
clasohm@0
  1080
fun ba i = by (assume_tac i);
clasohm@0
  1081
clasohm@0
  1082
fun ren str i = by (rename_tac str i);
clasohm@0
  1083
clasohm@0
  1084
(** Shortcuts to work on the first applicable subgoal **)
clasohm@0
  1085
clasohm@0
  1086
fun frs rls = by (FIRSTGOAL (trace_goalno_tac (resolve_tac rls)));
clasohm@0
  1087
fun fr rl = frs [rl];
clasohm@0
  1088
clasohm@0
  1089
fun fes rls = by (FIRSTGOAL (trace_goalno_tac (eresolve_tac rls)));
clasohm@0
  1090
fun fe rl = fes [rl];
clasohm@0
  1091
clasohm@0
  1092
fun fds rls = by (FIRSTGOAL (trace_goalno_tac (dresolve_tac rls)));
clasohm@0
  1093
fun fd rl = fds [rl];
clasohm@0
  1094
clasohm@0
  1095
fun fa() = by (FIRSTGOAL (trace_goalno_tac assume_tac));
clasohm@0
  1096
clasohm@0
  1097
(** Reading and printing terms wrt the current theory **)
clasohm@0
  1098
wenzelm@14643
  1099
fun top_sg() = Thm.sign_of_thm (topthm());
clasohm@0
  1100
wenzelm@8086
  1101
fun read s = term_of (read_cterm (top_sg()) (s, TypeInfer.logicT));
clasohm@0
  1102
clasohm@0
  1103
(*Print a term under the current signature of the proof state*)
clasohm@0
  1104
fun prin t = writeln (Sign.string_of_term (top_sg()) t);
clasohm@0
  1105
clasohm@0
  1106
fun printyp T = writeln (Sign.string_of_typ (top_sg()) T);
clasohm@0
  1107
clasohm@0
  1108
fun pprint_term t = Sign.pprint_term (top_sg()) t;
clasohm@0
  1109
clasohm@0
  1110
fun pprint_typ T = Sign.pprint_typ (top_sg()) T;
clasohm@0
  1111
berghofe@1628
  1112
clasohm@0
  1113
(*Prints exceptions nicely at top level; 
clasohm@0
  1114
  raises the exception in order to have a polymorphic type!*)
lcp@914
  1115
fun print_exn e = (print_sign_exn_unit (top_sg()) e;  raise e);
clasohm@0
  1116
wenzelm@11884
  1117
wenzelm@11884
  1118
wenzelm@11884
  1119
(** theorem database operations **)
wenzelm@11884
  1120
wenzelm@11884
  1121
(* storing *)
wenzelm@11884
  1122
wenzelm@11884
  1123
fun bind_thm (name, thm) = ThmDatabase.ml_store_thm (name, standard thm);
wenzelm@11884
  1124
fun bind_thms (name, thms) = ThmDatabase.ml_store_thms (name, map standard thms);
wenzelm@11884
  1125
wenzelm@11884
  1126
fun qed name = ThmDatabase.ml_store_thm (name, result ());
wenzelm@11884
  1127
fun qed_goal name thy goal tacsf = ThmDatabase.ml_store_thm (name, prove_goal thy goal tacsf);
wenzelm@11884
  1128
fun qed_goalw name thy rews goal tacsf =
wenzelm@11884
  1129
  ThmDatabase.ml_store_thm (name, prove_goalw thy rews goal tacsf);
wenzelm@11884
  1130
fun qed_spec_mp name =
wenzelm@11884
  1131
  ThmDatabase.ml_store_thm (name, ObjectLogic.rulify_no_asm (result ()));
wenzelm@11884
  1132
fun qed_goal_spec_mp name thy s p =
wenzelm@11884
  1133
  bind_thm (name, ObjectLogic.rulify_no_asm (prove_goal thy s p));
wenzelm@11884
  1134
fun qed_goalw_spec_mp name thy defs s p =
wenzelm@11884
  1135
  bind_thm (name, ObjectLogic.rulify_no_asm (prove_goalw thy defs s p));
wenzelm@11884
  1136
wenzelm@11884
  1137
fun no_qed () = ();
wenzelm@11884
  1138
wenzelm@11884
  1139
wenzelm@11884
  1140
(* retrieval *)
wenzelm@11884
  1141
wenzelm@11884
  1142
fun theory_of_goal () = ThyInfo.theory_of_sign (Thm.sign_of_thm (topthm ()));
wenzelm@11884
  1143
wenzelm@11884
  1144
fun thms_containing raw_consts =
wenzelm@11884
  1145
  let
wenzelm@11884
  1146
    val thy = theory_of_goal ();
wenzelm@11884
  1147
    val consts = map (Sign.intern_const (Theory.sign_of thy)) raw_consts;
wenzelm@13272
  1148
  in
wenzelm@13272
  1149
    (case filter (is_none o Sign.const_type (Theory.sign_of thy)) consts of
wenzelm@13272
  1150
      [] => ()
wenzelm@13272
  1151
    | cs => error ("thms_containing: undeclared consts " ^ commas_quote cs));
nipkow@13646
  1152
    PureThy.thms_containing_consts thy consts
wenzelm@13272
  1153
  end;
wenzelm@11884
  1154
berghofe@13799
  1155
fun findI i = PureThy.find_intros_goal (theory_of_goal()) (topthm()) i;
wenzelm@11884
  1156
wenzelm@11884
  1157
fun findEs i =
wenzelm@11884
  1158
  let fun eq_nth((n1,th1),(n2,th2)) = n1=n2 andalso eq_thm(th1,th2);
berghofe@13799
  1159
      val prems = Logic.prems_of_goal (prop_of (topthm())) i
nipkow@13646
  1160
      val thy = theory_of_goal();
nipkow@13646
  1161
      val thmss = map (PureThy.find_elims thy) prems
wenzelm@11884
  1162
  in foldl (gen_union eq_nth) ([],thmss) end;
wenzelm@11884
  1163
wenzelm@11884
  1164
fun findE i j =
berghofe@13799
  1165
  let val prems = Logic.prems_of_goal (prop_of (topthm())) i
nipkow@13646
  1166
      val thy = theory_of_goal();
nipkow@13646
  1167
  in PureThy.find_elims thy (nth_elem(j-1, prems)) end;
wenzelm@11884
  1168
clasohm@0
  1169
end;
paulson@1500
  1170
wenzelm@12012
  1171
structure BasicGoals: BASIC_GOALS = Goals;
wenzelm@12012
  1172
open BasicGoals;