src/HOL/Tools/Sledgehammer/sledgehammer_isar_proof.ML
author blanchet
Fri, 31 Jan 2014 10:23:32 +0100
changeset 56544 824c48a539c9
parent 56536 src/HOL/Tools/Sledgehammer/sledgehammer_proof.ML@daa64e603e70
child 56553 5d027af93a08
permissions -rw-r--r--
renamed many Sledgehammer ML files to clarify structure
blanchet@56544
     1
(*  Title:      HOL/Tools/Sledgehammer/sledgehammer_isar_proof.ML
smolkas@51278
     2
    Author:     Jasmin Blanchette, TU Muenchen
smolkas@51278
     3
    Author:     Steffen Juilf Smolka, TU Muenchen
smolkas@51278
     4
smolkas@51278
     5
Basic data structures for representing and basic methods
smolkas@51278
     6
for dealing with Isar proof texts.
smolkas@51278
     7
*)
smolkas@51278
     8
blanchet@56544
     9
signature SLEDGEHAMMER_ISAR_PROOF =
smolkas@51274
    10
sig
wenzelm@52376
    11
  type label = string * int
blanchet@56158
    12
  type facts = label list * string list (* local and global facts *)
smolkas@51283
    13
smolkas@52315
    14
  datatype isar_qualifier = Show | Then
smolkas@51283
    15
smolkas@53591
    16
  datatype isar_proof =
blanchet@56042
    17
    Proof of (string * typ) list * (label * term) list * isar_step list
smolkas@52316
    18
  and isar_step =
smolkas@51283
    19
    Let of term * term |
blanchet@56042
    20
    Prove of isar_qualifier list * (string * typ) list * label * term * isar_proof list *
blanchet@56109
    21
      (facts * proof_method list list)
smolkas@53729
    22
  and proof_method =
blanchet@56536
    23
    Metis_Method | Simp_Method | Simp_Size_Method | Auto_Method | Fastforce_Method | Force_Method |
blanchet@56536
    24
    Arith_Method | Blast_Method | Meson_Method
smolkas@53729
    25
smolkas@52316
    26
  val no_label : label
smolkas@52316
    27
  val no_facts : facts
smolkas@51283
    28
smolkas@53729
    29
  val label_ord : label * label -> order
smolkas@53693
    30
smolkas@53693
    31
  val dummy_isar_step : isar_step
smolkas@53693
    32
blanchet@53135
    33
  val string_of_label : label -> string
smolkas@53729
    34
blanchet@56042
    35
  val fix_of_proof : isar_proof -> (string * typ) list
blanchet@56042
    36
  val assms_of_proof : isar_proof -> (label * term) list
smolkas@52316
    37
  val steps_of_proof : isar_proof -> isar_step list
smolkas@52316
    38
smolkas@52316
    39
  val label_of_step : isar_step -> label option
smolkas@53591
    40
  val subproofs_of_step : isar_step -> isar_proof list option
blanchet@56109
    41
  val byline_of_step : isar_step -> (facts * proof_method list list) option
smolkas@52316
    42
smolkas@53591
    43
  val fold_isar_step : (isar_step -> 'a -> 'a) -> isar_step -> 'a -> 'a
blanchet@56107
    44
  val fold_isar_steps : (isar_step -> 'a -> 'a) -> isar_step list -> 'a -> 'a
smolkas@53591
    45
smolkas@53729
    46
  val map_isar_steps : (isar_step -> isar_step) -> isar_proof -> isar_proof
smolkas@53729
    47
smolkas@53729
    48
  val add_proof_steps : isar_step list -> int -> int
smolkas@53693
    49
blanchet@55907
    50
  (** canonical proof labels: 1, 2, 3, ... in postorder **)
smolkas@53693
    51
  val canonical_label_ord : (label * label) -> order
smolkas@53693
    52
  val relabel_proof_canonically : isar_proof -> isar_proof
smolkas@53693
    53
smolkas@53693
    54
  structure Canonical_Lbl_Tab : TABLE
blanchet@55877
    55
end;
smolkas@51274
    56
blanchet@56544
    57
structure Sledgehammer_Isar_Proof : SLEDGEHAMMER_ISAR_PROOF =
smolkas@51274
    58
struct
smolkas@51274
    59
smolkas@51274
    60
type label = string * int
blanchet@55907
    61
type facts = label list * string list (* local and global facts *)
smolkas@51274
    62
smolkas@52315
    63
datatype isar_qualifier = Show | Then
smolkas@51274
    64
smolkas@53591
    65
datatype isar_proof =
blanchet@56042
    66
  Proof of (string * typ) list * (label * term) list * isar_step list
smolkas@52316
    67
and isar_step =
smolkas@51274
    68
  Let of term * term |
blanchet@56042
    69
  Prove of isar_qualifier list * (string * typ) list * label * term * isar_proof list *
blanchet@56109
    70
    (facts * proof_method list list)
smolkas@53729
    71
and proof_method =
blanchet@56536
    72
  Metis_Method | Simp_Method | Simp_Size_Method | Auto_Method | Fastforce_Method | Force_Method |
blanchet@56536
    73
  Arith_Method | Blast_Method | Meson_Method
smolkas@53729
    74
smolkas@52316
    75
val no_label = ("", ~1)
smolkas@52316
    76
val no_facts = ([],[])
smolkas@51274
    77
smolkas@53729
    78
val label_ord = pairself swap #> prod_ord int_ord fast_string_ord
smolkas@53693
    79
smolkas@53693
    80
val dummy_isar_step = Let (Term.dummy, Term.dummy)
smolkas@53693
    81
blanchet@53135
    82
fun string_of_label (s, num) = s ^ string_of_int num
smolkas@51274
    83
smolkas@52316
    84
fun fix_of_proof (Proof (fix, _, _)) = fix
smolkas@52316
    85
fun assms_of_proof (Proof (_, assms, _)) = assms
smolkas@52316
    86
fun steps_of_proof (Proof (_, _, steps)) = steps
smolkas@52315
    87
smolkas@53591
    88
fun label_of_step (Prove (_, _, l, _, _, _)) = SOME l
smolkas@52316
    89
  | label_of_step _ = NONE
smolkas@52315
    90
smolkas@53591
    91
fun subproofs_of_step (Prove (_, _, _, _, subproofs, _)) = SOME subproofs
smolkas@53591
    92
  | subproofs_of_step _ = NONE
smolkas@53591
    93
smolkas@53591
    94
fun byline_of_step (Prove (_, _, _, _, _, byline)) = SOME byline
smolkas@52316
    95
  | byline_of_step _ = NONE
smolkas@52316
    96
smolkas@53693
    97
fun fold_isar_steps f = fold (fold_isar_step f)
blanchet@56107
    98
and fold_isar_step f step =
blanchet@56109
    99
  fold (steps_of_proof #> fold_isar_steps f) (these (subproofs_of_step step)) #> f step
smolkas@53591
   100
blanchet@56107
   101
fun map_isar_steps f =
smolkas@53729
   102
  let
blanchet@56042
   103
    fun do_proof (Proof (fix, assms, steps)) = Proof (fix, assms, map do_step steps)
smolkas@53729
   104
    and do_step (step as Let _) = f step
smolkas@53729
   105
      | do_step (Prove (qs, xs, l, t, subproofs, by)) =
blanchet@56042
   106
        f (Prove (qs, xs, l, t, map do_proof subproofs, by))
smolkas@53729
   107
  in
blanchet@56107
   108
    do_proof
smolkas@53729
   109
  end
smolkas@53729
   110
smolkas@53729
   111
val add_proof_steps = fold_isar_steps (fn Prove _ => Integer.add 1 | _ => I)
smolkas@53693
   112
smolkas@53693
   113
smolkas@53693
   114
(** canonical proof labels: 1, 2, 3, ... in post traversal order **)
smolkas@53693
   115
smolkas@53694
   116
fun canonical_label_ord (((_, i1), (_, i2)) : label * label) = int_ord (i1, i2)
smolkas@53693
   117
smolkas@53693
   118
structure Canonical_Lbl_Tab = Table(
smolkas@53693
   119
  type key = label
smolkas@53693
   120
  val ord = canonical_label_ord)
smolkas@53693
   121
smolkas@53693
   122
fun relabel_proof_canonically proof =
smolkas@53693
   123
  let
smolkas@53693
   124
    fun next_label l (next, subst) =
blanchet@55907
   125
      let val l' = ("", next) in (l', (next + 1, (l, l') :: subst)) end
smolkas@53693
   126
blanchet@56042
   127
    fun do_byline by (_, subst) = apfst (apfst (map (AList.lookup (op =) subst #> the))) by
smolkas@53693
   128
    handle Option.Option =>
blanchet@56544
   129
      raise Fail "Sledgehammer_Isar_Proof: relabel_proof_canonically"
smolkas@53693
   130
smolkas@53693
   131
    fun do_assm (l, t) state =
smolkas@53693
   132
      let
smolkas@53693
   133
        val (l, state) = next_label l state
smolkas@53693
   134
      in ((l, t), state) end
smolkas@53693
   135
blanchet@56042
   136
    fun do_proof (Proof (fix, assms, steps)) state =
smolkas@53693
   137
      let
smolkas@53693
   138
        val (assms, state) = fold_map do_assm assms state
smolkas@53693
   139
        val (steps, state) = fold_map do_step steps state
smolkas@53693
   140
      in
blanchet@56042
   141
        (Proof (fix, assms, steps), state)
smolkas@53693
   142
      end
smolkas@53693
   143
smolkas@53693
   144
    and do_step (step as Let _) state = (step, state)
smolkas@53693
   145
      | do_step (Prove (qs, fix, l, t, subproofs, by)) state=
blanchet@55907
   146
        let
blanchet@55907
   147
          val by = do_byline by state
blanchet@55907
   148
          val (subproofs, state) = fold_map do_proof subproofs state
blanchet@55907
   149
          val (l, state) = next_label l state
blanchet@55907
   150
        in
blanchet@55907
   151
          (Prove (qs, fix, l, t, subproofs, by), state)
blanchet@55907
   152
        end
smolkas@53693
   153
  in
smolkas@53693
   154
    fst (do_proof proof (0, []))
smolkas@53693
   155
  end
smolkas@53693
   156
blanchet@55877
   157
end;