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