src/HOL/Tools/meson.ML
 author berghofe Mon Jan 29 13:26:04 2001 +0100 (2001-01-29 ago) changeset 10988 e0016a009c17 parent 10821 dcb75538f542 child 12299 2c76042c3b06 permissions -rw-r--r--
Splitting of arguments of product types in induction rules is now less
aggressive.
```     1 (*  Title:      HOL/Tools/meson.ML
```
```     2     ID:         \$Id\$
```
```     3     Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
```
```     4     Copyright   1992  University of Cambridge
```
```     5
```
```     6 The MESON resolution proof procedure for HOL.
```
```     7
```
```     8 When making clauses, avoids using the rewriter -- instead uses RS recursively
```
```     9
```
```    10 NEED TO SORT LITERALS BY # OF VARS, USING ==>I/E.  ELIMINATES NEED FOR
```
```    11 FUNCTION nodups -- if done to goal clauses too!
```
```    12 *)
```
```    13
```
```    14 local
```
```    15
```
```    16  (*Prove theorems using fast_tac*)
```
```    17  fun prove_fun s =
```
```    18      prove_goal (the_context ()) s
```
```    19           (fn prems => [ cut_facts_tac prems 1, Fast_tac 1 ]);
```
```    20
```
```    21  (**** Negation Normal Form ****)
```
```    22
```
```    23  (*** de Morgan laws ***)
```
```    24
```
```    25  val not_conjD = prove_fun "~(P&Q) ==> ~P | ~Q";
```
```    26  val not_disjD = prove_fun "~(P|Q) ==> ~P & ~Q";
```
```    27  val not_notD = prove_fun "~~P ==> P";
```
```    28  val not_allD = prove_fun  "~(ALL x. P(x)) ==> EX x. ~P(x)";
```
```    29  val not_exD = prove_fun   "~(EX x. P(x)) ==> ALL x. ~P(x)";
```
```    30
```
```    31
```
```    32  (*** Removal of --> and <-> (positive and negative occurrences) ***)
```
```    33
```
```    34  val imp_to_disjD = prove_fun "P-->Q ==> ~P | Q";
```
```    35  val not_impD = prove_fun   "~(P-->Q) ==> P & ~Q";
```
```    36
```
```    37  val iff_to_disjD = prove_fun "P=Q ==> (~P | Q) & (~Q | P)";
```
```    38
```
```    39  (*Much more efficient than (P & ~Q) | (Q & ~P) for computing CNF*)
```
```    40  val not_iffD = prove_fun "~(P=Q) ==> (P | Q) & (~P | ~Q)";
```
```    41
```
```    42
```
```    43  (**** Pulling out the existential quantifiers ****)
```
```    44
```
```    45  (*** Conjunction ***)
```
```    46
```
```    47  val conj_exD1 = prove_fun "(EX x. P(x)) & Q ==> EX x. P(x) & Q";
```
```    48  val conj_exD2 = prove_fun "P & (EX x. Q(x)) ==> EX x. P & Q(x)";
```
```    49
```
```    50  (*** Disjunction ***)
```
```    51
```
```    52  (*DO NOT USE with forall-Skolemization: makes fewer schematic variables!!
```
```    53    With ex-Skolemization, makes fewer Skolem constants*)
```
```    54  val disj_exD = prove_fun "(EX x. P(x)) | (EX x. Q(x)) ==> EX x. P(x) | Q(x)";
```
```    55
```
```    56  val disj_exD1 = prove_fun "(EX x. P(x)) | Q ==> EX x. P(x) | Q";
```
```    57  val disj_exD2 = prove_fun "P | (EX x. Q(x)) ==> EX x. P | Q(x)";
```
```    58
```
```    59
```
```    60
```
```    61  (***** Generating clauses for the Meson Proof Procedure *****)
```
```    62
```
```    63  (*** Disjunctions ***)
```
```    64
```
```    65  val disj_assoc = prove_fun "(P|Q)|R ==> P|(Q|R)";
```
```    66
```
```    67  val disj_comm = prove_fun "P|Q ==> Q|P";
```
```    68
```
```    69  val disj_FalseD1 = prove_fun "False|P ==> P";
```
```    70  val disj_FalseD2 = prove_fun "P|False ==> P";
```
```    71
```
```    72
```
```    73  (**** Operators for forward proof ****)
```
```    74
```
```    75  (*raises exception if no rules apply -- unlike RL*)
```
```    76  fun tryres (th, rl::rls) = (th RS rl handle THM _ => tryres(th,rls))
```
```    77    | tryres (th, []) = raise THM("tryres", 0, [th]);
```
```    78
```
```    79  val prop_of = #prop o rep_thm;
```
```    80
```
```    81  (*Permits forward proof from rules that discharge assumptions*)
```
```    82  fun forward_res nf st =
```
```    83    case Seq.pull (ALLGOALS (METAHYPS (fn [prem] => rtac (nf prem) 1)) st)
```
```    84    of Some(th,_) => th
```
```    85     | None => raise THM("forward_res", 0, [st]);
```
```    86
```
```    87
```
```    88  (*Are any of the constants in "bs" present in the term?*)
```
```    89  fun has_consts bs =
```
```    90    let fun has (Const(a,_)) = a mem bs
```
```    91          | has (f\$u) = has f orelse has u
```
```    92          | has (Abs(_,_,t)) = has t
```
```    93          | has _ = false
```
```    94    in  has  end;
```
```    95
```
```    96
```
```    97  (**** Clause handling ****)
```
```    98
```
```    99  fun literals (Const("Trueprop",_) \$ P) = literals P
```
```   100    | literals (Const("op |",_) \$ P \$ Q) = literals P @ literals Q
```
```   101    | literals (Const("Not",_) \$ P) = [(false,P)]
```
```   102    | literals P = [(true,P)];
```
```   103
```
```   104  (*number of literals in a term*)
```
```   105  val nliterals = length o literals;
```
```   106
```
```   107  (*to detect, and remove, tautologous clauses*)
```
```   108  fun taut_lits [] = false
```
```   109    | taut_lits ((flg,t)::ts) = (not flg,t) mem ts orelse taut_lits ts;
```
```   110
```
```   111  (*Include False as a literal: an occurrence of ~False is a tautology*)
```
```   112  fun is_taut th = taut_lits ((true, HOLogic.false_const) ::
```
```   113                              literals (prop_of th));
```
```   114
```
```   115  (*Generation of unique names -- maxidx cannot be relied upon to increase!
```
```   116    Cannot rely on "variant", since variables might coincide when literals
```
```   117    are joined to make a clause...
```
```   118    19 chooses "U" as the first variable name*)
```
```   119  val name_ref = ref 19;
```
```   120
```
```   121  (*Replaces universally quantified variables by FREE variables -- because
```
```   122    assumptions may not contain scheme variables.  Later, call "generalize". *)
```
```   123  fun freeze_spec th =
```
```   124    let val sth = th RS spec
```
```   125        val newname = (name_ref := !name_ref + 1;
```
```   126                       radixstring(26, "A", !name_ref))
```
```   127    in  read_instantiate [("x", newname)] sth  end;
```
```   128
```
```   129  fun resop nf [prem] = resolve_tac (nf prem) 1;
```
```   130
```
```   131  (*Conjunctive normal form, detecting tautologies early.
```
```   132    Strips universal quantifiers and breaks up conjunctions. *)
```
```   133  fun cnf_aux seen (th,ths) =
```
```   134    if taut_lits (literals(prop_of th) @ seen)  then ths
```
```   135    else if not (has_consts ["All","op &"] (prop_of th))  then th::ths
```
```   136    else (*conjunction?*)
```
```   137          cnf_aux seen (th RS conjunct1,
```
```   138                        cnf_aux seen (th RS conjunct2, ths))
```
```   139    handle THM _ => (*universal quant?*)
```
```   140          cnf_aux  seen (freeze_spec th,  ths)
```
```   141    handle THM _ => (*disjunction?*)
```
```   142      let val tac =
```
```   143          (METAHYPS (resop (cnf_nil seen)) 1) THEN
```
```   144          (fn st' => st' |>
```
```   145                  METAHYPS (resop (cnf_nil (literals (concl_of st') @ seen))) 1)
```
```   146      in  Seq.list_of (tac (th RS disj_forward)) @ ths  end
```
```   147  and cnf_nil seen th = cnf_aux seen (th,[]);
```
```   148
```
```   149  (*Top-level call to cnf -- it's safe to reset name_ref*)
```
```   150  fun cnf (th,ths) =
```
```   151     (name_ref := 19;  cnf (th RS conjunct1, cnf (th RS conjunct2, ths))
```
```   152      handle THM _ => (*not a conjunction*) cnf_aux [] (th, ths));
```
```   153
```
```   154  (**** Removal of duplicate literals ****)
```
```   155
```
```   156  (*Forward proof, passing extra assumptions as theorems to the tactic*)
```
```   157  fun forward_res2 nf hyps st =
```
```   158    case Seq.pull
```
```   159          (REPEAT
```
```   160           (METAHYPS (fn major::minors => rtac (nf (minors@hyps) major) 1) 1)
```
```   161           st)
```
```   162    of Some(th,_) => th
```
```   163     | None => raise THM("forward_res2", 0, [st]);
```
```   164
```
```   165  (*Remove duplicates in P|Q by assuming ~P in Q
```
```   166    rls (initially []) accumulates assumptions of the form P==>False*)
```
```   167  fun nodups_aux rls th = nodups_aux rls (th RS disj_assoc)
```
```   168      handle THM _ => tryres(th,rls)
```
```   169      handle THM _ => tryres(forward_res2 nodups_aux rls (th RS disj_forward2),
```
```   170                             [disj_FalseD1, disj_FalseD2, asm_rl])
```
```   171      handle THM _ => th;
```
```   172
```
```   173  (*Remove duplicate literals, if there are any*)
```
```   174  fun nodups th =
```
```   175      if null(findrep(literals(prop_of th))) then th
```
```   176      else nodups_aux [] th;
```
```   177
```
```   178
```
```   179  (**** Generation of contrapositives ****)
```
```   180
```
```   181  (*Associate disjuctions to right -- make leftmost disjunct a LITERAL*)
```
```   182  fun assoc_right th = assoc_right (th RS disj_assoc)
```
```   183          handle THM _ => th;
```
```   184
```
```   185  (*Must check for negative literal first!*)
```
```   186  val clause_rules = [disj_assoc, make_neg_rule, make_pos_rule];
```
```   187
```
```   188  (*For Plaisted's postive refinement.  [currently unused] *)
```
```   189  val refined_clause_rules = [disj_assoc, make_refined_neg_rule, make_pos_rule];
```
```   190
```
```   191  (*Create a goal or support clause, conclusing False*)
```
```   192  fun make_goal th =   (*Must check for negative literal first!*)
```
```   193      make_goal (tryres(th, clause_rules))
```
```   194    handle THM _ => tryres(th, [make_neg_goal, make_pos_goal]);
```
```   195
```
```   196  (*Sort clauses by number of literals*)
```
```   197  fun fewerlits(th1,th2) = nliterals(prop_of th1) < nliterals(prop_of th2);
```
```   198
```
```   199  (*TAUTOLOGY CHECK SHOULD NOT BE NECESSARY!*)
```
```   200  fun sort_clauses ths = sort (make_ord fewerlits) (filter (not o is_taut) ths);
```
```   201
```
```   202  (*Convert all suitable free variables to schematic variables*)
```
```   203  fun generalize th = forall_elim_vars 0 (forall_intr_frees th);
```
```   204
```
```   205  (*Create a meta-level Horn clause*)
```
```   206  fun make_horn crules th = make_horn crules (tryres(th,crules))
```
```   207                            handle THM _ => th;
```
```   208
```
```   209  (*Generate Horn clauses for all contrapositives of a clause*)
```
```   210  fun add_contras crules (th,hcs) =
```
```   211    let fun rots (0,th) = hcs
```
```   212          | rots (k,th) = zero_var_indexes (make_horn crules th) ::
```
```   213                          rots(k-1, assoc_right (th RS disj_comm))
```
```   214    in case nliterals(prop_of th) of
```
```   215          1 => th::hcs
```
```   216        | n => rots(n, assoc_right th)
```
```   217    end;
```
```   218
```
```   219  (*Use "theorem naming" to label the clauses*)
```
```   220  fun name_thms label =
```
```   221      let fun name1 (th, (k,ths)) =
```
```   222            (k-1, Thm.name_thm (label ^ string_of_int k, th) :: ths)
```
```   223
```
```   224      in  fn ths => #2 (foldr name1 (ths, (length ths, [])))  end;
```
```   225
```
```   226  (*Find an all-negative support clause*)
```
```   227  fun is_negative th = forall (not o #1) (literals (prop_of th));
```
```   228
```
```   229  val neg_clauses = filter is_negative;
```
```   230
```
```   231
```
```   232  (***** MESON PROOF PROCEDURE *****)
```
```   233
```
```   234  fun rhyps (Const("==>",_) \$ (Const("Trueprop",_) \$ A) \$ phi,
```
```   235             As) = rhyps(phi, A::As)
```
```   236    | rhyps (_, As) = As;
```
```   237
```
```   238  (** Detecting repeated assumptions in a subgoal **)
```
```   239
```
```   240  (*The stringtree detects repeated assumptions.*)
```
```   241  fun ins_term (net,t) = Net.insert_term((t,t), net, op aconv);
```
```   242
```
```   243  (*detects repetitions in a list of terms*)
```
```   244  fun has_reps [] = false
```
```   245    | has_reps [_] = false
```
```   246    | has_reps [t,u] = (t aconv u)
```
```   247    | has_reps ts = (foldl ins_term (Net.empty, ts);  false)
```
```   248                    handle INSERT => true;
```
```   249
```
```   250  (*Like TRYALL eq_assume_tac, but avoids expensive THEN calls*)
```
```   251  fun TRYALL_eq_assume_tac 0 st = Seq.single st
```
```   252    | TRYALL_eq_assume_tac i st =
```
```   253         TRYALL_eq_assume_tac (i-1) (eq_assumption i st)
```
```   254         handle THM _ => TRYALL_eq_assume_tac (i-1) st;
```
```   255
```
```   256  (*Loop checking: FAIL if trying to prove the same thing twice
```
```   257    -- if *ANY* subgoal has repeated literals*)
```
```   258  fun check_tac st =
```
```   259    if exists (fn prem => has_reps (rhyps(prem,[]))) (prems_of st)
```
```   260    then  Seq.empty  else  Seq.single st;
```
```   261
```
```   262
```
```   263  (* net_resolve_tac actually made it slower... *)
```
```   264  fun prolog_step_tac horns i =
```
```   265      (assume_tac i APPEND resolve_tac horns i) THEN check_tac THEN
```
```   266      TRYALL eq_assume_tac;
```
```   267
```
```   268
```
```   269 in
```
```   270
```
```   271
```
```   272 (*Sums the sizes of the subgoals, ignoring hypotheses (ancestors)*)
```
```   273 local fun addconcl(prem,sz) = size_of_term(Logic.strip_assums_concl prem) + sz
```
```   274 in
```
```   275 fun size_of_subgoals st = foldr addconcl (prems_of st, 0)
```
```   276 end;
```
```   277
```
```   278 (*Negation Normal Form*)
```
```   279 val nnf_rls = [imp_to_disjD, iff_to_disjD, not_conjD, not_disjD,
```
```   280                not_impD, not_iffD, not_allD, not_exD, not_notD];
```
```   281 fun make_nnf th = make_nnf (tryres(th, nnf_rls))
```
```   282     handle THM _ =>
```
```   283         forward_res make_nnf
```
```   284            (tryres(th, [conj_forward,disj_forward,all_forward,ex_forward]))
```
```   285     handle THM _ => th;
```
```   286
```
```   287 (*Pull existential quantifiers (Skolemization)*)
```
```   288 fun skolemize th =
```
```   289   if not (has_consts ["Ex"] (prop_of th)) then th
```
```   290   else skolemize (tryres(th, [choice, conj_exD1, conj_exD2,
```
```   291                               disj_exD, disj_exD1, disj_exD2]))
```
```   292     handle THM _ =>
```
```   293         skolemize (forward_res skolemize
```
```   294                    (tryres (th, [conj_forward, disj_forward, all_forward])))
```
```   295     handle THM _ => forward_res skolemize (th RS ex_forward);
```
```   296
```
```   297
```
```   298 (*Make clauses from a list of theorems, previously Skolemized and put into nnf.
```
```   299   The resulting clauses are HOL disjunctions.*)
```
```   300 fun make_clauses ths =
```
```   301     sort_clauses (map (generalize o nodups) (foldr cnf (ths,[])));
```
```   302
```
```   303 (*Convert a list of clauses to (contrapositive) Horn clauses*)
```
```   304 fun make_horns ths =
```
```   305     name_thms "Horn#"
```
```   306       (gen_distinct eq_thm (foldr (add_contras clause_rules) (ths,[])));
```
```   307
```
```   308 (*Could simply use nprems_of, which would count remaining subgoals -- no
```
```   309   discrimination as to their size!  With BEST_FIRST, fails for problem 41.*)
```
```   310
```
```   311 fun best_prolog_tac sizef horns =
```
```   312     BEST_FIRST (has_fewer_prems 1, sizef) (prolog_step_tac horns 1);
```
```   313
```
```   314 fun depth_prolog_tac horns =
```
```   315     DEPTH_FIRST (has_fewer_prems 1) (prolog_step_tac horns 1);
```
```   316
```
```   317 (*Return all negative clauses, as possible goal clauses*)
```
```   318 fun gocls cls = name_thms "Goal#" (map make_goal (neg_clauses cls));
```
```   319
```
```   320
```
```   321 fun skolemize_tac prems =
```
```   322     cut_facts_tac (map (skolemize o make_nnf) prems)  THEN'
```
```   323     REPEAT o (etac exE);
```
```   324
```
```   325 (*Shell of all meson-tactics.  Supplies cltac with clauses: HOL disjunctions*)
```
```   326 fun MESON cltac = SELECT_GOAL
```
```   327  (EVERY1 [rtac ccontr,
```
```   328           METAHYPS (fn negs =>
```
```   329                     EVERY1 [skolemize_tac negs,
```
```   330                             METAHYPS (cltac o make_clauses)])]);
```
```   331
```
```   332 (** Best-first search versions **)
```
```   333
```
```   334 fun best_meson_tac sizef =
```
```   335   MESON (fn cls =>
```
```   336          THEN_BEST_FIRST (resolve_tac (gocls cls) 1)
```
```   337                          (has_fewer_prems 1, sizef)
```
```   338                          (prolog_step_tac (make_horns cls) 1));
```
```   339
```
```   340 (*First, breaks the goal into independent units*)
```
```   341 val safe_best_meson_tac =
```
```   342      SELECT_GOAL (TRY Safe_tac THEN
```
```   343                   TRYALL (best_meson_tac size_of_subgoals));
```
```   344
```
```   345 (** Depth-first search version **)
```
```   346
```
```   347 val depth_meson_tac =
```
```   348      MESON (fn cls => EVERY [resolve_tac (gocls cls) 1,
```
```   349                              depth_prolog_tac (make_horns cls)]);
```
```   350
```
```   351
```
```   352
```
```   353 (** Iterative deepening version **)
```
```   354
```
```   355 (*This version does only one inference per call;
```
```   356   having only one eq_assume_tac speeds it up!*)
```
```   357 fun prolog_step_tac' horns =
```
```   358     let val (horn0s, hornps) = (*0 subgoals vs 1 or more*)
```
```   359             take_prefix Thm.no_prems horns
```
```   360         val nrtac = net_resolve_tac horns
```
```   361     in  fn i => eq_assume_tac i ORELSE
```
```   362                 match_tac horn0s i ORELSE  (*no backtracking if unit MATCHES*)
```
```   363                 ((assume_tac i APPEND nrtac i) THEN check_tac)
```
```   364     end;
```
```   365
```
```   366 fun iter_deepen_prolog_tac horns =
```
```   367     ITER_DEEPEN (has_fewer_prems 1) (prolog_step_tac' horns);
```
```   368
```
```   369 val iter_deepen_meson_tac =
```
```   370   MESON (fn cls =>
```
```   371          (THEN_ITER_DEEPEN (resolve_tac (gocls cls) 1)
```
```   372                            (has_fewer_prems 1)
```
```   373                            (prolog_step_tac' (make_horns cls))));
```
```   374
```
```   375 fun meson_claset_tac cs =
```
```   376   SELECT_GOAL (TRY (safe_tac cs) THEN TRYALL iter_deepen_meson_tac);
```
```   377
```
```   378 val meson_tac = CLASET' meson_claset_tac;
```
```   379
```
```   380
```
```   381 (* proof method setup *)
```
```   382
```
```   383 local
```
```   384
```
```   385 fun meson_meth ctxt =
```
```   386   Method.SIMPLE_METHOD' HEADGOAL
```
```   387     (CHANGED_PROP o meson_claset_tac (Classical.get_local_claset ctxt));
```
```   388
```
```   389 in
```
```   390
```
```   391 val meson_setup =
```
```   392  [Method.add_methods
```
```   393   [("meson", Method.ctxt_args meson_meth, "The MESON resolution proof procedure")]];
```
```   394
```
```   395 end;
```
```   396
```
```   397 end;
```