src/Tools/auto_solve.ML
author blanchet
Wed, 04 Mar 2009 10:45:52 +0100
changeset 30240 5b25fee0362c
parent 29858 c8cee17d7e50
child 30242 aea5d7fa7ef5
permissions -rw-r--r--
Merge.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
30240
blanchet
parents: 29858
diff changeset
     1
(*  Title:      Pure/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
30240
blanchet
parents: 29858
diff changeset
     4
Check whether a newly stated theorem can be solved directly by an
blanchet
parents: 29858
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
30240
blanchet
parents: 29858
diff changeset
     7
The implemenation is based in part on Berghofer and Haftmann's
blanchet
parents: 29858
diff changeset
     8
Pure/codegen.ML. It relies critically on the FindTheorems solves
blanchet
parents: 29858
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
30240
blanchet
parents: 29858
diff changeset
    14
  val auto : bool ref
blanchet
parents: 29858
diff changeset
    15
  val auto_time_limit : int ref
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    16
30240
blanchet
parents: 29858
diff changeset
    17
  val seek_solution : bool -> Proof.state -> Proof.state
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    18
end;
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    19
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    20
structure AutoSolve : AUTO_SOLVE =
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    21
struct
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    22
30240
blanchet
parents: 29858
diff changeset
    23
val auto = ref false;
blanchet
parents: 29858
diff changeset
    24
val auto_time_limit = ref 2500;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    25
30240
blanchet
parents: 29858
diff changeset
    26
fun seek_solution int state =
blanchet
parents: 29858
diff changeset
    27
  let
blanchet
parents: 29858
diff changeset
    28
    val ctxt = Proof.context_of state;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    29
30240
blanchet
parents: 29858
diff changeset
    30
    fun conj_to_list [] = []
blanchet
parents: 29858
diff changeset
    31
      | conj_to_list (t::ts) =
blanchet
parents: 29858
diff changeset
    32
        (Conjunction.dest_conjunction t
blanchet
parents: 29858
diff changeset
    33
         |> (fn (t1, t2) => conj_to_list (t1::t2::ts)))
blanchet
parents: 29858
diff changeset
    34
        handle TERM _ => t::conj_to_list ts;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    35
30240
blanchet
parents: 29858
diff changeset
    36
    val crits = [(true, FindTheorems.Solves)];
blanchet
parents: 29858
diff changeset
    37
    fun find g = (NONE, FindTheorems.find_theorems ctxt g true crits);
blanchet
parents: 29858
diff changeset
    38
    fun find_cterm g = (SOME g, FindTheorems.find_theorems ctxt
blanchet
parents: 29858
diff changeset
    39
                                  (SOME (Goal.init g)) true crits);
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    40
30240
blanchet
parents: 29858
diff changeset
    41
    fun prt_result (goal, results) =
blanchet
parents: 29858
diff changeset
    42
      let
blanchet
parents: 29858
diff changeset
    43
        val msg = case goal of
blanchet
parents: 29858
diff changeset
    44
                    NONE => "The current goal"
blanchet
parents: 29858
diff changeset
    45
                  | SOME g => Syntax.string_of_term ctxt (term_of g);
blanchet
parents: 29858
diff changeset
    46
      in
blanchet
parents: 29858
diff changeset
    47
        Pretty.big_list (msg ^ " could be solved directly with:")
blanchet
parents: 29858
diff changeset
    48
                        (map (FindTheorems.pretty_thm ctxt) results)
blanchet
parents: 29858
diff changeset
    49
      end;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    50
30240
blanchet
parents: 29858
diff changeset
    51
    fun seek_against_goal () =
blanchet
parents: 29858
diff changeset
    52
      let
blanchet
parents: 29858
diff changeset
    53
        val goal = try Proof.get_goal state
blanchet
parents: 29858
diff changeset
    54
                   |> Option.map (#2 o #2);
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    55
30240
blanchet
parents: 29858
diff changeset
    56
        val goals = goal
blanchet
parents: 29858
diff changeset
    57
                    |> Option.map (fn g => cprem_of g 1)
blanchet
parents: 29858
diff changeset
    58
                    |> the_list
blanchet
parents: 29858
diff changeset
    59
                    |> conj_to_list;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    60
30240
blanchet
parents: 29858
diff changeset
    61
        val rs = if length goals = 1
blanchet
parents: 29858
diff changeset
    62
                 then [find goal]
blanchet
parents: 29858
diff changeset
    63
                 else map find_cterm goals;
blanchet
parents: 29858
diff changeset
    64
        val frs = filter_out (null o snd) rs;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    65
30240
blanchet
parents: 29858
diff changeset
    66
      in if null frs then NONE else SOME frs end;
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    67
30240
blanchet
parents: 29858
diff changeset
    68
    fun go () =
blanchet
parents: 29858
diff changeset
    69
      let
blanchet
parents: 29858
diff changeset
    70
        val res = TimeLimit.timeLimit
blanchet
parents: 29858
diff changeset
    71
                    (Time.fromMilliseconds (! auto_time_limit))
blanchet
parents: 29858
diff changeset
    72
                    (try seek_against_goal) ();
blanchet
parents: 29858
diff changeset
    73
      in
blanchet
parents: 29858
diff changeset
    74
        case Option.join res of
blanchet
parents: 29858
diff changeset
    75
          NONE => state
blanchet
parents: 29858
diff changeset
    76
        | SOME results => (Proof.goal_message
blanchet
parents: 29858
diff changeset
    77
                            (fn () => Pretty.chunks [Pretty.str "",
blanchet
parents: 29858
diff changeset
    78
                              Pretty.markup Markup.hilite
blanchet
parents: 29858
diff changeset
    79
                              (Library.separate (Pretty.brk 0)
blanchet
parents: 29858
diff changeset
    80
                                                (map prt_result results))])
blanchet
parents: 29858
diff changeset
    81
                              state)
blanchet
parents: 29858
diff changeset
    82
      end handle TimeLimit.TimeOut => (warning "AutoSolve: timeout."; state);
blanchet
parents: 29858
diff changeset
    83
  in
blanchet
parents: 29858
diff changeset
    84
    if int andalso ! auto andalso not (! Toplevel.quiet)
blanchet
parents: 29858
diff changeset
    85
    then go ()
blanchet
parents: 29858
diff changeset
    86
    else state
blanchet
parents: 29858
diff changeset
    87
  end;
blanchet
parents: 29858
diff changeset
    88
29858
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    89
end;
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    90
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    91
val _ = Context.>> (Specification.add_theorem_hook AutoSolve.seek_solution);
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    92
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    93
val auto_solve = AutoSolve.auto;
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    94
val auto_solve_time_limit = AutoSolve.auto_time_limit;
c8cee17d7e50 Autosolve feature for detecting duplicate theorems; patch by Timothy Bourke
kleing
parents:
diff changeset
    95