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