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