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