src/Pure/Isar/obtain.ML
author wenzelm
Tue Jan 31 18:19:25 2006 +0100 (2006-01-31)
changeset 18870 020e242c02a0
parent 18769 e90eb0bc0ddd
child 18897 b31293969d4f
permissions -rw-r--r--
tuned comments;
     1 (*  Title:      Pure/Isar/obtain.ML
     2     ID:         $Id$
     3     Author:     Markus Wenzel, TU Muenchen
     4 
     5 The 'obtain' and 'guess' language elements -- generalized existence at
     6 the level of proof texts: 'obtain' involves a proof that certain
     7 fixes/assumes may be introduced into the present context; 'guess' is
     8 similar, but derives these elements from the course of reasoning!
     9 
    10   <chain_facts>
    11   obtain x where "A x" <proof> ==
    12 
    13   have "!!thesis. (!!x. A x ==> thesis) ==> thesis"
    14   proof succeed
    15     fix thesis
    16     assume that [intro?]: "!!x. A x ==> thesis"
    17     <chain_facts>
    18     show thesis
    19       apply (insert that)
    20       <proof>
    21   qed
    22   fix x assm <<obtain_export>> "A x"
    23 
    24 
    25   <chain_facts>
    26   guess x <proof body> <proof end> ==
    27 
    28   {
    29     fix thesis
    30     <chain_facts> have "PROP ?guess"
    31       apply magic      -- {* turns goal into "thesis ==> #thesis" *}
    32       <proof body>
    33       apply_end magic  -- {* turns final "(!!x. P x ==> thesis) ==> #thesis" into
    34         "#((!!x. A x ==> thesis) ==> thesis)" which is a finished goal state *}
    35       <proof end>
    36   }
    37   fix x assm <<obtain_export>> "A x"
    38 *)
    39 
    40 signature OBTAIN =
    41 sig
    42   val obtain: (string * string option) list ->
    43     ((string * Attrib.src list) * (string * (string list * string list)) list) list
    44     -> bool -> Proof.state -> Proof.state
    45   val obtain_i: (string * typ option) list ->
    46     ((string * attribute list) * (term * (term list * term list)) list) list
    47     -> bool -> Proof.state -> Proof.state
    48   val guess: (string * string option) list -> bool -> Proof.state -> Proof.state
    49   val guess_i: (string * typ option) list -> bool -> Proof.state -> Proof.state
    50 end;
    51 
    52 structure Obtain: OBTAIN =
    53 struct
    54 
    55 
    56 (** obtain_export **)
    57 
    58 (*
    59     [x]
    60     [A x]
    61       :
    62       B
    63     -----
    64       B
    65 *)
    66 fun obtain_export ctxt parms rule cprops thm =
    67   let
    68     val {thy, prop, maxidx, ...} = Thm.rep_thm thm;
    69     val cparms = map (Thm.cterm_of thy) parms;
    70 
    71     val thm' = thm
    72       |> Drule.implies_intr_protected cprops
    73       |> Drule.forall_intr_list cparms
    74       |> Drule.forall_elim_vars (maxidx + 1);
    75     val elim_tacs = replicate (length cprops) (Tactic.etac Drule.protectI);
    76 
    77     val concl = Logic.strip_assums_concl prop;
    78     val bads = parms inter (Term.term_frees concl);
    79   in
    80     if not (null bads) then
    81       error ("Conclusion contains obtained parameters: " ^
    82         space_implode " " (map (ProofContext.string_of_term ctxt) bads))
    83     else if not (ObjectLogic.is_judgment thy concl) then
    84       error "Conclusion in obtained context must be object-logic judgments"
    85     else (Tactic.rtac thm' THEN' RANGE elim_tacs) 1 rule
    86   end;
    87 
    88 
    89 
    90 (** obtain **)
    91 
    92 fun bind_judgment ctxt name =
    93   let
    94     val (bind, _) = ProofContext.bind_fixes [name] ctxt;
    95     val (t as _ $ Free v) = bind (ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name);
    96   in (v, t) end;
    97 
    98 local
    99 
   100 val thatN = "that";
   101 
   102 fun gen_obtain prep_att prep_vars prep_propp raw_vars raw_asms int state =
   103   let
   104     val _ = Proof.assert_forward_or_chain state;
   105     val ctxt = Proof.context_of state;
   106     val thy = Proof.theory_of state;
   107     val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
   108 
   109     (*obtain vars*)
   110     val (vars, vars_ctxt) = prep_vars (map Syntax.no_syn raw_vars) ctxt;
   111     val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
   112     val xs = map #1 vars;
   113 
   114     (*obtain asms*)
   115     val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms);
   116     val asm_props = List.concat (map (map fst) proppss);
   117     val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss;
   118 
   119     val _ = ProofContext.warn_extra_tfrees fix_ctxt asms_ctxt;
   120 
   121     (*obtain statements*)
   122     val thesisN = Term.variant xs AutoBind.thesisN;
   123     val (thesis_var, thesis) = bind_judgment fix_ctxt thesisN;
   124 
   125     fun occs_var x = Library.get_first (fn t =>
   126       Term.find_free t (ProofContext.get_skolem fix_ctxt x)) asm_props;
   127     val raw_parms = map occs_var xs;
   128     val parms = List.mapPartial I raw_parms;
   129     val parm_names =
   130       List.mapPartial (fn (SOME (Free a), x) => SOME (a, x) | _ => NONE) (raw_parms ~~ xs);
   131 
   132     val that_prop =
   133       Term.list_all_free (map #1 parm_names, Logic.list_implies (asm_props, thesis))
   134       |> Library.curry Logic.list_rename_params (map #2 parm_names);
   135     val obtain_prop =
   136       Logic.list_rename_params ([AutoBind.thesisN],
   137         Term.list_all_free ([thesis_var], Logic.mk_implies (that_prop, thesis)));
   138 
   139     fun after_qed _ =
   140       Proof.local_qed (NONE, false)
   141       #> Seq.map (`Proof.the_fact #-> (fn rule =>
   142         Proof.fix_i (xs ~~ map #2 vars)
   143         #> Proof.assm_i (K (obtain_export ctxt parms rule)) asms));
   144   in
   145     state
   146     |> Proof.enter_forward
   147     |> Proof.have_i NONE (K Seq.single) [(("", []), [(obtain_prop, ([], []))])] int
   148     |> Proof.proof (SOME Method.succeed_text) |> Seq.hd
   149     |> Proof.fix_i [(thesisN, NONE)]
   150     |> Proof.assume_i [((thatN, [ContextRules.intro_query NONE]), [(that_prop, ([], []))])]
   151     |> `Proof.the_facts
   152     ||> Proof.chain_facts chain_facts
   153     ||> Proof.show_i NONE after_qed [(("", []), [(thesis, ([], []))])] false
   154     |-> (Proof.refine o Method.Basic o K o Method.insert) |> Seq.hd
   155   end;
   156 
   157 in
   158 
   159 val obtain = gen_obtain Attrib.attribute ProofContext.read_vars ProofContext.read_propp;
   160 val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp;
   161 
   162 end;
   163 
   164 
   165 
   166 (** guess **)
   167 
   168 local
   169 
   170 fun match_params ctxt vars rule =
   171   let
   172     val thy = ProofContext.theory_of ctxt;
   173     val string_of_typ = ProofContext.string_of_typ ctxt;
   174     val string_of_term = setmp show_types true (ProofContext.string_of_term ctxt);
   175 
   176     fun err msg th = error (msg ^ ":\n" ^ ProofContext.string_of_thm ctxt th);
   177 
   178     val params = RuleCases.strip_params (Logic.nth_prem (1, Thm.prop_of rule));
   179     val m = length vars;
   180     val n = length params;
   181     val _ = conditional (m > n)
   182       (fn () => err "More variables than parameters in obtained rule" rule);
   183 
   184     fun match ((x, SOME T), (y, U)) tyenv =
   185         ((x, T), Sign.typ_match thy (U, T) tyenv handle Type.TYPE_MATCH =>
   186           err ("Failed to match variable " ^
   187             string_of_term (Free (x, T)) ^ " against parameter " ^
   188             string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule)
   189       | match ((x, NONE), (_, U)) tyenv = ((x, U), tyenv);
   190     val (xs, tyenv) = fold_map match (vars ~~ Library.take (m, params)) Vartab.empty;
   191     val ys = Library.drop (m, params);
   192     val norm_type = Envir.norm_type tyenv;
   193 
   194     val xs' = xs |> map (apsnd norm_type);
   195     val ys' =
   196       map Syntax.internal (Term.variantlist (map fst ys, map fst xs)) ~~
   197       map (norm_type o snd) ys;
   198     val instT =
   199       fold (Term.add_tvarsT o #2) params []
   200       |> map (TVar #> (fn T => (Thm.ctyp_of thy T, Thm.ctyp_of thy (norm_type T))));
   201     val rule' = rule |> Thm.instantiate (instT, []);
   202 
   203     val tvars = Drule.tvars_of rule';
   204     val vars = fold (remove op =) (Term.add_vars (Thm.concl_of rule') []) (Drule.vars_of rule');
   205     val _ =
   206       if null tvars andalso null vars then ()
   207       else err ("Illegal schematic variable(s) " ^
   208         commas (map (string_of_typ o TVar) tvars @ map (string_of_term o Var) vars) ^ " in") rule';
   209   in (xs' @ ys', rule') end;
   210 
   211 fun inferred_type (x, _, mx) ctxt =
   212   let val ((_, T), ctxt') = ProofContext.inferred_param x ctxt
   213   in ((x, SOME T, mx), ctxt') end;
   214 
   215 fun gen_guess prep_vars raw_vars int state =
   216   let
   217     val _ = Proof.assert_forward_or_chain state;
   218     val thy = Proof.theory_of state;
   219     val ctxt = Proof.context_of state;
   220     val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
   221 
   222     val (thesis_var, thesis) = bind_judgment ctxt AutoBind.thesisN;
   223     val (vars, _) = ctxt |> prep_vars (map Syntax.no_syn raw_vars) |-> fold_map inferred_type;
   224 
   225     fun check_result th =
   226       (case Thm.prems_of th of
   227         [prem] =>
   228           if Thm.concl_of th aconv thesis andalso
   229             Logic.strip_assums_concl prem aconv thesis then ()
   230           else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th)
   231       | [] => error "Goal solved -- nothing guessed."
   232       | _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th));
   233 
   234     fun guess_context raw_rule =
   235       let
   236         val (parms, rule) = match_params ctxt (map (fn (x, T, _) => (x, T)) vars) raw_rule;
   237         val (bind, _) = ProofContext.bind_fixes (map #1 parms) ctxt;
   238         val ts = map (bind o Free) parms;
   239         val ps = map dest_Free ts;
   240         val asms =
   241           Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule))
   242           |> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), ([], [])));
   243         val _ = conditional (null asms) (fn () => error "Trivial result -- nothing guessed");
   244       in
   245         Proof.fix_i (map (apsnd SOME) parms)
   246         #> Proof.assm_i (K (obtain_export ctxt ts rule)) [(("", []), asms)]
   247         #> Proof.add_binds_i AutoBind.no_facts
   248       end;
   249 
   250     val before_qed = SOME (Method.primitive_text (Goal.conclude #> Goal.protect));
   251     fun after_qed [[res]] =
   252       (check_result res; Proof.end_block #> Seq.map (`Proof.the_fact #-> guess_context));
   253   in
   254     state
   255     |> Proof.enter_forward
   256     |> Proof.begin_block
   257     |> Proof.fix_i [(AutoBind.thesisN, NONE)]
   258     |> Proof.chain_facts chain_facts
   259     |> Proof.local_goal (ProofDisplay.print_results int) (K I) (apsnd (rpair I))
   260       "guess" before_qed after_qed [(("", []), [Var (("guess", 0), propT)])]
   261     |> Proof.refine (Method.primitive_text (K (Goal.init (Thm.cterm_of thy thesis)))) |> Seq.hd
   262   end;
   263 
   264 in
   265 
   266 val guess = gen_guess ProofContext.read_vars;
   267 val guess_i = gen_guess ProofContext.cert_vars;
   268 
   269 end;
   270 
   271 end;