src/Pure/thm.ML
author wenzelm
Sat Nov 04 15:24:40 2017 +0100 (19 months ago)
changeset 67003 49850a679c2c
parent 66168 fcd09fc36d7f
child 67330 2505cabfc515
permissions -rw-r--r--
more robust sorted_entries;
wenzelm@250
     1
(*  Title:      Pure/thm.ML
wenzelm@250
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
wenzelm@29269
     3
    Author:     Makarius
lcp@229
     4
wenzelm@16425
     5
The very core of Isabelle's Meta Logic: certified types and terms,
wenzelm@59589
     6
derivations, theorems, inference rules (including lifting and
wenzelm@28321
     7
resolution), oracles.
clasohm@0
     8
*)
clasohm@0
     9
wenzelm@6089
    10
signature BASIC_THM =
wenzelm@59589
    11
sig
wenzelm@59582
    12
  type ctyp
wenzelm@59582
    13
  type cterm
wenzelm@59582
    14
  exception CTERM of string * cterm list
wenzelm@59582
    15
  type thm
wenzelm@59582
    16
  type conv = cterm -> thm
wenzelm@59582
    17
  exception THM of string * int * thm list
wenzelm@59582
    18
end;
wenzelm@59582
    19
wenzelm@59582
    20
signature THM =
wenzelm@59582
    21
sig
wenzelm@59582
    22
  include BASIC_THM
wenzelm@1160
    23
  (*certified types*)
wenzelm@16425
    24
  val typ_of: ctyp -> typ
wenzelm@59621
    25
  val global_ctyp_of: theory -> typ -> ctyp
wenzelm@59621
    26
  val ctyp_of: Proof.context -> typ -> ctyp
wenzelm@59582
    27
  val dest_ctyp: ctyp -> ctyp list
wenzelm@1160
    28
  (*certified terms*)
wenzelm@16425
    29
  val term_of: cterm -> term
wenzelm@59586
    30
  val typ_of_cterm: cterm -> typ
wenzelm@59586
    31
  val ctyp_of_cterm: cterm -> ctyp
wenzelm@59586
    32
  val maxidx_of_cterm: cterm -> int
wenzelm@59621
    33
  val global_cterm_of: theory -> term -> cterm
wenzelm@59621
    34
  val cterm_of: Proof.context -> term -> cterm
wenzelm@59969
    35
  val renamed_term: term -> cterm -> cterm
wenzelm@59582
    36
  val dest_comb: cterm -> cterm * cterm
wenzelm@59582
    37
  val dest_fun: cterm -> cterm
wenzelm@59582
    38
  val dest_arg: cterm -> cterm
wenzelm@59582
    39
  val dest_fun2: cterm -> cterm
wenzelm@59582
    40
  val dest_arg1: cterm -> cterm
wenzelm@59582
    41
  val dest_abs: string option -> cterm -> cterm * cterm
wenzelm@60952
    42
  val rename_tvar: indexname -> ctyp -> ctyp
wenzelm@60951
    43
  val var: indexname * ctyp -> cterm
wenzelm@59582
    44
  val apply: cterm -> cterm -> cterm
wenzelm@59582
    45
  val lambda_name: string * cterm -> cterm -> cterm
wenzelm@59582
    46
  val lambda: cterm -> cterm -> cterm
wenzelm@59582
    47
  val adjust_maxidx_cterm: int -> cterm -> cterm
wenzelm@59582
    48
  val incr_indexes_cterm: int -> cterm -> cterm
wenzelm@60642
    49
  val match: cterm * cterm ->
wenzelm@60642
    50
    ((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list
wenzelm@60642
    51
  val first_order_match: cterm * cterm ->
wenzelm@60642
    52
    ((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list
wenzelm@28321
    53
  (*theorems*)
wenzelm@59582
    54
  val fold_terms: (term -> 'a -> 'a) -> thm -> 'a -> 'a
wenzelm@60952
    55
  val fold_atomic_ctyps: (ctyp -> 'a -> 'a) -> thm -> 'a -> 'a
wenzelm@60818
    56
  val fold_atomic_cterms: (cterm -> 'a -> 'a) -> thm -> 'a -> 'a
wenzelm@59582
    57
  val terms_of_tpairs: (term * term) list -> term list
wenzelm@59582
    58
  val full_prop_of: thm -> term
wenzelm@65458
    59
  val theory_id: thm -> Context.theory_id
wenzelm@65458
    60
  val theory_name: thm -> string
wenzelm@59582
    61
  val maxidx_of: thm -> int
wenzelm@59582
    62
  val maxidx_thm: thm -> int -> int
wenzelm@61039
    63
  val shyps_of: thm -> sort Ord_List.T
wenzelm@59582
    64
  val hyps_of: thm -> term list
wenzelm@16425
    65
  val prop_of: thm -> term
wenzelm@59582
    66
  val tpairs_of: thm -> (term * term) list
wenzelm@16656
    67
  val concl_of: thm -> term
wenzelm@16425
    68
  val prems_of: thm -> term list
wenzelm@16425
    69
  val nprems_of: thm -> int
wenzelm@59582
    70
  val no_prems: thm -> bool
wenzelm@59582
    71
  val major_prem_of: thm -> term
wenzelm@16425
    72
  val cprop_of: thm -> cterm
wenzelm@18145
    73
  val cprem_of: thm -> int -> cterm
wenzelm@60949
    74
  val chyps_of: thm -> cterm list
wenzelm@61051
    75
  exception CONTEXT of string * ctyp list * cterm list * thm list * Context.generic option
wenzelm@61051
    76
  val theory_of_cterm: cterm -> theory
wenzelm@61051
    77
  val theory_of_thm: thm -> theory
wenzelm@61051
    78
  val trim_context_cterm: cterm -> cterm
wenzelm@61048
    79
  val trim_context: thm -> thm
wenzelm@61051
    80
  val transfer_cterm: theory -> cterm -> cterm
wenzelm@38709
    81
  val transfer: theory -> thm -> thm
wenzelm@59969
    82
  val renamed_prop: term -> thm -> thm
wenzelm@38709
    83
  val weaken: cterm -> thm -> thm
wenzelm@38709
    84
  val weaken_sorts: sort list -> cterm -> cterm
wenzelm@38709
    85
  val extra_shyps: thm -> sort list
wenzelm@44333
    86
  val proof_bodies_of: thm list -> proof_body list
wenzelm@32725
    87
  val proof_body_of: thm -> proof_body
wenzelm@32725
    88
  val proof_of: thm -> proof
wenzelm@66168
    89
  val consolidate: thm list -> unit
wenzelm@50126
    90
  val peek_status: thm -> {oracle: bool, unfinished: bool, failed: bool}
wenzelm@32725
    91
  val future: thm future -> cterm -> thm
wenzelm@64568
    92
  val derivation_closed: thm -> bool
wenzelm@36744
    93
  val derivation_name: thm -> string
wenzelm@36744
    94
  val name_derivation: string -> thm -> thm
wenzelm@28675
    95
  val axiom: theory -> string -> thm
wenzelm@28675
    96
  val axioms_of: theory -> (string * thm) list
wenzelm@28017
    97
  val get_tags: thm -> Properties.T
wenzelm@28017
    98
  val map_tags: (Properties.T -> Properties.T) -> thm -> thm
berghofe@23781
    99
  val norm_proof: thm -> thm
wenzelm@20261
   100
  val adjust_maxidx_thm: int -> thm -> thm
wenzelm@59589
   101
  (*inference rules*)
wenzelm@38709
   102
  val assume: cterm -> thm
wenzelm@38709
   103
  val implies_intr: cterm -> thm -> thm
wenzelm@38709
   104
  val implies_elim: thm -> thm -> thm
wenzelm@38709
   105
  val forall_intr: cterm -> thm -> thm
wenzelm@38709
   106
  val forall_elim: cterm -> thm -> thm
wenzelm@38709
   107
  val reflexive: cterm -> thm
wenzelm@38709
   108
  val symmetric: thm -> thm
wenzelm@38709
   109
  val transitive: thm -> thm -> thm
wenzelm@38709
   110
  val beta_conversion: bool -> conv
wenzelm@38709
   111
  val eta_conversion: conv
wenzelm@38709
   112
  val eta_long_conversion: conv
wenzelm@38709
   113
  val abstract_rule: string -> cterm -> thm -> thm
wenzelm@38709
   114
  val combination: thm -> thm -> thm
wenzelm@38709
   115
  val equal_intr: thm -> thm -> thm
wenzelm@38709
   116
  val equal_elim: thm -> thm -> thm
wenzelm@58950
   117
  val flexflex_rule: Proof.context option -> thm -> thm Seq.seq
wenzelm@38709
   118
  val generalize: string list * string list -> int -> thm -> thm
wenzelm@60642
   119
  val instantiate: ((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list ->
wenzelm@60642
   120
    thm -> thm
wenzelm@60642
   121
  val instantiate_cterm: ((indexname * sort) * ctyp) list * ((indexname * typ) * cterm) list ->
wenzelm@60642
   122
    cterm -> cterm
wenzelm@38709
   123
  val trivial: cterm -> thm
wenzelm@36330
   124
  val of_class: ctyp * class -> thm
wenzelm@36614
   125
  val strip_shyps: thm -> thm
wenzelm@36768
   126
  val unconstrainT: thm -> thm
wenzelm@38709
   127
  val varifyT_global': (string * sort) list -> thm -> ((string * sort) * indexname) list * thm
wenzelm@38709
   128
  val varifyT_global: thm -> thm
wenzelm@36615
   129
  val legacy_freezeT: thm -> thm
wenzelm@59969
   130
  val dest_state: thm * int -> (term * term) list * term list * term * term
wenzelm@38709
   131
  val lift_rule: cterm -> thm -> thm
wenzelm@38709
   132
  val incr_indexes: int -> thm -> thm
wenzelm@58950
   133
  val assumption: Proof.context option -> int -> thm -> thm Seq.seq
wenzelm@31945
   134
  val eq_assumption: int -> thm -> thm
wenzelm@31945
   135
  val rotate_rule: int -> int -> thm -> thm
wenzelm@31945
   136
  val permute_prems: int -> int -> thm -> thm
wenzelm@58950
   137
  val bicompose: Proof.context option -> {flatten: bool, match: bool, incremented: bool} ->
wenzelm@52223
   138
    bool * thm * int -> int -> thm -> thm Seq.seq
wenzelm@58950
   139
  val biresolution: Proof.context option -> bool -> (bool * thm) list -> int -> thm -> thm Seq.seq
wenzelm@59589
   140
  (*oracles*)
wenzelm@59917
   141
  val extern_oracles: bool -> Proof.context -> (Markup.T * xstring) list
wenzelm@30288
   142
  val add_oracle: binding * ('a -> cterm) -> theory -> (string * ('a -> thm)) * theory
wenzelm@6089
   143
end;
wenzelm@6089
   144
wenzelm@32590
   145
structure Thm: THM =
clasohm@0
   146
struct
wenzelm@250
   147
wenzelm@387
   148
(*** Certified terms and types ***)
wenzelm@387
   149
wenzelm@250
   150
(** certified types **)
wenzelm@250
   151
wenzelm@61044
   152
abstype ctyp = Ctyp of {cert: Context.certificate, T: typ, maxidx: int, sorts: sort Ord_List.T}
wenzelm@32590
   153
with
wenzelm@250
   154
wenzelm@250
   155
fun typ_of (Ctyp {T, ...}) = T;
wenzelm@250
   156
wenzelm@59621
   157
fun global_ctyp_of thy raw_T =
wenzelm@24143
   158
  let
wenzelm@24143
   159
    val T = Sign.certify_typ thy raw_T;
wenzelm@24143
   160
    val maxidx = Term.maxidx_of_typ T;
wenzelm@26640
   161
    val sorts = Sorts.insert_typ T [];
wenzelm@61044
   162
  in Ctyp {cert = Context.Certificate thy, T = T, maxidx = maxidx, sorts = sorts} end;
wenzelm@250
   163
wenzelm@59621
   164
val ctyp_of = global_ctyp_of o Proof_Context.theory_of;
wenzelm@59621
   165
wenzelm@61044
   166
fun dest_ctyp (Ctyp {cert, T = Type (_, Ts), maxidx, sorts}) =
wenzelm@61044
   167
      map (fn T => Ctyp {cert = cert, T = T, maxidx = maxidx, sorts = sorts}) Ts
wenzelm@16679
   168
  | dest_ctyp cT = raise TYPE ("dest_ctyp", [typ_of cT], []);
berghofe@15087
   169
lcp@229
   170
lcp@229
   171
wenzelm@250
   172
(** certified terms **)
lcp@229
   173
wenzelm@16601
   174
(*certified terms with checked typ, maxidx, and sorts*)
wenzelm@61044
   175
abstype cterm =
wenzelm@61044
   176
  Cterm of {cert: Context.certificate, t: term, T: typ, maxidx: int, sorts: sort Ord_List.T}
wenzelm@32590
   177
with
wenzelm@16425
   178
wenzelm@22584
   179
exception CTERM of string * cterm list;
wenzelm@16679
   180
wenzelm@250
   181
fun term_of (Cterm {t, ...}) = t;
lcp@229
   182
wenzelm@59586
   183
fun typ_of_cterm (Cterm {T, ...}) = T;
wenzelm@59586
   184
wenzelm@61044
   185
fun ctyp_of_cterm (Cterm {cert, T, maxidx, sorts, ...}) =
wenzelm@61044
   186
  Ctyp {cert = cert, T = T, maxidx = maxidx, sorts = sorts};
paulson@2671
   187
wenzelm@59586
   188
fun maxidx_of_cterm (Cterm {maxidx, ...}) = maxidx;
wenzelm@59586
   189
wenzelm@59621
   190
fun global_cterm_of thy tm =
wenzelm@16601
   191
  let
wenzelm@18969
   192
    val (t, T, maxidx) = Sign.certify_term thy tm;
wenzelm@26640
   193
    val sorts = Sorts.insert_term t [];
wenzelm@61044
   194
  in Cterm {cert = Context.Certificate thy, t = t, T = T, maxidx = maxidx, sorts = sorts} end;
lcp@229
   195
wenzelm@59621
   196
val cterm_of = global_cterm_of o Proof_Context.theory_of;
wenzelm@59621
   197
wenzelm@61044
   198
fun join_certificate0 (Cterm {cert = cert1, ...}, Cterm {cert = cert2, ...}) =
wenzelm@61044
   199
  Context.join_certificate (cert1, cert2);
wenzelm@60315
   200
wenzelm@61044
   201
fun renamed_term t' (Cterm {cert, t, T, maxidx, sorts}) =
wenzelm@61044
   202
  if t aconv t' then Cterm {cert = cert, t = t', T = T, maxidx = maxidx, sorts = sorts}
wenzelm@59969
   203
  else raise TERM ("renamed_term: terms disagree", [t, t']);
wenzelm@59969
   204
wenzelm@20580
   205
wenzelm@22909
   206
(* destructors *)
wenzelm@22909
   207
wenzelm@61044
   208
fun dest_comb (Cterm {t = c $ a, T, cert, maxidx, sorts}) =
wenzelm@22909
   209
      let val A = Term.argument_type_of c 0 in
wenzelm@61044
   210
        (Cterm {t = c, T = A --> T, cert = cert, maxidx = maxidx, sorts = sorts},
wenzelm@61044
   211
         Cterm {t = a, T = A, cert = cert, maxidx = maxidx, sorts = sorts})
clasohm@1493
   212
      end
wenzelm@22584
   213
  | dest_comb ct = raise CTERM ("dest_comb", [ct]);
clasohm@1493
   214
wenzelm@61044
   215
fun dest_fun (Cterm {t = c $ _, T, cert, maxidx, sorts}) =
wenzelm@22909
   216
      let val A = Term.argument_type_of c 0
wenzelm@61044
   217
      in Cterm {t = c, T = A --> T, cert = cert, maxidx = maxidx, sorts = sorts} end
wenzelm@22909
   218
  | dest_fun ct = raise CTERM ("dest_fun", [ct]);
wenzelm@22909
   219
wenzelm@61044
   220
fun dest_arg (Cterm {t = c $ a, T = _, cert, maxidx, sorts}) =
wenzelm@22909
   221
      let val A = Term.argument_type_of c 0
wenzelm@61044
   222
      in Cterm {t = a, T = A, cert = cert, maxidx = maxidx, sorts = sorts} end
wenzelm@22584
   223
  | dest_arg ct = raise CTERM ("dest_arg", [ct]);
wenzelm@20580
   224
wenzelm@22909
   225
wenzelm@61044
   226
fun dest_fun2 (Cterm {t = c $ _ $ _, T, cert, maxidx, sorts}) =
wenzelm@22909
   227
      let
wenzelm@22909
   228
        val A = Term.argument_type_of c 0;
wenzelm@22909
   229
        val B = Term.argument_type_of c 1;
wenzelm@61044
   230
      in Cterm {t = c, T = A --> B --> T, cert = cert, maxidx = maxidx, sorts = sorts} end
wenzelm@22909
   231
  | dest_fun2 ct = raise CTERM ("dest_fun2", [ct]);
wenzelm@22909
   232
wenzelm@61044
   233
fun dest_arg1 (Cterm {t = c $ a $ _, T = _, cert, maxidx, sorts}) =
wenzelm@22909
   234
      let val A = Term.argument_type_of c 0
wenzelm@61044
   235
      in Cterm {t = a, T = A, cert = cert, maxidx = maxidx, sorts = sorts} end
wenzelm@22909
   236
  | dest_arg1 ct = raise CTERM ("dest_arg1", [ct]);
wenzelm@20673
   237
wenzelm@61044
   238
fun dest_abs a (Cterm {t = Abs (x, T, t), T = Type ("fun", [_, U]), cert, maxidx, sorts}) =
wenzelm@18944
   239
      let val (y', t') = Term.dest_abs (the_default x a, T, t) in
wenzelm@61044
   240
        (Cterm {t = Free (y', T), T = T, cert = cert, maxidx = maxidx, sorts = sorts},
wenzelm@61044
   241
          Cterm {t = t', T = U, cert = cert, maxidx = maxidx, sorts = sorts})
clasohm@1493
   242
      end
wenzelm@22584
   243
  | dest_abs _ ct = raise CTERM ("dest_abs", [ct]);
clasohm@1493
   244
wenzelm@22909
   245
wenzelm@22909
   246
(* constructors *)
wenzelm@22909
   247
wenzelm@61044
   248
fun rename_tvar (a, i) (Ctyp {cert, T, maxidx, sorts}) =
wenzelm@60952
   249
  let
wenzelm@60952
   250
    val S =
wenzelm@60952
   251
      (case T of
wenzelm@60952
   252
        TFree (_, S) => S
wenzelm@60952
   253
      | TVar (_, S) => S
wenzelm@60952
   254
      | _ => raise TYPE ("rename_tvar: no variable", [T], []));
wenzelm@60952
   255
    val _ = if i < 0 then raise TYPE ("rename_tvar: bad index", [TVar ((a, i), S)], []) else ();
wenzelm@61044
   256
  in Ctyp {cert = cert, T = TVar ((a, i), S), maxidx = Int.max (i, maxidx), sorts = sorts} end;
wenzelm@60952
   257
wenzelm@61044
   258
fun var ((x, i), Ctyp {cert, T, maxidx, sorts}) =
wenzelm@60951
   259
  if i < 0 then raise TERM ("var: bad index", [Var ((x, i), T)])
wenzelm@61044
   260
  else Cterm {cert = cert, t = Var ((x, i), T), T = T, maxidx = Int.max (i, maxidx), sorts = sorts};
wenzelm@60951
   261
wenzelm@46497
   262
fun apply
wenzelm@16656
   263
  (cf as Cterm {t = f, T = Type ("fun", [dty, rty]), maxidx = maxidx1, sorts = sorts1, ...})
wenzelm@16656
   264
  (cx as Cterm {t = x, T, maxidx = maxidx2, sorts = sorts2, ...}) =
wenzelm@16601
   265
    if T = dty then
wenzelm@61044
   266
      Cterm {cert = join_certificate0 (cf, cx),
wenzelm@16656
   267
        t = f $ x,
wenzelm@16656
   268
        T = rty,
wenzelm@16656
   269
        maxidx = Int.max (maxidx1, maxidx2),
wenzelm@16601
   270
        sorts = Sorts.union sorts1 sorts2}
wenzelm@46497
   271
      else raise CTERM ("apply: types don't agree", [cf, cx])
wenzelm@46497
   272
  | apply cf cx = raise CTERM ("apply: first arg is not a function", [cf, cx]);
wenzelm@250
   273
wenzelm@46497
   274
fun lambda_name
wenzelm@32198
   275
  (x, ct1 as Cterm {t = t1, T = T1, maxidx = maxidx1, sorts = sorts1, ...})
wenzelm@16656
   276
  (ct2 as Cterm {t = t2, T = T2, maxidx = maxidx2, sorts = sorts2, ...}) =
wenzelm@32198
   277
    let val t = Term.lambda_name (x, t1) t2 in
wenzelm@61044
   278
      Cterm {cert = join_certificate0 (ct1, ct2),
wenzelm@16656
   279
        t = t, T = T1 --> T2,
wenzelm@16656
   280
        maxidx = Int.max (maxidx1, maxidx2),
wenzelm@16656
   281
        sorts = Sorts.union sorts1 sorts2}
wenzelm@16601
   282
    end;
lcp@229
   283
wenzelm@46497
   284
fun lambda t u = lambda_name ("", t) u;
wenzelm@32198
   285
wenzelm@20580
   286
wenzelm@22909
   287
(* indexes *)
wenzelm@22909
   288
wenzelm@61044
   289
fun adjust_maxidx_cterm i (ct as Cterm {cert, t, T, maxidx, sorts}) =
wenzelm@20580
   290
  if maxidx = i then ct
wenzelm@20580
   291
  else if maxidx < i then
wenzelm@61044
   292
    Cterm {maxidx = i, cert = cert, t = t, T = T, sorts = sorts}
wenzelm@20580
   293
  else
wenzelm@61044
   294
    Cterm {maxidx = Int.max (maxidx_of_term t, i), cert = cert, t = t, T = T, sorts = sorts};
wenzelm@20580
   295
wenzelm@61044
   296
fun incr_indexes_cterm i (ct as Cterm {cert, t, T, maxidx, sorts}) =
wenzelm@22909
   297
  if i < 0 then raise CTERM ("negative increment", [ct])
wenzelm@22909
   298
  else if i = 0 then ct
wenzelm@61044
   299
  else Cterm {cert = cert, t = Logic.incr_indexes ([], [], i) t,
wenzelm@22909
   300
    T = Logic.incr_tvar i T, maxidx = maxidx + i, sorts = sorts};
wenzelm@22909
   301
wenzelm@22909
   302
wenzelm@2509
   303
wenzelm@28321
   304
(*** Derivations and Theorems ***)
lcp@229
   305
wenzelm@32590
   306
abstype thm = Thm of
wenzelm@40124
   307
 deriv *                        (*derivation*)
wenzelm@61044
   308
 {cert: Context.certificate,    (*background theory certificate*)
wenzelm@40124
   309
  tags: Properties.T,           (*additional annotations/comments*)
wenzelm@40124
   310
  maxidx: int,                  (*maximum index of any Var or TVar*)
wenzelm@40124
   311
  shyps: sort Ord_List.T,       (*sort hypotheses*)
wenzelm@40124
   312
  hyps: term Ord_List.T,        (*hypotheses*)
wenzelm@40124
   313
  tpairs: (term * term) list,   (*flex-flex pairs*)
wenzelm@40124
   314
  prop: term}                   (*conclusion*)
wenzelm@28624
   315
and deriv = Deriv of
wenzelm@39687
   316
 {promises: (serial * thm future) Ord_List.T,
wenzelm@37309
   317
  body: Proofterm.proof_body}
wenzelm@32590
   318
with
clasohm@0
   319
wenzelm@23601
   320
type conv = cterm -> thm;
wenzelm@23601
   321
wenzelm@16725
   322
(*errors involving theorems*)
wenzelm@16725
   323
exception THM of string * int * thm list;
berghofe@13658
   324
wenzelm@28321
   325
fun rep_thm (Thm (_, args)) = args;
clasohm@0
   326
wenzelm@31947
   327
fun fold_terms f (Thm (_, {tpairs, prop, hyps, ...})) =
wenzelm@31947
   328
  fold (fn (t, u) => f t #> f u) tpairs #> f prop #> fold f hyps;
wenzelm@31947
   329
wenzelm@61044
   330
fun fold_atomic_ctyps f (th as Thm (_, {cert, maxidx, shyps, ...})) =
wenzelm@61044
   331
  let fun ctyp T = Ctyp {cert = cert, T = T, maxidx = maxidx, sorts = shyps}
wenzelm@60952
   332
  in (fold_terms o fold_types o fold_atyps) (f o ctyp) th end;
wenzelm@60952
   333
wenzelm@61044
   334
fun fold_atomic_cterms f (th as Thm (_, {cert, maxidx, shyps, ...})) =
wenzelm@61044
   335
  let fun cterm t T = Cterm {cert = cert, t = t, T = T, maxidx = maxidx, sorts = shyps} in
wenzelm@60818
   336
    (fold_terms o fold_aterms)
wenzelm@60818
   337
      (fn t as Const (_, T) => f (cterm t T)
wenzelm@60818
   338
        | t as Free (_, T) => f (cterm t T)
wenzelm@60818
   339
        | t as Var (_, T) => f (cterm t T)
wenzelm@60818
   340
        | _ => I) th
wenzelm@60818
   341
  end;
wenzelm@60818
   342
wenzelm@16725
   343
fun terms_of_tpairs tpairs = fold_rev (fn (t, u) => cons t o cons u) tpairs [];
wenzelm@16725
   344
wenzelm@16725
   345
fun eq_tpairs ((t, u), (t', u')) = t aconv t' andalso u aconv u';
wenzelm@18944
   346
fun union_tpairs ts us = Library.merge eq_tpairs (ts, us);
wenzelm@16884
   347
val maxidx_tpairs = fold (fn (t, u) => Term.maxidx_term t #> Term.maxidx_term u);
wenzelm@16725
   348
wenzelm@16725
   349
fun attach_tpairs tpairs prop =
wenzelm@16725
   350
  Logic.list_implies (map Logic.mk_equals tpairs, prop);
wenzelm@16725
   351
wenzelm@28321
   352
fun full_prop_of (Thm (_, {tpairs, prop, ...})) = attach_tpairs tpairs prop;
wenzelm@16945
   353
wenzelm@39687
   354
val union_hyps = Ord_List.union Term_Ord.fast_term_ord;
wenzelm@39687
   355
val insert_hyps = Ord_List.insert Term_Ord.fast_term_ord;
wenzelm@39687
   356
val remove_hyps = Ord_List.remove Term_Ord.fast_term_ord;
wenzelm@22365
   357
wenzelm@61044
   358
fun join_certificate1 (Cterm {cert = cert1, ...}, Thm (_, {cert = cert2, ...})) =
wenzelm@61044
   359
  Context.join_certificate (cert1, cert2);
wenzelm@16945
   360
wenzelm@61044
   361
fun join_certificate2 (Thm (_, {cert = cert1, ...}), Thm (_, {cert = cert2, ...})) =
wenzelm@61044
   362
  Context.join_certificate (cert1, cert2);
wenzelm@16945
   363
clasohm@0
   364
wenzelm@22365
   365
(* basic components *)
wenzelm@16135
   366
wenzelm@61044
   367
val cert_of = #cert o rep_thm;
wenzelm@65458
   368
val theory_id = Context.certificate_theory_id o cert_of;
wenzelm@65458
   369
val theory_name = Context.theory_id_name o theory_id;
wenzelm@61050
   370
wenzelm@28321
   371
val maxidx_of = #maxidx o rep_thm;
wenzelm@19910
   372
fun maxidx_thm th i = Int.max (maxidx_of th, i);
wenzelm@61039
   373
val shyps_of = #shyps o rep_thm;
wenzelm@28321
   374
val hyps_of = #hyps o rep_thm;
wenzelm@28321
   375
val prop_of = #prop o rep_thm;
wenzelm@28321
   376
val tpairs_of = #tpairs o rep_thm;
clasohm@0
   377
wenzelm@16601
   378
val concl_of = Logic.strip_imp_concl o prop_of;
wenzelm@16601
   379
val prems_of = Logic.strip_imp_prems o prop_of;
wenzelm@21576
   380
val nprems_of = Logic.count_prems o prop_of;
wenzelm@19305
   381
fun no_prems th = nprems_of th = 0;
wenzelm@16601
   382
wenzelm@16601
   383
fun major_prem_of th =
wenzelm@16601
   384
  (case prems_of th of
wenzelm@16601
   385
    prem :: _ => Logic.strip_assums_concl prem
wenzelm@16601
   386
  | [] => raise THM ("major_prem_of: rule with no premises", 0, [th]));
wenzelm@16601
   387
wenzelm@61044
   388
fun cprop_of (Thm (_, {cert, maxidx, shyps, prop, ...})) =
wenzelm@61044
   389
  Cterm {cert = cert, maxidx = maxidx, T = propT, t = prop, sorts = shyps};
wenzelm@16601
   390
wenzelm@61044
   391
fun cprem_of (th as Thm (_, {cert, maxidx, shyps, prop, ...})) i =
wenzelm@61044
   392
  Cterm {cert = cert, maxidx = maxidx, T = propT, sorts = shyps,
wenzelm@18145
   393
    t = Logic.nth_prem (i, prop) handle TERM _ => raise THM ("cprem_of", i, [th])};
wenzelm@18035
   394
wenzelm@61044
   395
fun chyps_of (Thm (_, {cert, shyps, hyps, ...})) =
wenzelm@61044
   396
  map (fn t => Cterm {cert = cert, maxidx = ~1, T = propT, sorts = shyps, t = t}) hyps;
wenzelm@60949
   397
wenzelm@61051
   398
wenzelm@61051
   399
(* implicit theory context *)
wenzelm@61051
   400
wenzelm@61051
   401
exception CONTEXT of string * ctyp list * cterm list * thm list * Context.generic option;
wenzelm@61051
   402
wenzelm@61051
   403
fun theory_of_cterm (ct as Cterm {cert, ...}) =
wenzelm@61051
   404
  Context.certificate_theory cert
wenzelm@61051
   405
    handle ERROR msg => raise CONTEXT (msg, [], [ct], [], NONE);
wenzelm@61051
   406
wenzelm@61051
   407
fun theory_of_thm th =
wenzelm@61051
   408
  Context.certificate_theory (cert_of th)
wenzelm@61051
   409
    handle ERROR msg => raise CONTEXT (msg, [], [], [th], NONE);
wenzelm@61051
   410
wenzelm@61051
   411
fun trim_context_cterm ct =
wenzelm@61051
   412
  (case ct of
wenzelm@61051
   413
    Cterm {cert = Context.Certificate_Id _, ...} => ct
wenzelm@61051
   414
  | Cterm {cert = Context.Certificate thy, t, T, maxidx, sorts} =>
wenzelm@61051
   415
      Cterm {cert = Context.Certificate_Id (Context.theory_id thy),
wenzelm@61051
   416
        t = t, T = T, maxidx = maxidx, sorts = sorts});
wenzelm@61051
   417
wenzelm@61048
   418
fun trim_context th =
wenzelm@61048
   419
  (case th of
wenzelm@61048
   420
    Thm (_, {cert = Context.Certificate_Id _, ...}) => th
wenzelm@61048
   421
  | Thm (der, {cert = Context.Certificate thy, tags, maxidx, shyps, hyps, tpairs, prop}) =>
wenzelm@61048
   422
      Thm (der,
wenzelm@61048
   423
       {cert = Context.Certificate_Id (Context.theory_id thy),
wenzelm@61048
   424
        tags = tags, maxidx = maxidx, shyps = shyps, hyps = hyps, tpairs = tpairs, prop = prop}));
wenzelm@61048
   425
wenzelm@61051
   426
fun transfer_cterm thy' ct =
wenzelm@3895
   427
  let
wenzelm@61051
   428
    val Cterm {cert, t, T, maxidx, sorts} = ct;
wenzelm@61044
   429
    val _ =
wenzelm@61044
   430
      Context.subthy_id (Context.certificate_theory_id cert, Context.theory_id thy') orelse
wenzelm@61051
   431
        raise CONTEXT ("Cannot transfer: not a super theory", [], [ct], [],
wenzelm@61051
   432
          SOME (Context.Theory thy'));
wenzelm@61044
   433
    val cert' = Context.join_certificate (Context.Certificate thy', cert);
wenzelm@3895
   434
  in
wenzelm@61051
   435
    if Context.eq_certificate (cert, cert') then ct
wenzelm@61051
   436
    else Cterm {cert = cert', t = t, T = T, maxidx = maxidx, sorts = sorts}
wenzelm@61051
   437
  end;
wenzelm@61051
   438
wenzelm@61051
   439
fun transfer thy' th =
wenzelm@61051
   440
  let
wenzelm@61051
   441
    val Thm (der, {cert, tags, maxidx, shyps, hyps, tpairs, prop}) = th;
wenzelm@61051
   442
    val _ =
wenzelm@61051
   443
      Context.subthy_id (Context.certificate_theory_id cert, Context.theory_id thy') orelse
wenzelm@61051
   444
        raise CONTEXT ("Cannot transfer: not a super theory", [], [], [th],
wenzelm@61051
   445
          SOME (Context.Theory thy'));
wenzelm@61051
   446
    val cert' = Context.join_certificate (Context.Certificate thy', cert);
wenzelm@61051
   447
  in
wenzelm@61051
   448
    if Context.eq_certificate (cert, cert') then th
wenzelm@16945
   449
    else
wenzelm@28321
   450
      Thm (der,
wenzelm@61044
   451
       {cert = cert',
wenzelm@21646
   452
        tags = tags,
wenzelm@16945
   453
        maxidx = maxidx,
wenzelm@16945
   454
        shyps = shyps,
wenzelm@16945
   455
        hyps = hyps,
wenzelm@16945
   456
        tpairs = tpairs,
wenzelm@28321
   457
        prop = prop})
wenzelm@3895
   458
  end;
wenzelm@387
   459
wenzelm@61051
   460
wenzelm@61051
   461
(* matching *)
wenzelm@61051
   462
wenzelm@61051
   463
local
wenzelm@61051
   464
wenzelm@61051
   465
fun gen_match match
wenzelm@61051
   466
    (ct1 as Cterm {t = t1, sorts = sorts1, ...},
wenzelm@61051
   467
     ct2 as Cterm {t = t2, sorts = sorts2, maxidx = maxidx2, ...}) =
wenzelm@61051
   468
  let
wenzelm@61051
   469
    val cert = join_certificate0 (ct1, ct2);
wenzelm@61051
   470
    val thy = Context.certificate_theory cert
wenzelm@61051
   471
      handle ERROR msg => raise CONTEXT (msg, [], [ct1, ct2], [], NONE);
wenzelm@61051
   472
    val (Tinsts, tinsts) = match thy (t1, t2) (Vartab.empty, Vartab.empty);
wenzelm@61051
   473
    val sorts = Sorts.union sorts1 sorts2;
wenzelm@61051
   474
    fun mk_cTinst ((a, i), (S, T)) =
wenzelm@61051
   475
      (((a, i), S), Ctyp {T = T, cert = cert, maxidx = maxidx2, sorts = sorts});
wenzelm@61051
   476
    fun mk_ctinst ((x, i), (U, t)) =
wenzelm@61051
   477
      let val T = Envir.subst_type Tinsts U in
wenzelm@61051
   478
        (((x, i), T), Cterm {t = t, T = T, cert = cert, maxidx = maxidx2, sorts = sorts})
wenzelm@61051
   479
      end;
wenzelm@61051
   480
  in (Vartab.fold (cons o mk_cTinst) Tinsts [], Vartab.fold (cons o mk_ctinst) tinsts []) end;
wenzelm@61051
   481
wenzelm@61051
   482
in
wenzelm@61051
   483
wenzelm@61051
   484
val match = gen_match Pattern.match;
wenzelm@61051
   485
val first_order_match = gen_match Pattern.first_order_match;
wenzelm@61051
   486
wenzelm@61051
   487
end;
wenzelm@61051
   488
wenzelm@61051
   489
wenzelm@59969
   490
(*implicit alpha-conversion*)
wenzelm@61044
   491
fun renamed_prop prop' (Thm (der, {cert, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@59969
   492
  if prop aconv prop' then
wenzelm@61044
   493
    Thm (der, {cert = cert, tags = tags, maxidx = maxidx, shyps = shyps,
wenzelm@59969
   494
      hyps = hyps, tpairs = tpairs, prop = prop'})
wenzelm@59969
   495
  else raise TERM ("renamed_prop: props disagree", [prop, prop']);
wenzelm@59969
   496
wenzelm@61044
   497
fun make_context ths NONE cert =
wenzelm@61044
   498
      (Context.Theory (Context.certificate_theory cert)
wenzelm@61051
   499
        handle ERROR msg => raise CONTEXT (msg, [], [], ths, NONE))
wenzelm@61045
   500
  | make_context ths (SOME ctxt) cert =
wenzelm@61044
   501
      let
wenzelm@61044
   502
        val thy_id = Context.certificate_theory_id cert;
wenzelm@61044
   503
        val thy_id' = Context.theory_id (Proof_Context.theory_of ctxt);
wenzelm@61044
   504
      in
wenzelm@61044
   505
        if Context.subthy_id (thy_id, thy_id') then Context.Proof ctxt
wenzelm@61051
   506
        else raise CONTEXT ("Bad context", [], [], ths, SOME (Context.Proof ctxt))
wenzelm@61044
   507
      end;
wenzelm@58950
   508
wenzelm@64981
   509
fun make_context_certificate ths opt_ctxt cert =
wenzelm@64981
   510
  let
wenzelm@64981
   511
    val context = make_context ths opt_ctxt cert;
wenzelm@64981
   512
    val cert' = Context.Certificate (Context.theory_of context);
wenzelm@64981
   513
  in (context, cert') end;
wenzelm@64981
   514
wenzelm@16945
   515
(*explicit weakening: maps |- B to A |- B*)
wenzelm@16945
   516
fun weaken raw_ct th =
wenzelm@16945
   517
  let
wenzelm@20261
   518
    val ct as Cterm {t = A, T, sorts, maxidx = maxidxA, ...} = adjust_maxidx_cterm ~1 raw_ct;
wenzelm@28321
   519
    val Thm (der, {tags, maxidx, shyps, hyps, tpairs, prop, ...}) = th;
wenzelm@16945
   520
  in
wenzelm@16945
   521
    if T <> propT then
wenzelm@16945
   522
      raise THM ("weaken: assumptions must have type prop", 0, [])
wenzelm@16945
   523
    else if maxidxA <> ~1 then
wenzelm@16945
   524
      raise THM ("weaken: assumptions may not contain schematic variables", maxidxA, [])
wenzelm@16945
   525
    else
wenzelm@28321
   526
      Thm (der,
wenzelm@61044
   527
       {cert = join_certificate1 (ct, th),
wenzelm@21646
   528
        tags = tags,
wenzelm@16945
   529
        maxidx = maxidx,
wenzelm@16945
   530
        shyps = Sorts.union sorts shyps,
wenzelm@28354
   531
        hyps = insert_hyps A hyps,
wenzelm@16945
   532
        tpairs = tpairs,
wenzelm@28321
   533
        prop = prop})
wenzelm@16945
   534
  end;
wenzelm@16656
   535
wenzelm@28624
   536
fun weaken_sorts raw_sorts ct =
wenzelm@28624
   537
  let
wenzelm@61044
   538
    val Cterm {cert, t, T, maxidx, sorts} = ct;
wenzelm@61050
   539
    val thy = theory_of_cterm ct;
wenzelm@28624
   540
    val more_sorts = Sorts.make (map (Sign.certify_sort thy) raw_sorts);
wenzelm@28624
   541
    val sorts' = Sorts.union sorts more_sorts;
wenzelm@61044
   542
  in Cterm {cert = cert, t = t, T = T, maxidx = maxidx, sorts = sorts'} end;
wenzelm@28624
   543
wenzelm@16656
   544
(*dangling sort constraints of a thm*)
wenzelm@31947
   545
fun extra_shyps (th as Thm (_, {shyps, ...})) =
wenzelm@31947
   546
  Sorts.subtract (fold_terms Sorts.insert_term th []) shyps;
wenzelm@28321
   547
wenzelm@28321
   548
wenzelm@28321
   549
wenzelm@32725
   550
(** derivations and promised proofs **)
wenzelm@28321
   551
wenzelm@32059
   552
fun make_deriv promises oracles thms proof =
wenzelm@32059
   553
  Deriv {promises = promises, body = PBody {oracles = oracles, thms = thms, proof = proof}};
wenzelm@28321
   554
wenzelm@37309
   555
val empty_deriv = make_deriv [] [] [] Proofterm.MinProof;
wenzelm@28321
   556
wenzelm@28330
   557
wenzelm@28354
   558
(* inference rules *)
wenzelm@28321
   559
wenzelm@28378
   560
fun promise_ord ((i, _), (j, _)) = int_ord (j, i);
wenzelm@28330
   561
wenzelm@52487
   562
fun deriv_rule2 f
wenzelm@32059
   563
    (Deriv {promises = ps1, body = PBody {oracles = oras1, thms = thms1, proof = prf1}})
wenzelm@32059
   564
    (Deriv {promises = ps2, body = PBody {oracles = oras2, thms = thms2, proof = prf2}}) =
wenzelm@28321
   565
  let
wenzelm@39687
   566
    val ps = Ord_List.union promise_ord ps1 ps2;
wenzelm@44334
   567
    val oras = Proofterm.unions_oracles [oras1, oras2];
wenzelm@44334
   568
    val thms = Proofterm.unions_thms [thms1, thms2];
wenzelm@28321
   569
    val prf =
wenzelm@52487
   570
      (case ! Proofterm.proofs of
wenzelm@28321
   571
        2 => f prf1 prf2
wenzelm@28804
   572
      | 1 => MinProof
wenzelm@28804
   573
      | 0 => MinProof
wenzelm@28321
   574
      | i => error ("Illegal level of detail for proof objects: " ^ string_of_int i));
wenzelm@32059
   575
  in make_deriv ps oras thms prf end;
wenzelm@28321
   576
wenzelm@52487
   577
fun deriv_rule1 f = deriv_rule2 (K f) empty_deriv;
wenzelm@52487
   578
fun deriv_rule0 prf = deriv_rule1 I (make_deriv [] [] [] prf);
wenzelm@28321
   579
wenzelm@36621
   580
fun deriv_rule_unconditional f (Deriv {promises, body = PBody {oracles, thms, proof}}) =
wenzelm@36621
   581
  make_deriv promises oracles thms (f proof);
wenzelm@36621
   582
wenzelm@1238
   583
wenzelm@32725
   584
(* fulfilled proofs *)
wenzelm@32725
   585
wenzelm@44331
   586
fun raw_body_of (Thm (Deriv {body, ...}, _)) = body;
wenzelm@44331
   587
fun raw_promises_of (Thm (Deriv {promises, ...}, _)) = promises;
wenzelm@44331
   588
wenzelm@44331
   589
fun join_promises [] = ()
wenzelm@44331
   590
  | join_promises promises = join_promises_of (Future.joins (map snd promises))
wenzelm@49008
   591
and join_promises_of thms = join_promises (Ord_List.make promise_ord (maps raw_promises_of thms));
wenzelm@32725
   592
wenzelm@61044
   593
fun fulfill_body (th as Thm (Deriv {promises, body}, _)) =
wenzelm@64571
   594
  let val fulfilled_promises = map #1 promises ~~ map fulfill_body (Future.joins (map #2 promises))
wenzelm@64571
   595
  in Proofterm.fulfill_norm_proof (theory_of_thm th) fulfilled_promises body end;
wenzelm@32725
   596
wenzelm@64574
   597
fun proof_bodies_of thms = (join_promises_of thms; map fulfill_body thms);
wenzelm@44333
   598
val proof_body_of = singleton proof_bodies_of;
wenzelm@44331
   599
val proof_of = Proofterm.proof_of o proof_body_of;
wenzelm@32725
   600
wenzelm@66168
   601
val consolidate = ignore o proof_bodies_of;
wenzelm@32725
   602
wenzelm@32725
   603
wenzelm@32725
   604
(* derivation status *)
wenzelm@32725
   605
wenzelm@50126
   606
fun peek_status (Thm (Deriv {promises, body}, _)) =
wenzelm@32725
   607
  let
wenzelm@32725
   608
    val ps = map (Future.peek o snd) promises;
wenzelm@32725
   609
    val bodies = body ::
wenzelm@44331
   610
      map_filter (fn SOME (Exn.Res th) => SOME (raw_body_of th) | _ => NONE) ps;
wenzelm@50126
   611
    val {oracle, unfinished, failed} = Proofterm.peek_status bodies;
wenzelm@32725
   612
  in
wenzelm@32725
   613
   {oracle = oracle,
wenzelm@32725
   614
    unfinished = unfinished orelse exists is_none ps,
wenzelm@32725
   615
    failed = failed orelse exists (fn SOME (Exn.Exn _) => true | _ => false) ps}
wenzelm@32725
   616
  end;
wenzelm@32725
   617
wenzelm@32725
   618
wenzelm@32725
   619
(* future rule *)
wenzelm@32725
   620
wenzelm@61044
   621
fun future_result i orig_cert orig_shyps orig_prop thm =
wenzelm@32725
   622
  let
wenzelm@36613
   623
    fun err msg = raise THM ("future_result: " ^ msg, 0, [thm]);
wenzelm@61044
   624
    val Thm (Deriv {promises, ...}, {cert, shyps, hyps, tpairs, prop, ...}) = thm;
wenzelm@36613
   625
wenzelm@61044
   626
    val _ = Context.eq_certificate (cert, orig_cert) orelse err "bad theory";
wenzelm@32725
   627
    val _ = prop aconv orig_prop orelse err "bad prop";
wenzelm@32725
   628
    val _ = null tpairs orelse err "bad tpairs";
wenzelm@32725
   629
    val _ = null hyps orelse err "bad hyps";
wenzelm@32725
   630
    val _ = Sorts.subset (shyps, orig_shyps) orelse err "bad shyps";
wenzelm@32725
   631
    val _ = forall (fn (j, _) => i <> j) promises orelse err "bad dependencies";
wenzelm@44331
   632
    val _ = join_promises promises;
wenzelm@32725
   633
  in thm end;
wenzelm@32725
   634
wenzelm@32725
   635
fun future future_thm ct =
wenzelm@32725
   636
  let
wenzelm@61044
   637
    val Cterm {cert = cert, t = prop, T, maxidx, sorts} = ct;
wenzelm@32725
   638
    val _ = T <> propT andalso raise CTERM ("future: prop expected", [ct]);
wenzelm@32725
   639
wenzelm@61050
   640
    val thy = theory_of_cterm ct;
wenzelm@32725
   641
    val i = serial ();
wenzelm@61044
   642
    val future = future_thm |> Future.map (future_result i cert sorts prop);
wenzelm@32725
   643
  in
wenzelm@37309
   644
    Thm (make_deriv [(i, future)] [] [] (Proofterm.promise_proof thy i prop),
wenzelm@61044
   645
     {cert = cert,
wenzelm@32725
   646
      tags = [],
wenzelm@32725
   647
      maxidx = maxidx,
wenzelm@32725
   648
      shyps = sorts,
wenzelm@32725
   649
      hyps = [],
wenzelm@32725
   650
      tpairs = [],
wenzelm@32725
   651
      prop = prop})
wenzelm@32725
   652
  end;
wenzelm@32725
   653
wenzelm@32725
   654
wenzelm@32725
   655
(* closed derivations with official name *)
wenzelm@32725
   656
wenzelm@41699
   657
(*non-deterministic, depends on unknown promises*)
wenzelm@64568
   658
fun derivation_closed (Thm (Deriv {body, ...}, _)) =
wenzelm@64568
   659
  Proofterm.compact_proof (Proofterm.proof_of body);
wenzelm@64568
   660
wenzelm@64568
   661
(*non-deterministic, depends on unknown promises*)
wenzelm@37297
   662
fun derivation_name (Thm (Deriv {body, ...}, {shyps, hyps, prop, ...})) =
wenzelm@37309
   663
  Proofterm.get_name shyps hyps prop (Proofterm.proof_of body);
wenzelm@32725
   664
wenzelm@36744
   665
fun name_derivation name (thm as Thm (der, args)) =
wenzelm@32725
   666
  let
wenzelm@32725
   667
    val Deriv {promises, body} = der;
wenzelm@61044
   668
    val {shyps, hyps, prop, tpairs, ...} = args;
wenzelm@32725
   669
    val _ = null tpairs orelse raise THM ("put_name: unsolved flex-flex constraints", 0, [thm]);
wenzelm@61044
   670
    val thy = theory_of_thm thm;
wenzelm@32725
   671
wenzelm@41700
   672
    val ps = map (apsnd (Future.map fulfill_body)) promises;
wenzelm@37309
   673
    val (pthm, proof) = Proofterm.thm_proof thy name shyps hyps prop ps body;
wenzelm@32725
   674
    val der' = make_deriv [] [] [pthm] proof;
wenzelm@32725
   675
  in Thm (der', args) end;
wenzelm@32725
   676
wenzelm@32725
   677
wenzelm@1238
   678
paulson@1529
   679
(** Axioms **)
wenzelm@387
   680
wenzelm@61044
   681
fun axiom thy0 name =
wenzelm@387
   682
  let
wenzelm@16425
   683
    fun get_ax thy =
wenzelm@59884
   684
      Name_Space.lookup (Theory.axiom_table thy) name
wenzelm@59884
   685
      |> Option.map (fn prop =>
wenzelm@24143
   686
           let
wenzelm@52487
   687
             val der = deriv_rule0 (Proofterm.axm_proof name prop);
wenzelm@61044
   688
             val cert = Context.Certificate thy;
wenzelm@24143
   689
             val maxidx = maxidx_of_term prop;
wenzelm@26640
   690
             val shyps = Sorts.insert_term prop [];
wenzelm@24143
   691
           in
wenzelm@61044
   692
             Thm (der, {cert = cert, tags = [],
wenzelm@28321
   693
               maxidx = maxidx, shyps = shyps, hyps = [], tpairs = [], prop = prop})
wenzelm@24143
   694
           end);
wenzelm@387
   695
  in
wenzelm@61044
   696
    (case get_first get_ax (Theory.nodes_of thy0) of
skalberg@15531
   697
      SOME thm => thm
wenzelm@61044
   698
    | NONE => raise THEORY ("No axiom " ^ quote name, [thy0]))
wenzelm@387
   699
  end;
wenzelm@387
   700
wenzelm@776
   701
(*return additional axioms of this theory node*)
wenzelm@776
   702
fun axioms_of thy =
wenzelm@56025
   703
  map (fn (name, _) => (name, axiom thy name)) (Theory.axioms_of thy);
wenzelm@776
   704
wenzelm@6089
   705
wenzelm@28804
   706
(* tags *)
wenzelm@6089
   707
wenzelm@21646
   708
val get_tags = #tags o rep_thm;
wenzelm@6089
   709
wenzelm@61044
   710
fun map_tags f (Thm (der, {cert, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@61044
   711
  Thm (der, {cert = cert, tags = f tags, maxidx = maxidx,
wenzelm@28321
   712
    shyps = shyps, hyps = hyps, tpairs = tpairs, prop = prop});
clasohm@0
   713
clasohm@0
   714
wenzelm@43795
   715
(* technical adjustments *)
wenzelm@43795
   716
wenzelm@61044
   717
fun norm_proof (th as Thm (der, args)) =
wenzelm@61044
   718
  Thm (deriv_rule1 (Proofterm.rew_proof (theory_of_thm th)) der, args);
berghofe@23781
   719
wenzelm@61044
   720
fun adjust_maxidx_thm i (th as Thm (der, {cert, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@20261
   721
  if maxidx = i then th
wenzelm@20261
   722
  else if maxidx < i then
wenzelm@61044
   723
    Thm (der, {maxidx = i, cert = cert, tags = tags, shyps = shyps,
wenzelm@28321
   724
      hyps = hyps, tpairs = tpairs, prop = prop})
wenzelm@20261
   725
  else
wenzelm@61044
   726
    Thm (der, {maxidx = Int.max (maxidx_tpairs tpairs (maxidx_of_term prop), i),
wenzelm@61044
   727
      cert = cert, tags = tags, shyps = shyps, hyps = hyps, tpairs = tpairs, prop = prop});
wenzelm@564
   728
wenzelm@387
   729
wenzelm@2509
   730
paulson@1529
   731
(*** Meta rules ***)
clasohm@0
   732
wenzelm@16601
   733
(** primitive rules **)
clasohm@0
   734
wenzelm@16656
   735
(*The assumption rule A |- A*)
wenzelm@16601
   736
fun assume raw_ct =
wenzelm@61044
   737
  let val Cterm {cert, t = prop, T, maxidx, sorts} = adjust_maxidx_cterm ~1 raw_ct in
wenzelm@16601
   738
    if T <> propT then
mengj@19230
   739
      raise THM ("assume: prop", 0, [])
wenzelm@16601
   740
    else if maxidx <> ~1 then
mengj@19230
   741
      raise THM ("assume: variables", maxidx, [])
wenzelm@52487
   742
    else Thm (deriv_rule0 (Proofterm.Hyp prop),
wenzelm@61044
   743
     {cert = cert,
wenzelm@21646
   744
      tags = [],
wenzelm@16601
   745
      maxidx = ~1,
wenzelm@16601
   746
      shyps = sorts,
wenzelm@16601
   747
      hyps = [prop],
wenzelm@16601
   748
      tpairs = [],
wenzelm@28321
   749
      prop = prop})
clasohm@0
   750
  end;
clasohm@0
   751
wenzelm@1220
   752
(*Implication introduction
wenzelm@3529
   753
    [A]
wenzelm@3529
   754
     :
wenzelm@3529
   755
     B
wenzelm@1220
   756
  -------
wenzelm@1220
   757
  A ==> B
wenzelm@1220
   758
*)
wenzelm@16601
   759
fun implies_intr
wenzelm@16679
   760
    (ct as Cterm {t = A, T, maxidx = maxidxA, sorts, ...})
wenzelm@28321
   761
    (th as Thm (der, {maxidx, hyps, shyps, tpairs, prop, ...})) =
wenzelm@16601
   762
  if T <> propT then
wenzelm@16601
   763
    raise THM ("implies_intr: assumptions must have type prop", 0, [th])
wenzelm@16601
   764
  else
wenzelm@52487
   765
    Thm (deriv_rule1 (Proofterm.implies_intr_proof A) der,
wenzelm@61044
   766
     {cert = join_certificate1 (ct, th),
wenzelm@52487
   767
      tags = [],
wenzelm@52487
   768
      maxidx = Int.max (maxidxA, maxidx),
wenzelm@52487
   769
      shyps = Sorts.union sorts shyps,
wenzelm@52487
   770
      hyps = remove_hyps A hyps,
wenzelm@52487
   771
      tpairs = tpairs,
wenzelm@52487
   772
      prop = Logic.mk_implies (A, prop)});
clasohm@0
   773
paulson@1529
   774
wenzelm@1220
   775
(*Implication elimination
wenzelm@1220
   776
  A ==> B    A
wenzelm@1220
   777
  ------------
wenzelm@1220
   778
        B
wenzelm@1220
   779
*)
wenzelm@16601
   780
fun implies_elim thAB thA =
wenzelm@16601
   781
  let
wenzelm@28321
   782
    val Thm (derA, {maxidx = maxA, hyps = hypsA, shyps = shypsA, tpairs = tpairsA,
wenzelm@28321
   783
      prop = propA, ...}) = thA
wenzelm@28321
   784
    and Thm (der, {maxidx, hyps, shyps, tpairs, prop, ...}) = thAB;
wenzelm@16601
   785
    fun err () = raise THM ("implies_elim: major premise", 0, [thAB, thA]);
wenzelm@16601
   786
  in
wenzelm@16601
   787
    case prop of
wenzelm@56245
   788
      Const ("Pure.imp", _) $ A $ B =>
wenzelm@20512
   789
        if A aconv propA then
wenzelm@52487
   790
          Thm (deriv_rule2 (curry Proofterm.%%) der derA,
wenzelm@61044
   791
           {cert = join_certificate2 (thAB, thA),
wenzelm@21646
   792
            tags = [],
wenzelm@16601
   793
            maxidx = Int.max (maxA, maxidx),
wenzelm@16601
   794
            shyps = Sorts.union shypsA shyps,
wenzelm@16601
   795
            hyps = union_hyps hypsA hyps,
wenzelm@16601
   796
            tpairs = union_tpairs tpairsA tpairs,
wenzelm@28321
   797
            prop = B})
wenzelm@16601
   798
        else err ()
wenzelm@16601
   799
    | _ => err ()
wenzelm@16601
   800
  end;
wenzelm@250
   801
wenzelm@1220
   802
(*Forall introduction.  The Free or Var x must not be free in the hypotheses.
wenzelm@16656
   803
    [x]
wenzelm@16656
   804
     :
wenzelm@16656
   805
     A
wenzelm@16656
   806
  ------
wenzelm@16656
   807
  !!x. A
wenzelm@1220
   808
*)
wenzelm@16601
   809
fun forall_intr
wenzelm@16601
   810
    (ct as Cterm {t = x, T, sorts, ...})
wenzelm@28321
   811
    (th as Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@16601
   812
  let
wenzelm@16601
   813
    fun result a =
wenzelm@52487
   814
      Thm (deriv_rule1 (Proofterm.forall_intr_proof x a) der,
wenzelm@61044
   815
       {cert = join_certificate1 (ct, th),
wenzelm@21646
   816
        tags = [],
wenzelm@16601
   817
        maxidx = maxidx,
wenzelm@16601
   818
        shyps = Sorts.union sorts shyps,
wenzelm@16601
   819
        hyps = hyps,
wenzelm@16601
   820
        tpairs = tpairs,
wenzelm@46217
   821
        prop = Logic.all_const T $ Abs (a, T, abstract_over (x, prop))});
wenzelm@21798
   822
    fun check_occs a x ts =
wenzelm@16847
   823
      if exists (fn t => Logic.occs (x, t)) ts then
wenzelm@21798
   824
        raise THM ("forall_intr: variable " ^ quote a ^ " free in assumptions", 0, [th])
wenzelm@16601
   825
      else ();
wenzelm@16601
   826
  in
wenzelm@52788
   827
    (case x of
wenzelm@21798
   828
      Free (a, _) => (check_occs a x hyps; check_occs a x (terms_of_tpairs tpairs); result a)
wenzelm@21798
   829
    | Var ((a, _), _) => (check_occs a x (terms_of_tpairs tpairs); result a)
wenzelm@52788
   830
    | _ => raise THM ("forall_intr: not a variable", 0, [th]))
clasohm@0
   831
  end;
clasohm@0
   832
wenzelm@1220
   833
(*Forall elimination
wenzelm@16656
   834
  !!x. A
wenzelm@1220
   835
  ------
wenzelm@1220
   836
  A[t/x]
wenzelm@1220
   837
*)
wenzelm@16601
   838
fun forall_elim
wenzelm@16601
   839
    (ct as Cterm {t, T, maxidx = maxt, sorts, ...})
wenzelm@28321
   840
    (th as Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@16601
   841
  (case prop of
wenzelm@56245
   842
    Const ("Pure.all", Type ("fun", [Type ("fun", [qary, _]), _])) $ A =>
wenzelm@16601
   843
      if T <> qary then
wenzelm@16601
   844
        raise THM ("forall_elim: type mismatch", 0, [th])
wenzelm@16601
   845
      else
wenzelm@52487
   846
        Thm (deriv_rule1 (Proofterm.% o rpair (SOME t)) der,
wenzelm@61044
   847
         {cert = join_certificate1 (ct, th),
wenzelm@52487
   848
          tags = [],
wenzelm@52487
   849
          maxidx = Int.max (maxidx, maxt),
wenzelm@52487
   850
          shyps = Sorts.union sorts shyps,
wenzelm@52487
   851
          hyps = hyps,
wenzelm@52487
   852
          tpairs = tpairs,
wenzelm@52487
   853
          prop = Term.betapply (A, t)})
wenzelm@16601
   854
  | _ => raise THM ("forall_elim: not quantified", 0, [th]));
clasohm@0
   855
clasohm@0
   856
wenzelm@1220
   857
(* Equality *)
clasohm@0
   858
wenzelm@16601
   859
(*Reflexivity
wenzelm@16601
   860
  t == t
wenzelm@16601
   861
*)
wenzelm@61044
   862
fun reflexive (Cterm {cert, t, T = _, maxidx, sorts}) =
wenzelm@52487
   863
  Thm (deriv_rule0 Proofterm.reflexive,
wenzelm@61044
   864
   {cert = cert,
wenzelm@21646
   865
    tags = [],
wenzelm@16601
   866
    maxidx = maxidx,
wenzelm@16601
   867
    shyps = sorts,
wenzelm@16601
   868
    hyps = [],
wenzelm@16601
   869
    tpairs = [],
wenzelm@28321
   870
    prop = Logic.mk_equals (t, t)});
clasohm@0
   871
wenzelm@16601
   872
(*Symmetry
wenzelm@16601
   873
  t == u
wenzelm@16601
   874
  ------
wenzelm@16601
   875
  u == t
wenzelm@1220
   876
*)
wenzelm@61044
   877
fun symmetric (th as Thm (der, {cert, maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@16601
   878
  (case prop of
wenzelm@56245
   879
    (eq as Const ("Pure.eq", _)) $ t $ u =>
wenzelm@52487
   880
      Thm (deriv_rule1 Proofterm.symmetric der,
wenzelm@61044
   881
       {cert = cert,
wenzelm@21646
   882
        tags = [],
wenzelm@16601
   883
        maxidx = maxidx,
wenzelm@16601
   884
        shyps = shyps,
wenzelm@16601
   885
        hyps = hyps,
wenzelm@16601
   886
        tpairs = tpairs,
wenzelm@28321
   887
        prop = eq $ u $ t})
wenzelm@16601
   888
    | _ => raise THM ("symmetric", 0, [th]));
clasohm@0
   889
wenzelm@16601
   890
(*Transitivity
wenzelm@16601
   891
  t1 == u    u == t2
wenzelm@16601
   892
  ------------------
wenzelm@16601
   893
       t1 == t2
wenzelm@1220
   894
*)
clasohm@0
   895
fun transitive th1 th2 =
wenzelm@16601
   896
  let
wenzelm@28321
   897
    val Thm (der1, {maxidx = max1, hyps = hyps1, shyps = shyps1, tpairs = tpairs1,
wenzelm@28321
   898
      prop = prop1, ...}) = th1
wenzelm@28321
   899
    and Thm (der2, {maxidx = max2, hyps = hyps2, shyps = shyps2, tpairs = tpairs2,
wenzelm@28321
   900
      prop = prop2, ...}) = th2;
wenzelm@16601
   901
    fun err msg = raise THM ("transitive: " ^ msg, 0, [th1, th2]);
wenzelm@16601
   902
  in
wenzelm@16601
   903
    case (prop1, prop2) of
wenzelm@56245
   904
      ((eq as Const ("Pure.eq", Type (_, [T, _]))) $ t1 $ u, Const ("Pure.eq", _) $ u' $ t2) =>
wenzelm@16601
   905
        if not (u aconv u') then err "middle term"
wenzelm@16601
   906
        else
wenzelm@52487
   907
          Thm (deriv_rule2 (Proofterm.transitive u T) der1 der2,
wenzelm@61044
   908
           {cert = join_certificate2 (th1, th2),
wenzelm@21646
   909
            tags = [],
wenzelm@16601
   910
            maxidx = Int.max (max1, max2),
wenzelm@16601
   911
            shyps = Sorts.union shyps1 shyps2,
wenzelm@16601
   912
            hyps = union_hyps hyps1 hyps2,
wenzelm@16601
   913
            tpairs = union_tpairs tpairs1 tpairs2,
wenzelm@28321
   914
            prop = eq $ t1 $ t2})
wenzelm@16601
   915
     | _ =>  err "premises"
clasohm@0
   916
  end;
clasohm@0
   917
wenzelm@16601
   918
(*Beta-conversion
wenzelm@16656
   919
  (%x. t)(u) == t[u/x]
wenzelm@16601
   920
  fully beta-reduces the term if full = true
berghofe@10416
   921
*)
wenzelm@61044
   922
fun beta_conversion full (Cterm {cert, t, T = _, maxidx, sorts}) =
wenzelm@16601
   923
  let val t' =
wenzelm@16601
   924
    if full then Envir.beta_norm t
wenzelm@16601
   925
    else
wenzelm@16601
   926
      (case t of Abs (_, _, bodt) $ u => subst_bound (u, bodt)
wenzelm@16601
   927
      | _ => raise THM ("beta_conversion: not a redex", 0, []));
wenzelm@16601
   928
  in
wenzelm@52487
   929
    Thm (deriv_rule0 Proofterm.reflexive,
wenzelm@61044
   930
     {cert = cert,
wenzelm@21646
   931
      tags = [],
wenzelm@16601
   932
      maxidx = maxidx,
wenzelm@16601
   933
      shyps = sorts,
wenzelm@16601
   934
      hyps = [],
wenzelm@16601
   935
      tpairs = [],
wenzelm@28321
   936
      prop = Logic.mk_equals (t, t')})
berghofe@10416
   937
  end;
berghofe@10416
   938
wenzelm@61044
   939
fun eta_conversion (Cterm {cert, t, T = _, maxidx, sorts}) =
wenzelm@52487
   940
  Thm (deriv_rule0 Proofterm.reflexive,
wenzelm@61044
   941
   {cert = cert,
wenzelm@21646
   942
    tags = [],
wenzelm@16601
   943
    maxidx = maxidx,
wenzelm@16601
   944
    shyps = sorts,
wenzelm@16601
   945
    hyps = [],
wenzelm@16601
   946
    tpairs = [],
wenzelm@28321
   947
    prop = Logic.mk_equals (t, Envir.eta_contract t)});
clasohm@0
   948
wenzelm@61044
   949
fun eta_long_conversion (Cterm {cert, t, T = _, maxidx, sorts}) =
wenzelm@52487
   950
  Thm (deriv_rule0 Proofterm.reflexive,
wenzelm@61044
   951
   {cert = cert,
wenzelm@23493
   952
    tags = [],
wenzelm@23493
   953
    maxidx = maxidx,
wenzelm@23493
   954
    shyps = sorts,
wenzelm@23493
   955
    hyps = [],
wenzelm@23493
   956
    tpairs = [],
wenzelm@52131
   957
    prop = Logic.mk_equals (t, Envir.eta_long [] t)});
wenzelm@23493
   958
clasohm@0
   959
(*The abstraction rule.  The Free or Var x must not be free in the hypotheses.
clasohm@0
   960
  The bound variable will be named "a" (since x will be something like x320)
wenzelm@16601
   961
      t == u
wenzelm@16601
   962
  --------------
wenzelm@16601
   963
  %x. t == %x. u
wenzelm@1220
   964
*)
wenzelm@16601
   965
fun abstract_rule a
wenzelm@16601
   966
    (Cterm {t = x, T, sorts, ...})
wenzelm@61044
   967
    (th as Thm (der, {cert, maxidx, hyps, shyps, tpairs, prop, ...})) =
wenzelm@16601
   968
  let
wenzelm@16601
   969
    val (t, u) = Logic.dest_equals prop
wenzelm@16601
   970
      handle TERM _ => raise THM ("abstract_rule: premise not an equality", 0, [th]);
wenzelm@16601
   971
    val result =
wenzelm@52487
   972
      Thm (deriv_rule1 (Proofterm.abstract_rule x a) der,
wenzelm@61044
   973
       {cert = cert,
wenzelm@21646
   974
        tags = [],
wenzelm@16601
   975
        maxidx = maxidx,
wenzelm@16601
   976
        shyps = Sorts.union sorts shyps,
wenzelm@16601
   977
        hyps = hyps,
wenzelm@16601
   978
        tpairs = tpairs,
wenzelm@16601
   979
        prop = Logic.mk_equals
wenzelm@28321
   980
          (Abs (a, T, abstract_over (x, t)), Abs (a, T, abstract_over (x, u)))});
wenzelm@21798
   981
    fun check_occs a x ts =
wenzelm@16847
   982
      if exists (fn t => Logic.occs (x, t)) ts then
wenzelm@21798
   983
        raise THM ("abstract_rule: variable " ^ quote a ^ " free in assumptions", 0, [th])
wenzelm@16601
   984
      else ();
wenzelm@16601
   985
  in
wenzelm@52788
   986
    (case x of
wenzelm@21798
   987
      Free (a, _) => (check_occs a x hyps; check_occs a x (terms_of_tpairs tpairs); result)
wenzelm@21798
   988
    | Var ((a, _), _) => (check_occs a x (terms_of_tpairs tpairs); result)
wenzelm@52788
   989
    | _ => raise THM ("abstract_rule: not a variable", 0, [th]))
clasohm@0
   990
  end;
clasohm@0
   991
clasohm@0
   992
(*The combination rule
wenzelm@3529
   993
  f == g  t == u
wenzelm@3529
   994
  --------------
wenzelm@16601
   995
    f t == g u
wenzelm@1220
   996
*)
clasohm@0
   997
fun combination th1 th2 =
wenzelm@16601
   998
  let
wenzelm@28321
   999
    val Thm (der1, {maxidx = max1, shyps = shyps1, hyps = hyps1, tpairs = tpairs1,
wenzelm@28321
  1000
      prop = prop1, ...}) = th1
wenzelm@28321
  1001
    and Thm (der2, {maxidx = max2, shyps = shyps2, hyps = hyps2, tpairs = tpairs2,
wenzelm@28321
  1002
      prop = prop2, ...}) = th2;
wenzelm@16601
  1003
    fun chktypes fT tT =
wenzelm@16601
  1004
      (case fT of
wenzelm@32784
  1005
        Type ("fun", [T1, _]) =>
wenzelm@16601
  1006
          if T1 <> tT then
wenzelm@16601
  1007
            raise THM ("combination: types", 0, [th1, th2])
wenzelm@16601
  1008
          else ()
wenzelm@16601
  1009
      | _ => raise THM ("combination: not function type", 0, [th1, th2]));
wenzelm@16601
  1010
  in
wenzelm@52788
  1011
    (case (prop1, prop2) of
wenzelm@56245
  1012
      (Const ("Pure.eq", Type ("fun", [fT, _])) $ f $ g,
wenzelm@56245
  1013
       Const ("Pure.eq", Type ("fun", [tT, _])) $ t $ u) =>
wenzelm@16601
  1014
        (chktypes fT tT;
wenzelm@52487
  1015
          Thm (deriv_rule2 (Proofterm.combination f g t u fT) der1 der2,
wenzelm@61044
  1016
           {cert = join_certificate2 (th1, th2),
wenzelm@21646
  1017
            tags = [],
wenzelm@16601
  1018
            maxidx = Int.max (max1, max2),
wenzelm@16601
  1019
            shyps = Sorts.union shyps1 shyps2,
wenzelm@16601
  1020
            hyps = union_hyps hyps1 hyps2,
wenzelm@16601
  1021
            tpairs = union_tpairs tpairs1 tpairs2,
wenzelm@28321
  1022
            prop = Logic.mk_equals (f $ t, g $ u)}))
wenzelm@52788
  1023
     | _ => raise THM ("combination: premises", 0, [th1, th2]))
clasohm@0
  1024
  end;
clasohm@0
  1025
wenzelm@16601
  1026
(*Equality introduction
wenzelm@3529
  1027
  A ==> B  B ==> A
wenzelm@3529
  1028
  ----------------
wenzelm@3529
  1029
       A == B
wenzelm@1220
  1030
*)
clasohm@0
  1031
fun equal_intr th1 th2 =
wenzelm@16601
  1032
  let
wenzelm@28321
  1033
    val Thm (der1, {maxidx = max1, shyps = shyps1, hyps = hyps1, tpairs = tpairs1,
wenzelm@28321
  1034
      prop = prop1, ...}) = th1
wenzelm@28321
  1035
    and Thm (der2, {maxidx = max2, shyps = shyps2, hyps = hyps2, tpairs = tpairs2,
wenzelm@28321
  1036
      prop = prop2, ...}) = th2;
wenzelm@16601
  1037
    fun err msg = raise THM ("equal_intr: " ^ msg, 0, [th1, th2]);
wenzelm@16601
  1038
  in
wenzelm@52788
  1039
    (case (prop1, prop2) of
wenzelm@56245
  1040
      (Const("Pure.imp", _) $ A $ B, Const("Pure.imp", _) $ B' $ A') =>
wenzelm@16601
  1041
        if A aconv A' andalso B aconv B' then
wenzelm@52487
  1042
          Thm (deriv_rule2 (Proofterm.equal_intr A B) der1 der2,
wenzelm@61044
  1043
           {cert = join_certificate2 (th1, th2),
wenzelm@21646
  1044
            tags = [],
wenzelm@16601
  1045
            maxidx = Int.max (max1, max2),
wenzelm@16601
  1046
            shyps = Sorts.union shyps1 shyps2,
wenzelm@16601
  1047
            hyps = union_hyps hyps1 hyps2,
wenzelm@16601
  1048
            tpairs = union_tpairs tpairs1 tpairs2,
wenzelm@28321
  1049
            prop = Logic.mk_equals (A, B)})
wenzelm@16601
  1050
        else err "not equal"
wenzelm@52788
  1051
    | _ =>  err "premises")
paulson@1529
  1052
  end;
paulson@1529
  1053
paulson@1529
  1054
(*The equal propositions rule
wenzelm@3529
  1055
  A == B  A
paulson@1529
  1056
  ---------
paulson@1529
  1057
      B
paulson@1529
  1058
*)
paulson@1529
  1059
fun equal_elim th1 th2 =
wenzelm@16601
  1060
  let
wenzelm@28321
  1061
    val Thm (der1, {maxidx = max1, shyps = shyps1, hyps = hyps1,
wenzelm@28321
  1062
      tpairs = tpairs1, prop = prop1, ...}) = th1
wenzelm@28321
  1063
    and Thm (der2, {maxidx = max2, shyps = shyps2, hyps = hyps2,
wenzelm@28321
  1064
      tpairs = tpairs2, prop = prop2, ...}) = th2;
wenzelm@16601
  1065
    fun err msg = raise THM ("equal_elim: " ^ msg, 0, [th1, th2]);
wenzelm@16601
  1066
  in
wenzelm@52788
  1067
    (case prop1 of
wenzelm@56245
  1068
      Const ("Pure.eq", _) $ A $ B =>
wenzelm@16601
  1069
        if prop2 aconv A then
wenzelm@52487
  1070
          Thm (deriv_rule2 (Proofterm.equal_elim A B) der1 der2,
wenzelm@61044
  1071
           {cert = join_certificate2 (th1, th2),
wenzelm@21646
  1072
            tags = [],
wenzelm@16601
  1073
            maxidx = Int.max (max1, max2),
wenzelm@16601
  1074
            shyps = Sorts.union shyps1 shyps2,
wenzelm@16601
  1075
            hyps = union_hyps hyps1 hyps2,
wenzelm@16601
  1076
            tpairs = union_tpairs tpairs1 tpairs2,
wenzelm@28321
  1077
            prop = B})
wenzelm@16601
  1078
        else err "not equal"
wenzelm@52789
  1079
     | _ =>  err "major premise")
paulson@1529
  1080
  end;
clasohm@0
  1081
wenzelm@1220
  1082
wenzelm@1220
  1083
clasohm@0
  1084
(**** Derived rules ****)
clasohm@0
  1085
wenzelm@16601
  1086
(*Smash unifies the list of term pairs leaving no flex-flex pairs.
wenzelm@24143
  1087
  Instantiates the theorem and deletes trivial tpairs.  Resulting
wenzelm@24143
  1088
  sequence may contain multiple elements if the tpairs are not all
wenzelm@24143
  1089
  flex-flex.*)
wenzelm@61044
  1090
fun flexflex_rule opt_ctxt (th as Thm (der, {cert, maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@64981
  1091
  let val (context, cert') = make_context_certificate [th] opt_ctxt cert in
wenzelm@64981
  1092
    Unify.smash_unifiers context tpairs (Envir.empty maxidx)
wenzelm@64981
  1093
    |> Seq.map (fn env =>
wenzelm@64981
  1094
        if Envir.is_empty env then th
wenzelm@64981
  1095
        else
wenzelm@64981
  1096
          let
wenzelm@64981
  1097
            val tpairs' = tpairs |> map (apply2 (Envir.norm_term env))
wenzelm@64981
  1098
              (*remove trivial tpairs, of the form t==t*)
wenzelm@64981
  1099
              |> filter_out (op aconv);
wenzelm@64981
  1100
            val der' = deriv_rule1 (Proofterm.norm_proof' env) der;
wenzelm@64981
  1101
            val prop' = Envir.norm_term env prop;
wenzelm@64981
  1102
            val maxidx = maxidx_tpairs tpairs' (maxidx_of_term prop');
wenzelm@64981
  1103
            val shyps = Envir.insert_sorts env shyps;
wenzelm@64981
  1104
          in
wenzelm@64981
  1105
            Thm (der', {cert = cert', tags = [], maxidx = maxidx,
wenzelm@64981
  1106
              shyps = shyps, hyps = hyps, tpairs = tpairs', prop = prop'})
wenzelm@64981
  1107
          end)
wenzelm@64981
  1108
  end;
wenzelm@16601
  1109
clasohm@0
  1110
wenzelm@19910
  1111
(*Generalization of fixed variables
wenzelm@19910
  1112
           A
wenzelm@19910
  1113
  --------------------
wenzelm@19910
  1114
  A[?'a/'a, ?x/x, ...]
wenzelm@19910
  1115
*)
wenzelm@19910
  1116
wenzelm@19910
  1117
fun generalize ([], []) _ th = th
wenzelm@19910
  1118
  | generalize (tfrees, frees) idx th =
wenzelm@19910
  1119
      let
wenzelm@61044
  1120
        val Thm (der, {cert, maxidx, shyps, hyps, tpairs, prop, ...}) = th;
wenzelm@19910
  1121
        val _ = idx <= maxidx andalso raise THM ("generalize: bad index", idx, [th]);
wenzelm@19910
  1122
wenzelm@33697
  1123
        val bad_type =
wenzelm@33697
  1124
          if null tfrees then K false
wenzelm@33697
  1125
          else Term.exists_subtype (fn TFree (a, _) => member (op =) tfrees a | _ => false);
wenzelm@19910
  1126
        fun bad_term (Free (x, T)) = bad_type T orelse member (op =) frees x
wenzelm@19910
  1127
          | bad_term (Var (_, T)) = bad_type T
wenzelm@19910
  1128
          | bad_term (Const (_, T)) = bad_type T
wenzelm@19910
  1129
          | bad_term (Abs (_, T, t)) = bad_type T orelse bad_term t
wenzelm@19910
  1130
          | bad_term (t $ u) = bad_term t orelse bad_term u
wenzelm@19910
  1131
          | bad_term (Bound _) = false;
wenzelm@19910
  1132
        val _ = exists bad_term hyps andalso
wenzelm@19910
  1133
          raise THM ("generalize: variable free in assumptions", 0, [th]);
wenzelm@19910
  1134
wenzelm@31977
  1135
        val gen = Term_Subst.generalize (tfrees, frees) idx;
wenzelm@19910
  1136
        val prop' = gen prop;
wenzelm@59058
  1137
        val tpairs' = map (apply2 gen) tpairs;
wenzelm@19910
  1138
        val maxidx' = maxidx_tpairs tpairs' (maxidx_of_term prop');
wenzelm@19910
  1139
      in
wenzelm@52487
  1140
        Thm (deriv_rule1 (Proofterm.generalize (tfrees, frees) idx) der,
wenzelm@61044
  1141
         {cert = cert,
wenzelm@21646
  1142
          tags = [],
wenzelm@19910
  1143
          maxidx = maxidx',
wenzelm@19910
  1144
          shyps = shyps,
wenzelm@19910
  1145
          hyps = hyps,
wenzelm@19910
  1146
          tpairs = tpairs',
wenzelm@28321
  1147
          prop = prop'})
wenzelm@19910
  1148
      end;
wenzelm@19910
  1149
wenzelm@19910
  1150
wenzelm@22584
  1151
(*Instantiation of schematic variables
wenzelm@16656
  1152
           A
wenzelm@16656
  1153
  --------------------
wenzelm@16656
  1154
  A[t1/v1, ..., tn/vn]
wenzelm@1220
  1155
*)
clasohm@0
  1156
wenzelm@6928
  1157
local
wenzelm@6928
  1158
wenzelm@26939
  1159
fun pretty_typing thy t T = Pretty.block
wenzelm@26939
  1160
  [Syntax.pretty_term_global thy t, Pretty.str " ::", Pretty.brk 1, Syntax.pretty_typ_global thy T];
berghofe@15797
  1161
wenzelm@61044
  1162
fun add_inst (v as (_, T), cu) (cert, sorts) =
wenzelm@6928
  1163
  let
wenzelm@61044
  1164
    val Cterm {t = u, T = U, cert = cert2, sorts = sorts_u, maxidx = maxidx_u, ...} = cu;
wenzelm@61044
  1165
    val cert' = Context.join_certificate (cert, cert2);
wenzelm@16884
  1166
    val sorts' = Sorts.union sorts_u sorts;
wenzelm@3967
  1167
  in
wenzelm@61044
  1168
    if T = U then ((v, (u, maxidx_u)), (cert', sorts'))
wenzelm@60642
  1169
    else
wenzelm@61044
  1170
      let
wenzelm@61044
  1171
        val msg =
wenzelm@61044
  1172
          (case cert' of
wenzelm@61044
  1173
            Context.Certificate thy' =>
wenzelm@61044
  1174
              Pretty.string_of (Pretty.block
wenzelm@61044
  1175
               [Pretty.str "instantiate: type conflict",
wenzelm@61044
  1176
                Pretty.fbrk, pretty_typing thy' (Var v) T,
wenzelm@61044
  1177
                Pretty.fbrk, pretty_typing thy' u U])
wenzelm@61044
  1178
          | Context.Certificate_Id _ => "instantiate: type conflict");
wenzelm@61044
  1179
      in raise TYPE (msg, [T, U], [Var v, u]) end
clasohm@0
  1180
  end;
clasohm@0
  1181
wenzelm@61044
  1182
fun add_instT (v as (_, S), cU) (cert, sorts) =
wenzelm@16656
  1183
  let
wenzelm@61044
  1184
    val Ctyp {T = U, cert = cert2, sorts = sorts_U, maxidx = maxidx_U, ...} = cU;
wenzelm@61044
  1185
    val cert' = Context.join_certificate (cert, cert2);
wenzelm@61051
  1186
    val thy' = Context.certificate_theory cert'
wenzelm@61051
  1187
      handle ERROR msg => raise CONTEXT (msg, [cU], [], [], NONE);
wenzelm@16884
  1188
    val sorts' = Sorts.union sorts_U sorts;
wenzelm@16656
  1189
  in
wenzelm@61044
  1190
    if Sign.of_sort thy' (U, S) then ((v, (U, maxidx_U)), (cert', sorts'))
wenzelm@60642
  1191
    else raise TYPE ("Type not of sort " ^ Syntax.string_of_sort_global thy' S, [U], [])
wenzelm@16656
  1192
  end;
clasohm@0
  1193
wenzelm@6928
  1194
in
wenzelm@6928
  1195
wenzelm@16601
  1196
(*Left-to-right replacements: ctpairs = [..., (vi, ti), ...].
clasohm@0
  1197
  Instantiates distinct Vars by terms of same type.
wenzelm@16601
  1198
  Does NOT normalize the resulting theorem!*)
paulson@1529
  1199
fun instantiate ([], []) th = th
wenzelm@16884
  1200
  | instantiate (instT, inst) th =
wenzelm@16656
  1201
      let
wenzelm@61044
  1202
        val Thm (der, {cert, hyps, shyps, tpairs, prop, ...}) = th;
wenzelm@61044
  1203
        val (inst', (instT', (cert', shyps'))) =
wenzelm@61051
  1204
          (cert, shyps) |> fold_map add_inst inst ||> fold_map add_instT instT
wenzelm@61053
  1205
            handle CONTEXT (msg, cTs, cts, ths, context) =>
wenzelm@61053
  1206
              raise CONTEXT (msg, cTs, cts, th :: ths, context);
wenzelm@31977
  1207
        val subst = Term_Subst.instantiate_maxidx (instT', inst');
wenzelm@20512
  1208
        val (prop', maxidx1) = subst prop ~1;
wenzelm@20512
  1209
        val (tpairs', maxidx') =
wenzelm@20512
  1210
          fold_map (fn (t, u) => fn i => subst t i ||>> subst u) tpairs maxidx1;
wenzelm@16656
  1211
      in
wenzelm@52487
  1212
        Thm (deriv_rule1
wenzelm@37309
  1213
          (fn d => Proofterm.instantiate (map (apsnd #1) instT', map (apsnd #1) inst') d) der,
wenzelm@61044
  1214
         {cert = cert',
wenzelm@21646
  1215
          tags = [],
wenzelm@20545
  1216
          maxidx = maxidx',
wenzelm@20545
  1217
          shyps = shyps',
wenzelm@20545
  1218
          hyps = hyps,
wenzelm@20545
  1219
          tpairs = tpairs',
wenzelm@28321
  1220
          prop = prop'})
wenzelm@16656
  1221
      end
wenzelm@16656
  1222
      handle TYPE (msg, _, _) => raise THM (msg, 0, [th]);
wenzelm@6928
  1223
wenzelm@22584
  1224
fun instantiate_cterm ([], []) ct = ct
wenzelm@22584
  1225
  | instantiate_cterm (instT, inst) ct =
wenzelm@22584
  1226
      let
wenzelm@61044
  1227
        val Cterm {cert, t, T, sorts, ...} = ct;
wenzelm@61044
  1228
        val (inst', (instT', (cert', sorts'))) =
wenzelm@61044
  1229
          (cert, sorts) |> fold_map add_inst inst ||> fold_map add_instT instT;
wenzelm@31977
  1230
        val subst = Term_Subst.instantiate_maxidx (instT', inst');
wenzelm@31977
  1231
        val substT = Term_Subst.instantiateT_maxidx instT';
wenzelm@22584
  1232
        val (t', maxidx1) = subst t ~1;
wenzelm@22584
  1233
        val (T', maxidx') = substT T maxidx1;
wenzelm@61044
  1234
      in Cterm {cert = cert', t = t', T = T', sorts = sorts', maxidx = maxidx'} end
wenzelm@22584
  1235
      handle TYPE (msg, _, _) => raise CTERM (msg, [ct]);
wenzelm@22584
  1236
wenzelm@6928
  1237
end;
wenzelm@6928
  1238
clasohm@0
  1239
wenzelm@16601
  1240
(*The trivial implication A ==> A, justified by assume and forall rules.
wenzelm@16601
  1241
  A can contain Vars, not so for assume!*)
wenzelm@61044
  1242
fun trivial (Cterm {cert, t = A, T, maxidx, sorts}) =
wenzelm@16601
  1243
  if T <> propT then
wenzelm@16601
  1244
    raise THM ("trivial: the term must have type prop", 0, [])
wenzelm@16601
  1245
  else
wenzelm@52487
  1246
    Thm (deriv_rule0 (Proofterm.AbsP ("H", NONE, Proofterm.PBound 0)),
wenzelm@61044
  1247
     {cert = cert,
wenzelm@21646
  1248
      tags = [],
wenzelm@16601
  1249
      maxidx = maxidx,
wenzelm@16601
  1250
      shyps = sorts,
wenzelm@16601
  1251
      hyps = [],
wenzelm@16601
  1252
      tpairs = [],
wenzelm@28321
  1253
      prop = Logic.mk_implies (A, A)});
clasohm@0
  1254
wenzelm@31944
  1255
(*Axiom-scheme reflecting signature contents
wenzelm@31944
  1256
        T :: c
wenzelm@31944
  1257
  -------------------
wenzelm@31944
  1258
  OFCLASS(T, c_class)
wenzelm@31944
  1259
*)
wenzelm@31944
  1260
fun of_class (cT, raw_c) =
wenzelm@24143
  1261
  let
wenzelm@61044
  1262
    val Ctyp {cert, T, ...} = cT;
wenzelm@61051
  1263
    val thy = Context.certificate_theory cert
wenzelm@61051
  1264
      handle ERROR msg => raise CONTEXT (msg, [cT], [], [], NONE);
wenzelm@31903
  1265
    val c = Sign.certify_class thy raw_c;
wenzelm@59621
  1266
    val Cterm {t = prop, maxidx, sorts, ...} = global_cterm_of thy (Logic.mk_of_class (T, c));
wenzelm@399
  1267
  in
wenzelm@31944
  1268
    if Sign.of_sort thy (T, [c]) then
wenzelm@52487
  1269
      Thm (deriv_rule0 (Proofterm.OfClass (T, c)),
wenzelm@61044
  1270
       {cert = cert,
wenzelm@31944
  1271
        tags = [],
wenzelm@31944
  1272
        maxidx = maxidx,
wenzelm@31944
  1273
        shyps = sorts,
wenzelm@31944
  1274
        hyps = [],
wenzelm@31944
  1275
        tpairs = [],
wenzelm@31944
  1276
        prop = prop})
wenzelm@31944
  1277
    else raise THM ("of_class: type not of class " ^ Syntax.string_of_sort_global thy [c], 0, [])
wenzelm@399
  1278
  end;
wenzelm@399
  1279
wenzelm@36614
  1280
(*Remove extra sorts that are witnessed by type signature information*)
wenzelm@36614
  1281
fun strip_shyps (thm as Thm (_, {shyps = [], ...})) = thm
wenzelm@61044
  1282
  | strip_shyps (thm as Thm (der, {cert, tags, maxidx, shyps, hyps, tpairs, prop})) =
wenzelm@36614
  1283
      let
wenzelm@61044
  1284
        val thy = theory_of_thm thm;
wenzelm@36621
  1285
        val algebra = Sign.classes_of thy;
wenzelm@36621
  1286
wenzelm@36621
  1287
        val present = (fold_terms o fold_types o fold_atyps_sorts) (insert (eq_fst op =)) thm [];
wenzelm@36614
  1288
        val extra = fold (Sorts.remove_sort o #2) present shyps;
wenzelm@36614
  1289
        val witnessed = Sign.witness_sorts thy present extra;
wenzelm@36614
  1290
        val extra' = fold (Sorts.remove_sort o #2) witnessed extra
wenzelm@36621
  1291
          |> Sorts.minimal_sorts algebra;
wenzelm@36614
  1292
        val shyps' = fold (Sorts.insert_sort o #2) present extra';
wenzelm@36614
  1293
      in
wenzelm@37309
  1294
        Thm (deriv_rule_unconditional
wenzelm@37309
  1295
          (Proofterm.strip_shyps_proof algebra present witnessed extra') der,
wenzelm@61044
  1296
         {cert = cert, tags = tags, maxidx = maxidx,
wenzelm@36614
  1297
          shyps = shyps', hyps = hyps, tpairs = tpairs, prop = prop})
wenzelm@36614
  1298
      end;
wenzelm@36614
  1299
wenzelm@36769
  1300
(*Internalize sort constraints of type variables*)
wenzelm@36883
  1301
fun unconstrainT (thm as Thm (der, args)) =
wenzelm@19505
  1302
  let
wenzelm@36883
  1303
    val Deriv {promises, body} = der;
wenzelm@61044
  1304
    val {cert, shyps, hyps, tpairs, prop, ...} = args;
wenzelm@61044
  1305
    val thy = theory_of_thm thm;
wenzelm@36883
  1306
wenzelm@36769
  1307
    fun err msg = raise THM ("unconstrainT: " ^ msg, 0, [thm]);
wenzelm@36769
  1308
    val _ = null hyps orelse err "illegal hyps";
wenzelm@36769
  1309
    val _ = null tpairs orelse err "unsolved flex-flex constraints";
wenzelm@36769
  1310
    val tfrees = rev (Term.add_tfree_names prop []);
wenzelm@36769
  1311
    val _ = null tfrees orelse err ("illegal free type variables " ^ commas_quote tfrees);
wenzelm@36769
  1312
wenzelm@41700
  1313
    val ps = map (apsnd (Future.map fulfill_body)) promises;
wenzelm@64573
  1314
    val (pthm, proof) = Proofterm.unconstrain_thm_proof thy shyps prop ps body;
wenzelm@36883
  1315
    val der' = make_deriv [] [] [pthm] proof;
wenzelm@64573
  1316
    val prop' = Proofterm.thm_node_prop (#2 pthm);
wenzelm@19505
  1317
  in
wenzelm@36883
  1318
    Thm (der',
wenzelm@61044
  1319
     {cert = cert,
wenzelm@21646
  1320
      tags = [],
wenzelm@36769
  1321
      maxidx = maxidx_of_term prop',
wenzelm@36769
  1322
      shyps = [[]],  (*potentially redundant*)
wenzelm@36883
  1323
      hyps = [],
wenzelm@36883
  1324
      tpairs = [],
wenzelm@36769
  1325
      prop = prop'})
wenzelm@19505
  1326
  end;
wenzelm@399
  1327
wenzelm@63611
  1328
(*Replace all TFrees not fixed or in the hyps by new TVars*)
wenzelm@61044
  1329
fun varifyT_global' fixed (Thm (der, {cert, maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@12500
  1330
  let
wenzelm@29272
  1331
    val tfrees = fold Term.add_tfrees hyps fixed;
berghofe@13658
  1332
    val prop1 = attach_tpairs tpairs prop;
wenzelm@35845
  1333
    val (al, prop2) = Type.varify_global tfrees prop1;
wenzelm@16601
  1334
    val (ts, prop3) = Logic.strip_prems (length tpairs, [], prop2);
wenzelm@16601
  1335
  in
wenzelm@52487
  1336
    (al, Thm (deriv_rule1 (Proofterm.varify_proof prop tfrees) der,
wenzelm@61044
  1337
     {cert = cert,
wenzelm@21646
  1338
      tags = [],
wenzelm@16601
  1339
      maxidx = Int.max (0, maxidx),
wenzelm@16601
  1340
      shyps = shyps,
wenzelm@16601
  1341
      hyps = hyps,
wenzelm@16601
  1342
      tpairs = rev (map Logic.dest_equals ts),
wenzelm@28321
  1343
      prop = prop3}))
wenzelm@28321
  1344
  end;
wenzelm@28321
  1345
wenzelm@35845
  1346
val varifyT_global = #2 o varifyT_global' [];
wenzelm@28321
  1347
wenzelm@63611
  1348
(*Replace all TVars by TFrees that are often new*)
wenzelm@61044
  1349
fun legacy_freezeT (Thm (der, {cert, shyps, hyps, tpairs, prop, ...})) =
wenzelm@28321
  1350
  let
wenzelm@28321
  1351
    val prop1 = attach_tpairs tpairs prop;
wenzelm@33832
  1352
    val prop2 = Type.legacy_freeze prop1;
wenzelm@28321
  1353
    val (ts, prop3) = Logic.strip_prems (length tpairs, [], prop2);
wenzelm@28321
  1354
  in
wenzelm@52487
  1355
    Thm (deriv_rule1 (Proofterm.legacy_freezeT prop1) der,
wenzelm@61044
  1356
     {cert = cert,
wenzelm@28321
  1357
      tags = [],
wenzelm@28321
  1358
      maxidx = maxidx_of_term prop2,
wenzelm@28321
  1359
      shyps = shyps,
wenzelm@28321
  1360
      hyps = hyps,
wenzelm@28321
  1361
      tpairs = rev (map Logic.dest_equals ts),
wenzelm@18127
  1362
      prop = prop3})
clasohm@0
  1363
  end;
clasohm@0
  1364
clasohm@0
  1365
clasohm@0
  1366
(*** Inference rules for tactics ***)
clasohm@0
  1367
clasohm@0
  1368
(*Destruct proof state into constraints, other goals, goal(i), rest *)
wenzelm@28321
  1369
fun dest_state (state as Thm (_, {prop,tpairs,...}), i) =
berghofe@13658
  1370
  (case  Logic.strip_prems(i, [], prop) of
berghofe@13658
  1371
      (B::rBs, C) => (tpairs, rev rBs, B, C)
berghofe@13658
  1372
    | _ => raise THM("dest_state", i, [state]))
clasohm@0
  1373
  handle TERM _ => raise THM("dest_state", i, [state]);
clasohm@0
  1374
wenzelm@46255
  1375
(*Prepare orule for resolution by lifting it over the parameters and
wenzelm@52487
  1376
assumptions of goal.*)
wenzelm@18035
  1377
fun lift_rule goal orule =
wenzelm@16601
  1378
  let
wenzelm@18035
  1379
    val Cterm {t = gprop, T, maxidx = gmax, sorts, ...} = goal;
wenzelm@18035
  1380
    val inc = gmax + 1;
wenzelm@18035
  1381
    val lift_abs = Logic.lift_abs inc gprop;
wenzelm@18035
  1382
    val lift_all = Logic.lift_all inc gprop;
wenzelm@28321
  1383
    val Thm (der, {maxidx, shyps, hyps, tpairs, prop, ...}) = orule;
wenzelm@16601
  1384
    val (As, B) = Logic.strip_horn prop;
wenzelm@16601
  1385
  in
wenzelm@18035
  1386
    if T <> propT then raise THM ("lift_rule: the term must have type prop", 0, [])
wenzelm@18035
  1387
    else
wenzelm@52487
  1388
      Thm (deriv_rule1 (Proofterm.lift_proof gprop inc prop) der,
wenzelm@61044
  1389
       {cert = join_certificate1 (goal, orule),
wenzelm@21646
  1390
        tags = [],
wenzelm@18035
  1391
        maxidx = maxidx + inc,
wenzelm@18035
  1392
        shyps = Sorts.union shyps sorts,  (*sic!*)
wenzelm@18035
  1393
        hyps = hyps,
wenzelm@59058
  1394
        tpairs = map (apply2 lift_abs) tpairs,
wenzelm@28321
  1395
        prop = Logic.list_implies (map lift_all As, lift_all B)})
clasohm@0
  1396
  end;
clasohm@0
  1397
wenzelm@61044
  1398
fun incr_indexes i (thm as Thm (der, {cert, maxidx, shyps, hyps, tpairs, prop, ...})) =
wenzelm@16601
  1399
  if i < 0 then raise THM ("negative increment", 0, [thm])
wenzelm@16601
  1400
  else if i = 0 then thm
wenzelm@16601
  1401
  else
wenzelm@52487
  1402
    Thm (deriv_rule1 (Proofterm.incr_indexes i) der,
wenzelm@61044
  1403
     {cert = cert,
wenzelm@21646
  1404
      tags = [],
wenzelm@16601
  1405
      maxidx = maxidx + i,
wenzelm@16601
  1406
      shyps = shyps,
wenzelm@16601
  1407
      hyps = hyps,
wenzelm@59787
  1408
      tpairs = map (apply2 (Logic.incr_indexes ([], [], i))) tpairs,
wenzelm@59787
  1409
      prop = Logic.incr_indexes ([], [], i) prop});
berghofe@10416
  1410
clasohm@0
  1411
(*Solve subgoal Bi of proof state B1...Bn/C by assumption. *)
wenzelm@58950
  1412
fun assumption opt_ctxt i state =
wenzelm@16601
  1413
  let
wenzelm@61044
  1414
    val Thm (der, {cert, maxidx, shyps, hyps, ...}) = state;
wenzelm@64981
  1415
    val (context, cert') = make_context_certificate [state] opt_ctxt cert;
wenzelm@16601
  1416
    val (tpairs, Bs, Bi, C) = dest_state (state, i);
wenzelm@32032
  1417
    fun newth n (env, tpairs) =
wenzelm@52487
  1418
      Thm (deriv_rule1
wenzelm@63858
  1419
          ((if Envir.is_empty env then I else Proofterm.norm_proof' env) o
wenzelm@37309
  1420
            Proofterm.assumption_proof Bs Bi n) der,
wenzelm@28321
  1421
       {tags = [],
wenzelm@32032
  1422
        maxidx = Envir.maxidx_of env,
wenzelm@26640
  1423
        shyps = Envir.insert_sorts env shyps,
wenzelm@16601
  1424
        hyps = hyps,
wenzelm@16601
  1425
        tpairs =
wenzelm@16601
  1426
          if Envir.is_empty env then tpairs
wenzelm@59058
  1427
          else map (apply2 (Envir.norm_term env)) tpairs,
wenzelm@16601
  1428
        prop =
wenzelm@16601
  1429
          if Envir.is_empty env then (*avoid wasted normalizations*)
wenzelm@16601
  1430
            Logic.list_implies (Bs, C)
wenzelm@16601
  1431
          else (*normalize the new rule fully*)
wenzelm@24143
  1432
            Envir.norm_term env (Logic.list_implies (Bs, C)),
wenzelm@64981
  1433
        cert = cert'});
wenzelm@30554
  1434
wenzelm@30556
  1435
    val (close, asms, concl) = Logic.assum_problems (~1, Bi);
wenzelm@30556
  1436
    val concl' = close concl;
wenzelm@16601
  1437
    fun addprfs [] _ = Seq.empty
wenzelm@30556
  1438
      | addprfs (asm :: rest) n = Seq.make (fn () => Seq.pull
wenzelm@16601
  1439
          (Seq.mapp (newth n)
wenzelm@30556
  1440
            (if Term.could_unify (asm, concl) then
wenzelm@58950
  1441
              (Unify.unifiers (context, Envir.empty maxidx, (close asm, concl') :: tpairs))
wenzelm@30554
  1442
             else Seq.empty)
wenzelm@30554
  1443
            (addprfs rest (n + 1))))
wenzelm@30556
  1444
  in addprfs asms 1 end;
clasohm@0
  1445
wenzelm@250
  1446
(*Solve subgoal Bi of proof state B1...Bn/C by assumption.
wenzelm@51604
  1447
  Checks if Bi's conclusion is alpha/eta-convertible to one of its assumptions*)
clasohm@0
  1448
fun eq_assumption i state =
wenzelm@16601
  1449
  let
wenzelm@61044
  1450
    val Thm (der, {cert, maxidx, shyps, hyps, ...}) = state;
wenzelm@16601
  1451
    val (tpairs, Bs, Bi, C) = dest_state (state, i);
wenzelm@30556
  1452
    val (_, asms, concl) = Logic.assum_problems (~1, Bi);
wenzelm@16601
  1453
  in
wenzelm@52131
  1454
    (case find_index (fn asm => Envir.aeconv (asm, concl)) asms of
wenzelm@16601
  1455
      ~1 => raise THM ("eq_assumption", 0, [state])
wenzelm@16601
  1456
    | n =>
wenzelm@52487
  1457
        Thm (deriv_rule1 (Proofterm.assumption_proof Bs Bi (n + 1)) der,
wenzelm@61044
  1458
         {cert = cert,
wenzelm@21646
  1459
          tags = [],
wenzelm@16601
  1460
          maxidx = maxidx,
wenzelm@16601
  1461
          shyps = shyps,
wenzelm@16601
  1462
          hyps = hyps,
wenzelm@16601
  1463
          tpairs = tpairs,
wenzelm@28321
  1464
          prop = Logic.list_implies (Bs, C)}))
clasohm@0
  1465
  end;
clasohm@0
  1466
clasohm@0
  1467
paulson@2671
  1468
(*For rotate_tac: fast rotation of assumptions of subgoal i*)
paulson@2671
  1469
fun rotate_rule k i state =
wenzelm@16601
  1470
  let
wenzelm@61044
  1471
    val Thm (der, {cert, maxidx, shyps, hyps, ...}) = state;
wenzelm@16601
  1472
    val (tpairs, Bs, Bi, C) = dest_state (state, i);
wenzelm@46218
  1473
    val params = Term.strip_all_vars Bi;
wenzelm@46218
  1474
    val rest = Term.strip_all_body Bi;
wenzelm@46218
  1475
    val asms = Logic.strip_imp_prems rest
wenzelm@46218
  1476
    val concl = Logic.strip_imp_concl rest;
wenzelm@16601
  1477
    val n = length asms;
wenzelm@16601
  1478
    val m = if k < 0 then n + k else k;
wenzelm@16601
  1479
    val Bi' =
wenzelm@16601
  1480
      if 0 = m orelse m = n then Bi
wenzelm@16601
  1481
      else if 0 < m andalso m < n then
wenzelm@19012
  1482
        let val (ps, qs) = chop m asms
wenzelm@46218
  1483
        in Logic.list_all (params, Logic.list_implies (qs @ ps, concl)) end
wenzelm@16601
  1484
      else raise THM ("rotate_rule", k, [state]);
wenzelm@16601
  1485
  in
wenzelm@52487
  1486
    Thm (deriv_rule1 (Proofterm.rotate_proof Bs Bi m) der,
wenzelm@61044
  1487
     {cert = cert,
wenzelm@21646
  1488
      tags = [],
wenzelm@16601
  1489
      maxidx = maxidx,
wenzelm@16601
  1490
      shyps = shyps,
wenzelm@16601
  1491
      hyps = hyps,
wenzelm@16601
  1492
      tpairs = tpairs,
wenzelm@28321
  1493
      prop = Logic.list_implies (Bs @ [Bi'], C)})
paulson@2671
  1494
  end;
paulson@2671
  1495
paulson@2671
  1496
paulson@7248
  1497
(*Rotates a rule's premises to the left by k, leaving the first j premises
paulson@7248
  1498
  unchanged.  Does nothing if k=0 or if k equals n-j, where n is the
wenzelm@58837
  1499
  number of premises.  Useful with eresolve_tac and underlies defer_tac*)
paulson@7248
  1500
fun permute_prems j k rl =
wenzelm@16601
  1501
  let
wenzelm@61044
  1502
    val Thm (der, {cert, maxidx, shyps, hyps, tpairs, prop, ...}) = rl;
wenzelm@16601
  1503
    val prems = Logic.strip_imp_prems prop
wenzelm@16601
  1504
    and concl = Logic.strip_imp_concl prop;
wenzelm@16601
  1505
    val moved_prems = List.drop (prems, j)
wenzelm@16601
  1506
    and fixed_prems = List.take (prems, j)
wenzelm@43278
  1507
      handle General.Subscript => raise THM ("permute_prems: j", j, [rl]);
wenzelm@16601
  1508
    val n_j = length moved_prems;
wenzelm@16601
  1509
    val m = if k < 0 then n_j + k else k;
wenzelm@16601
  1510
    val prop' =
wenzelm@16601
  1511
      if 0 = m orelse m = n_j then prop
wenzelm@16601
  1512
      else if 0 < m andalso m < n_j then
wenzelm@19012
  1513
        let val (ps, qs) = chop m moved_prems
wenzelm@16601
  1514
        in Logic.list_implies (fixed_prems @ qs @ ps, concl) end
wenzelm@16725
  1515
      else raise THM ("permute_prems: k", k, [rl]);
wenzelm@16601
  1516
  in
wenzelm@52487
  1517
    Thm (deriv_rule1 (Proofterm.permute_prems_proof prems j m) der,
wenzelm@61044
  1518
     {cert = cert,
wenzelm@21646
  1519
      tags = [],
wenzelm@16601
  1520
      maxidx = maxidx,
wenzelm@16601
  1521
      shyps = shyps,
wenzelm@16601
  1522
      hyps = hyps,
wenzelm@16601
  1523
      tpairs = tpairs,
wenzelm@28321
  1524
      prop = prop'})
paulson@7248
  1525
  end;
paulson@7248
  1526
paulson@7248
  1527
berghofe@44108
  1528
(* strip_apply f B A strips off all assumptions/parameters from A
clasohm@0
  1529
   introduced by lifting over B, and applies f to remaining part of A*)
clasohm@0
  1530
fun strip_apply f =
wenzelm@56245
  1531
  let fun strip (Const ("Pure.imp", _) $ _  $ B1)
wenzelm@56245
  1532
                (Const ("Pure.imp", _) $ A2 $ B2) = Logic.mk_implies (A2, strip B1 B2)
wenzelm@56245
  1533
        | strip ((c as Const ("Pure.all", _)) $ Abs (_, _, t1))
wenzelm@56245
  1534
                (      Const ("Pure.all", _)  $ Abs (a, T, t2)) = c $ Abs (a, T, strip t1 t2)
berghofe@44108
  1535
        | strip _ A = f A
clasohm@0
  1536
  in strip end;
clasohm@0
  1537
wenzelm@56245
  1538
fun strip_lifted (Const ("Pure.imp", _) $ _ $ B1)
wenzelm@56245
  1539
                 (Const ("Pure.imp", _) $ _ $ B2) = strip_lifted B1 B2
wenzelm@56245
  1540
  | strip_lifted (Const ("Pure.all", _) $ Abs (_, _, t1))
wenzelm@56245
  1541
                 (Const ("Pure.all", _) $ Abs (_, _, t2)) = strip_lifted t1 t2
berghofe@44108
  1542
  | strip_lifted _ A = A;
berghofe@44108
  1543
clasohm@0
  1544
(*Use the alist to rename all bound variables and some unknowns in a term
clasohm@0
  1545
  dpairs = current disagreement pairs;  tpairs = permanent ones (flexflex);
clasohm@0
  1546
  Preserves unknowns in tpairs and on lhs of dpairs. *)
berghofe@44108
  1547
fun rename_bvs [] _ _ _ _ = K I
berghofe@44108
  1548
  | rename_bvs al dpairs tpairs B As =
wenzelm@20330
  1549
      let
wenzelm@20330
  1550
        val add_var = fold_aterms (fn Var ((x, _), _) => insert (op =) x | _ => I);
wenzelm@20330
  1551
        val vids = []
wenzelm@20330
  1552
          |> fold (add_var o fst) dpairs
wenzelm@20330
  1553
          |> fold (add_var o fst) tpairs
wenzelm@20330
  1554
          |> fold (add_var o snd) tpairs;
berghofe@44108
  1555
        val vids' = fold (add_var o strip_lifted B) As [];
wenzelm@250
  1556
        (*unknowns appearing elsewhere be preserved!*)
wenzelm@59058
  1557
        val al' = distinct ((op =) o apply2 fst)
berghofe@44108
  1558
          (filter_out (fn (x, y) =>
berghofe@44108
  1559
             not (member (op =) vids' x) orelse
berghofe@44108
  1560
             member (op =) vids x orelse member (op =) vids y) al);
berghofe@44108
  1561
        val unchanged = filter_out (AList.defined (op =) al') vids';
berghofe@44108
  1562
        fun del_clashing clash xs _ [] qs =
berghofe@44108
  1563
              if clash then del_clashing false xs xs qs [] else qs
berghofe@44108
  1564
          | del_clashing clash xs ys ((p as (x, y)) :: ps) qs =
berghofe@44108
  1565
              if member (op =) ys y
berghofe@44108
  1566
              then del_clashing true (x :: xs) (x :: ys) ps qs
berghofe@44108
  1567
              else del_clashing clash xs (y :: ys) ps (p :: qs);
wenzelm@46497
  1568
        val al'' = del_clashing false unchanged unchanged al' [];
berghofe@44108
  1569
        fun rename (t as Var ((x, i), T)) =
berghofe@44108
  1570
              (case AList.lookup (op =) al'' x of
berghofe@44108
  1571
                 SOME y => Var ((y, i), T)
berghofe@44108
  1572
               | NONE => t)
berghofe@44108
  1573
          | rename (Abs (x, T, t)) =
wenzelm@18944
  1574
              Abs (the_default x (AList.lookup (op =) al x), T, rename t)
berghofe@44108
  1575
          | rename (f $ t) = rename f $ rename t
berghofe@44108
  1576
          | rename t = t;
berghofe@44108
  1577
        fun strip_ren f Ai = f rename B Ai
wenzelm@20330
  1578
      in strip_ren end;
clasohm@0
  1579
clasohm@0
  1580
(*Function to rename bounds/unknowns in the argument, lifted over B*)
berghofe@44108
  1581
fun rename_bvars dpairs =
wenzelm@48263
  1582
  rename_bvs (fold_rev Term.match_bvars dpairs []) dpairs;
clasohm@0
  1583
clasohm@0
  1584
clasohm@0
  1585
(*** RESOLUTION ***)
clasohm@0
  1586
lcp@721
  1587
(** Lifting optimizations **)
lcp@721
  1588
clasohm@0
  1589
(*strip off pairs of assumptions/parameters in parallel -- they are
clasohm@0
  1590
  identical because of lifting*)
wenzelm@56245
  1591
fun strip_assums2 (Const("Pure.imp", _) $ _ $ B1,
wenzelm@56245
  1592
                   Const("Pure.imp", _) $ _ $ B2) = strip_assums2 (B1,B2)
wenzelm@56245
  1593
  | strip_assums2 (Const("Pure.all",_)$Abs(a,T,t1),
wenzelm@56245
  1594
                   Const("Pure.all",_)$Abs(_,_,t2)) =
clasohm@0
  1595
      let val (B1,B2) = strip_assums2 (t1,t2)
clasohm@0
  1596
      in  (Abs(a,T,B1), Abs(a,T,B2))  end
clasohm@0
  1597
  | strip_assums2 BB = BB;
clasohm@0
  1598
clasohm@0
  1599
lcp@721
  1600
(*Faster normalization: skip assumptions that were lifted over*)
lcp@721
  1601
fun norm_term_skip env 0 t = Envir.norm_term env t
wenzelm@56245
  1602
  | norm_term_skip env n (Const ("Pure.all", _) $ Abs (a, T, t)) =
wenzelm@32032
  1603
      let
wenzelm@58946
  1604
        val T' = Envir.norm_type (Envir.type_env env) T
wenzelm@32032
  1605
        (*Must instantiate types of parameters because they are flattened;
wenzelm@32032
  1606
          this could be a NEW parameter*)
wenzelm@46217
  1607
      in Logic.all_const T' $ Abs (a, T', norm_term_skip env n t) end
wenzelm@56245
  1608
  | norm_term_skip env n (Const ("Pure.imp", _) $ A $ B) =
wenzelm@32032
  1609
      Logic.mk_implies (A, norm_term_skip env (n - 1) B)
wenzelm@32784
  1610
  | norm_term_skip _ _ _ = error "norm_term_skip: too few assumptions??";
lcp@721
  1611
lcp@721
  1612
wenzelm@52222
  1613
(*unify types of schematic variables (non-lifted case)*)
wenzelm@58950
  1614
fun unify_var_types context (th1, th2) env =
wenzelm@52222
  1615
  let
wenzelm@58950
  1616
    fun unify_vars (T :: Us) = fold (fn U => Pattern.unify_types context (T, U)) Us
wenzelm@52222
  1617
      | unify_vars _ = I;
wenzelm@52222
  1618
    val add_vars =
wenzelm@52222
  1619
      full_prop_of #>
wenzelm@52222
  1620
      fold_aterms (fn Var v => Vartab.insert_list (op =) v | _ => I);
wenzelm@52222
  1621
    val vars = Vartab.empty |> add_vars th1 |> add_vars th2;
wenzelm@52222
  1622
  in SOME (Vartab.fold (unify_vars o #2) vars env) end
wenzelm@52222
  1623
  handle Pattern.Unif => NONE;
wenzelm@52222
  1624
clasohm@0
  1625
(*Composition of object rule r=(A1...Am/B) with proof state s=(B1...Bn/C)
wenzelm@250
  1626
  Unifies B with Bi, replacing subgoal i    (1 <= i <= n)
clasohm@0
  1627
  If match then forbid instantiations in proof state
clasohm@0
  1628
  If lifted then shorten the dpair using strip_assums2.
clasohm@0
  1629
  If eres_flg then simultaneously proves A1 by assumption.
wenzelm@250
  1630
  nsubgoal is the number of new subgoals (written m above).
clasohm@0
  1631
  Curried so that resolution calls dest_state only once.
clasohm@0
  1632
*)
wenzelm@4270
  1633
local exception COMPOSE
clasohm@0
  1634
in
wenzelm@58950
  1635
fun bicompose_aux opt_ctxt {flatten, match, incremented} (state, (stpairs, Bs, Bi, C), lifted)
clasohm@0
  1636
                        (eres_flg, orule, nsubgoal) =
wenzelm@28321
  1637
 let val Thm (sder, {maxidx=smax, shyps=sshyps, hyps=shyps, ...}) = state
wenzelm@28321
  1638
     and Thm (rder, {maxidx=rmax, shyps=rshyps, hyps=rhyps,
wenzelm@28321
  1639
             tpairs=rtpairs, prop=rprop,...}) = orule
paulson@1529
  1640
         (*How many hyps to skip over during normalization*)
wenzelm@21576
  1641
     and nlift = Logic.count_prems (strip_all_body Bi) + (if eres_flg then ~1 else 0)
wenzelm@64981
  1642
     val (context, cert) =
wenzelm@64981
  1643
       make_context_certificate [state, orule] opt_ctxt (join_certificate2 (state, orule));
wenzelm@63611
  1644
     (*Add new theorem with prop = '[| Bs; As |] ==> C' to thq*)
wenzelm@32032
  1645
     fun addth A (As, oldAs, rder', n) ((env, tpairs), thq) =
wenzelm@250
  1646
       let val normt = Envir.norm_term env;
wenzelm@250
  1647
           (*perform minimal copying here by examining env*)
berghofe@13658
  1648
           val (ntpairs, normp) =
berghofe@13658
  1649
             if Envir.is_empty env then (tpairs, (Bs @ As, C))
wenzelm@250
  1650
             else
wenzelm@59058
  1651
             let val ntps = map (apply2 normt) tpairs
wenzelm@19861
  1652
             in if Envir.above env smax then
wenzelm@1238
  1653
                  (*no assignments in state; normalize the rule only*)
wenzelm@1238
  1654
                  if lifted
berghofe@13658
  1655
                  then (ntps, (Bs @ map (norm_term_skip env nlift) As, C))
berghofe@13658
  1656
                  else (ntps, (Bs @ map normt As, C))
paulson@1529
  1657
                else if match then raise COMPOSE
wenzelm@250
  1658
                else (*normalize the new rule fully*)
berghofe@13658
  1659
                  (ntps, (map normt (Bs @ As), normt C))
wenzelm@250
  1660
             end
wenzelm@16601
  1661
           val th =
wenzelm@52487
  1662
             Thm (deriv_rule2
berghofe@11518
  1663
                   ((if Envir.is_empty env then I
wenzelm@19861
  1664
                     else if Envir.above env smax then
wenzelm@37309
  1665
                       (fn f => fn der => f (Proofterm.norm_proof' env der))
berghofe@11518
  1666
                     else
wenzelm@37309
  1667
                       curry op oo (Proofterm.norm_proof' env))
wenzelm@37309
  1668
                    (Proofterm.bicompose_proof flatten Bs oldAs As A n (nlift+1))) rder' sder,
wenzelm@28321
  1669
                {tags = [],
wenzelm@32032
  1670
                 maxidx = Envir.maxidx_of env,
wenzelm@26640
  1671
                 shyps = Envir.insert_sorts env (Sorts.union rshyps sshyps),
wenzelm@16601
  1672
                 hyps = union_hyps rhyps shyps,
berghofe@13658
  1673
                 tpairs = ntpairs,
wenzelm@24143
  1674
                 prop = Logic.list_implies normp,
wenzelm@61044
  1675
                 cert = cert})
wenzelm@19475
  1676
        in  Seq.cons th thq  end  handle COMPOSE => thq;
berghofe@13658
  1677
     val (rAs,B) = Logic.strip_prems(nsubgoal, [], rprop)
clasohm@0
  1678
       handle TERM _ => raise THM("bicompose: rule", 0, [orule,state]);
clasohm@0
  1679
     (*Modify assumptions, deleting n-th if n>0 for e-resolution*)
clasohm@0
  1680
     fun newAs(As0, n, dpairs, tpairs) =
berghofe@11518
  1681
       let val (As1, rder') =
berghofe@25939
  1682
         if not lifted then (As0, rder)
berghofe@44108
  1683
         else
berghofe@44108
  1684
           let val rename = rename_bvars dpairs tpairs B As0
berghofe@44108
  1685
           in (map (rename strip_apply) As0,
wenzelm@52487
  1686
             deriv_rule1 (Proofterm.map_proof_terms (rename K) I) rder)
berghofe@44108
  1687
           end;
wenzelm@18486
  1688
       in (map (if flatten then (Logic.flatten_params n) else I) As1, As1, rder', n)
wenzelm@250
  1689
          handle TERM _ =>
wenzelm@250
  1690
          raise THM("bicompose: 1st premise", 0, [orule])
clasohm@0
  1691
       end;
clasohm@0
  1692
     val BBi = if lifted then strip_assums2(B,Bi) else (B,Bi);
clasohm@0
  1693
     val dpairs = BBi :: (rtpairs@stpairs);
wenzelm@30554
  1694
wenzelm@30554
  1695
     (*elim-resolution: try each assumption in turn*)
wenzelm@52222
  1696
     fun eres _ [] = raise THM ("bicompose: no premises", 0, [orule, state])
wenzelm@52222
  1697
       | eres env (A1 :: As) =
wenzelm@30554
  1698
           let
wenzelm@30554
  1699
             val A = SOME A1;
wenzelm@30556
  1700
             val (close, asms, concl) = Logic.assum_problems (nlift + 1, A1);
wenzelm@30556
  1701
             val concl' = close concl;
wenzelm@30554
  1702
             fun tryasms [] _ = Seq.empty
wenzelm@30556
  1703
               | tryasms (asm :: rest) n =
wenzelm@30556
  1704
                   if Term.could_unify (asm, concl) then
wenzelm@30556
  1705
                     let val asm' = close asm in
wenzelm@58950
  1706
                       (case Seq.pull (Unify.unifiers (context, env, (asm', concl') :: dpairs)) of
wenzelm@30554
  1707
                         NONE => tryasms rest (n + 1)
wenzelm@30554
  1708
                       | cell as SOME ((_, tpairs), _) =>
wenzelm@30556
  1709
                           Seq.it_right (addth A (newAs (As, n, [BBi, (concl', asm')], tpairs)))
wenzelm@30554
  1710
                             (Seq.make (fn () => cell),
wenzelm@30554
  1711
                              Seq.make (fn () => Seq.pull (tryasms rest (n + 1)))))
wenzelm@30554
  1712
                     end
wenzelm@30554
  1713
                   else tryasms rest (n + 1);
wenzelm@30556
  1714
           in tryasms asms 1 end;
wenzelm@30554
  1715
clasohm@0
  1716
     (*ordinary resolution*)
wenzelm@52222
  1717
     fun res env =
wenzelm@58950
  1718
       (case Seq.pull (Unify.unifiers (context, env, dpairs)) of
wenzelm@30554
  1719
         NONE => Seq.empty
wenzelm@30554
  1720
       | cell as SOME ((_, tpairs), _) =>
wenzelm@30554
  1721
           Seq.it_right (addth NONE (newAs (rev rAs, 0, [BBi], tpairs)))
wenzelm@30554
  1722
             (Seq.make (fn () => cell), Seq.empty));
wenzelm@52222
  1723
wenzelm@52222
  1724
     val env0 = Envir.empty (Int.max (rmax, smax));
wenzelm@30554
  1725
 in
wenzelm@58950
  1726
   (case if incremented then SOME env0 else unify_var_types context (state, orule) env0 of
wenzelm@52222
  1727
     NONE => Seq.empty
wenzelm@52222
  1728
   | SOME env => if eres_flg then eres env (rev rAs) else res env)
clasohm@0
  1729
 end;
wenzelm@7528
  1730
end;
clasohm@0
  1731
wenzelm@58950
  1732
fun bicompose opt_ctxt flags arg i state =
wenzelm@58950
  1733
  bicompose_aux opt_ctxt flags (state, dest_state (state,i), false) arg;
clasohm@0
  1734
clasohm@0
  1735
(*Quick test whether rule is resolvable with the subgoal with hyps Hs
clasohm@0
  1736
  and conclusion B.  If eres_flg then checks 1st premise of rule also*)
clasohm@0
  1737
fun could_bires (Hs, B, eres_flg, rule) =
wenzelm@29269
  1738
    let fun could_reshyp (A1::_) = exists (fn H => Term.could_unify (A1, H)) Hs
wenzelm@250
  1739
          | could_reshyp [] = false;  (*no premise -- illegal*)
wenzelm@29269
  1740
    in  Term.could_unify(concl_of rule, B) andalso
wenzelm@250
  1741
        (not eres_flg  orelse  could_reshyp (prems_of rule))
clasohm@0
  1742
    end;
clasohm@0
  1743
clasohm@0
  1744
(*Bi-resolution of a state with a list of (flag,rule) pairs.
clasohm@0
  1745
  Puts the rule above:  rule/state.  Renames vars in the rules. *)
wenzelm@58950
  1746
fun biresolution opt_ctxt match brules i state =
wenzelm@18035
  1747
    let val (stpairs, Bs, Bi, C) = dest_state(state,i);
wenzelm@18145
  1748
        val lift = lift_rule (cprem_of state i);
wenzelm@250
  1749
        val B = Logic.strip_assums_concl Bi;
wenzelm@250
  1750
        val Hs = Logic.strip_assums_hyp Bi;
wenzelm@52223
  1751
        val compose =
wenzelm@58950
  1752
          bicompose_aux opt_ctxt {flatten = true, match = match, incremented = true}
wenzelm@52223
  1753
            (state, (stpairs, Bs, Bi, C), true);
wenzelm@4270
  1754
        fun res [] = Seq.empty
wenzelm@250
  1755
          | res ((eres_flg, rule)::brules) =
wenzelm@61044
  1756
              if Config.get_generic (make_context [state] opt_ctxt (cert_of state))
wenzelm@58950
  1757
                  Pattern.unify_trace_failure orelse could_bires (Hs, B, eres_flg, rule)
wenzelm@4270
  1758
              then Seq.make (*delay processing remainder till needed*)
wenzelm@22573
  1759
                  (fn()=> SOME(compose (eres_flg, lift rule, nprems_of rule),
wenzelm@250
  1760
                               res brules))
wenzelm@250
  1761
              else res brules
wenzelm@4270
  1762
    in  Seq.flat (res brules)  end;
clasohm@0
  1763
clasohm@0
  1764
wenzelm@28321
  1765
wenzelm@2509
  1766
(*** Oracles ***)
wenzelm@2509
  1767
wenzelm@28290
  1768
(* oracle rule *)
wenzelm@28290
  1769
wenzelm@52788
  1770
fun invoke_oracle thy1 name oracle arg =
wenzelm@61044
  1771
  let val Cterm {cert = cert2, t = prop, T, maxidx, sorts} = oracle arg in
wenzelm@28290
  1772
    if T <> propT then
wenzelm@28290
  1773
      raise THM ("Oracle's result must have type prop: " ^ name, 0, [])
wenzelm@28290
  1774
    else
wenzelm@52487
  1775
      let val (ora, prf) = Proofterm.oracle_proof name prop in
wenzelm@32059
  1776
        Thm (make_deriv [] [ora] [] prf,
wenzelm@61044
  1777
         {cert = Context.join_certificate (Context.Certificate thy1, cert2),
wenzelm@28804
  1778
          tags = [],
wenzelm@28804
  1779
          maxidx = maxidx,
wenzelm@28804
  1780
          shyps = sorts,
wenzelm@28804
  1781
          hyps = [],
wenzelm@28804
  1782
          tpairs = [],
wenzelm@28804
  1783
          prop = prop})
wenzelm@28804
  1784
      end
wenzelm@3812
  1785
  end;
wenzelm@3812
  1786
wenzelm@32590
  1787
end;
wenzelm@32590
  1788
end;
wenzelm@32590
  1789
end;
wenzelm@32590
  1790
wenzelm@28290
  1791
wenzelm@28290
  1792
(* authentic derivation names *)
wenzelm@28290
  1793
wenzelm@33522
  1794
structure Oracles = Theory_Data
wenzelm@28290
  1795
(
wenzelm@33095
  1796
  type T = unit Name_Space.table;
wenzelm@33159
  1797
  val empty : T = Name_Space.empty_table "oracle";
wenzelm@28290
  1798
  val extend = I;
wenzelm@33522
  1799
  fun merge data : T = Name_Space.merge_tables data;
wenzelm@28290
  1800
);
wenzelm@28290
  1801
wenzelm@59917
  1802
fun extern_oracles verbose ctxt =
wenzelm@59917
  1803
  map #1 (Name_Space.markup_table verbose ctxt (Oracles.get (Proof_Context.theory_of ctxt)));
wenzelm@28290
  1804
wenzelm@30288
  1805
fun add_oracle (b, oracle) thy =
wenzelm@28290
  1806
  let
wenzelm@47005
  1807
    val (name, tab') = Name_Space.define (Context.Theory thy) true (b, ()) (Oracles.get thy);
wenzelm@30288
  1808
    val thy' = Oracles.put tab' thy;
wenzelm@52788
  1809
  in ((name, invoke_oracle thy' name oracle), thy') end;
wenzelm@28290
  1810
clasohm@0
  1811
end;
paulson@1503
  1812
wenzelm@32104
  1813
structure Basic_Thm: BASIC_THM = Thm;
wenzelm@32104
  1814
open Basic_Thm;