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