src/Pure/old_goals.ML
author wenzelm
Wed, 26 May 2010 18:19:12 +0200
changeset 37127 a4bf276a20b3
parent 36944 dbf831a50e4a
child 37145 01aa36932739
permissions -rw-r--r--
refer to polyml-5.3.0-old for ppc-darwin;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     1
(*  Title:      Pure/old_goals.ML
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     2
    Author:     Lawrence C Paulson, Cambridge University Computer Laboratory
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     3
    Copyright   1993  University of Cambridge
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     4
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     5
Old-style goal stack package.  The goal stack initially holds a dummy
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     6
proof, and can never become empty.  Each goal stack consists of a list
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     7
of levels.  The undo list is a list of goal stacks.  Finally, there
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     8
may be a stack of pending proofs.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
     9
*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    10
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    11
signature OLD_GOALS =
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    12
sig
35989
3418cdf1855e moved Primitive_Defs.mk_defpair to OldGoals.mk_defpair;
wenzelm
parents: 35625
diff changeset
    13
  val mk_defpair: term * term -> string * term
32231
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    14
  val strip_context: term -> (string * typ) list * term list * term
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    15
  val metahyps_thms: int -> thm -> thm list option
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    16
  val METAHYPS: (thm list -> tactic) -> int -> tactic
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    17
  val simple_read_term: theory -> typ -> string -> term
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    18
  val read_term: theory -> string -> term
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    19
  val read_prop: theory -> string -> term
35237
b625eb708d94 moved ancient Drule.get_def to OldGoals.get_def;
wenzelm
parents: 35021
diff changeset
    20
  val get_def: theory -> xstring -> thm
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    21
  type proof
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    22
  val premises: unit -> thm list
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    23
  val reset_goals: unit -> unit
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32432
diff changeset
    24
  val result_error_fn: (thm -> string -> thm) Unsynchronized.ref
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    25
  val print_sign_exn: theory -> exn -> 'a
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    26
  val prove_goalw_cterm: thm list->cterm->(thm list->tactic list)->thm
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    27
  val prove_goalw_cterm_nocheck: thm list->cterm->(thm list->tactic list)->thm
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    28
  val prove_goalw: theory -> thm list -> string -> (thm list -> tactic list) -> thm
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    29
  val prove_goal: theory -> string -> (thm list -> tactic list) -> thm
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    30
  val topthm: unit -> thm
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    31
  val result: unit -> thm
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    32
  val uresult: unit -> thm
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    33
  val getgoal: int -> term
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    34
  val gethyps: int -> thm list
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    35
  val print_exn: exn -> 'a
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    36
  val filter_goal: (term*term->bool) -> thm list -> int -> thm list
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    37
  val prlev: int -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    38
  val pr: unit -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    39
  val prlim: int -> unit
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    40
  val goalw_cterm: thm list -> cterm -> thm list
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    41
  val goalw: theory -> thm list -> string -> thm list
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    42
  val goal: theory -> string -> thm list
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    43
  val Goalw: thm list -> string -> thm list
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    44
  val Goal: string -> thm list
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    45
  val simple_prove_goal_cterm: cterm->(thm list->tactic list)->thm
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    46
  val by: tactic -> unit
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    47
  val byev: tactic list -> unit
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    48
  val back: unit -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    49
  val choplev: int -> unit
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    50
  val chop: unit -> unit
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    51
  val undo: unit -> unit
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    52
  val save_proof: unit -> proof
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    53
  val restore_proof: proof -> thm list
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    54
  val push_proof: unit -> unit
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    55
  val pop_proof: unit -> thm list
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
    56
  val rotate_proof: unit -> thm list
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    57
  val qed: string -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    58
  val qed_goal: string -> theory -> string -> (thm list -> tactic list) -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    59
  val qed_goalw: string -> theory -> thm list -> string
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    60
    -> (thm list -> tactic list) -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    61
  val qed_spec_mp: string -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    62
  val qed_goal_spec_mp: string -> theory -> string -> (thm list -> tactic list) -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    63
  val qed_goalw_spec_mp: string -> theory -> thm list -> string
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    64
    -> (thm list -> tactic list) -> unit
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    65
end;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    66
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    67
structure OldGoals: OLD_GOALS =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    68
struct
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
    69
35989
3418cdf1855e moved Primitive_Defs.mk_defpair to OldGoals.mk_defpair;
wenzelm
parents: 35625
diff changeset
    70
fun mk_defpair (lhs, rhs) =
3418cdf1855e moved Primitive_Defs.mk_defpair to OldGoals.mk_defpair;
wenzelm
parents: 35625
diff changeset
    71
  (case Term.head_of lhs of
3418cdf1855e moved Primitive_Defs.mk_defpair to OldGoals.mk_defpair;
wenzelm
parents: 35625
diff changeset
    72
    Const (name, _) =>
3418cdf1855e moved Primitive_Defs.mk_defpair to OldGoals.mk_defpair;
wenzelm
parents: 35625
diff changeset
    73
      (Long_Name.base_name name ^ "_def", Logic.mk_equals (lhs, rhs))
3418cdf1855e moved Primitive_Defs.mk_defpair to OldGoals.mk_defpair;
wenzelm
parents: 35625
diff changeset
    74
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
3418cdf1855e moved Primitive_Defs.mk_defpair to OldGoals.mk_defpair;
wenzelm
parents: 35625
diff changeset
    75
3418cdf1855e moved Primitive_Defs.mk_defpair to OldGoals.mk_defpair;
wenzelm
parents: 35625
diff changeset
    76
32231
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    77
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    78
       METAHYPS (fn prems => tac prems) i
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    79
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    80
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    81
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    82
"prems").  The parameters x1,...,xm become free variables.  If the
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    83
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    84
then it is lifted back into the original context, yielding k subgoals.
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    85
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    86
Replaces unknowns in the context by Frees having the prefix METAHYP_
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    87
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    88
DOES NOT HANDLE TYPE UNKNOWNS.
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    89
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    90
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    91
NOTE: This version does not observe the proof context, and thus cannot
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    92
work reliably.  See also Subgoal.SUBPROOF and Subgoal.FOCUS for
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    93
properly localized variants of the same idea.
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    94
****)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    95
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    96
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    97
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters.
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    98
  Main difference from strip_assums concerns parameters:
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
    99
    it replaces the bound variables by free variables.  *)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   100
fun strip_context_aux (params, Hs, Const ("==>", _) $ H $ B) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   101
      strip_context_aux (params, H :: Hs, B)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   102
  | strip_context_aux (params, Hs, Const ("all",_) $ Abs (a, T, t)) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   103
      let val (b, u) = Syntax.variant_abs (a, T, t)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   104
      in strip_context_aux ((b, T) :: params, Hs, u) end
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   105
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   106
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   107
fun strip_context A = strip_context_aux ([], [], A);
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   108
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   109
local
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   110
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   111
  (*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   112
    Instantiates distinct free variables by terms of same type.*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   113
  fun free_instantiate ctpairs =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   114
    forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   115
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   116
  fun free_of s ((a, i), T) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   117
    Free (s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i), T)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   118
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   119
  fun mk_inst v = (Var v, free_of "METAHYP1_" v)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   120
in
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   121
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   122
(*Common code for METAHYPS and metahyps_thms*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   123
fun metahyps_split_prem prem =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   124
  let (*find all vars in the hyps -- should find tvars also!*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   125
      val hyps_vars = fold Term.add_vars (Logic.strip_assums_hyp prem) []
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   126
      val insts = map mk_inst hyps_vars
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   127
      (*replace the hyps_vars by Frees*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   128
      val prem' = subst_atomic insts prem
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   129
      val (params,hyps,concl) = strip_context prem'
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   130
  in (insts,params,hyps,concl)  end;
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   131
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   132
fun metahyps_aux_tac tacf (prem,gno) state =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   133
  let val (insts,params,hyps,concl) = metahyps_split_prem prem
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   134
      val maxidx = Thm.maxidx_of state
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   135
      val cterm = Thm.cterm_of (Thm.theory_of_thm state)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   136
      val chyps = map cterm hyps
36944
dbf831a50e4a less pervasive names from structure Thm;
wenzelm
parents: 36863
diff changeset
   137
      val hypths = map Thm.assume chyps
32231
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   138
      val subprems = map (Thm.forall_elim_vars 0) hypths
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   139
      val fparams = map Free params
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   140
      val cparams = map cterm fparams
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   141
      fun swap_ctpair (t,u) = (cterm u, cterm t)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   142
      (*Subgoal variables: make Free; lift type over params*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   143
      fun mk_subgoal_inst concl_vars (v, T) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   144
          if member (op =) concl_vars (v, T)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   145
          then ((v, T), true, free_of "METAHYP2_" (v, T))
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   146
          else ((v, T), false, free_of "METAHYP2_" (v, map #2 params ---> T))
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   147
      (*Instantiate subgoal vars by Free applied to params*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   148
      fun mk_ctpair (v, in_concl, u) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   149
          if in_concl then (cterm (Var v), cterm u)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   150
          else (cterm (Var v), cterm (list_comb (u, fparams)))
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   151
      (*Restore Vars with higher type and index*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   152
      fun mk_subgoal_swap_ctpair (((a, i), T), in_concl, u as Free (_, U)) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   153
          if in_concl then (cterm u, cterm (Var ((a, i), T)))
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   154
          else (cterm u, cterm (Var ((a, i + maxidx), U)))
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   155
      (*Embed B in the original context of params and hyps*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   156
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   157
      (*Strip the context using elimination rules*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   158
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   159
      (*A form of lifting that discharges assumptions.*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   160
      fun relift st =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   161
        let val prop = Thm.prop_of st
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   162
            val subgoal_vars = (*Vars introduced in the subgoals*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   163
              fold Term.add_vars (Logic.strip_imp_prems prop) []
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   164
            and concl_vars = Term.add_vars (Logic.strip_imp_concl prop) []
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   165
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   166
            val st' = Thm.instantiate ([], map mk_ctpair subgoal_insts) st
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   167
            val emBs = map (cterm o embed) (prems_of st')
36944
dbf831a50e4a less pervasive names from structure Thm;
wenzelm
parents: 36863
diff changeset
   168
            val Cth  = implies_elim_list st' (map (elim o Thm.assume) emBs)
32231
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   169
        in  (*restore the unknowns to the hypotheses*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   170
            free_instantiate (map swap_ctpair insts @
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   171
                              map mk_subgoal_swap_ctpair subgoal_insts)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   172
                (*discharge assumptions from state in same order*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   173
                (implies_intr_list emBs
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   174
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   175
        end
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   176
      (*function to replace the current subgoal*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   177
      fun next st = Thm.bicompose false (false, relift st, nprems_of st) gno state
36944
dbf831a50e4a less pervasive names from structure Thm;
wenzelm
parents: 36863
diff changeset
   178
  in Seq.maps next (tacf subprems (Thm.trivial (cterm concl))) end;
32231
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   179
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   180
end;
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   181
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   182
(*Returns the theorem list that METAHYPS would supply to its tactic*)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   183
fun metahyps_thms i state =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   184
  let val prem = Logic.nth_prem (i, Thm.prop_of state)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   185
      and cterm = cterm_of (Thm.theory_of_thm state)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   186
      val (_,_,hyps,_) = metahyps_split_prem prem
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   187
  in SOME (map (Thm.forall_elim_vars 0 o Thm.assume o cterm) hyps) end
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   188
  handle TERM ("nth_prem", [A]) => NONE;
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   189
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   190
local
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   191
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   192
fun print_vars_terms thy (n,thm) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   193
  let
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   194
    fun typed ty = " has type: " ^ Syntax.string_of_typ_global thy ty;
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   195
    fun find_vars thy (Const (c, ty)) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   196
          if null (Term.add_tvarsT ty []) then I
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   197
          else insert (op =) (c ^ typed ty)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   198
      | find_vars thy (Var (xi, ty)) = insert (op =) (Term.string_of_vname xi ^ typed ty)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   199
      | find_vars _ (Free _) = I
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   200
      | find_vars _ (Bound _) = I
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   201
      | find_vars thy (Abs (_, _, t)) = find_vars thy t
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   202
      | find_vars thy (t1 $ t2) =
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   203
          find_vars thy t1 #> find_vars thy t1;
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   204
    val prem = Logic.nth_prem (n, Thm.prop_of thm)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   205
    val tms = find_vars thy prem []
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   206
  in
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   207
    (warning "Found schematic vars in assumptions:"; warning (cat_lines tms))
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   208
  end;
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   209
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   210
in
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   211
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   212
fun METAHYPS tacf n thm = SUBGOAL (metahyps_aux_tac tacf) n thm
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   213
  handle THM("assume: variables",_,_) => (print_vars_terms (theory_of_thm thm) (n,thm); Seq.empty)
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   214
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   215
end;
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   216
95b8afcbb0ed moved METAHYPS to old_goals.ML (cf. SUBPROOF and FOCUS in subgoal.ML for properly localized versions of the same idea);
wenzelm
parents: 32187
diff changeset
   217
27256
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   218
(* old ways of reading terms *)
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   219
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   220
fun simple_read_term thy T s =
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   221
  let
36610
bafd82950e24 renamed ProofContext.init to ProofContext.init_global to emphasize that this is not the real thing;
wenzelm
parents: 35989
diff changeset
   222
    val ctxt = ProofContext.init_global thy
27256
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   223
      |> ProofContext.allow_dummies
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   224
      |> ProofContext.set_mode ProofContext.mode_schematic;
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   225
    val parse = if T = propT then Syntax.parse_prop else Syntax.parse_term;
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   226
  in parse ctxt s |> TypeInfer.constrain T |> Syntax.check_term ctxt end;
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   227
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   228
fun read_term thy = simple_read_term thy dummyT;
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   229
fun read_prop thy = simple_read_term thy propT;
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   230
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   231
35237
b625eb708d94 moved ancient Drule.get_def to OldGoals.get_def;
wenzelm
parents: 35021
diff changeset
   232
fun get_def thy = Thm.axiom thy o Name_Space.intern (Theory.axiom_space thy) o Thm.def_name;
b625eb708d94 moved ancient Drule.get_def to OldGoals.get_def;
wenzelm
parents: 35021
diff changeset
   233
27256
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   234
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   235
(*** Goal package ***)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   236
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   237
(*Each level of goal stack includes a proof state and alternative states,
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   238
  the output of the tactic applied to the preceeding level.  *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   239
type gstack = (thm * thm Seq.seq) list;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   240
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   241
datatype proof = Proof of gstack list * thm list * (bool*thm->thm);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   242
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   243
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   244
(*** References ***)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   245
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   246
(*Current assumption list -- set by "goal".*)
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32432
diff changeset
   247
val curr_prems = Unsynchronized.ref([] : thm list);
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   248
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   249
(*Return assumption list -- useful if you didn't save "goal"'s result. *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   250
fun premises() = !curr_prems;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   251
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   252
(*Current result maker -- set by "goal", used by "result".  *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   253
fun init_mkresult _ = error "No goal has been supplied in subgoal module";
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32432
diff changeset
   254
val curr_mkresult = Unsynchronized.ref (init_mkresult: bool*thm->thm);
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   255
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   256
(*List of previous goal stacks, for the undo operation.  Set by setstate.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   257
  A list of lists!*)
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32432
diff changeset
   258
val undo_list = Unsynchronized.ref([[(asm_rl, Seq.empty)]] : gstack list);
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   259
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   260
(* Stack of proof attempts *)
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32432
diff changeset
   261
val proofstack = Unsynchronized.ref([]: proof list);
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   262
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   263
(*reset all refs*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   264
fun reset_goals () =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   265
  (curr_prems := []; curr_mkresult := init_mkresult;
26429
1afbc0139b1b moved old the_context here;
wenzelm
parents: 26414
diff changeset
   266
    undo_list := [[(asm_rl, Seq.empty)]]);
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   267
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   268
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   269
(*** Setting up goal-directed proof ***)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   270
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   271
(*Generates the list of new theories when the proof state's theory changes*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   272
fun thy_error (thy,thy') =
33040
cffdb7b28498 removed old-style \ and \\ infixes
haftmann
parents: 32966
diff changeset
   273
  let val names = subtract (op =) (Context.display_names thy) (Context.display_names thy')
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   274
  in  case names of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   275
        [name] => "\nNew theory: " ^ name
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   276
      | _       => "\nNew theories: " ^ space_implode ", " names
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   277
  end;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   278
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   279
(*Default action is to print an error message; could be suppressed for
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   280
  special applications.*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   281
fun result_error_default state msg : thm =
32145
220c9e439d39 clarified pretty_goals, pretty_thm_aux: plain context;
wenzelm
parents: 32091
diff changeset
   282
  Pretty.str "Bad final proof state:" ::
32187
cca43ca13f4f renamed structure Display_Goal to Goal_Display;
wenzelm
parents: 32179
diff changeset
   283
      Goal_Display.pretty_goals_without_context (!goals_limit) state @
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   284
    [Pretty.str msg, Pretty.str "Proof failed!"] |> Pretty.chunks |> Pretty.string_of |> error;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   285
32738
15bb09ca0378 explicit indication of Unsynchronized.ref;
wenzelm
parents: 32432
diff changeset
   286
val result_error_fn = Unsynchronized.ref result_error_default;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   287
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   288
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   289
(*Common treatment of "goal" and "prove_goal":
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   290
  Return assumptions, initial proof state, and function to make result.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   291
  "atomic" indicates if the goal should be wrapped up in the function
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   292
  "Goal::prop=>prop" to avoid assumptions being returned separately.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   293
*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   294
fun prepare_proof atomic rths chorn =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   295
  let
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26429
diff changeset
   296
      val thy = Thm.theory_of_cterm chorn;
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26429
diff changeset
   297
      val horn = Thm.term_of chorn;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   298
      val _ = Term.no_dummy_patterns horn handle TERM (msg, _) => error msg;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   299
      val (As, B) = Logic.strip_horn horn;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   300
      val atoms = atomic andalso
27332
94790a9620c3 Logic.is_all;
wenzelm
parents: 27256
diff changeset
   301
            forall (fn t => not (can Logic.dest_implies t orelse Logic.is_all t)) As;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   302
      val (As,B) = if atoms then ([],horn) else (As,B);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   303
      val cAs = map (cterm_of thy) As;
26653
60e0cf6bef89 Thm.forall_elim_var(s);
wenzelm
parents: 26626
diff changeset
   304
      val prems = map (rewrite_rule rths o Thm.forall_elim_vars 0 o Thm.assume) cAs;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   305
      val cB = cterm_of thy B;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   306
      val st0 = let val st = Goal.init cB |> fold Thm.weaken cAs
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   307
                in  rewrite_goals_rule rths st end
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   308
      (*discharges assumptions from state in the order they appear in goal;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   309
        checks (if requested) that resulting theorem is equivalent to goal. *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   310
      fun mkresult (check,state) =
36944
dbf831a50e4a less pervasive names from structure Thm;
wenzelm
parents: 36863
diff changeset
   311
        let val state = Seq.hd (Thm.flexflex_rule state)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   312
                        handle THM _ => state   (*smash flexflex pairs*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   313
            val ngoals = nprems_of state
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   314
            val ath = implies_intr_list cAs state
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   315
            val th = Goal.conclude ath
26626
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26429
diff changeset
   316
            val thy' = Thm.theory_of_thm th
c6231d64d264 rep_cterm/rep_thm: no longer dereference theory_ref;
wenzelm
parents: 26429
diff changeset
   317
            val {hyps, prop, ...} = Thm.rep_thm th
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 33040
diff changeset
   318
            val final_th = Drule.export_without_context th
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   319
        in  if not check then final_th
26665
2e363edf7578 Theory.eq_thy;
wenzelm
parents: 26653
diff changeset
   320
            else if not (Theory.eq_thy(thy,thy')) then !result_error_fn state
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   321
                ("Theory of proof state has changed!" ^
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   322
                 thy_error (thy,thy'))
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   323
            else if ngoals>0 then !result_error_fn state
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   324
                (string_of_int ngoals ^ " unsolved goals!")
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   325
            else if not (null hyps) then !result_error_fn state
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   326
                ("Additional hypotheses:\n" ^
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   327
                 cat_lines (map (Syntax.string_of_term_global thy) hyps))
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   328
            else if Pattern.matches thy
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   329
                                    (Envir.beta_norm (term_of chorn), Envir.beta_norm prop)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   330
                 then final_th
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   331
            else  !result_error_fn state "proved a different theorem"
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   332
        end
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   333
  in
26665
2e363edf7578 Theory.eq_thy;
wenzelm
parents: 26653
diff changeset
   334
     if Theory.eq_thy(thy, Thm.theory_of_thm st0)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   335
     then (prems, st0, mkresult)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   336
     else error ("Definitions would change the proof state's theory" ^
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   337
                 thy_error (thy, Thm.theory_of_thm st0))
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   338
  end
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   339
  handle THM(s,_,_) => error("prepare_proof: exception THM was raised!\n" ^ s);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   340
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   341
(*Prints exceptions readably to users*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   342
fun print_sign_exn_unit thy e =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   343
  case e of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   344
     THM (msg,i,thms) =>
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   345
         (writeln ("Exception THM " ^ string_of_int i ^ " raised:\n" ^ msg);
32091
30e2ffbba718 proper context for Display.pretty_thm etc. or old-style versions Display.pretty_thm_global, Display.pretty_thm_without_context etc.;
wenzelm
parents: 32089
diff changeset
   346
          List.app (writeln o Display.string_of_thm_global thy) thms)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   347
   | THEORY (msg,thys) =>
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   348
         (writeln ("Exception THEORY raised:\n" ^ msg);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   349
          List.app (writeln o Context.str_of_thy) thys)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   350
   | TERM (msg,ts) =>
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   351
         (writeln ("Exception TERM raised:\n" ^ msg);
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   352
          List.app (writeln o Syntax.string_of_term_global thy) ts)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   353
   | TYPE (msg,Ts,ts) =>
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   354
         (writeln ("Exception TYPE raised:\n" ^ msg);
26939
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   355
          List.app (writeln o Syntax.string_of_typ_global thy) Ts;
1035c89b4c02 moved global pretty/string_of functions from Sign to Syntax;
wenzelm
parents: 26928
diff changeset
   356
          List.app (writeln o Syntax.string_of_term_global thy) ts)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   357
   | e => raise e;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   358
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   359
(*Prints an exception, then fails*)
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18120
diff changeset
   360
fun print_sign_exn thy e = (print_sign_exn_unit thy e; raise ERROR "");
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   361
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   362
(** the prove_goal.... commands
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   363
    Prove theorem using the listed tactics; check it has the specified form.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   364
    Augment theory with all type assignments of goal.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   365
    Syntax is similar to "goal" command for easy keyboard use. **)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   366
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   367
(*Version taking the goal as a cterm*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   368
fun prove_goalw_cterm_general check rths chorn tacsf =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   369
  let val (prems, st0, mkresult) = prepare_proof false rths chorn
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   370
      val tac = EVERY (tacsf prems)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   371
      fun statef() =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   372
          (case Seq.pull (tac st0) of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   373
               SOME(st,_) => st
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   374
             | _ => error ("prove_goal: tactic failed"))
32432
64f30bdd3ba1 modernized messages -- eliminated ctyp/cterm operations;
wenzelm
parents: 32231
diff changeset
   375
  in  mkresult (check, cond_timeit (!Output.timing) "" statef)  end;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   376
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   377
(*Two variants: one checking the result, one not.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   378
  Neither prints runtime messages: they are for internal packages.*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   379
fun prove_goalw_cterm rths chorn =
32966
5b21661fe618 indicate CRITICAL nature of various setmp combinators;
wenzelm
parents: 32957
diff changeset
   380
        setmp_CRITICAL Output.timing false (prove_goalw_cterm_general true rths chorn)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   381
and prove_goalw_cterm_nocheck rths chorn =
32966
5b21661fe618 indicate CRITICAL nature of various setmp combinators;
wenzelm
parents: 32957
diff changeset
   382
        setmp_CRITICAL Output.timing false (prove_goalw_cterm_general false rths chorn);
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   383
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   384
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   385
(*Version taking the goal as a string*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   386
fun prove_goalw thy rths agoal tacsf =
27256
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   387
  let val chorn = cterm_of thy (read_prop thy agoal)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   388
  in prove_goalw_cterm_general true rths chorn tacsf end
27256
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   389
  handle ERROR msg => cat_error msg (*from read_prop?*)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   390
                ("The error(s) above occurred for " ^ quote agoal);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   391
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   392
(*String version with no meta-rewrite-rules*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   393
fun prove_goal thy = prove_goalw thy [];
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   394
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   395
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   396
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   397
(*** Commands etc ***)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   398
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   399
(*Return the current goal stack, if any, from undo_list*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   400
fun getstate() : gstack = case !undo_list of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   401
      []   => error"No current state in subgoal module"
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   402
    | x::_ => x;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   403
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   404
(*Pops the given goal stack*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   405
fun pop [] = error"Cannot go back past the beginning of the proof!"
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   406
  | pop (pair::pairs) = (pair,pairs);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   407
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   408
23635
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   409
(* Print a level of the goal stack *)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   410
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   411
fun print_top ((th, _), pairs) =
23635
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   412
  let
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   413
    val n = length pairs;
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   414
    val m = (! goals_limit);
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   415
    val ngoals = nprems_of th;
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   416
  in
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   417
    [Pretty.str ("Level " ^ string_of_int n ^
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   418
      (if ngoals > 0 then " (" ^ string_of_int ngoals ^ " subgoal" ^
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   419
        (if ngoals <> 1 then "s" else "") ^ ")"
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   420
      else ""))] @
32187
cca43ca13f4f renamed structure Display_Goal to Goal_Display;
wenzelm
parents: 32179
diff changeset
   421
    Goal_Display.pretty_goals_without_context m th
23635
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   422
  end |> Pretty.chunks |> Pretty.writeln;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   423
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   424
(*Printing can raise exceptions, so the assignment occurs last.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   425
  Can do   setstate[(st,Seq.empty)]  to set st as the state.  *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   426
fun setstate newgoals =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   427
  (print_top (pop newgoals);  undo_list := newgoals :: !undo_list);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   428
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   429
(*Given a proof state transformation, return a command that updates
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   430
    the goal stack*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   431
fun make_command com = setstate (com (pop (getstate())));
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   432
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   433
(*Apply a function on proof states to the current goal stack*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   434
fun apply_fun f = f (pop(getstate()));
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   435
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   436
(*Return the top theorem, representing the proof state*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   437
fun topthm () = apply_fun  (fn ((th,_), _) => th);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   438
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   439
(*Return the final result.  *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   440
fun result () = !curr_mkresult (true, topthm());
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   441
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   442
(*Return the result UNCHECKED that it equals the goal -- for synthesis,
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   443
  answer extraction, or other instantiation of Vars *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   444
fun uresult () = !curr_mkresult (false, topthm());
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   445
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   446
(*Get subgoal i from goal stack*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   447
fun getgoal i = Logic.get_goal (prop_of (topthm())) i;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   448
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   449
(*Return subgoal i's hypotheses as meta-level assumptions.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   450
  For debugging uses of METAHYPS*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   451
local exception GETHYPS of thm list
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   452
in
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   453
fun gethyps i =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   454
    (METAHYPS (fn hyps => raise (GETHYPS hyps)) i (topthm());  [])
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   455
    handle GETHYPS hyps => hyps
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   456
end;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   457
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   458
(*Prints exceptions nicely at top level;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   459
  raises the exception in order to have a polymorphic type!*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   460
fun print_exn e = (print_sign_exn_unit (Thm.theory_of_thm (topthm())) e;  raise e);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   461
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   462
(*Which thms could apply to goal i? (debugs tactics involving filter_thms) *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   463
fun filter_goal could ths i = filter_thms could (999, getgoal i, ths);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   464
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   465
(*For inspecting earlier levels of the backward proof*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   466
fun chop_level n (pair,pairs) =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   467
  let val level = length pairs
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   468
  in  if n<0 andalso ~n <= level
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   469
      then  List.drop (pair::pairs, ~n)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   470
      else if 0<=n andalso n<= level
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   471
      then  List.drop (pair::pairs, level - n)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   472
      else  error ("Level number must lie between 0 and " ^
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   473
                   string_of_int level)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   474
  end;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   475
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   476
(*Print the given level of the proof; prlev ~1 prints previous level*)
23635
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   477
fun prlev n = apply_fun (print_top o pop o (chop_level n));
e31a814c080a removed obsolete disable_pr/enable_pr;
wenzelm
parents: 23239
diff changeset
   478
fun pr () = apply_fun print_top;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   479
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   480
(*Set goals_limit and print again*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   481
fun prlim n = (goals_limit:=n; pr());
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   482
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   483
(** the goal.... commands
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   484
    Read main goal.  Set global variables curr_prems, curr_mkresult.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   485
    Initial subgoal and premises are rewritten using rths. **)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   486
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   487
(*Version taking the goal as a cterm; if you have a term t and theory thy, use
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   488
    goalw_cterm rths (cterm_of thy t);      *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   489
fun agoalw_cterm atomic rths chorn =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   490
  let val (prems, st0, mkresult) = prepare_proof atomic rths chorn
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   491
  in  undo_list := [];
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   492
      setstate [ (st0, Seq.empty) ];
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   493
      curr_prems := prems;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   494
      curr_mkresult := mkresult;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   495
      prems
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   496
  end;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   497
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   498
val goalw_cterm = agoalw_cterm false;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   499
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   500
(*Version taking the goal as a string*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   501
fun agoalw atomic thy rths agoal =
27256
bcb071683184 added emulations for simple_read_term/read_term/read_prop (formerly in sign.ML);
wenzelm
parents: 27242
diff changeset
   502
    agoalw_cterm atomic rths (cterm_of thy (read_prop thy agoal))
18678
dd0c569fa43d sane ERROR handling;
wenzelm
parents: 18120
diff changeset
   503
    handle ERROR msg => cat_error msg (*from type_assign, etc via prepare_proof*)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   504
        ("The error(s) above occurred for " ^ quote agoal);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   505
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   506
val goalw = agoalw false;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   507
fun goal thy = goalw thy [];
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   508
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   509
(*now the versions that wrap the goal up in `Goal' to make it atomic*)
26429
1afbc0139b1b moved old the_context here;
wenzelm
parents: 26414
diff changeset
   510
fun Goalw thms s = agoalw true (ML_Context.the_global_context ()) thms s;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   511
val Goal = Goalw [];
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   512
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   513
(*simple version with minimal amount of checking and postprocessing*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   514
fun simple_prove_goal_cterm G f =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   515
  let
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   516
    val As = Drule.strip_imp_prems G;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   517
    val B = Drule.strip_imp_concl G;
20229
da4ec4b48be1 Assumption.assume;
wenzelm
parents: 19053
diff changeset
   518
    val asms = map Assumption.assume As;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   519
    fun check NONE = error "prove_goal: tactic failed"
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   520
      | check (SOME (thm, _)) = (case nprems_of thm of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   521
            0 => thm
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   522
          | i => !result_error_fn thm (string_of_int i ^ " unsolved goals!"))
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   523
  in
35021
c839a4c670c6 renamed old-style Drule.standard to Drule.export_without_context, to emphasize that this is in no way a standard operation;
wenzelm
parents: 33040
diff changeset
   524
    Drule.export_without_context (implies_intr_list As
36944
dbf831a50e4a less pervasive names from structure Thm;
wenzelm
parents: 36863
diff changeset
   525
      (check (Seq.pull (EVERY (f asms) (Thm.trivial B)))))
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   526
  end;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   527
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   528
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   529
(*Proof step "by" the given tactic -- apply tactic to the proof state*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   530
fun by_com tac ((th,ths), pairs) : gstack =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   531
  (case  Seq.pull(tac th)  of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   532
     NONE      => error"by: tactic failed"
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   533
   | SOME(th2,ths2) =>
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 22109
diff changeset
   534
       (if Thm.eq_thm(th,th2)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   535
          then warning "Warning: same as previous level"
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 22109
diff changeset
   536
          else if Thm.eq_thm_thy(th,th2) then ()
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   537
          else warning ("Warning: theory of proof state has changed" ^
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   538
                       thy_error (Thm.theory_of_thm th, Thm.theory_of_thm th2));
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   539
       ((th2,ths2)::(th,ths)::pairs)));
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   540
25685
c36e10812ae4 cond_timeit: added message argument;
wenzelm
parents: 23635
diff changeset
   541
fun by tac = cond_timeit (!Output.timing) ""
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   542
    (fn() => make_command (by_com tac));
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   543
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   544
(* byev[tac1,...,tacn] applies tac1 THEN ... THEN tacn.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   545
   Good for debugging proofs involving prove_goal.*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   546
val byev = by o EVERY;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   547
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   548
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   549
(*Backtracking means find an alternative result from a tactic.
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   550
  If none at this level, try earlier levels*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   551
fun backtrack [] = error"back: no alternatives"
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   552
  | backtrack ((th,thstr) :: pairs) =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   553
     (case Seq.pull thstr of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   554
          NONE      => (writeln"Going back a level..."; backtrack pairs)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   555
        | SOME(th2,thstr2) =>
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 22109
diff changeset
   556
           (if Thm.eq_thm(th,th2)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   557
              then warning "Warning: same as previous choice at this level"
22360
26ead7ed4f4b moved eq_thm etc. to structure Thm in Pure/more_thm.ML;
wenzelm
parents: 22109
diff changeset
   558
              else if Thm.eq_thm_thy(th,th2) then ()
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   559
              else warning "Warning: theory of proof state has changed";
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   560
            (th2,thstr2)::pairs));
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   561
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   562
fun back() = setstate (backtrack (getstate()));
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   563
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   564
(*Chop back to previous level of the proof*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   565
fun choplev n = make_command (chop_level n);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   566
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   567
(*Chopping back the goal stack*)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   568
fun chop () = make_command (fn (_,pairs) => pairs);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   569
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   570
(*Restore the previous proof state;  discard current state. *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   571
fun undo() = case !undo_list of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   572
      [] => error"No proof state"
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   573
    | [_] => error"Already at initial state"
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   574
    | _::newundo =>  (undo_list := newundo;  pr()) ;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   575
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   576
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   577
(*** Managing the proof stack ***)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   578
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   579
fun save_proof() = Proof(!undo_list, !curr_prems, !curr_mkresult);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   580
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   581
fun restore_proof(Proof(ul,prems,mk)) =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   582
 (undo_list:= ul;  curr_prems:= prems;  curr_mkresult := mk;  prems);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   583
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   584
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   585
fun top_proof() = case !proofstack of
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   586
        [] => error("Stack of proof attempts is empty!")
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   587
    | p::ps => (p,ps);
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   588
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   589
(*  push a copy of the current proof state on to the stack *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   590
fun push_proof() = (proofstack := (save_proof() :: !proofstack));
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   591
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   592
(* discard the top proof state of the stack *)
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   593
fun pop_proof() =
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   594
  let val (p,ps) = top_proof()
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   595
      val prems = restore_proof p
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   596
  in proofstack := ps;  pr();  prems end;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   597
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   598
(* rotate the stack so that the top element goes to the bottom *)
32173
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
   599
fun rotate_proof() =
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
   600
  let val (p,ps) = top_proof()
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
   601
  in proofstack := ps@[save_proof()];
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
   602
     restore_proof p;
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
   603
     pr();
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
   604
     !curr_prems
34f7b0fbe047 structure OldGoals: no pervasive names;
wenzelm
parents: 32145
diff changeset
   605
  end;
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   606
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   607
26414
2780de5a1422 moved bind_thm(s) to ML/ml_context.ML;
wenzelm
parents: 26291
diff changeset
   608
(** theorem bindings **)
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   609
26414
2780de5a1422 moved bind_thm(s) to ML/ml_context.ML;
wenzelm
parents: 26291
diff changeset
   610
fun qed name = ML_Context.ml_store_thm (name, result ());
2780de5a1422 moved bind_thm(s) to ML/ml_context.ML;
wenzelm
parents: 26291
diff changeset
   611
fun qed_goal name thy goal tacsf = ML_Context.ml_store_thm (name, prove_goal thy goal tacsf);
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   612
fun qed_goalw name thy rews goal tacsf =
26414
2780de5a1422 moved bind_thm(s) to ML/ml_context.ML;
wenzelm
parents: 26291
diff changeset
   613
  ML_Context.ml_store_thm (name, prove_goalw thy rews goal tacsf);
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   614
fun qed_spec_mp name =
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35237
diff changeset
   615
  ML_Context.ml_store_thm (name, Object_Logic.rulify_no_asm (result ()));
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   616
fun qed_goal_spec_mp name thy s p =
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35237
diff changeset
   617
  bind_thm (name, Object_Logic.rulify_no_asm (prove_goal thy s p));
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   618
fun qed_goalw_spec_mp name thy defs s p =
35625
9c818cab0dd0 modernized structure Object_Logic;
wenzelm
parents: 35237
diff changeset
   619
  bind_thm (name, Object_Logic.rulify_no_asm (prove_goalw thy defs s p));
18120
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   620
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   621
end;
41328805d4db renamed goals.ML to old_goals.ML;
wenzelm
parents:
diff changeset
   622