src/Pure/thm.ML
author blanchet
Mon May 19 23:43:53 2014 +0200 (2014-05-19)
changeset 57008 10f68b83b474
parent 56245 84fc7dfa3cd4
child 58837 e84d900cd287
permissions -rw-r--r--
use E 1.8's auto scheduler option
wenzelm@250
     1
(*  Title:      Pure/thm.ML
wenzelm@250
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@29269
     3
    Author:     Makarius
lcp@229
     4
wenzelm@16425
     5
The very core of Isabelle's Meta Logic: certified types and terms,
wenzelm@28321
     6
derivations, theorems, framework rules (including lifting and
wenzelm@28321
     7
resolution), oracles.
clasohm@0
     8
*)
clasohm@0
     9
wenzelm@6089
    10
signature BASIC_THM =
paulson@1503
    11
  sig
wenzelm@1160
    12
  (*certified types*)
wenzelm@387
    13
  type ctyp
wenzelm@52788
    14
  val rep_ctyp: ctyp -> {thy: theory, T: typ, maxidx: int, sorts: sort Ord_List.T}
wenzelm@16425
    15
  val theory_of_ctyp: ctyp -> theory
wenzelm@16425
    16
  val typ_of: ctyp -> typ
wenzelm@16425
    17
  val ctyp_of: theory -> typ -> ctyp
wenzelm@1160
    18
wenzelm@1160
    19
  (*certified terms*)
wenzelm@1160
    20
  type cterm
wenzelm@22584
    21
  exception CTERM of string * cterm list
wenzelm@52788
    22
  val rep_cterm: cterm -> {thy: theory, t: term, T: typ, maxidx: int, sorts: sort Ord_List.T}
wenzelm@52788
    23
  val crep_cterm: cterm -> {thy: theory, t: term, T: ctyp, maxidx: int, sorts: sort Ord_List.T}
wenzelm@16425
    24
  val theory_of_cterm: cterm -> theory
wenzelm@16425
    25
  val term_of: cterm -> term
wenzelm@16425
    26
  val cterm_of: theory -> term -> cterm
wenzelm@16425
    27
  val ctyp_of_term: cterm -> ctyp
wenzelm@1160
    28
wenzelm@28321
    29
  (*theorems*)
wenzelm@1160
    30
  type thm
wenzelm@23601
    31
  type conv = cterm -> thm
wenzelm@16425
    32
  val rep_thm: thm ->
wenzelm@52788
    33
   {thy: theory,
wenzelm@28017
    34
    tags: Properties.T,
wenzelm@16425
    35
    maxidx: int,
wenzelm@39687
    36
    shyps: sort Ord_List.T,
wenzelm@39687
    37
    hyps: term Ord_List.T,
wenzelm@16425
    38
    tpairs: (term * term) list,
wenzelm@16425
    39
    prop: term}
wenzelm@16425
    40
  val crep_thm: thm ->
wenzelm@52788
    41
   {thy: theory,
wenzelm@28017
    42
    tags: Properties.T,
wenzelm@16425
    43
    maxidx: int,
wenzelm@39687
    44
    shyps: sort Ord_List.T,
wenzelm@39687
    45
    hyps: cterm Ord_List.T,
wenzelm@16425
    46
    tpairs: (cterm * cterm) list,
wenzelm@16425
    47
    prop: cterm}
wenzelm@6089
    48
  exception THM of string * int * thm list
wenzelm@16425
    49
  val theory_of_thm: thm -> theory
wenzelm@16425
    50
  val prop_of: thm -> term
wenzelm@16656
    51
  val concl_of: thm -> term
wenzelm@16425
    52
  val prems_of: thm -> term list
wenzelm@16425
    53
  val nprems_of: thm -> int
wenzelm@16425
    54
  val cprop_of: thm -> cterm
wenzelm@18145
    55
  val cprem_of: thm -> int -> cterm
wenzelm@250
    56
end;
clasohm@0
    57
wenzelm@6089
    58
signature THM =
wenzelm@6089
    59
sig
wenzelm@6089
    60
  include BASIC_THM
wenzelm@16425
    61
  val dest_ctyp: ctyp -> ctyp list
wenzelm@16425
    62
  val dest_comb: cterm -> cterm * cterm
wenzelm@22909
    63
  val dest_fun: cterm -> cterm
wenzelm@20580
    64
  val dest_arg: cterm -> cterm
wenzelm@22909
    65
  val dest_fun2: cterm -> cterm
wenzelm@22909
    66
  val dest_arg1: cterm -> cterm
wenzelm@16425
    67
  val dest_abs: string option -> cterm -> cterm * cterm
wenzelm@46497
    68
  val apply: cterm -> cterm -> cterm
wenzelm@46497
    69
  val lambda_name: string * cterm -> cterm -> cterm
wenzelm@46497
    70
  val lambda: cterm -> cterm -> cterm
wenzelm@31945
    71
  val adjust_maxidx_cterm: int -> cterm -> cterm
wenzelm@31945
    72
  val incr_indexes_cterm: int -> cterm -> cterm
wenzelm@31945
    73
  val match: cterm * cterm -> (ctyp * ctyp) list * (cterm * cterm) list
wenzelm@31945
    74
  val first_order_match: cterm * cterm -> (ctyp * ctyp) list * (cterm * cterm) list
wenzelm@31947
    75
  val fold_terms: (term -> 'a -> 'a) -> thm -> 'a -> 'a
wenzelm@16945
    76
  val terms_of_tpairs: (term * term) list -> term list
wenzelm@31945
    77
  val full_prop_of: thm -> term
wenzelm@19881
    78
  val maxidx_of: thm -> int
wenzelm@19910
    79
  val maxidx_thm: thm -> int -> int
wenzelm@19881
    80
  val hyps_of: thm -> term list
wenzelm@38709
    81
  val tpairs_of: thm -> (term * term) list
wenzelm@31945
    82
  val no_prems: thm -> bool
wenzelm@31945
    83
  val major_prem_of: thm -> term
wenzelm@38709
    84
  val transfer: theory -> thm -> thm
wenzelm@38709
    85
  val weaken: cterm -> thm -> thm
wenzelm@38709
    86
  val weaken_sorts: sort list -> cterm -> cterm
wenzelm@38709
    87
  val extra_shyps: thm -> sort list
wenzelm@44333
    88
  val proof_bodies_of: thm list -> proof_body list
wenzelm@32725
    89
  val proof_body_of: thm -> proof_body
wenzelm@32725
    90
  val proof_of: thm -> proof
wenzelm@44331
    91
  val join_proofs: thm list -> unit
wenzelm@50126
    92
  val peek_status: thm -> {oracle: bool, unfinished: bool, failed: bool}
wenzelm@32725
    93
  val future: thm future -> cterm -> thm
wenzelm@36744
    94
  val derivation_name: thm -> string
wenzelm@36744
    95
  val name_derivation: string -> thm -> thm
wenzelm@28675
    96
  val axiom: theory -> string -> thm
wenzelm@28675
    97
  val axioms_of: theory -> (string * thm) list
wenzelm@28017
    98
  val get_tags: thm -> Properties.T
wenzelm@28017
    99
  val map_tags: (Properties.T -> Properties.T) -> thm -> thm
berghofe@23781
   100
  val norm_proof: thm -> thm
wenzelm@20261
   101
  val adjust_maxidx_thm: int -> thm -> thm
wenzelm@38709
   102
  (*meta rules*)
wenzelm@38709
   103
  val assume: cterm -> thm
wenzelm@38709
   104
  val implies_intr: cterm -> thm -> thm
wenzelm@38709
   105
  val implies_elim: thm -> thm -> thm
wenzelm@38709
   106
  val forall_intr: cterm -> thm -> thm
wenzelm@38709
   107
  val forall_elim: cterm -> thm -> thm
wenzelm@38709
   108
  val reflexive: cterm -> thm
wenzelm@38709
   109
  val symmetric: thm -> thm
wenzelm@38709
   110
  val transitive: thm -> thm -> thm
wenzelm@38709
   111
  val beta_conversion: bool -> conv
wenzelm@38709
   112
  val eta_conversion: conv
wenzelm@38709
   113
  val eta_long_conversion: conv
wenzelm@38709
   114
  val abstract_rule: string -> cterm -> thm -> thm
wenzelm@38709
   115
  val combination: thm -> thm -> thm
wenzelm@38709
   116
  val equal_intr: thm -> thm -> thm
wenzelm@38709
   117
  val equal_elim: thm -> thm -> thm
wenzelm@38709
   118
  val flexflex_rule: thm -> thm Seq.seq
wenzelm@38709
   119
  val generalize: string list * string list -> int -> thm -> thm
wenzelm@38709
   120
  val instantiate: (ctyp * ctyp) list * (cterm * cterm) list -> thm -> thm
wenzelm@38709
   121
  val instantiate_cterm: (ctyp * ctyp) list * (cterm * cterm) list -> cterm -> cterm
wenzelm@38709
   122
  val trivial: cterm -> thm
wenzelm@36330
   123
  val of_class: ctyp * class -> thm
wenzelm@36614
   124
  val strip_shyps: thm -> thm
wenzelm@36768
   125
  val unconstrainT: thm -> thm
wenzelm@38709
   126
  val varifyT_global': (string * sort) list -> thm -> ((string * sort) * indexname) list * thm
wenzelm@38709
   127
  val varifyT_global: thm -> thm
wenzelm@36615
   128
  val legacy_freezeT: thm -> thm
wenzelm@38709
   129
  val lift_rule: cterm -> thm -> thm
wenzelm@38709
   130
  val incr_indexes: int -> thm -> thm
wenzelm@31945
   131
  val assumption: int -> thm -> thm Seq.seq
wenzelm@31945
   132
  val eq_assumption: int -> thm -> thm
wenzelm@31945
   133
  val rotate_rule: int -> int -> thm -> thm
wenzelm@31945
   134
  val permute_prems: int -> int -> thm -> thm
wenzelm@31945
   135
  val rename_params_rule: string list * int -> thm -> thm
wenzelm@38709
   136
  val rename_boundvars: term -> term -> thm -> thm
wenzelm@52223
   137
  val bicompose: {flatten: bool, match: bool, incremented: bool} ->
wenzelm@52223
   138
    bool * thm * int -> int -> thm -> thm Seq.seq
wenzelm@31945
   139
  val biresolution: bool -> (bool * thm) list -> int -> thm -> thm Seq.seq
wenzelm@50301
   140
  val extern_oracles: Proof.context -> (Markup.T * xstring) list
wenzelm@30288
   141
  val add_oracle: binding * ('a -> cterm) -> theory -> (string * ('a -> thm)) * theory
wenzelm@6089
   142
end;
wenzelm@6089
   143
wenzelm@32590
   144
structure Thm: THM =
clasohm@0
   145
struct
wenzelm@250
   146
wenzelm@387
   147
(*** Certified terms and types ***)
wenzelm@387
   148
wenzelm@250
   149
(** certified types **)
wenzelm@250
   150
wenzelm@52788
   151
abstype ctyp = Ctyp of {thy: theory, T: typ, maxidx: int, sorts: sort Ord_List.T}
wenzelm@32590
   152
with
wenzelm@250
   153
wenzelm@26631
   154
fun rep_ctyp (Ctyp args) = args;
wenzelm@52788
   155
fun theory_of_ctyp (Ctyp {thy, ...}) = thy;
wenzelm@250
   156
fun typ_of (Ctyp {T, ...}) = T;
wenzelm@250
   157
wenzelm@16656
   158
fun ctyp_of thy raw_T =
wenzelm@24143
   159
  let
wenzelm@24143
   160
    val T = Sign.certify_typ thy raw_T;
wenzelm@24143
   161
    val maxidx = Term.maxidx_of_typ T;
wenzelm@26640
   162
    val sorts = Sorts.insert_typ T [];
wenzelm@52788
   163
  in Ctyp {thy = thy, T = T, maxidx = maxidx, sorts = sorts} end;
wenzelm@250
   164
wenzelm@52788
   165
fun dest_ctyp (Ctyp {thy, T = Type (_, Ts), maxidx, sorts}) =
wenzelm@52788
   166
      map (fn T => Ctyp {thy = thy, T = T, maxidx = maxidx, sorts = sorts}) Ts
wenzelm@16679
   167
  | dest_ctyp cT = raise TYPE ("dest_ctyp", [typ_of cT], []);
berghofe@15087
   168
lcp@229
   169
lcp@229
   170
wenzelm@250
   171
(** certified terms **)
lcp@229
   172
wenzelm@16601
   173
(*certified terms with checked typ, maxidx, and sorts*)
wenzelm@52788
   174
abstype cterm = Cterm of {thy: theory, t: term, T: typ, maxidx: int, sorts: sort Ord_List.T}
wenzelm@32590
   175
with
wenzelm@16425
   176
wenzelm@22584
   177
exception CTERM of string * cterm list;
wenzelm@16679
   178
wenzelm@26631
   179
fun rep_cterm (Cterm args) = args;
lcp@229
   180
wenzelm@52788
   181
fun crep_cterm (Cterm {thy, t, T, maxidx, sorts}) =
wenzelm@52788
   182
  {thy = thy, t = t, maxidx = maxidx, sorts = sorts,
wenzelm@52788
   183
    T = Ctyp {thy = thy, T = T, maxidx = maxidx, sorts = sorts}};
wenzelm@3967
   184
wenzelm@52788
   185
fun theory_of_cterm (Cterm {thy, ...}) = thy;
wenzelm@250
   186
fun term_of (Cterm {t, ...}) = t;
lcp@229
   187
wenzelm@52788
   188
fun ctyp_of_term (Cterm {thy, T, maxidx, sorts, ...}) =
wenzelm@52788
   189
  Ctyp {thy = thy, T = T, maxidx = maxidx, sorts = sorts};
paulson@2671
   190
wenzelm@16425
   191
fun cterm_of thy tm =
wenzelm@16601
   192
  let
wenzelm@18969
   193
    val (t, T, maxidx) = Sign.certify_term thy tm;
wenzelm@26640
   194
    val sorts = Sorts.insert_term t [];
wenzelm@52788
   195
  in Cterm {thy = thy, t = t, T = T, maxidx = maxidx, sorts = sorts} end;
lcp@229
   196
wenzelm@52788
   197
fun merge_thys0 (Cterm {thy = thy1, ...}) (Cterm {thy = thy2, ...}) =
wenzelm@52788
   198
  Theory.merge (thy1, thy2);
wenzelm@16656
   199
wenzelm@20580
   200
wenzelm@22909
   201
(* destructors *)
wenzelm@22909
   202
wenzelm@52788
   203
fun dest_comb (Cterm {t = c $ a, T, thy, maxidx, sorts}) =
wenzelm@22909
   204
      let val A = Term.argument_type_of c 0 in
wenzelm@52788
   205
        (Cterm {t = c, T = A --> T, thy = thy, maxidx = maxidx, sorts = sorts},
wenzelm@52788
   206
         Cterm {t = a, T = A, thy = thy, maxidx = maxidx, sorts = sorts})
clasohm@1493
   207
      end
wenzelm@22584
   208
  | dest_comb ct = raise CTERM ("dest_comb", [ct]);
clasohm@1493
   209
wenzelm@52788
   210
fun dest_fun (Cterm {t = c $ _, T, thy, maxidx, sorts}) =
wenzelm@22909
   211
      let val A = Term.argument_type_of c 0
wenzelm@52788
   212
      in Cterm {t = c, T = A --> T, thy = thy, maxidx = maxidx, sorts = sorts} end
wenzelm@22909
   213
  | dest_fun ct = raise CTERM ("dest_fun", [ct]);
wenzelm@22909
   214
wenzelm@52788
   215
fun dest_arg (Cterm {t = c $ a, T = _, thy, maxidx, sorts}) =
wenzelm@22909
   216
      let val A = Term.argument_type_of c 0
wenzelm@52788
   217
      in Cterm {t = a, T = A, thy = thy, maxidx = maxidx, sorts = sorts} end
wenzelm@22584
   218
  | dest_arg ct = raise CTERM ("dest_arg", [ct]);
wenzelm@20580
   219
wenzelm@22909
   220
wenzelm@52788
   221
fun dest_fun2 (Cterm {t = c $ _ $ _, T, thy, maxidx, sorts}) =
wenzelm@22909
   222
      let
wenzelm@22909
   223
        val A = Term.argument_type_of c 0;
wenzelm@22909
   224
        val B = Term.argument_type_of c 1;
wenzelm@52788
   225
      in Cterm {t = c, T = A --> B --> T, thy = thy, maxidx = maxidx, sorts = sorts} end
wenzelm@22909
   226
  | dest_fun2 ct = raise CTERM ("dest_fun2", [ct]);
wenzelm@22909
   227
wenzelm@52788
   228
fun dest_arg1 (Cterm {t = c $ a $ _, T = _, thy, maxidx, sorts}) =
wenzelm@22909
   229
      let val A = Term.argument_type_of c 0
wenzelm@52788
   230
      in Cterm {t = a, T = A, thy = thy, maxidx = maxidx, sorts = sorts} end
wenzelm@22909
   231
  | dest_arg1 ct = raise CTERM ("dest_arg1", [ct]);
wenzelm@20673
   232
wenzelm@52788
   233
fun dest_abs a (Cterm {t = Abs (x, T, t), T = Type ("fun", [_, U]), thy, maxidx, sorts}) =
wenzelm@18944
   234
      let val (y', t') = Term.dest_abs (the_default x a, T, t) in
wenzelm@52788
   235
        (Cterm {t = Free (y', T), T = T, thy = thy, maxidx = maxidx, sorts = sorts},
wenzelm@52788
   236
          Cterm {t = t', T = U, thy = thy, maxidx = maxidx, sorts = sorts})
clasohm@1493
   237
      end
wenzelm@22584
   238
  | dest_abs _ ct = raise CTERM ("dest_abs", [ct]);
clasohm@1493
   239
wenzelm@22909
   240
wenzelm@22909
   241
(* constructors *)
wenzelm@22909
   242
wenzelm@46497
   243
fun apply
wenzelm@16656
   244
  (cf as Cterm {t = f, T = Type ("fun", [dty, rty]), maxidx = maxidx1, sorts = sorts1, ...})
wenzelm@16656
   245
  (cx as Cterm {t = x, T, maxidx = maxidx2, sorts = sorts2, ...}) =
wenzelm@16601
   246
    if T = dty then
wenzelm@52788
   247
      Cterm {thy = merge_thys0 cf cx,
wenzelm@16656
   248
        t = f $ x,
wenzelm@16656
   249
        T = rty,
wenzelm@16656
   250
        maxidx = Int.max (maxidx1, maxidx2),
wenzelm@16601
   251
        sorts = Sorts.union sorts1 sorts2}
wenzelm@46497
   252
      else raise CTERM ("apply: types don't agree", [cf, cx])
wenzelm@46497
   253
  | apply cf cx = raise CTERM ("apply: first arg is not a function", [cf, cx]);
wenzelm@250
   254
wenzelm@46497
   255
fun lambda_name
wenzelm@32198
   256
  (x, ct1 as Cterm {t = t1, T = T1, maxidx = maxidx1, sorts = sorts1, ...})
wenzelm@16656
   257
  (ct2 as Cterm {t = t2, T = T2, maxidx = maxidx2, sorts = sorts2, ...}) =
wenzelm@32198
   258
    let val t = Term.lambda_name (x, t1) t2 in
wenzelm@52788
   259
      Cterm {thy = merge_thys0 ct1 ct2,
wenzelm@16656
   260
        t = t, T = T1 --> T2,
wenzelm@16656
   261
        maxidx = Int.max (maxidx1, maxidx2),
wenzelm@16656
   262
        sorts = Sorts.union sorts1 sorts2}
wenzelm@16601
   263
    end;
lcp@229
   264
wenzelm@46497
   265
fun lambda t u = lambda_name ("", t) u;
wenzelm@32198
   266
wenzelm@20580
   267
wenzelm@22909
   268
(* indexes *)
wenzelm@22909
   269
wenzelm@52788
   270
fun adjust_maxidx_cterm i (ct as Cterm {thy, t, T, maxidx, sorts}) =
wenzelm@20580
   271
  if maxidx = i then ct
wenzelm@20580
   272
  else if maxidx < i then
wenzelm@52788
   273
    Cterm {maxidx = i, thy = thy, t = t, T = T, sorts = sorts}
wenzelm@20580
   274
  else
wenzelm@52788
   275
    Cterm {maxidx = Int.max (maxidx_of_term t, i), thy = thy, t = t, T = T, sorts = sorts};
wenzelm@20580
   276
wenzelm@52788
   277
fun incr_indexes_cterm i (ct as Cterm {thy, t, T, maxidx, sorts}) =
wenzelm@22909
   278
  if i < 0 then raise CTERM ("negative increment", [ct])
wenzelm@22909
   279
  else if i = 0 then ct
wenzelm@52788
   280
  else Cterm {thy = thy, t = Logic.incr_indexes ([], i) t,
wenzelm@22909
   281
    T = Logic.incr_tvar i T, maxidx = maxidx + i, sorts = sorts};
wenzelm@22909
   282
wenzelm@22909
   283
wenzelm@22909
   284
(* matching *)
wenzelm@22909
   285
wenzelm@22909
   286
local
wenzelm@22909
   287
wenzelm@22909
   288
fun gen_match match
wenzelm@20512
   289
    (ct1 as Cterm {t = t1, sorts = sorts1, ...},
wenzelm@20815
   290
     ct2 as Cterm {t = t2, sorts = sorts2, maxidx = maxidx2, ...}) =
berghofe@10416
   291
  let
wenzelm@52788
   292
    val thy = merge_thys0 ct1 ct2;
wenzelm@24143
   293
    val (Tinsts, tinsts) = match thy (t1, t2) (Vartab.empty, Vartab.empty);
wenzelm@16601
   294
    val sorts = Sorts.union sorts1 sorts2;
wenzelm@20512
   295
    fun mk_cTinst ((a, i), (S, T)) =
wenzelm@52788
   296
      (Ctyp {T = TVar ((a, i), S), thy = thy, maxidx = i, sorts = sorts},
wenzelm@52788
   297
       Ctyp {T = T, thy = thy, maxidx = maxidx2, sorts = sorts});
wenzelm@20512
   298
    fun mk_ctinst ((x, i), (T, t)) =
wenzelm@32035
   299
      let val T = Envir.subst_type Tinsts T in
wenzelm@52788
   300
        (Cterm {t = Var ((x, i), T), T = T, thy = thy, maxidx = i, sorts = sorts},
wenzelm@52788
   301
         Cterm {t = t, T = T, thy = thy, maxidx = maxidx2, sorts = sorts})
berghofe@10416
   302
      end;
wenzelm@16656
   303
  in (Vartab.fold (cons o mk_cTinst) Tinsts [], Vartab.fold (cons o mk_ctinst) tinsts []) end;
berghofe@10416
   304
wenzelm@22909
   305
in
berghofe@10416
   306
wenzelm@22909
   307
val match = gen_match Pattern.match;
wenzelm@22909
   308
val first_order_match = gen_match Pattern.first_order_match;
wenzelm@22909
   309
wenzelm@22909
   310
end;
berghofe@10416
   311
wenzelm@2509
   312
wenzelm@2509
   313
wenzelm@28321
   314
(*** Derivations and Theorems ***)
lcp@229
   315
wenzelm@32590
   316
abstype thm = Thm of
wenzelm@40124
   317
 deriv *                        (*derivation*)
wenzelm@52788
   318
 {thy: theory,                  (*background theory*)
wenzelm@40124
   319
  tags: Properties.T,           (*additional annotations/comments*)
wenzelm@40124
   320
  maxidx: int,                  (*maximum index of any Var or TVar*)
wenzelm@40124
   321
  shyps: sort Ord_List.T,       (*sort hypotheses*)
wenzelm@40124
   322
  hyps: term Ord_List.T,        (*hypotheses*)
wenzelm@40124
   323
  tpairs: (term * term) list,   (*flex-flex pairs*)
wenzelm@40124
   324
  prop: term}                   (*conclusion*)
wenzelm@28624
   325
and deriv = Deriv of
wenzelm@39687
   326
 {promises: (serial * thm future) Ord_List.T,
wenzelm@37309
   327
  body: Proofterm.proof_body}
wenzelm@32590
   328
with
clasohm@0
   329
wenzelm@23601
   330
type conv = cterm -> thm;
wenzelm@23601
   331
wenzelm@16725
   332
(*errors involving theorems*)
wenzelm@16725
   333
exception THM of string * int * thm list;
berghofe@13658
   334
wenzelm@28321
   335
fun rep_thm (Thm (_, args)) = args;
clasohm@0
   336
wenzelm@52788
   337
fun crep_thm (Thm (_, {thy, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@52788
   338
  let fun cterm max t = Cterm {thy = thy, t = t, T = propT, maxidx = max, sorts = shyps} in
wenzelm@52788
   339
   {thy = thy, tags = tags, maxidx = maxidx, shyps = shyps,
wenzelm@16425
   340
    hyps = map (cterm ~1) hyps,
wenzelm@16425
   341
    tpairs = map (pairself (cterm maxidx)) tpairs,
wenzelm@16425
   342
    prop = cterm maxidx prop}
clasohm@1517
   343
  end;
clasohm@1517
   344
wenzelm@31947
   345
fun fold_terms f (Thm (_, {tpairs, prop, hyps, ...})) =
wenzelm@31947
   346
  fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps;
wenzelm@31947
   347
wenzelm@16725
   348
fun terms_of_tpairs tpairs = fold_rev (fn (t, u) => cons t o cons u) tpairs [];
wenzelm@16725
   349
wenzelm@16725
   350
fun eq_tpairs ((t, u), (t', u')) = t aconv t' andalso u aconv u';
wenzelm@18944
   351
fun union_tpairs ts us = Library.merge eq_tpairs (ts, us);
wenzelm@16884
   352
val maxidx_tpairs = fold (fn (t, u) => Term.maxidx_term t #> Term.maxidx_term u);
wenzelm@16725
   353
wenzelm@16725
   354
fun attach_tpairs tpairs prop =
wenzelm@16725
   355
  Logic.list_implies (map Logic.mk_equals tpairs, prop);
wenzelm@16725
   356
wenzelm@28321
   357
fun full_prop_of (Thm (_, {tpairs, prop, ...})) = attach_tpairs tpairs prop;
wenzelm@16945
   358
wenzelm@39687
   359
val union_hyps = Ord_List.union Term_Ord.fast_term_ord;
wenzelm@39687
   360
val insert_hyps = Ord_List.insert Term_Ord.fast_term_ord;
wenzelm@39687
   361
val remove_hyps = Ord_List.remove Term_Ord.fast_term_ord;
wenzelm@22365
   362
wenzelm@16945
   363
wenzelm@24143
   364
(* merge theories of cterms/thms -- trivial absorption only *)
wenzelm@16945
   365
wenzelm@52788
   366
fun merge_thys1 (Cterm {thy = thy1, ...}) (Thm (_, {thy = thy2, ...})) =
wenzelm@52788
   367
  Theory.merge (thy1, thy2);
wenzelm@16945
   368
wenzelm@52788
   369
fun merge_thys2 (Thm (_, {thy = thy1, ...})) (Thm (_, {thy = thy2, ...})) =
wenzelm@52788
   370
  Theory.merge (thy1, thy2);
wenzelm@16945
   371
clasohm@0
   372
wenzelm@22365
   373
(* basic components *)
wenzelm@16135
   374
wenzelm@52788
   375
val theory_of_thm = #thy o rep_thm;
wenzelm@28321
   376
val maxidx_of = #maxidx o rep_thm;
wenzelm@19910
   377
fun maxidx_thm th i = Int.max (maxidx_of th, i);
wenzelm@28321
   378
val hyps_of = #hyps o rep_thm;
wenzelm@28321
   379
val prop_of = #prop o rep_thm;
wenzelm@28321
   380
val tpairs_of = #tpairs o rep_thm;
clasohm@0
   381
wenzelm@16601
   382
val concl_of = Logic.strip_imp_concl o prop_of;
wenzelm@16601
   383
val prems_of = Logic.strip_imp_prems o prop_of;
wenzelm@21576
   384
val nprems_of = Logic.count_prems o prop_of;
wenzelm@19305
   385
fun no_prems th = nprems_of th = 0;
wenzelm@16601
   386
wenzelm@16601
   387
fun major_prem_of th =
wenzelm@16601
   388
  (case prems_of th of
wenzelm@16601
   389
    prem :: _ => Logic.strip_assums_concl prem
wenzelm@16601
   390
  | [] => raise THM ("major_prem_of: rule with no premises", 0, [th]));
wenzelm@16601
   391
wenzelm@16601
   392
(*the statement of any thm is a cterm*)
wenzelm@52788
   393
fun cprop_of (Thm (_, {thy, maxidx, shyps, prop, ...})) =
wenzelm@52788
   394
  Cterm {thy = thy, maxidx = maxidx, T = propT, t = prop, sorts = shyps};
wenzelm@16601
   395
wenzelm@52788
   396
fun cprem_of (th as Thm (_, {thy, maxidx, shyps, prop, ...})) i =
wenzelm@52788
   397
  Cterm {thy = thy, maxidx = maxidx, T = propT, sorts = shyps,
wenzelm@18145
   398
    t = Logic.nth_prem (i, prop) handle TERM _ => raise THM ("cprem_of", i, [th])};
wenzelm@18035
   399
wenzelm@16656
   400
(*explicit transfer to a super theory*)
wenzelm@16425
   401
fun transfer thy' thm =
wenzelm@3895
   402
  let
wenzelm@52788
   403
    val Thm (der, {thy, tags, maxidx, shyps, hyps, tpairs, prop}) = thm;
wenzelm@26665
   404
    val _ = Theory.subthy (thy, thy') orelse raise THM ("transfer: not a super theory", 0, [thm]);
wenzelm@3895
   405
  in
wenzelm@52788
   406
    if Theory.eq_thy (thy, thy') then thm
wenzelm@16945
   407
    else
wenzelm@28321
   408
      Thm (der,
wenzelm@52788
   409
       {thy = thy',
wenzelm@21646
   410
        tags = tags,
wenzelm@16945
   411
        maxidx = maxidx,
wenzelm@16945
   412
        shyps = shyps,
wenzelm@16945
   413
        hyps = hyps,
wenzelm@16945
   414
        tpairs = tpairs,
wenzelm@28321
   415
        prop = prop})
wenzelm@3895
   416
  end;
wenzelm@387
   417
wenzelm@16945
   418
(*explicit weakening: maps |- B to A |- B*)
wenzelm@16945
   419
fun weaken raw_ct th =
wenzelm@16945
   420
  let
wenzelm@20261
   421
    val ct as Cterm {t = A, T, sorts, maxidx = maxidxA, ...} = adjust_maxidx_cterm ~1 raw_ct;
wenzelm@28321
   422
    val Thm (der, {tags, maxidx, shyps, hyps, tpairs, prop, ...}) = th;
wenzelm@16945
   423
  in
wenzelm@16945
   424
    if T <> propT then
wenzelm@16945
   425
      raise THM ("weaken: assumptions must have type prop", 0, [])
wenzelm@16945
   426
    else if maxidxA <> ~1 then
wenzelm@16945
   427
      raise THM ("weaken: assumptions may not contain schematic variables", maxidxA, [])
wenzelm@16945
   428
    else
wenzelm@28321
   429
      Thm (der,
wenzelm@52788
   430
       {thy = merge_thys1 ct th,
wenzelm@21646
   431
        tags = tags,
wenzelm@16945
   432
        maxidx = maxidx,
wenzelm@16945
   433
        shyps = Sorts.union sorts shyps,
wenzelm@28354
   434
        hyps = insert_hyps A hyps,
wenzelm@16945
   435
        tpairs = tpairs,
wenzelm@28321
   436
        prop = prop})
wenzelm@16945
   437
  end;
wenzelm@16656
   438
wenzelm@28624
   439
fun weaken_sorts raw_sorts ct =
wenzelm@28624
   440
  let
wenzelm@52788
   441
    val Cterm {thy, t, T, maxidx, sorts} = ct;
wenzelm@28624
   442
    val more_sorts = Sorts.make (map (Sign.certify_sort thy) raw_sorts);
wenzelm@28624
   443
    val sorts' = Sorts.union sorts more_sorts;
wenzelm@52788
   444
  in Cterm {thy = thy, t = t, T = T, maxidx = maxidx, sorts = sorts'} end;
wenzelm@28624
   445
wenzelm@16656
   446
(*dangling sort constraints of a thm*)
wenzelm@31947
   447
fun extra_shyps (th as Thm (_, {shyps, ...})) =
wenzelm@31947
   448
  Sorts.subtract (fold_terms Sorts.insert_term th []) shyps;
wenzelm@28321
   449
wenzelm@28321
   450
wenzelm@28321
   451
wenzelm@32725
   452
(** derivations and promised proofs **)
wenzelm@28321
   453
wenzelm@32059
   454
fun make_deriv promises oracles thms proof =
wenzelm@32059
   455
  Deriv {promises = promises, body = PBody {oracles = oracles, thms = thms, proof = proof}};
wenzelm@28321
   456
wenzelm@37309
   457
val empty_deriv = make_deriv [] [] [] Proofterm.MinProof;
wenzelm@28321
   458
wenzelm@28330
   459
wenzelm@28354
   460
(* inference rules *)
wenzelm@28321
   461
wenzelm@28378
   462
fun promise_ord ((i, _), (j, _)) = int_ord (j, i);
wenzelm@28330
   463
wenzelm@52487
   464
fun deriv_rule2 f
wenzelm@32059
   465
    (Deriv {promises = ps1, body = PBody {oracles = oras1, thms = thms1, proof = prf1}})
wenzelm@32059
   466
    (Deriv {promises = ps2, body = PBody {oracles = oras2, thms = thms2, proof = prf2}}) =
wenzelm@28321
   467
  let
wenzelm@39687
   468
    val ps = Ord_List.union promise_ord ps1 ps2;
wenzelm@44334
   469
    val oras = Proofterm.unions_oracles [oras1, oras2];
wenzelm@44334
   470
    val thms = Proofterm.unions_thms [thms1, thms2];
wenzelm@28321
   471
    val prf =
wenzelm@52487
   472
      (case ! Proofterm.proofs of
wenzelm@28321
   473
        2 => f prf1 prf2
wenzelm@28804
   474
      | 1 => MinProof
wenzelm@28804
   475
      | 0 => MinProof
wenzelm@28321
   476
      | i => error ("Illegal level of detail for proof objects: " ^ string_of_int i));
wenzelm@32059
   477
  in make_deriv ps oras thms prf end;
wenzelm@28321
   478
wenzelm@52487
   479
fun deriv_rule1 f = deriv_rule2 (K f) empty_deriv;
wenzelm@52487
   480
fun deriv_rule0 prf = deriv_rule1 I (make_deriv [] [] [] prf);
wenzelm@28321
   481
wenzelm@36621
   482
fun deriv_rule_unconditional f (Deriv {promises, body = PBody {oracles, thms, proof}}) =
wenzelm@36621
   483
  make_deriv promises oracles thms (f proof);
wenzelm@36621
   484
wenzelm@1238
   485
wenzelm@32725
   486
(* fulfilled proofs *)
wenzelm@32725
   487
wenzelm@44331
   488
fun raw_body_of (Thm (Deriv {body, ...}, _)) = body;
wenzelm@44331
   489
fun raw_promises_of (Thm (Deriv {promises, ...}, _)) = promises;
wenzelm@44331
   490
wenzelm@44331
   491
fun join_promises [] = ()
wenzelm@44331
   492
  | join_promises promises = join_promises_of (Future.joins (map snd promises))
wenzelm@49008
   493
and join_promises_of thms = join_promises (Ord_List.make promise_ord (maps raw_promises_of thms));
wenzelm@32725
   494
wenzelm@52788
   495
fun fulfill_body (Thm (Deriv {promises, body}, {thy, ...})) =
wenzelm@52788
   496
  Proofterm.fulfill_norm_proof thy (fulfill_promises promises) body
wenzelm@44331
   497
and fulfill_promises promises =
wenzelm@44331
   498
  map fst promises ~~ map fulfill_body (Future.joins (map snd promises));
wenzelm@32725
   499
wenzelm@44333
   500
fun proof_bodies_of thms =
wenzelm@44333
   501
  let
wenzelm@44333
   502
    val _ = join_promises_of thms;
wenzelm@44333
   503
    val bodies = map fulfill_body thms;
wenzelm@44333
   504
    val _ = Proofterm.join_bodies bodies;
wenzelm@44333
   505
  in bodies end;
wenzelm@44333
   506
wenzelm@44333
   507
val proof_body_of = singleton proof_bodies_of;
wenzelm@44331
   508
val proof_of = Proofterm.proof_of o proof_body_of;
wenzelm@32725
   509
wenzelm@44333
   510
val join_proofs = ignore o proof_bodies_of;
wenzelm@32725
   511
wenzelm@32725
   512
wenzelm@32725
   513
(* derivation status *)
wenzelm@32725
   514
wenzelm@50126
   515
fun peek_status (Thm (Deriv {promises, body}, _)) =
wenzelm@32725
   516
  let
wenzelm@32725
   517
    val ps = map (Future.peek o snd) promises;
wenzelm@32725
   518
    val bodies = body ::
wenzelm@44331
   519
      map_filter (fn SOME (Exn.Res th) => SOME (raw_body_of th) | _ => NONE) ps;
wenzelm@50126
   520
    val {oracle, unfinished, failed} = Proofterm.peek_status bodies;
wenzelm@32725
   521
  in
wenzelm@32725
   522
   {oracle = oracle,
wenzelm@32725
   523
    unfinished = unfinished orelse exists is_none ps,
wenzelm@32725
   524
    failed = failed orelse exists (fn SOME (Exn.Exn _) => true | _ => false) ps}
wenzelm@32725
   525
  end;
wenzelm@32725
   526
wenzelm@32725
   527
wenzelm@32725
   528
(* future rule *)
wenzelm@32725
   529
wenzelm@36613
   530
fun future_result i orig_thy orig_shyps orig_prop thm =
wenzelm@32725
   531
  let
wenzelm@36613
   532
    fun err msg = raise THM ("future_result: " ^ msg, 0, [thm]);
wenzelm@52788
   533
    val Thm (Deriv {promises, ...}, {thy, shyps, hyps, tpairs, prop, ...}) = thm;
wenzelm@36613
   534
wenzelm@52788
   535
    val _ = Theory.eq_thy (thy, orig_thy) orelse err "bad theory";
wenzelm@32725
   536
    val _ = prop aconv orig_prop orelse err "bad prop";
wenzelm@32725
   537
    val _ = null tpairs orelse err "bad tpairs";
wenzelm@32725
   538
    val _ = null hyps orelse err "bad hyps";
wenzelm@32725
   539
    val _ = Sorts.subset (shyps, orig_shyps) orelse err "bad shyps";
wenzelm@32725
   540
    val _ = forall (fn (j, _) => i <> j) promises orelse err "bad dependencies";
wenzelm@44331
   541
    val _ = join_promises promises;
wenzelm@32725
   542
  in thm end;
wenzelm@32725
   543
wenzelm@32725
   544
fun future future_thm ct =
wenzelm@32725
   545
  let
wenzelm@52788
   546
    val Cterm {thy = thy, t = prop, T, maxidx, sorts} = ct;
wenzelm@32725
   547
    val _ = T <> propT andalso raise CTERM ("future: prop expected", [ct]);
wenzelm@32725
   548
wenzelm@32725
   549
    val i = serial ();
wenzelm@32725
   550
    val future = future_thm |> Future.map (future_result i thy sorts prop);
wenzelm@32725
   551
  in
wenzelm@37309
   552
    Thm (make_deriv [(i, future)] [] [] (Proofterm.promise_proof thy i prop),
wenzelm@52788
   553
     {thy = thy,
wenzelm@32725
   554
      tags = [],
wenzelm@32725
   555
      maxidx = maxidx,
wenzelm@32725
   556
      shyps = sorts,
wenzelm@32725
   557
      hyps = [],
wenzelm@32725
   558
      tpairs = [],
wenzelm@32725
   559
      prop = prop})
wenzelm@32725
   560
  end;
wenzelm@32725
   561
wenzelm@32725
   562
wenzelm@32725
   563
(* closed derivations with official name *)
wenzelm@32725
   564
wenzelm@41699
   565
(*non-deterministic, depends on unknown promises*)
wenzelm@37297
   566
fun derivation_name (Thm (Deriv {body, ...}, {shyps, hyps, prop, ...})) =
wenzelm@37309
   567
  Proofterm.get_name shyps hyps prop (Proofterm.proof_of body);
wenzelm@32725
   568
wenzelm@36744
   569
fun name_derivation name (thm as Thm (der, args)) =
wenzelm@32725
   570
  let
wenzelm@32725
   571
    val Deriv {promises, body} = der;
wenzelm@52788
   572
    val {thy, shyps, hyps, prop, tpairs, ...} = args;
wenzelm@32725
   573
    val _ = null tpairs orelse raise THM ("put_name: unsolved flex-flex constraints", 0, [thm]);
wenzelm@32725
   574
wenzelm@41700
   575
    val ps = map (apsnd (Future.map fulfill_body)) promises;
wenzelm@37309
   576
    val (pthm, proof) = Proofterm.thm_proof thy name shyps hyps prop ps body;
wenzelm@32725
   577
    val der' = make_deriv [] [] [pthm] proof;
wenzelm@32725
   578
  in Thm (der', args) end;
wenzelm@32725
   579
wenzelm@32725
   580
wenzelm@1238
   581
paulson@1529
   582
(** Axioms **)
wenzelm@387
   583
wenzelm@28675
   584
fun axiom theory name =
wenzelm@387
   585
  let
wenzelm@16425
   586
    fun get_ax thy =
wenzelm@56025
   587
      Name_Space.lookup_key (Theory.axiom_table thy) name
wenzelm@56025
   588
      |> Option.map (fn (_, prop) =>
wenzelm@24143
   589
           let
wenzelm@52487
   590
             val der = deriv_rule0 (Proofterm.axm_proof name prop);
wenzelm@24143
   591
             val maxidx = maxidx_of_term prop;
wenzelm@26640
   592
             val shyps = Sorts.insert_term prop [];
wenzelm@24143
   593
           in
wenzelm@52788
   594
             Thm (der, {thy = thy, tags = [],
wenzelm@28321
   595
               maxidx = maxidx, shyps = shyps, hyps = [], tpairs = [], prop = prop})
wenzelm@24143
   596
           end);
wenzelm@387
   597
  in
wenzelm@42425
   598
    (case get_first get_ax (Theory.nodes_of theory) of
skalberg@15531
   599
      SOME thm => thm
skalberg@15531
   600
    | NONE => raise THEORY ("No axiom " ^ quote name, [theory]))
wenzelm@387
   601
  end;
wenzelm@387
   602
wenzelm@776
   603
(*return additional axioms of this theory node*)
wenzelm@776
   604
fun axioms_of thy =
wenzelm@56025
   605
  map (fn (name, _) => (name, axiom thy name)) (Theory.axioms_of thy);
wenzelm@776
   606
wenzelm@6089
   607
wenzelm@28804
   608
(* tags *)
wenzelm@6089
   609
wenzelm@21646
   610
val get_tags = #tags o rep_thm;
wenzelm@6089
   611
wenzelm@52788
   612
fun map_tags f (Thm (der, {thy, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@52788
   613
  Thm (der, {thy = thy, tags = f tags, maxidx = maxidx,
wenzelm@28321
   614
    shyps = shyps, hyps = hyps, tpairs = tpairs, prop = prop});
clasohm@0
   615
clasohm@0
   616
wenzelm@43795
   617
(* technical adjustments *)
wenzelm@43795
   618
wenzelm@52788
   619
fun norm_proof (Thm (der, args as {thy, ...})) =
wenzelm@52788
   620
  Thm (deriv_rule1 (Proofterm.rew_proof thy) der, args);
berghofe@23781
   621
wenzelm@52788
   622
fun adjust_maxidx_thm i (th as Thm (der, {thy, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@20261
   623
  if maxidx = i then th
wenzelm@20261
   624
  else if maxidx < i then
wenzelm@52788
   625
    Thm (der, {maxidx = i, thy = thy, tags = tags, shyps = shyps,
wenzelm@28321
   626
      hyps = hyps, tpairs = tpairs, prop = prop})
wenzelm@20261
   627
  else
wenzelm@52788
   628
    Thm (der, {maxidx = Int.max (maxidx_tpairs tpairs (maxidx_of_term prop), i), thy = thy,
wenzelm@28321
   629
      tags = tags, shyps = shyps, hyps = hyps, tpairs = tpairs, prop = prop});
wenzelm@564
   630
wenzelm@387
   631
wenzelm@2509
   632
paulson@1529
   633
(*** Meta rules ***)
clasohm@0
   634
wenzelm@16601
   635
(** primitive rules **)
clasohm@0
   636
wenzelm@16656
   637
(*The assumption rule A |- A*)
wenzelm@16601
   638
fun assume raw_ct =
wenzelm@52788
   639
  let val Cterm {thy, t = prop, T, maxidx, sorts} = adjust_maxidx_cterm ~1 raw_ct in
wenzelm@16601
   640
    if T <> propT then
mengj@19230
   641
      raise THM ("assume: prop", 0, [])
wenzelm@16601
   642
    else if maxidx <> ~1 then
mengj@19230
   643
      raise THM ("assume: variables", maxidx, [])
wenzelm@52487
   644
    else Thm (deriv_rule0 (Proofterm.Hyp prop),
wenzelm@52788
   645
     {thy = thy,
wenzelm@21646
   646
      tags = [],
wenzelm@16601
   647
      maxidx = ~1,
wenzelm@16601
   648
      shyps = sorts,
wenzelm@16601
   649
      hyps = [prop],
wenzelm@16601
   650
      tpairs = [],
wenzelm@28321
   651
      prop = prop})
clasohm@0
   652
  end;
clasohm@0
   653
wenzelm@1220
   654
(*Implication introduction
wenzelm@3529
   655
    [A]
wenzelm@3529
   656
     :
wenzelm@3529
   657
     B
wenzelm@1220
   658
  -------
wenzelm@1220
   659
  A ==> B
wenzelm@1220
   660
*)
wenzelm@16601
   661
fun implies_intr
wenzelm@16679
   662
    (ct as Cterm {t = A, T, maxidx = maxidxA, sorts, ...})
wenzelm@28321
   663
    (th as Thm (der, {maxidx, hyps, shyps, tpairs, prop, ...})) =
wenzelm@16601
   664
  if T <> propT then
wenzelm@16601
   665
    raise THM ("implies_intr: assumptions must have type prop", 0, [th])
wenzelm@16601
   666
  else
wenzelm@52487
   667
    Thm (deriv_rule1 (Proofterm.implies_intr_proof A) der,
wenzelm@52788
   668
     {thy = merge_thys1 ct th,
wenzelm@52487
   669
      tags = [],
wenzelm@52487
   670
      maxidx = Int.max (maxidxA, maxidx),
wenzelm@52487
   671
      shyps = Sorts.union sorts shyps,
wenzelm@52487
   672
      hyps = remove_hyps A hyps,
wenzelm@52487
   673
      tpairs = tpairs,
wenzelm@52487
   674
      prop = Logic.mk_implies (A, prop)});
clasohm@0
   675
paulson@1529
   676
wenzelm@1220
   677
(*Implication elimination
wenzelm@1220
   678
  A ==> B    A
wenzelm@1220
   679
  ------------
wenzelm@1220
   680
        B
wenzelm@1220
   681
*)
wenzelm@16601
   682
fun implies_elim thAB thA =
wenzelm@16601
   683
  let
wenzelm@28321
   684
    val Thm (derA, {maxidx = maxA, hyps = hypsA, shyps = shypsA, tpairs = tpairsA,
wenzelm@28321
   685
      prop = propA, ...}) = thA
wenzelm@28321
   686
    and Thm (der, {maxidx, hyps, shyps, tpairs, prop, ...}) = thAB;
wenzelm@16601
   687
    fun err () = raise THM ("implies_elim: major premise", 0, [thAB, thA]);
wenzelm@16601
   688
  in
wenzelm@16601
   689
    case prop of
wenzelm@56245
   690
      Const ("Pure.imp", _) $ A $ B =>
wenzelm@20512
   691
        if A aconv propA then
wenzelm@52487
   692
          Thm (deriv_rule2 (curry Proofterm.%%) der derA,
wenzelm@52788
   693
           {thy = merge_thys2 thAB thA,
wenzelm@21646
   694
            tags = [],
wenzelm@16601
   695
            maxidx = Int.max (maxA, maxidx),
wenzelm@16601
   696
            shyps = Sorts.union shypsA shyps,
wenzelm@16601
   697
            hyps = union_hyps hypsA hyps,
wenzelm@16601
   698
            tpairs = union_tpairs tpairsA tpairs,
wenzelm@28321
   699
            prop = B})
wenzelm@16601
   700
        else err ()
wenzelm@16601
   701
    | _ => err ()
wenzelm@16601
   702
  end;
wenzelm@250
   703
wenzelm@1220
   704
(*Forall introduction.  The Free or Var x must not be free in the hypotheses.
wenzelm@16656
   705
    [x]
wenzelm@16656
   706
     :
wenzelm@16656
   707
     A
wenzelm@16656
   708
  ------
wenzelm@16656
   709
  !!x. A
wenzelm@1220
   710
*)
wenzelm@16601
   711
fun forall_intr
wenzelm@16601
   712
    (ct as Cterm {t = x, T, sorts, ...})
wenzelm@28321
   713
    (th as Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@16601
   714
  let
wenzelm@16601
   715
    fun result a =
wenzelm@52487
   716
      Thm (deriv_rule1 (Proofterm.forall_intr_proof x a) der,
wenzelm@52788
   717
       {thy = merge_thys1 ct th,
wenzelm@21646
   718
        tags = [],
wenzelm@16601
   719
        maxidx = maxidx,
wenzelm@16601
   720
        shyps = Sorts.union sorts shyps,
wenzelm@16601
   721
        hyps = hyps,
wenzelm@16601
   722
        tpairs = tpairs,
wenzelm@46217
   723
        prop = Logic.all_const T $ Abs (a, T, abstract_over (x, prop))});
wenzelm@21798
   724
    fun check_occs a x ts =
wenzelm@16847
   725
      if exists (fn t => Logic.occs (x, t)) ts then
wenzelm@21798
   726
        raise THM ("forall_intr: variable " ^ quote a ^ " free in assumptions", 0, [th])
wenzelm@16601
   727
      else ();
wenzelm@16601
   728
  in
wenzelm@52788
   729
    (case x of
wenzelm@21798
   730
      Free (a, _) => (check_occs a x hyps; check_occs a x (terms_of_tpairs tpairs); result a)
wenzelm@21798
   731
    | Var ((a, _), _) => (check_occs a x (terms_of_tpairs tpairs); result a)
wenzelm@52788
   732
    | _ => raise THM ("forall_intr: not a variable", 0, [th]))
clasohm@0
   733
  end;
clasohm@0
   734
wenzelm@1220
   735
(*Forall elimination
wenzelm@16656
   736
  !!x. A
wenzelm@1220
   737
  ------
wenzelm@1220
   738
  A[t/x]
wenzelm@1220
   739
*)
wenzelm@16601
   740
fun forall_elim
wenzelm@16601
   741
    (ct as Cterm {t, T, maxidx = maxt, sorts, ...})
wenzelm@28321
   742
    (th as Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@16601
   743
  (case prop of
wenzelm@56245
   744
    Const ("Pure.all", Type ("fun", [Type ("fun", [qary, _]), _])) $ A =>
wenzelm@16601
   745
      if T <> qary then
wenzelm@16601
   746
        raise THM ("forall_elim: type mismatch", 0, [th])
wenzelm@16601
   747
      else
wenzelm@52487
   748
        Thm (deriv_rule1 (Proofterm.% o rpair (SOME t)) der,
wenzelm@52788
   749
         {thy = merge_thys1 ct th,
wenzelm@52487
   750
          tags = [],
wenzelm@52487
   751
          maxidx = Int.max (maxidx, maxt),
wenzelm@52487
   752
          shyps = Sorts.union sorts shyps,
wenzelm@52487
   753
          hyps = hyps,
wenzelm@52487
   754
          tpairs = tpairs,
wenzelm@52487
   755
          prop = Term.betapply (A, t)})
wenzelm@16601
   756
  | _ => raise THM ("forall_elim: not quantified", 0, [th]));
clasohm@0
   757
clasohm@0
   758
wenzelm@1220
   759
(* Equality *)
clasohm@0
   760
wenzelm@16601
   761
(*Reflexivity
wenzelm@16601
   762
  t == t
wenzelm@16601
   763
*)
wenzelm@52788
   764
fun reflexive (Cterm {thy, t, T = _, maxidx, sorts}) =
wenzelm@52487
   765
  Thm (deriv_rule0 Proofterm.reflexive,
wenzelm@52788
   766
   {thy = thy,
wenzelm@21646
   767
    tags = [],
wenzelm@16601
   768
    maxidx = maxidx,
wenzelm@16601
   769
    shyps = sorts,
wenzelm@16601
   770
    hyps = [],
wenzelm@16601
   771
    tpairs = [],
wenzelm@28321
   772
    prop = Logic.mk_equals (t, t)});
clasohm@0
   773
wenzelm@16601
   774
(*Symmetry
wenzelm@16601
   775
  t == u
wenzelm@16601
   776
  ------
wenzelm@16601
   777
  u == t
wenzelm@1220
   778
*)
wenzelm@52788
   779
fun symmetric (th as Thm (der, {thy, maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@16601
   780
  (case prop of
wenzelm@56245
   781
    (eq as Const ("Pure.eq", _)) $ t $ u =>
wenzelm@52487
   782
      Thm (deriv_rule1 Proofterm.symmetric der,
wenzelm@52788
   783
       {thy = thy,
wenzelm@21646
   784
        tags = [],
wenzelm@16601
   785
        maxidx = maxidx,
wenzelm@16601
   786
        shyps = shyps,
wenzelm@16601
   787
        hyps = hyps,
wenzelm@16601
   788
        tpairs = tpairs,
wenzelm@28321
   789
        prop = eq $ u $ t})
wenzelm@16601
   790
    | _ => raise THM ("symmetric", 0, [th]));
clasohm@0
   791
wenzelm@16601
   792
(*Transitivity
wenzelm@16601
   793
  t1 == u    u == t2
wenzelm@16601
   794
  ------------------
wenzelm@16601
   795
       t1 == t2
wenzelm@1220
   796
*)
clasohm@0
   797
fun transitive th1 th2 =
wenzelm@16601
   798
  let
wenzelm@28321
   799
    val Thm (der1, {maxidx = max1, hyps = hyps1, shyps = shyps1, tpairs = tpairs1,
wenzelm@28321
   800
      prop = prop1, ...}) = th1
wenzelm@28321
   801
    and Thm (der2, {maxidx = max2, hyps = hyps2, shyps = shyps2, tpairs = tpairs2,
wenzelm@28321
   802
      prop = prop2, ...}) = th2;
wenzelm@16601
   803
    fun err msg = raise THM ("transitive: " ^ msg, 0, [th1, th2]);
wenzelm@16601
   804
  in
wenzelm@16601
   805
    case (prop1, prop2) of
wenzelm@56245
   806
      ((eq as Const ("Pure.eq", Type (_, [T, _]))) $ t1 $ u, Const ("Pure.eq", _) $ u' $ t2) =>
wenzelm@16601
   807
        if not (u aconv u') then err "middle term"
wenzelm@16601
   808
        else
wenzelm@52487
   809
          Thm (deriv_rule2 (Proofterm.transitive u T) der1 der2,
wenzelm@52788
   810
           {thy = merge_thys2 th1 th2,
wenzelm@21646
   811
            tags = [],
wenzelm@16601
   812
            maxidx = Int.max (max1, max2),
wenzelm@16601
   813
            shyps = Sorts.union shyps1 shyps2,
wenzelm@16601
   814
            hyps = union_hyps hyps1 hyps2,
wenzelm@16601
   815
            tpairs = union_tpairs tpairs1 tpairs2,
wenzelm@28321
   816
            prop = eq $ t1 $ t2})
wenzelm@16601
   817
     | _ =>  err "premises"
clasohm@0
   818
  end;
clasohm@0
   819
wenzelm@16601
   820
(*Beta-conversion
wenzelm@16656
   821
  (%x. t)(u) == t[u/x]
wenzelm@16601
   822
  fully beta-reduces the term if full = true
berghofe@10416
   823
*)
wenzelm@52788
   824
fun beta_conversion full (Cterm {thy, t, T = _, maxidx, sorts}) =
wenzelm@16601
   825
  let val t' =
wenzelm@16601
   826
    if full then Envir.beta_norm t
wenzelm@16601
   827
    else
wenzelm@16601
   828
      (case t of Abs (_, _, bodt) $ u => subst_bound (u, bodt)
wenzelm@16601
   829
      | _ => raise THM ("beta_conversion: not a redex", 0, []));
wenzelm@16601
   830
  in
wenzelm@52487
   831
    Thm (deriv_rule0 Proofterm.reflexive,
wenzelm@52788
   832
     {thy = thy,
wenzelm@21646
   833
      tags = [],
wenzelm@16601
   834
      maxidx = maxidx,
wenzelm@16601
   835
      shyps = sorts,
wenzelm@16601
   836
      hyps = [],
wenzelm@16601
   837
      tpairs = [],
wenzelm@28321
   838
      prop = Logic.mk_equals (t, t')})
berghofe@10416
   839
  end;
berghofe@10416
   840
wenzelm@52788
   841
fun eta_conversion (Cterm {thy, t, T = _, maxidx, sorts}) =
wenzelm@52487
   842
  Thm (deriv_rule0 Proofterm.reflexive,
wenzelm@52788
   843
   {thy = thy,
wenzelm@21646
   844
    tags = [],
wenzelm@16601
   845
    maxidx = maxidx,
wenzelm@16601
   846
    shyps = sorts,
wenzelm@16601
   847
    hyps = [],
wenzelm@16601
   848
    tpairs = [],
wenzelm@28321
   849
    prop = Logic.mk_equals (t, Envir.eta_contract t)});
clasohm@0
   850
wenzelm@52788
   851
fun eta_long_conversion (Cterm {thy, t, T = _, maxidx, sorts}) =
wenzelm@52487
   852
  Thm (deriv_rule0 Proofterm.reflexive,
wenzelm@52788
   853
   {thy = thy,
wenzelm@23493
   854
    tags = [],
wenzelm@23493
   855
    maxidx = maxidx,
wenzelm@23493
   856
    shyps = sorts,
wenzelm@23493
   857
    hyps = [],
wenzelm@23493
   858
    tpairs = [],
wenzelm@52131
   859
    prop = Logic.mk_equals (t, Envir.eta_long [] t)});
wenzelm@23493
   860
clasohm@0
   861
(*The abstraction rule.  The Free or Var x must not be free in the hypotheses.
clasohm@0
   862
  The bound variable will be named "a" (since x will be something like x320)
wenzelm@16601
   863
      t == u
wenzelm@16601
   864
  --------------
wenzelm@16601
   865
  %x. t == %x. u
wenzelm@1220
   866
*)
wenzelm@16601
   867
fun abstract_rule a
wenzelm@16601
   868
    (Cterm {t = x, T, sorts, ...})
wenzelm@52788
   869
    (th as Thm (der, {thy, maxidx, hyps, shyps, tpairs, prop, ...})) =
wenzelm@16601
   870
  let
wenzelm@16601
   871
    val (t, u) = Logic.dest_equals prop
wenzelm@16601
   872
      handle TERM _ => raise THM ("abstract_rule: premise not an equality", 0, [th]);
wenzelm@16601
   873
    val result =
wenzelm@52487
   874
      Thm (deriv_rule1 (Proofterm.abstract_rule x a) der,
wenzelm@52788
   875
       {thy = thy,
wenzelm@21646
   876
        tags = [],
wenzelm@16601
   877
        maxidx = maxidx,
wenzelm@16601
   878
        shyps = Sorts.union sorts shyps,
wenzelm@16601
   879
        hyps = hyps,
wenzelm@16601
   880
        tpairs = tpairs,
wenzelm@16601
   881
        prop = Logic.mk_equals
wenzelm@28321
   882
          (Abs (a, T, abstract_over (x, t)), Abs (a, T, abstract_over (x, u)))});
wenzelm@21798
   883
    fun check_occs a x ts =
wenzelm@16847
   884
      if exists (fn t => Logic.occs (x, t)) ts then
wenzelm@21798
   885
        raise THM ("abstract_rule: variable " ^ quote a ^ " free in assumptions", 0, [th])
wenzelm@16601
   886
      else ();
wenzelm@16601
   887
  in
wenzelm@52788
   888
    (case x of
wenzelm@21798
   889
      Free (a, _) => (check_occs a x hyps; check_occs a x (terms_of_tpairs tpairs); result)
wenzelm@21798
   890
    | Var ((a, _), _) => (check_occs a x (terms_of_tpairs tpairs); result)
wenzelm@52788
   891
    | _ => raise THM ("abstract_rule: not a variable", 0, [th]))
clasohm@0
   892
  end;
clasohm@0
   893
clasohm@0
   894
(*The combination rule
wenzelm@3529
   895
  f == g  t == u
wenzelm@3529
   896
  --------------
wenzelm@16601
   897
    f t == g u
wenzelm@1220
   898
*)
clasohm@0
   899
fun combination th1 th2 =
wenzelm@16601
   900
  let
wenzelm@28321
   901
    val Thm (der1, {maxidx = max1, shyps = shyps1, hyps = hyps1, tpairs = tpairs1,
wenzelm@28321
   902
      prop = prop1, ...}) = th1
wenzelm@28321
   903
    and Thm (der2, {maxidx = max2, shyps = shyps2, hyps = hyps2, tpairs = tpairs2,
wenzelm@28321
   904
      prop = prop2, ...}) = th2;
wenzelm@16601
   905
    fun chktypes fT tT =
wenzelm@16601
   906
      (case fT of
wenzelm@32784
   907
        Type ("fun", [T1, _]) =>
wenzelm@16601
   908
          if T1 <> tT then
wenzelm@16601
   909
            raise THM ("combination: types", 0, [th1, th2])
wenzelm@16601
   910
          else ()
wenzelm@16601
   911
      | _ => raise THM ("combination: not function type", 0, [th1, th2]));
wenzelm@16601
   912
  in
wenzelm@52788
   913
    (case (prop1, prop2) of
wenzelm@56245
   914
      (Const ("Pure.eq", Type ("fun", [fT, _])) $ f $ g,
wenzelm@56245
   915
       Const ("Pure.eq", Type ("fun", [tT, _])) $ t $ u) =>
wenzelm@16601
   916
        (chktypes fT tT;
wenzelm@52487
   917
          Thm (deriv_rule2 (Proofterm.combination f g t u fT) der1 der2,
wenzelm@52788
   918
           {thy = merge_thys2 th1 th2,
wenzelm@21646
   919
            tags = [],
wenzelm@16601
   920
            maxidx = Int.max (max1, max2),
wenzelm@16601
   921
            shyps = Sorts.union shyps1 shyps2,
wenzelm@16601
   922
            hyps = union_hyps hyps1 hyps2,
wenzelm@16601
   923
            tpairs = union_tpairs tpairs1 tpairs2,
wenzelm@28321
   924
            prop = Logic.mk_equals (f $ t, g $ u)}))
wenzelm@52788
   925
     | _ => raise THM ("combination: premises", 0, [th1, th2]))
clasohm@0
   926
  end;
clasohm@0
   927
wenzelm@16601
   928
(*Equality introduction
wenzelm@3529
   929
  A ==> B  B ==> A
wenzelm@3529
   930
  ----------------
wenzelm@3529
   931
       A == B
wenzelm@1220
   932
*)
clasohm@0
   933
fun equal_intr th1 th2 =
wenzelm@16601
   934
  let
wenzelm@28321
   935
    val Thm (der1, {maxidx = max1, shyps = shyps1, hyps = hyps1, tpairs = tpairs1,
wenzelm@28321
   936
      prop = prop1, ...}) = th1
wenzelm@28321
   937
    and Thm (der2, {maxidx = max2, shyps = shyps2, hyps = hyps2, tpairs = tpairs2,
wenzelm@28321
   938
      prop = prop2, ...}) = th2;
wenzelm@16601
   939
    fun err msg = raise THM ("equal_intr: " ^ msg, 0, [th1, th2]);
wenzelm@16601
   940
  in
wenzelm@52788
   941
    (case (prop1, prop2) of
wenzelm@56245
   942
      (Const("Pure.imp", _) $ A $ B, Const("Pure.imp", _) $ B' $ A') =>
wenzelm@16601
   943
        if A aconv A' andalso B aconv B' then
wenzelm@52487
   944
          Thm (deriv_rule2 (Proofterm.equal_intr A B) der1 der2,
wenzelm@52788
   945
           {thy = merge_thys2 th1 th2,
wenzelm@21646
   946
            tags = [],
wenzelm@16601
   947
            maxidx = Int.max (max1, max2),
wenzelm@16601
   948
            shyps = Sorts.union shyps1 shyps2,
wenzelm@16601
   949
            hyps = union_hyps hyps1 hyps2,
wenzelm@16601
   950
            tpairs = union_tpairs tpairs1 tpairs2,
wenzelm@28321
   951
            prop = Logic.mk_equals (A, B)})
wenzelm@16601
   952
        else err "not equal"
wenzelm@52788
   953
    | _ =>  err "premises")
paulson@1529
   954
  end;
paulson@1529
   955
paulson@1529
   956
(*The equal propositions rule
wenzelm@3529
   957
  A == B  A
paulson@1529
   958
  ---------
paulson@1529
   959
      B
paulson@1529
   960
*)
paulson@1529
   961
fun equal_elim th1 th2 =
wenzelm@16601
   962
  let
wenzelm@28321
   963
    val Thm (der1, {maxidx = max1, shyps = shyps1, hyps = hyps1,
wenzelm@28321
   964
      tpairs = tpairs1, prop = prop1, ...}) = th1
wenzelm@28321
   965
    and Thm (der2, {maxidx = max2, shyps = shyps2, hyps = hyps2,
wenzelm@28321
   966
      tpairs = tpairs2, prop = prop2, ...}) = th2;
wenzelm@16601
   967
    fun err msg = raise THM ("equal_elim: " ^ msg, 0, [th1, th2]);
wenzelm@16601
   968
  in
wenzelm@52788
   969
    (case prop1 of
wenzelm@56245
   970
      Const ("Pure.eq", _) $ A $ B =>
wenzelm@16601
   971
        if prop2 aconv A then
wenzelm@52487
   972
          Thm (deriv_rule2 (Proofterm.equal_elim A B) der1 der2,
wenzelm@52788
   973
           {thy = merge_thys2 th1 th2,
wenzelm@21646
   974
            tags = [],
wenzelm@16601
   975
            maxidx = Int.max (max1, max2),
wenzelm@16601
   976
            shyps = Sorts.union shyps1 shyps2,
wenzelm@16601
   977
            hyps = union_hyps hyps1 hyps2,
wenzelm@16601
   978
            tpairs = union_tpairs tpairs1 tpairs2,
wenzelm@28321
   979
            prop = B})
wenzelm@16601
   980
        else err "not equal"
wenzelm@52789
   981
     | _ =>  err "major premise")
paulson@1529
   982
  end;
clasohm@0
   983
wenzelm@1220
   984
wenzelm@1220
   985
clasohm@0
   986
(**** Derived rules ****)
clasohm@0
   987
wenzelm@16601
   988
(*Smash unifies the list of term pairs leaving no flex-flex pairs.
wenzelm@24143
   989
  Instantiates the theorem and deletes trivial tpairs.  Resulting
wenzelm@24143
   990
  sequence may contain multiple elements if the tpairs are not all
wenzelm@24143
   991
  flex-flex.*)
wenzelm@52788
   992
fun flexflex_rule (th as Thm (der, {thy, maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@52788
   993
  Unify.smash_unifiers thy tpairs (Envir.empty maxidx)
wenzelm@52788
   994
  |> Seq.map (fn env =>
wenzelm@52788
   995
      if Envir.is_empty env then th
wenzelm@52788
   996
      else
wenzelm@52788
   997
        let
wenzelm@52788
   998
          val tpairs' = tpairs |> map (pairself (Envir.norm_term env))
wenzelm@52788
   999
            (*remove trivial tpairs, of the form t==t*)
wenzelm@52788
  1000
            |> filter_out (op aconv);
wenzelm@52788
  1001
          val der' = deriv_rule1 (Proofterm.norm_proof' env) der;
wenzelm@52788
  1002
          val prop' = Envir.norm_term env prop;
wenzelm@52788
  1003
          val maxidx = maxidx_tpairs tpairs' (maxidx_of_term prop');
wenzelm@52788
  1004
          val shyps = Envir.insert_sorts env shyps;
wenzelm@52788
  1005
        in
wenzelm@52788
  1006
          Thm (der', {thy = thy, tags = [], maxidx = maxidx,
wenzelm@52788
  1007
            shyps = shyps, hyps = hyps, tpairs = tpairs', prop = prop'})
wenzelm@52788
  1008
        end);
wenzelm@16601
  1009
clasohm@0
  1010
wenzelm@19910
  1011
(*Generalization of fixed variables
wenzelm@19910
  1012
           A
wenzelm@19910
  1013
  --------------------
wenzelm@19910
  1014
  A[?'a/'a, ?x/x, ...]
wenzelm@19910
  1015
*)
wenzelm@19910
  1016
wenzelm@19910
  1017
fun generalize ([], []) _ th = th
wenzelm@19910
  1018
  | generalize (tfrees, frees) idx th =
wenzelm@19910
  1019
      let
wenzelm@52788
  1020
        val Thm (der, {thy, maxidx, shyps, hyps, tpairs, prop, ...}) = th;
wenzelm@19910
  1021
        val _ = idx <= maxidx andalso raise THM ("generalize: bad index", idx, [th]);
wenzelm@19910
  1022
wenzelm@33697
  1023
        val bad_type =
wenzelm@33697
  1024
          if null tfrees then K false
wenzelm@33697
  1025
          else Term.exists_subtype (fn TFree (a, _) => member (op =) tfrees a | _ => false);
wenzelm@19910
  1026
        fun bad_term (Free (x, T)) = bad_type T orelse member (op =) frees x
wenzelm@19910
  1027
          | bad_term (Var (_, T)) = bad_type T
wenzelm@19910
  1028
          | bad_term (Const (_, T)) = bad_type T
wenzelm@19910
  1029
          | bad_term (Abs (_, T, t)) = bad_type T orelse bad_term t
wenzelm@19910
  1030
          | bad_term (t $ u) = bad_term t orelse bad_term u
wenzelm@19910
  1031
          | bad_term (Bound _) = false;
wenzelm@19910
  1032
        val _ = exists bad_term hyps andalso
wenzelm@19910
  1033
          raise THM ("generalize: variable free in assumptions", 0, [th]);
wenzelm@19910
  1034
wenzelm@31977
  1035
        val gen = Term_Subst.generalize (tfrees, frees) idx;
wenzelm@19910
  1036
        val prop' = gen prop;
wenzelm@19910
  1037
        val tpairs' = map (pairself gen) tpairs;
wenzelm@19910
  1038
        val maxidx' = maxidx_tpairs tpairs' (maxidx_of_term prop');
wenzelm@19910
  1039
      in
wenzelm@52487
  1040
        Thm (deriv_rule1 (Proofterm.generalize (tfrees, frees) idx) der,
wenzelm@52788
  1041
         {thy = thy,
wenzelm@21646
  1042
          tags = [],
wenzelm@19910
  1043
          maxidx = maxidx',
wenzelm@19910
  1044
          shyps = shyps,
wenzelm@19910
  1045
          hyps = hyps,
wenzelm@19910
  1046
          tpairs = tpairs',
wenzelm@28321
  1047
          prop = prop'})
wenzelm@19910
  1048
      end;
wenzelm@19910
  1049
wenzelm@19910
  1050
wenzelm@22584
  1051
(*Instantiation of schematic variables
wenzelm@16656
  1052
           A
wenzelm@16656
  1053
  --------------------
wenzelm@16656
  1054
  A[t1/v1, ..., tn/vn]
wenzelm@1220
  1055
*)
clasohm@0
  1056
wenzelm@6928
  1057
local
wenzelm@6928
  1058
wenzelm@26939
  1059
fun pretty_typing thy t T = Pretty.block
wenzelm@26939
  1060
  [Syntax.pretty_term_global thy t, Pretty.str " ::", Pretty.brk 1, Syntax.pretty_typ_global thy T];
berghofe@15797
  1061
wenzelm@52788
  1062
fun add_inst (ct, cu) (thy, sorts) =
wenzelm@6928
  1063
  let
wenzelm@26939
  1064
    val Cterm {t = t, T = T, ...} = ct;
wenzelm@26939
  1065
    val Cterm {t = u, T = U, sorts = sorts_u, maxidx = maxidx_u, ...} = cu;
wenzelm@52788
  1066
    val thy' = Theory.merge (thy, merge_thys0 ct cu);
wenzelm@16884
  1067
    val sorts' = Sorts.union sorts_u sorts;
wenzelm@3967
  1068
  in
wenzelm@16884
  1069
    (case t of Var v =>
wenzelm@52788
  1070
      if T = U then ((v, (u, maxidx_u)), (thy', sorts'))
wenzelm@16884
  1071
      else raise TYPE (Pretty.string_of (Pretty.block
wenzelm@16884
  1072
       [Pretty.str "instantiate: type conflict",
wenzelm@52788
  1073
        Pretty.fbrk, pretty_typing thy' t T,
wenzelm@52788
  1074
        Pretty.fbrk, pretty_typing thy' u U]), [T, U], [t, u])
wenzelm@16884
  1075
    | _ => raise TYPE (Pretty.string_of (Pretty.block
wenzelm@16884
  1076
       [Pretty.str "instantiate: not a variable",
wenzelm@52788
  1077
        Pretty.fbrk, Syntax.pretty_term_global thy' t]), [], [t]))
clasohm@0
  1078
  end;
clasohm@0
  1079
wenzelm@52788
  1080
fun add_instT (cT, cU) (thy, sorts) =
wenzelm@16656
  1081
  let
wenzelm@52788
  1082
    val Ctyp {T, thy = thy1, ...} = cT
wenzelm@52788
  1083
    and Ctyp {T = U, thy = thy2, sorts = sorts_U, maxidx = maxidx_U, ...} = cU;
wenzelm@52788
  1084
    val thy' = Theory.merge (thy, Theory.merge (thy1, thy2));
wenzelm@16884
  1085
    val sorts' = Sorts.union sorts_U sorts;
wenzelm@16656
  1086
  in
wenzelm@16884
  1087
    (case T of TVar (v as (_, S)) =>
wenzelm@52788
  1088
      if Sign.of_sort thy' (U, S) then ((v, (U, maxidx_U)), (thy', sorts'))
wenzelm@26939
  1089
      else raise TYPE ("Type not of sort " ^ Syntax.string_of_sort_global thy' S, [U], [])
wenzelm@16656
  1090
    | _ => raise TYPE (Pretty.string_of (Pretty.block
berghofe@15797
  1091
        [Pretty.str "instantiate: not a type variable",
wenzelm@26939
  1092
         Pretty.fbrk, Syntax.pretty_typ_global thy' T]), [T], []))
wenzelm@16656
  1093
  end;
clasohm@0
  1094
wenzelm@6928
  1095
in
wenzelm@6928
  1096
wenzelm@16601
  1097
(*Left-to-right replacements: ctpairs = [..., (vi, ti), ...].
clasohm@0
  1098
  Instantiates distinct Vars by terms of same type.
wenzelm@16601
  1099
  Does NOT normalize the resulting theorem!*)
paulson@1529
  1100
fun instantiate ([], []) th = th
wenzelm@16884
  1101
  | instantiate (instT, inst) th =
wenzelm@16656
  1102
      let
wenzelm@52788
  1103
        val Thm (der, {thy, hyps, shyps, tpairs, prop, ...}) = th;
wenzelm@52788
  1104
        val (inst', (instT', (thy', shyps'))) =
wenzelm@52788
  1105
          (thy, shyps) |> fold_map add_inst inst ||> fold_map add_instT instT;
wenzelm@31977
  1106
        val subst = Term_Subst.instantiate_maxidx (instT', inst');
wenzelm@20512
  1107
        val (prop', maxidx1) = subst prop ~1;
wenzelm@20512
  1108
        val (tpairs', maxidx') =
wenzelm@20512
  1109
          fold_map (fn (t, u) => fn i => subst t i ||>> subst u) tpairs maxidx1;
wenzelm@16656
  1110
      in
wenzelm@52487
  1111
        Thm (deriv_rule1
wenzelm@37309
  1112
          (fn d => Proofterm.instantiate (map (apsnd #1) instT', map (apsnd #1) inst') d) der,
wenzelm@52788
  1113
         {thy = thy',
wenzelm@21646
  1114
          tags = [],
wenzelm@20545
  1115
          maxidx = maxidx',
wenzelm@20545
  1116
          shyps = shyps',
wenzelm@20545
  1117
          hyps = hyps,
wenzelm@20545
  1118
          tpairs = tpairs',
wenzelm@28321
  1119
          prop = prop'})
wenzelm@16656
  1120
      end
wenzelm@16656
  1121
      handle TYPE (msg, _, _) => raise THM (msg, 0, [th]);
wenzelm@6928
  1122
wenzelm@22584
  1123
fun instantiate_cterm ([], []) ct = ct
wenzelm@22584
  1124
  | instantiate_cterm (instT, inst) ct =
wenzelm@22584
  1125
      let
wenzelm@52788
  1126
        val Cterm {thy, t, T, sorts, ...} = ct;
wenzelm@52788
  1127
        val (inst', (instT', (thy', sorts'))) =
wenzelm@52788
  1128
          (thy, sorts) |> fold_map add_inst inst ||> fold_map add_instT instT;
wenzelm@31977
  1129
        val subst = Term_Subst.instantiate_maxidx (instT', inst');
wenzelm@31977
  1130
        val substT = Term_Subst.instantiateT_maxidx instT';
wenzelm@22584
  1131
        val (t', maxidx1) = subst t ~1;
wenzelm@22584
  1132
        val (T', maxidx') = substT T maxidx1;
wenzelm@52788
  1133
      in Cterm {thy = thy', t = t', T = T', sorts = sorts', maxidx = maxidx'} end
wenzelm@22584
  1134
      handle TYPE (msg, _, _) => raise CTERM (msg, [ct]);
wenzelm@22584
  1135
wenzelm@6928
  1136
end;
wenzelm@6928
  1137
clasohm@0
  1138
wenzelm@16601
  1139
(*The trivial implication A ==> A, justified by assume and forall rules.
wenzelm@16601
  1140
  A can contain Vars, not so for assume!*)
wenzelm@52788
  1141
fun trivial (Cterm {thy, t = A, T, maxidx, sorts}) =
wenzelm@16601
  1142
  if T <> propT then
wenzelm@16601
  1143
    raise THM ("trivial: the term must have type prop", 0, [])
wenzelm@16601
  1144
  else
wenzelm@52487
  1145
    Thm (deriv_rule0 (Proofterm.AbsP ("H", NONE, Proofterm.PBound 0)),
wenzelm@52788
  1146
     {thy = thy,
wenzelm@21646
  1147
      tags = [],
wenzelm@16601
  1148
      maxidx = maxidx,
wenzelm@16601
  1149
      shyps = sorts,
wenzelm@16601
  1150
      hyps = [],
wenzelm@16601
  1151
      tpairs = [],
wenzelm@28321
  1152
      prop = Logic.mk_implies (A, A)});
clasohm@0
  1153
wenzelm@31944
  1154
(*Axiom-scheme reflecting signature contents
wenzelm@31944
  1155
        T :: c
wenzelm@31944
  1156
  -------------------
wenzelm@31944
  1157
  OFCLASS(T, c_class)
wenzelm@31944
  1158
*)
wenzelm@31944
  1159
fun of_class (cT, raw_c) =
wenzelm@24143
  1160
  let
wenzelm@52788
  1161
    val Ctyp {thy, T, ...} = cT;
wenzelm@31903
  1162
    val c = Sign.certify_class thy raw_c;
wenzelm@31944
  1163
    val Cterm {t = prop, maxidx, sorts, ...} = cterm_of thy (Logic.mk_of_class (T, c));
wenzelm@399
  1164
  in
wenzelm@31944
  1165
    if Sign.of_sort thy (T, [c]) then
wenzelm@52487
  1166
      Thm (deriv_rule0 (Proofterm.OfClass (T, c)),
wenzelm@52788
  1167
       {thy = thy,
wenzelm@31944
  1168
        tags = [],
wenzelm@31944
  1169
        maxidx = maxidx,
wenzelm@31944
  1170
        shyps = sorts,
wenzelm@31944
  1171
        hyps = [],
wenzelm@31944
  1172
        tpairs = [],
wenzelm@31944
  1173
        prop = prop})
wenzelm@31944
  1174
    else raise THM ("of_class: type not of class " ^ Syntax.string_of_sort_global thy [c], 0, [])
wenzelm@399
  1175
  end;
wenzelm@399
  1176
wenzelm@36614
  1177
(*Remove extra sorts that are witnessed by type signature information*)
wenzelm@36614
  1178
fun strip_shyps (thm as Thm (_, {shyps = [], ...})) = thm
wenzelm@52788
  1179
  | strip_shyps (thm as Thm (der, {thy, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@36614
  1180
      let
wenzelm@36621
  1181
        val algebra = Sign.classes_of thy;
wenzelm@36621
  1182
wenzelm@36621
  1183
        val present = (fold_terms o fold_types o fold_atyps_sorts) (insert (eq_fst op =)) thm [];
wenzelm@36614
  1184
        val extra = fold (Sorts.remove_sort o #2) present shyps;
wenzelm@36614
  1185
        val witnessed = Sign.witness_sorts thy present extra;
wenzelm@36614
  1186
        val extra' = fold (Sorts.remove_sort o #2) witnessed extra
wenzelm@36621
  1187
          |> Sorts.minimal_sorts algebra;
wenzelm@36614
  1188
        val shyps' = fold (Sorts.insert_sort o #2) present extra';
wenzelm@36614
  1189
      in
wenzelm@37309
  1190
        Thm (deriv_rule_unconditional
wenzelm@37309
  1191
          (Proofterm.strip_shyps_proof algebra present witnessed extra') der,
wenzelm@52788
  1192
         {thy = thy, tags = tags, maxidx = maxidx,
wenzelm@36614
  1193
          shyps = shyps', hyps = hyps, tpairs = tpairs, prop = prop})
wenzelm@36614
  1194
      end;
wenzelm@36614
  1195
wenzelm@36769
  1196
(*Internalize sort constraints of type variables*)
wenzelm@36883
  1197
fun unconstrainT (thm as Thm (der, args)) =
wenzelm@19505
  1198
  let
wenzelm@36883
  1199
    val Deriv {promises, body} = der;
wenzelm@52788
  1200
    val {thy, shyps, hyps, tpairs, prop, ...} = args;
wenzelm@36883
  1201
wenzelm@36769
  1202
    fun err msg = raise THM ("unconstrainT: " ^ msg, 0, [thm]);
wenzelm@36769
  1203
    val _ = null hyps orelse err "illegal hyps";
wenzelm@36769
  1204
    val _ = null tpairs orelse err "unsolved flex-flex constraints";
wenzelm@36769
  1205
    val tfrees = rev (Term.add_tfree_names prop []);
wenzelm@36769
  1206
    val _ = null tfrees orelse err ("illegal free type variables " ^ commas_quote tfrees);
wenzelm@36769
  1207
wenzelm@41700
  1208
    val ps = map (apsnd (Future.map fulfill_body)) promises;
wenzelm@37309
  1209
    val (pthm as (_, (_, prop', _)), proof) =
wenzelm@37309
  1210
      Proofterm.unconstrain_thm_proof thy shyps prop ps body;
wenzelm@36883
  1211
    val der' = make_deriv [] [] [pthm] proof;
wenzelm@19505
  1212
  in
wenzelm@36883
  1213
    Thm (der',
wenzelm@52788
  1214
     {thy = thy,
wenzelm@21646
  1215
      tags = [],
wenzelm@36769
  1216
      maxidx = maxidx_of_term prop',
wenzelm@36769
  1217
      shyps = [[]],  (*potentially redundant*)
wenzelm@36883
  1218
      hyps = [],
wenzelm@36883
  1219
      tpairs = [],
wenzelm@36769
  1220
      prop = prop'})
wenzelm@19505
  1221
  end;
wenzelm@399
  1222
wenzelm@6786
  1223
(* Replace all TFrees not fixed or in the hyps by new TVars *)
wenzelm@52788
  1224
fun varifyT_global' fixed (Thm (der, {thy, maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@12500
  1225
  let
wenzelm@29272
  1226
    val tfrees = fold Term.add_tfrees hyps fixed;
berghofe@13658
  1227
    val prop1 = attach_tpairs tpairs prop;
wenzelm@35845
  1228
    val (al, prop2) = Type.varify_global tfrees prop1;
wenzelm@16601
  1229
    val (ts, prop3) = Logic.strip_prems (length tpairs, [], prop2);
wenzelm@16601
  1230
  in
wenzelm@52487
  1231
    (al, Thm (deriv_rule1 (Proofterm.varify_proof prop tfrees) der,
wenzelm@52788
  1232
     {thy = thy,
wenzelm@21646
  1233
      tags = [],
wenzelm@16601
  1234
      maxidx = Int.max (0, maxidx),
wenzelm@16601
  1235
      shyps = shyps,
wenzelm@16601
  1236
      hyps = hyps,
wenzelm@16601
  1237
      tpairs = rev (map Logic.dest_equals ts),
wenzelm@28321
  1238
      prop = prop3}))
wenzelm@28321
  1239
  end;
wenzelm@28321
  1240
wenzelm@35845
  1241
val varifyT_global = #2 o varifyT_global' [];
wenzelm@28321
  1242
wenzelm@36615
  1243
(* Replace all TVars by TFrees that are often new *)
wenzelm@52788
  1244
fun legacy_freezeT (Thm (der, {thy, shyps, hyps, tpairs, prop, ...})) =
wenzelm@28321
  1245
  let
wenzelm@28321
  1246
    val prop1 = attach_tpairs tpairs prop;
wenzelm@33832
  1247
    val prop2 = Type.legacy_freeze prop1;
wenzelm@28321
  1248
    val (ts, prop3) = Logic.strip_prems (length tpairs, [], prop2);
wenzelm@28321
  1249
  in
wenzelm@52487
  1250
    Thm (deriv_rule1 (Proofterm.legacy_freezeT prop1) der,
wenzelm@52788
  1251
     {thy = thy,
wenzelm@28321
  1252
      tags = [],
wenzelm@28321
  1253
      maxidx = maxidx_of_term prop2,
wenzelm@28321
  1254
      shyps = shyps,
wenzelm@28321
  1255
      hyps = hyps,
wenzelm@28321
  1256
      tpairs = rev (map Logic.dest_equals ts),
wenzelm@18127
  1257
      prop = prop3})
clasohm@0
  1258
  end;
clasohm@0
  1259
clasohm@0
  1260
clasohm@0
  1261
(*** Inference rules for tactics ***)
clasohm@0
  1262
clasohm@0
  1263
(*Destruct proof state into constraints, other goals, goal(i), rest *)
wenzelm@28321
  1264
fun dest_state (state as Thm (_, {prop,tpairs,...}), i) =
berghofe@13658
  1265
  (case  Logic.strip_prems(i, [], prop) of
berghofe@13658
  1266
      (B::rBs, C) => (tpairs, rev rBs, B, C)
berghofe@13658
  1267
    | _ => raise THM("dest_state", i, [state]))
clasohm@0
  1268
  handle TERM _ => raise THM("dest_state", i, [state]);
clasohm@0
  1269
wenzelm@46255
  1270
(*Prepare orule for resolution by lifting it over the parameters and
wenzelm@52487
  1271
assumptions of goal.*)
wenzelm@18035
  1272
fun lift_rule goal orule =
wenzelm@16601
  1273
  let
wenzelm@18035
  1274
    val Cterm {t = gprop, T, maxidx = gmax, sorts, ...} = goal;
wenzelm@18035
  1275
    val inc = gmax + 1;
wenzelm@18035
  1276
    val lift_abs = Logic.lift_abs inc gprop;
wenzelm@18035
  1277
    val lift_all = Logic.lift_all inc gprop;
wenzelm@28321
  1278
    val Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...}) = orule;
wenzelm@16601
  1279
    val (As, B) = Logic.strip_horn prop;
wenzelm@16601
  1280
  in
wenzelm@18035
  1281
    if T <> propT then raise THM ("lift_rule: the term must have type prop", 0, [])
wenzelm@18035
  1282
    else
wenzelm@52487
  1283
      Thm (deriv_rule1 (Proofterm.lift_proof gprop inc prop) der,
wenzelm@52788
  1284
       {thy = merge_thys1 goal orule,
wenzelm@21646
  1285
        tags = [],
wenzelm@18035
  1286
        maxidx = maxidx + inc,
wenzelm@18035
  1287
        shyps = Sorts.union shyps sorts,  (*sic!*)
wenzelm@18035
  1288
        hyps = hyps,
wenzelm@18035
  1289
        tpairs = map (pairself lift_abs) tpairs,
wenzelm@28321
  1290
        prop = Logic.list_implies (map lift_all As, lift_all B)})
clasohm@0
  1291
  end;
clasohm@0
  1292
wenzelm@52788
  1293
fun incr_indexes i (thm as Thm (der, {thy, maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@16601
  1294
  if i < 0 then raise THM ("negative increment", 0, [thm])
wenzelm@16601
  1295
  else if i = 0 then thm
wenzelm@16601
  1296
  else
wenzelm@52487
  1297
    Thm (deriv_rule1 (Proofterm.incr_indexes i) der,
wenzelm@52788
  1298
     {thy = thy,
wenzelm@21646
  1299
      tags = [],
wenzelm@16601
  1300
      maxidx = maxidx + i,
wenzelm@16601
  1301
      shyps = shyps,
wenzelm@16601
  1302
      hyps = hyps,
wenzelm@16601
  1303
      tpairs = map (pairself (Logic.incr_indexes ([], i))) tpairs,
wenzelm@28321
  1304
      prop = Logic.incr_indexes ([], i) prop});
berghofe@10416
  1305
clasohm@0
  1306
(*Solve subgoal Bi of proof state B1...Bn/C by assumption. *)
clasohm@0
  1307
fun assumption i state =
wenzelm@16601
  1308
  let
wenzelm@52788
  1309
    val Thm (der, {thy, maxidx, shyps, hyps, ...}) = state;
wenzelm@16601
  1310
    val (tpairs, Bs, Bi, C) = dest_state (state, i);
wenzelm@32032
  1311
    fun newth n (env, tpairs) =
wenzelm@52487
  1312
      Thm (deriv_rule1
wenzelm@37309
  1313
          ((if Envir.is_empty env then I else (Proofterm.norm_proof' env)) o
wenzelm@37309
  1314
            Proofterm.assumption_proof Bs Bi n) der,
wenzelm@28321
  1315
       {tags = [],
wenzelm@32032
  1316
        maxidx = Envir.maxidx_of env,
wenzelm@26640
  1317
        shyps = Envir.insert_sorts env shyps,
wenzelm@16601
  1318
        hyps = hyps,
wenzelm@16601
  1319
        tpairs =
wenzelm@16601
  1320
          if Envir.is_empty env then tpairs
wenzelm@16601
  1321
          else map (pairself (Envir.norm_term env)) tpairs,
wenzelm@16601
  1322
        prop =
wenzelm@16601
  1323
          if Envir.is_empty env then (*avoid wasted normalizations*)
wenzelm@16601
  1324
            Logic.list_implies (Bs, C)
wenzelm@16601
  1325
          else (*normalize the new rule fully*)
wenzelm@24143
  1326
            Envir.norm_term env (Logic.list_implies (Bs, C)),
wenzelm@52788
  1327
        thy = thy});
wenzelm@30554
  1328
wenzelm@30556
  1329
    val (close, asms, concl) = Logic.assum_problems (~1, Bi);
wenzelm@30556
  1330
    val concl' = close concl;
wenzelm@16601
  1331
    fun addprfs [] _ = Seq.empty
wenzelm@30556
  1332
      | addprfs (asm :: rest) n = Seq.make (fn () => Seq.pull
wenzelm@16601
  1333
          (Seq.mapp (newth n)
wenzelm@30556
  1334
            (if Term.could_unify (asm, concl) then
wenzelm@30556
  1335
              (Unify.unifiers (thy, Envir.empty maxidx, (close asm, concl') :: tpairs))
wenzelm@30554
  1336
             else Seq.empty)
wenzelm@30554
  1337
            (addprfs rest (n + 1))))
wenzelm@30556
  1338
  in addprfs asms 1 end;
clasohm@0
  1339
wenzelm@250
  1340
(*Solve subgoal Bi of proof state B1...Bn/C by assumption.
wenzelm@51604
  1341
  Checks if Bi's conclusion is alpha/eta-convertible to one of its assumptions*)
clasohm@0
  1342
fun eq_assumption i state =
wenzelm@16601
  1343
  let
wenzelm@52788
  1344
    val Thm (der, {thy, maxidx, shyps, hyps, ...}) = state;
wenzelm@16601
  1345
    val (tpairs, Bs, Bi, C) = dest_state (state, i);
wenzelm@30556
  1346
    val (_, asms, concl) = Logic.assum_problems (~1, Bi);
wenzelm@16601
  1347
  in
wenzelm@52131
  1348
    (case find_index (fn asm => Envir.aeconv (asm, concl)) asms of
wenzelm@16601
  1349
      ~1 => raise THM ("eq_assumption", 0, [state])
wenzelm@16601
  1350
    | n =>
wenzelm@52487
  1351
        Thm (deriv_rule1 (Proofterm.assumption_proof Bs Bi (n + 1)) der,
wenzelm@52788
  1352
         {thy = thy,
wenzelm@21646
  1353
          tags = [],
wenzelm@16601
  1354
          maxidx = maxidx,
wenzelm@16601
  1355
          shyps = shyps,
wenzelm@16601
  1356
          hyps = hyps,
wenzelm@16601
  1357
          tpairs = tpairs,
wenzelm@28321
  1358
          prop = Logic.list_implies (Bs, C)}))
clasohm@0
  1359
  end;
clasohm@0
  1360
clasohm@0
  1361
paulson@2671
  1362
(*For rotate_tac: fast rotation of assumptions of subgoal i*)
paulson@2671
  1363
fun rotate_rule k i state =
wenzelm@16601
  1364
  let
wenzelm@52788
  1365
    val Thm (der, {thy, maxidx, shyps, hyps, ...}) = state;
wenzelm@16601
  1366
    val (tpairs, Bs, Bi, C) = dest_state (state, i);
wenzelm@46218
  1367
    val params = Term.strip_all_vars Bi;
wenzelm@46218
  1368
    val rest = Term.strip_all_body Bi;
wenzelm@46218
  1369
    val asms = Logic.strip_imp_prems rest
wenzelm@46218
  1370
    val concl = Logic.strip_imp_concl rest;
wenzelm@16601
  1371
    val n = length asms;
wenzelm@16601
  1372
    val m = if k < 0 then n + k else k;
wenzelm@16601
  1373
    val Bi' =
wenzelm@16601
  1374
      if 0 = m orelse m = n then Bi
wenzelm@16601
  1375
      else if 0 < m andalso m < n then
wenzelm@19012
  1376
        let val (ps, qs) = chop m asms
wenzelm@46218
  1377
        in Logic.list_all (params, Logic.list_implies (qs @ ps, concl)) end
wenzelm@16601
  1378
      else raise THM ("rotate_rule", k, [state]);
wenzelm@16601
  1379
  in
wenzelm@52487
  1380
    Thm (deriv_rule1 (Proofterm.rotate_proof Bs Bi m) der,
wenzelm@52788
  1381
     {thy = thy,
wenzelm@21646
  1382
      tags = [],
wenzelm@16601
  1383
      maxidx = maxidx,
wenzelm@16601
  1384
      shyps = shyps,
wenzelm@16601
  1385
      hyps = hyps,
wenzelm@16601
  1386
      tpairs = tpairs,
wenzelm@28321
  1387
      prop = Logic.list_implies (Bs @ [Bi'], C)})
paulson@2671
  1388
  end;
paulson@2671
  1389
paulson@2671
  1390
paulson@7248
  1391
(*Rotates a rule's premises to the left by k, leaving the first j premises
paulson@7248
  1392
  unchanged.  Does nothing if k=0 or if k equals n-j, where n is the
wenzelm@16656
  1393
  number of premises.  Useful with etac and underlies defer_tac*)
paulson@7248
  1394
fun permute_prems j k rl =
wenzelm@16601
  1395
  let
wenzelm@52788
  1396
    val Thm (der, {thy, maxidx, shyps, hyps, tpairs, prop, ...}) = rl;
wenzelm@16601
  1397
    val prems = Logic.strip_imp_prems prop
wenzelm@16601
  1398
    and concl = Logic.strip_imp_concl prop;
wenzelm@16601
  1399
    val moved_prems = List.drop (prems, j)
wenzelm@16601
  1400
    and fixed_prems = List.take (prems, j)
wenzelm@43278
  1401
      handle General.Subscript => raise THM ("permute_prems: j", j, [rl]);
wenzelm@16601
  1402
    val n_j = length moved_prems;
wenzelm@16601
  1403
    val m = if k < 0 then n_j + k else k;
wenzelm@16601
  1404
    val prop' =
wenzelm@16601
  1405
      if 0 = m orelse m = n_j then prop
wenzelm@16601
  1406
      else if 0 < m andalso m < n_j then
wenzelm@19012
  1407
        let val (ps, qs) = chop m moved_prems
wenzelm@16601
  1408
        in Logic.list_implies (fixed_prems @ qs @ ps, concl) end
wenzelm@16725
  1409
      else raise THM ("permute_prems: k", k, [rl]);
wenzelm@16601
  1410
  in
wenzelm@52487
  1411
    Thm (deriv_rule1 (Proofterm.permute_prems_proof prems j m) der,
wenzelm@52788
  1412
     {thy = thy,
wenzelm@21646
  1413
      tags = [],
wenzelm@16601
  1414
      maxidx = maxidx,
wenzelm@16601
  1415
      shyps = shyps,
wenzelm@16601
  1416
      hyps = hyps,
wenzelm@16601
  1417
      tpairs = tpairs,
wenzelm@28321
  1418
      prop = prop'})
paulson@7248
  1419
  end;
paulson@7248
  1420
paulson@7248
  1421
clasohm@0
  1422
(** User renaming of parameters in a subgoal **)
clasohm@0
  1423
clasohm@0
  1424
(*Calls error rather than raising an exception because it is intended
clasohm@0
  1425
  for top-level use -- exception handling would not make sense here.
clasohm@0
  1426
  The names in cs, if distinct, are used for the innermost parameters;
wenzelm@17868
  1427
  preceding parameters may be renamed to make all params distinct.*)
clasohm@0
  1428
fun rename_params_rule (cs, i) state =
wenzelm@16601
  1429
  let
wenzelm@52788
  1430
    val Thm (der, {thy, tags, maxidx, shyps, hyps, ...}) = state;
wenzelm@16601
  1431
    val (tpairs, Bs, Bi, C) = dest_state (state, i);
wenzelm@16601
  1432
    val iparams = map #1 (Logic.strip_params Bi);
wenzelm@16601
  1433
    val short = length iparams - length cs;
wenzelm@16601
  1434
    val newnames =
wenzelm@16601
  1435
      if short < 0 then error "More names than abstractions!"
haftmann@33957
  1436
      else Name.variant_list cs (take short iparams) @ cs;
wenzelm@20330
  1437
    val freenames = Term.fold_aterms (fn Free (x, _) => insert (op =) x | _ => I) Bi [];
wenzelm@45328
  1438
    val newBi = Logic.list_rename_params newnames Bi;
wenzelm@250
  1439
  in
wenzelm@21182
  1440
    (case duplicates (op =) cs of
wenzelm@21182
  1441
      a :: _ => (warning ("Can't rename.  Bound variables not distinct: " ^ a); state)
wenzelm@21182
  1442
    | [] =>
haftmann@33049
  1443
      (case inter (op =) cs freenames of
wenzelm@16601
  1444
        a :: _ => (warning ("Can't rename.  Bound/Free variable clash: " ^ a); state)
wenzelm@16601
  1445
      | [] =>
wenzelm@28321
  1446
        Thm (der,
wenzelm@52788
  1447
         {thy = thy,
wenzelm@21646
  1448
          tags = tags,
wenzelm@16601
  1449
          maxidx = maxidx,
wenzelm@16601
  1450
          shyps = shyps,
wenzelm@16601
  1451
          hyps = hyps,
wenzelm@16601
  1452
          tpairs = tpairs,
wenzelm@28321
  1453
          prop = Logic.list_implies (Bs @ [newBi], C)})))
clasohm@0
  1454
  end;
clasohm@0
  1455
wenzelm@12982
  1456
clasohm@0
  1457
(*** Preservation of bound variable names ***)
clasohm@0
  1458
wenzelm@52788
  1459
fun rename_boundvars pat obj (thm as Thm (der, {thy, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@12982
  1460
  (case Term.rename_abs pat obj prop of
skalberg@15531
  1461
    NONE => thm
wenzelm@28321
  1462
  | SOME prop' => Thm (der,
wenzelm@52788
  1463
      {thy = thy,
wenzelm@21646
  1464
       tags = tags,
wenzelm@12982
  1465
       maxidx = maxidx,
wenzelm@12982
  1466
       hyps = hyps,
wenzelm@12982
  1467
       shyps = shyps,
berghofe@13658
  1468
       tpairs = tpairs,
wenzelm@28321
  1469
       prop = prop'}));
berghofe@10416
  1470
clasohm@0
  1471
berghofe@44108
  1472
(* strip_apply f B A strips off all assumptions/parameters from A
clasohm@0
  1473
   introduced by lifting over B, and applies f to remaining part of A*)
clasohm@0
  1474
fun strip_apply f =
wenzelm@56245
  1475
  let fun strip (Const ("Pure.imp", _) $ _  $ B1)
wenzelm@56245
  1476
                (Const ("Pure.imp", _) $ A2 $ B2) = Logic.mk_implies (A2, strip B1 B2)
wenzelm@56245
  1477
        | strip ((c as Const ("Pure.all", _)) $ Abs (_, _, t1))
wenzelm@56245
  1478
                (      Const ("Pure.all", _)  $ Abs (a, T, t2)) = c $ Abs (a, T, strip t1 t2)
berghofe@44108
  1479
        | strip _ A = f A
clasohm@0
  1480
  in strip end;
clasohm@0
  1481
wenzelm@56245
  1482
fun strip_lifted (Const ("Pure.imp", _) $ _ $ B1)
wenzelm@56245
  1483
                 (Const ("Pure.imp", _) $ _ $ B2) = strip_lifted B1 B2
wenzelm@56245
  1484
  | strip_lifted (Const ("Pure.all", _) $ Abs (_, _, t1))
wenzelm@56245
  1485
                 (Const ("Pure.all", _) $ Abs (_, _, t2)) = strip_lifted t1 t2
berghofe@44108
  1486
  | strip_lifted _ A = A;
berghofe@44108
  1487
clasohm@0
  1488
(*Use the alist to rename all bound variables and some unknowns in a term
clasohm@0
  1489
  dpairs = current disagreement pairs;  tpairs = permanent ones (flexflex);
clasohm@0
  1490
  Preserves unknowns in tpairs and on lhs of dpairs. *)
berghofe@44108
  1491
fun rename_bvs [] _ _ _ _ = K I
berghofe@44108
  1492
  | rename_bvs al dpairs tpairs B As =
wenzelm@20330
  1493
      let
wenzelm@20330
  1494
        val add_var = fold_aterms (fn Var ((x, _), _) => insert (op =) x | _ => I);
wenzelm@20330
  1495
        val vids = []
wenzelm@20330
  1496
          |> fold (add_var o fst) dpairs
wenzelm@20330
  1497
          |> fold (add_var o fst) tpairs
wenzelm@20330
  1498
          |> fold (add_var o snd) tpairs;
berghofe@44108
  1499
        val vids' = fold (add_var o strip_lifted B) As [];
wenzelm@250
  1500
        (*unknowns appearing elsewhere be preserved!*)
berghofe@44108
  1501
        val al' = distinct ((op =) o pairself fst)
berghofe@44108
  1502
          (filter_out (fn (x, y) =>
berghofe@44108
  1503
             not (member (op =) vids' x) orelse
berghofe@44108
  1504
             member (op =) vids x orelse member (op =) vids y) al);
berghofe@44108
  1505
        val unchanged = filter_out (AList.defined (op =) al') vids';
berghofe@44108
  1506
        fun del_clashing clash xs _ [] qs =
berghofe@44108
  1507
              if clash then del_clashing false xs xs qs [] else qs
berghofe@44108
  1508
          | del_clashing clash xs ys ((p as (x, y)) :: ps) qs =
berghofe@44108
  1509
              if member (op =) ys y
berghofe@44108
  1510
              then del_clashing true (x :: xs) (x :: ys) ps qs
berghofe@44108
  1511
              else del_clashing clash xs (y :: ys) ps (p :: qs);
wenzelm@46497
  1512
        val al'' = del_clashing false unchanged unchanged al' [];
berghofe@44108
  1513
        fun rename (t as Var ((x, i), T)) =
berghofe@44108
  1514
              (case AList.lookup (op =) al'' x of
berghofe@44108
  1515
                 SOME y => Var ((y, i), T)
berghofe@44108
  1516
               | NONE => t)
berghofe@44108
  1517
          | rename (Abs (x, T, t)) =
wenzelm@18944
  1518
              Abs (the_default x (AList.lookup (op =) al x), T, rename t)
berghofe@44108
  1519
          | rename (f $ t) = rename f $ rename t
berghofe@44108
  1520
          | rename t = t;
berghofe@44108
  1521
        fun strip_ren f Ai = f rename B Ai
wenzelm@20330
  1522
      in strip_ren end;
clasohm@0
  1523
clasohm@0
  1524
(*Function to rename bounds/unknowns in the argument, lifted over B*)
berghofe@44108
  1525
fun rename_bvars dpairs =
wenzelm@48263
  1526
  rename_bvs (fold_rev Term.match_bvars dpairs []) dpairs;
clasohm@0
  1527
clasohm@0
  1528
clasohm@0
  1529
(*** RESOLUTION ***)
clasohm@0
  1530
lcp@721
  1531
(** Lifting optimizations **)
lcp@721
  1532
clasohm@0
  1533
(*strip off pairs of assumptions/parameters in parallel -- they are
clasohm@0
  1534
  identical because of lifting*)
wenzelm@56245
  1535
fun strip_assums2 (Const("Pure.imp", _) $ _ $ B1,
wenzelm@56245
  1536
                   Const("Pure.imp", _) $ _ $ B2) = strip_assums2 (B1,B2)
wenzelm@56245
  1537
  | strip_assums2 (Const("Pure.all",_)$Abs(a,T,t1),
wenzelm@56245
  1538
                   Const("Pure.all",_)$Abs(_,_,t2)) =
clasohm@0
  1539
      let val (B1,B2) = strip_assums2 (t1,t2)
clasohm@0
  1540
      in  (Abs(a,T,B1), Abs(a,T,B2))  end
clasohm@0
  1541
  | strip_assums2 BB = BB;
clasohm@0
  1542
clasohm@0
  1543
lcp@721
  1544
(*Faster normalization: skip assumptions that were lifted over*)
lcp@721
  1545
fun norm_term_skip env 0 t = Envir.norm_term env t
wenzelm@56245
  1546
  | norm_term_skip env n (Const ("Pure.all", _) $ Abs (a, T, t)) =
wenzelm@32032
  1547
      let
wenzelm@32035
  1548
        val T' = Envir.subst_type (Envir.type_env env) T
wenzelm@32032
  1549
        (*Must instantiate types of parameters because they are flattened;
wenzelm@32032
  1550
          this could be a NEW parameter*)
wenzelm@46217
  1551
      in Logic.all_const T' $ Abs (a, T', norm_term_skip env n t) end
wenzelm@56245
  1552
  | norm_term_skip env n (Const ("Pure.imp", _) $ A $ B) =
wenzelm@32032
  1553
      Logic.mk_implies (A, norm_term_skip env (n - 1) B)
wenzelm@32784
  1554
  | norm_term_skip _ _ _ = error "norm_term_skip: too few assumptions??";
lcp@721
  1555
lcp@721
  1556
wenzelm@52222
  1557
(*unify types of schematic variables (non-lifted case)*)
wenzelm@52222
  1558
fun unify_var_types thy (th1, th2) env =
wenzelm@52222
  1559
  let
wenzelm@52222
  1560
    fun unify_vars (T :: Us) = fold (fn U => Pattern.unify_types thy (T, U)) Us
wenzelm@52222
  1561
      | unify_vars _ = I;
wenzelm@52222
  1562
    val add_vars =
wenzelm@52222
  1563
      full_prop_of #>
wenzelm@52222
  1564
      fold_aterms (fn Var v => Vartab.insert_list (op =) v | _ => I);
wenzelm@52222
  1565
    val vars = Vartab.empty |> add_vars th1 |> add_vars th2;
wenzelm@52222
  1566
  in SOME (Vartab.fold (unify_vars o #2) vars env) end
wenzelm@52222
  1567
  handle Pattern.Unif => NONE;
wenzelm@52222
  1568
clasohm@0
  1569
(*Composition of object rule r=(A1...Am/B) with proof state s=(B1...Bn/C)
wenzelm@250
  1570
  Unifies B with Bi, replacing subgoal i    (1 <= i <= n)
clasohm@0
  1571
  If match then forbid instantiations in proof state
clasohm@0
  1572
  If lifted then shorten the dpair using strip_assums2.
clasohm@0
  1573
  If eres_flg then simultaneously proves A1 by assumption.
wenzelm@250
  1574
  nsubgoal is the number of new subgoals (written m above).
clasohm@0
  1575
  Curried so that resolution calls dest_state only once.
clasohm@0
  1576
*)
wenzelm@4270
  1577
local exception COMPOSE
clasohm@0
  1578
in
wenzelm@52223
  1579
fun bicompose_aux {flatten, match, incremented} (state, (stpairs, Bs, Bi, C), lifted)
clasohm@0
  1580
                        (eres_flg, orule, nsubgoal) =
wenzelm@28321
  1581
 let val Thm (sder, {maxidx=smax, shyps=sshyps, hyps=shyps, ...}) = state
wenzelm@28321
  1582
     and Thm (rder, {maxidx=rmax, shyps=rshyps, hyps=rhyps,
wenzelm@28321
  1583
             tpairs=rtpairs, prop=rprop,...}) = orule
paulson@1529
  1584
         (*How many hyps to skip over during normalization*)
wenzelm@21576
  1585
     and nlift = Logic.count_prems (strip_all_body Bi) + (if eres_flg then ~1 else 0)
wenzelm@52788
  1586
     val thy = merge_thys2 state orule;
clasohm@0
  1587
     (** Add new theorem with prop = '[| Bs; As |] ==> C' to thq **)
wenzelm@32032
  1588
     fun addth A (As, oldAs, rder', n) ((env, tpairs), thq) =
wenzelm@250
  1589
       let val normt = Envir.norm_term env;
wenzelm@250
  1590
           (*perform minimal copying here by examining env*)
berghofe@13658
  1591
           val (ntpairs, normp) =
berghofe@13658
  1592
             if Envir.is_empty env then (tpairs, (Bs @ As, C))
wenzelm@250
  1593
             else
wenzelm@250
  1594
             let val ntps = map (pairself normt) tpairs
wenzelm@19861
  1595
             in if Envir.above env smax then
wenzelm@1238
  1596
                  (*no assignments in state; normalize the rule only*)
wenzelm@1238
  1597
                  if lifted
berghofe@13658
  1598
                  then (ntps, (Bs @ map (norm_term_skip env nlift) As, C))
berghofe@13658
  1599
                  else (ntps, (Bs @ map normt As, C))
paulson@1529
  1600
                else if match then raise COMPOSE
wenzelm@250
  1601
                else (*normalize the new rule fully*)
berghofe@13658
  1602
                  (ntps, (map normt (Bs @ As), normt C))
wenzelm@250
  1603
             end
wenzelm@16601
  1604
           val th =
wenzelm@52487
  1605
             Thm (deriv_rule2
berghofe@11518
  1606
                   ((if Envir.is_empty env then I
wenzelm@19861
  1607
                     else if Envir.above env smax then
wenzelm@37309
  1608
                       (fn f => fn der => f (Proofterm.norm_proof' env der))
berghofe@11518
  1609
                     else
wenzelm@37309
  1610
                       curry op oo (Proofterm.norm_proof' env))
wenzelm@37309
  1611
                    (Proofterm.bicompose_proof flatten Bs oldAs As A n (nlift+1))) rder' sder,
wenzelm@28321
  1612
                {tags = [],
wenzelm@32032
  1613
                 maxidx = Envir.maxidx_of env,
wenzelm@26640
  1614
                 shyps = Envir.insert_sorts env (Sorts.union rshyps sshyps),
wenzelm@16601
  1615
                 hyps = union_hyps rhyps shyps,
berghofe@13658
  1616
                 tpairs = ntpairs,
wenzelm@24143
  1617
                 prop = Logic.list_implies normp,
wenzelm@52788
  1618
                 thy = thy})
wenzelm@19475
  1619
        in  Seq.cons th thq  end  handle COMPOSE => thq;
berghofe@13658
  1620
     val (rAs,B) = Logic.strip_prems(nsubgoal, [], rprop)
clasohm@0
  1621
       handle TERM _ => raise THM("bicompose: rule", 0, [orule,state]);
clasohm@0
  1622
     (*Modify assumptions, deleting n-th if n>0 for e-resolution*)
clasohm@0
  1623
     fun newAs(As0, n, dpairs, tpairs) =
berghofe@11518
  1624
       let val (As1, rder') =
berghofe@25939
  1625
         if not lifted then (As0, rder)
berghofe@44108
  1626
         else
berghofe@44108
  1627
           let val rename = rename_bvars dpairs tpairs B As0
berghofe@44108
  1628
           in (map (rename strip_apply) As0,
wenzelm@52487
  1629
             deriv_rule1 (Proofterm.map_proof_terms (rename K) I) rder)
berghofe@44108
  1630
           end;
wenzelm@18486
  1631
       in (map (if flatten then (Logic.flatten_params n) else I) As1, As1, rder', n)
wenzelm@250
  1632
          handle TERM _ =>
wenzelm@250
  1633
          raise THM("bicompose: 1st premise", 0, [orule])
clasohm@0
  1634
       end;
clasohm@0
  1635
     val BBi = if lifted then strip_assums2(B,Bi) else (B,Bi);
clasohm@0
  1636
     val dpairs = BBi :: (rtpairs@stpairs);
wenzelm@30554
  1637
wenzelm@30554
  1638
     (*elim-resolution: try each assumption in turn*)
wenzelm@52222
  1639
     fun eres _ [] = raise THM ("bicompose: no premises", 0, [orule, state])
wenzelm@52222
  1640
       | eres env (A1 :: As) =
wenzelm@30554
  1641
           let
wenzelm@30554
  1642
             val A = SOME A1;
wenzelm@30556
  1643
             val (close, asms, concl) = Logic.assum_problems (nlift + 1, A1);
wenzelm@30556
  1644
             val concl' = close concl;
wenzelm@30554
  1645
             fun tryasms [] _ = Seq.empty
wenzelm@30556
  1646
               | tryasms (asm :: rest) n =
wenzelm@30556
  1647
                   if Term.could_unify (asm, concl) then
wenzelm@30556
  1648
                     let val asm' = close asm in
wenzelm@30556
  1649
                       (case Seq.pull (Unify.unifiers (thy, env, (asm', concl') :: dpairs)) of
wenzelm@30554
  1650
                         NONE => tryasms rest (n + 1)
wenzelm@30554
  1651
                       | cell as SOME ((_, tpairs), _) =>
wenzelm@30556
  1652
                           Seq.it_right (addth A (newAs (As, n, [BBi, (concl', asm')], tpairs)))
wenzelm@30554
  1653
                             (Seq.make (fn () => cell),
wenzelm@30554
  1654
                              Seq.make (fn () => Seq.pull (tryasms rest (n + 1)))))
wenzelm@30554
  1655
                     end
wenzelm@30554
  1656
                   else tryasms rest (n + 1);
wenzelm@30556
  1657
           in tryasms asms 1 end;
wenzelm@30554
  1658
clasohm@0
  1659
     (*ordinary resolution*)
wenzelm@52222
  1660
     fun res env =
wenzelm@30554
  1661
       (case Seq.pull (Unify.unifiers (thy, env, dpairs)) of
wenzelm@30554
  1662
         NONE => Seq.empty
wenzelm@30554
  1663
       | cell as SOME ((_, tpairs), _) =>
wenzelm@30554
  1664
           Seq.it_right (addth NONE (newAs (rev rAs, 0, [BBi], tpairs)))
wenzelm@30554
  1665
             (Seq.make (fn () => cell), Seq.empty));
wenzelm@52222
  1666
wenzelm@52222
  1667
     val env0 = Envir.empty (Int.max (rmax, smax));
wenzelm@30554
  1668
 in
wenzelm@52223
  1669
   (case if incremented then SOME env0 else unify_var_types thy (state, orule) env0 of
wenzelm@52222
  1670
     NONE => Seq.empty
wenzelm@52222
  1671
   | SOME env => if eres_flg then eres env (rev rAs) else res env)
clasohm@0
  1672
 end;
wenzelm@7528
  1673
end;
clasohm@0
  1674
wenzelm@52223
  1675
fun bicompose flags arg i state =
wenzelm@52223
  1676
  bicompose_aux flags (state, dest_state (state,i), false) arg;
clasohm@0
  1677
clasohm@0
  1678
(*Quick test whether rule is resolvable with the subgoal with hyps Hs
clasohm@0
  1679
  and conclusion B.  If eres_flg then checks 1st premise of rule also*)
clasohm@0
  1680
fun could_bires (Hs, B, eres_flg, rule) =
wenzelm@29269
  1681
    let fun could_reshyp (A1::_) = exists (fn H => Term.could_unify (A1, H)) Hs
wenzelm@250
  1682
          | could_reshyp [] = false;  (*no premise -- illegal*)
wenzelm@29269
  1683
    in  Term.could_unify(concl_of rule, B) andalso
wenzelm@250
  1684
        (not eres_flg  orelse  could_reshyp (prems_of rule))
clasohm@0
  1685
    end;
clasohm@0
  1686
clasohm@0
  1687
(*Bi-resolution of a state with a list of (flag,rule) pairs.
clasohm@0
  1688
  Puts the rule above:  rule/state.  Renames vars in the rules. *)
wenzelm@250
  1689
fun biresolution match brules i state =
wenzelm@18035
  1690
    let val (stpairs, Bs, Bi, C) = dest_state(state,i);
wenzelm@18145
  1691
        val lift = lift_rule (cprem_of state i);
wenzelm@250
  1692
        val B = Logic.strip_assums_concl Bi;
wenzelm@250
  1693
        val Hs = Logic.strip_assums_hyp Bi;
wenzelm@52223
  1694
        val compose =
wenzelm@52223
  1695
          bicompose_aux {flatten = true, match = match, incremented = true}
wenzelm@52223
  1696
            (state, (stpairs, Bs, Bi, C), true);
wenzelm@4270
  1697
        fun res [] = Seq.empty
wenzelm@250
  1698
          | res ((eres_flg, rule)::brules) =
wenzelm@52709
  1699
              if Config.get_global (theory_of_thm state) Pattern.unify_trace_failure orelse
nipkow@13642
  1700
                 could_bires (Hs, B, eres_flg, rule)
wenzelm@4270
  1701
              then Seq.make (*delay processing remainder till needed*)
wenzelm@22573
  1702
                  (fn()=> SOME(compose (eres_flg, lift rule, nprems_of rule),
wenzelm@250
  1703
                               res brules))
wenzelm@250
  1704
              else res brules
wenzelm@4270
  1705
    in  Seq.flat (res brules)  end;
clasohm@0
  1706
clasohm@0
  1707
wenzelm@28321
  1708
wenzelm@2509
  1709
(*** Oracles ***)
wenzelm@2509
  1710
wenzelm@28290
  1711
(* oracle rule *)
wenzelm@28290
  1712
wenzelm@52788
  1713
fun invoke_oracle thy1 name oracle arg =
wenzelm@52788
  1714
  let val Cterm {thy = thy2, t = prop, T, maxidx, sorts} = oracle arg in
wenzelm@28290
  1715
    if T <> propT then
wenzelm@28290
  1716
      raise THM ("Oracle's result must have type prop: " ^ name, 0, [])
wenzelm@28290
  1717
    else
wenzelm@52487
  1718
      let val (ora, prf) = Proofterm.oracle_proof name prop in
wenzelm@32059
  1719
        Thm (make_deriv [] [ora] [] prf,
wenzelm@52788
  1720
         {thy = Theory.merge (thy1, thy2),
wenzelm@28804
  1721
          tags = [],
wenzelm@28804
  1722
          maxidx = maxidx,
wenzelm@28804
  1723
          shyps = sorts,
wenzelm@28804
  1724
          hyps = [],
wenzelm@28804
  1725
          tpairs = [],
wenzelm@28804
  1726
          prop = prop})
wenzelm@28804
  1727
      end
wenzelm@3812
  1728
  end;
wenzelm@3812
  1729
wenzelm@32590
  1730
end;
wenzelm@32590
  1731
end;
wenzelm@32590
  1732
end;
wenzelm@32590
  1733
wenzelm@28290
  1734
wenzelm@28290
  1735
(* authentic derivation names *)
wenzelm@28290
  1736
wenzelm@33522
  1737
structure Oracles = Theory_Data
wenzelm@28290
  1738
(
wenzelm@33095
  1739
  type T = unit Name_Space.table;
wenzelm@33159
  1740
  val empty : T = Name_Space.empty_table "oracle";
wenzelm@28290
  1741
  val extend = I;
wenzelm@33522
  1742
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@28290
  1743
);
wenzelm@28290
  1744
wenzelm@42358
  1745
fun extern_oracles ctxt =
wenzelm@56052
  1746
  map #1 (Name_Space.markup_table ctxt (Oracles.get (Proof_Context.theory_of ctxt)));
wenzelm@28290
  1747
wenzelm@30288
  1748
fun add_oracle (b, oracle) thy =
wenzelm@28290
  1749
  let
wenzelm@47005
  1750
    val (name, tab') = Name_Space.define (Context.Theory thy) true (b, ()) (Oracles.get thy);
wenzelm@30288
  1751
    val thy' = Oracles.put tab' thy;
wenzelm@52788
  1752
  in ((name, invoke_oracle thy' name oracle), thy') end;
wenzelm@28290
  1753
clasohm@0
  1754
end;
paulson@1503
  1755
wenzelm@32104
  1756
structure Basic_Thm: BASIC_THM = Thm;
wenzelm@32104
  1757
open Basic_Thm;