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