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