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