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