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