src/Tools/auto_solve.ML
author wenzelm
Tue, 31 Mar 2009 22:23:33 +0200
changeset 30825 14d24e1fe594
parent 30822 8aac4b974280
child 31007 7c871a9cf6f4
permissions -rw-r--r--
fixed header; misc simplification, use Conjunction.dest_conjunctions;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30825
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
     1
(*  Title:      Tools/auto_solve.ML
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
     2
    Author:     Timothy Bourke and Gerwin Klein, NICTA
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
     3
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
     4
Check whether a newly stated theorem can be solved directly by an
30825
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
     5
existing theorem.  Duplicate lemmas can be detected in this way.
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
     6
30825
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
     7
The implementation is based in part on Berghofer and Haftmann's
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
     8
quickcheck.ML.  It relies critically on the FindTheorems solves
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
     9
feature.
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    10
*)
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    11
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    12
signature AUTO_SOLVE =
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    13
sig
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    14
  val auto : bool ref
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    15
  val auto_time_limit : int ref
30785
15f64e05e703 Limit the number of results returned by auto_solves.
Timothy Bourke
parents: 30242
diff changeset
    16
  val limit : int ref
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    17
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    18
  val seek_solution : bool -> Proof.state -> Proof.state
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    19
end;
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    20
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    21
structure AutoSolve : AUTO_SOLVE =
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    22
struct
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    23
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    24
val auto = ref false;
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    25
val auto_time_limit = ref 2500;
30785
15f64e05e703 Limit the number of results returned by auto_solves.
Timothy Bourke
parents: 30242
diff changeset
    26
val limit = ref 5;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    27
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    28
fun seek_solution int state =
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    29
  let
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    30
    val ctxt = Proof.context_of state;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    31
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    32
    val crits = [(true, FindTheorems.Solves)];
30825
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    33
    fun find g = snd (FindTheorems.find_theorems ctxt (SOME g) (SOME (! limit)) false crits);
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    34
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    35
    fun prt_result (goal, results) =
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    36
      let
30822
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    37
        val msg =
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    38
          (case goal of
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    39
            NONE => "The current goal"
30825
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    40
          | SOME sg => Syntax.string_of_term ctxt (Thm.term_of sg));
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    41
      in
30822
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    42
        Pretty.big_list
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    43
          (msg ^ " could be solved directly with:")
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    44
          (map (FindTheorems.pretty_thm ctxt) results)
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    45
      end;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    46
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    47
    fun seek_against_goal () =
30825
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    48
      (case try Proof.get_goal state of
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    49
        NONE => NONE
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    50
      | SOME (_, (_, goal)) =>
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    51
          let
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    52
            val subgoals = Conjunction.dest_conjunctions (Thm.cprem_of goal 1);
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    53
            val rs =
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    54
              if length subgoals = 1
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    55
              then [(NONE, find goal)]
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    56
              else map (fn sg => (SOME sg, find (Goal.init sg))) subgoals;
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    57
            val results = filter_out (null o snd) rs;
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    58
          in if null results then NONE else SOME results end);
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    59
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    60
    fun go () =
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    61
      let
30822
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    62
        val res =
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    63
          TimeLimit.timeLimit (Time.fromMilliseconds (! auto_time_limit))
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    64
            (try seek_against_goal) ();
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    65
      in
30822
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    66
        (case res of
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    67
          SOME (SOME results) =>
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    68
            state |> Proof.goal_message
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    69
              (fn () => Pretty.chunks
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    70
                [Pretty.str "",
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    71
                  Pretty.markup Markup.hilite
8aac4b974280 superficial tuning;
wenzelm
parents: 30785
diff changeset
    72
                    (separate (Pretty.brk 0) (map prt_result results))])
30825
14d24e1fe594 fixed header;
wenzelm
parents: 30822
diff changeset
    73
        | _ => state)
30147
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    74
      end handle TimeLimit.TimeOut => (warning "AutoSolve: timeout."; state);
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    75
  in
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    76
    if int andalso ! auto andalso not (! Toplevel.quiet)
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    77
    then go ()
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    78
    else state
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    79
  end;
c1e1d96903ea observe some Isabelle/ML coding conventions;
wenzelm
parents: 29984
diff changeset
    80
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    81
end;
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    82
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    83
val _ = Context.>> (Specification.add_theorem_hook AutoSolve.seek_solution);
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    84
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    85
val auto_solve = AutoSolve.auto;
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    86
val auto_solve_time_limit = AutoSolve.auto_time_limit;
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    87