src/Pure/proofterm.ML
author wenzelm
Tue Sep 26 20:54:40 2017 +0200 (22 months ago)
changeset 66695 91500c024c7f
parent 66168 fcd09fc36d7f
child 67669 ad8ca85f13e2
permissions -rw-r--r--
tuned;
berghofe@11519
     1
(*  Title:      Pure/proofterm.ML
wenzelm@11540
     2
    Author:     Stefan Berghofer, TU Muenchen
berghofe@11519
     3
wenzelm@11540
     4
LF style proof terms.
berghofe@11519
     5
*)
berghofe@11519
     6
berghofe@11615
     7
infix 8 % %% %>;
berghofe@11519
     8
berghofe@11519
     9
signature BASIC_PROOFTERM =
berghofe@11519
    10
sig
wenzelm@64573
    11
  type thm_node
berghofe@11519
    12
  datatype proof =
wenzelm@28803
    13
     MinProof
wenzelm@28803
    14
   | PBound of int
berghofe@11519
    15
   | Abst of string * typ option * proof
berghofe@11519
    16
   | AbsP of string * term option * proof
wenzelm@63857
    17
   | % of proof * term option
wenzelm@63857
    18
   | %% of proof * proof
berghofe@11519
    19
   | Hyp of term
berghofe@11519
    20
   | PAxm of string * term * typ list option
wenzelm@31943
    21
   | OfClass of typ * class
berghofe@11519
    22
   | Oracle of string * term * typ list option
wenzelm@28828
    23
   | Promise of serial * term * typ list
wenzelm@29635
    24
   | PThm of serial * ((string * term * typ list option) * proof_body future)
wenzelm@28803
    25
  and proof_body = PBody of
wenzelm@39687
    26
    {oracles: (string * term) Ord_List.T,
wenzelm@64573
    27
     thms: (serial * thm_node) Ord_List.T,
wenzelm@28803
    28
     proof: proof}
berghofe@11615
    29
  val %> : proof * term -> proof
berghofe@11519
    30
end;
berghofe@11519
    31
berghofe@11519
    32
signature PROOFTERM =
berghofe@11519
    33
sig
berghofe@11519
    34
  include BASIC_PROOFTERM
wenzelm@65446
    35
  val proofs: int Unsynchronized.ref
wenzelm@64573
    36
  type pthm = serial * thm_node
wenzelm@28815
    37
  type oracle = string * term
wenzelm@32094
    38
  val proof_of: proof_body -> proof
wenzelm@64573
    39
  val thm_node_name: thm_node -> string
wenzelm@64573
    40
  val thm_node_prop: thm_node -> term
wenzelm@64573
    41
  val thm_node_body: thm_node -> proof_body future
wenzelm@29635
    42
  val join_proof: proof_body future -> proof
wenzelm@30711
    43
  val fold_proof_atoms: bool -> (proof -> 'a -> 'a) -> proof list -> 'a -> 'a
wenzelm@64572
    44
  val fold_body_thms: ({serial: serial, name: string, prop: term, body: proof_body} -> 'a -> 'a) ->
wenzelm@64572
    45
    proof_body list -> 'a -> 'a
wenzelm@66168
    46
  val consolidate: proof_body list -> unit
wenzelm@50126
    47
  val peek_status: proof_body list -> {failed: bool, oracle: bool, unfinished: bool}
wenzelm@28803
    48
wenzelm@28803
    49
  val oracle_ord: oracle * oracle -> order
wenzelm@28803
    50
  val thm_ord: pthm * pthm -> order
wenzelm@44334
    51
  val unions_oracles: oracle Ord_List.T list -> oracle Ord_List.T
wenzelm@44334
    52
  val unions_thms: pthm Ord_List.T list -> pthm Ord_List.T
wenzelm@39687
    53
  val all_oracles_of: proof_body -> oracle Ord_List.T
wenzelm@30716
    54
  val approximate_proof_body: proof -> proof_body
wenzelm@52424
    55
  val no_proof_body: proof_body
wenzelm@52424
    56
  val no_thm_proofs: proof -> proof
wenzelm@52424
    57
wenzelm@52424
    58
  val encode: proof XML.Encode.T
wenzelm@52424
    59
  val encode_body: proof_body XML.Encode.T
wenzelm@52424
    60
  val decode: proof XML.Decode.T
wenzelm@52424
    61
  val decode_body: proof_body XML.Decode.T
wenzelm@28803
    62
berghofe@11519
    63
  (** primitive operations **)
wenzelm@52487
    64
  val proofs_enabled: unit -> bool
wenzelm@64568
    65
  val atomic_proof: proof -> bool
wenzelm@64568
    66
  val compact_proof: proof -> bool
wenzelm@28803
    67
  val proof_combt: proof * term list -> proof
wenzelm@28803
    68
  val proof_combt': proof * term option list -> proof
wenzelm@28803
    69
  val proof_combP: proof * proof list -> proof
wenzelm@28803
    70
  val strip_combt: proof -> proof * term option list
wenzelm@28803
    71
  val strip_combP: proof -> proof * proof list
wenzelm@28803
    72
  val strip_thm: proof_body -> proof_body
berghofe@37231
    73
  val map_proof_same: term Same.operation -> typ Same.operation
berghofe@37231
    74
    -> (typ * class -> proof) -> proof Same.operation
wenzelm@36620
    75
  val map_proof_terms_same: term Same.operation -> typ Same.operation -> proof Same.operation
wenzelm@36620
    76
  val map_proof_types_same: typ Same.operation -> proof Same.operation
wenzelm@28803
    77
  val map_proof_terms: (term -> term) -> (typ -> typ) -> proof -> proof
wenzelm@36620
    78
  val map_proof_types: (typ -> typ) -> proof -> proof
wenzelm@28803
    79
  val fold_proof_terms: (term -> 'a -> 'a) -> (typ -> 'a -> 'a) -> proof -> 'a -> 'a
wenzelm@28803
    80
  val maxidx_proof: proof -> int -> int
wenzelm@28803
    81
  val size_of_proof: proof -> int
wenzelm@28803
    82
  val change_type: typ list option -> proof -> proof
wenzelm@28803
    83
  val prf_abstract_over: term -> proof -> proof
wenzelm@28803
    84
  val prf_incr_bv: int -> int -> int -> int -> proof -> proof
wenzelm@28803
    85
  val incr_pboundvars: int -> int -> proof -> proof
wenzelm@28803
    86
  val prf_loose_bvar1: proof -> int -> bool
wenzelm@28803
    87
  val prf_loose_Pbvar1: proof -> int -> bool
wenzelm@28803
    88
  val prf_add_loose_bnos: int -> int -> proof -> int list * int list -> int list * int list
wenzelm@28803
    89
  val norm_proof: Envir.env -> proof -> proof
wenzelm@28803
    90
  val norm_proof': Envir.env -> proof -> proof
wenzelm@28803
    91
  val prf_subst_bounds: term list -> proof -> proof
wenzelm@28803
    92
  val prf_subst_pbounds: proof list -> proof -> proof
wenzelm@28803
    93
  val freeze_thaw_prf: proof -> proof * (proof -> proof)
berghofe@11519
    94
berghofe@11519
    95
  (** proof terms for specific inference rules **)
wenzelm@28803
    96
  val implies_intr_proof: term -> proof -> proof
berghofe@37231
    97
  val implies_intr_proof': term -> proof -> proof
wenzelm@28803
    98
  val forall_intr_proof: term -> string -> proof -> proof
berghofe@37231
    99
  val forall_intr_proof': term -> proof -> proof
wenzelm@28803
   100
  val varify_proof: term -> (string * sort) list -> proof -> proof
wenzelm@36619
   101
  val legacy_freezeT: term -> proof -> proof
wenzelm@28803
   102
  val rotate_proof: term list -> term -> int -> proof -> proof
wenzelm@36742
   103
  val permute_prems_proof: term list -> int -> int -> proof -> proof
wenzelm@19908
   104
  val generalize: string list * string list -> int -> proof -> proof
wenzelm@28803
   105
  val instantiate: ((indexname * sort) * typ) list * ((indexname * typ) * term) list
wenzelm@16880
   106
    -> proof -> proof
wenzelm@28803
   107
  val lift_proof: term -> int -> term -> proof -> proof
wenzelm@32027
   108
  val incr_indexes: int -> proof -> proof
wenzelm@28803
   109
  val assumption_proof: term list -> term -> int -> proof -> proof
wenzelm@28803
   110
  val bicompose_proof: bool -> term list -> term list -> term list -> term option ->
berghofe@23296
   111
    int -> int -> proof -> proof -> proof
wenzelm@28803
   112
  val equality_axms: (string * term) list
wenzelm@28803
   113
  val reflexive_axm: proof
wenzelm@28803
   114
  val symmetric_axm: proof
wenzelm@28803
   115
  val transitive_axm: proof
wenzelm@28803
   116
  val equal_intr_axm: proof
wenzelm@28803
   117
  val equal_elim_axm: proof
wenzelm@28803
   118
  val abstract_rule_axm: proof
wenzelm@28803
   119
  val combination_axm: proof
wenzelm@28803
   120
  val reflexive: proof
wenzelm@28803
   121
  val symmetric: proof -> proof
wenzelm@28803
   122
  val transitive: term -> typ -> proof -> proof -> proof
wenzelm@28803
   123
  val abstract_rule: term -> string -> proof -> proof
wenzelm@28803
   124
  val combination: term -> term -> term -> term -> typ -> proof -> proof -> proof
wenzelm@28803
   125
  val equal_intr: term -> term -> proof -> proof -> proof
wenzelm@28803
   126
  val equal_elim: term -> term -> proof -> proof -> proof
wenzelm@36621
   127
  val strip_shyps_proof: Sorts.algebra -> (typ * sort) list -> (typ * sort) list ->
wenzelm@36621
   128
    sort list -> proof -> proof
wenzelm@36740
   129
  val classrel_proof: theory -> class * class -> proof
wenzelm@36740
   130
  val arity_proof: theory -> string * sort list * class -> proof
wenzelm@36741
   131
  val of_sort_proof: theory -> (typ * class -> proof) -> typ * sort -> proof list
wenzelm@36740
   132
  val install_axclass_proofs:
wenzelm@36740
   133
   {classrel_proof: theory -> class * class -> proof,
wenzelm@62897
   134
    arity_proof: theory -> string * sort list * class -> proof} -> theory -> theory
wenzelm@28803
   135
  val axm_proof: string -> term -> proof
wenzelm@52487
   136
  val oracle_proof: string -> term -> oracle * proof
wenzelm@63623
   137
  val shrink_proof: proof -> proof
berghofe@11519
   138
berghofe@11519
   139
  (** rewriting on proof terms **)
wenzelm@28803
   140
  val add_prf_rrule: proof * proof -> theory -> theory
wenzelm@52487
   141
  val add_prf_rproc: (typ list -> term option list -> proof -> (proof * proof) option) -> theory -> theory
wenzelm@33722
   142
  val no_skel: proof
wenzelm@33722
   143
  val normal_skel: proof
wenzelm@28803
   144
  val rewrite_proof: theory -> (proof * proof) list *
berghofe@37231
   145
    (typ list -> term option list -> proof -> (proof * proof) option) list -> proof -> proof
wenzelm@28803
   146
  val rewrite_proof_notypes: (proof * proof) list *
berghofe@37231
   147
    (typ list -> term option list -> proof -> (proof * proof) option) list -> proof -> proof
wenzelm@28803
   148
  val rew_proof: theory -> proof -> proof
wenzelm@36877
   149
wenzelm@36877
   150
  val promise_proof: theory -> serial -> term -> proof
wenzelm@36877
   151
  val fulfill_norm_proof: theory -> (serial * proof_body) list -> proof_body -> proof_body
wenzelm@36882
   152
  val thm_proof: theory -> string -> sort list -> term list -> term ->
wenzelm@36877
   153
    (serial * proof_body future) list -> proof_body -> pthm * proof
wenzelm@36883
   154
  val unconstrain_thm_proof: theory -> sort list -> term ->
wenzelm@36883
   155
    (serial * proof_body future) list -> proof_body -> pthm * proof
wenzelm@37297
   156
  val get_name: sort list -> term list -> term -> proof -> string
wenzelm@36877
   157
  val guess_name: proof -> string
berghofe@11519
   158
end
berghofe@11519
   159
berghofe@11519
   160
structure Proofterm : PROOFTERM =
berghofe@11519
   161
struct
berghofe@11519
   162
wenzelm@28803
   163
(***** datatype proof *****)
wenzelm@28803
   164
berghofe@11519
   165
datatype proof =
wenzelm@28803
   166
   MinProof
wenzelm@28803
   167
 | PBound of int
berghofe@11519
   168
 | Abst of string * typ option * proof
berghofe@11519
   169
 | AbsP of string * term option * proof
wenzelm@12497
   170
 | op % of proof * term option
wenzelm@12497
   171
 | op %% of proof * proof
berghofe@11519
   172
 | Hyp of term
berghofe@11519
   173
 | PAxm of string * term * typ list option
wenzelm@31943
   174
 | OfClass of typ * class
berghofe@11519
   175
 | Oracle of string * term * typ list option
wenzelm@28828
   176
 | Promise of serial * term * typ list
wenzelm@29635
   177
 | PThm of serial * ((string * term * typ list option) * proof_body future)
wenzelm@28803
   178
and proof_body = PBody of
wenzelm@39687
   179
  {oracles: (string * term) Ord_List.T,
wenzelm@64573
   180
   thms: (serial * thm_node) Ord_List.T,
wenzelm@64573
   181
   proof: proof}
wenzelm@64574
   182
and thm_node =
wenzelm@64574
   183
  Thm_Node of {name: string, prop: term, body: proof_body future, consolidate: unit lazy};
berghofe@11519
   184
wenzelm@28815
   185
type oracle = string * term;
wenzelm@64573
   186
type pthm = serial * thm_node;
wenzelm@28815
   187
wenzelm@28803
   188
fun proof_of (PBody {proof, ...}) = proof;
wenzelm@32094
   189
val join_proof = Future.join #> proof_of;
berghofe@17017
   190
wenzelm@64574
   191
fun rep_thm_node (Thm_Node args) = args;
wenzelm@64574
   192
val thm_node_name = #name o rep_thm_node;
wenzelm@64574
   193
val thm_node_prop = #prop o rep_thm_node;
wenzelm@64574
   194
val thm_node_body = #body o rep_thm_node;
wenzelm@64574
   195
val thm_node_consolidate = #consolidate o rep_thm_node;
wenzelm@64573
   196
wenzelm@64573
   197
fun join_thms (thms: pthm list) =
wenzelm@64574
   198
  Future.joins (map (thm_node_body o #2) thms);
wenzelm@64574
   199
wenzelm@66168
   200
val consolidate =
wenzelm@66168
   201
  maps (fn PBody {thms, ...} => map (thm_node_consolidate o #2) thms)
wenzelm@66168
   202
  #> Lazy.force_list #> ignore;
wenzelm@64574
   203
wenzelm@64574
   204
fun make_thm_node name prop body =
wenzelm@64574
   205
  Thm_Node {name = name, prop = prop, body = body,
wenzelm@64574
   206
    consolidate =
wenzelm@66167
   207
      Lazy.lazy_name "Proofterm.make_thm_node" (fn () =>
wenzelm@64574
   208
        let val PBody {thms, ...} = Future.join body
wenzelm@66168
   209
        in consolidate (join_thms thms) end)};
wenzelm@44333
   210
berghofe@17017
   211
wenzelm@28803
   212
(***** proof atoms *****)
wenzelm@28803
   213
wenzelm@28803
   214
fun fold_proof_atoms all f =
wenzelm@28803
   215
  let
wenzelm@28803
   216
    fun app (Abst (_, _, prf)) = app prf
wenzelm@28803
   217
      | app (AbsP (_, _, prf)) = app prf
wenzelm@28803
   218
      | app (prf % _) = app prf
wenzelm@28803
   219
      | app (prf1 %% prf2) = app prf1 #> app prf2
wenzelm@28803
   220
      | app (prf as PThm (i, (_, body))) = (fn (x, seen) =>
wenzelm@28803
   221
          if Inttab.defined seen i then (x, seen)
wenzelm@28803
   222
          else
wenzelm@28815
   223
            let val (x', seen') =
wenzelm@29635
   224
              (if all then app (join_proof body) else I) (x, Inttab.update (i, ()) seen)
wenzelm@28815
   225
            in (f prf x', seen') end)
wenzelm@28803
   226
      | app prf = (fn (x, seen) => (f prf x, seen));
wenzelm@28803
   227
  in fn prfs => fn x => #1 (fold app prfs (x, Inttab.empty)) end;
berghofe@19357
   228
wenzelm@30711
   229
fun fold_body_thms f =
wenzelm@30711
   230
  let
wenzelm@32726
   231
    fun app (PBody {thms, ...}) =
wenzelm@64574
   232
      tap join_thms thms |> fold (fn (i, thm_node) => fn (x, seen) =>
wenzelm@32726
   233
        if Inttab.defined seen i then (x, seen)
wenzelm@32094
   234
        else
wenzelm@32094
   235
          let
wenzelm@64574
   236
            val name = thm_node_name thm_node;
wenzelm@64574
   237
            val prop = thm_node_prop thm_node;
wenzelm@64574
   238
            val body = Future.join (thm_node_body thm_node);
wenzelm@64574
   239
            val (x', seen') = app body (x, Inttab.update (i, ()) seen);
wenzelm@64574
   240
          in (f {serial = i, name = name, prop = prop, body = body} x', seen') end);
wenzelm@32726
   241
  in fn bodies => fn x => #1 (fold app bodies (x, Inttab.empty)) end;
wenzelm@30711
   242
wenzelm@50126
   243
fun peek_status bodies =
wenzelm@30711
   244
  let
wenzelm@30711
   245
    fun status (PBody {oracles, thms, ...}) x =
wenzelm@30711
   246
      let
wenzelm@30711
   247
        val ((oracle, unfinished, failed), seen) =
wenzelm@64574
   248
          (thms, x) |-> fold (fn (i, thm_node) => fn (st, seen) =>
wenzelm@30711
   249
            if Inttab.defined seen i then (st, seen)
wenzelm@30711
   250
            else
wenzelm@30711
   251
              let val seen' = Inttab.update (i, ()) seen in
wenzelm@64574
   252
                (case Future.peek (thm_node_body thm_node) of
wenzelm@43761
   253
                  SOME (Exn.Res body') => status body' (st, seen')
wenzelm@30711
   254
                | SOME (Exn.Exn _) =>
wenzelm@30711
   255
                    let val (oracle, unfinished, _) = st
wenzelm@30711
   256
                    in ((oracle, unfinished, true), seen') end
wenzelm@30711
   257
                | NONE =>
wenzelm@30711
   258
                    let val (oracle, _, failed) = st
wenzelm@30711
   259
                    in ((oracle, true, failed), seen') end)
wenzelm@30711
   260
              end);
wenzelm@30711
   261
      in ((oracle orelse not (null oracles), unfinished, failed), seen) end;
wenzelm@32057
   262
    val (oracle, unfinished, failed) =
wenzelm@32057
   263
      #1 (fold status bodies ((false, false, false), Inttab.empty));
wenzelm@30711
   264
  in {oracle = oracle, unfinished = unfinished, failed = failed} end;
wenzelm@30711
   265
wenzelm@28803
   266
wenzelm@28815
   267
(* proof body *)
wenzelm@28803
   268
wenzelm@35408
   269
val oracle_ord = prod_ord fast_string_ord Term_Ord.fast_term_ord;
wenzelm@64573
   270
fun thm_ord ((i, _): pthm, (j, _): pthm) = int_ord (j, i);
wenzelm@28803
   271
wenzelm@44334
   272
val unions_oracles = Ord_List.unions oracle_ord;
wenzelm@44334
   273
val unions_thms = Ord_List.unions thm_ord;
wenzelm@30716
   274
wenzelm@30716
   275
val all_oracles_of =
wenzelm@30716
   276
  let
wenzelm@32057
   277
    fun collect (PBody {oracles, thms, ...}) =
wenzelm@64574
   278
      tap join_thms thms |> fold (fn (i, thm_node) => fn (x, seen) =>
wenzelm@32057
   279
        if Inttab.defined seen i then (x, seen)
wenzelm@32057
   280
        else
wenzelm@32057
   281
          let
wenzelm@64574
   282
            val body = Future.join (thm_node_body thm_node);
wenzelm@64574
   283
            val (x', seen') = collect body (x, Inttab.update (i, ()) seen);
wenzelm@44334
   284
          in (if null oracles then x' else oracles :: x', seen') end);
wenzelm@44334
   285
  in fn body => unions_oracles (#1 (collect body ([], Inttab.empty))) end;
wenzelm@30716
   286
wenzelm@30716
   287
fun approximate_proof_body prf =
wenzelm@28803
   288
  let
wenzelm@28803
   289
    val (oracles, thms) = fold_proof_atoms false
wenzelm@28803
   290
      (fn Oracle (s, prop, _) => apfst (cons (s, prop))
wenzelm@64574
   291
        | PThm (i, ((name, prop, _), body)) => apsnd (cons (i, make_thm_node name prop body))
wenzelm@28803
   292
        | _ => I) [prf] ([], []);
wenzelm@30716
   293
  in
wenzelm@30716
   294
    PBody
wenzelm@39687
   295
     {oracles = Ord_List.make oracle_ord oracles,
wenzelm@39687
   296
      thms = Ord_List.make thm_ord thms,
wenzelm@30716
   297
      proof = prf}
wenzelm@30716
   298
  end;
berghofe@11519
   299
wenzelm@52424
   300
val no_proof_body = PBody {oracles = [], thms = [], proof = MinProof};
wenzelm@52424
   301
val no_body = Future.value no_proof_body;
wenzelm@52424
   302
wenzelm@52424
   303
fun no_thm_proofs (PThm (i, (a, _))) = PThm (i, (a, no_body))
wenzelm@52424
   304
  | no_thm_proofs (Abst (x, T, prf)) = Abst (x, T, no_thm_proofs prf)
wenzelm@52424
   305
  | no_thm_proofs (AbsP (x, t, prf)) = AbsP (x, t, no_thm_proofs prf)
wenzelm@52424
   306
  | no_thm_proofs (prf % t) = no_thm_proofs prf % t
wenzelm@52424
   307
  | no_thm_proofs (prf1 %% prf2) = no_thm_proofs prf1 %% no_thm_proofs prf2
wenzelm@52424
   308
  | no_thm_proofs a = a;
wenzelm@52424
   309
wenzelm@52424
   310
wenzelm@52424
   311
(***** XML data representation *****)
wenzelm@52424
   312
wenzelm@52424
   313
(* encode *)
wenzelm@52424
   314
wenzelm@52424
   315
local
wenzelm@52424
   316
wenzelm@52424
   317
open XML.Encode Term_XML.Encode;
wenzelm@52424
   318
wenzelm@52424
   319
fun proof prf = prf |> variant
wenzelm@52424
   320
 [fn MinProof => ([], []),
wenzelm@52424
   321
  fn PBound a => ([int_atom a], []),
wenzelm@52424
   322
  fn Abst (a, b, c) => ([a], pair (option typ) proof (b, c)),
wenzelm@52424
   323
  fn AbsP (a, b, c) => ([a], pair (option term) proof (b, c)),
wenzelm@52424
   324
  fn a % b => ([], pair proof (option term) (a, b)),
wenzelm@52424
   325
  fn a %% b => ([], pair proof proof (a, b)),
wenzelm@52424
   326
  fn Hyp a => ([], term a),
wenzelm@52424
   327
  fn PAxm (a, b, c) => ([a], pair term (option (list typ)) (b, c)),
wenzelm@52424
   328
  fn OfClass (a, b) => ([b], typ a),
wenzelm@52424
   329
  fn Oracle (a, b, c) => ([a], pair term (option (list typ)) (b, c)),
wenzelm@52424
   330
  fn Promise (a, b, c) => ([int_atom a], pair term (list typ) (b, c)),
wenzelm@52424
   331
  fn PThm (a, ((b, c, d), body)) =>
wenzelm@52424
   332
    ([int_atom a, b], triple term (option (list typ)) proof_body (c, d, Future.join body))]
wenzelm@52424
   333
and proof_body (PBody {oracles, thms, proof = prf}) =
wenzelm@52424
   334
  triple (list (pair string term)) (list pthm) proof (oracles, thms, prf)
wenzelm@64574
   335
and pthm (a, thm_node) =
wenzelm@64574
   336
  pair int (triple string term proof_body)
wenzelm@64574
   337
    (a, (thm_node_name thm_node, thm_node_prop thm_node, Future.join (thm_node_body thm_node)));
wenzelm@52424
   338
wenzelm@52424
   339
in
wenzelm@52424
   340
wenzelm@52424
   341
val encode = proof;
wenzelm@52424
   342
val encode_body = proof_body;
wenzelm@52424
   343
wenzelm@52424
   344
end;
wenzelm@52424
   345
wenzelm@52424
   346
wenzelm@52424
   347
(* decode *)
wenzelm@52424
   348
wenzelm@52424
   349
local
wenzelm@52424
   350
wenzelm@52424
   351
open XML.Decode Term_XML.Decode;
wenzelm@52424
   352
wenzelm@52424
   353
fun proof prf = prf |> variant
wenzelm@52424
   354
 [fn ([], []) => MinProof,
wenzelm@52424
   355
  fn ([a], []) => PBound (int_atom a),
wenzelm@52424
   356
  fn ([a], b) => let val (c, d) = pair (option typ) proof b in Abst (a, c, d) end,
wenzelm@52424
   357
  fn ([a], b) => let val (c, d) = pair (option term) proof b in AbsP (a, c, d) end,
wenzelm@52424
   358
  fn ([], a) => op % (pair proof (option term) a),
wenzelm@52424
   359
  fn ([], a) => op %% (pair proof proof a),
wenzelm@52424
   360
  fn ([], a) => Hyp (term a),
wenzelm@52424
   361
  fn ([a], b) => let val (c, d) = pair term (option (list typ)) b in PAxm (a, c, d) end,
wenzelm@52424
   362
  fn ([b], a) => OfClass (typ a, b),
wenzelm@52424
   363
  fn ([a], b) => let val (c, d) = pair term (option (list typ)) b in Oracle (a, c, d) end,
wenzelm@52424
   364
  fn ([a], b) => let val (c, d) = pair term (list typ) b in Promise (int_atom a, c, d) end,
wenzelm@52424
   365
  fn ([a, b], c) =>
wenzelm@52424
   366
    let val (d, e, f) = triple term (option (list typ)) proof_body c
wenzelm@52424
   367
    in PThm (int_atom a, ((b, d, e), Future.value f)) end]
wenzelm@52424
   368
and proof_body x =
wenzelm@52424
   369
  let val (a, b, c) = triple (list (pair string term)) (list pthm) proof x
wenzelm@52424
   370
  in PBody {oracles = a, thms = b, proof = c} end
wenzelm@52424
   371
and pthm x =
wenzelm@52424
   372
  let val (a, (b, c, d)) = pair int (triple string term proof_body) x
wenzelm@64574
   373
  in (a, make_thm_node b c (Future.value d)) end;
wenzelm@52424
   374
wenzelm@52424
   375
in
wenzelm@52424
   376
wenzelm@52424
   377
val decode = proof;
wenzelm@52424
   378
val decode_body = proof_body;
wenzelm@52424
   379
wenzelm@52424
   380
end;
wenzelm@52424
   381
wenzelm@28803
   382
wenzelm@28803
   383
(***** proof objects with different levels of detail *****)
berghofe@11519
   384
wenzelm@64568
   385
fun atomic_proof prf =
wenzelm@64568
   386
  (case prf of
wenzelm@64568
   387
    Abst _ => false
wenzelm@64568
   388
  | AbsP _ => false
wenzelm@64568
   389
  | op % _ => false
wenzelm@64568
   390
  | op %% _ => false
wenzelm@64568
   391
  | Promise _ => false
wenzelm@64568
   392
  | _ => true);
wenzelm@64568
   393
wenzelm@64568
   394
fun compact_proof (prf % _) = compact_proof prf
wenzelm@64568
   395
  | compact_proof (prf1 %% prf2) = atomic_proof prf2 andalso compact_proof prf1
wenzelm@64568
   396
  | compact_proof prf = atomic_proof prf;
wenzelm@64568
   397
skalberg@15531
   398
fun (prf %> t) = prf % SOME t;
berghofe@11519
   399
skalberg@15570
   400
val proof_combt = Library.foldl (op %>);
skalberg@15570
   401
val proof_combt' = Library.foldl (op %);
skalberg@15570
   402
val proof_combP = Library.foldl (op %%);
berghofe@11519
   403
wenzelm@21646
   404
fun strip_combt prf =
berghofe@11615
   405
    let fun stripc (prf % t, ts) = stripc (prf, t::ts)
wenzelm@21646
   406
          | stripc  x =  x
berghofe@11519
   407
    in  stripc (prf, [])  end;
berghofe@11519
   408
wenzelm@21646
   409
fun strip_combP prf =
berghofe@11615
   410
    let fun stripc (prf %% prf', prfs) = stripc (prf, prf'::prfs)
berghofe@11519
   411
          | stripc  x =  x
berghofe@11519
   412
    in  stripc (prf, [])  end;
berghofe@11519
   413
wenzelm@28803
   414
fun strip_thm (body as PBody {proof, ...}) =
wenzelm@28803
   415
  (case strip_combt (fst (strip_combP proof)) of
wenzelm@29635
   416
    (PThm (_, (_, body')), _) => Future.join body'
wenzelm@28803
   417
  | _ => body);
berghofe@11519
   418
wenzelm@63857
   419
val mk_Abst = fold_rev (fn (s, _: typ) => fn prf => Abst (s, NONE, prf));
skalberg@15531
   420
fun mk_AbsP (i, prf) = funpow i (fn prf => AbsP ("H", NONE, prf)) prf;
berghofe@11519
   421
wenzelm@36620
   422
fun map_proof_same term typ ofclass =
wenzelm@20000
   423
  let
wenzelm@32024
   424
    val typs = Same.map typ;
wenzelm@20000
   425
wenzelm@32024
   426
    fun proof (Abst (s, T, prf)) =
wenzelm@32024
   427
          (Abst (s, Same.map_option typ T, Same.commit proof prf)
wenzelm@32024
   428
            handle Same.SAME => Abst (s, T, proof prf))
wenzelm@32024
   429
      | proof (AbsP (s, t, prf)) =
wenzelm@32024
   430
          (AbsP (s, Same.map_option term t, Same.commit proof prf)
wenzelm@32024
   431
            handle Same.SAME => AbsP (s, t, proof prf))
wenzelm@32024
   432
      | proof (prf % t) =
wenzelm@32024
   433
          (proof prf % Same.commit (Same.map_option term) t
wenzelm@32024
   434
            handle Same.SAME => prf % Same.map_option term t)
wenzelm@32024
   435
      | proof (prf1 %% prf2) =
wenzelm@32024
   436
          (proof prf1 %% Same.commit proof prf2
wenzelm@32024
   437
            handle Same.SAME => prf1 %% proof prf2)
wenzelm@32024
   438
      | proof (PAxm (a, prop, SOME Ts)) = PAxm (a, prop, SOME (typs Ts))
wenzelm@36620
   439
      | proof (OfClass T_c) = ofclass T_c
wenzelm@32024
   440
      | proof (Oracle (a, prop, SOME Ts)) = Oracle (a, prop, SOME (typs Ts))
wenzelm@32024
   441
      | proof (Promise (i, prop, Ts)) = Promise (i, prop, typs Ts)
wenzelm@32057
   442
      | proof (PThm (i, ((a, prop, SOME Ts), body))) =
wenzelm@32057
   443
          PThm (i, ((a, prop, SOME (typs Ts)), body))
wenzelm@32024
   444
      | proof _ = raise Same.SAME;
wenzelm@36620
   445
  in proof end;
wenzelm@36620
   446
wenzelm@36620
   447
fun map_proof_terms_same term typ = map_proof_same term typ (fn (T, c) => OfClass (typ T, c));
wenzelm@36620
   448
fun map_proof_types_same typ = map_proof_terms_same (Term_Subst.map_types_same typ) typ;
wenzelm@20000
   449
haftmann@22662
   450
fun same eq f x =
berghofe@11715
   451
  let val x' = f x
wenzelm@32019
   452
  in if eq (x, x') then raise Same.SAME else x' end;
berghofe@11715
   453
wenzelm@36620
   454
fun map_proof_terms f g = Same.commit (map_proof_terms_same (same (op =) f) (same (op =) g));
wenzelm@36620
   455
fun map_proof_types f = Same.commit (map_proof_types_same (same (op =) f));
berghofe@11519
   456
wenzelm@20147
   457
fun fold_proof_terms f g (Abst (_, SOME T, prf)) = g T #> fold_proof_terms f g prf
wenzelm@20147
   458
  | fold_proof_terms f g (Abst (_, NONE, prf)) = fold_proof_terms f g prf
wenzelm@20147
   459
  | fold_proof_terms f g (AbsP (_, SOME t, prf)) = f t #> fold_proof_terms f g prf
wenzelm@20147
   460
  | fold_proof_terms f g (AbsP (_, NONE, prf)) = fold_proof_terms f g prf
wenzelm@20147
   461
  | fold_proof_terms f g (prf % SOME t) = fold_proof_terms f g prf #> f t
wenzelm@20147
   462
  | fold_proof_terms f g (prf % NONE) = fold_proof_terms f g prf
wenzelm@20147
   463
  | fold_proof_terms f g (prf1 %% prf2) =
wenzelm@20147
   464
      fold_proof_terms f g prf1 #> fold_proof_terms f g prf2
wenzelm@20159
   465
  | fold_proof_terms _ g (PAxm (_, _, SOME Ts)) = fold g Ts
wenzelm@31943
   466
  | fold_proof_terms _ g (OfClass (T, _)) = g T
wenzelm@28828
   467
  | fold_proof_terms _ g (Oracle (_, _, SOME Ts)) = fold g Ts
wenzelm@28828
   468
  | fold_proof_terms _ g (Promise (_, _, Ts)) = fold g Ts
wenzelm@28803
   469
  | fold_proof_terms _ g (PThm (_, ((_, _, SOME Ts), _))) = fold g Ts
wenzelm@20147
   470
  | fold_proof_terms _ _ _ = I;
berghofe@11519
   471
wenzelm@20300
   472
fun maxidx_proof prf = fold_proof_terms Term.maxidx_term Term.maxidx_typ prf;
berghofe@12868
   473
berghofe@13744
   474
fun size_of_proof (Abst (_, _, prf)) = 1 + size_of_proof prf
wenzelm@63857
   475
  | size_of_proof (AbsP (_, _, prf)) = 1 + size_of_proof prf
wenzelm@28803
   476
  | size_of_proof (prf % _) = 1 + size_of_proof prf
berghofe@13744
   477
  | size_of_proof (prf1 %% prf2) = size_of_proof prf1 + size_of_proof prf2
berghofe@13744
   478
  | size_of_proof _ = 1;
berghofe@13744
   479
wenzelm@28803
   480
fun change_type opTs (PAxm (name, prop, _)) = PAxm (name, prop, opTs)
wenzelm@31943
   481
  | change_type (SOME [T]) (OfClass (_, c)) = OfClass (T, c)
berghofe@12907
   482
  | change_type opTs (Oracle (name, prop, _)) = Oracle (name, prop, opTs)
wenzelm@63857
   483
  | change_type _ (Promise _) = raise Fail "change_type: unexpected promise"
wenzelm@32057
   484
  | change_type opTs (PThm (i, ((name, prop, _), body))) =
wenzelm@32057
   485
      PThm (i, ((name, prop, opTs), body))
berghofe@12907
   486
  | change_type _ prf = prf;
berghofe@12907
   487
berghofe@11519
   488
berghofe@11519
   489
(***** utilities *****)
berghofe@11519
   490
berghofe@11519
   491
fun strip_abs (_::Ts) (Abs (_, _, t)) = strip_abs Ts t
berghofe@11519
   492
  | strip_abs _ t = t;
berghofe@11519
   493
skalberg@15570
   494
fun mk_abs Ts t = Library.foldl (fn (t', T) => Abs ("", T, t')) (t, Ts);
berghofe@11519
   495
berghofe@11519
   496
wenzelm@21646
   497
(*Abstraction of a proof term over its occurrences of v,
berghofe@11519
   498
    which must contain no loose bound variables.
berghofe@11519
   499
  The resulting proof term is ready to become the body of an Abst.*)
berghofe@11519
   500
berghofe@11519
   501
fun prf_abstract_over v =
berghofe@11519
   502
  let
berghofe@11715
   503
    fun abst' lev u = if v aconv u then Bound lev else
berghofe@11715
   504
      (case u of
berghofe@11715
   505
         Abs (a, T, t) => Abs (a, T, abst' (lev + 1) t)
wenzelm@32019
   506
       | f $ t => (abst' lev f $ absth' lev t handle Same.SAME => f $ abst' lev t)
wenzelm@32019
   507
       | _ => raise Same.SAME)
wenzelm@32019
   508
    and absth' lev t = (abst' lev t handle Same.SAME => t);
berghofe@11519
   509
berghofe@11715
   510
    fun abst lev (AbsP (a, t, prf)) =
wenzelm@32024
   511
          (AbsP (a, Same.map_option (abst' lev) t, absth lev prf)
wenzelm@32019
   512
           handle Same.SAME => AbsP (a, t, abst lev prf))
berghofe@11715
   513
      | abst lev (Abst (a, T, prf)) = Abst (a, T, abst (lev + 1) prf)
berghofe@11715
   514
      | abst lev (prf1 %% prf2) = (abst lev prf1 %% absth lev prf2
wenzelm@32019
   515
          handle Same.SAME => prf1 %% abst lev prf2)
skalberg@15570
   516
      | abst lev (prf % t) = (abst lev prf % Option.map (absth' lev) t
wenzelm@32024
   517
          handle Same.SAME => prf % Same.map_option (abst' lev) t)
wenzelm@32019
   518
      | abst _ _ = raise Same.SAME
wenzelm@32024
   519
    and absth lev prf = (abst lev prf handle Same.SAME => prf);
berghofe@11519
   520
berghofe@11715
   521
  in absth 0 end;
berghofe@11519
   522
berghofe@11519
   523
berghofe@11519
   524
(*increments a proof term's non-local bound variables
berghofe@11519
   525
  required when moving a proof term within abstractions
berghofe@11519
   526
     inc is  increment for bound variables
berghofe@11519
   527
     lev is  level at which a bound variable is considered 'loose'*)
berghofe@11519
   528
berghofe@11519
   529
fun incr_bv' inct tlev t = incr_bv (inct, tlev, t);
berghofe@11519
   530
wenzelm@63857
   531
fun prf_incr_bv' incP _ Plev _ (PBound i) =
wenzelm@32019
   532
      if i >= Plev then PBound (i+incP) else raise Same.SAME
berghofe@11715
   533
  | prf_incr_bv' incP inct Plev tlev (AbsP (a, t, body)) =
wenzelm@32024
   534
      (AbsP (a, Same.map_option (same (op =) (incr_bv' inct tlev)) t,
wenzelm@32019
   535
         prf_incr_bv incP inct (Plev+1) tlev body) handle Same.SAME =>
berghofe@11715
   536
           AbsP (a, t, prf_incr_bv' incP inct (Plev+1) tlev body))
berghofe@11715
   537
  | prf_incr_bv' incP inct Plev tlev (Abst (a, T, body)) =
berghofe@11715
   538
      Abst (a, T, prf_incr_bv' incP inct Plev (tlev+1) body)
wenzelm@21646
   539
  | prf_incr_bv' incP inct Plev tlev (prf %% prf') =
berghofe@11715
   540
      (prf_incr_bv' incP inct Plev tlev prf %% prf_incr_bv incP inct Plev tlev prf'
wenzelm@32019
   541
       handle Same.SAME => prf %% prf_incr_bv' incP inct Plev tlev prf')
wenzelm@21646
   542
  | prf_incr_bv' incP inct Plev tlev (prf % t) =
skalberg@15570
   543
      (prf_incr_bv' incP inct Plev tlev prf % Option.map (incr_bv' inct tlev) t
wenzelm@32024
   544
       handle Same.SAME => prf % Same.map_option (same (op =) (incr_bv' inct tlev)) t)
wenzelm@32019
   545
  | prf_incr_bv' _ _ _ _ _ = raise Same.SAME
berghofe@11715
   546
and prf_incr_bv incP inct Plev tlev prf =
wenzelm@32019
   547
      (prf_incr_bv' incP inct Plev tlev prf handle Same.SAME => prf);
berghofe@11519
   548
berghofe@11519
   549
fun incr_pboundvars  0 0 prf = prf
berghofe@11519
   550
  | incr_pboundvars incP inct prf = prf_incr_bv incP inct 0 0 prf;
berghofe@11519
   551
berghofe@11519
   552
berghofe@11615
   553
fun prf_loose_bvar1 (prf1 %% prf2) k = prf_loose_bvar1 prf1 k orelse prf_loose_bvar1 prf2 k
skalberg@15531
   554
  | prf_loose_bvar1 (prf % SOME t) k = prf_loose_bvar1 prf k orelse loose_bvar1 (t, k)
skalberg@15531
   555
  | prf_loose_bvar1 (_ % NONE) _ = true
skalberg@15531
   556
  | prf_loose_bvar1 (AbsP (_, SOME t, prf)) k = loose_bvar1 (t, k) orelse prf_loose_bvar1 prf k
wenzelm@63857
   557
  | prf_loose_bvar1 (AbsP (_, NONE, _)) _ = true
berghofe@11519
   558
  | prf_loose_bvar1 (Abst (_, _, prf)) k = prf_loose_bvar1 prf (k+1)
berghofe@11519
   559
  | prf_loose_bvar1 _ _ = false;
berghofe@11519
   560
berghofe@11519
   561
fun prf_loose_Pbvar1 (PBound i) k = i = k
berghofe@11615
   562
  | prf_loose_Pbvar1 (prf1 %% prf2) k = prf_loose_Pbvar1 prf1 k orelse prf_loose_Pbvar1 prf2 k
berghofe@11615
   563
  | prf_loose_Pbvar1 (prf % _) k = prf_loose_Pbvar1 prf k
berghofe@11519
   564
  | prf_loose_Pbvar1 (AbsP (_, _, prf)) k = prf_loose_Pbvar1 prf (k+1)
berghofe@11519
   565
  | prf_loose_Pbvar1 (Abst (_, _, prf)) k = prf_loose_Pbvar1 prf k
berghofe@11519
   566
  | prf_loose_Pbvar1 _ _ = false;
berghofe@11519
   567
wenzelm@63857
   568
fun prf_add_loose_bnos plev _ (PBound i) (is, js) =
wenzelm@17492
   569
      if i < plev then (is, js) else (insert (op =) (i-plev) is, js)
berghofe@12279
   570
  | prf_add_loose_bnos plev tlev (prf1 %% prf2) p =
berghofe@12279
   571
      prf_add_loose_bnos plev tlev prf2
berghofe@12279
   572
        (prf_add_loose_bnos plev tlev prf1 p)
berghofe@12279
   573
  | prf_add_loose_bnos plev tlev (prf % opt) (is, js) =
wenzelm@64566
   574
      prf_add_loose_bnos plev tlev prf
wenzelm@64566
   575
        (case opt of
wenzelm@17492
   576
          NONE => (is, insert (op =) ~1 js)
skalberg@15531
   577
        | SOME t => (is, add_loose_bnos (t, tlev, js)))
berghofe@12279
   578
  | prf_add_loose_bnos plev tlev (AbsP (_, opt, prf)) (is, js) =
wenzelm@64566
   579
      prf_add_loose_bnos (plev+1) tlev prf
wenzelm@64566
   580
        (case opt of
wenzelm@17492
   581
          NONE => (is, insert (op =) ~1 js)
skalberg@15531
   582
        | SOME t => (is, add_loose_bnos (t, tlev, js)))
berghofe@12279
   583
  | prf_add_loose_bnos plev tlev (Abst (_, _, prf)) p =
berghofe@12279
   584
      prf_add_loose_bnos plev (tlev+1) prf p
berghofe@12279
   585
  | prf_add_loose_bnos _ _ _ _ = ([], []);
berghofe@12279
   586
berghofe@11519
   587
berghofe@11519
   588
(**** substitutions ****)
berghofe@11519
   589
wenzelm@31977
   590
fun del_conflicting_tvars envT T = Term_Subst.instantiateT
wenzelm@19482
   591
  (map_filter (fn ixnS as (_, S) =>
haftmann@26328
   592
     (Type.lookup envT ixnS; NONE) handle TYPE _ =>
wenzelm@44118
   593
        SOME (ixnS, TFree ("'dummy", S))) (Term.add_tvarsT T [])) T;
berghofe@18316
   594
wenzelm@31977
   595
fun del_conflicting_vars env t = Term_Subst.instantiate
wenzelm@19482
   596
  (map_filter (fn ixnS as (_, S) =>
wenzelm@32019
   597
     (Type.lookup (Envir.type_env env) ixnS; NONE) handle TYPE _ =>
wenzelm@44118
   598
        SOME (ixnS, TFree ("'dummy", S))) (Term.add_tvars t []),
wenzelm@44118
   599
   map_filter (fn (ixnT as (_, T)) =>
wenzelm@51700
   600
     (Envir.lookup env ixnT; NONE) handle TYPE _ =>
wenzelm@44118
   601
        SOME (ixnT, Free ("dummy", T))) (Term.add_vars t [])) t;
berghofe@18316
   602
berghofe@11519
   603
fun norm_proof env =
berghofe@11519
   604
  let
wenzelm@32019
   605
    val envT = Envir.type_env env;
berghofe@18316
   606
    fun msg s = warning ("type conflict in norm_proof:\n" ^ s);
berghofe@18316
   607
    fun htype f t = f env t handle TYPE (s, _, _) =>
berghofe@18316
   608
      (msg s; f env (del_conflicting_vars env t));
berghofe@18316
   609
    fun htypeT f T = f envT T handle TYPE (s, _, _) =>
berghofe@18316
   610
      (msg s; f envT (del_conflicting_tvars envT T));
berghofe@18316
   611
    fun htypeTs f Ts = f envT Ts handle TYPE (s, _, _) =>
berghofe@18316
   612
      (msg s; f envT (map (del_conflicting_tvars envT) Ts));
wenzelm@32024
   613
wenzelm@32019
   614
    fun norm (Abst (s, T, prf)) =
wenzelm@32024
   615
          (Abst (s, Same.map_option (htypeT Envir.norm_type_same) T, Same.commit norm prf)
wenzelm@32019
   616
            handle Same.SAME => Abst (s, T, norm prf))
wenzelm@32019
   617
      | norm (AbsP (s, t, prf)) =
wenzelm@32024
   618
          (AbsP (s, Same.map_option (htype Envir.norm_term_same) t, Same.commit norm prf)
wenzelm@32019
   619
            handle Same.SAME => AbsP (s, t, norm prf))
wenzelm@32019
   620
      | norm (prf % t) =
wenzelm@32019
   621
          (norm prf % Option.map (htype Envir.norm_term) t
wenzelm@32024
   622
            handle Same.SAME => prf % Same.map_option (htype Envir.norm_term_same) t)
wenzelm@32019
   623
      | norm (prf1 %% prf2) =
wenzelm@32019
   624
          (norm prf1 %% Same.commit norm prf2
wenzelm@32019
   625
            handle Same.SAME => prf1 %% norm prf2)
wenzelm@32019
   626
      | norm (PAxm (s, prop, Ts)) =
wenzelm@32024
   627
          PAxm (s, prop, Same.map_option (htypeTs Envir.norm_types_same) Ts)
wenzelm@32019
   628
      | norm (OfClass (T, c)) =
wenzelm@32019
   629
          OfClass (htypeT Envir.norm_type_same T, c)
wenzelm@32019
   630
      | norm (Oracle (s, prop, Ts)) =
wenzelm@32024
   631
          Oracle (s, prop, Same.map_option (htypeTs Envir.norm_types_same) Ts)
wenzelm@32019
   632
      | norm (Promise (i, prop, Ts)) =
wenzelm@32019
   633
          Promise (i, prop, htypeTs Envir.norm_types_same Ts)
wenzelm@28803
   634
      | norm (PThm (i, ((s, t, Ts), body))) =
wenzelm@32024
   635
          PThm (i, ((s, t, Same.map_option (htypeTs Envir.norm_types_same) Ts), body))
wenzelm@32019
   636
      | norm _ = raise Same.SAME;
wenzelm@32019
   637
  in Same.commit norm end;
berghofe@11519
   638
wenzelm@28803
   639
berghofe@11519
   640
(***** Remove some types in proof term (to save space) *****)
berghofe@11519
   641
berghofe@11519
   642
fun remove_types (Abs (s, _, t)) = Abs (s, dummyT, remove_types t)
berghofe@11519
   643
  | remove_types (t $ u) = remove_types t $ remove_types u
berghofe@11519
   644
  | remove_types (Const (s, _)) = Const (s, dummyT)
berghofe@11519
   645
  | remove_types t = t;
berghofe@11519
   646
wenzelm@32032
   647
fun remove_types_env (Envir.Envir {maxidx, tenv, tyenv}) =
haftmann@39020
   648
  Envir.Envir {maxidx = maxidx, tenv = Vartab.map (K (apsnd remove_types)) tenv, tyenv = tyenv};
berghofe@11519
   649
berghofe@11519
   650
fun norm_proof' env prf = norm_proof (remove_types_env env) prf;
berghofe@11519
   651
wenzelm@28803
   652
berghofe@11519
   653
(**** substitution of bound variables ****)
berghofe@11519
   654
berghofe@11519
   655
fun prf_subst_bounds args prf =
berghofe@11519
   656
  let
berghofe@11519
   657
    val n = length args;
berghofe@11519
   658
    fun subst' lev (Bound i) =
wenzelm@32019
   659
         (if i<lev then raise Same.SAME    (*var is locally bound*)
wenzelm@30146
   660
          else  incr_boundvars lev (nth args (i-lev))
wenzelm@43278
   661
                  handle General.Subscript => Bound (i-n))  (*loose: change it*)
berghofe@11519
   662
      | subst' lev (Abs (a, T, body)) = Abs (a, T,  subst' (lev+1) body)
berghofe@11519
   663
      | subst' lev (f $ t) = (subst' lev f $ substh' lev t
wenzelm@32019
   664
          handle Same.SAME => f $ subst' lev t)
wenzelm@32019
   665
      | subst' _ _ = raise Same.SAME
wenzelm@32019
   666
    and substh' lev t = (subst' lev t handle Same.SAME => t);
berghofe@11519
   667
wenzelm@32057
   668
    fun subst lev (AbsP (a, t, body)) =
wenzelm@32057
   669
        (AbsP (a, Same.map_option (subst' lev) t, substh lev body)
wenzelm@32019
   670
          handle Same.SAME => AbsP (a, t, subst lev body))
berghofe@11519
   671
      | subst lev (Abst (a, T, body)) = Abst (a, T, subst (lev+1) body)
berghofe@11615
   672
      | subst lev (prf %% prf') = (subst lev prf %% substh lev prf'
wenzelm@32019
   673
          handle Same.SAME => prf %% subst lev prf')
skalberg@15570
   674
      | subst lev (prf % t) = (subst lev prf % Option.map (substh' lev) t
wenzelm@32024
   675
          handle Same.SAME => prf % Same.map_option (subst' lev) t)
wenzelm@32019
   676
      | subst _ _ = raise Same.SAME
wenzelm@32024
   677
    and substh lev prf = (subst lev prf handle Same.SAME => prf);
wenzelm@64566
   678
  in (case args of [] => prf | _ => substh 0 prf) end;
berghofe@11519
   679
berghofe@11519
   680
fun prf_subst_pbounds args prf =
berghofe@11519
   681
  let
berghofe@11519
   682
    val n = length args;
berghofe@11519
   683
    fun subst (PBound i) Plev tlev =
wenzelm@32019
   684
         (if i < Plev then raise Same.SAME    (*var is locally bound*)
wenzelm@30146
   685
          else incr_pboundvars Plev tlev (nth args (i-Plev))
wenzelm@43278
   686
                 handle General.Subscript => PBound (i-n)  (*loose: change it*))
berghofe@11519
   687
      | subst (AbsP (a, t, body)) Plev tlev = AbsP (a, t, subst body (Plev+1) tlev)
berghofe@11519
   688
      | subst (Abst (a, T, body)) Plev tlev = Abst (a, T, subst body Plev (tlev+1))
berghofe@11615
   689
      | subst (prf %% prf') Plev tlev = (subst prf Plev tlev %% substh prf' Plev tlev
wenzelm@32019
   690
          handle Same.SAME => prf %% subst prf' Plev tlev)
berghofe@11615
   691
      | subst (prf % t) Plev tlev = subst prf Plev tlev % t
wenzelm@63857
   692
      | subst  _ _ _ = raise Same.SAME
wenzelm@32019
   693
    and substh prf Plev tlev = (subst prf Plev tlev handle Same.SAME => prf)
wenzelm@64566
   694
  in (case args of [] => prf | _ => substh prf 0 0) end;
berghofe@11519
   695
berghofe@11519
   696
berghofe@11519
   697
(**** Freezing and thawing of variables in proof terms ****)
berghofe@11519
   698
wenzelm@44101
   699
local
wenzelm@44101
   700
berghofe@11519
   701
fun frzT names =
wenzelm@44101
   702
  map_type_tvar (fn (ixn, S) => TFree (the (AList.lookup (op =) names ixn), S));
berghofe@11519
   703
berghofe@11519
   704
fun thawT names =
wenzelm@44101
   705
  map_type_tfree (fn (a, S) =>
wenzelm@44101
   706
    (case AList.lookup (op =) names a of
wenzelm@44101
   707
      NONE => TFree (a, S)
wenzelm@44101
   708
    | SOME ixn => TVar (ixn, S)));
berghofe@11519
   709
berghofe@11519
   710
fun freeze names names' (t $ u) =
berghofe@11519
   711
      freeze names names' t $ freeze names names' u
berghofe@11519
   712
  | freeze names names' (Abs (s, T, t)) =
berghofe@11519
   713
      Abs (s, frzT names' T, freeze names names' t)
wenzelm@63857
   714
  | freeze _ names' (Const (s, T)) = Const (s, frzT names' T)
wenzelm@63857
   715
  | freeze _ names' (Free (s, T)) = Free (s, frzT names' T)
berghofe@11519
   716
  | freeze names names' (Var (ixn, T)) =
wenzelm@44101
   717
      Free (the (AList.lookup (op =) names ixn), frzT names' T)
wenzelm@63857
   718
  | freeze _ _ t = t;
berghofe@11519
   719
berghofe@11519
   720
fun thaw names names' (t $ u) =
berghofe@11519
   721
      thaw names names' t $ thaw names names' u
berghofe@11519
   722
  | thaw names names' (Abs (s, T, t)) =
berghofe@11519
   723
      Abs (s, thawT names' T, thaw names names' t)
wenzelm@63857
   724
  | thaw _ names' (Const (s, T)) = Const (s, thawT names' T)
wenzelm@21646
   725
  | thaw names names' (Free (s, T)) =
wenzelm@44101
   726
      let val T' = thawT names' T in
wenzelm@44101
   727
        (case AList.lookup (op =) names s of
skalberg@15531
   728
          NONE => Free (s, T')
wenzelm@44101
   729
        | SOME ixn => Var (ixn, T'))
berghofe@11519
   730
      end
wenzelm@63857
   731
  | thaw _ names' (Var (ixn, T)) = Var (ixn, thawT names' T)
wenzelm@63857
   732
  | thaw _ _ t = t;
berghofe@11519
   733
wenzelm@44101
   734
in
wenzelm@44101
   735
berghofe@11519
   736
fun freeze_thaw_prf prf =
berghofe@11519
   737
  let
berghofe@11519
   738
    val (fs, Tfs, vs, Tvs) = fold_proof_terms
wenzelm@20147
   739
      (fn t => fn (fs, Tfs, vs, Tvs) =>
wenzelm@29261
   740
         (Term.add_free_names t fs, Term.add_tfree_names t Tfs,
wenzelm@29261
   741
          Term.add_var_names t vs, Term.add_tvar_names t Tvs))
wenzelm@20147
   742
      (fn T => fn (fs, Tfs, vs, Tvs) =>
wenzelm@29261
   743
         (fs, Term.add_tfree_namesT T Tfs,
wenzelm@29261
   744
          vs, Term.add_tvar_namesT T Tvs))
wenzelm@20147
   745
      prf ([], [], [], []);
wenzelm@29261
   746
    val names = vs ~~ Name.variant_list fs (map fst vs);
wenzelm@20071
   747
    val names' = Tvs ~~ Name.variant_list Tfs (map fst Tvs);
berghofe@11519
   748
    val rnames = map swap names;
berghofe@11519
   749
    val rnames' = map swap names';
berghofe@11519
   750
  in
berghofe@11519
   751
    (map_proof_terms (freeze names names') (frzT names') prf,
berghofe@11519
   752
     map_proof_terms (thaw rnames rnames') (thawT rnames'))
berghofe@11519
   753
  end;
berghofe@11519
   754
wenzelm@44101
   755
end;
wenzelm@44101
   756
berghofe@11519
   757
berghofe@11519
   758
(***** implication introduction *****)
berghofe@11519
   759
berghofe@37231
   760
fun gen_implies_intr_proof f h prf =
berghofe@11519
   761
  let
wenzelm@32019
   762
    fun abshyp i (Hyp t) = if h aconv t then PBound i else raise Same.SAME
berghofe@11519
   763
      | abshyp i (Abst (s, T, prf)) = Abst (s, T, abshyp i prf)
wenzelm@32024
   764
      | abshyp i (AbsP (s, t, prf)) = AbsP (s, t, abshyp (i + 1) prf)
berghofe@11615
   765
      | abshyp i (prf % t) = abshyp i prf % t
wenzelm@32024
   766
      | abshyp i (prf1 %% prf2) =
wenzelm@32024
   767
          (abshyp i prf1 %% abshyph i prf2
wenzelm@32024
   768
            handle Same.SAME => prf1 %% abshyp i prf2)
wenzelm@32019
   769
      | abshyp _ _ = raise Same.SAME
wenzelm@32024
   770
    and abshyph i prf = (abshyp i prf handle Same.SAME => prf);
berghofe@11519
   771
  in
berghofe@37231
   772
    AbsP ("H", f h, abshyph 0 prf)
berghofe@11519
   773
  end;
berghofe@11519
   774
berghofe@37231
   775
val implies_intr_proof = gen_implies_intr_proof (K NONE);
berghofe@37231
   776
val implies_intr_proof' = gen_implies_intr_proof SOME;
berghofe@37231
   777
berghofe@11519
   778
berghofe@11519
   779
(***** forall introduction *****)
berghofe@11519
   780
skalberg@15531
   781
fun forall_intr_proof x a prf = Abst (a, NONE, prf_abstract_over x prf);
berghofe@11519
   782
berghofe@37231
   783
fun forall_intr_proof' t prf =
berghofe@37231
   784
  let val (a, T) = (case t of Var ((a, _), T) => (a, T) | Free p => p)
berghofe@37231
   785
  in Abst (a, SOME T, prf_abstract_over t prf) end;
berghofe@37231
   786
berghofe@11519
   787
berghofe@11519
   788
(***** varify *****)
berghofe@11519
   789
berghofe@11519
   790
fun varify_proof t fixed prf =
berghofe@11519
   791
  let
wenzelm@19304
   792
    val fs = Term.fold_types (Term.fold_atyps
wenzelm@19304
   793
      (fn TFree v => if member (op =) fixed v then I else insert (op =) v | _ => I)) t [];
wenzelm@29261
   794
    val used = Name.context
wenzelm@29261
   795
      |> fold_types (fold_atyps (fn TVar ((a, _), _) => Name.declare a | _ => I)) t;
wenzelm@43326
   796
    val fmap = fs ~~ #1 (fold_map Name.variant (map fst fs) used);
wenzelm@63857
   797
    fun thaw (a, S) =
wenzelm@63857
   798
      (case AList.lookup (op =) fmap (a, S) of
wenzelm@63857
   799
        NONE => TFree (a, S)
skalberg@15531
   800
      | SOME b => TVar ((b, 0), S));
wenzelm@28803
   801
  in map_proof_terms (map_types (map_type_tfree thaw)) (map_type_tfree thaw) prf end;
berghofe@11519
   802
berghofe@11519
   803
berghofe@11519
   804
local
berghofe@11519
   805
wenzelm@44116
   806
fun new_name ix (pairs, used) =
wenzelm@43324
   807
  let val v = singleton (Name.variant_list used) (string_of_indexname ix)
wenzelm@44116
   808
  in ((ix, v) :: pairs, v :: used) end;
berghofe@11519
   809
wenzelm@44116
   810
fun freeze_one alist (ix, sort) =
wenzelm@44116
   811
  (case AList.lookup (op =) alist ix of
skalberg@15531
   812
    NONE => TVar (ix, sort)
skalberg@15531
   813
  | SOME name => TFree (name, sort));
berghofe@11519
   814
berghofe@11519
   815
in
berghofe@11519
   816
wenzelm@36619
   817
fun legacy_freezeT t prf =
berghofe@11519
   818
  let
wenzelm@44116
   819
    val used = Term.add_tfree_names t [];
wenzelm@44116
   820
    val (alist, _) = fold_rev new_name (map #1 (Term.add_tvars t [])) ([], used);
berghofe@11519
   821
  in
berghofe@11519
   822
    (case alist of
berghofe@11519
   823
      [] => prf (*nothing to do!*)
berghofe@11519
   824
    | _ =>
wenzelm@44116
   825
        let val frzT = map_type_tvar (freeze_one alist)
wenzelm@44116
   826
        in map_proof_terms (map_types frzT) frzT prf end)
berghofe@11519
   827
  end;
berghofe@11519
   828
berghofe@11519
   829
end;
berghofe@11519
   830
berghofe@11519
   831
berghofe@11519
   832
(***** rotate assumptions *****)
berghofe@11519
   833
berghofe@11519
   834
fun rotate_proof Bs Bi m prf =
berghofe@11519
   835
  let
berghofe@11519
   836
    val params = Term.strip_all_vars Bi;
berghofe@11519
   837
    val asms = Logic.strip_imp_prems (Term.strip_all_body Bi);
berghofe@11519
   838
    val i = length asms;
berghofe@11519
   839
    val j = length Bs;
berghofe@11519
   840
  in
berghofe@11519
   841
    mk_AbsP (j+1, proof_combP (prf, map PBound
wenzelm@23178
   842
      (j downto 1) @ [mk_Abst params (mk_AbsP (i,
berghofe@11519
   843
        proof_combP (proof_combt (PBound i, map Bound ((length params - 1) downto 0)),
wenzelm@23178
   844
          map PBound (((i-m-1) downto 0) @ ((i-1) downto (i-m))))))]))
berghofe@11519
   845
  end;
berghofe@11519
   846
berghofe@11519
   847
berghofe@11519
   848
(***** permute premises *****)
berghofe@11519
   849
wenzelm@36742
   850
fun permute_prems_proof prems j k prf =
berghofe@11519
   851
  let val n = length prems
berghofe@11519
   852
  in mk_AbsP (n, proof_combP (prf,
berghofe@11519
   853
    map PBound ((n-1 downto n-j) @ (k-1 downto 0) @ (n-j-1 downto k))))
berghofe@11519
   854
  end;
berghofe@11519
   855
berghofe@11519
   856
wenzelm@19908
   857
(***** generalization *****)
wenzelm@19908
   858
wenzelm@20000
   859
fun generalize (tfrees, frees) idx =
wenzelm@36620
   860
  Same.commit (map_proof_terms_same
wenzelm@36620
   861
    (Term_Subst.generalize_same (tfrees, frees) idx)
wenzelm@36620
   862
    (Term_Subst.generalizeT_same tfrees idx));
wenzelm@19908
   863
wenzelm@19908
   864
berghofe@11519
   865
(***** instantiation *****)
berghofe@11519
   866
wenzelm@20000
   867
fun instantiate (instT, inst) =
wenzelm@36620
   868
  Same.commit (map_proof_terms_same
wenzelm@36620
   869
    (Term_Subst.instantiate_same (instT, map (apsnd remove_types) inst))
wenzelm@36620
   870
    (Term_Subst.instantiateT_same instT));
berghofe@11519
   871
berghofe@11519
   872
berghofe@11519
   873
(***** lifting *****)
berghofe@11519
   874
berghofe@11519
   875
fun lift_proof Bi inc prop prf =
berghofe@11519
   876
  let
wenzelm@32024
   877
    fun lift'' Us Ts t =
wenzelm@59787
   878
      strip_abs Ts (Logic.incr_indexes ([], Us, inc) (mk_abs Ts t));
berghofe@11519
   879
berghofe@11715
   880
    fun lift' Us Ts (Abst (s, T, prf)) =
wenzelm@32024
   881
          (Abst (s, Same.map_option (Logic.incr_tvar_same inc) T, lifth' Us (dummyT::Ts) prf)
wenzelm@32019
   882
           handle Same.SAME => Abst (s, T, lift' Us (dummyT::Ts) prf))
berghofe@11715
   883
      | lift' Us Ts (AbsP (s, t, prf)) =
wenzelm@32024
   884
          (AbsP (s, Same.map_option (same (op =) (lift'' Us Ts)) t, lifth' Us Ts prf)
wenzelm@32019
   885
           handle Same.SAME => AbsP (s, t, lift' Us Ts prf))
skalberg@15570
   886
      | lift' Us Ts (prf % t) = (lift' Us Ts prf % Option.map (lift'' Us Ts) t
wenzelm@32024
   887
          handle Same.SAME => prf % Same.map_option (same (op =) (lift'' Us Ts)) t)
berghofe@11715
   888
      | lift' Us Ts (prf1 %% prf2) = (lift' Us Ts prf1 %% lifth' Us Ts prf2
wenzelm@32019
   889
          handle Same.SAME => prf1 %% lift' Us Ts prf2)
berghofe@11715
   890
      | lift' _ _ (PAxm (s, prop, Ts)) =
wenzelm@32024
   891
          PAxm (s, prop, (Same.map_option o Same.map) (Logic.incr_tvar_same inc) Ts)
wenzelm@31943
   892
      | lift' _ _ (OfClass (T, c)) =
wenzelm@32024
   893
          OfClass (Logic.incr_tvar_same inc T, c)
wenzelm@28828
   894
      | lift' _ _ (Oracle (s, prop, Ts)) =
wenzelm@32024
   895
          Oracle (s, prop, (Same.map_option o Same.map) (Logic.incr_tvar_same inc) Ts)
wenzelm@28828
   896
      | lift' _ _ (Promise (i, prop, Ts)) =
wenzelm@32024
   897
          Promise (i, prop, Same.map (Logic.incr_tvar_same inc) Ts)
wenzelm@28803
   898
      | lift' _ _ (PThm (i, ((s, prop, Ts), body))) =
wenzelm@32024
   899
          PThm (i, ((s, prop, (Same.map_option o Same.map) (Logic.incr_tvar inc) Ts), body))
wenzelm@32019
   900
      | lift' _ _ _ = raise Same.SAME
wenzelm@32019
   901
    and lifth' Us Ts prf = (lift' Us Ts prf handle Same.SAME => prf);
berghofe@11519
   902
wenzelm@18030
   903
    val ps = map (Logic.lift_all inc Bi) (Logic.strip_imp_prems prop);
berghofe@11519
   904
    val k = length ps;
berghofe@11519
   905
wenzelm@23178
   906
    fun mk_app b (i, j, prf) =
berghofe@11615
   907
          if b then (i-1, j, prf %% PBound i) else (i, j-1, prf %> Bound j);
berghofe@11519
   908
wenzelm@56245
   909
    fun lift Us bs i j (Const ("Pure.imp", _) $ A $ B) =
wenzelm@20147
   910
            AbsP ("H", NONE (*A*), lift Us (true::bs) (i+1) j B)
wenzelm@56245
   911
      | lift Us bs i j (Const ("Pure.all", _) $ Abs (a, T, t)) =
wenzelm@20147
   912
            Abst (a, NONE (*T*), lift (T::Us) (false::bs) i (j+1) t)
berghofe@11715
   913
      | lift Us bs i j _ = proof_combP (lifth' (rev Us) [] prf,
wenzelm@23178
   914
            map (fn k => (#3 (fold_rev mk_app bs (i-1, j-1, PBound k))))
berghofe@11519
   915
              (i + k - 1 downto i));
berghofe@11519
   916
  in
berghofe@11519
   917
    mk_AbsP (k, lift [] [] 0 0 Bi)
berghofe@11519
   918
  end;
berghofe@11519
   919
wenzelm@32027
   920
fun incr_indexes i =
wenzelm@36620
   921
  Same.commit (map_proof_terms_same
wenzelm@59787
   922
    (Logic.incr_indexes_same ([], [], i)) (Logic.incr_tvar_same i));
wenzelm@32027
   923
berghofe@11519
   924
berghofe@11519
   925
(***** proof by assumption *****)
berghofe@11519
   926
berghofe@23296
   927
fun mk_asm_prf t i m =
berghofe@23296
   928
  let
berghofe@23296
   929
    fun imp_prf _ i 0 = PBound i
wenzelm@56245
   930
      | imp_prf (Const ("Pure.imp", _) $ A $ B) i m = AbsP ("H", NONE (*A*), imp_prf B (i+1) (m-1))
berghofe@23296
   931
      | imp_prf _ i _ = PBound i;
wenzelm@56245
   932
    fun all_prf (Const ("Pure.all", _) $ Abs (a, T, t)) = Abst (a, NONE (*T*), all_prf t)
berghofe@23296
   933
      | all_prf t = imp_prf t (~i) m
berghofe@23296
   934
  in all_prf t end;
berghofe@11519
   935
berghofe@11519
   936
fun assumption_proof Bs Bi n prf =
berghofe@11519
   937
  mk_AbsP (length Bs, proof_combP (prf,
berghofe@23296
   938
    map PBound (length Bs - 1 downto 0) @ [mk_asm_prf Bi n ~1]));
berghofe@11519
   939
berghofe@11519
   940
berghofe@11519
   941
(***** Composition of object rule with proof state *****)
berghofe@11519
   942
wenzelm@56245
   943
fun flatten_params_proof i j n (Const ("Pure.imp", _) $ A $ B, k) =
skalberg@15531
   944
      AbsP ("H", NONE (*A*), flatten_params_proof (i+1) j n (B, k))
wenzelm@56245
   945
  | flatten_params_proof i j n (Const ("Pure.all", _) $ Abs (a, T, t), k) =
skalberg@15531
   946
      Abst (a, NONE (*T*), flatten_params_proof i (j+1) n (t, k))
berghofe@11519
   947
  | flatten_params_proof i j n (_, k) = proof_combP (proof_combt (PBound (k+i),
wenzelm@19304
   948
      map Bound (j-1 downto 0)), map PBound (remove (op =) (i-n) (i-1 downto 0)));
berghofe@11519
   949
berghofe@23296
   950
fun bicompose_proof flatten Bs oldAs newAs A n m rprf sprf =
berghofe@11519
   951
  let
berghofe@11519
   952
    val la = length newAs;
berghofe@11519
   953
    val lb = length Bs;
berghofe@11519
   954
  in
berghofe@11519
   955
    mk_AbsP (lb+la, proof_combP (sprf,
berghofe@11615
   956
      map PBound (lb + la - 1 downto la)) %%
berghofe@23296
   957
        proof_combP (rprf, (if n>0 then [mk_asm_prf (the A) n m] else []) @
wenzelm@18485
   958
          map (if flatten then flatten_params_proof 0 0 n else PBound o snd)
wenzelm@18485
   959
            (oldAs ~~ (la - 1 downto 0))))
berghofe@11519
   960
  end;
berghofe@11519
   961
berghofe@11519
   962
berghofe@11519
   963
(***** axioms for equality *****)
berghofe@11519
   964
wenzelm@14854
   965
val aT = TFree ("'a", []);
wenzelm@14854
   966
val bT = TFree ("'b", []);
berghofe@11519
   967
val x = Free ("x", aT);
berghofe@11519
   968
val y = Free ("y", aT);
berghofe@11519
   969
val z = Free ("z", aT);
berghofe@11519
   970
val A = Free ("A", propT);
berghofe@11519
   971
val B = Free ("B", propT);
berghofe@11519
   972
val f = Free ("f", aT --> bT);
berghofe@11519
   973
val g = Free ("g", aT --> bT);
berghofe@11519
   974
berghofe@11519
   975
val equality_axms =
wenzelm@35851
   976
 [("reflexive", Logic.mk_equals (x, x)),
wenzelm@35851
   977
  ("symmetric", Logic.mk_implies (Logic.mk_equals (x, y), Logic.mk_equals (y, x))),
wenzelm@35851
   978
  ("transitive",
wenzelm@35851
   979
    Logic.list_implies ([Logic.mk_equals (x, y), Logic.mk_equals (y, z)], Logic.mk_equals (x, z))),
wenzelm@35851
   980
  ("equal_intr",
wenzelm@35851
   981
    Logic.list_implies ([Logic.mk_implies (A, B), Logic.mk_implies (B, A)], Logic.mk_equals (A, B))),
wenzelm@35851
   982
  ("equal_elim", Logic.list_implies ([Logic.mk_equals (A, B), A], B)),
wenzelm@35851
   983
  ("abstract_rule",
wenzelm@35851
   984
    Logic.mk_implies
wenzelm@35851
   985
      (Logic.all x
wenzelm@35851
   986
        (Logic.mk_equals (f $ x, g $ x)), Logic.mk_equals (lambda x (f $ x), lambda x (g $ x)))),
wenzelm@35851
   987
  ("combination", Logic.list_implies
wenzelm@35851
   988
    ([Logic.mk_equals (f, g), Logic.mk_equals (x, y)], Logic.mk_equals (f $ x, g $ y)))];
berghofe@11519
   989
berghofe@11519
   990
val [reflexive_axm, symmetric_axm, transitive_axm, equal_intr_axm,
berghofe@11519
   991
  equal_elim_axm, abstract_rule_axm, combination_axm] =
wenzelm@35851
   992
    map (fn (s, t) => PAxm ("Pure." ^ s, Logic.varify_global t, NONE)) equality_axms;
berghofe@11519
   993
skalberg@15531
   994
val reflexive = reflexive_axm % NONE;
berghofe@11519
   995
wenzelm@26424
   996
fun symmetric (prf as PAxm ("Pure.reflexive", _, _) % _) = prf
skalberg@15531
   997
  | symmetric prf = symmetric_axm % NONE % NONE %% prf;
berghofe@11519
   998
wenzelm@26424
   999
fun transitive _ _ (PAxm ("Pure.reflexive", _, _) % _) prf2 = prf2
wenzelm@26424
  1000
  | transitive _ _ prf1 (PAxm ("Pure.reflexive", _, _) % _) = prf1
berghofe@11519
  1001
  | transitive u (Type ("prop", [])) prf1 prf2 =
skalberg@15531
  1002
      transitive_axm % NONE % SOME (remove_types u) % NONE %% prf1 %% prf2
wenzelm@63857
  1003
  | transitive _ _ prf1 prf2 = transitive_axm % NONE % NONE % NONE %% prf1 %% prf2;
berghofe@11519
  1004
berghofe@11519
  1005
fun abstract_rule x a prf =
skalberg@15531
  1006
  abstract_rule_axm % NONE % NONE %% forall_intr_proof x a prf;
berghofe@11519
  1007
wenzelm@63857
  1008
fun check_comb (PAxm ("Pure.combination", _, _) % f % _ % _ % _ %% prf %% _) =
wenzelm@19502
  1009
      is_some f orelse check_comb prf
wenzelm@26424
  1010
  | check_comb (PAxm ("Pure.transitive", _, _) % _ % _ % _ %% prf1 %% prf2) =
berghofe@11519
  1011
      check_comb prf1 andalso check_comb prf2
wenzelm@26424
  1012
  | check_comb (PAxm ("Pure.symmetric", _, _) % _ % _ %% prf) = check_comb prf
berghofe@11519
  1013
  | check_comb _ = false;
berghofe@11519
  1014
berghofe@11519
  1015
fun combination f g t u (Type (_, [T, U])) prf1 prf2 =
berghofe@11519
  1016
  let
berghofe@11519
  1017
    val f = Envir.beta_norm f;
berghofe@11519
  1018
    val g = Envir.beta_norm g;
wenzelm@44100
  1019
    val prf =
wenzelm@44100
  1020
      if check_comb prf1 then
skalberg@15531
  1021
        combination_axm % NONE % NONE
wenzelm@44100
  1022
      else
wenzelm@44100
  1023
        (case prf1 of
wenzelm@26424
  1024
          PAxm ("Pure.reflexive", _, _) % _ =>
skalberg@15531
  1025
            combination_axm %> remove_types f % NONE
berghofe@11615
  1026
        | _ => combination_axm %> remove_types f %> remove_types g)
berghofe@11519
  1027
  in
berghofe@11519
  1028
    (case T of
wenzelm@44100
  1029
      Type ("fun", _) => prf %
wenzelm@44100
  1030
        (case head_of f of
wenzelm@44100
  1031
          Abs _ => SOME (remove_types t)
wenzelm@44100
  1032
        | Var _ => SOME (remove_types t)
wenzelm@44100
  1033
        | _ => NONE) %
wenzelm@44100
  1034
        (case head_of g of
wenzelm@64566
  1035
          Abs _ => SOME (remove_types u)
wenzelm@44100
  1036
        | Var _ => SOME (remove_types u)
wenzelm@44100
  1037
        | _ => NONE) %% prf1 %% prf2
skalberg@15531
  1038
     | _ => prf % NONE % NONE %% prf1 %% prf2)
berghofe@11519
  1039
  end;
berghofe@11519
  1040
berghofe@11519
  1041
fun equal_intr A B prf1 prf2 =
berghofe@11615
  1042
  equal_intr_axm %> remove_types A %> remove_types B %% prf1 %% prf2;
berghofe@11519
  1043
berghofe@11519
  1044
fun equal_elim A B prf1 prf2 =
berghofe@11615
  1045
  equal_elim_axm %> remove_types A %> remove_types B %% prf1 %% prf2;
berghofe@11519
  1046
berghofe@11519
  1047
wenzelm@36740
  1048
(**** type classes ****)
wenzelm@36621
  1049
wenzelm@36621
  1050
fun strip_shyps_proof algebra present witnessed extra_sorts prf =
wenzelm@36621
  1051
  let
wenzelm@36621
  1052
    fun get S2 (T, S1) = if Sorts.sort_le algebra (S1, S2) then SOME T else NONE;
wenzelm@36732
  1053
    val extra = map (fn S => (TFree ("'dummy", S), S)) extra_sorts;
wenzelm@36621
  1054
    val replacements = present @ extra @ witnessed;
wenzelm@36621
  1055
    fun replace T =
wenzelm@36621
  1056
      if exists (fn (T', _) => T' = T) present then raise Same.SAME
wenzelm@36621
  1057
      else
wenzelm@36621
  1058
        (case get_first (get (Type.sort_of_atyp T)) replacements of
wenzelm@36621
  1059
          SOME T' => T'
wenzelm@36621
  1060
        | NONE => raise Fail "strip_shyps_proof: bad type variable in proof term");
wenzelm@36621
  1061
  in Same.commit (map_proof_types_same (Term_Subst.map_atypsT_same replace)) prf end;
wenzelm@36621
  1062
wenzelm@36621
  1063
wenzelm@36740
  1064
local
wenzelm@36740
  1065
wenzelm@36740
  1066
type axclass_proofs =
wenzelm@36740
  1067
 {classrel_proof: theory -> class * class -> proof,
wenzelm@36740
  1068
  arity_proof: theory -> string * sort list * class -> proof};
wenzelm@36740
  1069
wenzelm@62897
  1070
structure Axclass_Proofs = Theory_Data
wenzelm@62897
  1071
(
wenzelm@62897
  1072
  type T = axclass_proofs option;
wenzelm@62897
  1073
  val empty = NONE;
wenzelm@62897
  1074
  val extend = I;
wenzelm@62897
  1075
  val merge = merge_options;
wenzelm@62897
  1076
);
wenzelm@36740
  1077
wenzelm@62897
  1078
fun the_axclass_proofs which thy x =
wenzelm@62897
  1079
  (case Axclass_Proofs.get thy of
wenzelm@36740
  1080
    NONE => raise Fail "Axclass proof operations not installed"
wenzelm@62897
  1081
  | SOME proofs => which proofs thy x);
wenzelm@36740
  1082
wenzelm@36740
  1083
in
wenzelm@36740
  1084
wenzelm@62897
  1085
val classrel_proof = the_axclass_proofs #classrel_proof;
wenzelm@62897
  1086
val arity_proof = the_axclass_proofs #arity_proof;
wenzelm@36740
  1087
wenzelm@62897
  1088
fun install_axclass_proofs proofs =
wenzelm@62897
  1089
  Axclass_Proofs.map
wenzelm@62897
  1090
    (fn NONE => SOME proofs
wenzelm@62897
  1091
      | SOME _ => raise Fail "Axclass proof operations already installed");
wenzelm@36740
  1092
wenzelm@36740
  1093
end;
wenzelm@36740
  1094
wenzelm@36740
  1095
wenzelm@36741
  1096
local
wenzelm@36741
  1097
wenzelm@36741
  1098
fun canonical_instance typs =
wenzelm@36741
  1099
  let
wenzelm@43329
  1100
    val names = Name.invent Name.context Name.aT (length typs);
wenzelm@36741
  1101
    val instT = map2 (fn a => fn T => (((a, 0), []), Type.strip_sorts T)) names typs;
wenzelm@36741
  1102
  in instantiate (instT, []) end;
wenzelm@36741
  1103
wenzelm@36741
  1104
in
wenzelm@36741
  1105
wenzelm@36741
  1106
fun of_sort_proof thy hyps =
wenzelm@36741
  1107
  Sorts.of_sort_derivation (Sign.classes_of thy)
haftmann@62538
  1108
   {class_relation = fn typ => fn _ => fn (prf, c1) => fn c2 =>
wenzelm@36741
  1109
      if c1 = c2 then prf
wenzelm@36741
  1110
      else canonical_instance [typ] (classrel_proof thy (c1, c2)) %% prf,
wenzelm@36741
  1111
    type_constructor = fn (a, typs) => fn dom => fn c =>
wenzelm@36741
  1112
      let val Ss = map (map snd) dom and prfs = maps (map fst) dom
wenzelm@36741
  1113
      in proof_combP (canonical_instance typs (arity_proof thy (a, Ss, c)), prfs) end,
wenzelm@36741
  1114
    type_variable = fn typ => map (fn c => (hyps (typ, c), c)) (Type.sort_of_atyp typ)};
wenzelm@36741
  1115
wenzelm@36741
  1116
end;
wenzelm@36741
  1117
wenzelm@36741
  1118
berghofe@11519
  1119
(***** axioms and theorems *****)
berghofe@11519
  1120
wenzelm@52487
  1121
val proofs = Unsynchronized.ref 2;
wenzelm@52487
  1122
fun proofs_enabled () = ! proofs >= 2;
wenzelm@52487
  1123
berghofe@28812
  1124
fun vars_of t = map Var (rev (Term.add_vars t []));
berghofe@28812
  1125
fun frees_of t = map Free (rev (Term.add_frees t []));
berghofe@11519
  1126
berghofe@11519
  1127
fun test_args _ [] = true
berghofe@11519
  1128
  | test_args is (Bound i :: ts) =
wenzelm@17492
  1129
      not (member (op =) is i) andalso test_args (i :: is) ts
berghofe@11519
  1130
  | test_args _ _ = false;
berghofe@11519
  1131
berghofe@11519
  1132
fun is_fun (Type ("fun", _)) = true
berghofe@11519
  1133
  | is_fun (TVar _) = true
berghofe@11519
  1134
  | is_fun _ = false;
berghofe@11519
  1135
berghofe@11519
  1136
fun add_funvars Ts (vs, t) =
berghofe@11519
  1137
  if is_fun (fastype_of1 (Ts, t)) then
haftmann@33042
  1138
    union (op =) vs (map_filter (fn Var (ixn, T) =>
haftmann@33037
  1139
      if is_fun T then SOME ixn else NONE | _ => NONE) (vars_of t))
berghofe@11519
  1140
  else vs;
berghofe@11519
  1141
wenzelm@56245
  1142
fun add_npvars q p Ts (vs, Const ("Pure.imp", _) $ t $ u) =
berghofe@11519
  1143
      add_npvars q p Ts (add_npvars q (not p) Ts (vs, t), u)
wenzelm@56245
  1144
  | add_npvars q p Ts (vs, Const ("Pure.all", Type (_, [Type (_, [T, _]), _])) $ t) =
berghofe@11519
  1145
      add_npvars q p Ts (vs, if p andalso q then betapply (t, Var (("",0), T)) else t)
berghofe@12041
  1146
  | add_npvars q p Ts (vs, Abs (_, T, t)) = add_npvars q p (T::Ts) (vs, t)
berghofe@12041
  1147
  | add_npvars _ _ Ts (vs, t) = add_npvars' Ts (vs, t)
wenzelm@64566
  1148
and add_npvars' Ts (vs, t) =
wenzelm@64566
  1149
  (case strip_comb t of
berghofe@11519
  1150
    (Var (ixn, _), ts) => if test_args [] ts then vs
haftmann@17314
  1151
      else Library.foldl (add_npvars' Ts)
haftmann@17314
  1152
        (AList.update (op =) (ixn,
haftmann@17314
  1153
          Library.foldl (add_funvars Ts) ((these ooo AList.lookup) (op =) vs ixn, ts)) vs, ts)
skalberg@15570
  1154
  | (Abs (_, T, u), ts) => Library.foldl (add_npvars' (T::Ts)) (vs, u :: ts)
skalberg@15570
  1155
  | (_, ts) => Library.foldl (add_npvars' Ts) (vs, ts));
berghofe@11519
  1156
wenzelm@56245
  1157
fun prop_vars (Const ("Pure.imp", _) $ P $ Q) = union (op =) (prop_vars P) (prop_vars Q)
wenzelm@56245
  1158
  | prop_vars (Const ("Pure.all", _) $ Abs (_, _, t)) = prop_vars t
wenzelm@64566
  1159
  | prop_vars t = (case strip_comb t of (Var (ixn, _), _) => [ixn] | _ => []);
berghofe@11519
  1160
berghofe@11519
  1161
fun is_proj t =
berghofe@11519
  1162
  let
wenzelm@64566
  1163
    fun is_p i t =
wenzelm@64566
  1164
      (case strip_comb t of
wenzelm@63857
  1165
        (Bound _, []) => false
berghofe@11519
  1166
      | (Bound j, ts) => j >= i orelse exists (is_p i) ts
berghofe@11519
  1167
      | (Abs (_, _, u), _) => is_p (i+1) u
berghofe@11519
  1168
      | (_, ts) => exists (is_p i) ts)
wenzelm@64566
  1169
  in
wenzelm@64566
  1170
    (case strip_abs_body t of
wenzelm@64566
  1171
      Bound _ => true
wenzelm@64566
  1172
    | t' => is_p 0 t')
berghofe@11519
  1173
  end;
berghofe@11519
  1174
wenzelm@21646
  1175
fun needed_vars prop =
haftmann@33042
  1176
  union (op =) (Library.foldl (uncurry (union (op =)))
haftmann@33042
  1177
    ([], map (uncurry (insert (op =))) (add_npvars true true [] ([], prop))))
haftmann@33042
  1178
  (prop_vars prop);
berghofe@11519
  1179
berghofe@11519
  1180
fun gen_axm_proof c name prop =
berghofe@11519
  1181
  let
berghofe@11519
  1182
    val nvs = needed_vars prop;
berghofe@11519
  1183
    val args = map (fn (v as Var (ixn, _)) =>
wenzelm@17492
  1184
        if member (op =) nvs ixn then SOME v else NONE) (vars_of prop) @
berghofe@28812
  1185
      map SOME (frees_of prop);
berghofe@11519
  1186
  in
skalberg@15531
  1187
    proof_combt' (c (name, prop, NONE), args)
berghofe@11519
  1188
  end;
berghofe@11519
  1189
berghofe@11519
  1190
val axm_proof = gen_axm_proof PAxm;
berghofe@17017
  1191
wenzelm@52487
  1192
fun oracle_proof name prop =
wenzelm@52487
  1193
  if ! proofs = 0 then ((name, Term.dummy), Oracle (name, Term.dummy, NONE))
wenzelm@30716
  1194
  else ((name, prop), gen_axm_proof Oracle name prop);
berghofe@11519
  1195
wenzelm@63623
  1196
val shrink_proof =
wenzelm@17492
  1197
  let
wenzelm@17492
  1198
    fun shrink ls lev (prf as Abst (a, T, body)) =
wenzelm@17492
  1199
          let val (b, is, ch, body') = shrink ls (lev+1) body
wenzelm@51100
  1200
          in (b, is, ch, if ch then Abst (a, T, body') else prf) end
wenzelm@17492
  1201
      | shrink ls lev (prf as AbsP (a, t, body)) =
wenzelm@17492
  1202
          let val (b, is, ch, body') = shrink (lev::ls) lev body
wenzelm@19482
  1203
          in (b orelse member (op =) is 0, map_filter (fn 0 => NONE | i => SOME (i-1)) is,
wenzelm@51100
  1204
            ch, if ch then AbsP (a, t, body') else prf)
wenzelm@17492
  1205
          end
wenzelm@17492
  1206
      | shrink ls lev prf =
wenzelm@17492
  1207
          let val (is, ch, _, prf') = shrink' ls lev [] [] prf
wenzelm@17492
  1208
          in (false, is, ch, prf') end
wenzelm@17492
  1209
    and shrink' ls lev ts prfs (prf as prf1 %% prf2) =
wenzelm@17492
  1210
          let
wenzelm@17492
  1211
            val p as (_, is', ch', prf') = shrink ls lev prf2;
wenzelm@17492
  1212
            val (is, ch, ts', prf'') = shrink' ls lev ts (p::prfs) prf1
haftmann@33042
  1213
          in (union (op =) is is', ch orelse ch', ts',
wenzelm@17492
  1214
              if ch orelse ch' then prf'' %% prf' else prf)
wenzelm@17492
  1215
          end
wenzelm@17492
  1216
      | shrink' ls lev ts prfs (prf as prf1 % t) =
wenzelm@17492
  1217
          let val (is, ch, (ch', t')::ts', prf') = shrink' ls lev (t::ts) prfs prf1
wenzelm@17492
  1218
          in (is, ch orelse ch', ts',
wenzelm@51100
  1219
              if ch orelse ch' then prf' % t' else prf) end
wenzelm@17492
  1220
      | shrink' ls lev ts prfs (prf as PBound i) =
wenzelm@30146
  1221
          (if exists (fn SOME (Bound j) => lev-j <= nth ls i | _ => true) ts
haftmann@18928
  1222
             orelse has_duplicates (op =)
haftmann@18928
  1223
               (Library.foldl (fn (js, SOME (Bound j)) => j :: js | (js, _) => js) ([], ts))
wenzelm@17492
  1224
             orelse exists #1 prfs then [i] else [], false, map (pair false) ts, prf)
wenzelm@63857
  1225
      | shrink' _ _ ts _ (Hyp t) = ([], false, map (pair false) ts, Hyp t)
wenzelm@63857
  1226
      | shrink' _ _ ts _ (prf as MinProof) = ([], false, map (pair false) ts, prf)
wenzelm@63857
  1227
      | shrink' _ _ ts _ (prf as OfClass _) = ([], false, map (pair false) ts, prf)
wenzelm@63857
  1228
      | shrink' _ _ ts prfs prf =
wenzelm@17492
  1229
          let
wenzelm@28803
  1230
            val prop =
wenzelm@28803
  1231
              (case prf of
wenzelm@28803
  1232
                PAxm (_, prop, _) => prop
wenzelm@28803
  1233
              | Oracle (_, prop, _) => prop
wenzelm@28803
  1234
              | Promise (_, prop, _) => prop
wenzelm@28803
  1235
              | PThm (_, ((_, prop, _), _)) => prop
wenzelm@36879
  1236
              | _ => raise Fail "shrink: proof not in normal form");
wenzelm@17492
  1237
            val vs = vars_of prop;
wenzelm@19012
  1238
            val (ts', ts'') = chop (length vs) ts;
haftmann@33957
  1239
            val insts = take (length ts') (map (fst o dest_Var) vs) ~~ ts';
wenzelm@17492
  1240
            val nvs = Library.foldl (fn (ixns', (ixn, ixns)) =>
wenzelm@64566
  1241
              insert (op =) ixn
wenzelm@64566
  1242
                (case AList.lookup (op =) insts ixn of
haftmann@33042
  1243
                  SOME (SOME t) => if is_proj t then union (op =) ixns ixns' else ixns'
haftmann@33042
  1244
                | _ => union (op =) ixns ixns'))
wenzelm@17492
  1245
                  (needed prop ts'' prfs, add_npvars false true [] ([], prop));
wenzelm@17492
  1246
            val insts' = map
wenzelm@17492
  1247
              (fn (ixn, x as SOME _) => if member (op =) nvs ixn then (false, x) else (true, NONE)
wenzelm@17492
  1248
                | (_, x) => (false, x)) insts
wenzelm@17492
  1249
          in ([], false, insts' @ map (pair false) ts'', prf) end
wenzelm@56245
  1250
    and needed (Const ("Pure.imp", _) $ t $ u) ts ((b, _, _, _)::prfs) =
haftmann@33042
  1251
          union (op =) (if b then map (fst o dest_Var) (vars_of t) else []) (needed u ts prfs)
wenzelm@17492
  1252
      | needed (Var (ixn, _)) (_::_) _ = [ixn]
wenzelm@17492
  1253
      | needed _ _ _ = [];
wenzelm@63623
  1254
  in fn prf => #4 (shrink [] 0 prf) end;
berghofe@11519
  1255
berghofe@11519
  1256
berghofe@11519
  1257
(**** Simple first order matching functions for terms and proofs ****)
berghofe@11519
  1258
berghofe@11519
  1259
exception PMatch;
berghofe@11519
  1260
berghofe@11519
  1261
(** see pattern.ML **)
berghofe@11519
  1262
wenzelm@33317
  1263
fun flt (i: int) = filter (fn n => n < i);
berghofe@12279
  1264
berghofe@37231
  1265
fun fomatch Ts tymatch j instsp p =
berghofe@11519
  1266
  let
berghofe@11519
  1267
    fun mtch (instsp as (tyinsts, insts)) = fn
berghofe@11519
  1268
        (Var (ixn, T), t)  =>
berghofe@12279
  1269
          if j>0 andalso not (null (flt j (loose_bnos t)))
berghofe@12279
  1270
          then raise PMatch
berghofe@12279
  1271
          else (tymatch (tyinsts, fn () => (T, fastype_of1 (Ts, t))),
berghofe@12279
  1272
            (ixn, t) :: insts)
berghofe@11519
  1273
      | (Free (a, T), Free (b, U)) =>
wenzelm@20147
  1274
          if a=b then (tymatch (tyinsts, K (T, U)), insts) else raise PMatch
berghofe@11519
  1275
      | (Const (a, T), Const (b, U))  =>
wenzelm@20147
  1276
          if a=b then (tymatch (tyinsts, K (T, U)), insts) else raise PMatch
berghofe@11519
  1277
      | (f $ t, g $ u) => mtch (mtch instsp (f, g)) (t, u)
berghofe@12279
  1278
      | (Bound i, Bound j) => if i=j then instsp else raise PMatch
berghofe@11519
  1279
      | _ => raise PMatch
wenzelm@59058
  1280
  in mtch instsp (apply2 Envir.beta_eta_contract p) end;
berghofe@11519
  1281
berghofe@12279
  1282
fun match_proof Ts tymatch =
berghofe@11519
  1283
  let
skalberg@15531
  1284
    fun optmatch _ inst (NONE, _) = inst
skalberg@15531
  1285
      | optmatch _ _ (SOME _, NONE) = raise PMatch
skalberg@15531
  1286
      | optmatch mtch inst (SOME x, SOME y) = mtch inst (x, y)
berghofe@12279
  1287
berghofe@12279
  1288
    fun matcht Ts j (pinst, tinst) (t, u) =
berghofe@12279
  1289
      (pinst, fomatch Ts tymatch j tinst (t, Envir.beta_norm u));
berghofe@12279
  1290
    fun matchT (pinst, (tyinsts, insts)) p =
berghofe@12279
  1291
      (pinst, (tymatch (tyinsts, K p), insts));
skalberg@15570
  1292
    fun matchTs inst (Ts, Us) = Library.foldl (uncurry matchT) (inst, Ts ~~ Us);
berghofe@12279
  1293
berghofe@12279
  1294
    fun mtch Ts i j (pinst, tinst) (Hyp (Var (ixn, _)), prf) =
berghofe@12279
  1295
          if i = 0 andalso j = 0 then ((ixn, prf) :: pinst, tinst)
wenzelm@64566
  1296
          else
wenzelm@64566
  1297
            (case apfst (flt i) (apsnd (flt j) (prf_add_loose_bnos 0 0 prf ([], []))) of
berghofe@12279
  1298
              ([], []) => ((ixn, incr_pboundvars (~i) (~j) prf) :: pinst, tinst)
wenzelm@64566
  1299
            | ([], _) =>
wenzelm@64566
  1300
                if j = 0 then ((ixn, incr_pboundvars (~i) (~j) prf) :: pinst, tinst)
wenzelm@64566
  1301
                else raise PMatch
berghofe@12279
  1302
            | _ => raise PMatch)
berghofe@12279
  1303
      | mtch Ts i j inst (prf1 % opt1, prf2 % opt2) =
berghofe@12279
  1304
          optmatch (matcht Ts j) (mtch Ts i j inst (prf1, prf2)) (opt1, opt2)
berghofe@12279
  1305
      | mtch Ts i j inst (prf1 %% prf2, prf1' %% prf2') =
berghofe@12279
  1306
          mtch Ts i j (mtch Ts i j inst (prf1, prf1')) (prf2, prf2')
berghofe@12279
  1307
      | mtch Ts i j inst (Abst (_, opT, prf1), Abst (_, opU, prf2)) =
wenzelm@18485
  1308
          mtch (the_default dummyT opU :: Ts) i (j+1)
berghofe@12279
  1309
            (optmatch matchT inst (opT, opU)) (prf1, prf2)
berghofe@12279
  1310
      | mtch Ts i j inst (prf1, Abst (_, opU, prf2)) =
wenzelm@18485
  1311
          mtch (the_default dummyT opU :: Ts) i (j+1) inst
berghofe@12279
  1312
            (incr_pboundvars 0 1 prf1 %> Bound 0, prf2)
berghofe@12279
  1313
      | mtch Ts i j inst (AbsP (_, opt, prf1), AbsP (_, opu, prf2)) =
berghofe@12279
  1314
          mtch Ts (i+1) j (optmatch (matcht Ts j) inst (opt, opu)) (prf1, prf2)
berghofe@12279
  1315
      | mtch Ts i j inst (prf1, AbsP (_, _, prf2)) =
berghofe@12279
  1316
          mtch Ts (i+1) j inst (incr_pboundvars 1 0 prf1 %% PBound 0, prf2)
wenzelm@28803
  1317
      | mtch Ts i j inst (PAxm (s1, _, opTs), PAxm (s2, _, opUs)) =
wenzelm@28803
  1318
          if s1 = s2 then optmatch matchTs inst (opTs, opUs)
wenzelm@28803
  1319
          else raise PMatch
wenzelm@31943
  1320
      | mtch Ts i j inst (OfClass (T1, c1), OfClass (T2, c2)) =
wenzelm@31903
  1321
          if c1 = c2 then matchT inst (T1, T2)
wenzelm@31903
  1322
          else raise PMatch
wenzelm@28803
  1323
      | mtch Ts i j inst (PThm (_, ((name1, prop1, opTs), _)), PThm (_, ((name2, prop2, opUs), _))) =
wenzelm@28803
  1324
          if name1 = name2 andalso prop1 = prop2 then
berghofe@12279
  1325
            optmatch matchTs inst (opTs, opUs)
berghofe@11519
  1326
          else raise PMatch
berghofe@12279
  1327
      | mtch _ _ _ inst (PBound i, PBound j) = if i = j then inst else raise PMatch
berghofe@12279
  1328
      | mtch _ _ _ _ _ = raise PMatch
berghofe@12279
  1329
  in mtch Ts 0 0 end;
berghofe@11519
  1330
berghofe@11519
  1331
fun prf_subst (pinst, (tyinsts, insts)) =
berghofe@11519
  1332
  let
wenzelm@32049
  1333
    val substT = Envir.subst_type_same tyinsts;
wenzelm@32049
  1334
    val substTs = Same.map substT;
berghofe@11519
  1335
wenzelm@32049
  1336
    fun subst' lev (Var (xi, _)) =
wenzelm@32049
  1337
        (case AList.lookup (op =) insts xi of
wenzelm@32049
  1338
          NONE => raise Same.SAME
skalberg@15531
  1339
        | SOME u => incr_boundvars lev u)
wenzelm@32049
  1340
      | subst' _ (Const (s, T)) = Const (s, substT T)
wenzelm@32049
  1341
      | subst' _ (Free (s, T)) = Free (s, substT T)
wenzelm@32049
  1342
      | subst' lev (Abs (a, T, body)) =
wenzelm@32049
  1343
          (Abs (a, substT T, Same.commit (subst' (lev + 1)) body)
wenzelm@32049
  1344
            handle Same.SAME => Abs (a, T, subst' (lev + 1) body))
wenzelm@32049
  1345
      | subst' lev (f $ t) =
wenzelm@32049
  1346
          (subst' lev f $ Same.commit (subst' lev) t
wenzelm@32049
  1347
            handle Same.SAME => f $ subst' lev t)
wenzelm@32049
  1348
      | subst' _ _ = raise Same.SAME;
berghofe@11519
  1349
berghofe@11519
  1350
    fun subst plev tlev (AbsP (a, t, body)) =
wenzelm@32049
  1351
          (AbsP (a, Same.map_option (subst' tlev) t, Same.commit (subst (plev + 1) tlev) body)
wenzelm@32049
  1352
            handle Same.SAME => AbsP (a, t, subst (plev + 1) tlev body))
berghofe@11519
  1353
      | subst plev tlev (Abst (a, T, body)) =
wenzelm@32049
  1354
          (Abst (a, Same.map_option substT T, Same.commit (subst plev (tlev + 1)) body)
wenzelm@32049
  1355
            handle Same.SAME => Abst (a, T, subst plev (tlev + 1) body))
wenzelm@32049
  1356
      | subst plev tlev (prf %% prf') =
wenzelm@32049
  1357
          (subst plev tlev prf %% Same.commit (subst plev tlev) prf'
wenzelm@32049
  1358
            handle Same.SAME => prf %% subst plev tlev prf')
wenzelm@32049
  1359
      | subst plev tlev (prf % t) =
wenzelm@32049
  1360
          (subst plev tlev prf % Same.commit (Same.map_option (subst' tlev)) t
wenzelm@32049
  1361
            handle Same.SAME => prf % Same.map_option (subst' tlev) t)
wenzelm@32049
  1362
      | subst plev tlev (Hyp (Var (xi, _))) =
wenzelm@32049
  1363
          (case AList.lookup (op =) pinst xi of
wenzelm@32049
  1364
            NONE => raise Same.SAME
wenzelm@32049
  1365
          | SOME prf' => incr_pboundvars plev tlev prf')
wenzelm@32049
  1366
      | subst _ _ (PAxm (id, prop, Ts)) = PAxm (id, prop, Same.map_option substTs Ts)
wenzelm@31943
  1367
      | subst _ _ (OfClass (T, c)) = OfClass (substT T, c)
wenzelm@32049
  1368
      | subst _ _ (Oracle (id, prop, Ts)) = Oracle (id, prop, Same.map_option substTs Ts)
wenzelm@32049
  1369
      | subst _ _ (Promise (i, prop, Ts)) = Promise (i, prop, substTs Ts)
wenzelm@28803
  1370
      | subst _ _ (PThm (i, ((id, prop, Ts), body))) =
wenzelm@32049
  1371
          PThm (i, ((id, prop, Same.map_option substTs Ts), body))
wenzelm@32049
  1372
      | subst _ _ _ = raise Same.SAME;
wenzelm@32049
  1373
  in fn t => subst 0 0 t handle Same.SAME => t end;
berghofe@11519
  1374
wenzelm@21646
  1375
(*A fast unification filter: true unless the two terms cannot be unified.
berghofe@12871
  1376
  Terms must be NORMAL.  Treats all Vars as distinct. *)
berghofe@12871
  1377
fun could_unify prf1 prf2 =
berghofe@12871
  1378
  let
berghofe@12871
  1379
    fun matchrands (prf1 %% prf2) (prf1' %% prf2') =
berghofe@12871
  1380
          could_unify prf2 prf2' andalso matchrands prf1 prf1'
skalberg@15531
  1381
      | matchrands (prf % SOME t) (prf' % SOME t') =
berghofe@12871
  1382
          Term.could_unify (t, t') andalso matchrands prf prf'
berghofe@12871
  1383
      | matchrands (prf % _) (prf' % _) = matchrands prf prf'
berghofe@12871
  1384
      | matchrands _ _ = true
berghofe@12871
  1385
berghofe@12871
  1386
    fun head_of (prf %% _) = head_of prf
berghofe@12871
  1387
      | head_of (prf % _) = head_of prf
berghofe@12871
  1388
      | head_of prf = prf
berghofe@12871
  1389
berghofe@12871
  1390
  in case (head_of prf1, head_of prf2) of
berghofe@12871
  1391
        (_, Hyp (Var _)) => true
berghofe@12871
  1392
      | (Hyp (Var _), _) => true
wenzelm@28803
  1393
      | (PAxm (a, _, _), PAxm (b, _, _)) => a = b andalso matchrands prf1 prf2
wenzelm@31943
  1394
      | (OfClass (_, c), OfClass (_, d)) => c = d andalso matchrands prf1 prf2
wenzelm@28803
  1395
      | (PThm (_, ((a, propa, _), _)), PThm (_, ((b, propb, _), _))) =>
berghofe@12871
  1396
          a = b andalso propa = propb andalso matchrands prf1 prf2
wenzelm@28803
  1397
      | (PBound i, PBound j) => i = j andalso matchrands prf1 prf2
berghofe@12871
  1398
      | (AbsP _, _) =>  true   (*because of possible eta equality*)
berghofe@12871
  1399
      | (Abst _, _) =>  true
berghofe@12871
  1400
      | (_, AbsP _) =>  true
berghofe@12871
  1401
      | (_, Abst _) =>  true
berghofe@12871
  1402
      | _ => false
berghofe@12871
  1403
  end;
berghofe@12871
  1404
wenzelm@28329
  1405
berghofe@11519
  1406
(**** rewriting on proof terms ****)
berghofe@11519
  1407
wenzelm@33722
  1408
val no_skel = PBound 0;
wenzelm@33722
  1409
val normal_skel = Hyp (Var ((Name.uu, 0), propT));
berghofe@13102
  1410
berghofe@12279
  1411
fun rewrite_prf tymatch (rules, procs) prf =
berghofe@11519
  1412
  let
berghofe@37231
  1413
    fun rew _ _ (Abst (_, _, body) % SOME t) = SOME (prf_subst_bounds [t] body, no_skel)
berghofe@37231
  1414
      | rew _ _ (AbsP (_, _, body) %% prf) = SOME (prf_subst_pbounds [prf] body, no_skel)
berghofe@37231
  1415
      | rew Ts hs prf =
berghofe@37231
  1416
          (case get_first (fn r => r Ts hs prf) procs of
wenzelm@33722
  1417
            NONE => get_first (fn (prf1, prf2) => SOME (prf_subst
wenzelm@33722
  1418
              (match_proof Ts tymatch ([], (Vartab.empty, [])) (prf1, prf)) prf2, prf2)
wenzelm@33722
  1419
                 handle PMatch => NONE) (filter (could_unify prf o fst) rules)
wenzelm@33722
  1420
          | some => some);
berghofe@11519
  1421
berghofe@37231
  1422
    fun rew0 Ts hs (prf as AbsP (_, _, prf' %% PBound 0)) =
berghofe@37231
  1423
          if prf_loose_Pbvar1 prf' 0 then rew Ts hs prf
berghofe@11519
  1424
          else
berghofe@11519
  1425
            let val prf'' = incr_pboundvars (~1) 0 prf'
berghofe@37231
  1426
            in SOME (the_default (prf'', no_skel) (rew Ts hs prf'')) end
berghofe@37231
  1427
      | rew0 Ts hs (prf as Abst (_, _, prf' % SOME (Bound 0))) =
berghofe@37231
  1428
          if prf_loose_bvar1 prf' 0 then rew Ts hs prf
berghofe@11519
  1429
          else
berghofe@11519
  1430
            let val prf'' = incr_pboundvars 0 (~1) prf'
berghofe@37231
  1431
            in SOME (the_default (prf'', no_skel) (rew Ts hs prf'')) end
berghofe@37231
  1432
      | rew0 Ts hs prf = rew Ts hs prf;
berghofe@11519
  1433
berghofe@37231
  1434
    fun rew1 _ _ (Hyp (Var _)) _ = NONE
wenzelm@64566
  1435
      | rew1 Ts hs skel prf =
wenzelm@64566
  1436
          (case rew2 Ts hs skel prf of
wenzelm@64566
  1437
            SOME prf1 =>
wenzelm@64566
  1438
              (case rew0 Ts hs prf1 of
wenzelm@64566
  1439
                SOME (prf2, skel') => SOME (the_default prf2 (rew1 Ts hs skel' prf2))
wenzelm@64566
  1440
              | NONE => SOME prf1)
wenzelm@64566
  1441
          | NONE =>
wenzelm@64566
  1442
              (case rew0 Ts hs prf of
wenzelm@64566
  1443
                SOME (prf1, skel') => SOME (the_default prf1 (rew1 Ts hs skel' prf1))
wenzelm@64566
  1444
              | NONE => NONE))
berghofe@11519
  1445
wenzelm@64566
  1446
    and rew2 Ts hs skel (prf % SOME t) =
wenzelm@64566
  1447
          (case prf of
berghofe@11519
  1448
            Abst (_, _, body) =>
berghofe@11519
  1449
              let val prf' = prf_subst_bounds [t] body
berghofe@37231
  1450
              in SOME (the_default prf' (rew2 Ts hs no_skel prf')) end
wenzelm@64566
  1451
          | _ =>
wenzelm@64566
  1452
              (case rew1 Ts hs (case skel of skel' % _ => skel' | _ => no_skel) prf of
wenzelm@64566
  1453
                SOME prf' => SOME (prf' % SOME t)
wenzelm@64566
  1454
              | NONE => NONE))
berghofe@37231
  1455
      | rew2 Ts hs skel (prf % NONE) = Option.map (fn prf' => prf' % NONE)
berghofe@37231
  1456
          (rew1 Ts hs (case skel of skel' % _ => skel' | _ => no_skel) prf)
wenzelm@64566
  1457
      | rew2 Ts hs skel (prf1 %% prf2) =
wenzelm@64566
  1458
          (case prf1 of
berghofe@11519
  1459
            AbsP (_, _, body) =>
berghofe@11519
  1460
              let val prf' = prf_subst_pbounds [prf2] body
berghofe@37231
  1461
              in SOME (the_default prf' (rew2 Ts hs no_skel prf')) end
berghofe@13102
  1462
          | _ =>
wenzelm@64566
  1463
            let
wenzelm@64566
  1464
              val (skel1, skel2) =
wenzelm@64566
  1465
                (case skel of
wenzelm@64566
  1466
                  skel1 %% skel2 => (skel1, skel2)
wenzelm@64566
  1467
                | _ => (no_skel, no_skel))
wenzelm@64566
  1468
            in
wenzelm@64566
  1469
              (case rew1 Ts hs skel1 prf1 of
wenzelm@64566
  1470
                SOME prf1' =>
wenzelm@64566
  1471
                  (case rew1 Ts hs skel2 prf2 of
skalberg@15531
  1472
                    SOME prf2' => SOME (prf1' %% prf2')
skalberg@15531
  1473
                  | NONE => SOME (prf1' %% prf2))
wenzelm@64566
  1474
              | NONE =>
wenzelm@64566
  1475
                  (case rew1 Ts hs skel2 prf2 of
skalberg@15531
  1476
                    SOME prf2' => SOME (prf1 %% prf2')
wenzelm@64566
  1477
                  | NONE => NONE))
berghofe@13102
  1478
            end)
wenzelm@64566
  1479
      | rew2 Ts hs skel (Abst (s, T, prf)) =
wenzelm@64566
  1480
          (case rew1 (the_default dummyT T :: Ts) hs
wenzelm@33722
  1481
              (case skel of Abst (_, _, skel') => skel' | _ => no_skel) prf of
skalberg@15531
  1482
            SOME prf' => SOME (Abst (s, T, prf'))
skalberg@15531
  1483
          | NONE => NONE)
wenzelm@64566
  1484
      | rew2 Ts hs skel (AbsP (s, t, prf)) =
wenzelm@64566
  1485
          (case rew1 Ts (t :: hs) (case skel of AbsP (_, _, skel') => skel' | _ => no_skel) prf of
skalberg@15531
  1486
            SOME prf' => SOME (AbsP (s, t, prf'))
skalberg@15531
  1487
          | NONE => NONE)
berghofe@37231
  1488
      | rew2 _ _ _ _ = NONE;
berghofe@11519
  1489
berghofe@37231
  1490
  in the_default prf (rew1 [] [] no_skel prf) end;
berghofe@11519
  1491
wenzelm@17203
  1492
fun rewrite_proof thy = rewrite_prf (fn (tyenv, f) =>
wenzelm@17203
  1493
  Sign.typ_match thy (f ()) tyenv handle Type.TYPE_MATCH => raise PMatch);
berghofe@11519
  1494
berghofe@11715
  1495
fun rewrite_proof_notypes rews = rewrite_prf fst rews;
berghofe@11615
  1496
wenzelm@16940
  1497
berghofe@11519
  1498
(**** theory data ****)
berghofe@11519
  1499
wenzelm@37216
  1500
structure Data = Theory_Data
wenzelm@22846
  1501
(
wenzelm@33722
  1502
  type T =
wenzelm@33722
  1503
    (stamp * (proof * proof)) list *
berghofe@37231
  1504
    (stamp * (typ list -> term option list -> proof -> (proof * proof) option)) list;
berghofe@11519
  1505
berghofe@12233
  1506
  val empty = ([], []);
wenzelm@16458
  1507
  val extend = I;
wenzelm@33522
  1508
  fun merge ((rules1, procs1), (rules2, procs2)) : T =
wenzelm@28803
  1509
    (AList.merge (op =) (K true) (rules1, rules2),
haftmann@22662
  1510
      AList.merge (op =) (K true) (procs1, procs2));
wenzelm@22846
  1511
);
berghofe@11519
  1512
wenzelm@37216
  1513
fun get_data thy = let val (rules, procs) = Data.get thy in (map #2 rules, map #2 procs) end;
wenzelm@28803
  1514
fun rew_proof thy = rewrite_prf fst (get_data thy);
berghofe@23780
  1515
wenzelm@37216
  1516
fun add_prf_rrule r = (Data.map o apfst) (cons (stamp (), r));
wenzelm@37216
  1517
fun add_prf_rproc p = (Data.map o apsnd) (cons (stamp (), p));
wenzelm@28803
  1518
wenzelm@28803
  1519
wenzelm@28828
  1520
(***** promises *****)
berghofe@11519
  1521
wenzelm@28828
  1522
fun promise_proof thy i prop =
wenzelm@28828
  1523
  let
wenzelm@28828
  1524
    val _ = prop |> Term.exists_subterm (fn t =>
wenzelm@28828
  1525
      (Term.is_Free t orelse Term.is_Var t) andalso
wenzelm@36879
  1526
        raise Fail ("promise_proof: illegal variable " ^ Syntax.string_of_term_global thy t));
wenzelm@28828
  1527
    val _ = prop |> Term.exists_type (Term.exists_subtype
wenzelm@36879
  1528
      (fn TFree (a, _) => raise Fail ("promise_proof: illegal type variable " ^ quote a)
wenzelm@28828
  1529
        | _ => false));
wenzelm@28828
  1530
  in Promise (i, prop, map TVar (Term.add_tvars prop [])) end;
wenzelm@28828
  1531
wenzelm@33722
  1532
fun fulfill_norm_proof thy ps body0 =
wenzelm@33722
  1533
  let
wenzelm@66168
  1534
    val _ = consolidate (map #2 ps @ [body0]);
wenzelm@33722
  1535
    val PBody {oracles = oracles0, thms = thms0, proof = proof0} = body0;
wenzelm@44334
  1536
    val oracles =
wenzelm@44334
  1537
      unions_oracles
wenzelm@44334
  1538
        (fold (fn (_, PBody {oracles, ...}) => not (null oracles) ? cons oracles) ps [oracles0]);
wenzelm@44334
  1539
    val thms =
wenzelm@44334
  1540
      unions_thms (fold (fn (_, PBody {thms, ...}) => not (null thms) ? cons thms) ps [thms0]);
wenzelm@33722
  1541
    val proofs = fold (fn (i, PBody {proof, ...}) => Inttab.update (i, proof)) ps Inttab.empty;
wenzelm@28875
  1542
wenzelm@33722
  1543
    fun fill (Promise (i, prop, Ts)) =
wenzelm@33722
  1544
          (case Inttab.lookup proofs i of
wenzelm@33722
  1545
            NONE => NONE
wenzelm@33722
  1546
          | SOME prf => SOME (instantiate (Term.add_tvars prop [] ~~ Ts, []) prf, normal_skel))
wenzelm@33722
  1547
      | fill _ = NONE;
wenzelm@33722
  1548
    val (rules, procs) = get_data thy;
berghofe@37231
  1549
    val proof = rewrite_prf fst (rules, K (K fill) :: procs) proof0;
wenzelm@33722
  1550
  in PBody {oracles = oracles, thms = thms, proof = proof} end;
wenzelm@28828
  1551
wenzelm@51047
  1552
fun fulfill_proof_future thy promises postproc body =
wenzelm@51047
  1553
  let
wenzelm@51047
  1554
    fun fulfill () =
wenzelm@51047
  1555
      postproc (fulfill_norm_proof thy (map (apsnd Future.join) promises) (Future.join body));
wenzelm@51047
  1556
  in
wenzelm@51047
  1557
    if null promises then Future.map postproc body
wenzelm@51047
  1558
    else if Future.is_finished body andalso length promises = 1 then
wenzelm@51047
  1559
      Future.map (fn _ => fulfill ()) (snd (hd promises))
wenzelm@51047
  1560
    else
wenzelm@51047
  1561
      (singleton o Future.forks)
wenzelm@51047
  1562
        {name = "Proofterm.fulfill_proof_future", group = NONE,
wenzelm@51047
  1563
          deps = Future.task_of body :: map (Future.task_of o snd) promises, pri = 0,
wenzelm@51047
  1564
          interrupts = true}
wenzelm@51047
  1565
        fulfill
wenzelm@51047
  1566
  end;
wenzelm@29642
  1567
wenzelm@28828
  1568
wenzelm@36878
  1569
(***** abstraction over sort constraints *****)
wenzelm@36878
  1570
wenzelm@36882
  1571
fun unconstrainT_prf thy (atyp_map, constraints) =
wenzelm@36878
  1572
  let
wenzelm@36878
  1573
    fun hyp_map hyp =
wenzelm@36878
  1574
      (case AList.lookup (op =) constraints hyp of
wenzelm@36878
  1575
        SOME t => Hyp t
wenzelm@36882
  1576
      | NONE => raise Fail "unconstrainT_prf: missing constraint");
wenzelm@36878
  1577
wenzelm@36882
  1578
    val typ = Term_Subst.map_atypsT_same (Type.strip_sorts o atyp_map);
wenzelm@36878
  1579
    fun ofclass (ty, c) =
wenzelm@36878
  1580
      let val ty' = Term.map_atyps atyp_map ty;
wenzelm@36878
  1581
      in the_single (of_sort_proof thy hyp_map (ty', [c])) end;
wenzelm@36878
  1582
  in
wenzelm@36878
  1583
    Same.commit (map_proof_same (Term_Subst.map_types_same typ) typ ofclass)
wenzelm@36878
  1584
    #> fold_rev (implies_intr_proof o snd) constraints
wenzelm@36878
  1585
  end;
wenzelm@36878
  1586
wenzelm@36882
  1587
fun unconstrainT_body thy constrs (PBody {oracles, thms, proof}) =
wenzelm@36878
  1588
  PBody
wenzelm@36883
  1589
   {oracles = oracles,  (* FIXME merge (!), unconstrain (!?!) *)
wenzelm@36883
  1590
    thms = thms,  (* FIXME merge (!) *)
wenzelm@36882
  1591
    proof = unconstrainT_prf thy constrs proof};
wenzelm@36878
  1592
wenzelm@36878
  1593
wenzelm@28828
  1594
(***** theorems *****)
berghofe@11519
  1595
wenzelm@37297
  1596
fun prepare_thm_proof thy name shyps hyps concl promises body =
berghofe@11519
  1597
  let
wenzelm@28803
  1598
    val PBody {oracles = oracles0, thms = thms0, proof = prf} = body;
wenzelm@32810
  1599
    val prop = Logic.list_implies (hyps, concl);
berghofe@11519
  1600
    val nvs = needed_vars prop;
berghofe@11519
  1601
    val args = map (fn (v as Var (ixn, _)) =>
wenzelm@17492
  1602
        if member (op =) nvs ixn then SOME v else NONE) (vars_of prop) @
berghofe@28812
  1603
      map SOME (frees_of prop);
wenzelm@28803
  1604
wenzelm@37297
  1605
    val ((atyp_map, constraints, outer_constraints), prop1) = Logic.unconstrainT shyps prop;
wenzelm@37297
  1606
    val postproc = unconstrainT_body thy (atyp_map, constraints);
wenzelm@37297
  1607
    val args1 =
wenzelm@37297
  1608
      (map o Option.map o Term.map_types o Term.map_atyps)
wenzelm@37297
  1609
        (Type.strip_sorts o atyp_map) args;
wenzelm@37297
  1610
    val argsP = map OfClass outer_constraints @ map Hyp hyps;
wenzelm@36882
  1611
wenzelm@37251
  1612
    fun make_body0 proof0 = PBody {oracles = oracles0, thms = thms0, proof = proof0};
wenzelm@37251
  1613
    val body0 =
wenzelm@52487
  1614
      if not (proofs_enabled ()) then Future.value (make_body0 MinProof)
wenzelm@41673
  1615
      else
wenzelm@44331
  1616
        (singleton o Future.cond_forks)
wenzelm@44302
  1617
          {name = "Proofterm.prepare_thm_proof", group = NONE,
wenzelm@44332
  1618
            deps = [], pri = 0, interrupts = true}
wenzelm@52696
  1619
          (fn () =>
wenzelm@63623
  1620
            make_body0 (shrink_proof (rew_proof thy (fold_rev implies_intr_proof hyps prf))));
wenzelm@28803
  1621
wenzelm@44333
  1622
    fun new_prf () = (serial (), fulfill_proof_future thy promises postproc body0);
wenzelm@36880
  1623
    val (i, body') =
wenzelm@41699
  1624
      (*non-deterministic, depends on unknown promises*)
wenzelm@28803
  1625
      (case strip_combt (fst (strip_combP prf)) of
wenzelm@28803
  1626
        (PThm (i, ((old_name, prop', NONE), body')), args') =>
wenzelm@36882
  1627
          if (old_name = "" orelse old_name = name) andalso prop1 = prop' andalso args = args'
wenzelm@36880
  1628
          then (i, body')
wenzelm@28803
  1629
          else new_prf ()
wenzelm@28815
  1630
      | _ => new_prf ());
wenzelm@36882
  1631
    val head = PThm (i, ((name, prop1, NONE), body'));
wenzelm@64574
  1632
  in ((i, make_thm_node name prop1 body'), head, args, argsP, args1) end;
wenzelm@36883
  1633
wenzelm@36883
  1634
fun thm_proof thy name shyps hyps concl promises body =
wenzelm@37297
  1635
  let val (pthm, head, args, argsP, _) = prepare_thm_proof thy name shyps hyps concl promises body
wenzelm@36883
  1636
  in (pthm, proof_combP (proof_combt' (head, args), argsP)) end;
wenzelm@36883
  1637
wenzelm@36883
  1638
fun unconstrain_thm_proof thy shyps concl promises body =
wenzelm@64566
  1639
  let val (pthm, head, _, _, args) = prepare_thm_proof thy "" shyps [] concl promises body
wenzelm@36883
  1640
  in (pthm, proof_combt' (head, args)) end;
wenzelm@36883
  1641
berghofe@11519
  1642
wenzelm@37297
  1643
fun get_name shyps hyps prop prf =
wenzelm@36877
  1644
  let val (_, prop) = Logic.unconstrainT shyps (Logic.list_implies (hyps, prop)) in
wenzelm@36877
  1645
    (case strip_combt (fst (strip_combP prf)) of
wenzelm@36877
  1646
      (PThm (_, ((name, prop', _), _)), _) => if prop = prop' then name else ""
wenzelm@36877
  1647
    | _ => "")
wenzelm@36877
  1648
  end;
wenzelm@36877
  1649
wenzelm@36877
  1650
fun guess_name (PThm (_, ((name, _, _), _))) = name
wenzelm@36877
  1651
  | guess_name (prf %% Hyp _) = guess_name prf
wenzelm@36877
  1652
  | guess_name (prf %% OfClass _) = guess_name prf
wenzelm@36877
  1653
  | guess_name (prf % NONE) = guess_name prf
wenzelm@36877
  1654
  | guess_name (prf % SOME (Var _)) = guess_name prf
wenzelm@36877
  1655
  | guess_name _ = "";
wenzelm@36877
  1656
berghofe@11519
  1657
end;
berghofe@11519
  1658
wenzelm@32094
  1659
structure Basic_Proofterm : BASIC_PROOFTERM = Proofterm;
wenzelm@32094
  1660
open Basic_Proofterm;