src/HOL/Tools/Sledgehammer/sledgehammer_isar_proof.ML
author blanchet
Tue, 01 Feb 2022 17:33:12 +0100
changeset 75057 79b4e711d6a2
parent 72585 18eb7ec2720f
child 77919 8734ca279e59
permissions -rw-r--r--
robustly handle empty proof blocks in Isar proof output
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_isar_proof.ML
50263
0b430064296a added comments to new source files
smolkas
parents: 50260
diff changeset
     2
    Author:     Jasmin Blanchette, TU Muenchen
0b430064296a added comments to new source files
smolkas
parents: 50260
diff changeset
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
0b430064296a added comments to new source files
smolkas
parents: 50260
diff changeset
     4
0b430064296a added comments to new source files
smolkas
parents: 50260
diff changeset
     5
Basic data structures for representing and basic methods
0b430064296a added comments to new source files
smolkas
parents: 50260
diff changeset
     6
for dealing with Isar proof texts.
0b430064296a added comments to new source files
smolkas
parents: 50260
diff changeset
     7
*)
0b430064296a added comments to new source files
smolkas
parents: 50260
diff changeset
     8
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
     9
signature SLEDGEHAMMER_ISAR_PROOF =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    10
sig
55287
ffa306239316 renamed ML file
blanchet
parents: 55285
diff changeset
    11
  type proof_method = Sledgehammer_Proof_Methods.proof_method
55285
e88ad20035f4 merged 'reconstructors' and 'proof methods'
blanchet
parents: 55284
diff changeset
    12
51239
67cc209493b2 eliminated hard tabs;
wenzelm
parents: 51179
diff changeset
    13
  type label = string * int
54816
10d48c2a3e32 made timeouts in Sledgehammer not be 'option's -- simplified lots of code
blanchet
parents: 54767
diff changeset
    14
  type facts = label list * string list (* local and global facts *)
50268
5d6494332b0b added signature
smolkas
parents: 50267
diff changeset
    15
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
    16
  datatype isar_qualifier = Show | Then
50268
5d6494332b0b added signature
smolkas
parents: 50267
diff changeset
    17
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    18
  datatype isar_proof =
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    19
    Proof of {
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    20
      fixes : (string * typ) list,
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    21
      assumptions: (label * term) list,
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    22
      steps : isar_step list
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    23
    }
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    24
  and isar_step =
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    25
    Let of {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    26
      lhs : term,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    27
      rhs : term
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    28
    } |
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    29
    Prove of {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    30
      qualifiers : isar_qualifier list,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    31
      obtains : (string * typ) list,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    32
      label : label,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    33
      goal : term,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    34
      subproofs : isar_proof list,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    35
      facts : facts,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    36
      proof_methods : proof_method list,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    37
      comment : string
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    38
    }
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52590
diff changeset
    39
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    40
  val no_label : label
50268
5d6494332b0b added signature
smolkas
parents: 50267
diff changeset
    41
70586
57df8a85317a clarified signature;
wenzelm
parents: 70308
diff changeset
    42
  val label_ord : label ord
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51239
diff changeset
    43
  val string_of_label : label -> string
57776
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
    44
  val sort_facts : facts -> facts
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52590
diff changeset
    45
55260
ada3ae6458d4 more data structure rationalization
blanchet
parents: 55257
diff changeset
    46
  val steps_of_isar_proof : isar_proof -> isar_step list
72583
e728d3a3d383 Tuned isar_proofs constructions
desharna
parents: 72582
diff changeset
    47
  val isar_proof_with_steps : isar_proof -> isar_step list -> isar_proof
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    48
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55222
diff changeset
    49
  val label_of_isar_step : isar_step -> label option
55279
df41d34d1324 tuned data structure
blanchet
parents: 55278
diff changeset
    50
  val facts_of_isar_step : isar_step -> facts
df41d34d1324 tuned data structure
blanchet
parents: 55278
diff changeset
    51
  val proof_methods_of_isar_step : isar_step -> proof_method list
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    52
54765
blanchet
parents: 54764
diff changeset
    53
  val fold_isar_steps : (isar_step -> 'a -> 'a) -> isar_step list -> 'a -> 'a
55212
blanchet
parents: 55211
diff changeset
    54
  val map_isar_steps : (isar_step -> isar_step) -> isar_proof -> isar_proof
blanchet
parents: 55211
diff changeset
    55
  val add_isar_steps : isar_step list -> int -> int
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    56
55212
blanchet
parents: 55211
diff changeset
    57
  structure Canonical_Label_Tab : TABLE
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    58
70586
57df8a85317a clarified signature;
wenzelm
parents: 70308
diff changeset
    59
  val canonical_label_ord : label ord
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
    60
55299
c3bb1cffce26 generate comments in Isar proofs
blanchet
parents: 55287
diff changeset
    61
  val comment_isar_proof : (label -> proof_method list -> string) -> isar_proof -> isar_proof
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
    62
  val chain_isar_proof : isar_proof -> isar_proof
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
    63
  val kill_useless_labels_in_isar_proof : isar_proof -> isar_proof
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
    64
  val relabel_isar_proof_canonically : isar_proof -> isar_proof
55282
blanchet
parents: 55281
diff changeset
    65
  val relabel_isar_proof_nicely : isar_proof -> isar_proof
57792
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
    66
  val rationalize_obtains_in_isar_proofs : Proof.context -> isar_proof -> isar_proof
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
    67
55299
c3bb1cffce26 generate comments in Isar proofs
blanchet
parents: 55287
diff changeset
    68
  val string_of_isar_proof : Proof.context -> int -> int -> isar_proof -> string
54504
blanchet
parents: 54503
diff changeset
    69
end;
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    70
55202
824c48a539c9 renamed many Sledgehammer ML files to clarify structure
blanchet
parents: 55194
diff changeset
    71
structure Sledgehammer_Isar_Proof : SLEDGEHAMMER_ISAR_PROOF =
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    72
struct
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    73
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
    74
open ATP_Util
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
    75
open ATP_Proof
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
    76
open ATP_Problem_Generate
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
    77
open ATP_Proof_Reconstruct
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
    78
open Sledgehammer_Util
55287
ffa306239316 renamed ML file
blanchet
parents: 55285
diff changeset
    79
open Sledgehammer_Proof_Methods
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
    80
open Sledgehammer_Isar_Annotate
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
    81
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    82
type label = string * int
54534
blanchet
parents: 54504
diff changeset
    83
type facts = label list * string list (* local and global facts *)
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    84
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
    85
datatype isar_qualifier = Show | Then
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
    86
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
    87
datatype isar_proof =
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    88
  Proof of {
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    89
    fixes : (string * typ) list,
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    90
    assumptions: (label * term) list,
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    91
    steps : isar_step list
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
    92
  }
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
    93
and isar_step =
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    94
  Let of {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    95
    lhs : term,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    96
    rhs : term
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    97
  } |
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    98
  Prove of {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
    99
    qualifiers : isar_qualifier list,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   100
    obtains : (string * typ) list,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   101
    label : label,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   102
    goal : term,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   103
    subproofs : isar_proof list,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   104
    facts : facts,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   105
    proof_methods : proof_method list,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   106
    comment : string
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   107
  }
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52590
diff changeset
   108
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   109
val no_label = ("", ~1)
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   110
57776
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   111
(* cf. "label_ord" below *)
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   112
val assume_prefix = "a"
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   113
val have_prefix = "f"
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   114
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   115
fun label_ord ((s1, i1), (s2, i2)) =
59058
a78612c67ec0 renamed "pairself" to "apply2", in accordance to @{apply 2};
wenzelm
parents: 58928
diff changeset
   116
  (case int_ord (apply2 String.size (s1, s2)) of
57776
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   117
    EQUAL =>
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   118
    (case string_ord (s1, s2) of
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   119
      EQUAL => int_ord (i1, i2)
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   120
    | ord => ord (* "assume" before "have" *))
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   121
  | ord => ord)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   122
51998
f732a674db1b renamed Sledgehammer functions with 'for' in their names to 'of'
blanchet
parents: 51239
diff changeset
   123
fun string_of_label (s, num) = s ^ string_of_int num
50259
9c64a52ae499 put shrink in own structure
smolkas
parents:
diff changeset
   124
57776
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   125
(* Put the nearest local label first, since it's the most likely to be replaced by a "hence".
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   126
   (Some preplaying proof methods, e.g. "blast", react poorly to fact reorderings.) *)
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   127
fun sort_facts (lfs, gfs) = (sort (label_ord o swap) lfs, sort string_ord gfs)
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   128
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   129
fun steps_of_isar_proof (Proof {steps, ...}) = steps
72583
e728d3a3d383 Tuned isar_proofs constructions
desharna
parents: 72582
diff changeset
   130
fun isar_proof_with_steps (Proof {fixes, assumptions, ...}) steps =
e728d3a3d383 Tuned isar_proofs constructions
desharna
parents: 72582
diff changeset
   131
  Proof {fixes = fixes, assumptions = assumptions, steps = steps}
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   132
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   133
fun label_of_isar_step (Prove {label, ...}) = SOME label
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55222
diff changeset
   134
  | label_of_isar_step _ = NONE
51178
06689dbfe072 simplified byline, isar_qualifier
smolkas
parents: 51147
diff changeset
   135
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   136
fun subproofs_of_isar_step (Prove {subproofs, ...}) = subproofs
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   137
  | subproofs_of_isar_step _ = []
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   138
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   139
fun facts_of_isar_step (Prove {facts, ...}) = facts
55279
df41d34d1324 tuned data structure
blanchet
parents: 55278
diff changeset
   140
  | facts_of_isar_step _ = ([], [])
df41d34d1324 tuned data structure
blanchet
parents: 55278
diff changeset
   141
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   142
fun proof_methods_of_isar_step (Prove {proof_methods, ...}) = proof_methods
55279
df41d34d1324 tuned data structure
blanchet
parents: 55278
diff changeset
   143
  | proof_methods_of_isar_step _ = []
51179
0d5f8812856f split isar_step into isar_step, fix, assms; made isar_proof explicit; register fixed variables in ctxt and auto_fix terms to avoid superfluous annotations
smolkas
parents: 51178
diff changeset
   144
55212
blanchet
parents: 55211
diff changeset
   145
fun fold_isar_step f step =
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   146
  fold (steps_of_isar_proof #> fold_isar_steps f) (subproofs_of_isar_step step) #> f step
55212
blanchet
parents: 55211
diff changeset
   147
and fold_isar_steps f = fold (fold_isar_step f)
52454
b528a975b256 tuned: cleaned up data structure
smolkas
parents: 52453
diff changeset
   148
54765
blanchet
parents: 54764
diff changeset
   149
fun map_isar_steps f =
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52590
diff changeset
   150
  let
72583
e728d3a3d383 Tuned isar_proofs constructions
desharna
parents: 72582
diff changeset
   151
    fun map_proof (proof as Proof {steps, ...}) = isar_proof_with_steps proof (map map_step steps)
55212
blanchet
parents: 55211
diff changeset
   152
    and map_step (step as Let _) = f step
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   153
      | map_step (Prove {qualifiers, obtains, label, goal, subproofs, facts, proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   154
          comment}) =
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   155
        f (Prove {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   156
            qualifiers = qualifiers,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   157
            obtains = obtains,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   158
            label = label,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   159
            goal = goal,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   160
            subproofs = map map_proof subproofs,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   161
            facts = facts,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   162
            proof_methods = proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   163
            comment = comment})
55212
blanchet
parents: 55211
diff changeset
   164
  in map_proof end
52592
8a25b17e3d79 optimize isar-proofs by trying different proof methods
smolkas
parents: 52590
diff changeset
   165
55212
blanchet
parents: 55211
diff changeset
   166
val add_isar_steps = fold_isar_steps (fn Prove _ => Integer.add 1 | _ => I)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   167
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   168
(* canonical proof labels: 1, 2, 3, ... in post traversal order *)
52557
92ed2926596d made SML/NJ happy
smolkas
parents: 52556
diff changeset
   169
fun canonical_label_ord (((_, i1), (_, i2)) : label * label) = int_ord (i1, i2)
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   170
55212
blanchet
parents: 55211
diff changeset
   171
structure Canonical_Label_Tab = Table(
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   172
  type key = label
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   173
  val ord = canonical_label_ord)
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   174
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   175
fun comment_isar_step comment_of (Prove {qualifiers, obtains, label, goal, subproofs, facts,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   176
      proof_methods, ...}) =
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   177
    Prove {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   178
      qualifiers = qualifiers,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   179
      obtains = obtains,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   180
      label = label,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   181
      goal = goal,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   182
      subproofs = subproofs,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   183
      facts = facts,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   184
      proof_methods = proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   185
      comment = comment_of label proof_methods}
55299
c3bb1cffce26 generate comments in Isar proofs
blanchet
parents: 55287
diff changeset
   186
  | comment_isar_step _ step = step
c3bb1cffce26 generate comments in Isar proofs
blanchet
parents: 55287
diff changeset
   187
fun comment_isar_proof comment_of = map_isar_steps (comment_isar_step comment_of)
c3bb1cffce26 generate comments in Isar proofs
blanchet
parents: 55287
diff changeset
   188
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   189
fun chain_qs_lfs NONE lfs = ([], lfs)
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   190
  | chain_qs_lfs (SOME l0) lfs =
57655
dde0e76996ad introduce fact chaining also under first step
blanchet
parents: 57287
diff changeset
   191
    if member (op =) lfs l0 then ([Then], remove (op =) l0 lfs) else ([], lfs)
dde0e76996ad introduce fact chaining also under first step
blanchet
parents: 57287
diff changeset
   192
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   193
fun chain_isar_step lbl (Prove {qualifiers, obtains, label, goal, subproofs,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   194
      facts = (lfs, gfs), proof_methods, comment}) =
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   195
    let val (qs', lfs) = chain_qs_lfs lbl lfs in
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   196
      Prove {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   197
        qualifiers = qs' @ qualifiers,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   198
        obtains = obtains,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   199
        label = label,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   200
        goal = goal,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   201
        subproofs = map chain_isar_proof subproofs,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   202
        facts = (lfs, gfs),
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   203
        proof_methods = proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   204
        comment = comment}
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   205
    end
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   206
  | chain_isar_step _ step = step
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   207
and chain_isar_steps _ [] = []
57655
dde0e76996ad introduce fact chaining also under first step
blanchet
parents: 57287
diff changeset
   208
  | chain_isar_steps prev (i :: is) =
55223
3c593bad6b31 generalized preplaying infrastructure to store various results for various methods
blanchet
parents: 55222
diff changeset
   209
    chain_isar_step prev i :: chain_isar_steps (label_of_isar_step i) is
72583
e728d3a3d383 Tuned isar_proofs constructions
desharna
parents: 72582
diff changeset
   210
and chain_isar_proof (proof as Proof {assumptions, steps, ...}) =
72585
18eb7ec2720f Tuned indentation
desharna
parents: 72584
diff changeset
   211
  isar_proof_with_steps proof (chain_isar_steps (try (List.last #> fst) assumptions) steps)
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   212
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   213
fun kill_useless_labels_in_isar_proof proof =
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   214
  let
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   215
    val used_ls =
55279
df41d34d1324 tuned data structure
blanchet
parents: 55278
diff changeset
   216
      fold_isar_steps (facts_of_isar_step #> fst #> union (op =)) (steps_of_isar_proof proof) []
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   217
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   218
    fun kill_label l = if member (op =) used_ls l then l else no_label
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   219
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   220
    fun kill_step (Prove {qualifiers, obtains, label, goal, subproofs, facts, proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   221
          comment}) =
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   222
        Prove {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   223
          qualifiers = qualifiers,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   224
          obtains = obtains,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   225
          label = kill_label label,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   226
          goal = goal,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   227
          subproofs = map kill_proof subproofs,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   228
          facts = facts,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   229
          proof_methods = proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   230
          comment = comment}
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   231
      | kill_step step = step
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   232
    and kill_proof (Proof {fixes, assumptions, steps}) =
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   233
      let
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   234
        val assumptions' = map (apfst kill_label) assumptions
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   235
        val steps' = map kill_step steps
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   236
      in
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   237
        Proof {fixes = fixes, assumptions = assumptions', steps = steps'}
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   238
      end
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   239
  in
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   240
    kill_proof proof
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   241
  end
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   242
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
   243
fun relabel_isar_proof_canonically proof =
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   244
  let
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   245
    fun next_label l (next, subst) =
54534
blanchet
parents: 54504
diff changeset
   246
      let val l' = ("", next) in (l', (next + 1, (l, l') :: subst)) end
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   247
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   248
    fun relabel_step (Prove {qualifiers, obtains, label, goal, subproofs, facts = (lfs, gfs),
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   249
          proof_methods, comment}) (accum as (_, subst)) =
54534
blanchet
parents: 54504
diff changeset
   250
        let
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   251
          val lfs' = maps (the_list o AList.lookup (op =) subst) lfs
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   252
          val ((subs', l'), accum') = accum
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   253
            |> fold_map relabel_proof subproofs
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   254
            ||>> next_label label
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   255
          val prove = Prove {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   256
            qualifiers = qualifiers,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   257
            obtains = obtains,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   258
            label = l',
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   259
            goal = goal,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   260
            subproofs = subs',
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   261
            facts = (lfs', gfs),
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   262
            proof_methods = proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   263
            comment = comment}
54534
blanchet
parents: 54504
diff changeset
   264
        in
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   265
          (prove, accum')
54534
blanchet
parents: 54504
diff changeset
   266
        end
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   267
      | relabel_step step accum = (step, accum)
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   268
    and relabel_proof (Proof {fixes, assumptions, steps}) =
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   269
      fold_map (fn (l, t) => next_label l #> apfst (rpair t)) assumptions
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   270
      ##>> fold_map relabel_step steps
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   271
      #>> (fn (assumptions', steps') =>
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   272
            Proof {fixes = fixes, assumptions = assumptions', steps = steps'})
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   273
  in
55279
df41d34d1324 tuned data structure
blanchet
parents: 55278
diff changeset
   274
    fst (relabel_proof proof (0, []))
52556
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   275
  end
c8357085217c completely rewrote SH compress; added two parameters for experimentation/fine grained control
smolkas
parents: 52454
diff changeset
   276
55282
blanchet
parents: 55281
diff changeset
   277
val relabel_isar_proof_nicely =
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   278
  let
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   279
    fun next_label depth prefix l (accum as (next, subst)) =
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   280
      if l = no_label then
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   281
        (l, accum)
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   282
      else
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   283
        let val l' = (replicate_string (depth + 1) prefix, next) in
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   284
          (l', (next + 1, (l, l') :: subst))
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   285
        end
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   286
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   287
    fun relabel_step depth (Prove {qualifiers, obtains, label, goal,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   288
          subproofs, facts = (lfs, gfs), proof_methods, comment}) (accum as (_, subst)) =
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   289
        let
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   290
          val (l', accum' as (_, subst')) = next_label depth have_prefix label accum
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   291
          val prove = Prove {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   292
            qualifiers = qualifiers,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   293
            obtains = obtains,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   294
            label = l',
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   295
            goal = goal,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   296
            subproofs = map (relabel_proof subst' (depth + 1)) subproofs,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   297
            facts = (maps (the_list o AList.lookup (op =) subst) lfs, gfs),
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   298
            proof_methods = proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   299
            comment = comment}
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   300
        in
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   301
          (prove, accum')
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   302
        end
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   303
      | relabel_step _ step accum = (step, accum)
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   304
    and relabel_proof subst depth (Proof {fixes, assumptions, steps}) =
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   305
      (1, subst)
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   306
      |> fold_map (fn (l, t) => next_label depth assume_prefix l #> apfst (rpair t)) assumptions
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   307
      ||>> fold_map (relabel_step depth) steps
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   308
      |> (fn ((assumptions', steps'), _) =>
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   309
           Proof {fixes = fixes, assumptions = assumptions', steps = steps'})
55220
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   310
  in
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   311
    relabel_proof [] 0
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   312
  end
9d833fe96c51 moved code around
blanchet
parents: 55219
diff changeset
   313
57793
d1cf76cef93b tuned skolemization
blanchet
parents: 57792
diff changeset
   314
fun stutter_single_letter s = String.extract (s, 0, SOME 1) ^ s
d1cf76cef93b tuned skolemization
blanchet
parents: 57792
diff changeset
   315
57792
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   316
fun rationalize_obtains_in_isar_proofs ctxt =
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   317
  let
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   318
    fun rename_obtains xs (subst, ctxt) =
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   319
      let
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   320
        val Ts = map snd xs
57793
d1cf76cef93b tuned skolemization
blanchet
parents: 57792
diff changeset
   321
        val new_names0 = map (stutter_single_letter o var_name_of_typ o body_type) Ts
57792
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   322
        val (new_names, ctxt') = Variable.variant_fixes new_names0 ctxt
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   323
        val ys = map2 pair new_names Ts
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   324
      in
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   325
        (ys, ((map Free xs ~~ map Free ys) @ subst, ctxt'))
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   326
      end
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   327
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   328
    fun rationalize_step (Prove {qualifiers, obtains, label, goal, subproofs, facts, proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   329
          comment}) subst_ctxt =
57792
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   330
        let
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   331
          val (obtains', subst_ctxt' as (subst', _)) = rename_obtains obtains subst_ctxt
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   332
          val prove = Prove {
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   333
            qualifiers = qualifiers,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   334
            obtains = obtains',
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   335
            label = label,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   336
            goal = subst_atomic subst' goal,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   337
            subproofs = map (rationalize_proof false subst_ctxt') subproofs,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   338
            facts = facts,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   339
            proof_methods = proof_methods,
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   340
            comment = comment}
57792
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   341
        in
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   342
          (prove, subst_ctxt')
57792
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   343
        end
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   344
    and rationalize_proof outer (subst_ctxt as (subst, ctxt)) (Proof {fixes, assumptions, steps}) =
58475
4508b6bff671 rename skolem symbols in the negative case as well
blanchet
parents: 58087
diff changeset
   345
      let
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   346
        val (fixes', subst_ctxt' as (subst', _)) =
58476
6ade4c7109a8 make sure no '__' suffixes make it until Isar proof
blanchet
parents: 58475
diff changeset
   347
          if outer then
6ade4c7109a8 make sure no '__' suffixes make it until Isar proof
blanchet
parents: 58475
diff changeset
   348
            (* last call: eliminate any remaining skolem names (from SMT proofs) *)
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   349
            (fixes, (subst @ map (fn x => (Free (apfst Name.skolem x), Free x)) fixes, ctxt))
58476
6ade4c7109a8 make sure no '__' suffixes make it until Isar proof
blanchet
parents: 58475
diff changeset
   350
          else
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   351
            rename_obtains fixes subst_ctxt
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   352
        val assumptions' = map (apsnd (subst_atomic subst')) assumptions
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   353
        val steps' = fst (fold_map rationalize_step steps subst_ctxt')
58475
4508b6bff671 rename skolem symbols in the negative case as well
blanchet
parents: 58087
diff changeset
   354
      in
75057
79b4e711d6a2 robustly handle empty proof blocks in Isar proof output
blanchet
parents: 72585
diff changeset
   355
        Proof {fixes = fixes', assumptions = assumptions', steps = steps'}
58475
4508b6bff671 rename skolem symbols in the negative case as well
blanchet
parents: 58087
diff changeset
   356
      end
57792
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   357
  in
58475
4508b6bff671 rename skolem symbols in the negative case as well
blanchet
parents: 58087
diff changeset
   358
    rationalize_proof true ([], ctxt)
57792
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   359
  end
9cb24c835284 rationalize Skolem names
blanchet
parents: 57776
diff changeset
   360
58087
32d3fa94ebb4 use 'thesis' only if it expands to the right thing (it won't after an 'unfolding', for example)
blanchet
parents: 58082
diff changeset
   361
val thesis_var = ((Auto_Bind.thesisN, 0), HOLogic.boolT)
32d3fa94ebb4 use 'thesis' only if it expands to the right thing (it won't after an 'unfolding', for example)
blanchet
parents: 58082
diff changeset
   362
32d3fa94ebb4 use 'thesis' only if it expands to the right thing (it won't after an 'unfolding', for example)
blanchet
parents: 58082
diff changeset
   363
fun is_thesis ctxt t =
32d3fa94ebb4 use 'thesis' only if it expands to the right thing (it won't after an 'unfolding', for example)
blanchet
parents: 58082
diff changeset
   364
  (case Vartab.lookup (Variable.binds_of ctxt) (fst thesis_var) of
32d3fa94ebb4 use 'thesis' only if it expands to the right thing (it won't after an 'unfolding', for example)
blanchet
parents: 58082
diff changeset
   365
    SOME (_, t') => HOLogic.mk_Trueprop t' aconv t
32d3fa94ebb4 use 'thesis' only if it expands to the right thing (it won't after an 'unfolding', for example)
blanchet
parents: 58082
diff changeset
   366
  | NONE => false)
32d3fa94ebb4 use 'thesis' only if it expands to the right thing (it won't after an 'unfolding', for example)
blanchet
parents: 58082
diff changeset
   367
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   368
val indent_size = 2
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   369
56985
82c83978fbd9 correctly add extra facts to lemmas (cf. conjecture and hypotheses) in Z3 Isar proofs
blanchet
parents: 56983
diff changeset
   370
fun string_of_isar_proof ctxt0 i n proof =
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   371
  let
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58476
diff changeset
   372
    val keywords = Thy_Header.get_keywords' ctxt0
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58476
diff changeset
   373
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   374
    (* Make sure only type constraints inserted by the type annotation code are printed. *)
56985
82c83978fbd9 correctly add extra facts to lemmas (cf. conjecture and hypotheses) in Z3 Isar proofs
blanchet
parents: 56983
diff changeset
   375
    val ctxt = ctxt0
82c83978fbd9 correctly add extra facts to lemmas (cf. conjecture and hypotheses) in Z3 Isar proofs
blanchet
parents: 56983
diff changeset
   376
      |> Config.put show_markup false
82c83978fbd9 correctly add extra facts to lemmas (cf. conjecture and hypotheses) in Z3 Isar proofs
blanchet
parents: 56983
diff changeset
   377
      |> Config.put Printer.show_type_emphasis false
82c83978fbd9 correctly add extra facts to lemmas (cf. conjecture and hypotheses) in Z3 Isar proofs
blanchet
parents: 56983
diff changeset
   378
      |> Config.put show_types false
82c83978fbd9 correctly add extra facts to lemmas (cf. conjecture and hypotheses) in Z3 Isar proofs
blanchet
parents: 56983
diff changeset
   379
      |> Config.put show_sorts false
82c83978fbd9 correctly add extra facts to lemmas (cf. conjecture and hypotheses) in Z3 Isar proofs
blanchet
parents: 56983
diff changeset
   380
      |> Config.put show_consts false
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   381
55216
77953325d5f1 more concise Isar output
blanchet
parents: 55214
diff changeset
   382
    fun add_str s' = apfst (suffix s')
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   383
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   384
    fun of_indent ind = replicate_string (ind * indent_size) " "
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   385
    fun of_moreover ind = of_indent ind ^ "moreover\n"
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   386
    fun of_label l = if l = no_label then "" else string_of_label l ^ ": "
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   387
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   388
    fun of_obtain qs nr =
67405
e9ab4ad7bd15 uniform use of Standard ML op-infix -- eliminated warnings;
wenzelm
parents: 67399
diff changeset
   389
      (if nr > 1 orelse (nr = 1 andalso member (op =) qs Then) then "ultimately "
e9ab4ad7bd15 uniform use of Standard ML op-infix -- eliminated warnings;
wenzelm
parents: 67399
diff changeset
   390
       else if nr = 1 orelse member (op =) qs Then then "then "
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   391
       else "") ^ "obtain"
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   392
67405
e9ab4ad7bd15 uniform use of Standard ML op-infix -- eliminated warnings;
wenzelm
parents: 67399
diff changeset
   393
    fun of_show_have qs = if member (op =) qs Show then "show" else "have"
e9ab4ad7bd15 uniform use of Standard ML op-infix -- eliminated warnings;
wenzelm
parents: 67399
diff changeset
   394
    fun of_thus_hence qs = if member (op =) qs Show then "then show" else "then have"
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   395
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   396
    fun of_have qs nr =
67405
e9ab4ad7bd15 uniform use of Standard ML op-infix -- eliminated warnings;
wenzelm
parents: 67399
diff changeset
   397
      if nr > 1 orelse (nr = 1 andalso member (op =) qs Then) then "ultimately " ^ of_show_have qs
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   398
      else if nr = 1 orelse member (op =) qs Then then of_thus_hence qs
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   399
      else of_show_have qs
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   400
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   401
    fun add_term term (s, ctxt) =
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   402
      (s ^ (term
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   403
            |> singleton (Syntax.uncheck_terms ctxt)
55213
dcb36a2540bc tuned ML function names
blanchet
parents: 55212
diff changeset
   404
            |> annotate_types_in_term ctxt
66020
a31760eee09d discontinued obsolete print mode;
wenzelm
parents: 61756
diff changeset
   405
            |> Print_Mode.setmp [] (Syntax.unparse_term ctxt #> Pretty.string_of)
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   406
            |> simplify_spaces
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58476
diff changeset
   407
            |> maybe_quote keywords),
70308
7f568724d67e clarified signature;
wenzelm
parents: 67405
diff changeset
   408
       ctxt |> perhaps (try (Proof_Context.augment term)))
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   409
56983
132142089ea6 use 'simp add:' syntax in Sledgehammer rather than 'using'
blanchet
parents: 56081
diff changeset
   410
    fun using_facts [] [] = ""
132142089ea6 use 'simp add:' syntax in Sledgehammer rather than 'using'
blanchet
parents: 56081
diff changeset
   411
      | using_facts ls ss = enclose "using " " " (space_implode " " (map string_of_label ls @ ss))
55257
abfd7b90bba2 rationalized threading of 'metis' arguments
blanchet
parents: 55244
diff changeset
   412
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   413
    (* Local facts are always passed via "using", which affects "meson" and "metis". This is
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   414
       arguably stylistically superior, because it emphasises the structure of the proof. It is also
61756
21c2b1f691d1 avoid 'hence' and 'thus' in generated proofs
blanchet
parents: 59058
diff changeset
   415
       more robust w.r.t. preplay: Preplay is performed before chaining of local facts with "then"
21c2b1f691d1 avoid 'hence' and 'thus' in generated proofs
blanchet
parents: 59058
diff changeset
   416
       is introduced. See also "tac_of_method" in "Sledgehammer_Isar_Preplay". *)
57287
68aa585269ac given two one-liners, only show the best of the two
blanchet
parents: 57286
diff changeset
   417
    fun of_method ls ss meth =
68aa585269ac given two one-liners, only show the best of the two
blanchet
parents: 57286
diff changeset
   418
      let val direct = is_proof_method_direct meth in
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   419
        using_facts ls (if direct then [] else ss) ^
72401
2783779b7dd3 removed obsolete unmaintained experimental prover Pirate
blanchet
parents: 70586
diff changeset
   420
        "by " ^ string_of_proof_method (if direct then ss else []) meth
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   421
      end
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   422
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   423
    fun of_free (s, T) =
58928
23d0ffd48006 plain value Keywords.keywords, which might be used outside theory for bootstrap purposes;
wenzelm
parents: 58476
diff changeset
   424
      maybe_quote keywords s ^ " :: " ^
66020
a31760eee09d discontinued obsolete print mode;
wenzelm
parents: 61756
diff changeset
   425
      maybe_quote keywords (simplify_spaces (Print_Mode.setmp [] (Syntax.string_of_typ ctxt) T))
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   426
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   427
    fun add_frees xs (s, ctxt) =
70308
7f568724d67e clarified signature;
wenzelm
parents: 67405
diff changeset
   428
      (s ^ space_implode " and " (map of_free xs), ctxt |> fold Proof_Context.augment (map Free xs))
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   429
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   430
    fun add_fix _ [] = I
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   431
      | add_fix ind xs = add_str (of_indent ind ^ "fix ") #> add_frees xs #> add_str "\n"
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   432
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   433
    fun add_assm ind (l, t) =
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   434
      add_str (of_indent ind ^ "assume " ^ of_label l) #> add_term t #> add_str "\n"
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   435
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   436
    fun of_subproof ind ctxt proof =
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   437
      let
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   438
        val ind = ind + 1
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   439
        val s = of_proof ind ctxt proof
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   440
        val prefix = "{ "
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   441
        val suffix = " }"
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   442
      in
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   443
        replicate_string (ind * indent_size - size prefix) " " ^ prefix ^
75057
79b4e711d6a2 robustly handle empty proof blocks in Isar proof output
blanchet
parents: 72585
diff changeset
   444
        String.substring (s, ind * indent_size, size s - ind * indent_size - 1) ^
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   445
        suffix ^ "\n"
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   446
      end
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   447
    and of_subproofs _ _ _ [] = ""
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   448
      | of_subproofs ind ctxt qs subs =
67405
e9ab4ad7bd15 uniform use of Standard ML op-infix -- eliminated warnings;
wenzelm
parents: 67399
diff changeset
   449
        (if member (op =) qs Then then of_moreover ind else "") ^
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   450
        space_implode (of_moreover ind) (map (of_subproof ind ctxt) subs)
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   451
    and add_step_pre ind qs subs (s, ctxt) =
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   452
      (s ^ of_subproofs ind ctxt qs subs ^ of_indent ind, ctxt)
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   453
    and add_step ind (Let {lhs = t1, rhs = t2}) =
57776
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   454
        add_str (of_indent ind ^ "let ") #> add_term t1 #> add_str " = " #> add_term t2
1111a9a328fe rationalized sorting of facts -- so that preplaying (almost always) coincides with the real thing, preventing odd failures
blanchet
parents: 57765
diff changeset
   455
        #> add_str "\n"
72584
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   456
      | add_step ind (Prove {qualifiers, obtains, label, goal, subproofs, facts = (ls, ss),
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   457
          proof_methods = meth :: _, comment}) =
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   458
        let val num_subproofs = length subproofs in
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   459
          add_step_pre ind qualifiers subproofs
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   460
          #> (case obtains of
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   461
              [] => add_str (of_have qualifiers num_subproofs ^ " ")
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   462
            | _ =>
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   463
              add_str (of_obtain qualifiers num_subproofs ^ " ")
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   464
              #> add_frees obtains
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   465
              #> add_str (" where\n" ^ of_indent (ind + 1)))
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   466
          #> add_str (of_label label)
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   467
          #> add_term (if is_thesis ctxt goal then HOLogic.mk_Trueprop (Var thesis_var) else goal)
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   468
          #> add_str ("\n" ^ of_indent (ind + 1) ^ of_method ls ss meth ^
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   469
            (if comment = "" then "" else " (* " ^ comment ^ " *)") ^ "\n")
4ea19e5dc67e Tuned isar_step datatype
desharna
parents: 72583
diff changeset
   470
        end
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   471
    and add_steps ind = fold (add_step ind)
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   472
    and of_proof ind ctxt (Proof {fixes, assumptions, steps}) =
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   473
      ("", ctxt)
72582
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   474
      |> add_fix ind fixes
b69a3a7655f2 Tuned isar_proof datatype
desharna
parents: 72401
diff changeset
   475
      |> fold (add_assm ind) assumptions
55281
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   476
      |> add_steps ind steps
765555d6a0b2 refactor relabeling code
blanchet
parents: 55280
diff changeset
   477
      |> fst
75057
79b4e711d6a2 robustly handle empty proof blocks in Isar proof output
blanchet
parents: 72585
diff changeset
   478
      |> (fn s => if s = "" then of_indent ind ^ "\n" else s)  (* robustness *)
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   479
  in
57286
4868ec62f533 don't generate Isar proof skeleton for what amounts to a one-line proof
blanchet
parents: 57154
diff changeset
   480
    (if i <> 1 then "prefer " ^ string_of_int i ^ "\n" else "") ^
57287
68aa585269ac given two one-liners, only show the best of the two
blanchet
parents: 57286
diff changeset
   481
    of_indent 0 ^ "proof -\n" ^ of_proof 1 ctxt proof ^
68aa585269ac given two one-liners, only show the best of the two
blanchet
parents: 57286
diff changeset
   482
    of_indent 0 ^ (if n = 1 then "qed" else "next")
55211
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   483
  end
5d027af93a08 moved ML code around
blanchet
parents: 55202
diff changeset
   484
54504
blanchet
parents: 54503
diff changeset
   485
end;