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