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