src/Pure/Isar/method.ML
author wenzelm
Mon May 03 14:25:56 2010 +0200 (2010-05-03)
changeset 36610 bafd82950e24
parent 36096 abc6a2ea4b88
child 36950 75b8f26f2f07
permissions -rw-r--r--
renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
     1 (*  Title:      Pure/Isar/method.ML
     2     Author:     Markus Wenzel, TU Muenchen
     3 
     4 Isar proof methods.
     5 *)
     6 
     7 signature BASIC_METHOD =
     8 sig
     9   val FINDGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
    10   val HEADGOAL: (int -> thm -> 'a Seq.seq) -> thm -> 'a Seq.seq
    11   val trace_rules: bool Unsynchronized.ref
    12 end;
    13 
    14 signature METHOD =
    15 sig
    16   include BASIC_METHOD
    17   type method
    18   val apply: (Proof.context -> method) -> Proof.context -> thm list -> cases_tactic
    19   val RAW_METHOD_CASES: (thm list -> cases_tactic) -> method
    20   val RAW_METHOD: (thm list -> tactic) -> method
    21   val METHOD_CASES: (thm list -> cases_tactic) -> method
    22   val METHOD: (thm list -> tactic) -> method
    23   val fail: method
    24   val succeed: method
    25   val insert_tac: thm list -> int -> tactic
    26   val insert: thm list -> method
    27   val insert_facts: method
    28   val SIMPLE_METHOD: tactic -> method
    29   val SIMPLE_METHOD': (int -> tactic) -> method
    30   val SIMPLE_METHOD'': ((int -> tactic) -> tactic) -> (int -> tactic) -> method
    31   val defer: int option -> method
    32   val prefer: int -> method
    33   val cheating: bool -> Proof.context -> method
    34   val intro: thm list -> method
    35   val elim: thm list -> method
    36   val unfold: thm list -> Proof.context -> method
    37   val fold: thm list -> Proof.context -> method
    38   val atomize: bool -> method
    39   val this: method
    40   val fact: thm list -> Proof.context -> method
    41   val assm_tac: Proof.context -> int -> tactic
    42   val all_assm_tac: Proof.context -> tactic
    43   val assumption: Proof.context -> method
    44   val close: bool -> Proof.context -> method
    45   val trace: Proof.context -> thm list -> unit
    46   val rule_tac: thm list -> thm list -> int -> tactic
    47   val some_rule_tac: thm list -> Proof.context -> thm list -> int -> tactic
    48   val intros_tac: thm list -> thm list -> tactic
    49   val try_intros_tac: thm list -> thm list -> tactic
    50   val rule: thm list -> method
    51   val erule: int -> thm list -> method
    52   val drule: int -> thm list -> method
    53   val frule: int -> thm list -> method
    54   val set_tactic: (thm list -> tactic) -> Proof.context -> Proof.context
    55   val tactic: string * Position.T -> Proof.context -> method
    56   val raw_tactic: string * Position.T -> Proof.context -> method
    57   type src = Args.src
    58   datatype text =
    59     Basic of Proof.context -> method |
    60     Source of src |
    61     Source_i of src |
    62     Then of text list |
    63     Orelse of text list |
    64     Try of text |
    65     Repeat1 of text |
    66     SelectGoals of int * text
    67   val primitive_text: (thm -> thm) -> text
    68   val succeed_text: text
    69   val default_text: text
    70   val this_text: text
    71   val done_text: text
    72   val sorry_text: bool -> text
    73   val finish_text: text option * bool -> text
    74   val print_methods: theory -> unit
    75   val intern: theory -> xstring -> string
    76   val defined: theory -> string -> bool
    77   val method: theory -> src -> Proof.context -> method
    78   val method_i: theory -> src -> Proof.context -> method
    79   val syntax: 'a context_parser -> src -> Proof.context -> 'a * Proof.context
    80   val setup: binding -> (Proof.context -> method) context_parser -> string -> theory -> theory
    81   val method_setup: bstring * Position.T -> Symbol_Pos.text * Position.T -> string ->
    82     theory -> theory
    83   type modifier = (Proof.context -> Proof.context) * attribute
    84   val section: modifier parser list -> thm list context_parser
    85   val sections: modifier parser list -> thm list list context_parser
    86   val parse: text parser
    87 end;
    88 
    89 structure Method: METHOD =
    90 struct
    91 
    92 (** generic tools **)
    93 
    94 (* goal addressing *)
    95 
    96 fun FINDGOAL tac st =
    97   let fun find i n = if i > n then Seq.fail else Seq.APPEND (tac i, find (i + 1) n)
    98   in find 1 (Thm.nprems_of st) st end;
    99 
   100 fun HEADGOAL tac = tac 1;
   101 
   102 
   103 
   104 (** proof methods **)
   105 
   106 (* datatype method *)
   107 
   108 datatype method = Meth of thm list -> cases_tactic;
   109 
   110 fun apply meth ctxt = let val Meth m = meth ctxt in m end;
   111 
   112 val RAW_METHOD_CASES = Meth;
   113 
   114 fun RAW_METHOD tac = RAW_METHOD_CASES (NO_CASES o tac);
   115 
   116 fun METHOD_CASES tac = RAW_METHOD_CASES (fn facts =>
   117   Seq.THEN (ALLGOALS Goal.conjunction_tac, tac facts));
   118 
   119 fun METHOD tac = RAW_METHOD (fn facts => ALLGOALS Goal.conjunction_tac THEN tac facts);
   120 
   121 val fail = METHOD (K no_tac);
   122 val succeed = METHOD (K all_tac);
   123 
   124 
   125 (* insert facts *)
   126 
   127 local
   128 
   129 fun cut_rule_tac rule =
   130   Tactic.rtac (Drule.forall_intr_vars rule COMP_INCR revcut_rl);
   131 
   132 in
   133 
   134 fun insert_tac [] i = all_tac
   135   | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
   136 
   137 val insert_facts = METHOD (ALLGOALS o insert_tac);
   138 fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
   139 
   140 fun SIMPLE_METHOD tac = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN tac);
   141 fun SIMPLE_METHOD'' quant tac = METHOD (fn facts => quant (insert_tac facts THEN' tac));
   142 val SIMPLE_METHOD' = SIMPLE_METHOD'' HEADGOAL;
   143 
   144 end;
   145 
   146 
   147 (* shuffle subgoals *)
   148 
   149 fun prefer i = METHOD (K (Tactic.defer_tac i THEN PRIMITIVE (Thm.permute_prems 0 ~1)));
   150 fun defer opt_i = METHOD (K (Tactic.defer_tac (the_default 1 opt_i)));
   151 
   152 
   153 (* cheating *)
   154 
   155 fun cheating int ctxt =
   156   if int orelse ! quick_and_dirty then
   157     METHOD (K (Skip_Proof.cheat_tac (ProofContext.theory_of ctxt)))
   158   else error "Cheating requires quick_and_dirty mode!";
   159 
   160 
   161 (* unfold intro/elim rules *)
   162 
   163 fun intro ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.match_tac ths));
   164 fun elim ths = SIMPLE_METHOD' (CHANGED_PROP o REPEAT_ALL_NEW (Tactic.ematch_tac ths));
   165 
   166 
   167 (* unfold/fold definitions *)
   168 
   169 fun unfold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (Local_Defs.unfold_tac ctxt ths));
   170 fun fold_meth ths ctxt = SIMPLE_METHOD (CHANGED_PROP (Local_Defs.fold_tac ctxt ths));
   171 
   172 
   173 (* atomize rule statements *)
   174 
   175 fun atomize false = SIMPLE_METHOD' (CHANGED_PROP o Object_Logic.atomize_prems_tac)
   176   | atomize true = RAW_METHOD (K (HEADGOAL (CHANGED_PROP o Object_Logic.full_atomize_tac)));
   177 
   178 
   179 (* this -- resolve facts directly *)
   180 
   181 val this = METHOD (EVERY o map (HEADGOAL o Tactic.rtac));
   182 
   183 
   184 (* fact -- composition by facts from context *)
   185 
   186 fun fact [] ctxt = SIMPLE_METHOD' (ProofContext.some_fact_tac ctxt)
   187   | fact rules _ = SIMPLE_METHOD' (ProofContext.fact_tac rules);
   188 
   189 
   190 (* assumption *)
   191 
   192 local
   193 
   194 fun cond_rtac cond rule = SUBGOAL (fn (prop, i) =>
   195   if cond (Logic.strip_assums_concl prop)
   196   then Tactic.rtac rule i else no_tac);
   197 
   198 in
   199 
   200 fun assm_tac ctxt =
   201   assume_tac APPEND'
   202   Goal.assume_rule_tac ctxt APPEND'
   203   cond_rtac (can Logic.dest_equals) Drule.reflexive_thm APPEND'
   204   cond_rtac (can Logic.dest_term) Drule.termI;
   205 
   206 fun all_assm_tac ctxt st = EVERY1 (replicate (Thm.nprems_of st) (assm_tac ctxt)) st;
   207 
   208 fun assumption ctxt = METHOD (HEADGOAL o
   209   (fn [] => assm_tac ctxt
   210     | [fact] => solve_tac [fact]
   211     | _ => K no_tac));
   212 
   213 fun close immed ctxt = METHOD (K
   214   (FILTER Thm.no_prems ((if immed then all_assm_tac ctxt else all_tac) THEN flexflex_tac)));
   215 
   216 end;
   217 
   218 
   219 (* rule etc. -- single-step refinements *)
   220 
   221 val trace_rules = Unsynchronized.ref false;
   222 
   223 fun trace ctxt rules =
   224   if ! trace_rules andalso not (null rules) then
   225     Pretty.big_list "rules:" (map (Display.pretty_thm ctxt) rules)
   226     |> Pretty.string_of |> tracing
   227   else ();
   228 
   229 local
   230 
   231 fun gen_rule_tac tac rules facts =
   232   (fn i => fn st =>
   233     if null facts then tac rules i st
   234     else Seq.maps (fn rule => (tac o single) rule i st) (Drule.multi_resolves facts rules))
   235   THEN_ALL_NEW Goal.norm_hhf_tac;
   236 
   237 fun gen_arule_tac tac j rules facts =
   238   EVERY' (gen_rule_tac tac rules facts :: replicate j Tactic.assume_tac);
   239 
   240 fun gen_some_rule_tac tac arg_rules ctxt facts = SUBGOAL (fn (goal, i) =>
   241   let
   242     val rules =
   243       if not (null arg_rules) then arg_rules
   244       else flat (Context_Rules.find_rules false facts goal ctxt)
   245   in trace ctxt rules; tac rules facts i end);
   246 
   247 fun meth tac x = METHOD (HEADGOAL o tac x);
   248 fun meth' tac x y = METHOD (HEADGOAL o tac x y);
   249 
   250 in
   251 
   252 val rule_tac = gen_rule_tac Tactic.resolve_tac;
   253 val rule = meth rule_tac;
   254 val some_rule_tac = gen_some_rule_tac rule_tac;
   255 val some_rule = meth' some_rule_tac;
   256 
   257 val erule = meth' (gen_arule_tac Tactic.eresolve_tac);
   258 val drule = meth' (gen_arule_tac Tactic.dresolve_tac);
   259 val frule = meth' (gen_arule_tac Tactic.forward_tac);
   260 
   261 end;
   262 
   263 
   264 (* intros_tac -- pervasive search spanned by intro rules *)
   265 
   266 fun gen_intros_tac goals intros facts =
   267   goals (insert_tac facts THEN'
   268       REPEAT_ALL_NEW (resolve_tac intros))
   269     THEN Tactic.distinct_subgoals_tac;
   270 
   271 val intros_tac = gen_intros_tac ALLGOALS;
   272 val try_intros_tac = gen_intros_tac TRYALL;
   273 
   274 (* ML tactics *)
   275 
   276 structure TacticData = Proof_Data
   277 (
   278   type T = thm list -> tactic;
   279   fun init _ = undefined;
   280 );
   281 
   282 val set_tactic = TacticData.put;
   283 
   284 fun ml_tactic (txt, pos) ctxt =
   285   let
   286     val ctxt' = ctxt |> Context.proof_map
   287       (ML_Context.expression pos
   288         "fun tactic (facts: thm list) : tactic"
   289         "Context.map_proof (Method.set_tactic tactic)" txt);
   290   in Context.setmp_thread_data (SOME (Context.Proof ctxt)) (TacticData.get ctxt') end;
   291 
   292 fun tactic txt ctxt = METHOD (ml_tactic txt ctxt);
   293 fun raw_tactic txt ctxt = RAW_METHOD (ml_tactic txt ctxt);
   294 
   295 
   296 
   297 (** method syntax **)
   298 
   299 (* method text *)
   300 
   301 type src = Args.src;
   302 
   303 datatype text =
   304   Basic of Proof.context -> method |
   305   Source of src |
   306   Source_i of src |
   307   Then of text list |
   308   Orelse of text list |
   309   Try of text |
   310   Repeat1 of text |
   311   SelectGoals of int * text;
   312 
   313 fun primitive_text r = Basic (K (SIMPLE_METHOD (PRIMITIVE r)));
   314 val succeed_text = Basic (K succeed);
   315 val default_text = Source (Args.src (("default", []), Position.none));
   316 val this_text = Basic (K this);
   317 val done_text = Basic (K (SIMPLE_METHOD all_tac));
   318 fun sorry_text int = Basic (cheating int);
   319 
   320 fun finish_text (NONE, immed) = Basic (close immed)
   321   | finish_text (SOME txt, immed) = Then [txt, Basic (close immed)];
   322 
   323 
   324 (* method definitions *)
   325 
   326 structure Methods = Theory_Data
   327 (
   328   type T = ((src -> Proof.context -> method) * string) Name_Space.table;
   329   val empty : T = Name_Space.empty_table "method";
   330   val extend = I;
   331   fun merge data : T = Name_Space.merge_tables data;
   332 );
   333 
   334 fun print_methods thy =
   335   let
   336     val meths = Methods.get thy;
   337     fun prt_meth (name, (_, comment)) = Pretty.block
   338       [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   339   in
   340     [Pretty.big_list "methods:" (map prt_meth (Name_Space.extern_table meths))]
   341     |> Pretty.chunks |> Pretty.writeln
   342   end;
   343 
   344 fun add_method name meth comment thy = thy
   345   |> Methods.map (#2 o Name_Space.define true (Sign.naming_of thy) (name, (meth, comment)));
   346 
   347 
   348 (* get methods *)
   349 
   350 val intern = Name_Space.intern o #1 o Methods.get;
   351 val defined = Symtab.defined o #2 o Methods.get;
   352 
   353 fun method_i thy =
   354   let
   355     val meths = #2 (Methods.get thy);
   356     fun meth src =
   357       let val ((name, _), pos) = Args.dest_src src in
   358         (case Symtab.lookup meths name of
   359           NONE => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
   360         | SOME (mth, _) => (Position.report (Markup.method name) pos; mth src))
   361       end;
   362   in meth end;
   363 
   364 fun method thy = method_i thy o Args.map_name (Name_Space.intern (#1 (Methods.get thy)));
   365 
   366 
   367 (* method setup *)
   368 
   369 fun syntax scan = Args.context_syntax "method" scan;
   370 
   371 fun setup name scan =
   372   add_method name
   373     (fn src => fn ctxt => let val (m, ctxt') = syntax scan src ctxt in m ctxt' end);
   374 
   375 fun method_setup name (txt, pos) cmt =
   376   Context.theory_map (ML_Context.expression pos
   377     "val (name, scan, comment): binding * (Proof.context -> Proof.method) context_parser * string"
   378     "Context.map_theory (Method.setup name scan comment)"
   379     ("(" ^ ML_Syntax.make_binding name ^ ", " ^ txt ^ ", " ^ ML_Syntax.print_string cmt ^ ")"));
   380 
   381 
   382 
   383 (** concrete syntax **)
   384 
   385 structure P = OuterParse;
   386 
   387 
   388 (* sections *)
   389 
   390 type modifier = (Proof.context -> Proof.context) * attribute;
   391 
   392 local
   393 
   394 fun thms ss = Scan.repeat (Scan.unless (Scan.lift (Scan.first ss)) Attrib.multi_thm) >> flat;
   395 fun app (f, att) (context, ths) = Library.foldl_map att (Context.map_proof f context, ths);
   396 
   397 in
   398 
   399 fun section ss = Scan.depend (fn context => (Scan.first ss -- Scan.pass context (thms ss)) :|--
   400   (fn (m, ths) => Scan.succeed (app m (context, ths))));
   401 
   402 fun sections ss = Scan.repeat (section ss);
   403 
   404 end;
   405 
   406 
   407 (* extra rule methods *)
   408 
   409 fun xrule_meth m =
   410   Scan.lift (Scan.optional (Args.parens OuterParse.nat) 0) -- Attrib.thms >>
   411   (fn (n, ths) => K (m n ths));
   412 
   413 
   414 (* outer parser *)
   415 
   416 fun is_symid_meth s =
   417   s <> "|" andalso s <> "?" andalso s <> "+" andalso OuterLex.ident_or_symbolic s;
   418 
   419 local
   420 
   421 fun meth4 x =
   422  (P.position (P.xname >> rpair []) >> (Source o Args.src) ||
   423   P.$$$ "(" |-- P.!!! (meth0 --| P.$$$ ")")) x
   424 and meth3 x =
   425  (meth4 --| P.$$$ "?" >> Try ||
   426   meth4 --| P.$$$ "+" >> Repeat1 ||
   427   meth4 -- (P.$$$ "[" |-- Scan.optional P.nat 1 --| P.$$$ "]") >> (SelectGoals o swap) ||
   428   meth4) x
   429 and meth2 x =
   430  (P.position (P.xname -- Args.parse1 is_symid_meth) >> (Source o Args.src) ||
   431   meth3) x
   432 and meth1 x = (P.enum1 "," meth2 >> (fn [m] => m | ms => Then ms)) x
   433 and meth0 x = (P.enum1 "|" meth1 >> (fn [m] => m | ms => Orelse ms)) x;
   434 
   435 in val parse = meth3 end;
   436 
   437 
   438 (* theory setup *)
   439 
   440 val _ = Context.>> (Context.map_theory
   441  (setup (Binding.name "fail") (Scan.succeed (K fail)) "force failure" #>
   442   setup (Binding.name "succeed") (Scan.succeed (K succeed)) "succeed" #>
   443   setup (Binding.name "-") (Scan.succeed (K insert_facts))
   444     "do nothing (insert current facts only)" #>
   445   setup (Binding.name "insert") (Attrib.thms >> (K o insert))
   446     "insert theorems, ignoring facts (improper)" #>
   447   setup (Binding.name "intro") (Attrib.thms >> (K o intro))
   448     "repeatedly apply introduction rules" #>
   449   setup (Binding.name "elim") (Attrib.thms >> (K o elim))
   450     "repeatedly apply elimination rules" #>
   451   setup (Binding.name "unfold") (Attrib.thms >> unfold_meth) "unfold definitions" #>
   452   setup (Binding.name "fold") (Attrib.thms >> fold_meth) "fold definitions" #>
   453   setup (Binding.name "atomize") (Args.mode "full" >> (K o atomize))
   454     "present local premises as object-level statements" #>
   455   setup (Binding.name "rule") (Attrib.thms >> some_rule) "apply some intro/elim rule" #>
   456   setup (Binding.name "erule") (xrule_meth erule) "apply rule in elimination manner (improper)" #>
   457   setup (Binding.name "drule") (xrule_meth drule) "apply rule in destruct manner (improper)" #>
   458   setup (Binding.name "frule") (xrule_meth frule) "apply rule in forward manner (improper)" #>
   459   setup (Binding.name "this") (Scan.succeed (K this)) "apply current facts as rules" #>
   460   setup (Binding.name "fact") (Attrib.thms >> fact) "composition by facts from context" #>
   461   setup (Binding.name "assumption") (Scan.succeed assumption)
   462     "proof by assumption, preferring facts" #>
   463   setup (Binding.name "rename_tac") (Args.goal_spec -- Scan.lift (Scan.repeat1 Args.name) >>
   464     (fn (quant, xs) => K (SIMPLE_METHOD'' quant (Tactic.rename_tac xs))))
   465     "rename parameters of goal" #>
   466   setup (Binding.name "rotate_tac") (Args.goal_spec -- Scan.lift (Scan.optional P.int 1) >>
   467     (fn (quant, i) => K (SIMPLE_METHOD'' quant (Tactic.rotate_tac i))))
   468       "rotate assumptions of goal" #>
   469   setup (Binding.name "tactic") (Scan.lift (P.position Args.name) >> tactic)
   470     "ML tactic as proof method" #>
   471   setup (Binding.name "raw_tactic") (Scan.lift (P.position Args.name) >> raw_tactic)
   472     "ML tactic as raw proof method"));
   473 
   474 
   475 (*final declarations of this structure!*)
   476 val unfold = unfold_meth;
   477 val fold = fold_meth;
   478 
   479 end;
   480 
   481 structure Basic_Method: BASIC_METHOD = Method;
   482 open Basic_Method;
   483 
   484 val RAW_METHOD_CASES = Method.RAW_METHOD_CASES;
   485 val RAW_METHOD = Method.RAW_METHOD;
   486 val METHOD_CASES = Method.METHOD_CASES;
   487 val METHOD = Method.METHOD;
   488 val SIMPLE_METHOD = Method.SIMPLE_METHOD;
   489 val SIMPLE_METHOD' = Method.SIMPLE_METHOD';
   490 val SIMPLE_METHOD'' = Method.SIMPLE_METHOD'';
   491