src/Pure/subgoal.ML
author wenzelm
Wed, 01 Jul 2015 21:57:21 +0200
changeset 60625 f64658887a05
parent 60623 be39fe6c5620
child 60626 9eefb9f39021
permissions -rw-r--r--
proper state after qed;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/subgoal.ML
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
60623
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
     3
    Author:     Daniel Matichuk, NICTA/UNSW
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
     4
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
     5
Tactical operations with explicit subgoal focus, based on canonical
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
     6
proof decomposition.  The "visible" part of the text within the
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
     7
context is fixed, the remaining goal may be schematic.
60623
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
     8
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
     9
Isar subgoal command for proof structure within unstructured proof
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
    10
scripts.
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    11
*)
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    12
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    13
signature SUBGOAL =
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    14
sig
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    15
  type focus = {context: Proof.context, params: (string * cterm) list, prems: thm list,
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    16
    asms: cterm list, concl: cterm, schematics: (ctyp * ctyp) list * (cterm * cterm) list}
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    17
  val focus_params: Proof.context -> int -> thm -> focus * thm
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    18
  val focus_prems: Proof.context -> int -> thm -> focus * thm
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    19
  val focus: Proof.context -> int -> thm -> focus * thm
32210
a5e9d9f3e5e1 retrofit: actually handle schematic variables -- need to export into original context;
wenzelm
parents: 32200
diff changeset
    20
  val retrofit: Proof.context -> Proof.context -> (string * cterm) list -> cterm list ->
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    21
    int -> thm -> thm -> thm Seq.seq
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    22
  val FOCUS_PARAMS: (focus -> tactic) -> Proof.context -> int -> tactic
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    23
  val FOCUS_PREMS: (focus -> tactic) -> Proof.context -> int -> tactic
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    24
  val FOCUS: (focus -> tactic) -> Proof.context -> int -> tactic
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    25
  val SUBPROOF: (focus -> tactic) -> Proof.context -> int -> tactic
60623
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
    26
  val subgoal: Attrib.binding -> Attrib.binding option -> (binding * mixfix) list ->
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
    27
    Proof.state -> focus * Proof.state
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
    28
  val subgoal_cmd: Attrib.binding -> Attrib.binding option -> (binding * mixfix) list ->
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
    29
    Proof.state -> focus * Proof.state
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    30
end;
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    31
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    32
structure Subgoal: SUBGOAL =
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    33
struct
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    34
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    35
(* focus *)
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    36
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    37
type focus = {context: Proof.context, params: (string * cterm) list, prems: thm list,
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    38
  asms: cterm list, concl: cterm, schematics: (ctyp * ctyp) list * (cterm * cterm) list};
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    39
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    40
fun gen_focus (do_prems, do_concl) ctxt i raw_st =
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    41
  let
60367
e201bd8973db clarified context;
wenzelm
parents: 59621
diff changeset
    42
    val st = raw_st
e201bd8973db clarified context;
wenzelm
parents: 59621
diff changeset
    43
      |> Thm.transfer (Proof_Context.theory_of ctxt)
e201bd8973db clarified context;
wenzelm
parents: 59621
diff changeset
    44
      |> Simplifier.norm_hhf_protect ctxt;
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    45
    val ((schematic_types, [st']), ctxt1) = Variable.importT [st] ctxt;
42495
1af81b70cf09 clarified Variable.focus vs. Variable.focus_cterm -- eliminated clone;
wenzelm
parents: 42360
diff changeset
    46
    val ((params, goal), ctxt2) = Variable.focus_cterm (Thm.cprem_of st' i) ctxt1;
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    47
32213
f1b166317ae2 added focus_params/FOCUS_PARAMS, which focus on the parameter prefix only;
wenzelm
parents: 32210
diff changeset
    48
    val (asms, concl) =
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    49
      if do_prems then (Drule.strip_imp_prems goal, Drule.strip_imp_concl goal)
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    50
      else ([], goal);
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    51
    val text = asms @ (if do_concl then [concl] else []);
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    52
60367
e201bd8973db clarified context;
wenzelm
parents: 59621
diff changeset
    53
    val (inst, ctxt3) = Variable.import_inst true (map Thm.term_of text) ctxt2;
e201bd8973db clarified context;
wenzelm
parents: 59621
diff changeset
    54
    val (_, schematic_terms) = Thm.certify_inst ctxt3 inst;
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    55
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    56
    val schematics = (schematic_types, schematic_terms);
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    57
    val asms' = map (Thm.instantiate_cterm schematics) asms;
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    58
    val concl' = Thm.instantiate_cterm schematics concl;
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    59
    val (prems, context) = Assumption.add_assumes asms' ctxt3;
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    60
  in
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    61
    ({context = context, params = params, prems = prems,
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    62
      asms = asms', concl = concl', schematics = schematics}, Goal.init concl')
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    63
  end;
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
    64
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    65
val focus_params = gen_focus (false, false);
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    66
val focus_prems = gen_focus (true, false);
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    67
val focus = gen_focus (true, true);
32213
f1b166317ae2 added focus_params/FOCUS_PARAMS, which focus on the parameter prefix only;
wenzelm
parents: 32210
diff changeset
    68
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    69
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    70
(* lift and retrofit *)
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    71
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    72
(*
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    73
     B [?'b, ?y]
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    74
  ----------------
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    75
  B ['b, y params]
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    76
*)
32284
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    77
fun lift_import idx params th ctxt =
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    78
  let
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    79
    val ((_, [th']), ctxt') = Variable.importT [th] ctxt;
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    80
59586
ddf6deaadfe8 clarified signature;
wenzelm
parents: 59573
diff changeset
    81
    val Ts = map Thm.typ_of_cterm params;
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    82
    val ts = map Thm.term_of params;
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    83
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    84
    val prop = Thm.full_prop_of th';
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    85
    val concl_vars = Term.add_vars (Logic.strip_imp_concl prop) [];
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
    86
    val vars = rev (Term.add_vars prop []);
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
    87
    val (ys, ctxt'') = Variable.variant_fixes (map (Name.clean o #1 o #1) vars) ctxt';
32284
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    88
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    89
    fun var_inst v y =
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    90
      let
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    91
        val ((x, i), T) = v;
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    92
        val (U, args) =
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    93
          if member (op =) concl_vars v then (T, [])
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    94
          else (Ts ---> T, ts);
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    95
        val u = Free (y, U);
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    96
        in ((Var v, list_comb (u, args)), (u, Var ((x, i + idx), U))) end;
59616
eb59c6968219 tuned -- more explicit use of context;
wenzelm
parents: 59586
diff changeset
    97
    val (inst1, inst2) =
59621
291934bac95e Thm.cterm_of and Thm.ctyp_of operate on local context;
wenzelm
parents: 59616
diff changeset
    98
      split_list (map (apply2 (apply2 (Thm.cterm_of ctxt))) (map2 var_inst vars ys));
32284
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
    99
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
   100
    val th'' = Thm.instantiate ([], inst1) th';
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
   101
  in ((inst2, th''), ctxt'') end;
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   102
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   103
(*
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
   104
       [x, A x]
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   105
          :
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
   106
       B x ==> C
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   107
  ------------------
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   108
  [!!x. A x ==> B x]
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
   109
          :
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
   110
          C
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   111
*)
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   112
fun lift_subgoals params asms th =
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   113
  let
32329
1527ff8c2dfb SUBPROOF: recovered Goal.check_finished;
wenzelm
parents: 32284
diff changeset
   114
    fun lift ct = fold_rev Thm.all_name params (Drule.list_implies (asms, ct));
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   115
    val unlift =
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   116
      fold (Thm.elim_implies o Thm.assume) asms o
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   117
      Drule.forall_elim_list (map #2 params) o Thm.assume;
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   118
    val subgoals = map lift (Drule.strip_imp_prems (Thm.cprop_of th));
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   119
    val th' = fold (Thm.elim_implies o unlift) subgoals th;
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   120
  in (subgoals, th') end;
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   121
32213
f1b166317ae2 added focus_params/FOCUS_PARAMS, which focus on the parameter prefix only;
wenzelm
parents: 32210
diff changeset
   122
fun retrofit ctxt1 ctxt0 params asms i st1 st0 =
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   123
  let
32284
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
   124
    val idx = Thm.maxidx_of st0 + 1;
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   125
    val ps = map #2 params;
32284
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
   126
    val ((subgoal_inst, st2), ctxt2) = lift_import idx ps st1 ctxt1;
32213
f1b166317ae2 added focus_params/FOCUS_PARAMS, which focus on the parameter prefix only;
wenzelm
parents: 32210
diff changeset
   127
    val (subgoals, st3) = lift_subgoals params asms st2;
f1b166317ae2 added focus_params/FOCUS_PARAMS, which focus on the parameter prefix only;
wenzelm
parents: 32210
diff changeset
   128
    val result = st3
f1b166317ae2 added focus_params/FOCUS_PARAMS, which focus on the parameter prefix only;
wenzelm
parents: 32210
diff changeset
   129
      |> Goal.conclude
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   130
      |> Drule.implies_intr_list asms
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   131
      |> Drule.forall_intr_list ps
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   132
      |> Drule.implies_intr_list subgoals
32284
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
   133
      |> fold_rev (Thm.forall_intr o #1) subgoal_inst
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
   134
      |> fold (Thm.forall_elim o #2) subgoal_inst
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
   135
      |> Thm.adjust_maxidx_thm idx
d8ee8a956f19 retrofit: more precise handling of locally introduced schematic variables (avoid zero_var_indexes);
wenzelm
parents: 32283
diff changeset
   136
      |> singleton (Variable.export ctxt2 ctxt0);
52223
5bb6ae8acb87 tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents: 49845
diff changeset
   137
  in
58950
d07464875dd4 optional proof context for unify operations, for the sake of proper local options;
wenzelm
parents: 54883
diff changeset
   138
    Thm.bicompose (SOME ctxt0) {flatten = true, match = false, incremented = false}
52223
5bb6ae8acb87 tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents: 49845
diff changeset
   139
      (false, result, Thm.nprems_of st1) i st0
5bb6ae8acb87 tuned signature -- more explicit flags for low-level Thm.bicompose;
wenzelm
parents: 49845
diff changeset
   140
  end;
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   141
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   142
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   143
(* tacticals *)
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   144
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
   145
fun GEN_FOCUS flags tac ctxt i st =
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
   146
  if Thm.nprems_of st < i then Seq.empty
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
   147
  else
32329
1527ff8c2dfb SUBPROOF: recovered Goal.check_finished;
wenzelm
parents: 32284
diff changeset
   148
    let val (args as {context = ctxt', params, asms, ...}, st') = gen_focus flags ctxt i st;
1527ff8c2dfb SUBPROOF: recovered Goal.check_finished;
wenzelm
parents: 32284
diff changeset
   149
    in Seq.lifts (retrofit ctxt' ctxt params asms i) (tac args st') st end;
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   150
32281
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
   151
val FOCUS_PARAMS = GEN_FOCUS (false, false);
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
   152
val FOCUS_PREMS = GEN_FOCUS (true, false);
750101435f60 focus: more precise treatment of schematic variables, only the "visible" part of the text is fixed;
wenzelm
parents: 32213
diff changeset
   153
val FOCUS = GEN_FOCUS (true, true);
32213
f1b166317ae2 added focus_params/FOCUS_PARAMS, which focus on the parameter prefix only;
wenzelm
parents: 32210
diff changeset
   154
49845
9b19c0e81166 tuned signature;
wenzelm
parents: 42495
diff changeset
   155
fun SUBPROOF tac ctxt = FOCUS (Seq.map (Goal.check_finished ctxt) oo tac) ctxt;
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
   156
60623
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   157
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   158
(* Isar subgoal command *)
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   159
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   160
local
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   161
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   162
fun gen_focus prep_atts raw_result_binding raw_prems_binding params state =
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   163
  let
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   164
    val _ = Proof.assert_backward state;
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   165
    val {context = ctxt, facts = facts, goal = st} = Proof.raw_goal state;
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   166
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   167
    val result_binding = apsnd (map (prep_atts ctxt)) raw_result_binding;
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   168
    val (prems_binding, do_prems) =
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   169
      (case raw_prems_binding of
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   170
        SOME (b, raw_atts) => ((b, map (prep_atts ctxt) raw_atts), true)
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   171
      | NONE => ((Binding.empty, []), false));
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   172
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   173
    fun check_param (b, mx) = ignore (Proof_Context.cert_var (b, NONE, mx) ctxt);
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   174
    val _ = List.app check_param params;
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   175
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   176
    val _ = if Thm.no_prems st then error "No subgoals!" else ();
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   177
    val subgoal_focus = #1 (focus ctxt 1 st);  (* FIXME clarify prems/params *)
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   178
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   179
    fun after_qed (ctxt'', [[result]]) =
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   180
      Proof.end_block #> (fn state' =>
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   181
        let
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   182
          val ctxt' = Proof.context_of state';
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   183
          val results' =
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   184
            Proof_Context.export ctxt'' ctxt' (Conjunction.elim_conjunctions result);
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   185
        in
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   186
          state'
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   187
          |> Proof.refine_primitive (fn _ => fn _ =>
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   188
            retrofit ctxt'' ctxt' (#params subgoal_focus) (#asms subgoal_focus) 1
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   189
              (Goal.protect 0 result) st
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   190
            |> Seq.hd)
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   191
          |> Proof.map_context
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   192
            (#2 o Proof_Context.note_thmss "" [(result_binding, [(results', [])])])
60625
f64658887a05 proper state after qed;
wenzelm
parents: 60623
diff changeset
   193
        end)
f64658887a05 proper state after qed;
wenzelm
parents: 60623
diff changeset
   194
      #> Proof.reset_facts
f64658887a05 proper state after qed;
wenzelm
parents: 60623
diff changeset
   195
      #> Proof.enter_backward;
60623
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   196
  in
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   197
    state
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   198
    |> Proof.enter_forward
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   199
    |> Proof.using_facts []
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   200
    |> Proof.begin_block
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   201
    |> Proof.map_context (fn _ =>
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   202
      #context subgoal_focus
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   203
      |> Proof_Context.note_thmss "" [(prems_binding, [(#prems subgoal_focus, [])])] |> #2)
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   204
    |> Proof.internal_goal (K (K ())) (Proof_Context.get_mode ctxt) true "subgoal"
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   205
        NONE after_qed [] [] [(Thm.empty_binding, [(Thm.term_of (#concl subgoal_focus), [])])] |> #2
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   206
    |> Proof.using_facts facts
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   207
    |> pair subgoal_focus
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   208
  end;
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   209
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   210
in
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   211
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   212
val subgoal = gen_focus Attrib.attribute;
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   213
val subgoal_cmd = gen_focus Attrib.attribute_cmd;
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   214
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   215
end;
be39fe6c5620 support for subgoal focus command;
wenzelm
parents: 60367
diff changeset
   216
20210
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
   217
end;
8fe4ae4360eb Tactical operations depending on local subgoal structure.
wenzelm
parents:
diff changeset
   218
32200
2bd8ab91a426 advanced retrofit, which allows new subgoals and variables;
wenzelm
parents: 31794
diff changeset
   219
val SUBPROOF = Subgoal.SUBPROOF;