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