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