src/Pure/Proof/proof_rewrite_rules.ML
author wenzelm
Sat, 04 Sep 2021 14:18:44 +0200
changeset 74228 c22e5bdb207d
parent 71843 07c85c68ff03
child 74232 1091880266e5
permissions -rw-r--r--
more scalable operations; tuned;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     1
(*  Title:      Pure/Proof/proof_rewrite_rules.ML
11539
0f17da240450 tuned headers;
wenzelm
parents: 11522
diff changeset
     2
    Author:     Stefan Berghofer, TU Muenchen
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     3
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
     4
Simplification functions for proof terms involving meta level rules.
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     5
*)
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     6
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     7
signature PROOF_REWRITE_RULES =
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
     8
sig
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
     9
  val rew : bool -> typ list -> term option list -> Proofterm.proof -> (Proofterm.proof * Proofterm.proof) option
33722
e588744f14da generalized procs for rewrite_proof: allow skeleton;
wenzelm
parents: 29271
diff changeset
    10
  val rprocs : bool ->
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
    11
    (typ list -> term option list -> Proofterm.proof -> (Proofterm.proof * Proofterm.proof) option) list
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
    12
  val rewrite_terms : (term -> term) -> Proofterm.proof -> Proofterm.proof
70449
6e34025981be clarified global theory context;
wenzelm
parents: 70447
diff changeset
    13
  val elim_defs : theory -> bool -> thm list -> Proofterm.proof -> Proofterm.proof
13608
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
    14
  val elim_vars : (typ -> term) -> Proofterm.proof -> Proofterm.proof
22280
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
    15
  val hhf_proof : term -> term -> Proofterm.proof -> Proofterm.proof
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
    16
  val un_hhf_proof : term -> term -> Proofterm.proof -> Proofterm.proof
70836
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
    17
  val expand_of_sort_proof : theory -> term option list -> typ * sort -> Proofterm.proof list
70837
874092c031c3 tuned signature;
wenzelm
parents: 70836
diff changeset
    18
  val expand_of_class_proof : theory -> term option list -> typ * class -> proof
70449
6e34025981be clarified global theory context;
wenzelm
parents: 70447
diff changeset
    19
  val expand_of_class : theory -> typ list -> term option list -> Proofterm.proof ->
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
    20
    (Proofterm.proof * Proofterm.proof) option
70879
0b320e92485c tuned signature;
wenzelm
parents: 70840
diff changeset
    21
  val standard_preproc: (proof * proof) list -> theory -> proof -> proof
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    22
end;
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    23
70840
5b80eb4fd0f3 proper ML names;
wenzelm
parents: 70837
diff changeset
    24
structure Proof_Rewrite_Rules : PROOF_REWRITE_RULES =
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    25
struct
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    26
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
    27
fun rew b _ _ =
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    28
  let
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
    29
    fun ?? x = if b then SOME x else NONE;
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    30
    fun ax (prf as PAxm (s, prop, _)) Ts =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    31
      if b then PAxm (s, prop, SOME Ts) else prf;
71087
77580c977e0c more robust;
wenzelm
parents: 70915
diff changeset
    32
    fun ty T =
77580c977e0c more robust;
wenzelm
parents: 70915
diff changeset
    33
      if b then
77580c977e0c more robust;
wenzelm
parents: 70915
diff changeset
    34
        (case T of
77580c977e0c more robust;
wenzelm
parents: 70915
diff changeset
    35
          Type (_, [Type (_, [U, _]), _]) => SOME U
77580c977e0c more robust;
wenzelm
parents: 70915
diff changeset
    36
        | _ => NONE)
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    37
      else NONE;
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    38
    val equal_intr_axm = ax Proofterm.equal_intr_axm [];
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    39
    val equal_elim_axm = ax Proofterm.equal_elim_axm [];
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    40
    val symmetric_axm = ax Proofterm.symmetric_axm [propT];
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    41
70493
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
    42
    fun rew' (PThm ({name = "Pure.protectD", ...}, _) % _ %%
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
    43
        (PThm ({name = "Pure.protectI", ...}, _) % _ %% prf)) = SOME prf
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
    44
      | rew' (PThm ({name = "Pure.conjunctionD1", ...}, _) % _ % _ %%
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
    45
        (PThm ({name = "Pure.conjunctionI", ...}, _) % _ % _ %% prf %% _)) = SOME prf
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
    46
      | rew' (PThm ({name = "Pure.conjunctionD2", ...}, _) % _ % _ %%
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
    47
        (PThm ({name = "Pure.conjunctionI", ...}, _) % _ % _ %% _ %% prf)) = SOME prf
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    48
      | rew' (PAxm ("Pure.equal_elim", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    49
        (PAxm ("Pure.equal_intr", _, _) % _ % _ %% prf %% _)) = SOME prf
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    50
      | rew' (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    51
        (PAxm ("Pure.equal_intr", _, _) % A % B %% prf1 %% prf2)) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    52
            SOME (equal_intr_axm % B % A %% prf2 %% prf1)
12002
bc9b5bad0e7b Additional rules for simplifying inside "Goal"
berghofe
parents: 11612
diff changeset
    53
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    54
      | rew' (PAxm ("Pure.equal_elim", _, _) % SOME (_ $ A) % SOME (_ $ B) %%
56244
3298b7a2795a more qualified names;
wenzelm
parents: 53171
diff changeset
    55
        (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.prop", _)) %
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    56
          _ % _ % _ %% (PAxm ("Pure.reflexive", _, _) % _) %% prf1) %%
70493
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
    57
        ((tg as PThm ({name = "Pure.protectI", ...}, _)) % _ %% prf2)) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    58
        SOME (tg %> B %% (equal_elim_axm %> A %> B %% prf1 %% prf2))
12002
bc9b5bad0e7b Additional rules for simplifying inside "Goal"
berghofe
parents: 11612
diff changeset
    59
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    60
      | rew' (PAxm ("Pure.equal_elim", _, _) % SOME (_ $ A) % SOME (_ $ B) %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    61
        (PAxm ("Pure.symmetric", _, _) % _ % _ %%
56244
3298b7a2795a more qualified names;
wenzelm
parents: 53171
diff changeset
    62
          (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.prop", _)) %
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    63
             _ % _ % _ %% (PAxm ("Pure.reflexive", _, _) % _) %% prf1)) %%
70493
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
    64
        ((tg as PThm ({name = "Pure.protectI", ...}, _)) % _ %% prf2)) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
    65
        SOME (tg %> B %% (equal_elim_axm %> A %> B %%
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
    66
          (symmetric_axm % ?? B % ?? A %% prf1) %% prf2))
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    67
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    68
      | rew' (PAxm ("Pure.equal_elim", _, _) % SOME X % SOME Y %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    69
        (PAxm ("Pure.combination", _, _) % _ % _ % _ % _ %%
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
    70
          (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.imp", _)) % _ % _ % _ %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    71
             (PAxm ("Pure.reflexive", _, _) % _) %% prf1) %% prf2)) =
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    72
        let
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    73
          val _ $ A $ C = Envir.beta_norm X;
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    74
          val _ $ B $ D = Envir.beta_norm Y
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
    75
        in SOME (AbsP ("H1", ?? X, AbsP ("H2", ?? B,
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    76
          Proofterm.equal_elim_axm %> C %> D %% Proofterm.incr_pboundvars 2 0 prf2 %%
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    77
            (PBound 1 %% (equal_elim_axm %> B %> A %%
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    78
              (Proofterm.symmetric_axm % ?? A % ?? B %% Proofterm.incr_pboundvars 2 0 prf1) %%
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    79
                PBound 0)))))
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    80
        end
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    81
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    82
      | rew' (PAxm ("Pure.equal_elim", _, _) % SOME X % SOME Y %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    83
        (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    84
          (PAxm ("Pure.combination", _, _) % _ % _ % _ % _ %%
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
    85
            (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.imp", _)) % _ % _ % _ %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
    86
               (PAxm ("Pure.reflexive", _, _) % _) %% prf1) %% prf2))) =
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    87
        let
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    88
          val _ $ A $ C = Envir.beta_norm Y;
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    89
          val _ $ B $ D = Envir.beta_norm X
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
    90
        in SOME (AbsP ("H1", ?? X, AbsP ("H2", ?? A,
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    91
          equal_elim_axm %> D %> C %%
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    92
            (symmetric_axm % ?? C % ?? D %% Proofterm.incr_pboundvars 2 0 prf2) %%
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    93
              (PBound 1 %%
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
    94
                (equal_elim_axm %> A %> B %% Proofterm.incr_pboundvars 2 0 prf1 %% PBound 0)))))
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
    95
        end
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
    96
71843
07c85c68ff03 added missing preprocessing step for extraction (due to Stefan Berghofer)
Manuel Eberl <eberlm@in.tum.de>
parents: 71777
diff changeset
    97
      | rew' (PAxm ("Pure.combination", _, _) %
07c85c68ff03 added missing preprocessing step for extraction (due to Stefan Berghofer)
Manuel Eberl <eberlm@in.tum.de>
parents: 71777
diff changeset
    98
        SOME (imp as (imp' as Const ("Pure.imp", T)) $ X) % _ % Y % Z %%
07c85c68ff03 added missing preprocessing step for extraction (due to Stefan Berghofer)
Manuel Eberl <eberlm@in.tum.de>
parents: 71777
diff changeset
    99
          (PAxm ("Pure.reflexive", _, _) % _) %% prf) =
07c85c68ff03 added missing preprocessing step for extraction (due to Stefan Berghofer)
Manuel Eberl <eberlm@in.tum.de>
parents: 71777
diff changeset
   100
        SOME (ax Proofterm.combination_axm [propT, propT] %> imp % ?? imp % Y % Z %%
07c85c68ff03 added missing preprocessing step for extraction (due to Stefan Berghofer)
Manuel Eberl <eberlm@in.tum.de>
parents: 71777
diff changeset
   101
          (ax Proofterm.combination_axm [propT, propT --> propT] %> imp' % ?? imp' % ?? X % ?? X %%
07c85c68ff03 added missing preprocessing step for extraction (due to Stefan Berghofer)
Manuel Eberl <eberlm@in.tum.de>
parents: 71777
diff changeset
   102
             (ax Proofterm.reflexive_axm [T] % ?? imp') %%
07c85c68ff03 added missing preprocessing step for extraction (due to Stefan Berghofer)
Manuel Eberl <eberlm@in.tum.de>
parents: 71777
diff changeset
   103
             (ax Proofterm.reflexive_axm [propT] % ?? X)) %% prf)
07c85c68ff03 added missing preprocessing step for extraction (due to Stefan Berghofer)
Manuel Eberl <eberlm@in.tum.de>
parents: 71777
diff changeset
   104
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   105
      | rew' (PAxm ("Pure.equal_elim", _, _) % SOME X % SOME Y %%
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   106
        (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.all", _)) % _ % _ % _ %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   107
          (PAxm ("Pure.reflexive", _, _) % _) %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   108
            (PAxm ("Pure.abstract_rule", _, _) % _ % _ %% prf))) =
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   109
        let
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   110
          val Const (_, T) $ P = Envir.beta_norm X;
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   111
          val _ $ Q = Envir.beta_norm Y;
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   112
        in SOME (AbsP ("H", ?? X, Abst ("x", ty T,
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   113
            equal_elim_axm %> incr_boundvars 1 P $ Bound 0 %> incr_boundvars 1 Q $ Bound 0 %%
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   114
              (Proofterm.incr_pboundvars 1 1 prf %> Bound 0) %% (PBound 0 %> Bound 0))))
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   115
        end
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   116
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   117
      | rew' (PAxm ("Pure.equal_elim", _, _) % SOME X % SOME Y %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   118
        (PAxm ("Pure.symmetric", _, _) % _ % _ %%        
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   119
          (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.all", _)) % _ % _ % _ %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   120
            (PAxm ("Pure.reflexive", _, _) % _) %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   121
              (PAxm ("Pure.abstract_rule", _, _) % _ % _ %% prf)))) =
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   122
        let
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   123
          val Const (_, T) $ P = Envir.beta_norm X;
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   124
          val _ $ Q = Envir.beta_norm Y;
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   125
          val t = incr_boundvars 1 P $ Bound 0;
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   126
          val u = incr_boundvars 1 Q $ Bound 0
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   127
        in SOME (AbsP ("H", ?? X, Abst ("x", ty T,
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   128
          equal_elim_axm %> t %> u %%
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   129
            (symmetric_axm % ?? u % ?? t %% (Proofterm.incr_pboundvars 1 1 prf %> Bound 0))
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   130
              %% (PBound 0 %> Bound 0))))
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   131
        end
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   132
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   133
      | rew' (PAxm ("Pure.equal_elim", _, _) % SOME A % SOME C %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   134
        (PAxm ("Pure.transitive", _, _) % _ % SOME B % _ %% prf1 %% prf2) %% prf3) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   135
           SOME (equal_elim_axm %> B %> C %% prf2 %%
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   136
             (equal_elim_axm %> A %> B %% prf1 %% prf3))
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   137
      | rew' (PAxm ("Pure.equal_elim", _, _) % SOME A % SOME C %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   138
        (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   139
          (PAxm ("Pure.transitive", _, _) % _ % SOME B % _ %% prf1 %% prf2)) %% prf3) =
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   140
           SOME (equal_elim_axm %> B %> C %% (symmetric_axm % ?? C % ?? B %% prf1) %%
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   141
             (equal_elim_axm %> A %> B %% (symmetric_axm % ?? B % ?? A %% prf2) %% prf3))
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   142
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   143
      | rew' (PAxm ("Pure.equal_elim", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   144
        (PAxm ("Pure.reflexive", _, _) % _) %% prf) = SOME prf
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   145
      | rew' (PAxm ("Pure.equal_elim", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   146
        (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   147
          (PAxm ("Pure.reflexive", _, _) % _)) %% prf) = SOME prf
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   148
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   149
      | rew' (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   150
        (PAxm ("Pure.symmetric", _, _) % _ % _ %% prf)) = SOME prf
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   151
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   152
      | rew' (PAxm ("Pure.equal_elim", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   153
        (PAxm ("Pure.equal_elim", _, _) % SOME (_ $ A $ C) % SOME (_ $ B $ D) %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   154
          (PAxm ("Pure.combination", _, _) % _ % _ % _ % _ %%
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   155
            (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.eq", _)) % _ % _ % _ %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   156
              (PAxm ("Pure.reflexive", _, _) % _) %% prf1) %% prf2) %% prf3) %% prf4) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   157
          SOME (equal_elim_axm %> C %> D %% prf2 %%
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   158
            (equal_elim_axm %> A %> C %% prf3 %%
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   159
              (equal_elim_axm %> B %> A %% (symmetric_axm % ?? A % ?? B %% prf1) %% prf4)))
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   160
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   161
      | rew' (PAxm ("Pure.equal_elim", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   162
        (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   163
          (PAxm ("Pure.equal_elim", _, _) % SOME (_ $ A $ C) % SOME (_ $ B $ D) %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   164
            (PAxm ("Pure.combination", _, _) % _ % _ % _ % _ %%
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   165
              (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.eq", _)) % _ % _ % _ %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   166
                (PAxm ("Pure.reflexive", _, _) % _) %% prf1) %% prf2) %% prf3)) %% prf4) =
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   167
          SOME (equal_elim_axm %> A %> B %% prf1 %%
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   168
            (equal_elim_axm %> C %> A %% (symmetric_axm % ?? A % ?? C %% prf3) %%
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   169
              (equal_elim_axm %> D %> C %% (symmetric_axm % ?? C % ?? D %% prf2) %% prf4)))
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   170
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   171
      | rew' (PAxm ("Pure.equal_elim", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   172
        (PAxm ("Pure.equal_elim", _, _) % SOME (_ $ B $ D) % SOME (_ $ A $ C) %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   173
          (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   174
            (PAxm ("Pure.combination", _, _) % _ % _ % _ % _ %%
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   175
              (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.eq", _)) % _ % _ % _ %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   176
                (PAxm ("Pure.reflexive", _, _) % _) %% prf1) %% prf2)) %% prf3) %% prf4) =
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   177
          SOME (equal_elim_axm %> D %> C %% (symmetric_axm % ?? C % ?? D %% prf2) %%
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   178
            (equal_elim_axm %> B %> D %% prf3 %%
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   179
              (equal_elim_axm %> A %> B %% prf1 %% prf4)))
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   180
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   181
      | rew' (PAxm ("Pure.equal_elim", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   182
        (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   183
          (PAxm ("Pure.equal_elim", _, _) % SOME (_ $ B $ D) % SOME (_ $ A $ C) %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   184
            (PAxm ("Pure.symmetric", _, _) % _ % _ %%
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   185
              (PAxm ("Pure.combination", _, _) % _ % _ % _ % _ %%
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   186
                (PAxm ("Pure.combination", _, _) % SOME (Const ("Pure.eq", _)) % _ % _ % _ %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   187
                  (PAxm ("Pure.reflexive", _, _) % _) %% prf1) %% prf2)) %% prf3)) %% prf4) =
17137
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   188
          SOME (equal_elim_axm %> B %> A %% (symmetric_axm % ?? A % ?? B %% prf1) %%
0f48fbb60a61 replaced ? by ??
haftmann
parents: 17018
diff changeset
   189
            (equal_elim_axm %> D %> B %% (symmetric_axm % ?? B % ?? D %% prf3) %%
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   190
              (equal_elim_axm %> C %> D %% prf2 %% prf4)))
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   191
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   192
      | rew' ((prf as PAxm ("Pure.combination", _, _) %
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   193
        SOME ((eq as Const ("Pure.eq", T)) $ t) % _ % _ % _) %%
26424
a6cad32a27b0 eliminated theory ProtoPure;
wenzelm
parents: 23178
diff changeset
   194
          (PAxm ("Pure.reflexive", _, _) % _)) =
13257
1b7104a1c0bd Additional rule for rewriting on ==.
berghofe
parents: 13198
diff changeset
   195
        let val (U, V) = (case T of
1b7104a1c0bd Additional rule for rewriting on ==.
berghofe
parents: 13198
diff changeset
   196
          Type (_, [U, V]) => (U, V) | _ => (dummyT, dummyT))
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   197
        in SOME (prf %% (ax Proofterm.combination_axm [U, V] %> eq % ?? eq % ?? t % ?? t %%
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   198
          (ax Proofterm.reflexive_axm [T] % ?? eq) %% (ax Proofterm.reflexive_axm [U] % ?? t)))
13257
1b7104a1c0bd Additional rule for rewriting on ==.
berghofe
parents: 13198
diff changeset
   199
        end
1b7104a1c0bd Additional rule for rewriting on ==.
berghofe
parents: 13198
diff changeset
   200
19309
8ea43e9ad83a avoid polymorphic equality;
wenzelm
parents: 19126
diff changeset
   201
      | rew' _ = NONE;
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   202
  in rew' #> Option.map (rpair Proofterm.no_skel) end;
12866
c00df7765656 Rewrite procedure now works for both compact and full proof objects.
berghofe
parents: 12237
diff changeset
   203
28806
ba0ffe4cfc2b rewrite_proof: simplified simprocs (no name required);
wenzelm
parents: 26463
diff changeset
   204
fun rprocs b = [rew b];
53171
a5e54d4d9081 added Theory.setup convenience;
wenzelm
parents: 46219
diff changeset
   205
val _ = Theory.setup (fold Proofterm.add_prf_rproc (rprocs false));
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   206
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   207
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   208
(**** apply rewriting function to all terms in proof ****)
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   209
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   210
fun rewrite_terms r =
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   211
  let
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   212
    fun rew_term Ts t =
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   213
      let
29271
1d685baea08e moved old add_type_XXX, add_term_XXX etc. to structure OldTerm;
wenzelm
parents: 28806
diff changeset
   214
        val frees =
43329
84472e198515 tuned signature: Name.invent and Name.invent_names;
wenzelm
parents: 43322
diff changeset
   215
          map Free (Name.invent (Term.declare_term_frees t Name.context) "xa" (length Ts) ~~ Ts);
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   216
        val t' = r (subst_bounds (frees, t));
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   217
        fun strip [] t = t
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   218
          | strip (_ :: xs) (Abs (_, _, t)) = strip xs t;
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   219
      in
19473
wenzelm
parents: 19466
diff changeset
   220
        strip Ts (fold lambda frees t')
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   221
      end;
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   222
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   223
    fun rew Ts (prf1 %% prf2) = rew Ts prf1 %% rew Ts prf2
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   224
      | rew Ts (prf % SOME t) = rew Ts prf % SOME (rew_term Ts t)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   225
      | rew Ts (Abst (s, SOME T, prf)) = Abst (s, SOME T, rew (T :: Ts) prf)
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   226
      | rew Ts (AbsP (s, SOME t, prf)) = AbsP (s, SOME (rew_term Ts t), rew Ts prf)
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   227
      | rew _ prf = prf
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   228
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   229
  in rew [] end;
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   230
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   231
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   232
(**** eliminate definitions in proof ****)
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   233
16861
7446b4be013b tuned fold on terms;
wenzelm
parents: 16787
diff changeset
   234
fun vars_of t = rev (fold_aterms (fn v as Var _ => insert (op =) v | _ => I) t []);
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   235
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   236
fun insert_refl defs Ts (prf1 %% prf2) =
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   237
      let val (prf1', b) = insert_refl defs Ts prf1
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   238
      in
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   239
        if b then (prf1', true)
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   240
        else (prf1' %% fst (insert_refl defs Ts prf2), false)
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   241
      end
15531
08c8dad8e399 Deleted Library.option type.
skalberg
parents: 14981
diff changeset
   242
  | insert_refl defs Ts (Abst (s, SOME T, prf)) =
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   243
      (Abst (s, SOME T, fst (insert_refl defs (T :: Ts) prf)), false)
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   244
  | insert_refl defs Ts (AbsP (s, t, prf)) =
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   245
      (AbsP (s, t, fst (insert_refl defs Ts prf)), false)
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   246
  | insert_refl defs Ts prf =
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   247
      (case Proofterm.strip_combt prf of
70493
a9053fa30909 clarified ML types;
wenzelm
parents: 70458
diff changeset
   248
        (PThm ({name = s, prop, types = SOME Ts, ...}, _), ts) =>
20664
ffbc5a57191a member (op =);
wenzelm
parents: 20076
diff changeset
   249
          if member (op =) defs s then
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   250
            let
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   251
              val vs = vars_of prop;
74228
c22e5bdb207d more scalable operations;
wenzelm
parents: 71843
diff changeset
   252
              val tvars = rev (Term.add_tvars prop []);
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   253
              val (_, rhs) = Logic.dest_equals (Logic.strip_imp_concl prop);
18185
9d51fad6bb1f Term.betapplys;
wenzelm
parents: 18024
diff changeset
   254
              val rhs' = Term.betapplys (subst_TVars (map fst tvars ~~ Ts)
23178
07ba6b58b3d2 simplified/unified list fold;
wenzelm
parents: 22662
diff changeset
   255
                (fold_rev (fn x => fn b => Abs ("", dummyT, abstract_over (x, b))) vs rhs),
19466
wenzelm
parents: 19309
diff changeset
   256
                map the ts);
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   257
            in
70417
eb6ff14767cd tuned signature;
wenzelm
parents: 70412
diff changeset
   258
              (Proofterm.change_types (SOME [fastype_of1 (Ts, rhs')])
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   259
                Proofterm.reflexive_axm %> rhs', true)
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   260
            end
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   261
          else (prf, false)
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   262
      | (_, []) => (prf, false)
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   263
      | (prf', ts) => (Proofterm.proof_combt' (fst (insert_refl defs Ts prf'), ts), false));
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   264
70449
6e34025981be clarified global theory context;
wenzelm
parents: 70447
diff changeset
   265
fun elim_defs thy r defs prf =
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   266
  let
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   267
    val defs' = map (Logic.dest_equals o
59582
0fbed69ff081 tuned signature -- prefer qualified names;
wenzelm
parents: 56245
diff changeset
   268
      map_types Type.strip_sorts o Thm.prop_of o Drule.abs_def) defs;
36744
6e1f3d609a68 renamed Thm.get_name -> Thm.derivation_name and Thm.put_name -> Thm.name_derivation, to emphasize the true nature of these operations;
wenzelm
parents: 36042
diff changeset
   269
    val defnames = map Thm.derivation_name defs;
70826
wenzelm
parents: 70493
diff changeset
   270
    val prf' =
wenzelm
parents: 70493
diff changeset
   271
      if r then
wenzelm
parents: 70493
diff changeset
   272
        let
wenzelm
parents: 70493
diff changeset
   273
          val cnames = map (fst o dest_Const o fst) defs';
70915
bd4d37edfee4 clarified expand_proof/expand_name: allow more detailed control via thm_header;
wenzelm
parents: 70879
diff changeset
   274
          val expand = Proofterm.fold_proof_atoms true
bd4d37edfee4 clarified expand_proof/expand_name: allow more detailed control via thm_header;
wenzelm
parents: 70879
diff changeset
   275
            (fn PThm ({serial, name, prop, ...}, _) =>
70826
wenzelm
parents: 70493
diff changeset
   276
                if member (op =) defnames name orelse
wenzelm
parents: 70493
diff changeset
   277
                  not (exists_Const (member (op =) cnames o #1) prop)
wenzelm
parents: 70493
diff changeset
   278
                then I
70915
bd4d37edfee4 clarified expand_proof/expand_name: allow more detailed control via thm_header;
wenzelm
parents: 70879
diff changeset
   279
                else Inttab.update (serial, "")
bd4d37edfee4 clarified expand_proof/expand_name: allow more detailed control via thm_header;
wenzelm
parents: 70879
diff changeset
   280
              | _ => I) [prf] Inttab.empty;
bd4d37edfee4 clarified expand_proof/expand_name: allow more detailed control via thm_header;
wenzelm
parents: 70879
diff changeset
   281
        in Proofterm.expand_proof thy (Inttab.lookup expand o #serial) prf end
70826
wenzelm
parents: 70493
diff changeset
   282
      else prf;
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   283
  in
70449
6e34025981be clarified global theory context;
wenzelm
parents: 70447
diff changeset
   284
    rewrite_terms (Pattern.rewrite_term thy defs' [])
70826
wenzelm
parents: 70493
diff changeset
   285
      (fst (insert_refl defnames [] prf'))
12906
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   286
  end;
165f4e1937f4 New function for eliminating definitions in proof term.
berghofe
parents: 12866
diff changeset
   287
13608
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
   288
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
   289
(**** eliminate all variables that don't occur in the proposition ****)
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
   290
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
   291
fun elim_vars mk_default prf =
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
   292
  let
70447
755d58b48cec clarified modules: provide reconstruct_proof / expand_proof at the bottom of proof term construction;
wenzelm
parents: 70417
diff changeset
   293
    val prop = Proofterm.prop_of prf;
74228
c22e5bdb207d more scalable operations;
wenzelm
parents: 71843
diff changeset
   294
    val tv = Term_Subst.add_vars prop Term_Subst.Vars.empty;
c22e5bdb207d more scalable operations;
wenzelm
parents: 71843
diff changeset
   295
    val tf = Term_Subst.add_frees prop Term_Subst.Frees.empty;
19309
8ea43e9ad83a avoid polymorphic equality;
wenzelm
parents: 19126
diff changeset
   296
74228
c22e5bdb207d more scalable operations;
wenzelm
parents: 71843
diff changeset
   297
    fun hidden_variable (Var v) = not (Term_Subst.Vars.defined tv v)
c22e5bdb207d more scalable operations;
wenzelm
parents: 71843
diff changeset
   298
      | hidden_variable (Free f) = not (Term_Subst.Frees.defined tf f)
19309
8ea43e9ad83a avoid polymorphic equality;
wenzelm
parents: 19126
diff changeset
   299
      | hidden_variable _ = false;
13917
a67c9e6570ac elim_vars now handles both Vars and Frees.
berghofe
parents: 13646
diff changeset
   300
46219
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 43329
diff changeset
   301
    fun mk_default' T =
426ed18eba43 discontinued old-style Term.list_abs in favour of plain Term.abs;
wenzelm
parents: 43329
diff changeset
   302
      fold_rev (Term.abs o pair "x") (binder_types T) (mk_default (body_type T));
13917
a67c9e6570ac elim_vars now handles both Vars and Frees.
berghofe
parents: 13646
diff changeset
   303
a67c9e6570ac elim_vars now handles both Vars and Frees.
berghofe
parents: 13646
diff changeset
   304
    fun elim_varst (t $ u) = elim_varst t $ elim_varst u
a67c9e6570ac elim_vars now handles both Vars and Frees.
berghofe
parents: 13646
diff changeset
   305
      | elim_varst (Abs (s, T, t)) = Abs (s, T, elim_varst t)
74228
c22e5bdb207d more scalable operations;
wenzelm
parents: 71843
diff changeset
   306
      | elim_varst (t as Free (x, T)) = if Term_Subst.Frees.defined tf (x, T) then t else mk_default' T
c22e5bdb207d more scalable operations;
wenzelm
parents: 71843
diff changeset
   307
      | elim_varst (t as Var (xi, T)) = if Term_Subst.Vars.defined tv (xi, T) then t else mk_default' T
19309
8ea43e9ad83a avoid polymorphic equality;
wenzelm
parents: 19126
diff changeset
   308
      | elim_varst t = t;
13608
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
   309
  in
37310
96e2b9a6f074 do not open Proofterm, which is very ould style;
wenzelm
parents: 37233
diff changeset
   310
    Proofterm.map_proof_terms (fn t =>
19309
8ea43e9ad83a avoid polymorphic equality;
wenzelm
parents: 19126
diff changeset
   311
      if Term.exists_subterm hidden_variable t then Envir.beta_norm (elim_varst t) else t) I prf
13608
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
   312
  end;
9a6f43b8eae1 Added function elim_vars.
berghofe
parents: 13341
diff changeset
   313
22280
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   314
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   315
(**** convert between hhf and non-hhf form ****)
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   316
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   317
fun hhf_proof P Q prf =
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   318
  let
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   319
    val params = Logic.strip_params Q;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   320
    val Hs = Logic.strip_assums_hyp P;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   321
    val Hs' = Logic.strip_assums_hyp Q;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   322
    val k = length Hs;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   323
    val l = length params;
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   324
    fun mk_prf i j Hs Hs' (Const ("Pure.all", _) $ Abs (_, _, P)) prf =
22280
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   325
          mk_prf i (j - 1) Hs Hs' P (prf %> Bound j)
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   326
      | mk_prf i j (H :: Hs) (H' :: Hs') (Const ("Pure.imp", _) $ _ $ P) prf =
22280
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   327
          mk_prf (i - 1) j Hs Hs' P (prf %% un_hhf_proof H' H (PBound i))
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   328
      | mk_prf _ _ _ _ _ prf = prf
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   329
  in
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   330
    prf |> Proofterm.incr_pboundvars k l |> mk_prf (k - 1) (l - 1) Hs Hs' P |>
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   331
    fold_rev (fn P => fn prf => AbsP ("H", SOME P, prf)) Hs' |>
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   332
    fold_rev (fn (s, T) => fn prf => Abst (s, SOME T, prf)) params
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   333
  end
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   334
and un_hhf_proof P Q prf =
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   335
  let
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   336
    val params = Logic.strip_params Q;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   337
    val Hs = Logic.strip_assums_hyp P;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   338
    val Hs' = Logic.strip_assums_hyp Q;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   339
    val k = length Hs;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   340
    val l = length params;
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   341
    fun mk_prf (Const ("Pure.all", _) $ Abs (s, T, P)) prf =
22280
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   342
          Abst (s, SOME T, mk_prf P prf)
56245
84fc7dfa3cd4 more qualified names;
wenzelm
parents: 56244
diff changeset
   343
      | mk_prf (Const ("Pure.imp", _) $ P $ Q) prf =
22280
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   344
          AbsP ("H", SOME P, mk_prf Q prf)
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   345
      | mk_prf _ prf = prf
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   346
  in
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   347
    prf |> Proofterm.incr_pboundvars k l |>
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   348
    fold (fn i => fn prf => prf %> Bound i) (l - 1 downto 0) |>
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   349
    fold (fn ((H, H'), i) => fn prf => prf %% hhf_proof H' H (PBound i))
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   350
      (Hs ~~ Hs' ~~ (k - 1 downto 0)) |>
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   351
    mk_prf Q
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   352
  end;
a20a203c8f41 Added functions hhf_proof and un_hhf_proof.
berghofe
parents: 21646
diff changeset
   353
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   354
71777
3875815f5967 clarified signature: avoid clash with Isabelle/Scala Term.OFCLASS on case-insensible file-system;
wenzelm
parents: 71087
diff changeset
   355
(**** expand PClass proofs ****)
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   356
70836
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   357
fun expand_of_sort_proof thy hyps (T, S) =
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   358
  let
70836
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   359
    val of_class_hyps = map (fn SOME t => try Logic.dest_of_class t | NONE => NONE) hyps;
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   360
    fun of_class_index p =
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   361
      (case find_index (fn SOME h => h = p | NONE => false) of_class_hyps of
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   362
        ~1 => raise Fail "expand_of_class_proof: missing class hypothesis"
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   363
      | i => PBound i);
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   364
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   365
    val sorts = AList.coalesce (op =) (rev (map_filter I of_class_hyps));
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   366
    fun get_sort T = the_default [] (AList.lookup (op =) sorts T);
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   367
    val subst = map_atyps
70836
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   368
      (fn T as TVar (ai, _) => TVar (ai, get_sort T)
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   369
        | T as TFree (a, _) => TFree (a, get_sort T));
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   370
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   371
    fun reconstruct prop =
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   372
      Proofterm.reconstruct_proof thy prop #>
70915
bd4d37edfee4 clarified expand_proof/expand_name: allow more detailed control via thm_header;
wenzelm
parents: 70879
diff changeset
   373
      Proofterm.expand_proof thy Proofterm.expand_name_empty #>
70836
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   374
      Same.commit (Proofterm.map_proof_same Same.same Same.same of_class_index);
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   375
  in
70836
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   376
    map2 reconstruct (Logic.mk_of_sort (T, S))
70458
9e2173eb23eb more direct proofs for type classes;
wenzelm
parents: 70449
diff changeset
   377
      (Proofterm.of_sort_proof (Sign.classes_of thy) (Thm.classrel_proof thy) (Thm.arity_proof thy)
71777
3875815f5967 clarified signature: avoid clash with Isabelle/Scala Term.OFCLASS on case-insensible file-system;
wenzelm
parents: 71087
diff changeset
   378
        (PClass o apfst Type.strip_sorts) (subst T, S))
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   379
  end;
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   380
70837
874092c031c3 tuned signature;
wenzelm
parents: 70836
diff changeset
   381
fun expand_of_class_proof thy hyps (T, c) =
874092c031c3 tuned signature;
wenzelm
parents: 70836
diff changeset
   382
  hd (expand_of_sort_proof thy hyps (T, [c]));
874092c031c3 tuned signature;
wenzelm
parents: 70836
diff changeset
   383
71777
3875815f5967 clarified signature: avoid clash with Isabelle/Scala Term.OFCLASS on case-insensible file-system;
wenzelm
parents: 71087
diff changeset
   384
fun expand_of_class thy (_: typ list) hyps (PClass (T, c)) =
70837
874092c031c3 tuned signature;
wenzelm
parents: 70836
diff changeset
   385
      SOME (expand_of_class_proof thy hyps (T, c), Proofterm.no_skel)
70836
44efbf252525 misc tuning and clarification;
wenzelm
parents: 70826
diff changeset
   386
  | expand_of_class _ _ _ _ = NONE;
37233
b78f31ca4675 Adapted to new format of proof terms containing explicit proofs of class membership.
berghofe
parents: 36744
diff changeset
   387
70879
0b320e92485c tuned signature;
wenzelm
parents: 70840
diff changeset
   388
0b320e92485c tuned signature;
wenzelm
parents: 70840
diff changeset
   389
(* standard preprocessor *)
0b320e92485c tuned signature;
wenzelm
parents: 70840
diff changeset
   390
0b320e92485c tuned signature;
wenzelm
parents: 70840
diff changeset
   391
fun standard_preproc rews thy =
0b320e92485c tuned signature;
wenzelm
parents: 70840
diff changeset
   392
  Proofterm.rewrite_proof thy (rews, rprocs true @ [expand_of_class thy]);
0b320e92485c tuned signature;
wenzelm
parents: 70840
diff changeset
   393
11522
42fbb6abed5a Initial revision of tools for proof terms.
berghofe
parents:
diff changeset
   394
end;