src/Pure/Isar/method.ML
author wenzelm
Wed Jan 05 11:43:37 2000 +0100 (2000-01-05)
changeset 8093 d5eb246c94ec
parent 7664 c151ac595551
child 8153 9bdbcb71dc56
permissions -rw-r--r--
added thms_ctxt_args;
     1 (*  Title:      Pure/Isar/method.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 Proof methods.
     6 *)
     7 
     8 signature BASIC_METHOD =
     9 sig
    10   val print_methods: theory -> unit
    11   val Method: bstring -> (Args.src -> Proof.context -> Proof.method) -> string -> unit
    12 end;
    13 
    14 signature METHOD =
    15 sig
    16   include BASIC_METHOD
    17   val METHOD: (thm list -> tactic) -> Proof.method
    18   val METHOD0: tactic -> Proof.method
    19   val fail: Proof.method
    20   val succeed: Proof.method
    21   val insert_tac: thm list -> int -> tactic
    22   val insert: thm list -> Proof.method
    23   val insert_facts: Proof.method
    24   val unfold: thm list -> Proof.method
    25   val fold: thm list -> Proof.method
    26   val multi_resolve: thm list -> thm -> thm Seq.seq
    27   val multi_resolves: thm list -> thm list -> thm Seq.seq
    28   val rule_tac: thm list -> thm list -> int -> tactic
    29   val erule_tac: thm list -> thm list -> int -> tactic
    30   val rule: thm list -> Proof.method
    31   val erule: thm list -> Proof.method
    32   val assumption: Proof.context -> Proof.method
    33   exception METHOD_FAIL of (string * Position.T) * exn
    34   val help_methods: theory option -> unit
    35   val method: theory -> Args.src -> Proof.context -> Proof.method
    36   val add_methods: (bstring * (Args.src -> Proof.context -> Proof.method) * string) list
    37     -> theory -> theory
    38   val syntax: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
    39     Proof.context -> Args.src -> Proof.context * 'a
    40   val ctxt_args: (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    41   val no_args: Proof.method -> Args.src -> Proof.context -> Proof.method
    42   type modifier
    43   val sectioned_args: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list)) ->
    44     (Args.T list -> modifier * Args.T list) list ->
    45     ('a -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    46   val bang_sectioned_args:
    47     (Args.T list -> modifier * Args.T list) list ->
    48     (thm list -> Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    49   val only_sectioned_args:
    50     (Args.T list -> modifier * Args.T list) list ->
    51     (Proof.context -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    52   val thms_ctxt_args: (thm list -> Proof.context -> Proof.method)
    53     -> Args.src -> Proof.context -> Proof.method
    54   val thms_args: (thm list -> Proof.method) -> Args.src -> Proof.context -> Proof.method
    55   datatype text =
    56     Basic of (Proof.context -> Proof.method) |
    57     Source of Args.src |
    58     Then of text list |
    59     Orelse of text list |
    60     Try of text |
    61     Repeat1 of text
    62   val refine: text -> Proof.state -> Proof.state Seq.seq
    63   val refine_no_facts: text -> Proof.state -> Proof.state Seq.seq
    64   val proof: text option -> Proof.state -> Proof.state Seq.seq
    65   val local_qed: text option
    66     -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
    67     -> Proof.state -> Proof.state Seq.seq
    68   val local_terminal_proof: text * text option
    69     -> ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
    70     -> Proof.state -> Proof.state Seq.seq
    71   val local_immediate_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
    72     -> Proof.state -> Proof.state Seq.seq
    73   val local_default_proof: ({kind: string, name: string, thm: thm} -> unit) * (thm -> unit)
    74     -> Proof.state -> Proof.state Seq.seq
    75   val global_qed: text option -> Proof.state -> theory * {kind: string, name: string, thm: thm}
    76   val global_terminal_proof: text * text option
    77     -> Proof.state -> theory * {kind: string, name: string, thm: thm}
    78   val global_immediate_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
    79   val global_default_proof: Proof.state -> theory * {kind: string, name: string, thm: thm}
    80   val setup: (theory -> theory) list
    81 end;
    82 
    83 structure Method: METHOD =
    84 struct
    85 
    86 
    87 (** proof methods **)
    88 
    89 (* method from tactic *)
    90 
    91 val METHOD = Proof.method;
    92 fun METHOD0 tac = METHOD (fn [] => tac | _ => error "Method may not be used with facts");
    93 
    94 
    95 (* primitive *)
    96 
    97 val fail = METHOD (K no_tac);
    98 val succeed = METHOD (K all_tac);
    99 
   100 
   101 (* insert *)
   102 
   103 local
   104 
   105 fun cut_rule_tac raw_rule =
   106   let
   107     val rule = Drule.forall_intr_vars raw_rule;
   108     val revcut_rl = Drule.incr_indexes_wrt [] [] [] [rule] Drule.revcut_rl;
   109   in Tactic.rtac (rule COMP revcut_rl) end;
   110 
   111 in
   112 
   113 fun insert_tac [] i = all_tac
   114   | insert_tac facts i = EVERY (map (fn th => cut_rule_tac th i) facts);
   115 
   116 val insert_facts = METHOD (ALLGOALS o insert_tac);
   117 fun insert thms = METHOD (fn _ => ALLGOALS (insert_tac thms));
   118 
   119 end;
   120 
   121 
   122 (* unfold / fold definitions *)
   123 
   124 fun unfold thms = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN rewrite_goals_tac thms);
   125 fun fold thms = METHOD (fn facts => ALLGOALS (insert_tac facts) THEN fold_goals_tac thms);
   126 
   127 
   128 (* multi_resolve *)
   129 
   130 local
   131 
   132 fun res th i rule =
   133   Thm.biresolution false [(false, th)] i rule handle THM _ => Seq.empty;
   134 
   135 fun multi_res _ [] rule = Seq.single rule
   136   | multi_res i (th :: ths) rule = Seq.flat (Seq.map (res th i) (multi_res (i + 1) ths rule));
   137 
   138 in
   139 
   140 val multi_resolve = multi_res 1;
   141 fun multi_resolves facts rules = Seq.flat (Seq.map (multi_resolve facts) (Seq.of_list rules));
   142 
   143 end;
   144 
   145 
   146 (* rule *)
   147 
   148 local
   149 
   150 fun gen_rule_tac tac rules [] = tac rules
   151   | gen_rule_tac tac erules facts =
   152       let
   153         val rules = multi_resolves facts erules;
   154         fun tactic i state = Seq.flat (Seq.map (fn rule => tac [rule] i state) rules);
   155       in tactic end;
   156 
   157 in
   158 
   159 val rule_tac = gen_rule_tac Tactic.resolve_tac;
   160 val erule_tac = gen_rule_tac Tactic.eresolve_tac;
   161 
   162 fun rule rules = METHOD (FIRSTGOAL o rule_tac rules);
   163 fun erule rules = METHOD (FIRSTGOAL o erule_tac rules);
   164 
   165 end;
   166 
   167 
   168 (* assumption / finish *)
   169 
   170 fun assm_tac ctxt =
   171   assume_tac APPEND' resolve_tac (filter Thm.no_prems (ProofContext.prems_of ctxt));
   172 
   173 fun assumption_tac ctxt [] = assm_tac ctxt
   174   | assumption_tac _ [fact] = resolve_tac [fact]
   175   | assumption_tac _ _ = K no_tac;
   176 
   177 fun assumption ctxt = METHOD (FIRSTGOAL o assumption_tac ctxt);
   178 fun finish ctxt = METHOD (K (FILTER Thm.no_prems (ALLGOALS (assm_tac ctxt))));
   179 
   180 
   181 
   182 (** methods theory data **)
   183 
   184 (* data kind 'Isar/methods' *)
   185 
   186 structure MethodsDataArgs =
   187 struct
   188   val name = "Isar/methods";
   189   type T =
   190     {space: NameSpace.T,
   191      meths: (((Args.src -> Proof.context -> Proof.method) * string) * stamp) Symtab.table};
   192 
   193   val empty = {space = NameSpace.empty, meths = Symtab.empty};
   194   val copy = I;
   195   val prep_ext = I;
   196   fun merge ({space = space1, meths = meths1}, {space = space2, meths = meths2}) =
   197     {space = NameSpace.merge (space1, space2),
   198       meths = Symtab.merge eq_snd (meths1, meths2) handle Symtab.DUPS dups =>
   199         error ("Attempt to merge different versions of methods " ^ commas_quote dups)};
   200 
   201   fun print_meths verbose {space, meths} =
   202     let
   203       fun prt_meth (name, ((_, comment), _)) = Pretty.block
   204         [Pretty.str (name ^ ":"), Pretty.brk 2, Pretty.str comment];
   205     in
   206       if not verbose then ()
   207       else Pretty.writeln (Display.pretty_name_space ("method name space", space));
   208       Pretty.writeln (Pretty.big_list "methods:"
   209         (map prt_meth (NameSpace.cond_extern_table space meths)))
   210     end;
   211 
   212   fun print _ = print_meths true;
   213 end;
   214 
   215 structure MethodsData = TheoryDataFun(MethodsDataArgs);
   216 val print_methods = MethodsData.print;
   217 
   218 fun help_methods None = writeln "methods: (unkown theory context)"
   219   | help_methods (Some thy) = MethodsDataArgs.print_meths false (MethodsData.get thy);
   220 
   221 
   222 (* get methods *)
   223 
   224 exception METHOD_FAIL of (string * Position.T) * exn;
   225 
   226 fun method thy =
   227   let
   228     val {space, meths} = MethodsData.get thy;
   229 
   230     fun meth src =
   231       let
   232         val ((raw_name, _), pos) = Args.dest_src src;
   233         val name = NameSpace.intern space raw_name;
   234       in
   235         (case Symtab.lookup (meths, name) of
   236           None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
   237         | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
   238       end;
   239   in meth end;
   240 
   241 
   242 (* add_methods *)
   243 
   244 fun add_methods raw_meths thy =
   245   let
   246     val full = Sign.full_name (Theory.sign_of thy);
   247     val new_meths =
   248       map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
   249 
   250     val {space, meths} = MethodsData.get thy;
   251     val space' = NameSpace.extend (space, map fst new_meths);
   252     val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
   253       error ("Duplicate declaration of method(s) " ^ commas_quote dups);
   254   in
   255     thy |> MethodsData.put {space = space', meths = meths'}
   256   end;
   257 
   258 (*implicit version*)
   259 fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
   260 
   261 
   262 
   263 (** method syntax **)
   264 
   265 (* basic *)
   266 
   267 fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
   268   Args.syntax "method" scan;
   269 
   270 fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
   271   #2 (syntax (Scan.succeed (f ctxt)) ctxt src);
   272 
   273 fun no_args m = ctxt_args (K m);
   274 
   275 
   276 (* sections *)
   277 
   278 type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
   279 
   280 local
   281 
   282 fun sect ss = Scan.first (map (fn s => Scan.lift (s --| Args.$$$ ":")) ss);
   283 fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
   284 fun thmss ss = Scan.repeat (thms ss) >> flat;
   285 
   286 fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
   287 
   288 fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
   289   Scan.succeed (apply m (ctxt, ths)))) >> #2;
   290 
   291 fun sectioned args ss = args -- Scan.repeat (section ss);
   292 
   293 in
   294 
   295 fun sectioned_args args ss f src ctxt =
   296   let val (ctxt', (x, _)) = syntax (sectioned args ss) ctxt src
   297   in f x ctxt' end;
   298 
   299 fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
   300 fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
   301 
   302 fun thms_ctxt_args f = sectioned_args (thmss []) [] f;
   303 fun thms_args f = thms_ctxt_args (K o f);
   304 
   305 end;
   306 
   307 
   308 
   309 (** method text **)
   310 
   311 (* datatype text *)
   312 
   313 datatype text =
   314   Basic of (Proof.context -> Proof.method) |
   315   Source of Args.src |
   316   Then of text list |
   317   Orelse of text list |
   318   Try of text |
   319   Repeat1 of text;
   320 
   321 
   322 (* refine *)
   323 
   324 fun refine text state =
   325   let
   326     val thy = Proof.theory_of state;
   327 
   328     fun eval (Basic mth) = Proof.refine mth
   329       | eval (Source src) = Proof.refine (method thy src)
   330       | eval (Then txts) = Seq.EVERY (map eval txts)
   331       | eval (Orelse txts) = Seq.FIRST (map eval txts)
   332       | eval (Try txt) = Seq.TRY (eval txt)
   333       | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
   334   in eval text state end;
   335 
   336 fun refine_no_facts text state =
   337   state
   338   |> Proof.goal_facts (K [])
   339   |> refine text;
   340 
   341 
   342 (* structured proof steps *)
   343 
   344 val default_text = Source (Args.src (("default", []), Position.none));
   345 
   346 fun finish_text None = Basic finish
   347   | finish_text (Some txt) = Then [txt, Basic finish];
   348 
   349 fun proof opt_text state =
   350   state
   351   |> Proof.assert_backward
   352   |> refine (if_none opt_text default_text)
   353   |> Seq.map Proof.enter_forward;
   354 
   355 fun local_qed opt_text = Proof.local_qed (refine (finish_text opt_text));
   356 fun local_terminal_proof (text, opt_text) pr = Seq.THEN (proof (Some text), local_qed opt_text pr);
   357 val local_immediate_proof = local_terminal_proof (Basic assumption, None);
   358 val local_default_proof = local_terminal_proof (default_text, None);
   359 
   360 
   361 fun global_qeds opt_text = Proof.global_qed (refine (finish_text opt_text));
   362 
   363 fun global_qed opt_text state =
   364   state
   365   |> global_qeds opt_text
   366   |> Proof.check_result "Failed to finish proof" state
   367   |> Seq.hd;
   368 
   369 fun global_terminal_proof (text, opt_text) state =
   370   state
   371   |> proof (Some text)
   372   |> Proof.check_result "Terminal proof method failed" state
   373   |> (Seq.flat o Seq.map (global_qeds opt_text))
   374   |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
   375   |> Seq.hd;
   376 
   377 val global_immediate_proof = global_terminal_proof (Basic assumption, None);
   378 val global_default_proof = global_terminal_proof (default_text, None);
   379 
   380 
   381 
   382 (** theory setup **)
   383 
   384 (* pure_methods *)
   385 
   386 val pure_methods =
   387  [("fail", no_args fail, "force failure"),
   388   ("succeed", no_args succeed, "succeed"),
   389   ("-", no_args insert_facts, "do nothing, inserting current facts only"),
   390   ("insert", thms_args insert, "insert theorems, ignoring facts (improper!)"),
   391   ("unfold", thms_args unfold, "unfold definitions"),
   392   ("fold", thms_args fold, "fold definitions"),
   393   ("rule", thms_args rule, "apply some rule"),
   394   ("erule", thms_args erule, "apply some erule (improper!)"),
   395   ("assumption", ctxt_args assumption, "proof by assumption, preferring facts")];
   396 
   397 
   398 (* setup *)
   399 
   400 val setup = [MethodsData.init, add_methods pure_methods];
   401 
   402 
   403 end;
   404 
   405 
   406 structure BasicMethod: BASIC_METHOD = Method;
   407 open BasicMethod;