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