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