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