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