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