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