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