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