src/Pure/search.ML
author haftmann
Tue Oct 20 16:13:01 2009 +0200 (2009-10-20)
changeset 33037 b22e44496dc2
parent 32940 51d450f278ce
child 33335 1e189f96c393
permissions -rw-r--r--
replaced old_style infixes eq_set, subset, union, inter and variants by generic versions
wenzelm@23178
     1
(*  Title:      Pure/search.ML
wenzelm@23178
     2
    Author:     Lawrence C Paulson and Norbert Voelker
paulson@1588
     3
wenzelm@16179
     4
Search tacticals.
paulson@1588
     5
*)
paulson@1588
     6
paulson@2672
     7
infix 1 THEN_MAYBE THEN_MAYBE';
paulson@2672
     8
paulson@1588
     9
signature SEARCH =
wenzelm@32940
    10
sig
wenzelm@32940
    11
  val trace_DEPTH_FIRST: bool Unsynchronized.ref
wenzelm@32940
    12
  val DEPTH_FIRST: (thm -> bool) -> tactic -> tactic
wenzelm@32940
    13
  val has_fewer_prems: int -> thm -> bool
wenzelm@32940
    14
  val IF_UNSOLVED: tactic -> tactic
wenzelm@32940
    15
  val SOLVE: tactic -> tactic
oheimb@8149
    16
  val DETERM_UNTIL_SOLVED: tactic -> tactic
wenzelm@32940
    17
  val THEN_MAYBE: tactic * tactic -> tactic
wenzelm@32940
    18
  val THEN_MAYBE': ('a -> tactic) * ('a -> tactic) -> 'a -> tactic
wenzelm@32940
    19
  val DEPTH_SOLVE: tactic -> tactic
wenzelm@32940
    20
  val DEPTH_SOLVE_1: tactic -> tactic
wenzelm@32940
    21
  val iter_deepen_limit: int Unsynchronized.ref
wenzelm@32940
    22
  val THEN_ITER_DEEPEN: tactic -> (thm -> bool) -> (int -> tactic) -> tactic
wenzelm@32940
    23
  val ITER_DEEPEN: (thm -> bool) -> (int -> tactic) -> tactic
wenzelm@32940
    24
  val DEEPEN: int * int -> (int -> int -> tactic) -> int -> int -> tactic
wenzelm@32940
    25
  val trace_BEST_FIRST: bool Unsynchronized.ref
wenzelm@32940
    26
  val THEN_BEST_FIRST: tactic -> (thm -> bool) * (thm -> int) -> tactic -> tactic
wenzelm@32940
    27
  val BEST_FIRST: (thm -> bool) * (thm -> int) -> tactic -> tactic
wenzelm@32940
    28
  val BREADTH_FIRST: (thm -> bool) -> tactic -> tactic
wenzelm@32940
    29
  val QUIET_BREADTH_FIRST: (thm -> bool) -> tactic -> tactic
wenzelm@32940
    30
  val trace_ASTAR: bool Unsynchronized.ref
wenzelm@32940
    31
  val THEN_ASTAR: tactic -> (thm -> bool) * (int -> thm -> int) -> tactic -> tactic
wenzelm@32940
    32
  val ASTAR: (thm -> bool) * (int -> thm -> int) -> tactic -> tactic
wenzelm@32940
    33
end;
paulson@1588
    34
wenzelm@32940
    35
structure Search: SEARCH =
paulson@1588
    36
struct
paulson@1588
    37
paulson@1588
    38
(**** Depth-first search ****)
paulson@1588
    39
wenzelm@32738
    40
val trace_DEPTH_FIRST = Unsynchronized.ref false;
paulson@1588
    41
paulson@1588
    42
(*Searches until "satp" reports proof tree as satisfied.
paulson@1588
    43
  Suppresses duplicate solutions to minimize search space.*)
wenzelm@23178
    44
fun DEPTH_FIRST satp tac =
paulson@1588
    45
 let val tac = tracify trace_DEPTH_FIRST tac
skalberg@15531
    46
     fun depth used [] = NONE
paulson@1588
    47
       | depth used (q::qs) =
wenzelm@23178
    48
          case Seq.pull q of
wenzelm@23178
    49
              NONE         => depth used qs
wenzelm@23178
    50
            | SOME(st,stq) =>
wenzelm@23178
    51
                if satp st andalso not (member Thm.eq_thm used st)
wenzelm@23178
    52
                then SOME(st, Seq.make
wenzelm@23178
    53
                                 (fn()=> depth (st::used) (stq::qs)))
wenzelm@23178
    54
                else depth used (tac st :: stq :: qs)
webertj@20852
    55
  in  traced_tac (fn st => depth [] [Seq.single st])  end;
paulson@1588
    56
paulson@1588
    57
paulson@1588
    58
paulson@1588
    59
(*Predicate: Does the rule have fewer than n premises?*)
paulson@1588
    60
fun has_fewer_prems n rule = (nprems_of rule < n);
paulson@1588
    61
paulson@1588
    62
(*Apply a tactic if subgoals remain, else do nothing.*)
paulson@1588
    63
val IF_UNSOLVED = COND (has_fewer_prems 1) all_tac;
paulson@1588
    64
oheimb@5754
    65
(*Force a tactic to solve its goal completely, otherwise fail *)
oheimb@5754
    66
fun SOLVE tac = tac THEN COND (has_fewer_prems 1) all_tac no_tac;
oheimb@5754
    67
oheimb@8149
    68
(*Force repeated application of tactic until goal is solved completely *)
oheimb@8149
    69
val DETERM_UNTIL_SOLVED = DETERM_UNTIL (has_fewer_prems 1);
oheimb@8149
    70
paulson@2672
    71
(*Execute tac1, but only execute tac2 if there are at least as many subgoals
paulson@2672
    72
  as before.  This ensures that tac2 is only applied to an outcome of tac1.*)
wenzelm@23178
    73
fun (tac1 THEN_MAYBE tac2) st =
wenzelm@32940
    74
  (tac1  THEN  COND (has_fewer_prems (nprems_of st)) all_tac tac2)  st;
paulson@2672
    75
paulson@2672
    76
fun (tac1 THEN_MAYBE' tac2) x = tac1 x THEN_MAYBE tac2 x;
paulson@2672
    77
paulson@1588
    78
(*Tactical to reduce the number of premises by 1.
paulson@1588
    79
  If no subgoals then it must fail! *)
paulson@3538
    80
fun DEPTH_SOLVE_1 tac st = st |>
paulson@1588
    81
    (case nprems_of st of
wenzelm@23178
    82
        0 => no_tac
paulson@3538
    83
      | n => DEPTH_FIRST (has_fewer_prems n) tac);
paulson@1588
    84
paulson@1588
    85
(*Uses depth-first search to solve ALL subgoals*)
paulson@1588
    86
val DEPTH_SOLVE = DEPTH_FIRST (has_fewer_prems 1);
paulson@1588
    87
paulson@1588
    88
paulson@1588
    89
paulson@2869
    90
(**** Iterative deepening with pruning ****)
paulson@1588
    91
paulson@1588
    92
fun has_vars (Var _) = true
paulson@1588
    93
  | has_vars (Abs (_,_,t)) = has_vars t
paulson@1588
    94
  | has_vars (f$t) = has_vars f orelse has_vars t
paulson@1588
    95
  | has_vars _ = false;
paulson@1588
    96
paulson@1588
    97
(*Counting of primitive inferences is APPROXIMATE, as the step tactic
paulson@1588
    98
  may perform >1 inference*)
paulson@1588
    99
paulson@1588
   100
(*Pruning of rigid ancestor to prevent backtracking*)
wenzelm@23178
   101
fun prune (new as (k', np':int, rgd', stq), qs) =
paulson@1588
   102
    let fun prune_aux (qs, []) = new::qs
paulson@1588
   103
          | prune_aux (qs, (k,np,rgd,q)::rqs) =
wenzelm@23178
   104
              if np'+1 = np andalso rgd then
wenzelm@23178
   105
                  (if !trace_DEPTH_FIRST then
wenzelm@23178
   106
                       tracing ("Pruning " ^
wenzelm@23178
   107
                                string_of_int (1+length rqs) ^ " levels")
wenzelm@23178
   108
                   else ();
wenzelm@23178
   109
                   (*Use OLD k: zero-cost solution; see Stickel, p 365*)
wenzelm@23178
   110
                   (k, np', rgd', stq) :: qs)
wenzelm@23178
   111
              else prune_aux ((k,np,rgd,q)::qs, rqs)
paulson@1588
   112
        fun take ([], rqs) = ([], rqs)
wenzelm@23178
   113
          | take (arg as ((k,np,rgd,stq)::qs, rqs)) =
wenzelm@23178
   114
                if np' < np then take (qs, (k,np,rgd,stq)::rqs)
wenzelm@23178
   115
                            else arg
paulson@1588
   116
    in  prune_aux (take (qs, []))  end;
paulson@1588
   117
paulson@1588
   118
paulson@22025
   119
(*No known example (on 1-5-2007) needs even thirty*)
wenzelm@32738
   120
val iter_deepen_limit = Unsynchronized.ref 50;
paulson@22025
   121
paulson@1588
   122
(*Depth-first iterative deepening search for a state that satisfies satp
paulson@1588
   123
  tactic tac0 sets up the initial goal queue, while tac1 searches it.
paulson@1588
   124
  The solution sequence is redundant: the cutoff heuristic makes it impossible
paulson@1588
   125
  to suppress solutions arising from earlier searches, as the accumulated cost
paulson@1588
   126
  (k) can be wrong.*)
wenzelm@23178
   127
fun THEN_ITER_DEEPEN tac0 satp tac1 = traced_tac (fn st =>
wenzelm@32738
   128
 let val countr = Unsynchronized.ref 0
paulson@1588
   129
     and tf = tracify trace_DEPTH_FIRST (tac1 1)
paulson@1588
   130
     and qs0 = tac0 st
paulson@1588
   131
     (*bnd = depth bound; inc = estimate of increment required next*)
wenzelm@23178
   132
     fun depth (bnd,inc) [] =
paulson@22025
   133
          if bnd > !iter_deepen_limit then
wenzelm@23178
   134
             (tracing (string_of_int (!countr) ^
wenzelm@23178
   135
                       " inferences so far.  Giving up at " ^ string_of_int bnd);
wenzelm@23178
   136
              NONE)
paulson@22025
   137
          else
wenzelm@23178
   138
             (tracing (string_of_int (!countr) ^
wenzelm@23178
   139
                       " inferences so far.  Searching to depth " ^
wenzelm@23178
   140
                       string_of_int bnd);
wenzelm@23178
   141
              (*larger increments make it run slower for the hard problems*)
wenzelm@23178
   142
              depth (bnd+inc, 10)) [(0, 1, false, qs0)]
paulson@1588
   143
       | depth (bnd,inc) ((k,np,rgd,q)::qs) =
wenzelm@23178
   144
          if k>=bnd then depth (bnd,inc) qs
paulson@1588
   145
          else
wenzelm@32738
   146
          case (Unsynchronized.inc countr;
wenzelm@23178
   147
                if !trace_DEPTH_FIRST then
wenzelm@23178
   148
                    tracing (string_of_int np ^ implode (map (fn _ => "*") qs))
wenzelm@23178
   149
                else ();
wenzelm@23178
   150
                Seq.pull q) of
wenzelm@23178
   151
             NONE         => depth (bnd,inc) qs
wenzelm@23178
   152
           | SOME(st,stq) =>
wenzelm@23178
   153
               if satp st       (*solution!*)
wenzelm@23178
   154
               then SOME(st, Seq.make
wenzelm@23178
   155
                         (fn()=> depth (bnd,inc) ((k,np,rgd,stq)::qs)))
paulson@1588
   156
wenzelm@23178
   157
               else
paulson@1588
   158
               let val np' = nprems_of st
wenzelm@23178
   159
                     (*rgd' calculation assumes tactic operates on subgoal 1*)
paulson@1588
   160
                   val rgd' = not (has_vars (hd (prems_of st)))
paulson@1588
   161
                   val k' = k+np'-np+1  (*difference in # of subgoals, +1*)
wenzelm@23178
   162
               in  if k'+np' >= bnd
wenzelm@23178
   163
                   then depth (bnd, Int.min(inc, k'+np'+1-bnd)) qs
wenzelm@23178
   164
                   else if np' < np (*solved a subgoal; prune rigid ancestors*)
wenzelm@23178
   165
                   then depth (bnd,inc)
wenzelm@23178
   166
                         (prune ((k', np', rgd', tf st), (k,np,rgd,stq) :: qs))
wenzelm@23178
   167
                   else depth (bnd,inc) ((k', np', rgd', tf st) ::
wenzelm@23178
   168
                                         (k,np,rgd,stq) :: qs)
wenzelm@23178
   169
               end
paulson@1588
   170
  in depth (0,5) [] end);
paulson@1588
   171
paulson@1588
   172
val ITER_DEEPEN = THEN_ITER_DEEPEN all_tac;
paulson@1588
   173
paulson@1588
   174
paulson@2869
   175
(*Simple iterative deepening tactical.  It merely "deepens" any search tactic
paulson@2869
   176
  using increment "inc" up to limit "lim". *)
wenzelm@23178
   177
fun DEEPEN (inc,lim) tacf m i =
wenzelm@23178
   178
  let fun dpn m st =
paulson@14160
   179
       st |> (if has_fewer_prems i st then no_tac
wenzelm@23178
   180
              else if m>lim then
wenzelm@23178
   181
                       (warning "Search depth limit exceeded: giving up";
wenzelm@23178
   182
                        no_tac)
aspinall@23841
   183
              else (priority ("Search depth = " ^ string_of_int m);
wenzelm@23178
   184
                             tacf m i  ORELSE  dpn (m+inc)))
paulson@2869
   185
  in  dpn m  end;
wenzelm@23178
   186
wenzelm@32940
   187
paulson@1588
   188
(*** Best-first search ***)
paulson@1588
   189
wenzelm@32940
   190
(*total ordering on theorems, allowing duplicates to be found*)
wenzelm@32940
   191
structure Thm_Heap = Heap
wenzelm@32940
   192
(
wenzelm@32940
   193
  type elem = int * thm;
wenzelm@32940
   194
  val ord = prod_ord int_ord (TermOrd.term_ord o pairself Thm.prop_of);
wenzelm@32940
   195
);
wenzelm@32940
   196
wenzelm@32738
   197
val trace_BEST_FIRST = Unsynchronized.ref false;
paulson@1588
   198
paulson@1588
   199
(*For creating output sequence*)
skalberg@15531
   200
fun some_of_list []     = NONE
skalberg@15531
   201
  | some_of_list (x::l) = SOME (x, Seq.make (fn () => some_of_list l));
paulson@1588
   202
paulson@9094
   203
(*Check for and delete duplicate proof states*)
wenzelm@32940
   204
fun delete_all_min prf heap =
wenzelm@32940
   205
  if Thm_Heap.is_empty heap then heap
wenzelm@32940
   206
  else if Thm.eq_thm (prf, #2 (Thm_Heap.min heap))
wenzelm@32940
   207
  then delete_all_min prf (Thm_Heap.delete_min heap)
wenzelm@32940
   208
  else heap;
paulson@1588
   209
paulson@1588
   210
(*Best-first search for a state that satisfies satp (incl initial state)
paulson@1588
   211
  Function sizef estimates size of problem remaining (smaller means better).
paulson@1588
   212
  tactic tac0 sets up the initial priority queue, while tac1 searches it. *)
wenzelm@23178
   213
fun THEN_BEST_FIRST tac0 (satp, sizef) tac1 =
paulson@1588
   214
  let val tac = tracify trace_BEST_FIRST tac1
paulson@1588
   215
      fun pairsize th = (sizef th, th);
paulson@9094
   216
      fun bfs (news,nprf_heap) =
wenzelm@23178
   217
           (case  List.partition satp news  of
wenzelm@32940
   218
                ([],nonsats) => next(fold_rev Thm_Heap.insert (map pairsize nonsats) nprf_heap)
wenzelm@23178
   219
              | (sats,_)  => some_of_list sats)
paulson@9094
   220
      and next nprf_heap =
wenzelm@32940
   221
            if Thm_Heap.is_empty nprf_heap then NONE
wenzelm@23178
   222
            else
wenzelm@32940
   223
            let val (n,prf) = Thm_Heap.min nprf_heap
wenzelm@23178
   224
            in if !trace_BEST_FIRST
wenzelm@23178
   225
               then tracing("state size = " ^ string_of_int n)
paulson@1588
   226
               else ();
wenzelm@23178
   227
               bfs (Seq.list_of (tac prf),
wenzelm@32940
   228
                    delete_all_min prf (Thm_Heap.delete_min nprf_heap))
paulson@9094
   229
            end
wenzelm@32940
   230
      fun btac st = bfs (Seq.list_of (tac0 st), Thm_Heap.empty)
paulson@1588
   231
  in traced_tac btac end;
paulson@1588
   232
paulson@1588
   233
(*Ordinary best-first search, with no initial tactic*)
paulson@1588
   234
val BEST_FIRST = THEN_BEST_FIRST all_tac;
paulson@1588
   235
wenzelm@23178
   236
(*Breadth-first search to satisfy satpred (including initial state)
paulson@1588
   237
  SLOW -- SHOULD NOT USE APPEND!*)
wenzelm@23178
   238
fun gen_BREADTH_FIRST message satpred (tac:tactic) =
wenzelm@4270
   239
  let val tacf = Seq.list_of o tac;
paulson@1588
   240
      fun bfs prfs =
wenzelm@23178
   241
         (case  List.partition satpred prfs  of
wenzelm@23178
   242
              ([],[]) => []
wenzelm@23178
   243
            | ([],nonsats) =>
wenzelm@23178
   244
                  (message("breadth=" ^ string_of_int(length nonsats));
wenzelm@23178
   245
                   bfs (maps tacf nonsats))
wenzelm@23178
   246
            | (sats,_)  => sats)
wenzelm@4270
   247
  in (fn st => Seq.of_list (bfs [st])) end;
paulson@1588
   248
wenzelm@12262
   249
val BREADTH_FIRST = gen_BREADTH_FIRST tracing;
wenzelm@5693
   250
val QUIET_BREADTH_FIRST = gen_BREADTH_FIRST (K ());
wenzelm@5693
   251
paulson@1588
   252
wenzelm@23178
   253
(*  Author:     Norbert Voelker, FernUniversitaet Hagen
paulson@1588
   254
    Remarks:    Implementation of A*-like proof procedure by modification
wenzelm@23178
   255
                of the existing code for BEST_FIRST and best_tac so that the
wenzelm@23178
   256
                current level of search is taken into account.
wenzelm@23178
   257
*)
paulson@1588
   258
paulson@1588
   259
(*Insertion into priority queue of states, marked with level *)
paulson@1588
   260
fun insert_with_level (lnth: int*int*thm, []) = [lnth]
wenzelm@23178
   261
  | insert_with_level ((l,m,th), (l',n,th')::nths) =
paulson@1588
   262
      if  n<m then (l',n,th') :: insert_with_level ((l,m,th), nths)
wenzelm@22360
   263
      else if  n=m andalso Thm.eq_thm(th,th')
paulson@1588
   264
              then (l',n,th')::nths
paulson@1588
   265
              else (l,m,th)::(l',n,th')::nths;
paulson@1588
   266
paulson@1588
   267
(*For creating output sequence*)
skalberg@15531
   268
fun some_of_list []     = NONE
skalberg@15531
   269
  | some_of_list (x::l) = SOME (x, Seq.make (fn () => some_of_list l));
paulson@1588
   270
wenzelm@32738
   271
val trace_ASTAR = Unsynchronized.ref false;
paulson@1588
   272
wenzelm@23178
   273
fun THEN_ASTAR tac0 (satp, costf) tac1 =
wenzelm@23178
   274
  let val tf = tracify trace_ASTAR tac1;
paulson@1588
   275
      fun bfs (news,nprfs,level) =
paulson@1588
   276
      let fun cost thm = (level, costf level thm, thm)
skalberg@15570
   277
      in (case  List.partition satp news  of
wenzelm@23178
   278
            ([],nonsats)
wenzelm@23178
   279
                 => next (List.foldr insert_with_level nprfs (map cost nonsats))
paulson@1588
   280
          | (sats,_)  => some_of_list sats)
wenzelm@23178
   281
      end and
skalberg@15531
   282
      next []  = NONE
paulson@1588
   283
        | next ((level,n,prf)::nprfs)  =
wenzelm@23178
   284
            (if !trace_ASTAR
wenzelm@12262
   285
               then tracing("level = " ^ string_of_int level ^
wenzelm@23178
   286
                         "  cost = " ^ string_of_int n ^
wenzelm@23178
   287
                         "  queue length =" ^ string_of_int (length nprfs))
paulson@1588
   288
               else ();
wenzelm@4270
   289
             bfs (Seq.list_of (tf prf), nprfs,level+1))
wenzelm@4270
   290
      fun tf st = bfs (Seq.list_of (tac0 st), [], 0)
paulson@1588
   291
  in traced_tac tf end;
paulson@1588
   292
paulson@1588
   293
(*Ordinary ASTAR, with no initial tactic*)
paulson@1588
   294
val ASTAR = THEN_ASTAR all_tac;
paulson@1588
   295
paulson@1588
   296
end;
paulson@1588
   297
paulson@1588
   298
open Search;