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