src/Tools/misc_legacy.ML
author wenzelm
Wed, 10 Aug 2011 20:53:43 +0200
changeset 44121 44adaa6db327
parent 42361 23f352990944
child 45195 63ce9e743734
permissions -rw-r--r--
old term operations are legacy;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
37781
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
     1
(*  Title:      Tools/misc_legacy.ML
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
     2
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
     3
Misc legacy stuff -- to be phased out eventually.
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
     4
*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
     5
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
     6
signature MISC_LEGACY =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
     7
sig
44121
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
     8
  val it_term_types: (typ * 'a -> 'a) -> term * 'a -> 'a
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
     9
  val add_term_names: term * string list -> string list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    10
  val add_typ_tvars: typ * (indexname * sort) list -> (indexname * sort) list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    11
  val add_typ_tfree_names: typ * string list -> string list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    12
  val add_typ_tfrees: typ * (string * sort) list -> (string * sort) list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    13
  val add_term_tvars: term * (indexname * sort) list -> (indexname * sort) list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    14
  val add_term_tfrees: term * (string * sort) list -> (string * sort) list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    15
  val add_term_tfree_names: term * string list -> string list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    16
  val typ_tfrees: typ -> (string * sort) list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    17
  val typ_tvars: typ -> (indexname * sort) list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    18
  val term_tfrees: term -> (string * sort) list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    19
  val term_tvars: term -> (indexname * sort) list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    20
  val add_term_vars: term * term list -> term list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    21
  val term_vars: term -> term list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    22
  val add_term_frees: term * term list -> term list
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    23
  val term_frees: term -> term list
37781
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    24
  val mk_defpair: term * term -> string * term
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    25
  val get_def: theory -> xstring -> thm
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    26
  val simple_read_term: theory -> typ -> string -> term
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    27
  val METAHYPS: (thm list -> tactic) -> int -> tactic
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    28
end;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    29
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    30
structure Misc_Legacy: MISC_LEGACY =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    31
struct
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    32
44121
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    33
(*iterate a function over all types in a term*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    34
fun it_term_types f =
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    35
let fun iter(Const(_,T), a) = f(T,a)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    36
      | iter(Free(_,T), a) = f(T,a)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    37
      | iter(Var(_,T), a) = f(T,a)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    38
      | iter(Abs(_,T,t), a) = iter(t,f(T,a))
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    39
      | iter(f$u, a) = iter(f, iter(u, a))
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    40
      | iter(Bound _, a) = a
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    41
in iter end
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    42
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    43
(*Accumulates the names in the term, suppressing duplicates.
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    44
  Includes Frees and Consts.  For choosing unambiguous bound var names.*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    45
fun add_term_names (Const(a,_), bs) = insert (op =) (Long_Name.base_name a) bs
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    46
  | add_term_names (Free(a,_), bs) = insert (op =) a bs
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    47
  | add_term_names (f$u, bs) = add_term_names (f, add_term_names(u, bs))
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    48
  | add_term_names (Abs(_,_,t), bs) = add_term_names(t,bs)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    49
  | add_term_names (_, bs) = bs;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    50
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    51
(*Accumulates the TVars in a type, suppressing duplicates.*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    52
fun add_typ_tvars(Type(_,Ts),vs) = List.foldr add_typ_tvars vs Ts
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    53
  | add_typ_tvars(TFree(_),vs) = vs
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    54
  | add_typ_tvars(TVar(v),vs) = insert (op =) v vs;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    55
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    56
(*Accumulates the TFrees in a type, suppressing duplicates.*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    57
fun add_typ_tfree_names(Type(_,Ts),fs) = List.foldr add_typ_tfree_names fs Ts
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    58
  | add_typ_tfree_names(TFree(f,_),fs) = insert (op =) f fs
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    59
  | add_typ_tfree_names(TVar(_),fs) = fs;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    60
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    61
fun add_typ_tfrees(Type(_,Ts),fs) = List.foldr add_typ_tfrees fs Ts
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    62
  | add_typ_tfrees(TFree(f),fs) = insert (op =) f fs
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    63
  | add_typ_tfrees(TVar(_),fs) = fs;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    64
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    65
(*Accumulates the TVars in a term, suppressing duplicates.*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    66
val add_term_tvars = it_term_types add_typ_tvars;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    67
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    68
(*Accumulates the TFrees in a term, suppressing duplicates.*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    69
val add_term_tfrees = it_term_types add_typ_tfrees;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    70
val add_term_tfree_names = it_term_types add_typ_tfree_names;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    71
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    72
(*Non-list versions*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    73
fun typ_tfrees T = add_typ_tfrees(T,[]);
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    74
fun typ_tvars T = add_typ_tvars(T,[]);
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    75
fun term_tfrees t = add_term_tfrees(t,[]);
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    76
fun term_tvars t = add_term_tvars(t,[]);
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    77
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    78
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    79
(*Accumulates the Vars in the term, suppressing duplicates.*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    80
fun add_term_vars (t, vars: term list) = case t of
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    81
    Var   _ => Ord_List.insert Term_Ord.term_ord t vars
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    82
  | Abs (_,_,body) => add_term_vars(body,vars)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    83
  | f$t =>  add_term_vars (f, add_term_vars(t, vars))
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    84
  | _ => vars;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    85
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    86
fun term_vars t = add_term_vars(t,[]);
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    87
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    88
(*Accumulates the Frees in the term, suppressing duplicates.*)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    89
fun add_term_frees (t, frees: term list) = case t of
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    90
    Free   _ => Ord_List.insert Term_Ord.term_ord t frees
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    91
  | Abs (_,_,body) => add_term_frees(body,frees)
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    92
  | f$t =>  add_term_frees (f, add_term_frees(t, frees))
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    93
  | _ => frees;
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    94
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    95
fun term_frees t = add_term_frees(t,[]);
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    96
44adaa6db327 old term operations are legacy;
wenzelm
parents: 42361
diff changeset
    97
37781
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    98
fun mk_defpair (lhs, rhs) =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
    99
  (case Term.head_of lhs of
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   100
    Const (name, _) =>
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   101
      (Long_Name.base_name name ^ "_def", Logic.mk_equals (lhs, rhs))
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   102
  | _ => raise TERM ("Malformed definition: head of lhs not a constant", [lhs, rhs]));
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   103
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   104
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   105
fun get_def thy = Thm.axiom thy o Name_Space.intern (Theory.axiom_space thy) o Thm.def_name;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   106
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   107
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   108
fun simple_read_term thy T s =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   109
  let
42361
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42284
diff changeset
   110
    val ctxt = Proof_Context.init_global thy
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42284
diff changeset
   111
      |> Proof_Context.allow_dummies
23f352990944 modernized structure Proof_Context;
wenzelm
parents: 42284
diff changeset
   112
      |> Proof_Context.set_mode Proof_Context.mode_schematic;
37781
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   113
    val parse = if T = propT then Syntax.parse_prop else Syntax.parse_term;
39288
f1ae2493d93f eliminated aliases of Type.constraint;
wenzelm
parents: 37781
diff changeset
   114
  in parse ctxt s |> Type.constraint T |> Syntax.check_term ctxt end;
37781
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   115
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   116
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   117
(**** METAHYPS -- tactical for using hypotheses as meta-level assumptions
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   118
       METAHYPS (fn prems => tac prems) i
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   119
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   120
converts subgoal i, of the form !!x1...xm. [| A1;...;An] ==> A into a new
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   121
proof state A==>A, supplying A1,...,An as meta-level assumptions (in
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   122
"prems").  The parameters x1,...,xm become free variables.  If the
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   123
resulting proof state is [| B1;...;Bk] ==> C (possibly assuming A1,...,An)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   124
then it is lifted back into the original context, yielding k subgoals.
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   125
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   126
Replaces unknowns in the context by Frees having the prefix METAHYP_
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   127
New unknowns in [| B1;...;Bk] ==> C are lifted over x1,...,xm.
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   128
DOES NOT HANDLE TYPE UNKNOWNS.
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   129
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   130
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   131
NOTE: This version does not observe the proof context, and thus cannot
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   132
work reliably.  See also Subgoal.SUBPROOF and Subgoal.FOCUS for
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   133
properly localized variants of the same idea.
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   134
****)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   135
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   136
local
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   137
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   138
(*Strips assumptions in goal yielding  ( [x1,...,xm], [H1,...,Hn], B )
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   139
    H1,...,Hn are the hypotheses;  x1...xm are variants of the parameters.
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   140
  Main difference from strip_assums concerns parameters:
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   141
    it replaces the bound variables by free variables.  *)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   142
fun strip_context_aux (params, Hs, Const ("==>", _) $ H $ B) =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   143
      strip_context_aux (params, H :: Hs, B)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   144
  | strip_context_aux (params, Hs, Const ("all",_) $ Abs (a, T, t)) =
42284
326f57825e1a explicit structure Syntax_Trans;
wenzelm
parents: 39288
diff changeset
   145
      let val (b, u) = Syntax_Trans.variant_abs (a, T, t)
37781
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   146
      in strip_context_aux ((b, T) :: params, Hs, u) end
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   147
  | strip_context_aux (params, Hs, B) = (rev params, rev Hs, B);
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   148
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   149
fun strip_context A = strip_context_aux ([], [], A);
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   150
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   151
(*Left-to-right replacements: ctpairs = [...,(vi,ti),...].
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   152
  Instantiates distinct free variables by terms of same type.*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   153
fun free_instantiate ctpairs =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   154
  forall_elim_list (map snd ctpairs) o forall_intr_list (map fst ctpairs);
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   155
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   156
fun free_of s ((a, i), T) =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   157
  Free (s ^ (case i of 0 => a | _ => a ^ "_" ^ string_of_int i), T)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   158
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   159
fun mk_inst v = (Var v, free_of "METAHYP1_" v)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   160
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   161
fun metahyps_split_prem prem =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   162
  let (*find all vars in the hyps -- should find tvars also!*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   163
      val hyps_vars = fold Term.add_vars (Logic.strip_assums_hyp prem) []
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   164
      val insts = map mk_inst hyps_vars
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   165
      (*replace the hyps_vars by Frees*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   166
      val prem' = subst_atomic insts prem
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   167
      val (params,hyps,concl) = strip_context prem'
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   168
  in (insts,params,hyps,concl)  end;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   169
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   170
fun metahyps_aux_tac tacf (prem,gno) state =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   171
  let val (insts,params,hyps,concl) = metahyps_split_prem prem
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   172
      val maxidx = Thm.maxidx_of state
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   173
      val cterm = Thm.cterm_of (Thm.theory_of_thm state)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   174
      val chyps = map cterm hyps
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   175
      val hypths = map Thm.assume chyps
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   176
      val subprems = map (Thm.forall_elim_vars 0) hypths
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   177
      val fparams = map Free params
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   178
      val cparams = map cterm fparams
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   179
      fun swap_ctpair (t,u) = (cterm u, cterm t)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   180
      (*Subgoal variables: make Free; lift type over params*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   181
      fun mk_subgoal_inst concl_vars (v, T) =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   182
          if member (op =) concl_vars (v, T)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   183
          then ((v, T), true, free_of "METAHYP2_" (v, T))
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   184
          else ((v, T), false, free_of "METAHYP2_" (v, map #2 params ---> T))
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   185
      (*Instantiate subgoal vars by Free applied to params*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   186
      fun mk_ctpair (v, in_concl, u) =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   187
          if in_concl then (cterm (Var v), cterm u)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   188
          else (cterm (Var v), cterm (list_comb (u, fparams)))
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   189
      (*Restore Vars with higher type and index*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   190
      fun mk_subgoal_swap_ctpair (((a, i), T), in_concl, u as Free (_, U)) =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   191
          if in_concl then (cterm u, cterm (Var ((a, i), T)))
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   192
          else (cterm u, cterm (Var ((a, i + maxidx), U)))
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   193
      (*Embed B in the original context of params and hyps*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   194
      fun embed B = list_all_free (params, Logic.list_implies (hyps, B))
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   195
      (*Strip the context using elimination rules*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   196
      fun elim Bhyp = implies_elim_list (forall_elim_list cparams Bhyp) hypths
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   197
      (*A form of lifting that discharges assumptions.*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   198
      fun relift st =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   199
        let val prop = Thm.prop_of st
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   200
            val subgoal_vars = (*Vars introduced in the subgoals*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   201
              fold Term.add_vars (Logic.strip_imp_prems prop) []
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   202
            and concl_vars = Term.add_vars (Logic.strip_imp_concl prop) []
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   203
            val subgoal_insts = map (mk_subgoal_inst concl_vars) subgoal_vars
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   204
            val st' = Thm.instantiate ([], map mk_ctpair subgoal_insts) st
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   205
            val emBs = map (cterm o embed) (prems_of st')
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   206
            val Cth  = implies_elim_list st' (map (elim o Thm.assume) emBs)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   207
        in  (*restore the unknowns to the hypotheses*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   208
            free_instantiate (map swap_ctpair insts @
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   209
                              map mk_subgoal_swap_ctpair subgoal_insts)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   210
                (*discharge assumptions from state in same order*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   211
                (implies_intr_list emBs
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   212
                  (forall_intr_list cparams (implies_intr_list chyps Cth)))
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   213
        end
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   214
      (*function to replace the current subgoal*)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   215
      fun next st = Thm.bicompose false (false, relift st, nprems_of st) gno state
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   216
  in Seq.maps next (tacf subprems (Thm.trivial (cterm concl))) end;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   217
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   218
fun print_vars_terms n thm =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   219
  let
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   220
    val thy = theory_of_thm thm
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   221
    fun typed s ty =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   222
      "  " ^ s ^ " has type: " ^ Syntax.string_of_typ_global thy ty;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   223
    fun find_vars (Const (c, ty)) =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   224
          if null (Term.add_tvarsT ty []) then I
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   225
          else insert (op =) (typed c ty)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   226
      | find_vars (Var (xi, ty)) =
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   227
          insert (op =) (typed (Term.string_of_vname xi) ty)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   228
      | find_vars (Free _) = I
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   229
      | find_vars (Bound _) = I
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   230
      | find_vars (Abs (_, _, t)) = find_vars t
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   231
      | find_vars (t1 $ t2) = find_vars t1 #> find_vars t2;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   232
    val prem = Logic.nth_prem (n, Thm.prop_of thm)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   233
    val tms = find_vars prem []
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   234
  in warning (cat_lines ("Found schematic vars in assumptions:" :: tms)) end;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   235
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   236
in
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   237
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   238
fun METAHYPS tacf n thm = SUBGOAL (metahyps_aux_tac tacf) n thm
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   239
  handle THM("assume: variables",_,_) => (print_vars_terms n thm; Seq.empty)
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   240
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   241
end;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   242
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   243
end;
2fbbf0a48cef moved misc legacy stuff from OldGoals to Misc_Legacy;
wenzelm
parents:
diff changeset
   244