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