src/Pure/Isar/method.ML
author wenzelm
Sun Sep 26 16:42:14 1999 +0200 (1999-09-26 ago)
changeset 7613 fe818734c387
parent 7611 5b5aba10c8f6
child 7664 c151ac595551
permissions -rw-r--r--
help: unknown theory context;
     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_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 
   216 fun help_methods None = writeln "methods: (unkown theory context)"
   217   | help_methods (Some thy) = MethodsDataArgs.print_meths false (MethodsData.get thy);
   218 
   219 
   220 (* get methods *)
   221 
   222 exception METHOD_FAIL of (string * Position.T) * exn;
   223 
   224 fun method thy =
   225   let
   226     val {space, meths} = MethodsData.get thy;
   227 
   228     fun meth src =
   229       let
   230         val ((raw_name, _), pos) = Args.dest_src src;
   231         val name = NameSpace.intern space raw_name;
   232       in
   233         (case Symtab.lookup (meths, name) of
   234           None => error ("Unknown proof method: " ^ quote name ^ Position.str_of pos)
   235         | Some ((mth, _), _) => transform_failure (curry METHOD_FAIL (name, pos)) (mth src))
   236       end;
   237   in meth end;
   238 
   239 
   240 (* add_methods *)
   241 
   242 fun add_methods raw_meths thy =
   243   let
   244     val full = Sign.full_name (Theory.sign_of thy);
   245     val new_meths =
   246       map (fn (name, f, comment) => (full name, ((f, comment), stamp ()))) raw_meths;
   247 
   248     val {space, meths} = MethodsData.get thy;
   249     val space' = NameSpace.extend (space, map fst new_meths);
   250     val meths' = Symtab.extend (meths, new_meths) handle Symtab.DUPS dups =>
   251       error ("Duplicate declaration of method(s) " ^ commas_quote dups);
   252   in
   253     thy |> MethodsData.put {space = space', meths = meths'}
   254   end;
   255 
   256 (*implicit version*)
   257 fun Method name meth cmt = Context.>> (add_methods [(name, meth, cmt)]);
   258 
   259 
   260 
   261 (** method syntax **)
   262 
   263 (* basic *)
   264 
   265 fun syntax (scan: (Proof.context * Args.T list -> 'a * (Proof.context * Args.T list))) =
   266   Args.syntax "method" scan;
   267 
   268 fun ctxt_args (f: Proof.context -> Proof.method) src ctxt =
   269   #2 (syntax (Scan.succeed (f ctxt)) ctxt src);
   270 
   271 fun no_args m = ctxt_args (K m);
   272 
   273 
   274 (* sections *)
   275 
   276 type modifier = (Proof.context -> Proof.context) * Proof.context attribute;
   277 
   278 local
   279 
   280 fun sect ss = Scan.first (map (fn s => Scan.lift (s --| Args.$$$ ":")) ss);
   281 fun thms ss = Scan.unless (sect ss) Attrib.local_thms;
   282 fun thmss ss = Scan.repeat (thms ss) >> flat;
   283 
   284 fun apply (f, att) (ctxt, ths) = Thm.applys_attributes ((f ctxt, ths), [att]);
   285 
   286 fun section ss = (sect ss -- thmss ss) :-- (fn (m, ths) => Scan.depend (fn ctxt =>
   287   Scan.succeed (apply m (ctxt, ths)))) >> #2;
   288 
   289 fun sectioned args ss = args -- Scan.repeat (section ss);
   290 
   291 in
   292 
   293 fun sectioned_args args ss f src ctxt =
   294   let val (ctxt', (x, _)) = syntax (sectioned args ss) ctxt src
   295   in f x ctxt' end;
   296 
   297 fun bang_sectioned_args ss f = sectioned_args Args.bang_facts ss f;
   298 fun only_sectioned_args ss f = sectioned_args (Scan.succeed ()) ss (fn () => f);
   299 
   300 fun thms_args f = sectioned_args (thmss []) [] (fn ths => fn _ => f ths);
   301 
   302 end;
   303 
   304 
   305 
   306 (** method text **)
   307 
   308 (* datatype text *)
   309 
   310 datatype text =
   311   Basic of (Proof.context -> Proof.method) |
   312   Source of Args.src |
   313   Then of text list |
   314   Orelse of text list |
   315   Try of text |
   316   Repeat1 of text;
   317 
   318 
   319 (* refine *)
   320 
   321 fun refine text state =
   322   let
   323     val thy = Proof.theory_of state;
   324 
   325     fun eval (Basic mth) = Proof.refine mth
   326       | eval (Source src) = Proof.refine (method thy src)
   327       | eval (Then txts) = Seq.EVERY (map eval txts)
   328       | eval (Orelse txts) = Seq.FIRST (map eval txts)
   329       | eval (Try txt) = Seq.TRY (eval txt)
   330       | eval (Repeat1 txt) = Seq.REPEAT1 (eval txt);
   331   in eval text state end;
   332 
   333 fun refine_no_facts text state =
   334   state
   335   |> Proof.goal_facts (K [])
   336   |> refine text;
   337 
   338 
   339 (* structured proof steps *)
   340 
   341 val default_text = Source (Args.src (("default", []), Position.none));
   342 
   343 fun finish_text None = Basic finish
   344   | finish_text (Some txt) = Then [txt, Basic finish];
   345 
   346 fun proof opt_text state =
   347   state
   348   |> Proof.assert_backward
   349   |> refine (if_none opt_text default_text)
   350   |> Seq.map Proof.enter_forward;
   351 
   352 fun local_qed opt_text = Proof.local_qed (refine (finish_text opt_text));
   353 fun local_terminal_proof (text, opt_text) pr = Seq.THEN (proof (Some text), local_qed opt_text pr);
   354 val local_immediate_proof = local_terminal_proof (Basic assumption, None);
   355 val local_default_proof = local_terminal_proof (default_text, None);
   356 
   357 
   358 fun global_qeds opt_text = Proof.global_qed (refine (finish_text opt_text));
   359 
   360 fun global_qed opt_text state =
   361   state
   362   |> global_qeds opt_text
   363   |> Proof.check_result "Failed to finish proof" state
   364   |> Seq.hd;
   365 
   366 fun global_terminal_proof (text, opt_text) state =
   367   state
   368   |> proof (Some text)
   369   |> Proof.check_result "Terminal proof method failed" state
   370   |> (Seq.flat o Seq.map (global_qeds opt_text))
   371   |> Proof.check_result "Failed to finish proof (after successful terminal method)" state
   372   |> Seq.hd;
   373 
   374 val global_immediate_proof = global_terminal_proof (Basic assumption, None);
   375 val global_default_proof = global_terminal_proof (default_text, None);
   376 
   377 
   378 
   379 (** theory setup **)
   380 
   381 (* pure_methods *)
   382 
   383 val pure_methods =
   384  [("fail", no_args fail, "force failure"),
   385   ("succeed", no_args succeed, "succeed"),
   386   ("-", no_args insert_facts, "do nothing, inserting current facts only"),
   387   ("insert", thms_args insert, "insert facts (improper!)"),
   388   ("unfold", thms_args unfold, "unfold definitions"),
   389   ("fold", thms_args fold, "fold definitions"),
   390   ("rule", thms_args rule, "apply some rule"),
   391   ("erule", thms_args erule, "apply some erule (improper!)"),
   392   ("assumption", ctxt_args assumption, "proof by assumption, preferring facts")];
   393 
   394 
   395 (* setup *)
   396 
   397 val setup = [MethodsData.init, add_methods pure_methods];
   398 
   399 
   400 end;
   401 
   402 
   403 structure BasicMethod: BASIC_METHOD = Method;
   404 open BasicMethod;