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