src/Pure/Isar/obtain.ML
author wenzelm
Mon Nov 14 16:52:19 2011 +0100 (2011-11-14 ago)
changeset 45488 6d71d9e52369
parent 45390 e29521ef9059
child 46215 0da9433f959e
permissions -rw-r--r--
pass positions for named targets, for formal links in the document model;
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
wenzelm@36323
    42
  val obtain: string -> (binding * typ option * mixfix) list ->
wenzelm@36323
    43
    (Thm.binding * (term * term list) list) list -> bool -> Proof.state -> Proof.state
wenzelm@36323
    44
  val obtain_cmd: string -> (binding * string option * mixfix) list ->
wenzelm@30211
    45
    (Attrib.binding * (string * string list) list) list -> bool -> Proof.state -> Proof.state
wenzelm@20308
    46
  val result: (Proof.context -> tactic) -> thm list -> Proof.context ->
wenzelm@32199
    47
    ((string * cterm) list * thm list) * Proof.context
wenzelm@36323
    48
  val guess: (binding * typ option * mixfix) list -> bool -> Proof.state -> Proof.state
wenzelm@36323
    49
  val guess_cmd: (binding * string option * mixfix) list -> bool -> Proof.state -> Proof.state
wenzelm@7674
    50
end;
wenzelm@7674
    51
wenzelm@10379
    52
structure Obtain: OBTAIN =
wenzelm@7674
    53
struct
wenzelm@7674
    54
wenzelm@18670
    55
(** obtain_export **)
wenzelm@18670
    56
wenzelm@18870
    57
(*
wenzelm@18897
    58
  [x, A x]
wenzelm@18897
    59
     :
wenzelm@18897
    60
     B
wenzelm@18897
    61
  --------
wenzelm@18897
    62
     B
wenzelm@18870
    63
*)
wenzelm@21686
    64
fun eliminate_term ctxt xs tm =
wenzelm@21686
    65
  let
wenzelm@21686
    66
    val vs = map (dest_Free o Thm.term_of) xs;
wenzelm@21686
    67
    val bads = Term.fold_aterms (fn t as Free v =>
wenzelm@21686
    68
      if member (op =) vs v then insert (op aconv) t else I | _ => I) tm [];
wenzelm@21686
    69
    val _ = null bads orelse
wenzelm@21686
    70
      error ("Result contains obtained parameters: " ^
wenzelm@24920
    71
        space_implode " " (map (Syntax.string_of_term ctxt) bads));
wenzelm@21686
    72
  in tm end;
wenzelm@21686
    73
wenzelm@21686
    74
fun eliminate fix_ctxt rule xs As thm =
wenzelm@9468
    75
  let
wenzelm@42360
    76
    val thy = Proof_Context.theory_of fix_ctxt;
wenzelm@9468
    77
wenzelm@21686
    78
    val _ = eliminate_term fix_ctxt xs (Thm.full_prop_of thm);
wenzelm@35625
    79
    val _ = Object_Logic.is_judgment thy (Thm.concl_of thm) orelse
wenzelm@20308
    80
      error "Conclusion in obtained context must be object-logic judgment";
wenzelm@20308
    81
wenzelm@31794
    82
    val ((_, [thm']), ctxt') = Variable.import true [thm] fix_ctxt;
wenzelm@20308
    83
    val prems = Drule.strip_imp_prems (#prop (Thm.crep_thm thm'));
wenzelm@9468
    84
  in
wenzelm@20308
    85
    ((Drule.implies_elim_list thm' (map Thm.assume prems)
wenzelm@20308
    86
        |> Drule.implies_intr_list (map Drule.norm_hhf_cterm As)
wenzelm@20308
    87
        |> Drule.forall_intr_list xs)
wenzelm@20308
    88
      COMP rule)
wenzelm@20308
    89
    |> Drule.implies_intr_list prems
wenzelm@20308
    90
    |> singleton (Variable.export ctxt' fix_ctxt)
wenzelm@9468
    91
  end;
wenzelm@9468
    92
wenzelm@21686
    93
fun obtain_export ctxt rule xs _ As =
wenzelm@21686
    94
  (eliminate ctxt rule xs As, eliminate_term ctxt xs);
wenzelm@21686
    95
wenzelm@9468
    96
wenzelm@9468
    97
wenzelm@17858
    98
(** obtain **)
wenzelm@17858
    99
wenzelm@17858
   100
fun bind_judgment ctxt name =
wenzelm@18670
   101
  let
wenzelm@42501
   102
    val thy = Proof_Context.theory_of ctxt;
wenzelm@42501
   103
    val ([x], ctxt') = Proof_Context.add_fixes [(Binding.name name, NONE, NoSyn)] ctxt;
wenzelm@42501
   104
    val (t as _ $ Free v) = Object_Logic.fixed_judgment thy x;
wenzelm@20308
   105
  in ((v, t), ctxt') end;
wenzelm@17858
   106
wenzelm@18897
   107
val thatN = "that";
wenzelm@18897
   108
wenzelm@17858
   109
local
wenzelm@8094
   110
wenzelm@18897
   111
fun gen_obtain prep_att prep_vars prep_propp
wenzelm@18897
   112
    name raw_vars raw_asms int state =
wenzelm@7674
   113
  let
wenzelm@9468
   114
    val _ = Proof.assert_forward_or_chain state;
wenzelm@20308
   115
    val thy = Proof.theory_of state;
wenzelm@20308
   116
    val cert = Thm.cterm_of thy;
wenzelm@17858
   117
    val ctxt = Proof.context_of state;
wenzelm@17357
   118
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@7674
   119
wenzelm@8543
   120
    (*obtain vars*)
wenzelm@19844
   121
    val (vars, vars_ctxt) = prep_vars raw_vars ctxt;
wenzelm@42490
   122
    val (xs', fix_ctxt) = vars_ctxt |> Proof_Context.add_fixes vars;
wenzelm@42494
   123
    val xs = map (Variable.check_name o #1) vars;
wenzelm@7674
   124
wenzelm@8543
   125
    (*obtain asms*)
wenzelm@45327
   126
    val (proppss, asms_ctxt) = prep_propp (map snd raw_asms) fix_ctxt;
wenzelm@45330
   127
    val ((_, bind_ctxt), _) = Proof_Context.bind_propp_i proppss asms_ctxt;
wenzelm@19482
   128
    val asm_props = maps (map fst) proppss;
wenzelm@45390
   129
    val asms = map fst (Attrib.map_specs (map (prep_att thy)) raw_asms) ~~ proppss;
wenzelm@10464
   130
wenzelm@42490
   131
    (*obtain parms*)
wenzelm@42490
   132
    val (Ts, parms_ctxt) = fold_map Proof_Context.inferred_param xs' asms_ctxt;
wenzelm@42490
   133
    val parms = xs' ~~ Ts;
wenzelm@42490
   134
    val _ = Variable.warn_extra_tfrees fix_ctxt parms_ctxt;
wenzelm@7674
   135
wenzelm@12970
   136
    (*obtain statements*)
wenzelm@43324
   137
    val thesisN = singleton (Name.variant_list xs) Auto_Bind.thesisN;
wenzelm@20308
   138
    val (thesis_var, thesis) = #1 (bind_judgment fix_ctxt thesisN);
wenzelm@9468
   139
wenzelm@18897
   140
    val that_name = if name = "" then thatN else name;
wenzelm@10582
   141
    val that_prop =
wenzelm@45328
   142
      Logic.list_rename_params xs
wenzelm@45328
   143
        (Term.list_all_free (parms, Logic.list_implies (asm_props, thesis)));
wenzelm@12970
   144
    val obtain_prop =
wenzelm@45328
   145
      Logic.list_rename_params [Auto_Bind.thesisN]
wenzelm@45328
   146
        (Term.list_all_free ([thesis_var], Logic.mk_implies (that_prop, thesis)));
wenzelm@7674
   147
wenzelm@18124
   148
    fun after_qed _ =
wenzelm@17357
   149
      Proof.local_qed (NONE, false)
wenzelm@29383
   150
      #> `Proof.the_fact #-> (fn rule =>
wenzelm@36323
   151
        Proof.fix vars
wenzelm@36323
   152
        #> Proof.assm (obtain_export fix_ctxt rule (map (cert o Free) parms)) asms);
wenzelm@7674
   153
  in
wenzelm@8094
   154
    state
wenzelm@9468
   155
    |> Proof.enter_forward
wenzelm@36323
   156
    |> Proof.have NONE (K I) [(Thm.empty_binding, [(obtain_prop, [])])] int
wenzelm@45330
   157
    |> Proof.map_context bind_ctxt
wenzelm@17858
   158
    |> Proof.proof (SOME Method.succeed_text) |> Seq.hd
wenzelm@36323
   159
    |> Proof.fix [(Binding.name thesisN, NONE, NoSyn)]
wenzelm@36323
   160
    |> Proof.assume
wenzelm@33369
   161
      [((Binding.name that_name, [Context_Rules.intro_query NONE]), [(that_prop, [])])]
wenzelm@16842
   162
    |> `Proof.the_facts
wenzelm@17357
   163
    ||> Proof.chain_facts chain_facts
wenzelm@36323
   164
    ||> Proof.show NONE after_qed [(Thm.empty_binding, [(thesis, [])])] false
wenzelm@18907
   165
    |-> Proof.refine_insert
wenzelm@7674
   166
  end;
wenzelm@7674
   167
wenzelm@17858
   168
in
wenzelm@17858
   169
wenzelm@42360
   170
val obtain = gen_obtain (K I) Proof_Context.cert_vars Proof_Context.cert_propp;
wenzelm@42360
   171
val obtain_cmd = gen_obtain Attrib.attribute Proof_Context.read_vars Proof_Context.read_propp;
wenzelm@8094
   172
wenzelm@8094
   173
end;
wenzelm@17858
   174
wenzelm@17858
   175
wenzelm@17858
   176
wenzelm@20308
   177
(** tactical result **)
wenzelm@20308
   178
wenzelm@20308
   179
fun check_result ctxt thesis th =
wenzelm@20308
   180
  (case Thm.prems_of th of
wenzelm@20308
   181
    [prem] =>
wenzelm@20308
   182
      if Thm.concl_of th aconv thesis andalso
wenzelm@20308
   183
        Logic.strip_assums_concl prem aconv thesis then th
wenzelm@32091
   184
      else error ("Guessed a different clause:\n" ^ Display.string_of_thm ctxt th)
wenzelm@38875
   185
  | [] => error "Goal solved -- nothing guessed"
wenzelm@32091
   186
  | _ => error ("Guess split into several cases:\n" ^ Display.string_of_thm ctxt th));
wenzelm@20308
   187
wenzelm@20308
   188
fun result tac facts ctxt =
wenzelm@20308
   189
  let
wenzelm@42360
   190
    val thy = Proof_Context.theory_of ctxt;
wenzelm@20308
   191
    val cert = Thm.cterm_of thy;
wenzelm@20308
   192
wenzelm@33386
   193
    val ((thesis_var, thesis), thesis_ctxt) = bind_judgment ctxt Auto_Bind.thesisN;
wenzelm@20308
   194
    val rule =
wenzelm@20308
   195
      (case SINGLE (Method.insert_tac facts 1 THEN tac thesis_ctxt) (Goal.init (cert thesis)) of
wenzelm@20308
   196
        NONE => raise THM ("Obtain.result: tactic failed", 0, facts)
wenzelm@41228
   197
      | SOME th => check_result ctxt thesis (Raw_Simplifier.norm_hhf (Goal.conclude th)));
wenzelm@20308
   198
wenzelm@20308
   199
    val closed_rule = Thm.forall_intr (cert (Free thesis_var)) rule;
wenzelm@31794
   200
    val ((_, [rule']), ctxt') = Variable.import false [closed_rule] ctxt;
wenzelm@35845
   201
    val obtain_rule = Thm.forall_elim (cert (Logic.varify_global (Free thesis_var))) rule';
wenzelm@42495
   202
    val ((params, stmt), fix_ctxt) = Variable.focus_cterm (Thm.cprem_of obtain_rule 1) ctxt';
wenzelm@20308
   203
    val (prems, ctxt'') =
wenzelm@32199
   204
      Assumption.add_assms (obtain_export fix_ctxt obtain_rule (map #2 params))
wenzelm@20308
   205
        (Drule.strip_imp_prems stmt) fix_ctxt;
wenzelm@20308
   206
  in ((params, prems), ctxt'') end;
wenzelm@20308
   207
wenzelm@20308
   208
wenzelm@20308
   209
wenzelm@17858
   210
(** guess **)
wenzelm@17858
   211
wenzelm@17858
   212
local
wenzelm@17858
   213
wenzelm@20308
   214
fun unify_params vars thesis_var raw_rule ctxt =
wenzelm@17858
   215
  let
wenzelm@42360
   216
    val thy = Proof_Context.theory_of ctxt;
wenzelm@19978
   217
    val certT = Thm.ctyp_of thy;
wenzelm@19978
   218
    val cert = Thm.cterm_of thy;
wenzelm@39134
   219
    val string_of_term = Syntax.string_of_term (Config.put show_types true ctxt);
wenzelm@17891
   220
wenzelm@32091
   221
    fun err msg th = error (msg ^ ":\n" ^ Display.string_of_thm ctxt th);
wenzelm@17858
   222
wenzelm@19978
   223
    val maxidx = fold (Term.maxidx_typ o snd o fst) vars ~1;
wenzelm@19779
   224
    val rule = Thm.incr_indexes (maxidx + 1) raw_rule;
wenzelm@19779
   225
wenzelm@33368
   226
    val params = Rule_Cases.strip_params (Logic.nth_prem (1, Thm.prop_of rule));
wenzelm@17858
   227
    val m = length vars;
wenzelm@17858
   228
    val n = length params;
wenzelm@19779
   229
    val _ = m <= n orelse err "More variables than parameters in obtained rule" rule;
wenzelm@17858
   230
wenzelm@19779
   231
    fun unify ((x, T), (y, U)) (tyenv, max) = Sign.typ_unify thy (T, U) (tyenv, max)
wenzelm@19779
   232
      handle Type.TUNIFY =>
wenzelm@19779
   233
        err ("Failed to unify variable " ^
wenzelm@19779
   234
          string_of_term (Free (x, Envir.norm_type tyenv T)) ^ " against parameter " ^
wenzelm@42284
   235
          string_of_term (Syntax_Trans.mark_boundT (y, Envir.norm_type tyenv U)) ^ " in") rule;
haftmann@33957
   236
    val (tyenv, _) = fold unify (map #1 vars ~~ take m params)
wenzelm@19779
   237
      (Vartab.empty, Int.max (maxidx, Thm.maxidx_of rule));
wenzelm@17858
   238
    val norm_type = Envir.norm_type tyenv;
wenzelm@17858
   239
wenzelm@19978
   240
    val xs = map (apsnd norm_type o fst) vars;
haftmann@33957
   241
    val ys = map (apsnd norm_type) (drop m params);
wenzelm@20085
   242
    val ys' = map Name.internal (Name.variant_list (map fst xs) (map fst ys)) ~~ map #2 ys;
wenzelm@19978
   243
    val terms = map (Drule.mk_term o cert o Free) (xs @ ys');
wenzelm@19779
   244
wenzelm@17858
   245
    val instT =
wenzelm@17858
   246
      fold (Term.add_tvarsT o #2) params []
wenzelm@19978
   247
      |> map (TVar #> (fn T => (certT T, certT (norm_type T))));
wenzelm@20308
   248
    val closed_rule = rule
wenzelm@20308
   249
      |> Thm.forall_intr (cert (Free thesis_var))
wenzelm@20308
   250
      |> Thm.instantiate (instT, []);
wenzelm@17891
   251
wenzelm@31794
   252
    val ((_, rule' :: terms'), ctxt') = Variable.import false (closed_rule :: terms) ctxt;
wenzelm@19978
   253
    val vars' =
wenzelm@19978
   254
      map (dest_Free o Thm.term_of o Drule.dest_term) terms' ~~
wenzelm@19978
   255
      (map snd vars @ replicate (length ys) NoSyn);
wenzelm@35845
   256
    val rule'' = Thm.forall_elim (cert (Logic.varify_global (Free thesis_var))) rule';
wenzelm@19978
   257
  in ((vars', rule''), ctxt') end;
wenzelm@17858
   258
wenzelm@28080
   259
fun inferred_type (binding, _, mx) ctxt =
wenzelm@28080
   260
  let
wenzelm@42494
   261
    val x = Variable.check_name binding;
wenzelm@42360
   262
    val (T, ctxt') = Proof_Context.inferred_param x ctxt
wenzelm@19779
   263
  in ((x, T, mx), ctxt') end;
wenzelm@19779
   264
wenzelm@20004
   265
fun polymorphic ctxt vars =
wenzelm@19897
   266
  let val Ts = map Logic.dest_type (Variable.polymorphic ctxt (map (Logic.mk_type o #2) vars))
wenzelm@19779
   267
  in map2 (fn (x, _, mx) => fn T => ((x, T), mx)) vars Ts end;
wenzelm@18693
   268
wenzelm@17858
   269
fun gen_guess prep_vars raw_vars int state =
wenzelm@17858
   270
  let
wenzelm@17858
   271
    val _ = Proof.assert_forward_or_chain state;
wenzelm@17858
   272
    val thy = Proof.theory_of state;
wenzelm@20308
   273
    val cert = Thm.cterm_of thy;
wenzelm@17858
   274
    val ctxt = Proof.context_of state;
wenzelm@17858
   275
    val chain_facts = if can Proof.assert_chain state then Proof.the_facts state else [];
wenzelm@17858
   276
wenzelm@33386
   277
    val (thesis_var, thesis) = #1 (bind_judgment ctxt Auto_Bind.thesisN);
wenzelm@20004
   278
    val vars = ctxt |> prep_vars raw_vars |-> fold_map inferred_type |> fst |> polymorphic ctxt;
wenzelm@17858
   279
wenzelm@19978
   280
    fun guess_context raw_rule state' =
wenzelm@17858
   281
      let
wenzelm@19978
   282
        val ((parms, rule), ctxt') =
wenzelm@20308
   283
          unify_params vars thesis_var raw_rule (Proof.context_of state');
wenzelm@42501
   284
        val (xs, _) = Variable.add_fixes (map (#1 o #1) parms) ctxt';
wenzelm@42501
   285
        val ps = xs ~~ map (#2 o #1) parms;
wenzelm@42501
   286
        val ts = map Free ps;
wenzelm@17858
   287
        val asms =
wenzelm@17858
   288
          Logic.strip_assums_hyp (Logic.nth_prem (1, Thm.prop_of rule))
wenzelm@19585
   289
          |> map (fn asm => (Term.betapplys (Term.list_abs (ps, asm), ts), []));
wenzelm@19779
   290
        val _ = not (null asms) orelse error "Trivial result -- nothing guessed";
wenzelm@17858
   291
      in
wenzelm@19978
   292
        state'
wenzelm@19978
   293
        |> Proof.map_context (K ctxt')
wenzelm@36323
   294
        |> Proof.fix (map (fn ((x, T), mx) => (Binding.name x, SOME T, mx)) parms)
wenzelm@36323
   295
        |> `Proof.context_of |-> (fn fix_ctxt => Proof.assm
wenzelm@30211
   296
          (obtain_export fix_ctxt rule (map cert ts)) [(Thm.empty_binding, asms)])
wenzelm@33386
   297
        |> Proof.bind_terms Auto_Bind.no_facts
wenzelm@17858
   298
      end;
wenzelm@17858
   299
wenzelm@19779
   300
    val goal = Var (("guess", 0), propT);
wenzelm@19779
   301
    fun print_result ctxt' (k, [(s, [_, th])]) =
wenzelm@33389
   302
      Proof_Display.print_results int ctxt' (k, [(s, [th])]);
wenzelm@41228
   303
    val before_qed = SOME (Method.primitive_text (Goal.conclude #> Raw_Simplifier.norm_hhf #>
wenzelm@20308
   304
        (fn th => Goal.protect (Conjunction.intr (Drule.mk_term (Thm.cprop_of th)) th))));
wenzelm@19779
   305
    fun after_qed [[_, res]] =
wenzelm@29383
   306
      Proof.end_block #> guess_context (check_result ctxt thesis res);
wenzelm@17858
   307
  in
wenzelm@17858
   308
    state
wenzelm@17858
   309
    |> Proof.enter_forward
wenzelm@17858
   310
    |> Proof.begin_block
wenzelm@36323
   311
    |> Proof.fix [(Binding.name Auto_Bind.thesisN, NONE, NoSyn)]
wenzelm@17858
   312
    |> Proof.chain_facts chain_facts
wenzelm@45327
   313
    |> Proof.local_goal print_result (K I) (pair o rpair I)
wenzelm@30211
   314
      "guess" before_qed after_qed [(Thm.empty_binding, [Logic.mk_term goal, goal])]
wenzelm@20308
   315
    |> Proof.refine (Method.primitive_text (K (Goal.init (cert thesis)))) |> Seq.hd
wenzelm@17858
   316
  end;
wenzelm@17858
   317
wenzelm@17858
   318
in
wenzelm@17858
   319
wenzelm@42360
   320
val guess = gen_guess Proof_Context.cert_vars;
wenzelm@42360
   321
val guess_cmd = gen_guess Proof_Context.read_vars;
wenzelm@17858
   322
wenzelm@17858
   323
end;
wenzelm@17858
   324
wenzelm@17858
   325
end;