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