src/Pure/Isar/obtain.ML
 author wenzelm Tue Sep 02 14:10:30 2008 +0200 (2008-09-02) changeset 28080 4723eb2456ce parent 24920 2a45e400fdad child 28084 a05ca48ef263 permissions -rw-r--r--
explicit type Name.binding for higher-specification elements;
simplified ProofContext.inferred_param;
```     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 thatN: string
```
```    43   val obtain: string -> (Name.binding * string option * mixfix) list ->
```
```    44     ((Name.binding * Attrib.src list) * (string * string list) list) list ->
```
```    45     bool -> Proof.state -> Proof.state
```
```    46   val obtain_i: string -> (Name.binding * typ option * mixfix) list ->
```
```    47     ((Name.binding * attribute list) * (term * term list) list) list ->
```
```    48     bool -> Proof.state -> Proof.state
```
```    49   val result: (Proof.context -> tactic) -> thm list -> Proof.context ->
```
```    50     (cterm list * thm list) * Proof.context
```
```    51   val guess: (Name.binding * string option * mixfix) list -> bool -> Proof.state -> Proof.state
```
```    52   val guess_i: (Name.binding * typ option * mixfix) list -> bool -> Proof.state -> Proof.state
```
```    53 end;
```
```    54
```
```    55 structure Obtain: OBTAIN =
```
```    56 struct
```
```    57
```
```    58 (** obtain_export **)
```
```    59
```
```    60 (*
```
```    61   [x, A x]
```
```    62      :
```
```    63      B
```
```    64   --------
```
```    65      B
```
```    66 *)
```
```    67 fun eliminate_term ctxt xs tm =
```
```    68   let
```
```    69     val vs = map (dest_Free o Thm.term_of) xs;
```
```    70     val bads = Term.fold_aterms (fn t as Free v =>
```
```    71       if member (op =) vs v then insert (op aconv) t else I | _ => I) tm [];
```
```    72     val _ = null bads orelse
```
```    73       error ("Result contains obtained parameters: " ^
```
```    74         space_implode " " (map (Syntax.string_of_term ctxt) bads));
```
```    75   in tm end;
```
```    76
```
```    77 fun eliminate fix_ctxt rule xs As thm =
```
```    78   let
```
```    79     val thy = ProofContext.theory_of fix_ctxt;
```
```    80
```
```    81     val _ = eliminate_term fix_ctxt xs (Thm.full_prop_of thm);
```
```    82     val _ = ObjectLogic.is_judgment thy (Thm.concl_of thm) orelse
```
```    83       error "Conclusion in obtained context must be object-logic judgment";
```
```    84
```
```    85     val ((_, [thm']), ctxt') = Variable.import_thms true [thm] fix_ctxt;
```
```    86     val prems = Drule.strip_imp_prems (#prop (Thm.crep_thm thm'));
```
```    87   in
```
```    88     ((Drule.implies_elim_list thm' (map Thm.assume prems)
```
```    89         |> Drule.implies_intr_list (map Drule.norm_hhf_cterm As)
```
```    90         |> Drule.forall_intr_list xs)
```
```    91       COMP rule)
```
```    92     |> Drule.implies_intr_list prems
```
```    93     |> singleton (Variable.export ctxt' fix_ctxt)
```
```    94   end;
```
```    95
```
```    96 fun obtain_export ctxt rule xs _ As =
```
```    97   (eliminate ctxt rule xs As, eliminate_term ctxt xs);
```
```    98
```
```    99
```
```   100
```
```   101 (** obtain **)
```
```   102
```
```   103 fun bind_judgment ctxt name =
```
```   104   let
```
```   105     val (bind, ctxt') = ProofContext.bind_fixes [name] ctxt;
```
```   106     val (t as _ \$ Free v) = bind (ObjectLogic.fixed_judgment (ProofContext.theory_of ctxt) name);
```
```   107   in ((v, t), ctxt') end;
```
```   108
```
```   109 val thatN = "that";
```
```   110
```
```   111 local
```
```   112
```
```   113 fun gen_obtain prep_att prep_vars prep_propp
```
```   114     name raw_vars raw_asms int state =
```
```   115   let
```
```   116     val _ = Proof.assert_forward_or_chain state;
```
```   117     val thy = Proof.theory_of state;
```
```   118     val cert = Thm.cterm_of thy;
```
```   119     val ctxt = Proof.context_of state;
```
```   120     val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
```
```   121
```
```   122     (*obtain vars*)
```
```   123     val (vars, vars_ctxt) = prep_vars raw_vars ctxt;
```
```   124     val (_, fix_ctxt) = vars_ctxt |> ProofContext.add_fixes_i vars;
```
```   125     val xs = map (Name.name_of o #1) vars;
```
```   126
```
```   127     (*obtain asms*)
```
```   128     val (asms_ctxt, proppss) = prep_propp (fix_ctxt, map snd raw_asms);
```
```   129     val asm_props = maps (map fst) proppss;
```
```   130     val asms = map fst (Attrib.map_specs (prep_att thy) raw_asms) ~~ proppss;
```
```   131
```
```   132     val _ = Variable.warn_extra_tfrees fix_ctxt asms_ctxt;
```
```   133
```
```   134     (*obtain statements*)
```
```   135     val thesisN = Name.variant xs AutoBind.thesisN;
```
```   136     val (thesis_var, thesis) = #1 (bind_judgment fix_ctxt thesisN);
```
```   137
```
```   138     val asm_frees = fold Term.add_frees asm_props [];
```
```   139     val parms = xs |> map (fn x =>
```
```   140       let val x' = ProofContext.get_skolem fix_ctxt x
```
```   141       in (x', the_default propT (AList.lookup (op =) asm_frees x')) end);
```
```   142
```
```   143     val that_name = if name = "" then thatN else name;
```
```   144     val that_prop =
```
```   145       Term.list_all_free (parms, Logic.list_implies (asm_props, thesis))
```
```   146       |> Library.curry Logic.list_rename_params xs;
```
```   147     val obtain_prop =
```
```   148       Logic.list_rename_params ([AutoBind.thesisN],
```
```   149         Term.list_all_free ([thesis_var], Logic.mk_implies (that_prop, thesis)));
```
```   150
```
```   151     fun after_qed _ =
```
```   152       Proof.local_qed (NONE, false)
```
```   153       #> Seq.map (`Proof.the_fact #-> (fn rule =>
```
```   154         Proof.fix_i vars
```
```   155         #> Proof.assm_i (obtain_export fix_ctxt rule (map (cert o Free) parms)) asms));
```
```   156   in
```
```   157     state
```
```   158     |> Proof.enter_forward
```
```   159     |> Proof.have_i NONE (K Seq.single) [((Name.no_binding, []), [(obtain_prop, [])])] int
```
```   160     |> Proof.proof (SOME Method.succeed_text) |> Seq.hd
```
```   161     |> Proof.fix_i [(Name.binding thesisN, NONE, NoSyn)]
```
```   162     |> Proof.assume_i
```
```   163       [((Name.binding that_name, [ContextRules.intro_query NONE]), [(that_prop, [])])]
```
```   164     |> `Proof.the_facts
```
```   165     ||> Proof.chain_facts chain_facts
```
```   166     ||> Proof.show_i NONE after_qed [((Name.no_binding, []), [(thesis, [])])] false
```
```   167     |-> Proof.refine_insert
```
```   168   end;
```
```   169
```
```   170 in
```
```   171
```
```   172 val obtain = gen_obtain Attrib.attribute ProofContext.read_vars ProofContext.read_propp;
```
```   173 val obtain_i = gen_obtain (K I) ProofContext.cert_vars ProofContext.cert_propp;
```
```   174
```
```   175 end;
```
```   176
```
```   177
```
```   178
```
```   179 (** tactical result **)
```
```   180
```
```   181 fun check_result ctxt thesis th =
```
```   182   (case Thm.prems_of th of
```
```   183     [prem] =>
```
```   184       if Thm.concl_of th aconv thesis andalso
```
```   185         Logic.strip_assums_concl prem aconv thesis then th
```
```   186       else error ("Guessed a different clause:\n" ^ ProofContext.string_of_thm ctxt th)
```
```   187   | [] => error "Goal solved -- nothing guessed."
```
```   188   | _ => error ("Guess split into several cases:\n" ^ ProofContext.string_of_thm ctxt th));
```
```   189
```
```   190 fun result tac facts ctxt =
```
```   191   let
```
```   192     val thy = ProofContext.theory_of ctxt;
```
```   193     val cert = Thm.cterm_of thy;
```
```   194
```
```   195     val ((thesis_var, thesis), thesis_ctxt) = bind_judgment ctxt AutoBind.thesisN;
```
```   196     val rule =
```
```   197       (case SINGLE (Method.insert_tac facts 1 THEN tac thesis_ctxt) (Goal.init (cert thesis)) of
```
```   198         NONE => raise THM ("Obtain.result: tactic failed", 0, facts)
```
```   199       | SOME th => check_result ctxt thesis (MetaSimplifier.norm_hhf (Goal.conclude th)));
```
```   200
```
```   201     val closed_rule = Thm.forall_intr (cert (Free thesis_var)) rule;
```
```   202     val ((_, [rule']), ctxt') = Variable.import_thms false [closed_rule] ctxt;
```
```   203     val obtain_rule = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule';
```
```   204     val ((params, stmt), fix_ctxt) = Variable.focus (Thm.cprem_of obtain_rule 1) ctxt';
```
```   205     val (prems, ctxt'') =
```
```   206       Assumption.add_assms (obtain_export fix_ctxt obtain_rule params)
```
```   207         (Drule.strip_imp_prems stmt) fix_ctxt;
```
```   208   in ((params, prems), ctxt'') end;
```
```   209
```
```   210
```
```   211
```
```   212 (** guess **)
```
```   213
```
```   214 local
```
```   215
```
```   216 fun unify_params vars thesis_var raw_rule ctxt =
```
```   217   let
```
```   218     val thy = ProofContext.theory_of ctxt;
```
```   219     val certT = Thm.ctyp_of thy;
```
```   220     val cert = Thm.cterm_of thy;
```
```   221     val string_of_typ = Syntax.string_of_typ ctxt;
```
```   222     val string_of_term = setmp show_types true (Syntax.string_of_term ctxt);
```
```   223
```
```   224     fun err msg th = error (msg ^ ":\n" ^ ProofContext.string_of_thm ctxt th);
```
```   225
```
```   226     val maxidx = fold (Term.maxidx_typ o snd o fst) vars ~1;
```
```   227     val rule = Thm.incr_indexes (maxidx + 1) raw_rule;
```
```   228
```
```   229     val params = RuleCases.strip_params (Logic.nth_prem (1, Thm.prop_of rule));
```
```   230     val m = length vars;
```
```   231     val n = length params;
```
```   232     val _ = m <= n orelse err "More variables than parameters in obtained rule" rule;
```
```   233
```
```   234     fun unify ((x, T), (y, U)) (tyenv, max) = Sign.typ_unify thy (T, U) (tyenv, max)
```
```   235       handle Type.TUNIFY =>
```
```   236         err ("Failed to unify variable " ^
```
```   237           string_of_term (Free (x, Envir.norm_type tyenv T)) ^ " against parameter " ^
```
```   238           string_of_term (Syntax.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule;
```
```   239     val (tyenv, _) = fold unify (map #1 vars ~~ Library.take (m, params))
```
```   240       (Vartab.empty, Int.max (maxidx, Thm.maxidx_of rule));
```
```   241     val norm_type = Envir.norm_type tyenv;
```
```   242
```
```   243     val xs = map (apsnd norm_type o fst) vars;
```
```   244     val ys = map (apsnd norm_type) (Library.drop (m, params));
```
```   245     val ys' = map Name.internal (Name.variant_list (map fst xs) (map fst ys)) ~~ map #2 ys;
```
```   246     val terms = map (Drule.mk_term o cert o Free) (xs @ ys');
```
```   247
```
```   248     val instT =
```
```   249       fold (Term.add_tvarsT o #2) params []
```
```   250       |> map (TVar #> (fn T => (certT T, certT (norm_type T))));
```
```   251     val closed_rule = rule
```
```   252       |> Thm.forall_intr (cert (Free thesis_var))
```
```   253       |> Thm.instantiate (instT, []);
```
```   254
```
```   255     val ((_, rule' :: terms'), ctxt') = Variable.import_thms false (closed_rule :: terms) ctxt;
```
```   256     val vars' =
```
```   257       map (dest_Free o Thm.term_of o Drule.dest_term) terms' ~~
```
```   258       (map snd vars @ replicate (length ys) NoSyn);
```
```   259     val rule'' = Thm.forall_elim (cert (Logic.varify (Free thesis_var))) rule';
```
```   260   in ((vars', rule''), ctxt') end;
```
```   261
```
```   262 fun inferred_type (binding, _, mx) ctxt =
```
```   263   let
```
```   264     val x = Name.name_of binding;
```
```   265     val (T, ctxt') = ProofContext.inferred_param x ctxt
```
```   266   in ((x, T, mx), ctxt') end;
```
```   267
```
```   268 fun polymorphic ctxt vars =
```
```   269   let val Ts = map Logic.dest_type (Variable.polymorphic ctxt (map (Logic.mk_type o #2) vars))
```
```   270   in map2 (fn (x, _, mx) => fn T => ((x, T), mx)) vars Ts end;
```
```   271
```
```   272 fun gen_guess prep_vars raw_vars int state =
```
```   273   let
```
```   274     val _ = Proof.assert_forward_or_chain state;
```
```   275     val thy = Proof.theory_of state;
```
```   276     val cert = Thm.cterm_of thy;
```
```   277     val ctxt = Proof.context_of state;
```
```   278     val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
```
```   279
```
```   280     val (thesis_var, thesis) = #1 (bind_judgment ctxt AutoBind.thesisN);
```
```   281     val vars = ctxt |> prep_vars raw_vars |-> fold_map inferred_type |> fst |> polymorphic ctxt;
```
```   282
```
```   283     fun guess_context raw_rule state' =
```
```   284       let
```
```   285         val ((parms, rule), ctxt') =
```
```   286           unify_params vars thesis_var raw_rule (Proof.context_of state');
```
```   287         val (bind, _) = ProofContext.bind_fixes (map (#1 o #1) parms) ctxt';
```
```   288         val ts = map (bind o Free o #1) parms;
```
```   289         val ps = map dest_Free ts;
```
```   290         val asms =
```
```   291           Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule))
```
```   292           |> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), []));
```
```   293         val _ = not (null asms) orelse error "Trivial result -- nothing guessed";
```
```   294       in
```
```   295         state'
```
```   296         |> Proof.map_context (K ctxt')
```
```   297         |> Proof.fix_i (map (fn ((x, T), mx) => (Name.binding x, SOME T, mx)) parms)
```
```   298         |> `Proof.context_of |-> (fn fix_ctxt => Proof.assm_i
```
```   299           (obtain_export fix_ctxt rule (map cert ts)) [((Name.no_binding, []), asms)])
```
```   300         |> Proof.add_binds_i AutoBind.no_facts
```
```   301       end;
```
```   302
```
```   303     val goal = Var (("guess", 0), propT);
```
```   304     fun print_result ctxt' (k, [(s, [_, th])]) =
```
```   305       ProofDisplay.print_results int ctxt' (k, [(s, [th])]);
```
```   306     val before_qed = SOME (Method.primitive_text (Goal.conclude #> MetaSimplifier.norm_hhf #>
```
```   307         (fn th => Goal.protect (Conjunction.intr (Drule.mk_term (Thm.cprop_of th)) th))));
```
```   308     fun after_qed [[_, res]] =
```
```   309       Proof.end_block #> guess_context (check_result ctxt thesis res) #> Seq.single;
```
```   310   in
```
```   311     state
```
```   312     |> Proof.enter_forward
```
```   313     |> Proof.begin_block
```
```   314     |> Proof.fix_i [(Name.binding AutoBind.thesisN, NONE, NoSyn)]
```
```   315     |> Proof.chain_facts chain_facts
```
```   316     |> Proof.local_goal print_result (K I) (apsnd (rpair I))
```
```   317       "guess" before_qed after_qed [((Name.no_binding, []), [Logic.mk_term goal, goal])]
```
```   318     |> Proof.refine (Method.primitive_text (K (Goal.init (cert thesis)))) |> Seq.hd
```
```   319   end;
```
```   320
```
```   321 in
```
```   322
```
```   323 val guess = gen_guess ProofContext.read_vars;
```
```   324 val guess_i = gen_guess ProofContext.cert_vars;
```
```   325
```
```   326 end;
```
```   327
```
```   328 end;
```