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