src/Pure/search.ML
author wenzelm
Mon Mar 10 15:04:01 2014 +0100 (2014-03-10)
changeset 56026 893fe12639bc
parent 55171 dc7a6f6be01b
child 59058 a78612c67ec0
permissions -rw-r--r--
tuned signature -- prefer Name_Space.get with its builtin error;
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
blanchet@55171
   127
             (if !trace_DEPTH_FIRST then
blanchet@55171
   128
                  tracing (string_of_int (!countr) ^
blanchet@55171
   129
                           " inferences so far.  Giving up at " ^
blanchet@55171
   130
                           string_of_int bnd)
blanchet@55171
   131
              else ();
wenzelm@23178
   132
              NONE)
paulson@22025
   133
          else
blanchet@55171
   134
             (if !trace_DEPTH_FIRST then
blanchet@55171
   135
                  tracing (string_of_int (!countr) ^
blanchet@55171
   136
                           " inferences so far.  Searching to depth " ^
blanchet@55171
   137
                           string_of_int bnd)
blanchet@55171
   138
              else ();
wenzelm@23178
   139
              (*larger increments make it run slower for the hard problems*)
wenzelm@23178
   140
              depth (bnd+inc, 10)) [(0, 1, false, qs0)]
paulson@1588
   141
       | depth (bnd,inc) ((k,np,rgd,q)::qs) =
wenzelm@23178
   142
          if k>=bnd then depth (bnd,inc) qs
paulson@1588
   143
          else
wenzelm@32738
   144
          case (Unsynchronized.inc countr;
wenzelm@23178
   145
                if !trace_DEPTH_FIRST then
wenzelm@23178
   146
                    tracing (string_of_int np ^ implode (map (fn _ => "*") qs))
wenzelm@23178
   147
                else ();
wenzelm@23178
   148
                Seq.pull q) of
wenzelm@23178
   149
             NONE         => depth (bnd,inc) qs
wenzelm@23178
   150
           | SOME(st,stq) =>
wenzelm@23178
   151
               if satp st       (*solution!*)
wenzelm@23178
   152
               then SOME(st, Seq.make
wenzelm@23178
   153
                         (fn()=> depth (bnd,inc) ((k,np,rgd,stq)::qs)))
paulson@1588
   154
wenzelm@23178
   155
               else
paulson@1588
   156
               let val np' = nprems_of st
wenzelm@23178
   157
                     (*rgd' calculation assumes tactic operates on subgoal 1*)
paulson@1588
   158
                   val rgd' = not (has_vars (hd (prems_of st)))
paulson@1588
   159
                   val k' = k+np'-np+1  (*difference in # of subgoals, +1*)
wenzelm@23178
   160
               in  if k'+np' >= bnd
wenzelm@23178
   161
                   then depth (bnd, Int.min(inc, k'+np'+1-bnd)) qs
wenzelm@23178
   162
                   else if np' < np (*solved a subgoal; prune rigid ancestors*)
wenzelm@23178
   163
                   then depth (bnd,inc)
wenzelm@23178
   164
                         (prune ((k', np', rgd', tf st), (k,np,rgd,stq) :: qs))
wenzelm@23178
   165
                   else depth (bnd,inc) ((k', np', rgd', tf st) ::
wenzelm@23178
   166
                                         (k,np,rgd,stq) :: qs)
wenzelm@23178
   167
               end
paulson@1588
   168
  in depth (0,5) [] end);
paulson@1588
   169
wenzelm@38802
   170
fun ITER_DEEPEN lim = THEN_ITER_DEEPEN lim all_tac;
paulson@1588
   171
paulson@1588
   172
paulson@2869
   173
(*Simple iterative deepening tactical.  It merely "deepens" any search tactic
paulson@2869
   174
  using increment "inc" up to limit "lim". *)
wenzelm@33335
   175
val trace_DEEPEN = Unsynchronized.ref false;
wenzelm@33335
   176
wenzelm@38802
   177
fun DEEPEN (inc, lim) tacf m i =
wenzelm@33380
   178
  let
wenzelm@33380
   179
    fun dpn m st =
wenzelm@33380
   180
      st |>
wenzelm@33380
   181
       (if has_fewer_prems i st then no_tac
wenzelm@38802
   182
        else if m > lim then
wenzelm@33380
   183
          (if !trace_DEEPEN then tracing "Search depth limit exceeded: giving up" else ();
wenzelm@33380
   184
            no_tac)
wenzelm@33380
   185
        else
wenzelm@33380
   186
          (if !trace_DEEPEN then tracing ("Search depth = " ^ string_of_int m) else ();
wenzelm@33380
   187
            tacf m i  ORELSE  dpn (m+inc)))
paulson@2869
   188
  in  dpn m  end;
wenzelm@23178
   189
wenzelm@32940
   190
paulson@1588
   191
(*** Best-first search ***)
paulson@1588
   192
wenzelm@32940
   193
(*total ordering on theorems, allowing duplicates to be found*)
wenzelm@32940
   194
structure Thm_Heap = Heap
wenzelm@32940
   195
(
wenzelm@32940
   196
  type elem = int * thm;
wenzelm@35408
   197
  val ord = prod_ord int_ord (Term_Ord.term_ord o pairself Thm.prop_of);
wenzelm@32940
   198
);
wenzelm@32940
   199
wenzelm@32738
   200
val trace_BEST_FIRST = Unsynchronized.ref false;
paulson@1588
   201
paulson@1588
   202
(*For creating output sequence*)
skalberg@15531
   203
fun some_of_list []     = NONE
skalberg@15531
   204
  | some_of_list (x::l) = SOME (x, Seq.make (fn () => some_of_list l));
paulson@1588
   205
paulson@9094
   206
(*Check for and delete duplicate proof states*)
wenzelm@32940
   207
fun delete_all_min prf heap =
wenzelm@32940
   208
  if Thm_Heap.is_empty heap then heap
wenzelm@32940
   209
  else if Thm.eq_thm (prf, #2 (Thm_Heap.min heap))
wenzelm@32940
   210
  then delete_all_min prf (Thm_Heap.delete_min heap)
wenzelm@32940
   211
  else heap;
paulson@1588
   212
paulson@1588
   213
(*Best-first search for a state that satisfies satp (incl initial state)
paulson@1588
   214
  Function sizef estimates size of problem remaining (smaller means better).
paulson@1588
   215
  tactic tac0 sets up the initial priority queue, while tac1 searches it. *)
wenzelm@23178
   216
fun THEN_BEST_FIRST tac0 (satp, sizef) tac1 =
paulson@1588
   217
  let val tac = tracify trace_BEST_FIRST tac1
paulson@1588
   218
      fun pairsize th = (sizef th, th);
paulson@9094
   219
      fun bfs (news,nprf_heap) =
wenzelm@23178
   220
           (case  List.partition satp news  of
wenzelm@32940
   221
                ([],nonsats) => next(fold_rev Thm_Heap.insert (map pairsize nonsats) nprf_heap)
wenzelm@23178
   222
              | (sats,_)  => some_of_list sats)
paulson@9094
   223
      and next nprf_heap =
wenzelm@32940
   224
            if Thm_Heap.is_empty nprf_heap then NONE
wenzelm@23178
   225
            else
wenzelm@32940
   226
            let val (n,prf) = Thm_Heap.min nprf_heap
wenzelm@23178
   227
            in if !trace_BEST_FIRST
wenzelm@23178
   228
               then tracing("state size = " ^ string_of_int n)
paulson@1588
   229
               else ();
wenzelm@23178
   230
               bfs (Seq.list_of (tac prf),
wenzelm@32940
   231
                    delete_all_min prf (Thm_Heap.delete_min nprf_heap))
paulson@9094
   232
            end
wenzelm@32940
   233
      fun btac st = bfs (Seq.list_of (tac0 st), Thm_Heap.empty)
paulson@1588
   234
  in traced_tac btac end;
paulson@1588
   235
paulson@1588
   236
(*Ordinary best-first search, with no initial tactic*)
paulson@1588
   237
val BEST_FIRST = THEN_BEST_FIRST all_tac;
paulson@1588
   238
wenzelm@23178
   239
(*Breadth-first search to satisfy satpred (including initial state)
paulson@1588
   240
  SLOW -- SHOULD NOT USE APPEND!*)
wenzelm@23178
   241
fun gen_BREADTH_FIRST message satpred (tac:tactic) =
wenzelm@4270
   242
  let val tacf = Seq.list_of o tac;
paulson@1588
   243
      fun bfs prfs =
wenzelm@23178
   244
         (case  List.partition satpred prfs  of
wenzelm@23178
   245
              ([],[]) => []
wenzelm@23178
   246
            | ([],nonsats) =>
wenzelm@23178
   247
                  (message("breadth=" ^ string_of_int(length nonsats));
wenzelm@23178
   248
                   bfs (maps tacf nonsats))
wenzelm@23178
   249
            | (sats,_)  => sats)
wenzelm@4270
   250
  in (fn st => Seq.of_list (bfs [st])) end;
paulson@1588
   251
wenzelm@12262
   252
val BREADTH_FIRST = gen_BREADTH_FIRST tracing;
wenzelm@5693
   253
val QUIET_BREADTH_FIRST = gen_BREADTH_FIRST (K ());
wenzelm@5693
   254
paulson@1588
   255
wenzelm@23178
   256
(*  Author:     Norbert Voelker, FernUniversitaet Hagen
paulson@1588
   257
    Remarks:    Implementation of A*-like proof procedure by modification
wenzelm@23178
   258
                of the existing code for BEST_FIRST and best_tac so that the
wenzelm@23178
   259
                current level of search is taken into account.
wenzelm@23178
   260
*)
paulson@1588
   261
paulson@1588
   262
(*Insertion into priority queue of states, marked with level *)
wenzelm@48263
   263
fun insert_with_level (lnth: int*int*thm) [] = [lnth]
wenzelm@48263
   264
  | insert_with_level (l,m,th) ((l',n,th') :: nths) =
wenzelm@48263
   265
      if  n<m then (l',n,th') :: insert_with_level (l,m,th) nths
wenzelm@22360
   266
      else if  n=m andalso Thm.eq_thm(th,th')
paulson@1588
   267
              then (l',n,th')::nths
paulson@1588
   268
              else (l,m,th)::(l',n,th')::nths;
paulson@1588
   269
paulson@1588
   270
(*For creating output sequence*)
skalberg@15531
   271
fun some_of_list []     = NONE
skalberg@15531
   272
  | some_of_list (x::l) = SOME (x, Seq.make (fn () => some_of_list l));
paulson@1588
   273
wenzelm@32738
   274
val trace_ASTAR = Unsynchronized.ref false;
paulson@1588
   275
wenzelm@23178
   276
fun THEN_ASTAR tac0 (satp, costf) tac1 =
wenzelm@23178
   277
  let val tf = tracify trace_ASTAR tac1;
paulson@1588
   278
      fun bfs (news,nprfs,level) =
paulson@1588
   279
      let fun cost thm = (level, costf level thm, thm)
skalberg@15570
   280
      in (case  List.partition satp news  of
wenzelm@23178
   281
            ([],nonsats)
wenzelm@48263
   282
                 => next (fold_rev (insert_with_level o cost) nonsats nprfs)
paulson@1588
   283
          | (sats,_)  => some_of_list sats)
wenzelm@23178
   284
      end and
skalberg@15531
   285
      next []  = NONE
paulson@1588
   286
        | next ((level,n,prf)::nprfs)  =
wenzelm@23178
   287
            (if !trace_ASTAR
wenzelm@12262
   288
               then tracing("level = " ^ string_of_int level ^
wenzelm@23178
   289
                         "  cost = " ^ string_of_int n ^
wenzelm@23178
   290
                         "  queue length =" ^ string_of_int (length nprfs))
paulson@1588
   291
               else ();
wenzelm@4270
   292
             bfs (Seq.list_of (tf prf), nprfs,level+1))
wenzelm@4270
   293
      fun tf st = bfs (Seq.list_of (tac0 st), [], 0)
paulson@1588
   294
  in traced_tac tf end;
paulson@1588
   295
paulson@1588
   296
(*Ordinary ASTAR, with no initial tactic*)
paulson@1588
   297
val ASTAR = THEN_ASTAR all_tac;
paulson@1588
   298
paulson@1588
   299
end;
paulson@1588
   300
paulson@1588
   301
open Search;