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