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