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